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); }
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)); }
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)); }
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)); } }
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; } }
static NSJSVirtualMachine() { fixed(byte *exce_path = NSJSString.GetUTF8StringBuffer(Application.ExecutablePath)) { nsjs_initialize(exce_path); } }
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); } }
public static bool InstanceOf(NSJSValue instance, string type) { if (instance == null || string.IsNullOrEmpty(type)) { return(false); } return(nsjs_localvalue_instanceof(instance, NSJSString.New(instance.VirtualMachine, type))); }
public void SetReturnValue(string value) { if (value == null) { this.SetReturnValue(NSJSValue.Null(this.VirtualMachine)); return; } this.SetReturnValue(NSJSString.New(this.VirtualMachine, value)); }
public virtual bool IsDefined(string key) { if (string.IsNullOrEmpty(key)) { return(false); } NSJSFunction function = this.GetFrameworkFunction(RUNTIME_ISDEFINED_PROPERTYKEY); return((function.Call(new NSJSValue[] { this, NSJSString.New(this.VirtualMachine, key) }) as NSJSInt32)?.Value == 1); }
public virtual NSJSObject GetPropertyDescriptor(string key) { if (string.IsNullOrEmpty(key)) { return(null); } NSJSFunction function = this.GetFrameworkFunction(RUNTIME_GETPROPERTYDESCRIPTOR_PROPERTYKEY); return(function.Call(new NSJSValue[] { this, NSJSString.New(this.VirtualMachine, key) }) as NSJSObject); }
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); } })); }
public virtual void DefineProperty(string key, NSJSFunctionCallback get, NSJSFunctionCallback set) { this.InternalDefineProperty(key, (machine, function) => { NSJSValue[] s = new NSJSValue[] { this, NSJSString.New(machine, key), get == null ? NSJSValue.Undefined(machine) : NSJSFunction.New(machine, get), set == null ? NSJSValue.Undefined(machine) : NSJSFunction.New(machine, set), }; function.Call(s); }); }
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)); } }
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); } })); }
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)); } }
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)); } }
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); }
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); }
public virtual NSJSValue Call(IEnumerable <string> args, out NSJSException exception) { IList <NSJSValue> argv = new List <NSJSValue>(); NSJSVirtualMachine machine = this.VirtualMachine; foreach (string s in args) { NSJSValue value = null; if (s == null) { value = NSJSValue.Null(machine); } else { value = NSJSString.New(machine, s); } if (value == null) { continue; } argv.Add(value); } return(this.Call(argv, out exception)); }
private static string sprint(NSJSFunctionCallbackInfo arguments, bool newline) { if (arguments == null) { throw new ArgumentNullException("arguments"); } string contents = null; if (arguments.Length <= 0) { return(contents); } try { string format = arguments.Length > 0 ? ValueAuxiliary.ToString(arguments[0]) : null; if (format == null) { format = newline ? Environment.NewLine : string.Empty; } else if (newline) { format += Environment.NewLine; } int index = -1; int previous = 0; int solt = 1; do { index = format.IndexOf('%', index + 1); if (index > -1) { int ofs = index + 1; for (int i = ofs; i < format.Length; i++) { char character = format[i]; char flags = '\0'; char pending = '\0'; int width = 0; if (character == 'c') { if (solt >= arguments.Length) { break; } int count = (index - previous); count = count < 0 ? 0 : count; string g = format.Substring(previous, count); if (!string.IsNullOrEmpty(g)) { contents += g; } previous = i + 1; contents += unchecked ((char)ValueAuxiliary.ToInt64(arguments[solt++])); index = i; break; } else if (character == '%') { int count = (i - previous); count = count < 0 ? 0 : count; string g = format.Substring(previous, count); if (!string.IsNullOrEmpty(g)) { contents += g; } previous = i + 1; index = i; break; } else if (character == 's') { if (solt >= arguments.Length) { break; } int count = (index - previous); count = count < 0 ? 0 : count; string g = format.Substring(previous, count); if (!string.IsNullOrEmpty(g)) { contents += g; } previous = i + 1; contents += ValueAuxiliary.ToString(arguments[solt++]); index = i; break; } else if (character == 'i' || character == 'd' || character == 'u') { if (sprint_get_format_features(format, ofs, i, ref flags, ref pending, ref width)) { if (solt >= arguments.Length) { break; } int count = (index - previous); count = count < 0 ? 0 : count; string g = format.Substring(previous, count); if (!string.IsNullOrEmpty(g)) { contents += g; } previous = i + 1; string s = NSJSString.Cast(arguments[solt++]).Value; if (flags == '+') { s = s.PadLeft(width, pending); } else { s = s.PadRight(width, pending); } contents += s; index = i; } break; } else if (character == 'b') { if (solt >= arguments.Length) { break; } int count = (index - previous); count = count < 0 ? 0 : count; string g = format.Substring(previous, count); if (!string.IsNullOrEmpty(g)) { contents += g; } previous = i + 1; contents += "0b" + Convert.ToString(ValueAuxiliary.ToInt64(arguments[solt++]), 2); index = i; break; } else if (character == 'o' || character == 'x' || character == 'X') { if (sprint_get_format_features(format, ofs, i, ref flags, ref pending, ref width)) { if (solt >= arguments.Length) { break; } int count = (index - previous); count = count < 0 ? 0 : count; string g = format.Substring(previous, count); if (!string.IsNullOrEmpty(g)) { contents += g; } previous = i + 1; int radix = character == 'o' ? 0x08 : 0x10; string s = Convert.ToString(ValueAuxiliary.ToInt64(arguments[solt++]), radix); if (flags == '+') { s = s.PadLeft(width, pending); } else { s = s.PadRight(width, pending); } if (radix == 0x10) { s = "0x" + (character == 'X' ? s.ToUpper() : s); } contents += s; index = i; } break; } else if (character == 'p' || character == 'P') { if (sprint_get_format_features(format, ofs, i, ref flags, ref pending, ref width)) { if (solt >= arguments.Length) { break; } int count = (index - previous); count = count < 0 ? 0 : count; string g = format.Substring(previous, count); if (!string.IsNullOrEmpty(g)) { contents += g; } previous = i + 1; long ptr = Environment.Is64BitProcess ? ValueAuxiliary.ToInt64(arguments[solt++]) : ValueAuxiliary.ToInt32(arguments[solt++]); string s = ptr.ToString(character == 'p' ? "x2" : "X2"); if (flags == '+') { s = s.PadLeft(width, pending); } else { s = s.PadRight(width, pending); } s = "0x" + s; contents += s; index = i; } break; } else if (character == 'f' || character == 'l' || character == 'e' || character == 'g' || character == 'E' || character == 'G') // lf { int mode = 0; // 0: 单精度浮点数,1:双精度浮点数,2:科学型浮点数数,3:常规型浮点数 int n = i + 1; if (n < format.Length && format[n] == 'f') // lf { mode = 1; i++; } else if (character == 'e' || character == 'E') { mode = 2; } else if (character == 'g' || character == 'G') { mode = 3; } if (sprint_get_format_features(format, ofs, i, ref flags, ref pending, ref width)) { if (solt >= arguments.Length) { break; } int count = (index - previous); count = count < 0 ? 0 : count; string g = format.Substring(previous, count); if (!string.IsNullOrEmpty(g)) { contents += g; } previous = i + 1; double num = ValueAuxiliary.ToDouble(arguments[solt++]); string s = mode == 2 || mode == 3 ? num.ToString(character.ToString()) : num.ToString(); if (flags == '+') { s = s.PadLeft(width, pending); } else { s = s.PadRight(width, pending); } contents += s; index = i; } break; } } } } while (index >= 0); if (format.Length > previous) { string s = format.Substring(previous); if (!string.IsNullOrEmpty(s)) { contents += s; } } } catch (Exception) { contents = null; } return(contents); }
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); }