Пример #1
0
        protected internal virtual IntPtr GetPropertyAndReturnHandle(string key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (key.Length <= 0)
            {
                throw new ArgumentException("key");
            }
            byte[] buffer = NSJSString.GetUTF8StringBuffer(key);
            if (buffer == null || buffer.Length <= 0)
            {
                return(NULL);
            }

            fixed(byte *sz = buffer)
            {
                if (*sz == '\x0')
                {
                    return(NULL);
                }
                return(nsjs_localvalue_object_property_get(this.Isolate, this.Handle, sz));
            }
        }
Пример #2
0
        public static void Throw(NSJSVirtualMachine machine, string message, NSJSErrorKind kind)
        {
            if (machine == null)
            {
                throw new ArgumentNullException("machine");
            }
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }
            IntPtr isolate = machine.Isolate;

            if (isolate == NULL)
            {
                throw new InvalidOperationException("isolate");
            }
            if (!Enum.IsDefined(typeof(NSJSErrorKind), kind))
            {
                throw new NotSupportedException("kind");
            }
            byte[] cch = NSJSString.GetUTF8StringBuffer(message);
            if (cch.Length <= 0)
            {
                cch = new byte[] { 0 };
            }

            fixed(byte *s = cch)
            {
                nsjs_exception_throw_error(isolate, s, kind);
            }
        }
Пример #3
0
        public static NSJSString New(NSJSVirtualMachine machine, string value)
        {
            if (machine == null)
            {
                throw new ArgumentNullException("machine");
            }
            IntPtr isolate = machine.Isolate;

            if (isolate == NULL)
            {
                throw new InvalidOperationException("machine");
            }
            IntPtr handle = NULL;

            if (value == null)
            {
                handle = nsjs_localvalue_string_new(isolate, null, 0);
            }
            else
            {
                int    count;
                byte[] cch = NSJSString.GetUTF8StringBuffer(value, out count);
                fixed(byte *p = cch)
                {
                    handle = nsjs_localvalue_string_new(isolate, p, count);
                }
            }
            if (handle == NULL)
            {
                throw new InvalidOperationException("machine");
            }
            return(new NSJSString(handle, machine));
        }
Пример #4
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);
        }
Пример #5
0
        public static NSJSValue Parse(NSJSVirtualMachine machine, string json)
        {
            if (machine == null)
            {
                throw new ArgumentNullException("machine");
            }
            IntPtr isolate = machine.Isolate;

            if (isolate == NULL)
            {
                throw new InvalidOperationException("machine");
            }
            if (string.IsNullOrEmpty(json))
            {
                return(NSJSValue.Null(machine));
            }
            IntPtr handle = NULL;
            int    count;

            byte[] cch = NSJSString.GetUTF8StringBuffer(json, out count);
            fixed(byte *p = cch)
            {
                handle = nsjs_localvalue_json_parse(isolate, p, count);
            }

            if (handle == NULL)
            {
                throw new ArgumentOutOfRangeException("json");
            }
            return(NSJSValueBuilder.From(handle, machine));
        }
Пример #6
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;
            }
        }
Пример #7
0
 static NSJSVirtualMachine()
 {
     fixed(byte *exce_path = NSJSString.GetUTF8StringBuffer(Application.ExecutablePath))
     {
         nsjs_initialize(exce_path);
     }
 }
Пример #8
0
 public virtual bool Set(string name, string value, PropertyAttribute attributes = PropertyAttribute.None)
 {
     return(Handling <bool>(name, (s) =>
     {
         byte[] buffer = value != null ? NSJSString.GetUTF8StringBuffer(value) : null;
         fixed(byte *pinned = buffer)
         {
             return nsjs_virtualmachine_extension_object_template_set_string(this.Handle, s, pinned, attributes);
         }
     }));
 }
Пример #9
0
        public virtual bool Remove(string key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (key.Length <= 0)
            {
                throw new ArgumentException("key");
            }

            fixed(byte *s = NSJSString.GetUTF8StringBuffer(key))
            {
                return(nsjs_localvalue_object_property_delete(this.Isolate, this.Handle, s));
            }
        }
Пример #10
0
 public virtual bool Set(string key, string value)
 {
     if (value == null)
     {
         return(this.Set(key, NSJSValue.Null(this.VirtualMachine)));
     }
     return(this.Set(key, (name) =>
     {
         if (value == null)
         {
             return nsjs_localvalue_object_property_set_string(this.Isolate, this.Handle, name, null, 0);
         }
         int count;
         byte[] cch = NSJSString.GetUTF8StringBuffer(value, out count);
         fixed(byte *s = cch)
         {
             return nsjs_localvalue_object_property_set_string(this.Isolate, this.Handle, name, s, count);
         }
     }));
 }
Пример #11
0
        protected virtual bool Set(string key, Func <IntPtr, bool> func)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (key.Length <= 0)
            {
                throw new ArgumentException("key");
            }
            if (func == null)
            {
                throw new ArgumentNullException("func");
            }

            fixed(byte *s = NSJSString.GetUTF8StringBuffer(key))
            {
                return(func((IntPtr)s));
            }
        }
Пример #12
0
            private TResult Handling <TResult>(string name, Func <IntPtr, TResult> d)
            {
                if (d == null)
                {
                    throw new ArgumentNullException("d");
                }
                if (name == null)
                {
                    throw new ArgumentNullException("name");
                }
                if (name.Length <= 0)
                {
                    throw new ArgumentException("name");
                }

                fixed(byte *p = NSJSString.GetUTF8StringBuffer(name))
                {
                    return(d((IntPtr)p));
                }
            }
Пример #13
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);
        }
Пример #14
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);
        }
Пример #15
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);
        }