From 356cb09f92484442e9b5674547fe63bbeb037b65 Mon Sep 17 00:00:00 2001 From: Dmitry Vasilev Date: Tue, 7 Feb 2023 19:36:47 +0800 Subject: [PATCH] fix promise patch --- src/eval.js | 2 +- src/record_io.js | 2 +- src/runtime.js | 17 ++++++++++------- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/eval.js b/src/eval.js index 5845642..d7b8196 100644 --- a/src/eval.js +++ b/src/eval.js @@ -340,7 +340,7 @@ export const eval_modules = ( calltree_changed_token, is_toplevel_call: true, - Promise: globalThis.run_window.Promise, + window: globalThis.run_window, } const result = run(module_fns, cxt, io_cache) diff --git a/src/record_io.js b/src/record_io.js index d6dca79..f06f7b3 100644 --- a/src/record_io.js +++ b/src/record_io.js @@ -21,7 +21,7 @@ const io_patch = (cxt, obj, method, name, use_context = false) => { // } if(cxt.io_cache_is_replay_aborted) { // Try to finish fast - throw new Error('io recording aborted') + throw new Error('io replay aborted') } else if(cxt.io_cache_is_recording) { let ok, value, error const has_new_target = new.target != null diff --git a/src/runtime.js b/src/runtime.js index 675ced7..09ba51f 100644 --- a/src/runtime.js +++ b/src/runtime.js @@ -16,7 +16,8 @@ const gen_to_promise = gen_fn => { } else { // If promise if(result.value?.then != null) { - return result.value.then( + return result.value.then.__original.call( + result.value, value => next(gen.next(value)), error => next(gen.throw(error)), ) @@ -111,9 +112,9 @@ export const run = gen_to_promise(function*(module_fns, cxt, io_cache) { const apply_promise_patch = cxt => { - cxt.promise_then = cxt.Promise.prototype.then + cxt.promise_then = cxt.window.Promise.prototype.then - cxt.Promise.prototype.then = function then(on_resolve, on_reject) { + cxt.window.Promise.prototype.then = function then(on_resolve, on_reject) { if(cxt.children == null) { cxt.children = [] @@ -141,10 +142,12 @@ const apply_promise_patch = cxt => { make_callback(on_reject, false), ) } + + cxt.window.Promise.prototype.then.__original = cxt.promise_then } const remove_promise_patch = cxt => { - cxt.Promise.prototype.then = cxt.promise_then + cxt.window.Promise.prototype.then = cxt.promise_then } export const set_record_call = cxt => { @@ -265,7 +268,7 @@ const __do_await = async (cxt, value) => { cxt.children = [] } const children_copy = cxt.children - if(value instanceof cxt.Promise) { + if(value instanceof cxt.window.Promise) { cxt.promise_then.call(value, v => { value.status = {ok: true, value: v} @@ -315,7 +318,7 @@ const __trace = (cxt, fn, name, argscount, __location, get_closure) => { try { value = fn(...args) ok = true - if(value instanceof cxt.Promise) { + if(value instanceof cxt.window.Promise) { set_record_call(cxt) } return value @@ -417,7 +420,7 @@ const __trace_call = (cxt, fn, context, args, errormessage, is_new = false) => { value = undefined } ok = true - if(value instanceof cxt.Promise) { + if(value instanceof cxt.window.Promise) { set_record_call(cxt) } return value