mirror of
https://github.com/leporello-js/leporello-js
synced 2026-01-13 21:14:28 -08:00
refactor
This commit is contained in:
@@ -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 => {
|
||||||
|
|||||||
16
src/eval.js
16
src/eval.js
@@ -63,14 +63,8 @@ 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) => {
|
||||||
@@ -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
|
||||||
|
|||||||
@@ -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])
|
||||||
|
|||||||
Reference in New Issue
Block a user