fix promise patch

This commit is contained in:
Dmitry Vasilev
2023-02-07 19:36:47 +08:00
parent be4d104dc6
commit 356cb09f92
3 changed files with 12 additions and 9 deletions

View File

@@ -340,7 +340,7 @@ export const eval_modules = (
calltree_changed_token, calltree_changed_token,
is_toplevel_call: true, is_toplevel_call: true,
Promise: globalThis.run_window.Promise, window: globalThis.run_window,
} }
const result = run(module_fns, cxt, io_cache) const result = run(module_fns, cxt, io_cache)

View File

@@ -21,7 +21,7 @@ const io_patch = (cxt, obj, method, name, use_context = false) => {
// } // }
if(cxt.io_cache_is_replay_aborted) { if(cxt.io_cache_is_replay_aborted) {
// Try to finish fast // Try to finish fast
throw new Error('io recording aborted') throw new Error('io replay aborted')
} else if(cxt.io_cache_is_recording) { } else if(cxt.io_cache_is_recording) {
let ok, value, error let ok, value, error
const has_new_target = new.target != null const has_new_target = new.target != null

View File

@@ -16,7 +16,8 @@ const gen_to_promise = gen_fn => {
} else { } else {
// If promise // If promise
if(result.value?.then != null) { if(result.value?.then != null) {
return result.value.then( return result.value.then.__original.call(
result.value,
value => next(gen.next(value)), value => next(gen.next(value)),
error => next(gen.throw(error)), 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 => { 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) { if(cxt.children == null) {
cxt.children = [] cxt.children = []
@@ -141,10 +142,12 @@ const apply_promise_patch = cxt => {
make_callback(on_reject, false), make_callback(on_reject, false),
) )
} }
cxt.window.Promise.prototype.then.__original = cxt.promise_then
} }
const remove_promise_patch = cxt => { 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 => { export const set_record_call = cxt => {
@@ -265,7 +268,7 @@ const __do_await = async (cxt, value) => {
cxt.children = [] cxt.children = []
} }
const children_copy = cxt.children const children_copy = cxt.children
if(value instanceof cxt.Promise) { if(value instanceof cxt.window.Promise) {
cxt.promise_then.call(value, cxt.promise_then.call(value,
v => { v => {
value.status = {ok: true, value: v} value.status = {ok: true, value: v}
@@ -315,7 +318,7 @@ const __trace = (cxt, fn, name, argscount, __location, get_closure) => {
try { try {
value = fn(...args) value = fn(...args)
ok = true ok = true
if(value instanceof cxt.Promise) { if(value instanceof cxt.window.Promise) {
set_record_call(cxt) set_record_call(cxt)
} }
return value return value
@@ -417,7 +420,7 @@ const __trace_call = (cxt, fn, context, args, errormessage, is_new = false) => {
value = undefined value = undefined
} }
ok = true ok = true
if(value instanceof cxt.Promise) { if(value instanceof cxt.window.Promise) {
set_record_call(cxt) set_record_call(cxt)
} }
return value return value