From 6473f965ca699719b3b9908008f15da48cc2e6f1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 11 Apr 2001 15:39:37 -0300 Subject: [PATCH] new API functions to load (parse?) a chunk without running it. --- lapi.c | 28 ++++++++++++++++++++++++++-- lbaselib.c | 21 ++++++++++++++++++++- ldo.c | 30 ++++-------------------------- lua.h | 7 +++++-- 4 files changed, 55 insertions(+), 31 deletions(-) diff --git a/lapi.c b/lapi.c index f146bf8a..243087fe 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 1.137 2001/03/26 14:31:49 roberto Exp roberto $ +** $Id: lapi.c,v 1.138 2001/04/11 14:42:41 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -533,7 +533,6 @@ LUA_API int lua_ref (lua_State *L, int lock) { /* ** `do' functions (run Lua code) -** (most of them are in ldo.c) */ LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) { @@ -544,6 +543,31 @@ LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) { } +LUA_API int lua_dofile (lua_State *L, const l_char *filename) { + int status; + status = lua_loadfile(L, filename); + if (status == 0) /* parse OK? */ + status = lua_call(L, 0, LUA_MULTRET); /* call main */ + return status; +} + + +LUA_API int lua_dobuffer (lua_State *L, const l_char *buff, size_t size, + const l_char *name) { + int status; + status = lua_loadbuffer(L, buff, size, name); + if (status == 0) /* parse OK? */ + status = lua_call(L, 0, LUA_MULTRET); /* call main */ + return status; +} + + +LUA_API int lua_dostring (lua_State *L, const l_char *str) { + return lua_dobuffer(L, str, strlen(str), str); +} + + + /* ** Garbage-collection functions */ diff --git a/lbaselib.c b/lbaselib.c index 4a979814..119d80f7 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.32 2001/04/06 18:25:00 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.33 2001/04/11 14:42:41 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -311,6 +311,7 @@ static int passresults (lua_State *L, int status, int oldtop) { } } + static int luaB_dostring (lua_State *L) { int oldtop = lua_gettop(L); size_t l; @@ -320,6 +321,14 @@ static int luaB_dostring (lua_State *L) { } +static int luaB_loadstring (lua_State *L) { + int oldtop = lua_gettop(L); + size_t l; + const l_char *s = luaL_check_lstr(L, 1, &l); + const l_char *chunkname = luaL_opt_string(L, 2, s); + return passresults(L, lua_loadbuffer(L, s, l, chunkname), oldtop); +} + static int luaB_dofile (lua_State *L) { int oldtop = lua_gettop(L); const l_char *fname = luaL_opt_string(L, 1, NULL); @@ -327,6 +336,14 @@ static int luaB_dofile (lua_State *L) { } +static int luaB_loadfile (lua_State *L) { + int oldtop = lua_gettop(L); + const l_char *fname = luaL_opt_string(L, 1, NULL); + return passresults(L, lua_loadfile(L, fname), oldtop); +} + + + #define LUA_PATH l_s("LUA_PATH") #define LUA_PATH_SEP l_s(";") @@ -753,6 +770,8 @@ static const luaL_reg base_funcs[] = { {l_s("getglobal"), luaB_getglobal}, {l_s("gettagmethod"), luaB_gettagmethod}, {l_s("globals"), luaB_globals}, + {l_s("loadfile"), luaB_loadfile}, + {l_s("loadstring"), luaB_loadstring}, {l_s("newtype"), luaB_newtype}, {l_s("newtag"), luaB_newtype}, /* for compatibility 4.0 */ {l_s("next"), luaB_next}, diff --git a/ldo.c b/ldo.c index e676b68d..b2043b2b 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 1.132 2001/03/26 14:31:49 roberto Exp roberto $ +** $Id: ldo.c,v 1.133 2001/04/06 19:26:06 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -255,7 +255,7 @@ static int protectedparser (lua_State *L, ZIO *z, int bin) { } -static int parse_file (lua_State *L, const l_char *filename) { +LUA_API int lua_loadfile (lua_State *L, const l_char *filename) { ZIO z; int status; int bin; /* flag for file mode */ @@ -282,17 +282,8 @@ static int parse_file (lua_State *L, const l_char *filename) { } -LUA_API int lua_dofile (lua_State *L, const l_char *filename) { - int status; - status = parse_file(L, filename); - if (status == 0) /* parse OK? */ - status = lua_call(L, 0, LUA_MULTRET); /* call main */ - return status; -} - - -static int parse_buffer (lua_State *L, const l_char *buff, size_t size, - const l_char *name) { +LUA_API int lua_loadbuffer (lua_State *L, const l_char *buff, size_t size, + const l_char *name) { ZIO z; int status; if (!name) name = l_s("?"); @@ -302,19 +293,6 @@ static int parse_buffer (lua_State *L, const l_char *buff, size_t size, } -LUA_API int lua_dobuffer (lua_State *L, const l_char *buff, size_t size, const l_char *name) { - int status; - status = parse_buffer(L, buff, size, name); - if (status == 0) /* parse OK? */ - status = lua_call(L, 0, LUA_MULTRET); /* call main */ - return status; -} - - -LUA_API int lua_dostring (lua_State *L, const l_char *str) { - return lua_dobuffer(L, str, strlen(str), str); -} - /* ** {====================================================== diff --git a/lua.h b/lua.h index 6030246f..313a32be 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.93 2001/04/06 21:17:37 roberto Exp roberto $ +** $Id: lua.h,v 1.94 2001/04/11 14:42:41 roberto Exp roberto $ ** Lua - An Extensible Extension Language ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil ** e-mail: lua@tecgraf.puc-rio.br @@ -178,12 +178,15 @@ LUA_API int lua_ref (lua_State *L, int lock); /* -** `do' functions (run Lua code) +** `load' and `do' functions (load and run Lua code) */ LUA_API int lua_call (lua_State *L, int nargs, int nresults); LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults); +LUA_API int lua_loadfile (lua_State *L, const lua_char *filename); LUA_API int lua_dofile (lua_State *L, const lua_char *filename); LUA_API int lua_dostring (lua_State *L, const lua_char *str); +LUA_API int lua_loadbuffer (lua_State *L, const lua_char *buff, size_t size, + const lua_char *name); LUA_API int lua_dobuffer (lua_State *L, const lua_char *buff, size_t size, const lua_char *name);