aboutsummaryrefslogtreecommitdiff
path: root/lapi.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2023-12-14 11:41:57 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2023-12-14 11:41:57 -0300
commit4eda1acafa1a69224b2d4f786cf1ec8f7a4d9ac5 (patch)
tree474a56c1cbb1b109f945cb3765c6ebc918d35b37 /lapi.c
parentad73b332240ef5b9bab1517517f63a1425dc7545 (diff)
downloadlua-4eda1acafa1a69224b2d4f786cf1ec8f7a4d9ac5.tar.gz
lua-4eda1acafa1a69224b2d4f786cf1ec8f7a4d9ac5.tar.bz2
lua-4eda1acafa1a69224b2d4f786cf1ec8f7a4d9ac5.zip
Cleaner protocol between 'lua_dump' and writer function
'lua_dump' signals to the writer function the end of a dump, so that is has more freedom when using the stack.
Diffstat (limited to 'lapi.c')
-rw-r--r--lapi.c32
1 files changed, 7 insertions, 25 deletions
diff --git a/lapi.c b/lapi.c
index 374b3872..fd9ec4e4 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1116,36 +1116,18 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
1116 1116
1117 1117
1118/* 1118/*
1119** Dump a function, calling 'writer' to write its parts. Because the 1119** Dump a Lua function, calling 'writer' to write its parts. Ensure
1120** writer can use the stack in unkown ways, this function should not 1120** the stack returns with its original size.
1121** push things on the stack, but it must anchor an auxiliary table
1122** used by 'luaU_dump'. To do so, it creates the table, anchors the
1123** function that is on the stack in the table, and substitutes the
1124** table for the function in the stack.
1125*/ 1121*/
1126
1127LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { 1122LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
1128 int status; 1123 int status;
1129 StkId fstk; /* pointer to function */ 1124 ptrdiff_t otop = savestack(L, L->top.p); /* original top */
1130 TValue *o; 1125 TValue *f = s2v(L->top.p - 1); /* function to be dumped */
1131 lua_lock(L); 1126 lua_lock(L);
1132 api_checknelems(L, 1); 1127 api_checknelems(L, 1);
1133 fstk = L->top.p - 1; 1128 api_check(L, isLfunction(f), "Lua function expected");
1134 o = s2v(fstk); 1129 status = luaU_dump(L, clLvalue(f)->p, writer, data, strip);
1135 if (!isLfunction(o)) 1130 L->top.p = restorestack(L, otop); /* restore top */
1136 status = 1;
1137 else {
1138 LClosure *f = clLvalue(o);
1139 ptrdiff_t fidx = savestack(L, fstk); /* function index */
1140 Table *h = luaH_new(L); /* auxiliary table used by 'luaU_dump' */
1141 sethvalue2s(L, L->top.p, h); /* anchor it (luaH_set may call GC) */
1142 L->top.p++; /* (assume extra slot) */
1143 luaH_set(L, h, o, o); /* anchor function into table */
1144 setobjs2s(L, fstk, L->top.p - 1); /* move table over function */
1145 L->top.p--; /* stack back to initial size */
1146 status = luaU_dump(L, f->p, writer, data, strip, h);
1147 setclLvalue2s(L, restorestack(L, fidx), f); /* put function back */
1148 }
1149 lua_unlock(L); 1131 lua_unlock(L);
1150 return status; 1132 return status;
1151} 1133}