public AutoJSContext() { nsIJSRuntimeService runtimeService = (nsIJSRuntimeService)Xpcom.GetService("@mozilla.org/js/xpc/RuntimeService;1"); IntPtr jsRuntime = runtimeService.GetRuntime(); IntPtr cx = JS_NewContext(jsRuntime, 8192); nsIJSContextStack contextStack = Xpcom.GetService <nsIJSContextStack>("@mozilla.org/js/xpc/ContextStack;1"); contextStack.Push(cx); nsIPrincipal system = Xpcom.GetService <nsIScriptSecurityManager>("@mozilla.org/scriptsecuritymanager;1").GetSystemPrincipal(); IntPtr jsPrincipals = system.GetJSPrincipals(cx); JSStackFrame frame = new JSStackFrame(); frame.script = JS_CompileScriptForPrincipals(cx, JS_GetGlobalObject(cx), jsPrincipals, "", 0, "", 1); //NOTE: this code is based on the definition of JSContext from mozilla 1.8 and will not work for other versions IntPtr old = Marshal.ReadIntPtr(cx, 0x34); frame.down = old; IntPtr framePtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(JSStackFrame))); Marshal.StructureToPtr(frame, framePtr, true); // cx->fp = framePtr; Marshal.WriteIntPtr(cx, 0x34, framePtr); }
public AutoJSContext() { // obtain the JS runtime used by gecko nsIJSRuntimeService runtimeService = (nsIJSRuntimeService)Xpcom.GetService("@mozilla.org/js/xpc/RuntimeService;1"); IntPtr jsRuntime = runtimeService.GetRuntime(); // create a new JSContext cx = JS_NewContext(jsRuntime, 8192); // begin a new request JS_BeginRequest(cx); // push the context onto the context stack nsIJSContextStack contextStack = Xpcom.GetService <nsIJSContextStack>("@mozilla.org/js/xpc/ContextStack;1"); contextStack.Push(cx); // obtain the system principal (no security checks) which we will use when compiling the empty script below nsIPrincipal system = Xpcom.GetService <nsIScriptSecurityManager>("@mozilla.org/scriptsecuritymanager;1").GetSystemPrincipal(); IntPtr jsPrincipals = system.GetJSPrincipals(cx); // create a fake stack frame JSStackFrame frame = new JSStackFrame(); frame.script = JS_CompileScriptForPrincipals(cx, JS_GetGlobalObject(cx), jsPrincipals, "", 0, "", 1); // put a pointer to the fake stack frame on the JSContext // frame.down = cx->fp IntPtr old = Marshal.ReadIntPtr(cx, OfsetOfFP); frame.down = old; IntPtr framePtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(JSStackFrame))); Marshal.StructureToPtr(frame, framePtr, true); // cx->fp = framePtr; Marshal.WriteIntPtr(cx, OfsetOfFP, framePtr); }