Beispiel #1
0
        public virtual NSJSValue Call(IEnumerable <NSJSValue> args, out NSJSException exception)
        {
            NSJSObject owner = this.m_This;

            if (owner == null)
            {
                throw new InvalidOperationException("this");
            }
            NSJSException exception_info = null;
            NSJSValue     result         = this.Call(args, (argc) =>
            {
                NSJSVirtualMachine machine = this.VirtualMachine;
                fixed(IntPtr * argv        = argc.ToArray())
                {
                    IntPtr handle = nsjs_localvalue_object_property_call(machine.Isolate,
                                                                         owner.Handle,
                                                                         this.Handle,
                                                                         argc.Count, argv,
                                                                         ref *machine.exception);
                    exception_info = NSJSException.From(machine, machine.exception);
                    if (handle == NULL)
                    {
                        return(null);
                    }
                    return(NSJSValueBuilder.From(handle, this.VirtualMachine));
                }
            });

            exception = exception_info;
            return(result);
        }
Beispiel #2
0
        public virtual string Eval(string expression, out NSJSException exception)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }
            if (string.IsNullOrEmpty(expression))
            {
                throw new ArgumentException("expression");
            }
            NSJSException exception_info = null;
            string        result         = null;

            this.Executing(() =>
            {
                byte[] ch     = NSJSString.GetUTF8StringBuffer(expression);
                fixed(byte *s = ch)
                {
                    sbyte *p       = nsjs_virtualmachine_eval(this.Handle, s, ref *this.exception);
                    exception_info = NSJSException.From(this, this.exception);
                    result         = p != null ? new string(p) : null;
                    NSJSMemoryManagement.Free(p);
                    return(result);
                }
            });
            exception = exception_info;
            return(result);
        }
Beispiel #3
0
        public virtual string Run(string source, string alias, out NSJSException exception)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            NSJSException exception_info = null;
            string        result         = null;

            try
            {
                lock (this)
                {
                    RunContext context = new RunContext();
                    if (this.runings.TryGetValue(source, out context))
                    {
                        result         = context.result;
                        exception_info = context.exception;
                        return(result);
                    }
                }
                result = this.Executing((() =>
                {
                    byte[] alias_buffer = null;
                    if (alias != null)
                    {
                        alias_buffer = NSJSString.GetUTF8StringBuffer(alias);
                    }
                    byte[] source_buffer = NSJSString.GetUTF8StringBuffer(source);
                    fixed(byte *pstr_source = source_buffer)
                    {
                        fixed(byte *pstr_alias = alias_buffer)
                        {
                            sbyte *pstr_result = nsjs_virtualmachine_run(this.Handle,
                                                                         pstr_source, pstr_alias, ref *this.exception);
                            string result_str = pstr_result != null ? new string(pstr_result) : null;
                            NSJSMemoryManagement.Free(pstr_result);
                            exception_info = NSJSException.From(this, this.exception);
                            return(result_str);
                        }
                    }
                }));
                lock (this)
                {
                    RunContext context = new RunContext()
                    {
                        exception = exception_info,
                        result    = result,
                        alias     = alias,
                        source    = source
                    };
                    this.runings.Add(source, context);
                }
                return(result);
            }
            finally
            {
                exception = exception_info;
            }
        }
Beispiel #4
0
        public virtual string Call(string name, IEnumerable <NSJSValue> args, out NSJSException exception)
        {
            NSJSException exception_info = null;
            string        result         = null;

            this.InternalCall(name, args, (List <IntPtr> argc) =>
            {
                fixed(byte *key = NSJSString.GetUTF8StringBuffer(name))
                {
                    fixed(IntPtr * argv = argc.ToArray())
                    {
                        sbyte *chunk   = nsjs_virtualmachine_call(this.Handle, key, argc.Count, argv, ref *this.exception);
                        exception_info = NSJSException.From(this, this.exception);
                        result         = chunk != null ? new string(chunk) : null;
                        NSJSMemoryManagement.Free(chunk);
                        return(result);
                    }
                }
            });
            exception = exception_info;
            return(result);
        }
Beispiel #5
0
        public virtual NSJSValue Callvir(string name, IEnumerable <NSJSValue> args, out NSJSException exception)
        {
            NSJSException exception_info = null;
            NSJSValue     result         = this.InternalCall(name, args, (List <IntPtr> argc) =>
            {
                fixed(byte *key = NSJSString.GetUTF8StringBuffer(name))
                {
                    fixed(IntPtr * argv = argc.ToArray())
                    {
                        IntPtr handle  = nsjs_virtualmachine_callvir(this.Handle, key, argc.Count, argv, ref *this.exception);
                        exception_info = NSJSException.From(this, this.exception);
                        if (handle == NULL)
                        {
                            return(null);
                        }
                        return(NSJSValueBuilder.From(handle, this));
                    }
                }
            });

            exception = exception_info;
            return(result);
        }
Beispiel #6
0
        public virtual string Call(string name, IEnumerable <string> args, out NSJSException exception)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (name.Length <= 0)
            {
                throw new ArgumentException("name");
            }
            NSJSException exception_info = null;
            string        result         = null;

            this.Executing <string>(() =>
            {
                IList <GCHandle?> cookies = new List <GCHandle?>(256);
                IList <IntPtr> arguments  = new List <IntPtr>(256);
                cookies.Add(GCHandle.Alloc(NSJSString.GetUTF8StringBuffer(name), GCHandleType.Pinned));
                int argc = 0;
                if (args != null)
                {
                    foreach (string s in args)
                    {
                        byte[] ch = null;
                        if (s != null)
                        {
                            ch = NSJSString.GetUTF8StringBuffer(s);
                        }
                        if (s == null || ch.Length <= 0)
                        {
                            cookies.Add(null);
                            arguments.Add(NULL);
                        }
                        else
                        {
                            GCHandle cookie = GCHandle.Alloc(ch, GCHandleType.Pinned);
                            cookies.Add(cookie);
                            arguments.Add(cookie.AddrOfPinnedObject());
                        }
                        argc++;
                    }
                }
                void **argv = null;
                if (argc > 0)
                {
                    void **ppv = stackalloc void *[argc];
                    for (int i = 0; i < argc; i++)
                    {
                        ppv[i] = arguments[i].ToPointer();
                    }
                    argv = ppv;
                }
                IntPtr chunk   = nsjs_virtualmachine_call2(this.Handle, cookies[0].Value.AddrOfPinnedObject().ToPointer(), argc, argv, ref *this.exception);
                exception_info = NSJSException.From(this, this.exception);
                result         = chunk != NULL ? new string((sbyte *)chunk.ToPointer()) : null;
                foreach (GCHandle?cookie in cookies)
                {
                    if (cookie == null)
                    {
                        continue;
                    }
                    cookie.Value.Free();
                }
                return(result);
            });
            exception = exception_info;
            return(result);
        }