Ejemplo n.º 1
0
        public unsafe ClassDefinition(JSClassID id, JSClassCall call, JSClassGCMark gcMark, JSClassFinalizer finalizer)
        {
            if ((id.ToInt32() & 0xFFFF0000) != 0)             // JSObject.class_id is 16 bit unsigned integer.
            {
                throw new ArgumentOutOfRangeException(nameof(id));
            }

            JS_NewClassID(ref id);
            this.ID = id;
            if (call != null)
            {
                _callImpl = sizeof(JSValue) == 8 ? (Delegate) new JSClassCall32(CallImpl8) : new JSClassCall(CallImpl16);
                this.Call = call;
            }
            if (gcMark != null)
            {
                _markImpl = GCMarkImpl;
                this.Mark = gcMark;
            }
            if (finalizer != null)
            {
                _finalizerImpl = FinalizerImpl;
                this.Finalizer = finalizer;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Get the builtin class prototype object.
        /// </summary>
        /// <param name="id">A JavaScript class identifier.</param>
        public QuickJSValue GetClassPrototype(JSClassID id)
        {
            if (!Runtime.IsRegisteredClass(id))
            {
                throw new ArgumentOutOfRangeException(nameof(id));
            }
            JSValue proto = JS_GetClassProto(this.NativeInstance, id);

            return(proto.Tag == JSTag.Object ? QuickJSValue.Wrap(this, proto) : null);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Creates a new JavaScript object.
 /// </summary>
 /// <param name="context">The context in which to create the new object.</param>
 /// <param name="classId">The class ID.</param>
 /// <returns>The new JavaScript object.</returns>
 /// <exception cref="QuickJSException">Cannot create a new object.</exception>
 public static QuickJSValue Create(QuickJSContext context, JSClassID classId)
 {
     if (context is null)
     {
         throw new ArgumentOutOfRangeException(nameof(context));
     }
     if (!context.Runtime.IsRegisteredClass(classId))
     {
         throw new ArgumentOutOfRangeException(nameof(classId));
     }
     return(new QuickJSValue(context, JSValue.CreateObject(context.NativeInstance, classId)));
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Sets the class prototype object.
        /// </summary>
        /// <param name="id">A JavaScript class identifier.</param>
        /// <param name="proto">A prototype object or <see cref="JSValue.Null"/>.</param>
        public void SetClassPrototype(JSClassID id, JSValue proto)
        {
            JSTag tag = proto.Tag;

            if (tag != JSTag.Object && tag != JSTag.Null)
            {
                throw new ArgumentOutOfRangeException(nameof(proto));
            }
            if (!Runtime.IsRegisteredClass(id))
            {
                throw new ArgumentOutOfRangeException(nameof(id));
            }
            JS_SetClassProto(this.NativeInstance, id, proto);
        }
Ejemplo n.º 5
0
        public ExoticClassDefinition(JSClassID id, JSClassCall call, JSClassGCMark gcMark, JSClassFinalizer finalizer,
                                     JSGetOwnPropertyDelegate getOwnProperty, JSGetOwnPropertyNamesDelegate getOwnPropertyNames,
                                     JSDeletePropertyDelegate deleteProperty, JSDefineOwnPropertyDelegate defineOwnProperty,
                                     JSHasPropertyDelegate hasProperty, JSGetPropertyDelegate getProperty, JSSetPropertyDelegate setProperty)
            : base(id, call, gcMark, finalizer)
        {
            _callbacks.get_own_property       = getOwnProperty;
            _callbacks.get_own_property_names = getOwnPropertyNames;
            _callbacks.delete_property        = deleteProperty;
            _callbacks.define_own_property    = defineOwnProperty;
            _callbacks.has_property           = hasProperty;
            _callbacks.get_property           = getProperty;
            _callbacks.set_property           = setProperty;

            if (getOwnProperty != null)
            {
                _callbacksImpl.get_own_property = GetOwnPropertyImpl;
            }
            if (getOwnPropertyNames != null)
            {
                _callbacksImpl.get_own_property_names = new JSGetOwnPropertyNamesUnsafeDelegate(GetOwnPropertyNamesImpl);
            }
            if (deleteProperty != null)
            {
                _callbacksImpl.delete_property = DeletePropertyImpl;
            }
            if (defineOwnProperty != null)
            {
                _callbacksImpl.define_own_property = DefineOwnPropertyImpl;
            }
            if (hasProperty != null)
            {
                _callbacksImpl.has_property = HasPropertyImpl;
            }
            if (getProperty != null)
            {
                _callbacksImpl.get_property = sizeof(JSValue) == 8 ? (Delegate) new JSGetPropertyDelegate32(GetPropertyImpl8) : new JSGetPropertyDelegate(GetPropertyImpl16);
            }
            if (setProperty != null)
            {
                _callbacksImpl.set_property = SetPropertyImpl;
            }
            _exotic = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(JSClassExoticMethods)));
            Marshal.StructureToPtr(_callbacksImpl, _exotic, false);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Sets the class prototype object.
 /// </summary>
 /// <param name="id">A JavaScript class identifier.</param>
 /// <param name="proto">A prototype object or null.</param>
 /// <param name="disposeProto">true if <paramref name="proto"/> should be disposed of.</param>
 public void SetClassPrototype(JSClassID id, QuickJSValue proto, bool disposeProto)
 {
     if (!Runtime.IsRegisteredClass(id))
     {
         throw new ArgumentOutOfRangeException(nameof(id));
     }
     if (proto is null)
     {
         JS_SetClassProto(this.NativeInstance, id, JSValue.Null);
     }
     else
     {
         JS_SetClassProto(this.NativeInstance, id, proto.GetNativeInstance());
         if (disposeProto)
         {
             proto.Dispose();
         }
     }
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Determines whether a class with the specified ID is available in the given JavaScript runtime.
 /// </summary>
 /// <param name="id">The class ID.</param>
 /// <returns>true if a class with the specified ID is registered; otherwise, false.</returns>
 public bool IsRegisteredClass(JSClassID id)
 {
     return(JS_IsRegisteredClass(this.NativeInstance, id));
 }
Ejemplo n.º 8
0
        public static JSClassID CreateClassID()
        {
            var cid = new JSClassID();

            return(JS_NewClassID(ref cid));
        }