Ejemplo n.º 1
0
 public unsafe bool DefineProperty(string name, JSValue value, JSPropertyFlags flags)
 {
     fixed(byte *aName = Utils.StringToManagedUTF8(name))
     {
         return(DefinePropertyInternal(aName, value, flags));
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Defines a new property directly on an object, or modifies an existing property on an object.
 /// </summary>
 /// <param name="name">The name of the property to be defined or modified.</param>
 /// <param name="value">The value associated with the property.</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 bool DefineProperty(string name, QuickJSValue value, JSPropertyFlags flags)
 {
     if (value is null)
     {
         return(DefineProperty(name, JSValue.Null, flags));
     }
     else
     {
         if (!Context.IsCompatibleWith(value.Context))
         {
             throw new ArgumentOutOfRangeException(nameof(value));
         }
         return(DefineProperty(name, value.GetNativeInstance(), flags));
     }
 }
Ejemplo n.º 3
0
        private unsafe bool DefinePropertyInternal(byte *name, JSValue value, JSPropertyFlags flags)
        {
            if (name == null)
            {
                JS_FreeValue(Context.NativeInstance, value);
                throw new ArgumentNullException(nameof(name));
            }

            int rv = JS_DefinePropertyValueStr(Context.NativeInstance, this.NativeInstance, name, value, flags & JSPropertyFlags.CWE);

            if (rv == -1)
            {
                Context.NativeInstance.ThrowPendingException();
            }
            return(rv == 1);
        }
Ejemplo n.º 4
0
        private unsafe bool DefinePropertyInternal(byte *name, JSValue getter, JSValue setter, JSPropertyFlags flags)
        {
            JSContext context = Context.NativeInstance;

            if (name == null)
            {
                JS_FreeValue(context, getter);
                JS_FreeValue(context, setter);
                throw new ArgumentNullException(nameof(name));
            }

            JSAtom prop = JS_NewAtom(context, name);
            int    rv   = JS_DefinePropertyGetSet(context, this.NativeInstance, prop, getter, setter, flags & JSPropertyFlags.CWE);

            JS_FreeAtom(context, prop);
            if (rv == -1)
            {
                context.ThrowPendingException();
            }
            return(rv == 1);
        }
Ejemplo n.º 5
0
        public unsafe bool DefineProperty(string name, JSCFunction getter, JSCFunction setter, JSPropertyFlags flags)
        {
            JSValue getterVal, setterVal;

            getterVal = getter is null ? JSValue.Undefined : Context.CreateFunctionRaw("get_" + name, getter, 0);
            try
            {
                setterVal = setter is null ? JSValue.Undefined : Context.CreateFunctionRaw("set_" + name, setter, 1);
            }
            catch
            {
                JS_FreeValue(Context.NativeInstance, getterVal);
                throw;
            }

            fixed(byte *aName = Utils.StringToManagedUTF8(name))
            {
                return(DefinePropertyInternal(aName, getterVal, setterVal, flags));
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Defines a new property directly on an object, or modifies an existing property on an object.
 /// </summary>
 /// <param name="name">The name of the property to be defined or modified.</param>
 /// <param name="value">The value associated with the property.</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 bool DefineProperty(string name, string value, JSPropertyFlags flags)
 {
     return(DefineProperty(name, JSValue.Create(Context.NativeInstance, value), flags));
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Defines a new property directly on an object, or modifies an existing property on an object.
 /// </summary>
 /// <param name="name">The name of the property to be defined or modified.</param>
 /// <param name="value">The value associated with the property.</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 bool DefineProperty(string name, bool value, JSPropertyFlags flags)
 {
     return(DefineProperty(name, JS_NewBool(Context.NativeInstance, value), flags));
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Defines a new property directly on an object, or modifies an existing property on an object.
 /// </summary>
 /// <param name="name">The name of the property to be defined or modified.</param>
 /// <param name="value">The value associated with the property.</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 bool DefineProperty(string name, double value, JSPropertyFlags flags)
 {
     return(DefineProperty(name, JS_NewFloat64(Context.NativeInstance, value), flags));
 }
Ejemplo n.º 9
0
 /// <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));
     }
 }
Ejemplo n.º 10
0
 /// <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="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, JSCFunction func, int argCount, JSPropertyFlags flags)
 {
     fixed(byte *aName = Utils.StringToManagedUTF8(name))
     {
         return(DefinePropertyInternal(aName, Context.CreateFunctionRawInternal(aName, func, argCount), flags));
     }
 }
Ejemplo n.º 11
0
 private int SetPropertyImpl(JSContext cx, JSValue obj, JSAtom prop, JSValue value, JSValue receiver, JSPropertyFlags flags)
 {
     try
     {
         return(_callbacks.set_property(cx, obj, prop, value, receiver, flags));
     }
     catch (OutOfMemoryException)
     {
         JS_ThrowOutOfMemory(cx);
         return(-1);
     }
     catch (Exception ex)
     {
         Utils.ReportException(cx, ex);
         return(-1);
     }
 }
Ejemplo n.º 12
0
 private int DefineOwnPropertyImpl(JSContext cx, JSValue thisObj, JSAtom prop, JSValue val, JSValue getter, JSValue setter, JSPropertyFlags flags)
 {
     try
     {
         return(_callbacks.define_own_property(cx, thisObj, prop, val, getter, setter, flags));
     }
     catch (OutOfMemoryException)
     {
         JS_ThrowOutOfMemory(cx);
         return(-1);
     }
     catch (Exception ex)
     {
         Utils.ReportException(cx, ex);
         return(-1);
     }
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Defines a new property directly on an object, or modifies an existing property on an object.
 /// </summary>
 /// <param name="name">The name of the property to be defined or modified.</param>
 /// <param name="value">The value associated with the property.</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 bool DefineProperty(string name, long value, JSPropertyFlags flags)
 {
     return(DefineProperty(name, JS_NewInt64(_context.NativeInstance, value), flags));
 }
Ejemplo n.º 14
0
        /// <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);
            }
        }