示例#1
0
        /// <summary>
        /// Evaluates a script or module source code from the specified file.
        /// </summary>
        /// <param name="path">The path to the file that contains source code.</param>
        /// <param name="encoding">The encoding applied to the contents of the file.</param>
        /// <param name="flags">A bitwise combination of the <see cref="JSEvalFlags"/>.</param>
        /// <returns>A value of the final expression as a CLR type.</returns>
        public object EvalFile(string path, Encoding encoding, QuickJSValue thisObj, JSEvalFlags flags)
        {
            if (thisObj is null)
            {
                throw new ArgumentNullException(nameof(thisObj));
            }

            try
            {
                if (encoding is UTF8Encoding || encoding is ASCIIEncoding)
                {
                    byte[] buffer;
                    using (FileStream fs = File.OpenRead(path))
                    {
                        buffer = new byte[fs.Length + 1];
                        fs.Read(buffer, 0, (int)fs.Length);
                        buffer[buffer.Length - 1] = 0;
                    }
                    return(EvalThisInternal(buffer, path, thisObj.NativeInstance, flags));
                }
                else
                {
                    return(ConvertJSValueToClrObject(EvalThisInternal(Utils.StringToManagedUTF8(File.ReadAllText(path, encoding)), path, thisObj.NativeInstance, flags), true));
                }
            }
            finally
            {
                GC.KeepAlive(thisObj);
            }
        }
示例#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);
        }
示例#3
0
        internal void AddValue(QuickJSValue value)
        {
#if NET20
            if (_values.Contains(value))
            {
                return;
            }
#endif
            _values.Add(value);
        }
示例#4
0
        /// <summary>
        /// Converts a JavaScript object or value to a JSON string, optionally replacing values if
        /// a <paramref name="replacer"/> function is specified or optionally including only
        /// the specified properties if a <paramref name="replacer"/> array is specified.
        /// </summary>
        /// <param name="replacer">
        /// A function that alters the behavior of the stringification process, or an array of String
        /// and Number that serve as a whitelist for selecting/filtering the properties of the value
        /// object to be included in the JSON string.<para/>
        /// If this value is null or not provided, all properties of the object are included in the
        /// resulting JSON string.
        /// </param>
        /// <param name="space">
        /// The string (or the first 10 characters of the string, if it&apos;s longer than that)
        /// is used as white space. If this parameter is null, no white space is used.
        /// </param>
        /// <returns>The JSON string representing this <see cref="JSValue"/>.</returns>
        public string ToJSON(QuickJSValue replacer, string space)
        {
            if (replacer is null)
            {
                return(NativeInstance.ToJSON(Context.NativeInstance, JSValue.Undefined, space));
            }

            if (!Context.IsCompatibleWith(replacer.Context))
            {
                throw new ArgumentOutOfRangeException(nameof(replacer));
            }

            return(NativeInstance.ToJSON(Context.NativeInstance, replacer.NativeInstance, space));
        }
示例#5
0
        /// <summary>
        /// Converts a JavaScript object or value to a JSON string, optionally replacing values if
        /// a <paramref name="replacer"/> function is specified or optionally including only
        /// the specified properties if a <paramref name="replacer"/> array is specified.
        /// </summary>
        /// <param name="replacer">
        /// A function that alters the behavior of the stringification process, or an array of String
        /// and Number that serve as a whitelist for selecting/filtering the properties of the value
        /// object to be included in the JSON string.<para/>
        /// If this value is null or not provided, all properties of the object are included in the
        /// resulting JSON string.
        /// </param>
        /// <param name="space">
        /// Indicates the number of space characters to use as white space;
        /// this number is capped at 10 (if it is greater, the value is just 10).
        /// Values less than 1 indicate that no space should be used.
        /// </param>
        /// <param name="json">
        /// When the method returns, the JSON string representing this <see cref="JSValue"/>.
        /// </param>
        /// <returns>true if the operation is successful; otherwise, false.</returns>
        public bool TryGetJSON(QuickJSValue replacer, int space, out string json)
        {
            if (replacer is null)
            {
                return(NativeInstance.TryGetJSON(Context.NativeInstance, JSValue.Undefined, space, out json));
            }

            if (!Context.IsCompatibleWith(replacer.Context))
            {
                throw new ArgumentOutOfRangeException(nameof(replacer));
            }

            return(NativeInstance.TryGetJSON(Context.NativeInstance, replacer.NativeInstance, space, out json));
        }
示例#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, 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));
     }
 }
示例#7
0
 /// <summary>
 /// Assigns a <see cref="QuickJSValue"/> value to a property of an object.
 /// </summary>
 /// <param name="index">The index of the property to set.</param>
 /// <param name="value">The value to assign to the property.</param>
 /// <returns>On success, returns true; otherwise, false.</returns>
 public bool SetProperty(int index, QuickJSValue value)
 {
     if (value is null)
     {
         return(SetProperty(index, JSValue.Null));
     }
     else
     {
         if (!Context.IsCompatibleWith(value.Context))
         {
             throw new ArgumentOutOfRangeException(nameof(value));
         }
         return(SetProperty(index, value.GetNativeInstance()));
     }
 }
示例#8
0
 /// <summary>
 /// Converts the specified <paramref name="value"/> to a <see cref="JSValue"/>.
 /// </summary>
 /// <param name="value">The value to convert.</param>
 /// <returns>
 /// A new <see cref="JSValue"/> whose value is equivalent to <paramref name="value"/>.
 /// </returns>
 /// <exception cref="InvalidCastException">
 /// This conversion is not supported.
 /// </exception>
 protected internal virtual JSValue ConvertClrObjectToJSValue(object value)
 {
     if (value is null)
     {
         return(JSValue.Null);
     }
     if (QuickJSValue.IsUndefined(value))
     {
         return(JSValue.Undefined);
     }
     if (value.GetType().IsPrimitive)
     {
     }
     throw new NotImplementedException();
 }
示例#9
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();
         }
     }
 }
示例#10
0
        /// <summary>
        /// Evaluates JavaScript code represented as a string.
        /// </summary>
        /// <param name="code">The JavaScript code.</param>
        /// <param name="filename">The path or URI to file where the script in question can be found, if any.</param>
        /// <param name="thisArg">The object to use as &apos;this&apos;.</param>
        /// <param name="flags">A bitwise combination of the <see cref="JSEvalFlags"/>.</param>
        /// <returns>A value of the final expression as a CLR type.</returns>
        public object Eval(string code, string filename, QuickJSValue thisArg, JSEvalFlags flags)
        {
            if (code == null)
            {
                throw new ArgumentNullException(nameof(code));
            }

            if (thisArg is null)
            {
                throw new ArgumentNullException(nameof(thisArg));
            }

            if (thisArg.Context != this)
            {
                throw new ArgumentOutOfRangeException(nameof(thisArg));
            }

            JSValue rv = EvalThisInternal(Utils.StringToManagedUTF8(code), filename, thisArg.NativeInstance, flags);

            GC.KeepAlive(thisArg);
            return(ConvertJSValueToClrObject(rv, true));
        }
示例#11
0
        /// <summary>
        /// Converts the value of the specified <see cref="JSValue"/> to a value of a .NET type.
        /// </summary>
        /// <param name="value">The <see cref="JSValue"/> to convert.</param>
        /// <param name="freeValue">
        /// A value indicating that the <see cref="JSValue"/> should be freed.
        /// </param>
        /// <returns>
        /// An <see cref="Object"/> whose value is equivalent to <paramref name="value"/>.
        /// </returns>
        /// <exception cref="InvalidCastException">
        /// This conversion is not supported.
        /// </exception>
        protected internal virtual object ConvertJSValueToClrObject(JSValue value, bool freeValue)
        {
            switch (value.Tag)
            {
            case JSTag.Bool:
                return(value.ToBoolean());

            case JSTag.Null:
                return(null);

            case JSTag.String:
                string s = value.ToString(this.NativeInstance);
                if (freeValue)
                {
                    JS_FreeValue(this.NativeInstance, value);
                }
                return(s);

            case JSTag.Int:
                return(value.ToInt32());

            case JSTag.Float64:
                return(value.ToDouble());

            case JSTag.Undefined:
                return(QuickJSValue.Undefined);

            default:
                if (value.Tag <= JSTag.Object)
                {
                    return(QuickJSValue.Wrap(this, value));
                }
                if (freeValue)
                {
                    JS_FreeValue(this.NativeInstance, value);
                }
                throw new InvalidCastException();
            }
        }
示例#12
0
 /// <summary>
 /// Invokes the function represented by the current instance.
 /// </summary>
 /// <param name="thisArg">
 /// The object on which to invoke the function; is &apos;this&apos;
 /// when the function executes.
 /// </param>
 /// <param name="args">
 /// The array of argument values to pass to the function.
 /// </param>
 /// <returns>
 /// An object containing the return value of the invoked function.
 /// </returns>
 public object Call(QuickJSValue thisArg, params JSValue[] args)
 {
     return(Call(thisArg is null ? JSValue.Null : thisArg.NativeInstance, args));
 }
示例#13
0
 /// <summary>
 /// Invokes the function represented by the current instance.
 /// </summary>
 /// <param name="thisArg">
 /// The object on which to invoke the function; is &apos;this&apos;
 /// when the function executes.
 /// </param>
 /// <returns>
 /// An object containing the return value of the invoked function.
 /// </returns>
 public object Call(QuickJSValue thisArg)
 {
     return(Call(thisArg is null ? JSValue.Null : thisArg.NativeInstance));
 }
示例#14
0
 /// <summary>
 /// Invokes the function represented by the current instance.
 /// </summary>
 /// <param name="thisArg">
 /// The object on which to invoke the function; is &apos;this&apos;
 /// when the function executes.
 /// </param>
 /// <returns>
 /// An object containing the return value of the invoked function.
 /// </returns>
 public object Call(QuickJSValue thisArg)
 {
     return(Call(thisArg is null ? JSValue.Null : thisArg._value));
 }
示例#15
0
 /// <summary>
 /// Converts the specified <paramref name="value"/> to a <see cref="JSValue"/>.
 /// </summary>
 /// <param name="value">The value to convert.</param>
 /// <returns>
 /// A new <see cref="JSValue"/> whose value is equivalent to <paramref name="value"/>.
 /// </returns>
 /// <exception cref="InvalidCastException">
 /// This conversion is not supported.
 /// </exception>
 protected internal virtual JSValue ConvertClrObjectToJSValue(object value)
 {
     if (value is null)
     {
         return(JSValue.Null);
     }
     if (QuickJSValue.IsUndefined(value))
     {
         return(JSValue.Undefined);
     }
     if (value.GetType().IsPrimitive)
     {
         if (value is bool bVal)
         {
             return(JSValue.Create(bVal));
         }
         if (value is double f64)
         {
             return(JSValue.Create(f64));
         }
         if (value is float f32)
         {
             return(JSValue.Create(f32));
         }
         if (value is int i32)
         {
             return(JSValue.Create(i32));
         }
         if (value is long i64)
         {
             return(JSValue.Create(i64));
         }
         if (value is byte i8)
         {
             return(JSValue.Create(i8));
         }
         if (value is short i16)
         {
             return(JSValue.Create(i16));
         }
         if (value is string s)
         {
             return(JSValue.Create(this.NativeInstance, s));
         }
         if (value is uint u32)
         {
             return(JSValue.Create(u32));
         }
         if (value is ulong u64)
         {
             return(JSValue.Create(u64));
         }
         if (value is uint u16)
         {
             return(JSValue.Create(u16));
         }
         if (value is uint u8)
         {
             return(JSValue.Create(u8));
         }
     }
     throw new NotImplementedException();
 }
示例#16
0
 /// <summary>
 /// Retrieves a context&apos;s global object.
 /// </summary>
 /// <returns>A context&apos;s global object.</returns>
 public QuickJSValue GetGlobal()
 {
     return(QuickJSValue.Wrap(this, JS_GetGlobalObject(this.NativeInstance)));
 }
示例#17
0
 internal void RemoveValue(QuickJSValue value)
 {
     _values.Remove(value);
 }