about summary refs log tree commit diff stats
path: root/src/lua.c
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-11-13 17:48:04 -0800
committerKartik K. Agaram <vc@akkartik.com>2021-11-13 17:48:04 -0800
commit625a66e1961610742f5eb698fa9dd84454958b2e (patch)
tree942d4376226d0fee77d284f1d0b5969c49c3d2c6 /src/lua.c
parent12b2a44cf6413ef0c51c4e13af1c03369f9e803f (diff)
downloadteliva-625a66e1961610742f5eb698fa9dd84454958b2e.tar.gz
avoid side-effects on the Lua stack
Diffstat (limited to 'src/lua.c')
-rw-r--r--src/lua.c5
1 files changed, 5 insertions, 0 deletions
diff --git a/src/lua.c b/src/lua.c
index f226828..327cadd 100644
--- a/src/lua.c
+++ b/src/lua.c
@@ -316,6 +316,7 @@ void save_to_current_definition_and_editor_buffer (lua_State *L, const char *def
     if (contents != NULL)
       write(outfd, contents, strlen(contents));
     close(outfd);
+    lua_settop(L, 0);
 }
 
 
@@ -328,13 +329,16 @@ static void read_contents (lua_State *L, char *filename, char *out) {
 
 /* table to update is at top of stack */
 static void update_definition (lua_State *L, const char *name, char *out) {
+    lua_getglobal(L, "teliva_program");
     lua_pushstring(L, out);
     assert(strlen(name) > 0);
     lua_setfield(L, -2, name);
+    lua_settop(L, 0);
 }
 
 
 static void save_image (lua_State *L) {
+    lua_getglobal(L, "teliva_program");
     int table = lua_gettop(L);
     FILE* fp = fopen(Image_name, "w");
     fprintf(fp, "teliva_program = {\n");
@@ -347,6 +351,7 @@ static void save_image (lua_State *L) {
     }
     fprintf(fp, "}\n");
     fclose(fp);
+    lua_settop(L, 0);
 }