extract a function

This commit is contained in:
Kartik K. Agaram 2022-01-03 10:23:25 -08:00
parent 1261f3f3c9
commit 14ab0729c9
3 changed files with 24 additions and 21 deletions

View File

@ -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]));
}

View File

@ -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);

View File

@ -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 */