From 23429845d2e29c48dcf4cacb794b17e41241e30a Mon Sep 17 00:00:00 2001 From: Dmitry Vasilev Date: Mon, 19 Dec 2022 20:46:40 +0800 Subject: [PATCH] refactor --- src/editor/value_explorer.js | 10 ---------- src/eval.js | 20 ++++---------------- test/utils.js | 10 +++++++++- 3 files changed, 13 insertions(+), 27 deletions(-) diff --git a/src/editor/value_explorer.js b/src/editor/value_explorer.js index 23be626..e145359 100644 --- a/src/editor/value_explorer.js +++ b/src/editor/value_explorer.js @@ -6,23 +6,13 @@ import {el, stringify, scrollIntoViewIfNeeded} from './domutils.js' - -// TODO only test for globalThis. because we only eval code in -// another window - const has_custom_toString = object => object.toString != globalThis.run_window.Object.prototype.toString - && - object.toString != Object.prototype.toString const isError = object => - object instanceof Error - || object instanceof globalThis.run_window.Error const isPromise = object => - object instanceof Promise - || object instanceof globalThis.run_window.Promise const displayed_entries = object => { diff --git a/src/eval.js b/src/eval.js index c4f08cd..058774c 100644 --- a/src/eval.js +++ b/src/eval.js @@ -63,14 +63,8 @@ type Node = ToplevelCall | Call // TODO just export const Iframe_Function? const make_function = (...args) => { - if(globalThis.run_window == null) { - // Code is executed in test env - return new Function(...args) - } else { - // Code run in browser and user opened run_window - const fn_constructor = globalThis.run_window.Function - return new fn_constructor(...args) - } + const fn_constructor = globalThis.run_window.Function + return new fn_constructor(...args) } const codegen_function_expr = (node, cxt) => { @@ -275,12 +269,7 @@ export const eval_modules = ( // TODO bug if module imported twice, once as external and as regular - patch_promise( - globalThis.run_window - ?? - // Code executed in test env - globalThis - ) + patch_promise(globalThis.run_window) const is_async = has_toplevel_await(parse_result.modules) @@ -994,8 +983,7 @@ const do_eval_frame_expr = (node, scope, callsleft) => { ok = true value = - expr.result.value } else if(node.operator == 'await') { - const run_window = globalThis.run_window ?? globalThis - if(expr.result.value instanceof run_window.Promise.Original) { + if(expr.result.value instanceof globalThis.run_window.Promise.Original) { const status = expr.result.value.status if(status == null) { // Promise must be already resolved diff --git a/test/utils.js b/test/utils.js index f0b6109..b3e05f0 100644 --- a/test/utils.js +++ b/test/utils.js @@ -3,7 +3,15 @@ import {eval_tree, eval_frame} from '../src/eval.js' import {active_frame, pp_calltree} from '../src/calltree.js' import {COMMANDS} from '../src/cmd.js' -Object.assign(globalThis, {log: console.log}) +Object.assign(globalThis, + { + // for convenince, to type just `log` instead of `console.log` + log: console.log, + + // For test env, set globalThis.run_window to just globalThis + run_window: globalThis, + } +) export const parse_modules = (entry, modules) => load_modules(entry, module_name => modules[module_name])