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.moz;
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:
024: /**
025: * An implementation of {@link com.google.gwt.dev.shell.ModuleSpace} for
026: * Mozilla.
027: */
028: public class ModuleSpaceMoz extends ModuleSpace {
029:
030: private final int window;
031:
032: /**
033: * Constructs a browser interface for use with a Mozilla global window object.
034: */
035: public ModuleSpaceMoz(ModuleSpaceHost host, int scriptGlobalObject,
036: String moduleName, Object key) {
037: super (host, moduleName, key);
038:
039: // Hang on to the parent window.
040: //
041: window = scriptGlobalObject;
042: SwtGeckoGlue.addRefInt(window);
043: }
044:
045: /*
046: * (non-Javadoc)
047: *
048: * @see com.google.gwt.dev.shell.ShellJavaScriptHost#createNative(java.lang.String,
049: * int, java.lang.String, java.lang.String[], java.lang.String)
050: */
051: public void createNative(String file, int line,
052: String jsniSignature, String[] paramNames, String js) {
053: // Execute the function definition within the browser, which will define
054: // a new top-level function.
055: //
056: String newScript = createNativeMethodInjector(jsniSignature,
057: paramNames, js);
058: LowLevelMoz
059: .executeScriptWithInfo(window, newScript, file, line);
060: }
061:
062: /*
063: * (non-Javadoc)
064: *
065: * @see com.google.gwt.dev.shell.ModuleSpace#dispose()
066: */
067: @Override
068: public void dispose() {
069: SwtGeckoGlue.releaseInt(window);
070: super .dispose();
071: }
072:
073: /**
074: * Invokes a native JavaScript function.
075: *
076: * @param name the name of the function to invoke
077: * @param jthis the function's 'this' context
078: * @param types the type of each argument
079: * @param args the arguments to be passed
080: * @return the return value as a Object.
081: */
082: @Override
083: protected JsValue doInvoke(String name, Object jthis ,
084: Class<?>[] types, Object[] args) {
085: CompilingClassLoader isolatedClassLoader = getIsolatedClassLoader();
086:
087: JsValueMoz jsthis = new JsValueMoz();
088: Class<?> jthis Type = (jthis == null) ? Object.class : jthis
089: .getClass();
090: JsValueGlue.set(jsthis , isolatedClassLoader, jthis Type, jthis );
091:
092: int argc = args.length;
093: JsValueMoz argv[] = new JsValueMoz[argc];
094: int[] jsArgsInt = new int[argc];
095: for (int i = 0; i < argc; ++i) {
096: argv[i] = new JsValueMoz();
097: JsValueGlue.set(argv[i], isolatedClassLoader, types[i],
098: args[i]);
099: jsArgsInt[i] = argv[i].getJsRootedValue();
100: }
101: JsValueMoz returnVal = new JsValueMoz();
102: LowLevelMoz.invoke(window, name, jsthis .getJsRootedValue(),
103: jsArgsInt, returnVal.getJsRootedValue());
104: return returnVal;
105: }
106:
107: @Override
108: protected Object getStaticDispatcher() {
109: return new GeckoDispatchAdapter(getIsolatedClassLoader());
110: }
111: }
|