public DuktapeFunction GetMember(string name) { DuktapeFunction method = null; if (!_methodCache.TryGetValue(name, out method)) { var ctx = _context.rawValue; if (ctx != IntPtr.Zero) { this.PushProperty(ctx, name); if (DuktapeDLL.duk_is_function(ctx, -1)) { var ptr = DuktapeDLL.duk_get_heapptr(ctx, -1); var refid = DuktapeDLL.duk_unity_ref(ctx); method = new DuktapeFunction(ctx, refid, ptr); } else { DuktapeDLL.duk_pop(ctx); } } _methodCache[name] = method; } return(method); }
public static int SetTimeout(DuktapeFunction fn, float ms) { var id = ++_id; GetRunner().AddTimeout(id, fn, ms * 0.001f); return(id); }
public static int SetInterval(DuktapeFunction fn, float ms) { var id = ++_id; GetRunner().AddInterval(id, fn, ms * 0.001f); return(id); }
private static int _GetTimerFunction(IntPtr ctx, out DuktapeFunction fn) { if (!DuktapeDLL.duk_is_function(ctx, 0)) { fn = null; return(0); } if (!DuktapeDLL.duk_is_number(ctx, 1)) { fn = null; return(1); } var top_index = DuktapeDLL.duk_get_top_index(ctx); // Debug.Log($"_GetTimerFunction {top} ?? {DuktapeDLL.duk_get_top(ctx)}"); DuktapeValue[] argv = null; if (top_index > 1) { argv = new DuktapeValue[top_index - 1]; for (var i = 2; i <= top_index; i++) { DuktapeDLL.duk_dup(ctx, i); var argPtr = DuktapeDLL.duk_get_heapptr(ctx, -1); argv[i - 2] = new DuktapeValue(ctx, DuktapeDLL.duk_unity_ref(ctx), argPtr); } } DuktapeDLL.duk_dup(ctx, 0); var fnPtr = DuktapeDLL.duk_get_heapptr(ctx, -1); fn = new DuktapeFunction(ctx, DuktapeDLL.duk_unity_ref(ctx), fnPtr, argv); return(-1); }
public uint AddSpecial(string name, DuktapeFunction val) { // Debug.LogFormat("Add Special {0} {1}", name, val.rawValue); var refid = val.rawValue; _specialTypes[name] = val; return(refid); }
private IEnumerator _Timeout(int id, DuktapeFunction fn, float seconds) { var wait = seconds > 0 ? new WaitForSeconds(seconds) : null; yield return(wait); _timers.Remove(id); fn.Invoke(); }
public uint AddExported(Type type, DuktapeFunction fn) { var refid = fn.rawValue; _exported.Add(type, fn); _exportedTypes[refid] = type; // Debug.Log($"add export: {type}"); return(refid); }
public int AddExportedType(Type type, DuktapeFunction fn) { _exported.Add(type, fn); var index = _exportedTypes.Count; _exportedTypes.Add(type); _exportedTypeIndexer[type] = index; // Debug.Log($"add export: {type}"); return(index); }
private IEnumerator _Interval(int id, DuktapeFunction fn, float seconds) { var wait = seconds > 0 ? new WaitForSeconds(seconds) : null; while (true) { yield return(wait); fn.Invoke(); } }
protected override void Dispose(bool bManaged) { _jsInvoker = null; if (this._refid != 0 && this._context != null) { var vm = this._context.vm; vm.GC(this._refid, this.target, duk_unity_unref); this._refid = 0; this.target = null; } }
public static uint SetInterval(DuktapeFunction fn, int ms) { var runner = GetRunner(); if (runner != null) { var timer = runner._scheduler.CreateTimer(ms, fn, -1); runner._timers.Add(timer.id, timer); return(timer.id); } return(0); }
protected static void duk_begin_special(IntPtr ctx, string name) { DuktapeDLL.duk_push_c_function(ctx, object_private_ctor, 0); // ctor DuktapeDLL.duk_dup(ctx, -1); DuktapeDLL.duk_dup(ctx, -1); var typeValue = new DuktapeFunction(ctx, DuktapeDLL.duk_unity_ref(ctx)); // ctor, ctor DuktapeVM.GetVM(ctx).AddSpecial(name, typeValue); DuktapeDLL.duk_put_prop_string(ctx, -3, name); // ctor DuktapeDLL.duk_push_object(ctx); // ctor, prototype DuktapeDLL.duk_dup_top(ctx); // ctor, prototype, prototype DuktapeDLL.duk_put_prop_string(ctx, -3, "prototype"); // ctor, prototype }
private static uint CreateTimer(DuktapeFunction fn, int ms, bool once) { var runner = GetRunner(); if (runner != null) { var id = ++_idgen; var timer = runner._scheduler.Add(ms, once, fn); runner._timers.Add(id, timer); return(id); } return(0); }
protected override void Dispose(bool bManaged) { _jsInvoker = null; // Debug.LogErrorFormat("Dispose delegate ptr {0} {1}", _refPtr, _refid); if (this._refid != 0 && this._context != null) { var vm = this._context.vm; // vm.GC(0, this.target, duk_unity_unref_delegate); vm.GC(this._refid, this._refPtr, duk_unity_unref); this._refid = 0; this._refPtr = IntPtr.Zero; this.target = null; } }
private static IEnumerator DuktapeThreadRun(DuktapeFunction fn) { var thread = new DuktapeThread(fn); while (true) { object instruction; if (thread.Resume(out instruction)) { yield return(instruction); } yield break; } }
public DuktapeThread(DuktapeFunction fn) { var ctx = fn.ctx; var vm = DuktapeVM.GetContext(ctx).vm; var idx = DuktapeDLL.duk_push_thread(ctx); DuktapeDLL.duk_dup(ctx, -1); var ptr = DuktapeDLL.duk_get_heapptr(ctx, -1); _thread = new DuktapeValue(ctx, DuktapeDLL.duk_unity_ref(ctx), ptr); _threadContext = new DuktapeContext(vm, DuktapeDLL.duk_get_context(ctx, idx)); if (fn.Push(_threadContext.rawValue)) { } DuktapeDLL.duk_pop(ctx); }
// 记录栈状态 public void BeginInvoke(IntPtr ctx) { // Debug.Log($"BeginInvoke: {_savedState}"); if (_jsInvoker == null) { this.Push(ctx); // push this if (!DuktapeDLL.duk_is_function(ctx, -1)) { // Debug.Log("DuktapeDelegate based on Dispatcher"); DuktapeDLL.duk_get_prop_string(ctx, -1, "dispatch"); DuktapeDLL.duk_remove(ctx, -2); // remove this } _jsInvoker = new DuktapeFunction(ctx, DuktapeDLL.duk_unity_ref(ctx)); } _jsInvoker.Push(ctx); // push function this.Push(ctx); // push this _savedState = DuktapeDLL.duk_get_top(ctx); }
public static bool duk_get_classvalue(IntPtr ctx, int idx, out DuktapeFunction o) { if (DuktapeDLL.duk_is_function(ctx, idx)) { object obj; if (duk_get_cached_object(ctx, idx, out obj)) { if (obj is DuktapeFunction) { o = (DuktapeFunction)obj; return(true); } } DuktapeDLL.duk_dup(ctx, idx); var ptr = DuktapeDLL.duk_get_heapptr(ctx, -1); var refid = DuktapeDLL.duk_unity_ref(ctx); o = new DuktapeFunction(ctx, refid, ptr); return(true); } o = null; return(false); }
public static int SetTimeout(DuktapeFunction fn, double ms) { return(SetTimeout(fn, (float)ms)); }
public static int SetInterval(DuktapeFunction fn, double ms) { return(SetInterval(fn, (float)ms)); }
public static uint SetInterval(DuktapeFunction fn, int ms) { return(CreateTimer(fn, ms, false)); }
public static Coroutine StartCoroutine(this MonoBehaviour mb, DuktapeFunction fn) { Debug.LogError("DONT DO THIS, NOT IMPLEMENTED CORRECTLY"); return(mb.StartCoroutine(DuktapeThreadRun(fn))); }
public static uint SetTimeout(DuktapeFunction fn, int ms) { return(CreateTimer(fn, ms, true)); }
private void AddInterval(int id, DuktapeFunction fn, float seconds) { _timers[id] = StartCoroutine(_Interval(id, fn, seconds)); }