001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.dev.shell.mac;
017:
018: import com.google.gwt.dev.shell.CompilingClassLoader;
019: import com.google.gwt.dev.shell.JsValue;
020: import com.google.gwt.dev.shell.JsValueGlue;
021: import com.google.gwt.dev.shell.ModuleSpace;
022: import com.google.gwt.dev.shell.ModuleSpaceHost;
023: import com.google.gwt.dev.shell.mac.LowLevelSaf.DispatchObject;
024:
025: /**
026: * An implementation of {@link com.google.gwt.dev.shell.ModuleSpace} for Safari.
027: */
028: public class ModuleSpaceSaf extends ModuleSpace {
029:
030: private final int window;
031:
032: /**
033: * Constructs a browser interface for use with a global window object.
034: *
035: * @param moduleName name of the module
036: * @param key unique key for this instance of the module
037: */
038: public ModuleSpaceSaf(ModuleSpaceHost host, int scriptGlobalObject,
039: String moduleName, Object key) {
040: super (host, moduleName, key);
041:
042: // Hang on to the global execution state.
043: //
044: this .window = scriptGlobalObject;
045: LowLevelSaf.gcLock(scriptGlobalObject);
046: }
047:
048: public void createNative(String file, int line,
049: String jsniSignature, String[] paramNames, String js) {
050: // Execute the function definition within the browser, which will define
051: // a new top-level function.
052: //
053: String newScript = createNativeMethodInjector(jsniSignature,
054: paramNames, js);
055: LowLevelSaf.executeScriptWithInfo(LowLevelSaf
056: .getGlobalExecState(window), newScript, file, line);
057: }
058:
059: @Override
060: public void dispose() {
061: LowLevelSaf.gcUnlock(window, null);
062: super .dispose();
063: }
064:
065: /**
066: * Invokes a native JavaScript function.
067: *
068: * @param name the name of the function to invoke
069: * @param jthis the function's 'this' context
070: * @param types the type of each argument
071: * @param args the arguments to be passed
072: * @return the return value as a Object.
073: */
074: @Override
075: protected JsValue doInvoke(String name, Object jthis ,
076: Class<?>[] types, Object[] args) {
077: CompilingClassLoader isolatedClassLoader = getIsolatedClassLoader();
078:
079: JsValueSaf jsValueThis = new JsValueSaf();
080: Class<?> jthis Type = (jthis == null) ? Object.class : jthis
081: .getClass();
082: JsValueGlue.set(jsValueThis, isolatedClassLoader, jthis Type,
083: jthis );
084: int jsthis = jsValueThis.getJsValue();
085:
086: int argc = args.length;
087: int argv[] = new int[argc];
088: for (int i = 0; i < argc; ++i) {
089: JsValueSaf jsValue = new JsValueSaf();
090: JsValueGlue.set(jsValue, isolatedClassLoader, types[i],
091: args[i]);
092: argv[i] = jsValue.getJsValue();
093: }
094:
095: int curExecState = LowLevelSaf.getExecState();
096: int result = LowLevelSaf.invoke(curExecState, window, name,
097: jsthis , argv);
098: return new JsValueSaf(result);
099: }
100:
101: @Override
102: protected Object getStaticDispatcher() {
103: return new WebKitDispatchAdapter(getIsolatedClassLoader());
104: }
105:
106: protected int wrapObjectAsJSObject(Object o) {
107: if (o == null) {
108: return LowLevelSaf.jsNull();
109: }
110:
111: DispatchObject dispObj;
112: if (o instanceof DispatchObject) {
113: dispObj = (DispatchObject) o;
114: } else {
115: dispObj = new WebKitDispatchAdapter(
116: getIsolatedClassLoader(), o);
117: }
118: return LowLevelSaf.wrapDispatch(dispObj);
119: }
120: }
|