Browse Source

new facility for dumping chunks

Roberto Ierusalimschy 23 years ago
parent
commit
118e9cd843
3 changed files with 47 additions and 4 deletions
  1. 19 1
      lapi.c
  2. 21 1
      lbaselib.c
  3. 7 2
      lua.h

+ 19 - 1
lapi.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lapi.c,v 1.213 2002/09/20 17:01:24 roberto Exp roberto $
+** $Id: lapi.c,v 1.214 2002/10/25 20:05:28 roberto Exp roberto $
 ** Lua API
 ** See Copyright Notice in lua.h
 */
@@ -127,6 +127,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) {
 }
 
 
+
 /*
 ** basic stack manipulation
 */
@@ -681,6 +682,23 @@ LUA_API int lua_load (lua_State *L, lua_Chunkreader reader, void *data,
 }
 
 
+LUA_API int lua_dump (lua_State *L, lua_Chunkwriter writer, void *data) {
+  int status;
+  TObject *o;
+  lua_lock(L);
+  api_checknelems(L, 1);
+  o = L->top - 1;
+  if (isLfunction(o) && clvalue(o)->l.nupvalues == 0) {
+    luaU_dump(L, clvalue(o)->l.p, writer, data);
+    status = 1;
+  }
+  else
+    status = 0;
+  lua_unlock(L);
+  return status;
+}
+
+
 /*
 ** Garbage-collection functions
 */

+ 21 - 1
lbaselib.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lbaselib.c,v 1.100 2002/10/22 19:41:08 roberto Exp roberto $
+** $Id: lbaselib.c,v 1.101 2002/10/25 20:05:28 roberto Exp roberto $
 ** Basic library
 ** See Copyright Notice in lua.h
 */
@@ -271,6 +271,25 @@ static int luaB_loadstring (lua_State *L) {
 }
 
 
+static int writer (lua_State *L, const void* b, size_t size, void* B) {
+  (void)L;
+  luaL_addlstring((luaL_Buffer*) B, b, size);
+  return 1;
+}
+
+
+static int luaB_stringdump (lua_State *L) {
+  luaL_Buffer b;
+  luaL_check_type(L, 1, LUA_TFUNCTION);
+  luaL_buffinit(L,&b);
+  if (!lua_dump(L, writer, &b))
+    luaL_error(L, "unable to dump given function");
+  luaL_pushresult(&b);
+  return 1;
+}
+
+
+
 static int luaB_loadfile (lua_State *L) {
   const char *fname = luaL_opt_string(L, 1, NULL);
   return passresults(L, luaL_loadfile(L, fname));
@@ -524,6 +543,7 @@ static const luaL_reg base_funcs[] = {
   {"collectgarbage", luaB_collectgarbage},
   {"gcinfo", luaB_gcinfo},
   {"loadfile", luaB_loadfile},
+  {"stringdump", luaB_stringdump},
   {"dofile", luaB_dofile},
   {"loadstring", luaB_loadstring},
   {"require", luaB_require},

+ 7 - 2
lua.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lua.h,v 1.159 2002/10/22 17:21:25 roberto Exp roberto $
+** $Id: lua.h,v 1.160 2002/10/25 20:05:28 roberto Exp roberto $
 ** Lua - An Extensible Extension Language
 ** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil
 ** http://www.lua.org	mailto:[email protected]
@@ -52,10 +52,13 @@ typedef int (*lua_CFunction) (lua_State *L);
 
 
 /*
-** functions that read blocks when loading Lua chunk
+** functions that read/write blocks when loading/dumping Lua chunks
 */
 typedef const char * (*lua_Chunkreader) (lua_State *L, void *ud, size_t *sz);
 
+typedef int (*lua_Chunkwriter) (lua_State *L, const void* p,
+                                size_t sz, void* ud);
+
 
 /*
 ** basic types
@@ -192,6 +195,8 @@ LUA_API int   lua_pcall (lua_State *L, int nargs, int nresults, int errfunc);
 LUA_API int   lua_load (lua_State *L, lua_Chunkreader reader, void *dt,
                         const char *chunkname);
 
+LUA_API int lua_dump (lua_State *L, lua_Chunkwriter writer, void *data);
+
 
 /*
 ** coroutine functions