static int UnBindRawJSObjectAndFree(int gcHandle) { GCHandle h = (GCHandle)(IntPtr)gcHandle; JSObject obj = (JSObject)h.Target; if (obj != null && obj.RawObject != null) { var raw_obj = obj.RawObject; if (raw_to_js.ContainsKey(raw_obj)) { raw_to_js.Remove(raw_obj); } obj.Dispose(); var gCHandle = obj.Handle; obj.Handle.Free(); obj.JSHandle = -1; obj.RawObject = null; return((int)(IntPtr)gCHandle); } return(0); }
static void SetupJSContinuation(Task task, JSObject cont_obj) { if (task.GetType() == typeof(Task)) { task.GetAwaiter().OnCompleted((Action)(() => { if (task.Exception != null) { cont_obj.Invoke((string)"reject", task.Exception.ToString()); } else { cont_obj.Invoke((string)"resolve", (object [])null); } cont_obj.Dispose(); FreeObject(task); })); } else { //FIXME this is horrible codegen, we can do better with per-method glue if (gsjsc == null) { gsjsc = typeof(Runtime).GetMethod("GenericSetupJSContinuation", BindingFlags.NonPublic | BindingFlags.Static); } gsjsc.MakeGenericMethod(task.GetType().GetGenericArguments()).Invoke(null, new object [] { task, cont_obj }); } }
static void GenericSetupJSContinuation <T> (Task <T> task, JSObject cont_obj) { task.GetAwaiter().OnCompleted((Action)(() => { if (task.Exception != null) { cont_obj.Invoke((string)"reject", task.Exception.ToString()); } else { cont_obj.Invoke((string)"resolve", task.Result); } cont_obj.Dispose(); FreeObject(task); })); }
static void SetupJSContinuation(Task task, JSObject cont_obj) { if (task.IsCompleted) { Complete(); } else { task.GetAwaiter().OnCompleted(Complete); } void Complete() { try { if (task.Exception == null) { var resultProperty = task.GetType().GetProperty("Result"); if (resultProperty == null) { cont_obj.Invoke("resolve", (object[])null); } else { cont_obj.Invoke("resolve", resultProperty.GetValue(task)); } } else { cont_obj.Invoke("reject", task.Exception.ToString()); } } catch (Exception e) { cont_obj.Invoke("reject", e.ToString()); } finally { cont_obj.Dispose(); FreeObject(task); } } }