Пример #1
0
        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);
        }
Пример #2
0
        public AutoJSContext()
        {
            // obtain the JS runtime used by gecko
            nsIJSRuntimeService runtimeService = (nsIJSRuntimeService)Xpcom.GetService("@mozilla.org/js/xpc/RuntimeService;1");
            IntPtr jsRuntime = runtimeService.GetRuntimeAttribute();

            // 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);
        }
        /// <summary>
        /// Add hooks to listen for new JSContext creation and store the context for later use.
        /// This is the only way I have found of getting hold of the real JSContext
        /// </summary>
        protected void RecordNewJsContext()
        {
            Xpcom.AssertCorrectThread();

            // Add a hook to record when the a new Context is created.
            // If an existing hook exists, replace hook with one that
            // 1. call original hook
            // 2. reinstates original hook when done.

            _runtimeService = Xpcom.GetService<nsIJSRuntimeService>("@mozilla.org/js/xpc/RuntimeService;1");
            _jsRuntime = _runtimeService.GetRuntimeAttribute();

            _managedCallback = (IntPtr cx, UInt32 unitN) => { JSContext = cx; SpiderMonkey.JS_SetContextCallback(_jsRuntime, null); return 1; };

            _originalContextCallBack = SpiderMonkey.JS_SetContextCallback(_jsRuntime, _managedCallback);
            if (_originalContextCallBack != null)
            {
                RemoveJsContextCallBack();
                _managedCallback = (IntPtr cx, UInt32 unitN) => { JSContext = cx; SpiderMonkey.JS_SetContextCallback(_jsRuntime, _originalContextCallBack); return _originalContextCallBack(cx, unitN); };
                SpiderMonkey.JS_SetContextCallback(_jsRuntime, _managedCallback);
            }
        }