Beispiel #1
0
 public static void Free(NSJSExceptionInfo *exception)
 {
     if (exception != null)
     {
         NSJSMemoryManagement.Free(exception);
     }
 }
Beispiel #2
0
        protected virtual uint[] ValueToArray()
        {
            int   count = 0;
            void *cch   = null;

            try
            {
                cch = nsjs_localvalue_get_uint32array(this.Handle, ref count);
                uint[] buffer = new uint[count];
                if (count > 0)
                {
                    fixed(uint *data = buffer)
                    {
                        BufferExtension.memcpy(cch, data, sizeof(uint) * count);
                    }
                }
                return(buffer);
            }
            finally
            {
                if (cch != null)
                {
                    NSJSMemoryManagement.Free(cch);
                }
            }
        }
Beispiel #3
0
        public virtual IEnumerable <string> GetAllKeys()
        {
            int           count = 0;
            sbyte **      ch    = nsjs_localvalue_object_getallkeys(this.Handle, ref count);
            ISet <string> r     = new HashSet <string>();

            if (ch != null)
            {
                for (int i = 0; i < count; i++)
                {
                    sbyte *s   = ch[i];
                    string key = new string(s);
                    if (s != null)
                    {
                        NSJSMemoryManagement.Free(s);
                    }
                    if (string.IsNullOrEmpty(key))
                    {
                        continue;
                    }
                    r.Add(key);
                }
                NSJSMemoryManagement.Free(ch);
            }
            return(r);
        }
Beispiel #4
0
        protected virtual sbyte[] ValueToArray()
        {
            void *cch   = null;
            int   count = 0;

            try
            {
                cch = nsjs_localvalue_get_int8array(this.Handle, ref count);
                sbyte[] buffer = new sbyte[count];
                if (count > 0)
                {
                    fixed(sbyte *data = buffer)
                    {
                        BufferExtension.memcpy(cch, data, (sizeof(sbyte) * count));
                    }
                }
                return(buffer);
            }
            finally
            {
                if (cch != null)
                {
                    NSJSMemoryManagement.Free(cch);
                }
            }
        }
Beispiel #5
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 #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;
            }
        }
Beispiel #7
0
        protected virtual string LocalValueToString()
        {
            int    len    = 0;
            sbyte *chunk  = nsjs_localvalue_get_string(this.Handle, ref len);
            string result = chunk != null ? new string(chunk) : null;

            NSJSMemoryManagement.Free(chunk);
            return(result);
        }
Beispiel #8
0
 public static void Free(NSJSStackTrace *stacktrace)
 {
     if (stacktrace != null)
     {
         NSJSStackFrame *stackframes = stacktrace->Frame;
         if (stackframes != null)
         {
             NSJSMemoryManagement.Free(stackframes);
         }
         NSJSMemoryManagement.Free(stacktrace);
     }
 }
Beispiel #9
0
        public static string TypeOf(NSJSValue value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            sbyte *s = nsjs_localvalue_typeof(value.Isolate, value.Handle);
            string r = s != null ? new string(s) : null;

            NSJSMemoryManagement.Free(s);
            return(r);
        }
Beispiel #10
0
            public static NSJSExceptionInfo *New()
            {
                NSJSExceptionInfo *exception = (NSJSExceptionInfo *)NSJSMemoryManagement.Alloc(sizeof(NSJSExceptionInfo));

                if (exception == null)
                {
                    throw new InvalidOperationException("exception");
                }
                if (exception->NowIsWrong)
                {
                    exception->NowIsWrong = false;
                }
                return(exception);
            }
Beispiel #11
0
            public static NSJSStackTrace *New()
            {
                NSJSStackTrace *stacktrace = (NSJSStackTrace *)NSJSMemoryManagement.Alloc(sizeof(NSJSStackTrace));

                if (stacktrace == null)
                {
                    throw new InvalidOperationException("stacktrace");
                }
                stacktrace->Count = 0;
                int cb = sizeof(NSJSStackFrame) * MAXSTACKFRAMECOUNT;

                stacktrace->Frame = (NSJSStackFrame *)NSJSMemoryManagement.Alloc(cb);
                if (stacktrace->Frame == null)
                {
                    throw new InvalidOperationException("stackframes");
                }
                return(stacktrace);
            }
Beispiel #12
0
        public static string Stringify(NSJSValue value)
        {
            if (value == null)
            {
                return(null);
            }
            int    len   = 0;
            IntPtr chunk = nsjs_localvalue_json_stringify(value.Isolate, value.Handle, ref len);

            if (chunk == NULL)
            {
                return(null);
            }
            string result = chunk != NULL ? new string((sbyte *)chunk.ToPointer()) : null;

            NSJSMemoryManagement.Free(chunk);
            return(result);
        }
Beispiel #13
0
        private NSJSException(NSJSVirtualMachine machine, NSJSStructural.NSJSExceptionInfo *exception)
        {
            if (exception == null)
            {
                throw new ArgumentNullException("exception");
            }
            this.VirtualMachine    = machine ?? throw new ArgumentNullException("machine");
            this.EndColumn         = exception->EndColumn;
            this.EndPosition       = exception->EndPosition;
            this.ErrorLevel        = exception->ErrorLevel;
            this.exception_message = new string((sbyte *)exception->ExceptionMessage);
            NSJSMemoryManagement.Free(exception->ExceptionMessage);
            this.IsSharedCrossOrigin  = exception->IsSharedCrossOrigin;
            this.LineNumber           = exception->LineNumber;
            this.ResourceColumnOffset = exception->ResourceColumnOffset;
            this.ResourceLineOffset   = exception->ResourceLineOffset;
            this.ResourceName         = new string((sbyte *)exception->ResourceName);
            NSJSMemoryManagement.Free(exception->ResourceName);
            this.ScriptId           = exception->ScriptId;
            this.ScriptResourceName = new string((sbyte *)exception->ScriptResourceName);
            NSJSMemoryManagement.Free(exception->ScriptResourceName);
            this.SourceLine = new string((sbyte *)exception->SourceLine);
            NSJSMemoryManagement.Free(exception->SourceLine);
            this.SourceMapUrl = new string((sbyte *)exception->SourceMapUrl);
            NSJSMemoryManagement.Free(exception->SourceMapUrl);
            this.StartColumn   = exception->StartColumn;
            this.StartPosition = exception->StartPosition;
            string s = new string((sbyte *)exception->StackTrace);

            if (s != null)
            {
                int index = s.IndexOf('\n');
                if (index > -1)
                {
                    s = s.Substring(index + 1);
                }
                s = s.Replace("\n", "\r\n");
            }
            this.JavaScriptStackTrace = s;
            NSJSMemoryManagement.Free(exception->StackTrace);
            // memeset
            exception->Reset();
        }
Beispiel #14
0
 internal NSJSStackFrame(NSJSStructural.NSJSStackFrame *stackframe)
 {
     if (stackframe == null)
     {
         throw new ArgumentNullException("stackframe");
     }
     this.Column       = stackframe->Column;
     this.FunctionName = new string((sbyte *)stackframe->FunctionName);
     NSJSMemoryManagement.Free(stackframe->FunctionName);
     this.IsConstructor = stackframe->IsConstructor;
     this.IsEval        = stackframe->IsEval;
     this.IsWasm        = stackframe->IsWasm;
     this.LineNumber    = stackframe->LineNumber;
     this.ScriptId      = stackframe->ScriptId;
     this.ScriptName    = new string((sbyte *)stackframe->ScriptName);
     NSJSMemoryManagement.Free(stackframe->ScriptName);
     this.ScriptNameOrSourceURL = new string((sbyte *)stackframe->ScriptNameOrSourceURL);
     NSJSMemoryManagement.Free(stackframe->ScriptNameOrSourceURL);
     // memset
     stackframe->Reset();
 }
Beispiel #15
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 #16
0
 public static void Free(IntPtr memory)
 {
     NSJSMemoryManagement.Free(memory.ToPointer());
 }