Ejemplo n.º 1
0
 /// <summary>
 /// Gets the <see cref="QuickJSContext"/> associated with the specified <see cref="JSContext"/>.
 /// </summary>
 /// <param name="context">The pointer to the native JSContext.</param>
 /// <param name="wrapper">The <see cref="QuickJSContext"/> associated with the <paramref name="context"/> or null if not found.</param>
 /// <returns>true if wrapper is found; otherwise, false.</returns>
 public static bool TryWrap(JSContext context, out QuickJSContext wrapper)
 {
     lock (_AllContexts)
     {
         return(_AllContexts.TryGetValue(context, out wrapper));
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Creates a new JavaScript Array object.
 /// </summary>
 /// <param name="context">The context in which to create the new Array object.</param>
 /// <returns>The new JavaScript Array object.</returns>
 /// <exception cref="QuickJSException">Cannot create a new array.</exception>
 public static QuickJSValue CreateArray(QuickJSContext context)
 {
     if (context is null)
     {
         throw new ArgumentOutOfRangeException(nameof(context));
     }
     return(new QuickJSValue(context, JSValue.CreateArray(context.NativeInstance)));
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Determines that the specified <paramref name="context"/> and this context
 /// belong to the same runtime.
 /// </summary>
 /// <returns>true if contexts are compatible; otherwise, false.</returns>
 /// <remarks>
 /// There can be several JSContexts per JSRuntime and they can share objects,
 /// similar to frames of the same origin sharing JavaScript objects in a web
 /// browser.
 /// </remarks>
 public bool IsCompatibleWith(QuickJSContext context)
 {
     if (ReferenceEquals(context, this))
     {
         return(true);
     }
     return(!(context is null) && context.Runtime.NativeInstance == this.Runtime.NativeInstance);
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Parses the specified JSON string, constructing the JavaScript value or object described by the string.
        /// </summary>
        /// <param name="context">A pointer to the context in which to create the new object.</param>
        /// <param name="json">The string to parse as JSON.</param>
        /// <param name="filename">The name of the JSON file.</param>
        /// <returns>The <see cref="QuickJSValue"/> corresponding to the given JSON text.</returns>
        public static QuickJSValue FromJSON(QuickJSContext context, string json, string filename)
        {
            if (json == null)
            {
                return(null);
            }

            return(new QuickJSValue(context, JSValue.FromJSON(context.NativeInstance, json, filename)));
        }
Ejemplo n.º 5
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.º 6
0
        private QuickJSValue(QuickJSContext context, JSValue value)
        {
            if (context is null)
            {
                throw new ArgumentOutOfRangeException(nameof(context));
            }
            if (value.Tag != JSTag.Object)
            {
                throw new ArgumentOutOfRangeException(nameof(value));
            }

            _refcounted = new QuickJSRefcounted(context, value);
            context.AddValue(_refcounted);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Begins running a standard main loop which calls the user JS callbacks.
        /// </summary>
        /// <param name="context">The main context.</param>
        public void RunStdLoop(QuickJSContext context)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            JSRuntime runtime = JS_GetRuntime(context.NativeInstance);

            if (runtime != _runtime)
            {
                throw new ArgumentOutOfRangeException(nameof(context));
            }
            js_std_loop(context.NativeInstance);
        }
Ejemplo n.º 8
0
        private QuickJSValue(QuickJSContext context, JSValue value)
        {
            if (context is null)
            {
                throw new ArgumentOutOfRangeException(nameof(context));
            }
            if (value.Tag != JSTag.Object)
            {
                throw new ArgumentOutOfRangeException(nameof(value));
            }

            _context = context;
            _value   = value;
            _context.AddValue(this);
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Wraps a <see cref="JSValue"/> with a reference count.
 /// </summary>
 /// <param name="context">
 /// The context in which the <paramref name="value"/> was created.
 /// </param>
 /// <param name="value">A <see cref="JSValue"/> with a reference count.</param>
 /// <returns>
 /// An instance of the <see cref="QuickJSValue"/> that is the wrapper
 /// for the specified <paramref name="value"/>.
 /// </returns>
 public static QuickJSValue Wrap(QuickJSContext context, JSValue value)
 {
     return(new QuickJSValue(context, value));
 }
Ejemplo n.º 10
0
 public QuickJSRefcounted(QuickJSContext context, JSValue value)
 {
     Context          = context;
     this.NativeValue = value;
 }