public ComPtr <nsISupports> GetGlobalNsObject()
        {
            IntPtr globalObject = SpiderMonkey.CurrentGlobalOrNull(_cx);

            if (globalObject != IntPtr.Zero)
            {
                Guid guid = typeof(nsISupports).GUID;

                IntPtr pUnk = IntPtr.Zero;
                try
                {
                    pUnk = Xpcom.XPConnect.Instance.WrapJS(_cx, globalObject, ref guid);
                    object comObj = Xpcom.GetObjectForIUnknown(pUnk);
                    try
                    {
                        return(Xpcom.QueryInterface <nsISupports>(comObj).AsComPtr());
                    }
                    finally
                    {
                        Xpcom.FreeComObject(ref comObj);
                    }
                }
                finally
                {
                    if (pUnk != IntPtr.Zero)
                    {
                        Marshal.Release(pUnk);
                    }
                }
            }
            return(null);
        }
        private IntPtr GetGlobalObject()
        {
            IntPtr globalObject = SpiderMonkey.CurrentGlobalOrNull(_cx);

            if (globalObject == IntPtr.Zero)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }
            return(globalObject);
        }
Example #3
0
        /// <summary>
        /// Helper method which attempts to find the global object in a Context.
        /// </summary>
        /// <returns>the Global object ptr or Null/Zero ptr if not found.</returns>
        private IntPtr GetGlobalFromContext(IntPtr context)
        {
            IntPtr globalObject = SpiderMonkey.CurrentGlobalOrNull(context);

            if (globalObject == IntPtr.Zero)
            {
                globalObject = SpiderMonkey.DefaultObjectForContextOrNull(context);
                if (globalObject == IntPtr.Zero)
                {
                    return(IntPtr.Zero);
                }
            }

            return(globalObject);
        }
        /// <summary>
        /// Create a AutoJSContext using the SafeJSContext.
        /// If context is IntPtr.Zero use the SafeJSContext
        /// </summary>
        /// <param name="context"></param>
        public AutoJSContext(IntPtr context)
        {
            // We can't just use nsIXPConnect::GetSafeJSContext(); because its marked as [noxpcom, nostdcall]
            // TODO: Enhance IDL compiler to not generate methods for noxpcom, nostdcall tagged methods.
            if (context == IntPtr.Zero)
            {
                context = GlobalJSContextHolder.SafeJSContext;
            }

            IntPtr globalObject = SpiderMonkey.CurrentGlobalOrNull(context);

            if (globalObject == IntPtr.Zero)
            {
                globalObject = SpiderMonkey.DefaultObjectForContextOrNull(context);
                if (globalObject == IntPtr.Zero)
                {
                    throw new InvalidOperationException("JSContext don't store their default compartment object on the cx.");
                }
                _defaultCompartment = new JSAutoCompartment(context, globalObject);
            }

            _cx = context;
        }