private static void WriteAllBytes(IntPtr info) { NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info); NSJSString url = arguments.Length > 0 ? arguments[0] as NSJSString : null; NSJSUInt8Array buffer = arguments.Length > 1 ? arguments[1] as NSJSUInt8Array : null; bool success = false; Exception exception = null; if (url != null) { string path = url.Value; byte[] bytes = (buffer == null ? BufferExtension.EmptryBuffer : buffer.Buffer); try { FILE.WriteAllBytes(path, bytes); success = true; } catch (Exception e) { exception = e; success = false; } } if (exception == null) { arguments.SetReturnValue(success); } else { Throwable.Exception(arguments.VirtualMachine, exception); } }
private static void Require(IntPtr info) { NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info); NSJSString rawUri = arguments.Length > 0 ? arguments[0] as NSJSString : null; if (rawUri == null || rawUri.DateType != NSJSDataType.kString) { arguments.SetReturnValue(false); } else { NSJSVirtualMachine machine = arguments.VirtualMachine; NSJSObject global = machine.Global; string path = rawUri.Value; string source; if (string.IsNullOrEmpty(path)) { arguments.SetReturnValue(false); } else { int index = path.IndexOf('#'); if (index > -1) { path = path.Substring(0, index); } index = path.IndexOf('?'); if (index > -1) { path = path.Substring(0, index); } do { bool success = false; if (!FileAuxiliary.TryReadAllText(path, out source)) { if (!FileAuxiliary.TryReadAllText(Application.StartupPath + "/" + path, out source)) { arguments.SetReturnValue(false); break; } success = true; } if (!success) { if (!HttpAuxiliary.TryReadAllText(path, out source)) { arguments.SetReturnValue(false); break; } } if (File.Exists(path)) { path = Path.GetFullPath(path); } arguments.SetReturnValue(machine.Run(source, path)); } while (false); } } }
private static void GetBytes(IntPtr info) { NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info); ENCODING encoding = GetEncoding(arguments.This); if (encoding == null) { Throwable.ObjectDisposedException(arguments.VirtualMachine); } else { byte[] buffer = null; if (arguments.Length > 0) { NSJSString s = arguments[0] as NSJSString; if (s != null) { buffer = encoding.GetBytes(s.Value); } } if (buffer == null) { buffer = BufferExtension.EmptryBuffer; } arguments.SetReturnValue(buffer); } }
public static NSJSValue ToObject(NSJSVirtualMachine machine, IPHostEntry host) { if (machine == null) { return(null); } if (host == null) { return(NSJSValue.Null(machine)); } NSJSObject entry = NSJSObject.New(machine); entry.Set("HostName", host.HostName); entry.Set("AddressList", ArrayAuxiliary.ToArray(machine, host.AddressList)); string[] aliases = host.Aliases; NSJSArray array = NSJSArray.New(machine, aliases.Length); for (int i = 0; i < aliases.Length; i++) { array[i] = NSJSString.New(machine, aliases[i]); } entry.Set("Aliases", array); return(entry); }
public static MailAddress ToMailAddress(NSJSValue value) { NSJSObject a = value as NSJSObject; string address = null; if (a == null) { NSJSString s = value as NSJSString; if (s != null) { address = s.Value; if (string.IsNullOrEmpty(address)) { return(null); } return(new MailAddress(address)); } return(null); } address = ValueAuxiliary.ToString(a.Get("Address")); if (string.IsNullOrEmpty(address)) { return(null); } string displayName = ValueAuxiliary.ToString(a.Get("DisplayName")); return(new MailAddress(address, displayName, Encoding.UTF8)); }
public static NSJSValue ToArray(NSJSVirtualMachine machine, IEnumerable <string> s) { if (machine == null) { return(null); } int count = Enumerable.Count(s); NSJSArray array = NSJSArray.New(machine, count); int index = 0; s.FirstOrDefault(item => { NSJSValue value = null; if (item == null) { value = NSJSValue.Null(machine); } else { value = NSJSString.New(machine, item); } array[index++] = value; return(false); }); return(array); }
public static int Fill(NSJSValue source, MailAddressCollection destination) { int count = 0; if (destination == null) { return(count); } NSJSString addresss = source as NSJSString; if (addresss != null) { destination.Add(addresss.Value); return(count); } NSJSArray s = source as NSJSArray; if (s == null) { return(count); } int len = s.Length; for (int i = 0; i < len; i++) { MailAddress address = ObjectAuxiliary.ToMailAddress(s[i]); if (address == null) { continue; } destination.Add(address); count++; } return(count); }
private static void Delete(IntPtr info) { NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info); NSJSString rawUri = arguments.Length > 0 ? arguments[0] as NSJSString : null; bool success = false; Exception exception = null; if (rawUri != null) { string path = rawUri.Value; if (DIRECTORY.Exists(path)) { try { DIRECTORY.Delete(path, true); success = true; } catch (Exception e) { exception = e; success = false; } } } if (exception == null) { arguments.SetReturnValue(success); } else { Throwable.Exception(arguments.VirtualMachine, exception); } }
public static double ToDouble(NSJSValue value) { if (value == null || value.IsNullOrUndfined) { return(0); } NSJSInt32 i32 = value as NSJSInt32; if (i32 != null) { return(i32.Value); } NSJSUInt32 u32 = value as NSJSUInt32; if (u32 != null) { return(u32.Value); } NSJSBoolean boolean = value as NSJSBoolean; if (boolean != null) { return(boolean.Value ? 1 : 0); } NSJSDateTime time = value as NSJSDateTime; if (time != null) { return(NSJSDateTime.DateTimeToLocalDate(time.Value)); } NSJSDouble dbl = value as NSJSDouble; if (dbl != null) { return(dbl.Value); } NSJSInt64 i64 = value as NSJSInt64; if (i64 != null) { return(i64.Value); } NSJSString str = value as NSJSString; if (str != null) { double n; if (double.TryParse(str.Value, NumberStyles.Float | NumberStyles.None, null, out n)) { return(n); } } return(0); }
private static void MoveOrCopy(IntPtr info, bool copyMode) { NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info); bool success = false; Exception exception = null; do { if (arguments.Length <= 0) { break; } NSJSString sourceFileName = arguments[0] as NSJSString; NSJSString destFileName = arguments[1] as NSJSString; if (sourceFileName == null || destFileName == null) { break; } try { if (copyMode) { if (arguments.Length < 3) { FILE.Copy(sourceFileName.Value, destFileName.Value); } else { bool overwrite = ValueAuxiliary.ToBoolean(arguments[2]); FILE.Copy(sourceFileName.Value, destFileName.Value, overwrite); } } else { FILE.Move(sourceFileName.Value, destFileName.Value); } success = true; } catch (Exception e) { exception = e; success = false; } } while (false); if (exception == null) { arguments.SetReturnValue(success); } else { Throwable.Exception(arguments.VirtualMachine, exception); } }
private static void Exists(IntPtr info) { NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info); NSJSString path = arguments.Length > 0 ? arguments[0] as NSJSString : null; if (path == null) { arguments.SetReturnValue(false); } else { arguments.SetReturnValue(FILE.Exists(path.Value)); } }
private static void system(IntPtr info) { NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info); if (arguments.Length > 0) { NSJSString cmd = arguments[0] as NSJSString; string message = cmd.Value; if (!string.IsNullOrEmpty(message)) { NSJSConsoleHandler handler = GetConsoleHandler(arguments); handler.SystemCall(arguments, message); } } }
private static void Move(IntPtr info) { NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info); bool success = false; Exception exception = null; do { if (arguments.Length <= 0) { break; } NSJSString sourceFileName = arguments[0] as NSJSString; NSJSString destFileName = arguments[1] as NSJSString; if (sourceFileName == null || destFileName == null) { break; } try { DIRECTORY.Move(sourceFileName.Value, destFileName.Value); success = true; } catch (Exception e) { exception = e; success = false; } } while (false); if (exception == null) { arguments.SetReturnValue(success); } else { Throwable.Exception(arguments.VirtualMachine, exception); } }
private static void GetEncoding(IntPtr info) { NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info); NSJSValue result = null; if (arguments.Length > 0) { NSJSInt32 codepage = arguments[0] as NSJSInt32; try { if (codepage != null) { result = New(arguments.VirtualMachine, ENCODING.GetEncoding(codepage.Value)); } NSJSString name = arguments[0] as NSJSString; if (name != null) { result = New(arguments.VirtualMachine, ENCODING.GetEncoding(name.Value)); } } catch (Exception) { } } arguments.SetReturnValue(NSJSValue.UndefinedMerge(arguments.VirtualMachine, result)); }
public static string ToString(NSJSValue value) { if (value == null) { return(null); } if (value.IsNullOrUndfined) { return(null); } NSJSString s = value as NSJSString; if (s != null) { return(s.Value); } NSJSObject o = value as NSJSObject; if (o != null) { return(NSJSJson.Stringify(o)); } return(NSJSString.Cast(value).Value); }
private static HttpClientOptions object2options(NSJSObject options) { if (options == null) { return(null); } HttpClientOptions o = new HttpClientOptions(); NSJSBoolean boolean = options.Get("AllowAutoRedirect") as NSJSBoolean; if (boolean != null) { o.AllowAutoRedirect = boolean.Value; } NSJSInt32 int32 = options.Get("AutomaticDecompression") as NSJSInt32; if (int32 != null) { o.AutomaticDecompression = (DecompressionMethods)int32.Value; } int32 = options.Get("CachePolicy") as NSJSInt32; if (int32 != null) { o.CachePolicy = new HttpRequestCachePolicy((HttpRequestCacheLevel)int32.Value); } int32 = options.Get("MaximumAutomaticRedirections") as NSJSInt32; if (int32 != null) { o.MaximumAutomaticRedirections = int32.Value; } int32 = options.Get("Timeout") as NSJSInt32; if (int32 != null) { o.Timeout = int32.Value; } NSJSString stringt = options.Get("Proxy") as NSJSString; if (stringt != null) { o.Proxy = new WebProxy(stringt.Value); } stringt = options.Get("Referer") as NSJSString; if (stringt != null) { o.Referer = stringt.Value; } NSJSInt32Array int32array = options.Get("Range") as NSJSInt32Array; if (int32array != null) { o.Range = int32array.Buffer; } NSJSArray array = options.Get("Headers") as NSJSArray; if (array != null) { NameValueCollection headers = o.Headers; int count = array.Length; for (int i = 0; i < count; i++) { stringt = array[i] as NSJSString; if (stringt == null) { continue; } headers.Add(headers); } } return(o); }
private static void GetFilesOrDirectories(IntPtr info, bool fileMode) { NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info); string[] buffer = null; Exception exception = null; try { if (arguments.Length > 0) { string path = (arguments[0] as NSJSString)?.Value; if (DIRECTORY.Exists(path)) { if (arguments.Length > 1) { string searchPattern = (arguments[1] as NSJSString)?.Value; if (string.IsNullOrEmpty(searchPattern)) { searchPattern = "*"; } if (arguments.Length > 2) { int?searchOption = (arguments[2] as NSJSInt32)?.Value; buffer = fileMode ? DIRECTORY.GetFiles(path, searchPattern, (SearchOption)searchOption.GetValueOrDefault()) : DIRECTORY.GetDirectories(path, searchPattern, (SearchOption)searchOption.GetValueOrDefault()); } else { buffer = fileMode ? DIRECTORY.GetFiles(path, searchPattern) : DIRECTORY.GetDirectories(path, searchPattern); } } else { buffer = fileMode ? DIRECTORY.GetFiles(path) : DIRECTORY.GetDirectories(path); } } } } catch (Exception e) { exception = e; } if (exception != null) { Throwable.Exception(arguments.VirtualMachine, exception); } else if (buffer == null) { Throwable.DirectoryNotFoundException(arguments.VirtualMachine); } else { NSJSArray s = NSJSArray.New(arguments.VirtualMachine, buffer.Length); for (int i = 0; i < buffer.Length; i++) { s[i] = NSJSString.New(arguments.VirtualMachine, buffer[i]); } arguments.SetReturnValue(s); } }
public static NSJSValue As(this object value, NSJSVirtualMachine machine) { if (machine == null) { return(null); } if (value == null || value == DBNull.Value) { return(NSJSValue.Null(machine)); } if (value is NSJSValue) { return(value as NSJSValue); } Type typeid = value.GetType(); if (typeid == typeof(int) || typeid == typeof(short) || typeid == typeof(sbyte) || typeid == typeof(char)) { return(NSJSInt32.New(machine, Convert.ToInt32(value))); } else if (typeid == typeof(uint) || typeid == typeof(ushort) || typeid == typeof(byte)) { return(NSJSUInt32.New(machine, Convert.ToUInt32(value))); } else if (typeid == typeof(string)) { return(NSJSString.New(machine, value.ToString())); } else if (typeid == typeof(bool)) { return(NSJSBoolean.New(machine, Convert.ToBoolean(value))); } else if (typeid == typeof(DateTime)) { DateTime datetime = Convert.ToDateTime(value); if (NSJSDateTime.Invalid(datetime)) { datetime = NSJSDateTime.Min; } return(NSJSDateTime.New(machine, datetime)); } else if (typeid == typeof(float) || typeid == typeof(double)) { return(NSJSDouble.New(machine, Convert.ToDouble(value))); } else if (typeid == typeof(byte[])) { byte[] buffer = (byte[])(object)value; if (buffer == null) { return(NSJSValue.Null(machine)); } return(NSJSUInt8Array.New(machine, buffer)); } else if (typeid == typeof(sbyte[])) { sbyte[] buffer = (sbyte[])(object)value; if (buffer == null) { return(NSJSValue.Null(machine)); } return(NSJSInt8Array.New(machine, buffer)); } else if (typeid == typeof(short[])) { short[] buffer = (short[])(object)value; if (buffer == null) { return(NSJSValue.Null(machine)); } return(NSJSInt16Array.New(machine, buffer)); } else if (typeid == typeof(ushort[])) { ushort[] buffer = (ushort[])(object)value; if (buffer == null) { return(NSJSValue.Null(machine)); } return(NSJSUInt16Array.New(machine, buffer)); } else if (typeid == typeof(int[])) { int[] buffer = (int[])(object)value; if (buffer == null) { return(NSJSValue.Null(machine)); } return(NSJSInt32Array.New(machine, buffer)); } else if (typeid == typeof(uint[])) { uint[] buffer = (uint[])(object)value; if (buffer == null) { return(NSJSValue.Null(machine)); } return(NSJSUInt32Array.New(machine, buffer)); } else if (typeid == typeof(float[])) { float[] buffer = (float[])(object)value; if (buffer == null) { return(NSJSValue.Null(machine)); } return(NSJSFloat32Array.New(machine, buffer)); } else if (typeid == typeof(double[])) { double[] buffer = (double[])(object)value; if (buffer == null) { return(NSJSValue.Null(machine)); } return(NSJSFloat64Array.New(machine, buffer)); } return(NSJSValue.Null(machine)); }