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; } }
/// <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); }
/// <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))); }
/// <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); }
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); }
/// <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(); } } }
/// <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)); }
public static JSClassID CreateClassID() { var cid = new JSClassID(); return(JS_NewClassID(ref cid)); }