1 16 package com.google.gwt.dev.shell.ie; 17 18 import com.google.gwt.dev.shell.JsValue; 19 import com.google.gwt.dev.shell.ModuleSpace; 20 import com.google.gwt.dev.shell.ModuleSpaceHost; 21 import com.google.gwt.dev.shell.ie.IDispatchImpl.HResultException; 22 23 import org.eclipse.swt.internal.ole.win32.IDispatch; 24 import org.eclipse.swt.ole.win32.OleAutomation; 25 import org.eclipse.swt.ole.win32.Variant; 26 27 31 public class ModuleSpaceIE6 extends ModuleSpace { 32 43 protected static Variant doInvokeOnWindow(OleAutomation window, String name, 44 Variant[] vArgs) { 45 OleAutomation funcObj = null; 46 Variant funcObjVar = null; 47 try { 48 49 int[] ids = window.getIDsOfNames(new String [] {name}); 52 if (ids == null) { 53 throw new RuntimeException ( 54 "Could not find a native method with the signature '" + name + "'"); 55 } 56 int functionId = ids[0]; 57 funcObjVar = window.getProperty(functionId); 58 funcObj = funcObjVar.getAutomation(); 59 int callDispId = funcObj.getIDsOfNames(new String [] {"call"})[0]; 60 61 return funcObj.invoke(callDispId, vArgs); 64 65 } finally { 66 if (funcObjVar != null) { 67 funcObjVar.dispose(); 68 } 69 70 if (funcObj != null) { 71 funcObj.dispose(); 72 } 73 } 74 } 75 76 private static int CODE(int hresult) { 78 return hresult & 0xFFFF; 79 } 80 82 private final OleAutomation window; 83 84 90 public ModuleSpaceIE6(ModuleSpaceHost host, IDispatch scriptFrameWindow, 91 String moduleName, Object key) { 92 super(host, moduleName, key); 93 94 window = new OleAutomation(scriptFrameWindow); 95 } 96 97 public void createNative(String file, int line, String jsniSignature, 98 String [] paramNames, String js) { 99 String newScript = createNativeMethodInjector(jsniSignature, paramNames, js); 103 try { 104 Variant result = execute(newScript); 106 if (result != null) { 107 result.dispose(); 108 } 109 } catch (RuntimeException e) { 110 throw new RuntimeException (file + "(" + line 111 + "): Failed to create JSNI method with signature '" + jsniSignature 112 + "'", e); 113 } 114 } 115 116 public void dispose() { 117 if (window != null) { 119 window.dispose(); 120 } 121 super.dispose(); 122 } 123 124 133 protected JsValue doInvoke(String name, Object jthis, Class [] types, 134 Object [] args) throws Throwable { 135 Variant[] vArgs = null; 136 try { 137 int len = args.length; 140 vArgs = new Variant[len + 1]; 141 JsValueIE6 jsValue = new JsValueIE6(); 142 jsValue.setWrappedJavaObject(getIsolatedClassLoader(), jthis); 143 vArgs[0] = jsValue.getVariant(); 144 145 for (int i = 0; i < len; ++i) { 146 vArgs[i + 1] = SwtOleGlue.convertObjectToVariant( 147 getIsolatedClassLoader(), types[i], args[i]); 148 } 149 150 Variant result = doInvokeOnWindow(window, name, vArgs); 151 try { 152 return new JsValueIE6(result); 153 } finally { 154 if (result != null) { 155 result.dispose(); 156 } 157 } 158 } finally { 159 for (int i = 0; i < vArgs.length; ++i) { 162 if (vArgs[i] != null) { 163 vArgs[i].dispose(); 164 } 165 } 166 } 167 } 168 169 protected Object getStaticDispatcher() { 170 return new IDispatchProxy(getIsolatedClassLoader()); 171 } 172 173 protected boolean isExceptionSame(Throwable original, int number, String name, String message) { 174 HResultException hre = new HResultException(original); 175 return CODE(hre.getHResult()) == CODE(number) && hre.getMessage().equals(message); 176 } 177 178 private Variant execute(String code) { 179 int[] dispIds = window.getIDsOfNames(new String [] {"execScript", "code"}); 180 Variant[] vArgs = new Variant[1]; 181 vArgs[0] = new Variant(code); 182 int[] namedArgs = new int[1]; 183 namedArgs[0] = dispIds[1]; 184 Variant result = window.invoke(dispIds[0], vArgs, namedArgs); 185 vArgs[0].dispose(); 186 if (result == null) { 187 String lastError = window.getLastError(); 188 throw new RuntimeException ("Error (" + lastError 189 + ") executing JavaScript:\n" + code); 190 } 191 return result; 192 } 193 } 194 | Popular Tags |