diff --git a/src/ldo.c b/src/ldo.c index fa74f34..2abd935 100644 --- a/src/ldo.c +++ b/src/ldo.c @@ -283,27 +283,7 @@ void record_depth_of_global_function (lua_State *L, CallInfo *ci) { return; int g = GETARG_Bx(i); /* global index */ lua_assert(ttisstring(&p->k[g])); - const char *name = svalue(&p->k[g]); - int depth = ci - L->base_ci; - /* Maintain a global table mapping from function name to call-stack depth - * at first call to it. - * - * Won't be perfect; might get confused by shadowing locals. But we can't - * be perfect without a bidirectional mapping between interpreter state - * and source code. Which would make Lua either a lot less dynamic or a - * a lot more like Smalltalk. */ - // push table - luaL_newmetatable(L, "__teliva_call_graph_depth"); - int cgt = lua_gettop(L); - // if key doesn't already exist, set it - lua_getfield(L, cgt, name); - if (lua_isnil(L, -1)) { - lua_pushinteger(L, depth); - lua_setfield(L, cgt, name); - } - // clean up - lua_pop(L, 1); // key - lua_pop(L, 1); // table + assign_call_graph_depth_to_name(L, ci - L->base_ci, svalue(&p->k[g])); } diff --git a/src/teliva.c b/src/teliva.c index 5ef3325..777648b 100644 --- a/src/teliva.c +++ b/src/teliva.c @@ -254,6 +254,28 @@ void draw_highlighted_definition_name(const char* definition_name) { addstr(" "); } +void assign_call_graph_depth_to_name(lua_State* L, int depth, const char* name) { + /* Maintain a global table mapping from function name to call-stack depth + * at first call to it. + * + * Won't be perfect; might get confused by shadowing locals. But we can't + * be perfect without a bidirectional mapping between interpreter state + * and source code. Which would make Lua either a lot less dynamic or a + * a lot more like Smalltalk. */ + // push table + luaL_newmetatable(L, "__teliva_call_graph_depth"); + int cgt = lua_gettop(L); + // if key doesn't already exist, set it + lua_getfield(L, cgt, name); + if (lua_isnil(L, -1)) { + lua_pushinteger(L, depth); + lua_setfield(L, cgt, name); + } + // clean up + lua_pop(L, 1); // key + lua_pop(L, 1); // table +} + /* return true if submitted */ static int edit_current_definition(lua_State* L); static void recent_changes_view(lua_State* L); diff --git a/src/teliva.h b/src/teliva.h index bf75bc9..f783eec 100644 --- a/src/teliva.h +++ b/src/teliva.h @@ -162,6 +162,7 @@ extern int load_editor_buffer_to_current_definition_in_image(lua_State* L); extern void save_to_current_definition_and_editor_buffer(lua_State* L, const char* definition); extern void save_editor_state(int rowoff, int coloff, int cy, int cx); +extern void assign_call_graph_depth_to_name(lua_State* L, int depth, const char* name); extern void append_to_audit_log(lua_State* L, const char* buffer); /* Standard UI elements */