This commit is contained in:
Dmitry Vasilev
2022-12-19 20:46:40 +08:00
parent 1a55dbf619
commit 23429845d2
3 changed files with 13 additions and 27 deletions

View File

@@ -6,23 +6,13 @@
import {el, stringify, scrollIntoViewIfNeeded} from './domutils.js' import {el, stringify, scrollIntoViewIfNeeded} from './domutils.js'
// TODO only test for globalThis.<GlobalObject> because we only eval code in
// another window
const has_custom_toString = object => const has_custom_toString = object =>
object.toString != globalThis.run_window.Object.prototype.toString object.toString != globalThis.run_window.Object.prototype.toString
&&
object.toString != Object.prototype.toString
const isError = object => const isError = object =>
object instanceof Error
||
object instanceof globalThis.run_window.Error object instanceof globalThis.run_window.Error
const isPromise = object => const isPromise = object =>
object instanceof Promise
||
object instanceof globalThis.run_window.Promise object instanceof globalThis.run_window.Promise
const displayed_entries = object => { const displayed_entries = object => {

View File

@@ -63,15 +63,9 @@ type Node = ToplevelCall | Call
// TODO just export const Iframe_Function? // TODO just export const Iframe_Function?
const make_function = (...args) => { 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 const fn_constructor = globalThis.run_window.Function
return new fn_constructor(...args) return new fn_constructor(...args)
} }
}
const codegen_function_expr = (node, cxt) => { const codegen_function_expr = (node, cxt) => {
const do_codegen = n => codegen(n, cxt) const do_codegen = n => codegen(n, cxt)
@@ -275,12 +269,7 @@ export const eval_modules = (
// TODO bug if module imported twice, once as external and as regular // TODO bug if module imported twice, once as external and as regular
patch_promise( patch_promise(globalThis.run_window)
globalThis.run_window
??
// Code executed in test env
globalThis
)
const is_async = has_toplevel_await(parse_result.modules) const is_async = has_toplevel_await(parse_result.modules)
@@ -994,8 +983,7 @@ const do_eval_frame_expr = (node, scope, callsleft) => {
ok = true ok = true
value = - expr.result.value value = - expr.result.value
} else if(node.operator == 'await') { } else if(node.operator == 'await') {
const run_window = globalThis.run_window ?? globalThis if(expr.result.value instanceof globalThis.run_window.Promise.Original) {
if(expr.result.value instanceof run_window.Promise.Original) {
const status = expr.result.value.status const status = expr.result.value.status
if(status == null) { if(status == null) {
// Promise must be already resolved // Promise must be already resolved

View File

@@ -3,7 +3,15 @@ import {eval_tree, eval_frame} from '../src/eval.js'
import {active_frame, pp_calltree} from '../src/calltree.js' import {active_frame, pp_calltree} from '../src/calltree.js'
import {COMMANDS} from '../src/cmd.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) => export const parse_modules = (entry, modules) =>
load_modules(entry, module_name => modules[module_name]) load_modules(entry, module_name => modules[module_name])