diff options
Diffstat (limited to 'lapi.c')
-rw-r--r-- | lapi.c | 32 |
1 files changed, 7 insertions, 25 deletions
@@ -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 | |||
1127 | LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { | 1122 | LUA_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 | } |