aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-10-25 18:31:28 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-10-25 18:31:28 -0300
commit118e9cd843d2956d64b86bfdd70f89919153e471 (patch)
tree1b847891b5d725bd34b9335cd47fb982a904ea8b
parentde00d0d0add8686917bc28b7fe0f2974c155f22e (diff)
downloadlua-118e9cd843d2956d64b86bfdd70f89919153e471.tar.gz
lua-118e9cd843d2956d64b86bfdd70f89919153e471.tar.bz2
lua-118e9cd843d2956d64b86bfdd70f89919153e471.zip
new facility for dumping chunks
-rw-r--r--lapi.c20
-rw-r--r--lbaselib.c22
-rw-r--r--lua.h9
3 files changed, 47 insertions, 4 deletions
diff --git a/lapi.c b/lapi.c
index fbc122df..2c5b8a06 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.213 2002/09/20 17:01:24 roberto Exp roberto $ 2** $Id: lapi.c,v 1.214 2002/10/25 20:05:28 roberto Exp roberto $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -127,6 +127,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) {
127} 127}
128 128
129 129
130
130/* 131/*
131** basic stack manipulation 132** basic stack manipulation
132*/ 133*/
@@ -681,6 +682,23 @@ LUA_API int lua_load (lua_State *L, lua_Chunkreader reader, void *data,
681} 682}
682 683
683 684
685LUA_API int lua_dump (lua_State *L, lua_Chunkwriter writer, void *data) {
686 int status;
687 TObject *o;
688 lua_lock(L);
689 api_checknelems(L, 1);
690 o = L->top - 1;
691 if (isLfunction(o) && clvalue(o)->l.nupvalues == 0) {
692 luaU_dump(L, clvalue(o)->l.p, writer, data);
693 status = 1;
694 }
695 else
696 status = 0;
697 lua_unlock(L);
698 return status;
699}
700
701
684/* 702/*
685** Garbage-collection functions 703** Garbage-collection functions
686*/ 704*/
diff --git a/lbaselib.c b/lbaselib.c
index 1d8f8abf..4650b4b3 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbaselib.c,v 1.100 2002/10/22 19:41:08 roberto Exp roberto $ 2** $Id: lbaselib.c,v 1.101 2002/10/25 20:05:28 roberto Exp roberto $
3** Basic library 3** Basic library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -271,6 +271,25 @@ static int luaB_loadstring (lua_State *L) {
271} 271}
272 272
273 273
274static int writer (lua_State *L, const void* b, size_t size, void* B) {
275 (void)L;
276 luaL_addlstring((luaL_Buffer*) B, b, size);
277 return 1;
278}
279
280
281static int luaB_stringdump (lua_State *L) {
282 luaL_Buffer b;
283 luaL_check_type(L, 1, LUA_TFUNCTION);
284 luaL_buffinit(L,&b);
285 if (!lua_dump(L, writer, &b))
286 luaL_error(L, "unable to dump given function");
287 luaL_pushresult(&b);
288 return 1;
289}
290
291
292
274static int luaB_loadfile (lua_State *L) { 293static int luaB_loadfile (lua_State *L) {
275 const char *fname = luaL_opt_string(L, 1, NULL); 294 const char *fname = luaL_opt_string(L, 1, NULL);
276 return passresults(L, luaL_loadfile(L, fname)); 295 return passresults(L, luaL_loadfile(L, fname));
@@ -524,6 +543,7 @@ static const luaL_reg base_funcs[] = {
524 {"collectgarbage", luaB_collectgarbage}, 543 {"collectgarbage", luaB_collectgarbage},
525 {"gcinfo", luaB_gcinfo}, 544 {"gcinfo", luaB_gcinfo},
526 {"loadfile", luaB_loadfile}, 545 {"loadfile", luaB_loadfile},
546 {"stringdump", luaB_stringdump},
527 {"dofile", luaB_dofile}, 547 {"dofile", luaB_dofile},
528 {"loadstring", luaB_loadstring}, 548 {"loadstring", luaB_loadstring},
529 {"require", luaB_require}, 549 {"require", luaB_require},
diff --git a/lua.h b/lua.h
index f95ef4fd..3fef586d 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.159 2002/10/22 17:21:25 roberto Exp roberto $ 2** $Id: lua.h,v 1.160 2002/10/25 20:05:28 roberto Exp roberto $
3** Lua - An Extensible Extension Language 3** Lua - An Extensible Extension Language
4** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil 4** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil
5** http://www.lua.org mailto:info@lua.org 5** http://www.lua.org mailto:info@lua.org
@@ -52,10 +52,13 @@ typedef int (*lua_CFunction) (lua_State *L);
52 52
53 53
54/* 54/*
55** functions that read blocks when loading Lua chunk 55** functions that read/write blocks when loading/dumping Lua chunks
56*/ 56*/
57typedef const char * (*lua_Chunkreader) (lua_State *L, void *ud, size_t *sz); 57typedef const char * (*lua_Chunkreader) (lua_State *L, void *ud, size_t *sz);
58 58
59typedef int (*lua_Chunkwriter) (lua_State *L, const void* p,
60 size_t sz, void* ud);
61
59 62
60/* 63/*
61** basic types 64** basic types
@@ -192,6 +195,8 @@ LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc);
192LUA_API int lua_load (lua_State *L, lua_Chunkreader reader, void *dt, 195LUA_API int lua_load (lua_State *L, lua_Chunkreader reader, void *dt,
193 const char *chunkname); 196 const char *chunkname);
194 197
198LUA_API int lua_dump (lua_State *L, lua_Chunkwriter writer, void *data);
199
195 200
196/* 201/*
197** coroutine functions 202** coroutine functions