Exemple #1
0
        public static unsafe Rooted <JS.Value> ManagedToNativeException(JSContextPtr cx, Exception managedException)
        {
            var errorRoot = NewError(cx, managedException.Message);
            var errorObj  = errorRoot.Value.AsObject;

            var existingStackRoot = new Rooted <JS.Value>(cx);

            JSAPI.GetProperty(
                cx, &errorObj, "stack", existingStackRoot
                );
            var existingStackText = existingStackRoot.Value.ToManagedString(cx);

            JSAPI.SetProperty(
                cx, &errorObj, "stack",
                new JSString(
                    cx,
                    managedException.StackTrace +
                    "\n//---- JS-to-native boundary ----//\n" +
                    existingStackText
                    )
                );

            if (managedException.InnerException != null)
            {
                var inner = ManagedToNativeException(cx, managedException.InnerException);

                JSAPI.SetProperty(cx, &errorObj, "innerException", inner);
            }

            return(errorRoot);
        }
Exemple #2
0
        public object ToManaged(JSContextPtr context)
        {
            switch (ValueType)
            {
            case JSValueType.DOUBLE:
                return(packed.f64);

            case JSValueType.INT32:
                return(packed.i32);

            case JSValueType.STRING:
                return(ToManagedString(context));

            case JSValueType.NULL:
                return(null);

            case JSValueType.BOOLEAN:
                return(packed.i32 != 0);

            case JSValueType.OBJECT:
                return(ToManagedObject(context));

            default:
                throw new NotImplementedException("Type '" + ValueType + "' not convertible");
            }
        }
Exemple #3
0
        public static JS.Value ManagedToNative(JSContextPtr cx, object value)
        {
            if (value == null)
            {
                return(JS.Value.Null);
            }

            var s = value as string;

            if (s != null)
            {
                var pString = JSAPI.NewStringCopy(cx, s);
                return(new JS.Value(pString));
            }

            var a = value as Array;

            if (a != null)
            {
                var va = new JS.ValueArray((uint)a.Length);
                for (int i = 0, l = a.Length; i < l; i++)
                {
                    va.Elements[i] = ManagedToNative(cx, a.GetValue(i));
                }

                JS.ValueArrayPtr vaPtr = va;
                var pArray             = JSAPI.NewArrayObject(cx, ref vaPtr);
                return(new JS.Value(pArray));
            }

            return((JS.Value)Activator.CreateInstance(typeof(JS.Value), value));
        }
 public static unsafe JSStringPtr NewStringCopy(
     JSContextPtr cx,
     string s
     ) {
     fixed(char *pChars = s)
     return(NewUCStringCopyN(cx, (IntPtr)pChars, (uint)s.Length));
 }
Exemple #5
0
        public unsafe Rooted <Value> InvokeFunction(
            JSContextPtr context,
            JSHandleObject thisReference,
            params Value[] arguments
            )
        {
            fixed(Value *pThis = &this)
            fixed(Value * pArgs = arguments)
            {
                var argsPtr    = new ValueArrayPtr((uint)arguments.Length, (IntPtr)pArgs);
                var resultRoot = new Rooted <Value>(context, Undefined);

                if (JSAPI.CallFunctionValue(
                        context, thisReference,
                        pThis,
                        ref argsPtr,
                        resultRoot
                        ))
                {
                    return(resultRoot);
                }

                resultRoot.Dispose();
                return(null);
            }
        }
        public static unsafe bool EvaluateScript(
            JSContextPtr cx,
            JSHandleObject scope,
            string chars,
            string filename,
            uint lineno,
            JSMutableHandleValue rval
            )
        {
            fixed(char *pChars = chars)
            fixed(char *pFilename = filename)
            {
                byte *pFilenameBytes = null;

                if (filename != null)
                {
                    byte *temp = stackalloc byte[filename.Length + 1];
                    pFilenameBytes = temp;

                    Encoding.ASCII.GetBytes(
                        pFilename, filename.Length,
                        pFilenameBytes, filename.Length
                        );
                }

                return(EvaluateUCScript(
                           cx, scope,
                           (IntPtr)pChars, chars.Length,
                           (IntPtr)pFilenameBytes, lineno,
                           rval
                           ));
            }
        }
Exemple #7
0
 public static JSObjectPtr CreateInstance(JSContextPtr context)
 {
     return(JSAPI.NewObject(
                context,
                JSClassPtr.Zero,
                JSHandleObject.Zero, JSHandleObject.Zero
                ));
 }
Exemple #8
0
 public JSCustomClass(JSContextPtr context, string name, JSHandleObject globalObject)
 {
     Context         = context;
     ClassDefinition = new JSClass(name);
     ClassPtr        = new JSClassPtr(ClassDefinition, out ClassPin);
     GlobalObject    = globalObject;
     SetConstructor(DefaultConstructor);
 }
Exemple #9
0
        /// <summary>
        /// Creates a JSString wrapper from a JS string pointer.
        /// If the pointer is zero, null is returned.
        /// </summary>
        public static JSString New(JSContextPtr context, JSStringPtr str)
        {
            if (str.IsZero)
            {
                return(null);
            }

            return(new JSString(context, str));
        }
Exemple #10
0
        public JSBool DefaultConstructor(JSContextPtr context, uint argc, JSCallArgumentsPtr vp)
        {
            // We have to invoke NewObjectForConstructor in order to construct a this-reference
            //  that has our class's prototype and .constructor values.
            var @this = NewObjectForConstructor(argc, vp);

            vp.Result = @this;
            return(true);
        }
Exemple #11
0
 public static JSObjectPtr CreateInstance(JSContextPtr context)
 {
     return(JSAPI.NewGlobalObject(
                context,
                DefaultClass, null,
                JSOnNewGlobalHookOption.DontFireOnNewGlobalHook,
                ref JSCompartmentOptions.Default
                ));
 }
 public static unsafe bool SetProperty(
     JSContextPtr cx,
     JSHandleObject obj,
     string name,
     JSHandleValue vp
     ) {
     fixed(char *pName = name)
     return(SetUCProperty(cx, obj, (IntPtr)pName, (uint)name.Length, vp));
 }
Exemple #13
0
        public JSObjectPtr InvokeConstructor(
            JSContextPtr context,
            params Value[] arguments
            )
        {
            var obj = AsObject;

            return(obj.InvokeConstructor(context, arguments));
        }
Exemple #14
0
        /// <summary>
        /// Creates a JSString wrapper from a JS value containing a string.
        /// If the value is null or undefined, null is returned.
        /// </summary>
        public static JSString New(JSContextPtr context, JS.Value str)
        {
            if (str.IsNullOrUndefined)
            {
                return(null);
            }

            return(new JSString(context, str.AsString));
        }
Exemple #15
0
 public JSArray(
     JSContextPtr context,
     JSObjectPtr obj
     ) : base(context, obj)
 {
     if (!JSAPI.IsArrayObject(context, Root))
     {
         throw new ArgumentException("Value is not an array", "obj");
     }
 }
        public static unsafe JSObjectPtr NewError(
            JSContextPtr cx,
            ref JS.ValueArrayPtr args
            )
        {
            var errorPrototype   = GetErrorPrototype(cx);
            var errorConstructor = GetConstructor(cx, &errorPrototype);

            return(New(cx, &errorConstructor, ref args));
        }
        public Rooted <JS.Value> InvokeFunction(
            JSContextPtr context,
            JSHandleObject thisReference,
            params JS.Value[] arguments
            )
        {
            var thisValue = new JS.Value(this);

            return(thisValue.InvokeFunction(context, thisReference, arguments));
        }
Exemple #18
0
        public unsafe bool IsArray(JSContextPtr context)
        {
            if (ValueType != JSValueType.OBJECT)
            {
                return(false);
            }

            var obj = this.AsObject;

            return(JSAPI.IsArrayObject(context, &obj));
        }
        /// <summary>
        /// Registers a managed function as a property on the target object.
        /// The managed function is wrapped automatically by a marshalling proxy.
        /// </summary>
        /// <returns>
        /// The function's marshalling proxy that must be retained as long as the function is available to JS.
        /// </returns>
        public Managed.NativeToManagedProxy DefineFunction(
            JSContextPtr context, string name, Delegate @delegate, uint attrs = 0
            )
        {
            var wrapped = new Managed.NativeToManagedProxy(@delegate);

            JSAPI.DefineFunction(
                context, TransientSelf(), name, wrapped.WrappedMethod, wrapped.ArgumentCount, attrs
                );
            return(wrapped);
        }
        // Bound via reflection
        private JSBool Invoke(JSContextPtr cx, uint argc, JSCallArgumentsPtr args)
        {
            // TODO: Marshal params arrays
            var managedArgs = new object[ArgumentCount];

            for (uint i = 0, l = Math.Min(ArgumentCount, argc); i < l; i++)
            {
                try {
                    managedArgs[i] = JSMarshal.NativeToManaged(cx, args[i]);
                } catch (Exception exc) {
                    var wrapped = new Exception(
                        "Argument #" + i + " could not be converted",
                        exc
                        );

                    JSMarshal.Throw(cx, wrapped);
                    return(false);
                }
            }

            object managedResult;

            try {
                managedResult = ManagedMethod.DynamicInvoke(managedArgs);
            } catch (Exception exc) {
                JSMarshal.Throw(cx, exc);
                return(false);
            }

            if (
                (ManagedMethod.Method.ReturnType == null) ||
                (ManagedMethod.Method.ReturnType.FullName == "System.Void")
                )
            {
                args.Result = JS.Value.Undefined;
                return(true);
            }

            JS.Value nativeResult;
            try {
                nativeResult = JSMarshal.ManagedToNative(cx, managedResult);
            } catch (Exception exc) {
                var wrapped = new Exception(
                    "Return value could not be converted",
                    exc
                    );

                JSMarshal.Throw(cx, wrapped);
                return(false);
            }

            args.Result = nativeResult;
            return(true);
        }
        /// <summary>
        /// Registers a managed JSNative as a property on the target object.
        /// The JSNative should return true on success and always set a result value.
        /// </summary>
        /// <returns>
        /// A pinning handle for the function that must be retained as long as the function is available to JS.
        /// </returns>
        public unsafe Managed.JSNativePin DefineFunction(
            JSContextPtr context, string name, JSNative call,
            uint nargs = 0, uint attrs = 0
            )
        {
            var wrapped = new Managed.JSNativePin(call);

            JSAPI.DefineFunction(
                context, TransientSelf(), name, wrapped.Target, nargs, attrs
                );
            return(wrapped);
        }
        public Rooted <JS.Value> GetProperty(JSContextPtr context, string name)
        {
            var result = new Rooted <JS.Value>(context);

            if (JSAPI.GetProperty(context, TransientSelf(), name, result))
            {
                return(result);
            }

            result.Dispose();
            return(null);
        }
 public static unsafe JSFunctionPtr DefineFunction(
     JSContextPtr cx,
     JSHandleObject obj,
     string name,
     JSNative call,
     uint nargs, uint attrs
     ) {
     fixed(char *pName = name)
     return(DefineUCFunction(
                cx, obj, (IntPtr)pName, (uint)name.Length, call, nargs, attrs
                ));
 }
Exemple #24
0
        public static JSBool TestNative(JSContextPtr cx, uint argc, JSCallArgumentsPtr vp)
        {
            if (argc < 1)
            {
                return(false);
            }

            var n = vp[0];

            vp.Result = new JS.Value((int)n * 2);
            return(true);
        }
        public unsafe JSObjectPtr InvokeConstructor(
            JSContextPtr context,
            params JS.Value[] arguments
            )
        {
            fixed(JSFunctionPtr *pThis = &this)
            fixed(JS.Value * pArgs = arguments)
            {
                var argsPtr = new JS.ValueArrayPtr((uint)arguments.Length, (IntPtr)pArgs);

                return(JSAPI.New(context, (JSObjectPtr *)pThis, ref argsPtr));
            }
        }
Exemple #26
0
        public JSObjectReference(
            JSContextPtr context,
            JSObjectPtr obj
            )
        {
            if (context.IsZero)
            {
                throw new ArgumentNullException("context");
            }

            Context = context;
            Root    = new Rooted <JSObjectPtr>(Context, obj);
        }
Exemple #27
0
        /// <summary>
        /// Registers a managed function as a property on the target object.
        /// The managed function is wrapped automatically by a marshalling proxy.
        /// </summary>
        /// <param name="autoRetain">If true, the marshalling proxy is automatically retained by the object wrapper.</param>
        /// <returns>
        /// The function's marshalling proxy that must be retained as long as the function is available to JS.
        /// </returns>
        public NativeToManagedProxy DefineFunction(
            JSContextPtr context, string name, Delegate @delegate, uint attrs = 0,
            bool autoRetain = true
            )
        {
            var result = Pointer.DefineFunction(Context, name, @delegate, attrs);

            if (autoRetain)
            {
                Retain(result);
            }
            return(result);
        }
Exemple #28
0
        /// <summary>
        /// Wraps existing reference and roots it.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="str"></param>
        public JSString(
            JSContextPtr context,
            JSStringPtr str
            )
        {
            if (context.IsZero)
            {
                throw new ArgumentNullException("context");
            }

            Context = context;
            Root    = new Rooted <JSStringPtr>(Context, str);
        }
Exemple #29
0
        /// <summary>
        /// Registers a managed JSNative as a property on the target object.
        /// The JSNative should return true on success and always set a result value.
        /// </summary>
        /// <param name="autoRetain">If true, the marshalling proxy is automatically retained by the object wrapper.</param>
        /// <returns>
        /// A pinning handle for the function that must be retained as long as the function is available to JS.
        /// </returns>
        public JSNativePin DefineFunction(
            JSContextPtr context, string name, JSNative call,
            uint nargs      = 0, uint attrs = 0,
            bool autoRetain = true
            )
        {
            var result = Pointer.DefineFunction(Context, name, @call, nargs, attrs);

            if (autoRetain)
            {
                Retain(result);
            }
            return(result);
        }
Exemple #30
0
        private static Rooted <JS.Value> NewError(JSContextPtr cx, params object[] errorArguments)
        {
            var jsErrorArgs = new JS.ValueArray((uint)errorArguments.Length);

            for (int i = 0; i < errorArguments.Length; i++)
            {
                jsErrorArgs.Elements[i] = ManagedToNative(cx, errorArguments[i]);
            }

            JS.ValueArrayPtr vaPtr = jsErrorArgs;
            return(new Rooted <JS.Value>(
                       cx, new JS.Value(JSAPI.NewError(cx, ref vaPtr))
                       ));
        }