refactor globals

This commit is contained in:
Dmitry Vasilev
2023-06-15 23:55:06 +03:00
parent fdbe01249d
commit 2f5db0452c
7 changed files with 75 additions and 40 deletions

View File

@@ -77,7 +77,7 @@ const run_code = (s, dirty_files) => {
return s.files[module]
}
})
}, s.globals)
const state = {
...s,
@@ -839,12 +839,15 @@ const create_file = (state, dir, current_module) => {
return {...load_dir(state, dir), current_module}
}
const open_run_window = state => {
const open_run_window = (state, globals) => {
// After we reopen run window, we should reload external modules in the
// context of new window. Clear external_imports_cache
return run_code({
...state,
globals,
external_imports_cache: null,
// Bust parse result cache because list of globals may change
parse_result: null,
})
}

View File

@@ -3,8 +3,6 @@
import {set_push, set_diff, set_union, map_object, map_find, uniq} from './utils.js'
import {collect_destructuring_identifiers, collect_imports, ancestry, find_leaf} from './ast_utils.js'
import {globals} from './globals.js'
const map_find_definitions = (nodes, mapper) => {
const result = nodes.map(mapper)
const undeclared = result.reduce(
@@ -74,7 +72,14 @@ const add_trivial_definition = node => {
*/
// TODO in same pass find already declared
export const find_definitions = (ast, scope = {}, closure_scope = {}, module_name) => {
export const find_definitions = (ast, globals, scope = {}, closure_scope = {}, module_name) => {
// sanity check
if(!globals instanceof Set) {
throw new Error('not a set')
}
if(ast.type == 'identifier'){
if(ast.definition != null) {
// Definition previously added by add_trivial_definition
@@ -112,7 +117,7 @@ export const find_definitions = (ast, scope = {}, closure_scope = {}, module_nam
)
const local_scope = children_with_scope.scope
const {nodes, undeclared, closed} = map_find_definitions(children_with_scope.children, cs =>
find_definitions(cs.node, {...scope, ...cs.scope}, local_scope, module_name)
find_definitions(cs.node, globals, {...scope, ...cs.scope}, local_scope, module_name)
)
return {
node: {...ast, children: nodes},
@@ -125,7 +130,7 @@ export const find_definitions = (ast, scope = {}, closure_scope = {}, module_nam
a.value, a
]))
const {nodes, undeclared, closed} = map_find_definitions(ast.children,
node => find_definitions(node, {...scope, ...closure_scope, ...args_scope})
node => find_definitions(node, globals, {...scope, ...closure_scope, ...args_scope})
)
const next_closed = set_diff(closed, new Set(args_identifiers.map(a => a.value)))
return {
@@ -153,7 +158,7 @@ export const find_definitions = (ast, scope = {}, closure_scope = {}, module_nam
}
const {nodes, undeclared, closed} = map_find_definitions(children,
c => find_definitions(c, scope, closure_scope)
c => find_definitions(c, globals, scope, closure_scope)
)
return {

View File

@@ -1,4 +0,0 @@
export const globals = new Set(Object.getOwnPropertyNames(globalThis))
// Not available in node.js, but add to use in tests
globals.add('fetch')

View File

@@ -67,7 +67,10 @@ export const open_run_window = state => {
// event fired. TODO: better register SW explicitly and don't rely on
// already registered SW?
const onload = () => {
exec('open_run_window')
exec(
'open_run_window',
new Set(Object.getOwnPropertyNames(globalThis.run_window))
)
}
const add_load_handler = () => {
@@ -170,7 +173,10 @@ export const init = (container, _COMMANDS) => {
render_initial_state(ui, state)
open_run_iframe(state, () => {
exec('open_run_window')
exec(
'open_run_window',
new Set(Object.getOwnPropertyNames(globalThis.run_window))
)
})
})
}

View File

@@ -1580,7 +1580,7 @@ const deduce_fn_names = node => {
return do_deduce_fn_names(node, null)[0]
}
export const parse = (str, is_module = false, module_name) => {
export const parse = (str, globals, is_module = false, module_name) => {
// Add string to node for debugging
// TODO remove, only need for debug
@@ -1629,6 +1629,7 @@ export const parse = (str, is_module = false, module_name) => {
} else {
const {node, undeclared} = find_definitions(
update_children(result.value),
globals,
null,
null,
module_name
@@ -1675,7 +1676,7 @@ export const print_debug_node = node => {
return stringify(do_print_debug_node(node))
}
const do_load_modules = (module_names, loader, already_loaded) => {
const do_load_modules = (module_names, loader, already_loaded, globals) => {
const parsed = module_names
.filter(module_name => already_loaded[module_name] == null)
.map(module_name => {
@@ -1686,7 +1687,7 @@ const do_load_modules = (module_names, loader, already_loaded) => {
// Allows cache parse result
return [module_name, m]
} else if(typeof(m) == 'string') {
return [module_name, parse(m, true, module_name)]
return [module_name, parse(m, globals, true, module_name)]
} else {
throw new Error('illegal state')
}
@@ -1728,7 +1729,12 @@ const do_load_modules = (module_names, loader, already_loaded) => {
// TODO when refactor this function to async, do not forget that different
// deps can be loaded independently. So dont just put `await Promise.all(`
// here
const loaded_deps = do_load_modules(deps, loader, {...already_loaded, ...modules})
const loaded_deps = do_load_modules(
deps,
loader,
{...already_loaded, ...modules},
globals
)
if(loaded_deps.ok) {
return {
ok: true,
@@ -1770,11 +1776,11 @@ const do_load_modules = (module_names, loader, already_loaded) => {
}
}
export const load_modules = (entry_module, loader) => {
export const load_modules = (entry_module, loader, globals) => {
// TODO check_imports. detect cycles while modules are loading, in
// do_load_modules
const result = do_load_modules([entry_module], loader, {})
const result = do_load_modules([entry_module], loader, {}, globals)
if(!result.ok) {
return result
} else {