`lua_Chunkwriter' returns 0 in case of success

This commit is contained in:
Roberto Ierusalimschy
2004-07-09 15:24:41 -03:00
parent 7a796a0682
commit 4206d7ed60
2 changed files with 20 additions and 15 deletions

29
ldump.c
View File

@@ -1,6 +1,6 @@
/* /*
** $Id: ldump.c,v 2.1 2003/12/10 12:13:36 roberto Exp roberto $ ** $Id: ldump.c,v 1.7 2004/06/09 21:03:53 lhf Exp lhf $
** save bytecodes ** save pre-compiled Lua chunks
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -21,16 +21,20 @@
typedef struct { typedef struct {
lua_State* L; lua_State* L;
lua_Chunkwriter write; lua_Chunkwriter writer;
void* data; void* data;
int strip; int strip;
int status;
} DumpState; } DumpState;
static void DumpBlock(const void* b, size_t size, DumpState* D) static void DumpBlock(const void* b, size_t size, DumpState* D)
{ {
lua_unlock(D->L); if (D->status==0)
(*D->write)(D->L,b,size,D->data); {
lua_lock(D->L); lua_unlock(D->L);
D->status=(*D->writer)(D->L,b,size,D->data);
lua_lock(D->L);
}
} }
static void DumpByte(int y, DumpState* D) static void DumpByte(int y, DumpState* D)
@@ -54,7 +58,7 @@ static void DumpNumber(lua_Number x, DumpState* D)
DumpBlock(&x,sizeof(x),D); DumpBlock(&x,sizeof(x),D);
} }
static void DumpString(TString* s, DumpState* D) static void DumpString(const TString* s, DumpState* D)
{ {
if (s==NULL || getstr(s)==NULL) if (s==NULL || getstr(s)==NULL)
DumpSize(0,D); DumpSize(0,D);
@@ -154,16 +158,17 @@ static void DumpHeader(DumpState* D)
} }
/* /*
** dump function as precompiled chunk ** dump Lua function as precompiled chunk
*/ */
int luaU_dump (lua_State* L, const Proto* Main, lua_Chunkwriter w, void* data, int strip) int luaU_dump (lua_State* L, const Proto* f, lua_Chunkwriter w, void* data, int strip)
{ {
DumpState D; DumpState D;
D.L=L; D.L=L;
D.write=w; D.writer=w;
D.data=data; D.data=data;
D.strip=strip; D.strip=strip;
D.status=0;
DumpHeader(&D); DumpHeader(&D);
DumpFunction(Main,NULL,&D); DumpFunction(f,NULL,&D);
return 1; return D.status;
} }

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lstrlib.c,v 1.102 2004/04/30 20:13:38 roberto Exp roberto $ ** $Id: lstrlib.c,v 1.103 2004/06/08 14:31:15 roberto Exp roberto $
** Standard library for string operations and pattern-matching ** Standard library for string operations and pattern-matching
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -133,7 +133,7 @@ static int str_char (lua_State *L) {
static int writer (lua_State *L, const void* b, size_t size, void* B) { static int writer (lua_State *L, const void* b, size_t size, void* B) {
(void)L; (void)L;
luaL_addlstring((luaL_Buffer*) B, (const char *)b, size); luaL_addlstring((luaL_Buffer*) B, (const char *)b, size);
return 1; return 0;
} }
@@ -142,7 +142,7 @@ static int str_dump (lua_State *L) {
luaL_checktype(L, 1, LUA_TFUNCTION); luaL_checktype(L, 1, LUA_TFUNCTION);
lua_settop(L, 1); lua_settop(L, 1);
luaL_buffinit(L,&b); luaL_buffinit(L,&b);
if (!lua_dump(L, writer, &b)) if (lua_dump(L, writer, &b) != 0)
luaL_error(L, "unable to dump given function"); luaL_error(L, "unable to dump given function");
luaL_pushresult(&b); luaL_pushresult(&b);
return 1; return 1;