ZIO passes Lua state to chunk reader

This commit is contained in:
Roberto Ierusalimschy 2003-08-25 17:00:50 -03:00
parent 9fcc485176
commit 4b2e71ddb6
3 changed files with 9 additions and 7 deletions

4
lapi.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lapi.c,v 1.241 2003/08/15 13:48:53 roberto Exp roberto $ ** $Id: lapi.c,v 1.242 2003/08/25 19:51:54 roberto Exp roberto $
** Lua API ** Lua API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -718,7 +718,7 @@ LUA_API int lua_load (lua_State *L, lua_Chunkreader reader, void *data,
int c; int c;
lua_lock(L); lua_lock(L);
if (!chunkname) chunkname = "?"; if (!chunkname) chunkname = "?";
luaZ_init(&z, reader, data); luaZ_init(L, &z, reader, data);
c = luaZ_lookahead(&z); c = luaZ_lookahead(&z);
status = luaD_protectedparser(L, &z, (c == LUA_SIGNATURE[0]), chunkname); status = luaD_protectedparser(L, &z, (c == LUA_SIGNATURE[0]), chunkname);
lua_unlock(L); lua_unlock(L);

7
lzio.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lzio.c,v 1.24 2003/03/20 16:00:56 roberto Exp roberto $ ** $Id: lzio.c,v 1.25 2003/08/25 19:51:54 roberto Exp roberto $
** a generic input stream interface ** a generic input stream interface
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -18,7 +18,7 @@
int luaZ_fill (ZIO *z) { int luaZ_fill (ZIO *z) {
size_t size; size_t size;
const char *buff = z->reader(NULL, z->data, &size); const char *buff = z->reader(z->L, z->data, &size);
if (buff == NULL || size == 0) return EOZ; if (buff == NULL || size == 0) return EOZ;
z->n = size - 1; z->n = size - 1;
z->p = buff; z->p = buff;
@ -37,7 +37,8 @@ int luaZ_lookahead (ZIO *z) {
} }
void luaZ_init (ZIO *z, lua_Chunkreader reader, void *data) { void luaZ_init (lua_State *L, ZIO *z, lua_Chunkreader reader, void *data) {
z->L = L;
z->reader = reader; z->reader = reader;
z->data = data; z->data = data;
z->n = 0; z->n = 0;

5
lzio.h
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lzio.h,v 1.15 2003/03/20 16:00:56 roberto Exp roberto $ ** $Id: lzio.h,v 1.16 2003/08/25 19:51:54 roberto Exp roberto $
** Buffered streams ** Buffered streams
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -20,7 +20,7 @@ typedef struct Zio ZIO;
#define zgetc(z) (((z)->n--)>0 ? char2int(*(z)->p++) : luaZ_fill(z)) #define zgetc(z) (((z)->n--)>0 ? char2int(*(z)->p++) : luaZ_fill(z))
void luaZ_init (ZIO *z, lua_Chunkreader reader, void *data); void luaZ_init (lua_State *L, ZIO *z, lua_Chunkreader reader, void *data);
size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */
int luaZ_lookahead (ZIO *z); int luaZ_lookahead (ZIO *z);
@ -53,6 +53,7 @@ struct Zio {
const char *p; /* current position in buffer */ const char *p; /* current position in buffer */
lua_Chunkreader reader; lua_Chunkreader reader;
void* data; /* additional data */ void* data; /* additional data */
lua_State *L; /* Lua state (for reader) */
}; };