Exemple #1
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));
        }
Exemple #2
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))
                       ));
        }