Beispiel #1
0
        public object Invoke(IntPtr funcPtr, IntPtr thisPtr, object[] args)
        {
            CheckDisposed();

            if (funcPtr == IntPtr.Zero)
            {
                throw new JsInteropException("wrapped V8 function is empty (IntPtr is Zero)");
            }

            JsValue a = new JsValue();

            if (args != null)
            {
                _convert.AnyToJsValue(args, ref a);
            }

            JsValue v = new JsValue();

            jscontext_invoke(_context, funcPtr, thisPtr, ref a, ref v);
            object res = _convert.FromJsValue(ref v);

            //
            a.Dispose();
            v.Dispose();

            //
            Exception e = res as JsException;

            if (e != null)
            {
                throw e;
            }
            return(res);
        }
Beispiel #2
0
        public object GetVariable(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            CheckDisposed();

            JsValue v = new JsValue();

            jscontext_get_variable(_context, name, ref v);
            object res = _convert.FromJsValue(ref v);

#if DEBUG_TRACE_API
            Console.WriteLine("Cleaning up return value get variable.");
#endif

            v.Dispose();
            Exception e = res as JsException;
            if (e != null)
            {
                throw e;
            }
            return(res);
        }
Beispiel #3
0
        public object Execute(JsScript script, TimeSpan?executionTimeout = null)
        {
            if (script == null)
            {
                throw new ArgumentNullException("script");
            }

            CheckDisposed();

            bool  executionTimedOut = false;
            Timer timer             = null;

            if (executionTimeout.HasValue)
            {
                timer          = new Timer(executionTimeout.Value.TotalMilliseconds);
                timer.Elapsed += (sender, args) =>
                {
                    timer.Stop();
                    executionTimedOut = true;
                    _engine.TerminateExecution();
                };
                timer.Start();
            }
            object res;

            try
            {
                JsValue v = new JsValue();
                jscontext_execute_script(_context, script.Handle, ref v);
                res = _convert.FromJsValue(ref v);
#if DEBUG_TRACE_API
                Console.WriteLine("Cleaning up return value from execution");
#endif
                v.Dispose();
            }
            finally
            {
                if (executionTimeout.HasValue)
                {
                    timer.Dispose();
                }
            }

            if (executionTimedOut)
            {
                throw new JsExecutionTimedOutException();
            }

            Exception e = res as JsException;
            if (e != null)
            {
                throw e;
            }
            return(res);
        }
Beispiel #4
0
        public object GetGlobal()
        {
            CheckDisposed();
            JsValue v = new JsValue();

            jscontext_get_global(_context, ref v);
            object res = _convert.FromJsValue(ref v);

            v.Dispose();
            Exception e = res as JsException;

            if (e != null)
            {
                throw e;
            }
            return(res);
        }
Beispiel #5
0
        public void SetVariable(string name, INativeScriptable proxy)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            CheckDisposed();
            JsValue a = new JsValue();
            JsValue b = new JsValue();

            _convert.ToJsValue(proxy, ref a);
            jscontext_set_variable(_context, name, ref a, ref b);
#if DEBUG_TRACE_API
            Console.WriteLine("Cleaning up return value from set variable");
#endif
            a.Dispose();
            b.Dispose();
        }
Beispiel #6
0
        public object InvokeProperty(JsObject obj, string name, object[] args)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            CheckDisposed();

            if (obj.Handle == IntPtr.Zero)
            {
                throw new JsInteropException("wrapped V8 object is empty (IntPtr is Zero)");
            }


            JsValue a = new JsValue(); // Null value unless we're given args.

            a.Type = JsValueType.Null;
            if (args != null)
            {
                _convert.AnyToJsValue(args, ref a);
            }

            JsValue v = new JsValue();

            jscontext_invoke_property(_context, obj.Handle, name, ref a, ref v);
            object res = _convert.FromJsValue(ref v);

            v.Dispose();
            a.Dispose();

            Exception e = res as JsException;

            if (e != null)
            {
                throw e;
            }
            return(res);
        }
Beispiel #7
0
        //----------------------------------------------------------------------------------------

        /// <summary>
        /// set variable with any value
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public void SetVariableFromAny(string name, object value)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            CheckDisposed();

            JsValue a = new JsValue();
            JsValue b = new JsValue();

            _convert.AnyToJsValue(value, ref a);
            jscontext_set_variable(_context, name, ref a, ref b);
#if DEBUG_TRACE_API
            Console.WriteLine("Cleaning up return value from set variable");
#endif
            a.Dispose();
            b.Dispose();
            // TODO: Check the result of the operation for errors.
        }
Beispiel #8
0
        public void SetPropertyValue(JsObject obj, string name, object value)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            CheckDisposed();

            if (obj.Handle == IntPtr.Zero)
            {
                throw new JsInteropException("wrapped V8 object is empty (IntPtr is Zero)");
            }

            JsValue a      = new JsValue();
            JsValue output = new JsValue();

            _convert.AnyToJsValue(value, ref a);
            jscontext_set_property_value(_context, obj.Handle, name, ref a, ref output);

            //TODO: review exceptio here
            //not need to convert all the time if we not have error
            object res = _convert.FromJsValue(ref output);

            output.Dispose();
            a.Dispose();
            Exception e = res as JsException;

            if (e != null)
            {
                throw e;
            }
        }
Beispiel #9
0
        public object GetPropertyValue(JsObject obj, string name)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            CheckDisposed();

            if (obj.Handle == IntPtr.Zero)
            {
                throw new JsInteropException("wrapped V8 object is empty (IntPtr is Zero)");
            }

            JsValue output = new JsValue();

            jscontext_get_property_value(_context, obj.Handle, name, ref output);
            //
            object res = _convert.FromJsValue(ref output);

            //TODO: review here
            //we should dispose only type that contains native data***

            output.Dispose();
            Exception e = res as JsException;

            if (e != null)
            {
                throw e;
            }
            return(res);
        }
Beispiel #10
0
        public IEnumerable <string> GetMemberNames(JsObject obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            CheckDisposed();

            if (obj.Handle == IntPtr.Zero)
            {
                throw new JsInteropException("wrapped V8 object is empty (IntPtr is Zero)");
            }


            JsValue v = new JsValue();

            jscontext_get_property_names(_context, obj.Handle, ref v);
            object res = _convert.FromJsValue(ref v);

            v.Dispose();
            Exception e = res as JsException;

            if (e != null)
            {
                throw e;
            }

            object[] arr    = (object[])res;
            string[] strArr = new string[arr.Length];
            for (int i = arr.Length - 1; i >= 0; --i)
            {
                strArr[i] = arr[i].ToString();
            }
            return(strArr);
        }
Beispiel #11
0
        public object Execute(string code, string name = null, TimeSpan?executionTimeout = null)
        {
            //Stopwatch watch1 = new Stopwatch();
            //Stopwatch watch2 = new Stopwatch();
            //watch1.Start();
            if (code == null)
            {
                throw new ArgumentNullException("code");
            }

            CheckDisposed();

            bool  executionTimedOut = false;
            Timer timer             = null;

            if (executionTimeout.HasValue)
            {
                timer          = new Timer(executionTimeout.Value.TotalMilliseconds);
                timer.Elapsed += (sender, args) =>
                {
                    timer.Stop();
                    executionTimedOut = true;
                    _engine.TerminateExecution();
                };
                timer.Start();
            }
            object res = null;

            try
            {
                //watch2.Start();
                JsValue output = new JsValue();
                jscontext_execute(_context, code, name ?? "<Unnamed Script>", ref output);

                //watch2.Stop();
                res = _convert.FromJsValue(ref output);
#if DEBUG_TRACE_API
                Console.WriteLine("Cleaning up return value from execution");
#endif
                output.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine("exec_err:" + ex.Message);
            }
            finally
            {
                if (executionTimeout.HasValue)
                {
                    timer.Dispose();
                }
            }

            if (executionTimedOut)
            {
                throw new JsExecutionTimedOutException();
            }

            Exception e = res as JsException;
            if (e != null)
            {
                throw e;
            }
            //watch1.Stop();
            // Console.WriteLine("Execution time " + watch2.ElapsedTicks + " total time " + watch1.ElapsedTicks);
            return(res);
        }