diff --git a/index.html b/index.html index 2d7f25a..8120c4e 100644 --- a/index.html +++ b/index.html @@ -380,6 +380,8 @@ window.log = console.log if(new URLSearchParams(window.location.search).get('leporello') == null) { + await import('./src/launch.js'); + /* const {init} = await import('./src/index.js'); ( document.readyState == 'complete' @@ -390,6 +392,7 @@ ).then(() => { init(document.getElementById('app')) }) + */ } diff --git a/src/cmd.js b/src/cmd.js index 3be1d05..3bef6b7 100644 --- a/src/cmd.js +++ b/src/cmd.js @@ -759,6 +759,7 @@ const load_dir = (state, dir) => { collect_files(dir).map(f => [f.path, f.contents]) ) + // Clear parse cache and rerun code return rerun_code({ ...state, // remove cache @@ -773,6 +774,15 @@ const create_file = (state, dir, current_module) => { return {...load_dir(state, dir), current_module} } +const open_run_window = state => { + // After we reopen run window, we should reload external modules in the + // context of new window. Clear external_imports_cache + return rerun_code({ + ...state, + external_imports_cache: null, + }) +} + export const get_initial_state = state => { const with_files = state.project_dir == null ? state @@ -798,7 +808,7 @@ export const get_initial_state = state => { export const COMMANDS = { input, - rerun_code, + open_run_window, load_dir, create_file, step_into, diff --git a/src/effects.js b/src/effects.js index d8fc228..e414a04 100644 --- a/src/effects.js +++ b/src/effects.js @@ -8,13 +8,21 @@ import { import {FLAGS} from './feature_flags.js' import {exec} from './index.js' +// Imports in the context of `run_window`, so global variables in loaded +// modules refer to that window's context +const import_in_run_window = url => { + return new globalThis.run_window.Function('url', ` + return import(url) + `)(url) +} + const load_external_imports = async state => { if(state.loading_external_imports_state == null) { return } const urls = state.loading_external_imports_state.external_imports const results = await Promise.allSettled( - urls.map(u => import( + urls.map(u => import_in_run_window( /^\w+:\/\//.test(u) ? // starts with protocol, import as is u diff --git a/src/index.js b/src/index.js index 94d953c..021c137 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,4 @@ -import {COMMANDS, get_initial_state} from './cmd.js' -import {active_frame} from './calltree.js' +import {get_initial_state} from './cmd.js' import {UI} from './editor/ui.js' import {EFFECTS, render_initial_state, render_common_side_effects} from './effects.js' import {load_dir} from './filesystem.js' @@ -62,10 +61,13 @@ const read_modules = async () => { } } +let COMMANDS let ui let state -export const init = (container) => { +export const init = (container, _COMMANDS) => { + COMMANDS = _COMMANDS + set_error_handler(window) read_modules().then(initial_state => { diff --git a/src/launch.js b/src/launch.js new file mode 100644 index 0000000..239f4e1 --- /dev/null +++ b/src/launch.js @@ -0,0 +1,6 @@ +// external +import {init} from './index.js' + +import {COMMANDS} from './cmd.js' + +init(globalThis.document.getElementById('app'), COMMANDS)