From 7723d7d09d87c350fc211b621c72574e107bafde Mon Sep 17 00:00:00 2001 From: Dmitry Vasilev Date: Tue, 15 Nov 2022 14:43:42 +0800 Subject: [PATCH] logs in async calls --- src/calltree.js | 6 +++--- src/cmd.js | 43 +++++++++++++++++++++---------------------- test/test.js | 3 +++ 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/calltree.js b/src/calltree.js index f80cb1e..1e6337a 100644 --- a/src/calltree.js +++ b/src/calltree.js @@ -838,9 +838,9 @@ const navigate_logs_increment = (state, increment) => { } const navigate_logs_position = (state, log_position) => { - const node = find_node( - root_calltree_node(state), - n => n.id == state.logs.logs[log_position].id + const node = find_calltree_node( + state, + state.logs.logs[log_position].id ) const {state: next, effects} = select_arguments( expand_path(jump_calltree_node(state, node).state, node), diff --git a/src/cmd.js b/src/cmd.js index dc62c61..2740670 100644 --- a/src/cmd.js +++ b/src/cmd.js @@ -15,29 +15,27 @@ import { find_call, find_call_node, set_active_calltree_node } from './calltree.js' +const collect_logs = call => + collect_nodes_with_parents(call, n => n.is_log) + .map(({parent, node}) => ( + { + id: node.id, + toplevel: parent.toplevel, + module: parent.toplevel + ? parent.module + : parent.fn.__location.module, + parent_name: parent.fn?.name, + args: node.args, + log_fn_name: node.fn.name, + } + )) + const apply_eval_result = (state, eval_result) => { // TODO what if console.log called from native fn (like Array::map)? - // Currently it is not recorded. Maybe we should patch monkey patch console? - const logs = ( - eval_result.calltree[state.entrypoint] == null - ? [] - : collect_nodes_with_parents( - eval_result.calltree[state.entrypoint].calls, - n => n.is_log, - ) - ) - .map(({parent, node}) => ( - { - id: node.id, - toplevel: parent.toplevel, - module: parent.toplevel - ? parent.module - : parent.fn.__location.module, - parent_name: parent.fn?.name, - args: node.args, - log_fn_name: node.fn.name, - } - )) + // Currently it is not recorded. Maybe we should monkey patch `console`? + const logs = collect_logs( + root_calltree_node({...state, calltree: eval_result.calltree}) + ) return { ...state, @@ -740,7 +738,8 @@ const move_cursor = (s, index) => { const on_async_call = (state, call) => { return {...state, - async_calls: [...(state.async_calls ?? []), call] + async_calls: [...(state.async_calls ?? []), call], + logs: {...state.logs, logs: state.logs.logs.concat(collect_logs(call))}, } } diff --git a/test/test.js b/test/test.js index b15c1bf..f2c83a5 100644 --- a/test/test.js +++ b/test/test.js @@ -2314,6 +2314,7 @@ const y = x()` } const fn2 = () => { + console.log(1) } // Use Function constructor to exec impure code for testing @@ -2341,6 +2342,8 @@ const y = x()` const state = COMMANDS.on_async_call(i, call) assert_equal(state.async_calls, [call]) + assert_equal(state.logs.logs.length, 1) + // Expand call const {state: expanded} = COMMANDS.calltree.click(state, call.id) assert_equal(expanded.async_calls[0].children[0].fn.name, 'fn2')