This commit is contained in:
Dmitry Vasilev
2022-12-02 08:17:49 +08:00
parent 0845a87960
commit 747bceda7b
4 changed files with 41 additions and 17 deletions

4
src/effects.js vendored
View File

@@ -129,6 +129,7 @@ export const render_initial_state = (ui, state) => {
ui.editor.switch_session(state.current_module) ui.editor.switch_session(state.current_module)
} }
// TODO remove async
export const render_common_side_effects = async (prev, next, command, ui) => { export const render_common_side_effects = async (prev, next, command, ui) => {
if( if(
prev.project_dir != next.project_dir prev.project_dir != next.project_dir
@@ -188,7 +189,8 @@ export const render_common_side_effects = async (prev, next, command, ui) => {
} else { } else {
await unwrap_settled_promises(next.calltree) // TODO remove
// await unwrap_settled_promises(next.calltree)
if( if(
prev.calltree == null prev.calltree == null

View File

@@ -12,6 +12,8 @@ import {
map_tree, map_tree,
} from './ast_utils.js' } from './ast_utils.js'
import {has_toplevel_await} from './find_definitions.js'
// TODO: fix error messages. For example, "__fn is not a function" // TODO: fix error messages. For example, "__fn is not a function"
/* /*
@@ -255,6 +257,16 @@ const codegen = (node, cxt, parent) => {
} }
} }
/* TODO remove
const sync_promise = value => {
if(value instanceof run_window.Promise.Original) {
return value
} else {
return {is_sync_promise: true, then: fn => sync_promise(fn(value))}
}
}
*/
export const eval_modules = ( export const eval_modules = (
parse_result, parse_result,
external_imports, external_imports,
@@ -266,6 +278,8 @@ 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
const is_async = has_toplevel_await(parse_result.modules)
const codestring = const codestring =
` `
let children, prev_children let children, prev_children
@@ -530,7 +544,7 @@ export const eval_modules = (
} }
} }
const run = () => { const run = ${is_async ? 'async' : ''} () => {
is_recording_deferred_calls = false is_recording_deferred_calls = false
@@ -554,8 +568,7 @@ export const eval_modules = (
module: current_module, module: current_module,
id: call_counter++ id: call_counter++
} }
__modules[current_module] = __modules[current_module] = ${is_async ? 'await (async' : '('} () => {
(() => {
try { try {
const __exports = {}; const __exports = {};
${codegen(parse_result.modules[m], {module: m})}; ${codegen(parse_result.modules[m], {module: m})};
@@ -639,12 +652,16 @@ export const eval_modules = (
? actions.run() ? actions.run()
: calltree_actions.find_call(location) : calltree_actions.find_call(location)
return { const make_result = result => ({
modules: result.modules, modules: result.modules,
calltree: assign_code(parse_result.modules, result.calltree), calltree: assign_code(parse_result.modules, result.calltree),
call: result.call, call: result.call,
calltree_actions, calltree_actions,
} })
return is_async
? result.then(make_result)
: make_result(result)
} }
// TODO: assign_code: benchmark and use imperative version for perf? // TODO: assign_code: benchmark and use imperative version for perf?

View File

@@ -202,6 +202,11 @@ export const topsort_modules = (modules) => {
) )
} }
export const has_toplevel_await = modules =>
Object.values(modules).some(m =>
m.children.find(c => c.type == 'unary' && c.operator == 'await' ) != null
)
// TODO not implemented // TODO not implemented
// TODO detect cycles when loading modules // TODO detect cycles when loading modules
export const check_imports = modules => { export const check_imports = modules => {

View File

@@ -2593,7 +2593,7 @@ const y = x()`
const code = ` const code = `
const x = async () => 123 const x = async () => 123
const y = async () => await x() const y = async () => await x()
y() await y()
` `
const s = test_initial_state(code) const s = test_initial_state(code)
const move = COMMANDS.move_cursor(s, code.indexOf('await x()')).state const move = COMMANDS.move_cursor(s, code.indexOf('await x()')).state