示例#1
0
        private Rooted(
            JSContext managedContext,
            JSContextPtr context,
            T value
            )
        {
            if (context.IsZero)
            {
                throw new ArgumentNullException("context");
            }

            // HACK: Attempt to locate a managed context for a raw pointer if that's all we have.
            // This lets us finalize safely.
            if (managedContext == null)
            {
                managedContext = JSContext.FromPointer(context);
            }

            if (managedContext != null)
            {
                ManagedContextReference = managedContext.WeakSelf;
            }
            else
            {
                ManagedContextReference = null;
            }

            Context = context;

            State = new _State(value);
            Pin   = GCHandle.Alloc(State, GCHandleType.Pinned);
            Root  = new JSRootPtr(Pin.AddrOfPinnedObject());

            if (!default(T).AddRoot(context, Root))
            {
                throw new Exception("Failed to add root");
            }
        }