From c37b6ef78e9ce882dfe878abf075d1de87546874 Mon Sep 17 00:00:00 2001 From: Dmitry Vasilev Date: Thu, 15 Feb 2024 19:46:05 +0800 Subject: [PATCH] refactor window init --- src/runtime/record_io.js | 16 ++-------------- src/runtime/runtime.js | 15 +++++++++------ 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/src/runtime/record_io.js b/src/runtime/record_io.js index 409190e..68115d2 100644 --- a/src/runtime/record_io.js +++ b/src/runtime/record_io.js @@ -253,19 +253,7 @@ const patch_Date = (window) => { io_patch(window, ['Date', 'now']) } -export const apply_io_patches = (cxt) => { - const window = cxt.window - - // set current context - window.__cxt = cxt - - if(cxt.window.__io_patched) { - // Patches already applied, do nothing - return - } else { - cxt.window.__io_patched = true - } - +export const apply_io_patches = (window) => { io_patch(window, ['Math', 'random']) io_patch(window, ['setTimeout']) @@ -280,7 +268,7 @@ export const apply_io_patches = (cxt) => { io_patch(window, ['fetch']) // Check if Response is defined, for node.js - if(cxt.window.Response != null) { + if(window.Response != null) { const Response_methods = [ 'arrayBuffer', 'blob', diff --git a/src/runtime/runtime.js b/src/runtime/runtime.js index a232b19..777c537 100644 --- a/src/runtime/runtime.js +++ b/src/runtime/runtime.js @@ -71,9 +71,10 @@ const do_run = function*(module_fns, cxt, io_trace){ io_trace_abort_replay, } - defineMultiversion(cxt.window) + // Set current context + cxt.window.__cxt = cxt + apply_promise_patch(cxt) - apply_io_patches(cxt) for(let i = 0; i < module_fns.length; i++) { const {module, fn} = module_fns[i] @@ -140,6 +141,12 @@ const do_run = function*(module_fns, cxt, io_trace){ } export const run = gen_to_promise(function*(module_fns, cxt, io_trace) { + if(!cxt.window.__is_initialized) { + defineMultiversion(cxt.window) + apply_io_patches(cxt.window) + cxt.window.__is_initialized = true + } + const result = yield* do_run(module_fns, cxt, io_trace) if(result.rt_cxt.io_trace_is_replay_aborted) { @@ -400,10 +407,6 @@ const __trace = (cxt, fn, name, argscount, __location, get_closure, has_versione } const defineMultiversion = window => { - if(window.defineMultiversionDone) { - return - } - window.defineMultiversionDone = true defineMultiversionArray(window) defineMultiversionSet(window) defineMultiversionMap(window)