/// <summary> /// Creates a native function and assigns it as a property to this JS object. /// </summary> /// <param name="name">The name of the property to be defined or modified.</param> /// <param name="func">The function associated with the property.</param> /// <param name="argCount">The number of arguments the function expects to receive.</param> /// <param name="magic">A magic value.</param> /// <param name="data">An array containing data to be used by the method.</param> /// <param name="flags">A bitwise combination of the <see cref="JSPropertyFlags"/>.</param> /// <returns>true if the property has been defined or redefined; otherwise false.</returns> public unsafe bool DefineFunction(string name, JSCFunctionDataDelegate func, int argCount, int magic, JSValue[] data, JSPropertyFlags flags) { fixed(byte *aName = Utils.StringToManagedUTF8(name)) { return(DefinePropertyInternal(aName, Context.CreateFunctionRaw(func, argCount, magic, data), flags)); } }
/// <summary> /// Creates a new JavaScript function in this context. /// </summary> /// <param name="function">A delegate to the function that is to be exposed to JavaScript.</param> /// <param name="argCount">The number of arguments the function expects to receive.</param> /// <param name="magic">A magic value.</param> /// <param name="data">An array containing data to be used by the method.</param> /// <returns>A value containing the new function.</returns> public JSValue CreateConstructorRaw(JSCFunctionDataDelegate function, int argCount, int magic, JSValue[] data) { JSValue ctor = CreateFunctionRaw(function, argCount, magic, data); JS_SetConstructorBit(this.NativeInstance, ctor, true); return(ctor); }
/// <summary> /// Creates a new JavaScript function in this context. /// </summary> /// <param name="function">A delegate to the function that is to be exposed to JavaScript.</param> /// <param name="argCount">The number of arguments the function expects to receive.</param> /// <param name="magic">A magic value.</param> /// <param name="data">An array containing data to be used by the method.</param> /// <returns>A value containing the new function.</returns> public JSValue CreateFunctionRaw(JSCFunctionDataDelegate function, int argCount, int magic, JSValue[] data) { int dataLength = data is null ? 0 : data.Length; var fn = new QuickJSSafeDelegate(function, dataLength); JSValue fnValue = JS_NewCFunctionData(this.NativeInstance, fn.GetPointer(), argCount, magic, dataLength, data); if (JS_IsException(fnValue)) { _context.ThrowPendingException(); } else { _functions.Add(fn); } return(fnValue); }
/// <summary> /// Creates a native constructor function and assigns it as a property tothe global object. /// </summary> /// <param name="name">The name of the property to be defined or modified.</param> /// <param name="func">The function associated with the property.</param> /// <param name="argCount">The number of arguments the function expects to receive.</param> /// <param name="magic">A magic value.</param> /// <param name="data">An array containing data to be used by the method.</param> /// <param name="flags">A bitwise combination of the <see cref="JSPropertyFlags"/>.</param> /// <returns>true if the property has been defined or redefined; otherwise false.</returns> public unsafe bool DefineConstructor(string name, JSCFunctionDataDelegate func, int argCount, int magic, JSValue[] data, JSPropertyFlags flags) { fixed(byte *aName = Utils.StringToManagedUTF8(name)) { int rv = -1; JSValue globObj = JS_GetGlobalObject(this.NativeInstance); try { JSValue ctor = CreateFunctionRaw(func, argCount, magic, data); JS_SetConstructorBit(this.NativeInstance, ctor, true); rv = JS_DefinePropertyValueStr(this.NativeInstance, globObj, aName, ctor, flags & JSPropertyFlags.CWE); if (rv == -1) { this.NativeInstance.ThrowPendingException(); } } finally { JS_FreeValue(this.NativeInstance, globObj); } return(rv == 1); } }
public unsafe QuickJSSafeDelegate(JSCFunctionDataDelegate function, int dataLength) { _data_len = dataLength; _callback = function; _handler = sizeof(JSValue) == sizeof(ulong) ? (Delegate) new JSCFunctionData32(CFnData2Impl8) : new JSCFunctionData(CFnData2Impl16); }