From 620d22f2a09ccfe5b7a09ebc11a7b5dca28ac9e4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 14 Sep 2000 11:09:31 -0300 Subject: [PATCH] new API function lua_rawget --- lapi.c | 11 ++++++++++- lbaselib.c | 17 +++++++---------- liolib.c | 4 ++-- lstrlib.c | 7 ++----- ltests.c | 19 +++++++++++++++++-- lua.h | 3 ++- 6 files changed, 40 insertions(+), 21 deletions(-) diff --git a/lapi.c b/lapi.c index d3a14967..bde6eca8 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 1.96 2000/09/11 20:29:27 roberto Exp roberto $ +** $Id: lapi.c,v 1.97 2000/09/12 13:47:46 roberto Exp $ ** Lua API ** See Copyright Notice in lua.h */ @@ -366,6 +366,15 @@ int lua_ref (lua_State *L, int lock) { } +/* +** "do" functions (run Lua code) +** (most of them are in ldo.c) +*/ + +void lua_rawcall (lua_State *L, int nargs, int nresults) { + luaD_call(L, L->top-(nargs+1), nresults); +} + /* ** miscellaneous functions diff --git a/lbaselib.c b/lbaselib.c index e876f1e6..56f927e5 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.3 2000/09/12 18:41:43 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.4 2000/09/13 19:52:39 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -51,7 +51,7 @@ static int luaB__ERRORMESSAGE (lua_State *L) { } lua_pushstring(L, "\n"); lua_concat(L, 3); - lua_call(L, 1, 0); + lua_rawcall(L, 1, 0); } return 0; } @@ -71,8 +71,7 @@ static int luaB_print (lua_State *L) { const char *s; lua_pushvalue(L, -1); /* function to be called */ lua_pushvalue(L, i); /* value to print */ - if (lua_call(L, 1, 1) != 0) - lua_error(L, NULL); + lua_rawcall(L, 1, 1); s = lua_tostring(L, -1); /* get result */ if (s == NULL) lua_error(L, "`tostring' must return a string to `print'"); @@ -335,8 +334,7 @@ static int luaB_foreachi (lua_State *L) { lua_pushvalue(L, 2); /* function */ lua_pushnumber(L, i); /* 1st argument */ lua_rawgeti(L, 1, i); /* 2nd argument */ - if (lua_call(L, 2, 1) != 0) - lua_error(L, NULL); + lua_rawcall(L, 2, 1); if (!lua_isnil(L, -1)) return 1; lua_pop(L, 1); /* remove nil result */ @@ -355,7 +353,7 @@ static int luaB_foreach (lua_State *L) { lua_pushvalue(L, 2); /* function */ lua_pushvalue(L, -3); /* key */ lua_pushvalue(L, -3); /* value */ - if (lua_call(L, 2, 1) != 0) lua_error(L, NULL); + lua_rawcall(L, 2, 1); if (!lua_isnil(L, -1)) return 1; lua_pop(L, 2); /* remove value and result */ @@ -450,7 +448,7 @@ static int sort_comp (lua_State *L, int n, int r) { lua_pushvalue(L, -2); /* pivot */ lua_rawgeti(L, 1, n); /* a[n] */ } - if (lua_call(L, 2, 1) != 0) lua_error(L, NULL); + lua_rawcall(L, 2, 1); res = !lua_isnil(L, -1); } else { /* a < b? */ @@ -553,8 +551,7 @@ static int deprecated_func (lua_State *L) { lua_insert(L, 1); /* upvalue is the function to be called */ lua_getglobals(L); lua_insert(L, 2); /* table of globals is 1o argument */ - if (lua_call(L, lua_gettop(L)-1, LUA_MULTRET) != 0) - lua_error(L, NULL); + lua_rawcall(L, lua_gettop(L)-1, LUA_MULTRET); return lua_gettop(L); /* return all results */ } diff --git a/liolib.c b/liolib.c index 4dc8ef80..6ba4f527 100644 --- a/liolib.c +++ b/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 1.82 2000/09/12 18:41:55 roberto Exp roberto $ +** $Id: liolib.c,v 1.83 2000/09/13 20:12:14 roberto Exp roberto $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -654,7 +654,7 @@ static int errorfb (lua_State *L) { lua_getglobal(L, LUA_ALERT); if (lua_isfunction(L, -1)) { /* avoid loop if _ALERT is not defined */ lua_pushvalue(L, -2); /* error message */ - lua_call(L, 1, 0); + lua_rawcall(L, 1, 0); } return 0; } diff --git a/lstrlib.c b/lstrlib.c index 89114666..52caae20 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.51 2000/09/05 19:33:32 roberto Exp $ +** $Id: lstrlib.c,v 1.52 2000/09/11 17:38:42 roberto Exp roberto $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -457,13 +457,10 @@ static void add_s (lua_State *L, luaL_Buffer *b, struct Capture *cap) { } } else { /* is a function */ - int status; int n; lua_pushvalue(L, 3); n = push_captures(L, cap); - status = lua_call(L, n, 1); - if (status != 0) - lua_error(L, NULL); /* propagate error */ + lua_rawcall(L, n, 1); if (lua_isstring(L, -1)) luaL_addvalue(b); /* add return to accumulated result */ else diff --git a/ltests.c b/ltests.c index 60079ce3..04cd1b40 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 1.40 2000/09/05 19:33:32 roberto Exp roberto $ +** $Id: ltests.c,v 1.41 2000/09/11 19:42:57 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -25,6 +25,7 @@ #include "lstring.h" #include "ltable.h" #include "luadebug.h" +#include "lualib.h" void luaB_opentests (lua_State *L); @@ -275,6 +276,19 @@ static int newstate (lua_State *L) { return 1; } +static int loadlib (lua_State *L) { + lua_State *L1 = (lua_State *)lua_touserdata(L, 1); + switch (*luaL_check_string(L, 2)) { + case 'm': lua_mathlibopen(L1); break; + case 's': lua_strlibopen(L1); break; + case 'i': lua_iolibopen(L1); break; + case 'd': lua_dblibopen(L1); break; + case 'b': lua_baselibopen(L1); break; + default: luaL_argerror(L, 2, "invalid option"); + } + return 0; +} + static int closestate (lua_State *L) { luaL_checktype(L, 1, "userdata"); lua_close((lua_State *)lua_touserdata(L, 1)); @@ -405,7 +419,7 @@ static int testC (lua_State *L) { else if EQ("call") { int narg = getnum; int nres = getnum; - if (lua_call(L, narg, nres)) lua_error(L, NULL); + lua_rawcall(L, narg, nres); } else if EQ("type") { lua_pushstring(L, lua_type(L, getnum)); @@ -425,6 +439,7 @@ static const struct luaL_reg tests_funcs[] = { {"listcode", listcode}, {"liststrings", liststrings}, {"listlocals", listlocals}, + {"loadlib", loadlib}, {"querystr", string_query}, {"querytab", table_query}, {"testC", testC}, diff --git a/lua.h b/lua.h index 8e6a89bf..9720e2fd 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.67 2000/09/11 19:42:57 roberto Exp roberto $ +** $Id: lua.h,v 1.68 2000/09/12 13:46:59 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 @@ -128,6 +128,7 @@ int lua_ref (lua_State *L, int lock); ** "do" functions (run Lua code) */ int lua_call (lua_State *L, int nargs, int nresults); +void lua_rawcall (lua_State *L, int nargs, int nresults); int lua_dofile (lua_State *L, const char *filename); int lua_dostring (lua_State *L, const char *str); int lua_dobuffer (lua_State *L, const char *buff, size_t size,