diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-04-11 15:39:37 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-04-11 15:39:37 -0300 |
commit | 6473f965ca699719b3b9908008f15da48cc2e6f1 (patch) | |
tree | be13733e390e62f168995c82f9485a994b2e9464 | |
parent | 0e0e4a480e6d9b0125a96ca982a3e9571578a037 (diff) | |
download | lua-6473f965ca699719b3b9908008f15da48cc2e6f1.tar.gz lua-6473f965ca699719b3b9908008f15da48cc2e6f1.tar.bz2 lua-6473f965ca699719b3b9908008f15da48cc2e6f1.zip |
new API functions to load (parse?) a chunk without running it.
-rw-r--r-- | lapi.c | 28 | ||||
-rw-r--r-- | lbaselib.c | 21 | ||||
-rw-r--r-- | ldo.c | 30 | ||||
-rw-r--r-- | lua.h | 7 |
4 files changed, 55 insertions, 31 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 1.137 2001/03/26 14:31:49 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.138 2001/04/11 14:42:41 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 | */ |
@@ -533,7 +533,6 @@ LUA_API int lua_ref (lua_State *L, int lock) { | |||
533 | 533 | ||
534 | /* | 534 | /* |
535 | ** `do' functions (run Lua code) | 535 | ** `do' functions (run Lua code) |
536 | ** (most of them are in ldo.c) | ||
537 | */ | 536 | */ |
538 | 537 | ||
539 | LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) { | 538 | 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) { | |||
544 | } | 543 | } |
545 | 544 | ||
546 | 545 | ||
546 | LUA_API int lua_dofile (lua_State *L, const l_char *filename) { | ||
547 | int status; | ||
548 | status = lua_loadfile(L, filename); | ||
549 | if (status == 0) /* parse OK? */ | ||
550 | status = lua_call(L, 0, LUA_MULTRET); /* call main */ | ||
551 | return status; | ||
552 | } | ||
553 | |||
554 | |||
555 | LUA_API int lua_dobuffer (lua_State *L, const l_char *buff, size_t size, | ||
556 | const l_char *name) { | ||
557 | int status; | ||
558 | status = lua_loadbuffer(L, buff, size, name); | ||
559 | if (status == 0) /* parse OK? */ | ||
560 | status = lua_call(L, 0, LUA_MULTRET); /* call main */ | ||
561 | return status; | ||
562 | } | ||
563 | |||
564 | |||
565 | LUA_API int lua_dostring (lua_State *L, const l_char *str) { | ||
566 | return lua_dobuffer(L, str, strlen(str), str); | ||
567 | } | ||
568 | |||
569 | |||
570 | |||
547 | /* | 571 | /* |
548 | ** Garbage-collection functions | 572 | ** Garbage-collection functions |
549 | */ | 573 | */ |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.32 2001/04/06 18:25:00 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.33 2001/04/11 14:42:41 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 | */ |
@@ -311,6 +311,7 @@ static int passresults (lua_State *L, int status, int oldtop) { | |||
311 | } | 311 | } |
312 | } | 312 | } |
313 | 313 | ||
314 | |||
314 | static int luaB_dostring (lua_State *L) { | 315 | static int luaB_dostring (lua_State *L) { |
315 | int oldtop = lua_gettop(L); | 316 | int oldtop = lua_gettop(L); |
316 | size_t l; | 317 | size_t l; |
@@ -320,6 +321,14 @@ static int luaB_dostring (lua_State *L) { | |||
320 | } | 321 | } |
321 | 322 | ||
322 | 323 | ||
324 | static int luaB_loadstring (lua_State *L) { | ||
325 | int oldtop = lua_gettop(L); | ||
326 | size_t l; | ||
327 | const l_char *s = luaL_check_lstr(L, 1, &l); | ||
328 | const l_char *chunkname = luaL_opt_string(L, 2, s); | ||
329 | return passresults(L, lua_loadbuffer(L, s, l, chunkname), oldtop); | ||
330 | } | ||
331 | |||
323 | static int luaB_dofile (lua_State *L) { | 332 | static int luaB_dofile (lua_State *L) { |
324 | int oldtop = lua_gettop(L); | 333 | int oldtop = lua_gettop(L); |
325 | const l_char *fname = luaL_opt_string(L, 1, NULL); | 334 | const l_char *fname = luaL_opt_string(L, 1, NULL); |
@@ -327,6 +336,14 @@ static int luaB_dofile (lua_State *L) { | |||
327 | } | 336 | } |
328 | 337 | ||
329 | 338 | ||
339 | static int luaB_loadfile (lua_State *L) { | ||
340 | int oldtop = lua_gettop(L); | ||
341 | const l_char *fname = luaL_opt_string(L, 1, NULL); | ||
342 | return passresults(L, lua_loadfile(L, fname), oldtop); | ||
343 | } | ||
344 | |||
345 | |||
346 | |||
330 | #define LUA_PATH l_s("LUA_PATH") | 347 | #define LUA_PATH l_s("LUA_PATH") |
331 | 348 | ||
332 | #define LUA_PATH_SEP l_s(";") | 349 | #define LUA_PATH_SEP l_s(";") |
@@ -753,6 +770,8 @@ static const luaL_reg base_funcs[] = { | |||
753 | {l_s("getglobal"), luaB_getglobal}, | 770 | {l_s("getglobal"), luaB_getglobal}, |
754 | {l_s("gettagmethod"), luaB_gettagmethod}, | 771 | {l_s("gettagmethod"), luaB_gettagmethod}, |
755 | {l_s("globals"), luaB_globals}, | 772 | {l_s("globals"), luaB_globals}, |
773 | {l_s("loadfile"), luaB_loadfile}, | ||
774 | {l_s("loadstring"), luaB_loadstring}, | ||
756 | {l_s("newtype"), luaB_newtype}, | 775 | {l_s("newtype"), luaB_newtype}, |
757 | {l_s("newtag"), luaB_newtype}, /* for compatibility 4.0 */ | 776 | {l_s("newtag"), luaB_newtype}, /* for compatibility 4.0 */ |
758 | {l_s("next"), luaB_next}, | 777 | {l_s("next"), luaB_next}, |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldo.c,v 1.132 2001/03/26 14:31:49 roberto Exp roberto $ | 2 | ** $Id: ldo.c,v 1.133 2001/04/06 19:26:06 roberto Exp roberto $ |
3 | ** Stack and Call structure of Lua | 3 | ** Stack and Call structure of Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -255,7 +255,7 @@ static int protectedparser (lua_State *L, ZIO *z, int bin) { | |||
255 | } | 255 | } |
256 | 256 | ||
257 | 257 | ||
258 | static int parse_file (lua_State *L, const l_char *filename) { | 258 | LUA_API int lua_loadfile (lua_State *L, const l_char *filename) { |
259 | ZIO z; | 259 | ZIO z; |
260 | int status; | 260 | int status; |
261 | int bin; /* flag for file mode */ | 261 | int bin; /* flag for file mode */ |
@@ -282,17 +282,8 @@ static int parse_file (lua_State *L, const l_char *filename) { | |||
282 | } | 282 | } |
283 | 283 | ||
284 | 284 | ||
285 | LUA_API int lua_dofile (lua_State *L, const l_char *filename) { | 285 | LUA_API int lua_loadbuffer (lua_State *L, const l_char *buff, size_t size, |
286 | int status; | 286 | const l_char *name) { |
287 | status = parse_file(L, filename); | ||
288 | if (status == 0) /* parse OK? */ | ||
289 | status = lua_call(L, 0, LUA_MULTRET); /* call main */ | ||
290 | return status; | ||
291 | } | ||
292 | |||
293 | |||
294 | static int parse_buffer (lua_State *L, const l_char *buff, size_t size, | ||
295 | const l_char *name) { | ||
296 | ZIO z; | 287 | ZIO z; |
297 | int status; | 288 | int status; |
298 | if (!name) name = l_s("?"); | 289 | if (!name) name = l_s("?"); |
@@ -302,19 +293,6 @@ static int parse_buffer (lua_State *L, const l_char *buff, size_t size, | |||
302 | } | 293 | } |
303 | 294 | ||
304 | 295 | ||
305 | LUA_API int lua_dobuffer (lua_State *L, const l_char *buff, size_t size, const l_char *name) { | ||
306 | int status; | ||
307 | status = parse_buffer(L, buff, size, name); | ||
308 | if (status == 0) /* parse OK? */ | ||
309 | status = lua_call(L, 0, LUA_MULTRET); /* call main */ | ||
310 | return status; | ||
311 | } | ||
312 | |||
313 | |||
314 | LUA_API int lua_dostring (lua_State *L, const l_char *str) { | ||
315 | return lua_dobuffer(L, str, strlen(str), str); | ||
316 | } | ||
317 | |||
318 | 296 | ||
319 | /* | 297 | /* |
320 | ** {====================================================== | 298 | ** {====================================================== |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lua.h,v 1.93 2001/04/06 21:17:37 roberto Exp roberto $ | 2 | ** $Id: lua.h,v 1.94 2001/04/11 14:42:41 roberto Exp roberto $ |
3 | ** Lua - An Extensible Extension Language | 3 | ** Lua - An Extensible Extension Language |
4 | ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil | 4 | ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil |
5 | ** e-mail: lua@tecgraf.puc-rio.br | 5 | ** e-mail: lua@tecgraf.puc-rio.br |
@@ -178,12 +178,15 @@ LUA_API int lua_ref (lua_State *L, int lock); | |||
178 | 178 | ||
179 | 179 | ||
180 | /* | 180 | /* |
181 | ** `do' functions (run Lua code) | 181 | ** `load' and `do' functions (load and run Lua code) |
182 | */ | 182 | */ |
183 | LUA_API int lua_call (lua_State *L, int nargs, int nresults); | 183 | LUA_API int lua_call (lua_State *L, int nargs, int nresults); |
184 | LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults); | 184 | LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults); |
185 | LUA_API int lua_loadfile (lua_State *L, const lua_char *filename); | ||
185 | LUA_API int lua_dofile (lua_State *L, const lua_char *filename); | 186 | LUA_API int lua_dofile (lua_State *L, const lua_char *filename); |
186 | LUA_API int lua_dostring (lua_State *L, const lua_char *str); | 187 | LUA_API int lua_dostring (lua_State *L, const lua_char *str); |
188 | LUA_API int lua_loadbuffer (lua_State *L, const lua_char *buff, size_t size, | ||
189 | const lua_char *name); | ||
187 | LUA_API int lua_dobuffer (lua_State *L, const lua_char *buff, size_t size, | 190 | LUA_API int lua_dobuffer (lua_State *L, const lua_char *buff, size_t size, |
188 | const lua_char *name); | 191 | const lua_char *name); |
189 | 192 | ||