public static float TestArguments(QuickJS quickJS, MethodBase method, ParameterInfo[] parameters, object[] argv) { int i = 0; float minus = 0; if (argv.Length > parameters.Length) { return(0); } for (int l = 0, t = argv.Length; l < t; ++l) { Type pType = parameters[i].ParameterType; var obj = argv[l]; if (obj is JSValue) { float match = isJSValueMatch((JSValue)obj, pType); if (match == 1) { ++i; } else if (match == 0) { return(0); } else { ++i; minus += (1 - match); } } else { bool match = isTypeMatch(obj, pType); if (match) { ++i; } else { return(0); } } } for (int t = parameters.Length; i < t; ++i) { if (!parameters[i].HasDefaultValue) { return(0); } } if (parameters.Length == 0) { return(1); } else { return((i - minus) / parameters.Length); } }
public static bool ConvertDelegate(QuickJS quickJS, object obj, Type pType, ref object ret) { if (obj is JSValue) { JSValue value = (JSValue)obj; return(ConvertDelegate(value, pType, ref ret)); } return(false); }
public static object[] ProcessArguments(QuickJS quickJS, Api.QJS_Item[] argv, int argStart, int argEnd) { object[] results = new object[argEnd - argStart]; for (int i = 0, t = results.Length; i < t; ++i) { results[i] = ProcessObject(quickJS, ref argv[i + argStart]); } return(results); }
public WorkerInner(QuickJS quickJS, Worker worker, string path) { semaphore = new Semaphore(0, 2); this.quickJS = quickJS; self = quickJS.Eval(script); self.Set("__postMessage", new Action <string>(onmessage)); codePath = path; this.worker = new WeakReference <Worker>(worker); thread = new Thread(main); thread.Start(); }
private static bool copyArray <T>(QuickJS quickJS, object obj, Type pType, ref object ret) { if (pType == typeof(T[]) && obj is JSValue) { JSValue value = (JSValue)obj; uint len = (uint)value.GetLength(); var arr = new T[len]; IntPtr ptr = Marshal.UnsafeAddrOfPinnedArrayElement(arr, 0); value.CopyArrayBuffer(ptr, 0, len); ret = arr; return(true); } return(false); }
public static object ProcessObject(QuickJS quickJS, ref Api.QJS_Item item) { switch (item.type) { case Api.ITEM_TYPE_INT: return(item.val.i); case Api.ITEM_TYPE_LONG: return(item.val.l); case Api.ITEM_TYPE_DOUBLE: return(item.val.d); case Api.ITEM_TYPE_BOOL: return(item.val.b); case Api.ITEM_TYPE_STRING: return(Marshal.PtrToStringAuto(item.val.p)); case Api.ITEM_TYPE_JS_OBJECT: { Instance instance; if (quickJS.instances.items.TryGetValue(item.val.i, out instance)) { return(instance.target); } break; } case Api.ITEM_TYPE_JS_CLASS: { Class clazz = quickJS.instances.classes[item.val.i]; return(clazz.target); } case Api.ITEM_TYPE_JS_VALUE: { return(item.ToValue(quickJS)); } case Api.ITEM_TYPE_JS_STRING: return(item.ToString(quickJS)); } return(null); }
public static object ConvertType(QuickJS quickJS, object obj, Type type) { object ret = null; if (copyArray <sbyte>(quickJS, obj, type, ref ret)) { } else if (copyArray <byte>(quickJS, obj, type, ref ret)) { } else if (copyArray <Int16>(quickJS, obj, type, ref ret)) { } else if (copyArray <UInt16>(quickJS, obj, type, ref ret)) { } else if (copyArray <Int32>(quickJS, obj, type, ref ret)) { } else if (copyArray <UInt32>(quickJS, obj, type, ref ret)) { } else if (copyArray <Int64>(quickJS, obj, type, ref ret)) { } else if (copyArray <UInt64>(quickJS, obj, type, ref ret)) { } else if (copyArray <float>(quickJS, obj, type, ref ret)) { } else if (copyArray <double>(quickJS, obj, type, ref ret)) { } else if (ConvertDelegate(quickJS, obj, type, ref ret)) { } else { ret = ConvertType(obj, type); } return(ret); }
public static T FindMethod <T>(QuickJS quickJS, T[] methods, IntPtr indexes, int length, object[] argv) where T : MethodBase { float max = 0; T ret = null; ParameterInfo[] parameters = null; for (int i = 0; i < length; ++i) { int idx = Marshal.ReadInt32(indexes, i * 4); var method = methods[idx]; var ps = method.GetParameters(); float v = TestArguments(quickJS, method, ps, argv); if (v == 1) { parameters = ps; ret = method; break; } else if (v > 0) { if (v > max) { parameters = ps; ret = method; max = v; } } } if (parameters != null) { for (int i = 0, t = argv.Length; i < t; ++i) { argv[i] = ConvertType(quickJS, argv[i], parameters[i].ParameterType); } } return(ret); }
public Worker(QuickJS quickJS, QuickJS parent, string codePath) { this.quickJS = quickJS; this.parent = new WeakReference <QuickJS>(parent); inner = new WorkerInner(quickJS, this, codePath); }