lua_gettable and similars return type of gotten value

This commit is contained in:
Roberto Ierusalimschy 2014-03-12 17:57:40 -03:00
parent ad40bb1181
commit a3addae036
7 changed files with 40 additions and 44 deletions

20
lapi.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lapi.c,v 2.199 2014/02/25 14:30:21 roberto Exp roberto $ ** $Id: lapi.c,v 2.200 2014/02/26 12:39:30 roberto Exp roberto $
** Lua API ** Lua API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -628,7 +628,7 @@ LUA_API int lua_pushthread (lua_State *L) {
*/ */
LUA_API void lua_getglobal (lua_State *L, const char *var) { LUA_API int lua_getglobal (lua_State *L, const char *var) {
Table *reg = hvalue(&G(L)->l_registry); Table *reg = hvalue(&G(L)->l_registry);
const TValue *gt; /* global table */ const TValue *gt; /* global table */
lua_lock(L); lua_lock(L);
@ -636,19 +636,21 @@ LUA_API void lua_getglobal (lua_State *L, const char *var) {
setsvalue2s(L, L->top++, luaS_new(L, var)); setsvalue2s(L, L->top++, luaS_new(L, var));
luaV_gettable(L, gt, L->top - 1, L->top - 1); luaV_gettable(L, gt, L->top - 1, L->top - 1);
lua_unlock(L); lua_unlock(L);
return ttnov(L->top - 1);
} }
LUA_API void lua_gettable (lua_State *L, int idx) { LUA_API int lua_gettable (lua_State *L, int idx) {
StkId t; StkId t;
lua_lock(L); lua_lock(L);
t = index2addr(L, idx); t = index2addr(L, idx);
luaV_gettable(L, t, L->top - 1, L->top - 1); luaV_gettable(L, t, L->top - 1, L->top - 1);
lua_unlock(L); lua_unlock(L);
return ttnov(L->top - 1);
} }
LUA_API void lua_getfield (lua_State *L, int idx, const char *k) { LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
StkId t; StkId t;
lua_lock(L); lua_lock(L);
t = index2addr(L, idx); t = index2addr(L, idx);
@ -656,20 +658,22 @@ LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
api_incr_top(L); api_incr_top(L);
luaV_gettable(L, t, L->top - 1, L->top - 1); luaV_gettable(L, t, L->top - 1, L->top - 1);
lua_unlock(L); lua_unlock(L);
return ttnov(L->top - 1);
} }
LUA_API void lua_rawget (lua_State *L, int idx) { LUA_API int lua_rawget (lua_State *L, int idx) {
StkId t; StkId t;
lua_lock(L); lua_lock(L);
t = index2addr(L, idx); t = index2addr(L, idx);
api_check(L, ttistable(t), "table expected"); api_check(L, ttistable(t), "table expected");
setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1)); setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
lua_unlock(L); lua_unlock(L);
return ttnov(L->top - 1);
} }
LUA_API void lua_rawgeti (lua_State *L, int idx, lua_Integer n) { LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
StkId t; StkId t;
lua_lock(L); lua_lock(L);
t = index2addr(L, idx); t = index2addr(L, idx);
@ -677,10 +681,11 @@ LUA_API void lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
setobj2s(L, L->top, luaH_getint(hvalue(t), n)); setobj2s(L, L->top, luaH_getint(hvalue(t), n));
api_incr_top(L); api_incr_top(L);
lua_unlock(L); lua_unlock(L);
return ttnov(L->top - 1);
} }
LUA_API void lua_rawgetp (lua_State *L, int idx, const void *p) { LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
StkId t; StkId t;
TValue k; TValue k;
lua_lock(L); lua_lock(L);
@ -690,6 +695,7 @@ LUA_API void lua_rawgetp (lua_State *L, int idx, const void *p) {
setobj2s(L, L->top, luaH_get(hvalue(t), &k)); setobj2s(L, L->top, luaH_get(hvalue(t), &k));
api_incr_top(L); api_incr_top(L);
lua_unlock(L); lua_unlock(L);
return ttnov(L->top - 1);
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lauxlib.c,v 1.258 2014/02/11 17:39:15 roberto Exp roberto $ ** $Id: lauxlib.c,v 1.259 2014/02/19 13:48:53 roberto Exp roberto $
** Auxiliary functions for building Lua libraries ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -172,8 +172,7 @@ static int typeerror (lua_State *L, int arg, const char *tname) {
const char *msg; const char *msg;
const char *typearg = luaL_typename(L, arg); const char *typearg = luaL_typename(L, arg);
if (lua_getmetatable(L, arg)) { if (lua_getmetatable(L, arg)) {
lua_getfield(L, -1, "__name"); if (lua_getfield(L, -1, "__name") == LUA_TSTRING)
if (lua_isstring(L, -1))
typearg = lua_tostring(L, -1); typearg = lua_tostring(L, -1);
} }
else if (lua_type(L, arg) == LUA_TLIGHTUSERDATA) else if (lua_type(L, arg) == LUA_TLIGHTUSERDATA)
@ -719,8 +718,7 @@ LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
if (!lua_getmetatable(L, obj)) /* no metatable? */ if (!lua_getmetatable(L, obj)) /* no metatable? */
return 0; return 0;
lua_pushstring(L, event); lua_pushstring(L, event);
lua_rawget(L, -2); if (lua_rawget(L, -2) == LUA_TNIL) { /* is metafield nil? */
if (lua_isnil(L, -1)) {
lua_pop(L, 2); /* remove metatable and metafield */ lua_pop(L, 2); /* remove metatable and metafield */
return 0; return 0;
} }
@ -802,8 +800,7 @@ static const char *luaL_findtable (lua_State *L, int idx,
e = strchr(fname, '.'); e = strchr(fname, '.');
if (e == NULL) e = fname + strlen(fname); if (e == NULL) e = fname + strlen(fname);
lua_pushlstring(L, fname, e - fname); lua_pushlstring(L, fname, e - fname);
lua_rawget(L, -2); if (lua_rawget(L, -2) == LUA_TNIL) { /* no such field? */
if (lua_isnil(L, -1)) { /* no such field? */
lua_pop(L, 1); /* remove this nil */ lua_pop(L, 1); /* remove this nil */
lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */ lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
lua_pushlstring(L, fname, e - fname); lua_pushlstring(L, fname, e - fname);
@ -840,8 +837,7 @@ static int libsize (const luaL_Reg *l) {
LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname, LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname,
int sizehint) { int sizehint) {
luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); /* get _LOADED table */ luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); /* get _LOADED table */
lua_getfield(L, -1, modname); /* get _LOADED[modname] */ if (lua_getfield(L, -1, modname) != LUA_TTABLE) { /* no _LOADED[modname]? */
if (!lua_istable(L, -1)) { /* not found? */
lua_pop(L, 1); /* remove previous result */ lua_pop(L, 1); /* remove previous result */
/* try global variable (and create one if it does not exist) */ /* try global variable (and create one if it does not exist) */
lua_pushglobaltable(L); lua_pushglobaltable(L);
@ -893,8 +889,8 @@ LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
** into the stack ** into the stack
*/ */
LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) { LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) {
lua_getfield(L, idx, fname); if (lua_getfield(L, idx, fname) == LUA_TTABLE)
if (lua_istable(L, -1)) return 1; /* table already there */ return 1; /* table already there */
else { else {
lua_pop(L, 1); /* remove previous result */ lua_pop(L, 1); /* remove previous result */
idx = lua_absindex(L, idx); idx = lua_absindex(L, idx);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lbaselib.c,v 1.283 2014/02/13 12:11:34 roberto Exp roberto $ ** $Id: lbaselib.c,v 1.284 2014/02/14 16:45:38 roberto Exp roberto $
** Basic library ** Basic library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -246,8 +246,7 @@ static int ipairsaux (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE); luaL_checktype(L, 1, LUA_TTABLE);
i++; /* next value */ i++; /* next value */
lua_pushinteger(L, i); lua_pushinteger(L, i);
lua_rawgeti(L, 1, i); return (lua_rawgeti(L, 1, i) == LUA_TNIL) ? 1 : 2;
return (lua_isnil(L, -1)) ? 1 : 2;
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldblib.c,v 1.135 2013/07/22 16:05:53 roberto Exp roberto $ ** $Id: ldblib.c,v 1.136 2014/02/19 13:51:09 roberto Exp roberto $
** Interface from Lua to its debug API ** Interface from Lua to its debug API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -270,8 +270,7 @@ static void hookf (lua_State *L, lua_Debug *ar) {
{"call", "return", "line", "count", "tail call"}; {"call", "return", "line", "count", "tail call"};
gethooktable(L); gethooktable(L);
lua_pushthread(L); lua_pushthread(L);
lua_rawget(L, -2); if (lua_rawget(L, -2) == LUA_TFUNCTION) {
if (lua_isfunction(L, -1)) {
lua_pushstring(L, hooknames[(int)ar->event]); lua_pushstring(L, hooknames[(int)ar->event]);
if (ar->currentline >= 0) if (ar->currentline >= 0)
lua_pushinteger(L, ar->currentline); lua_pushinteger(L, ar->currentline);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: loadlib.c,v 1.111 2012/05/30 12:33:44 roberto Exp roberto $ ** $Id: loadlib.c,v 1.112 2013/10/07 14:20:31 roberto Exp roberto $
** Dynamic library loader for Lua ** Dynamic library loader for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
** **
@ -468,8 +468,7 @@ static int searcher_Croot (lua_State *L) {
static int searcher_preload (lua_State *L) { static int searcher_preload (lua_State *L) {
const char *name = luaL_checkstring(L, 1); const char *name = luaL_checkstring(L, 1);
lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD"); lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD");
lua_getfield(L, -1, name); if (lua_getfield(L, -1, name) == LUA_TNIL) /* not found? */
if (lua_isnil(L, -1)) /* not found? */
lua_pushfstring(L, "\n\tno field package.preload['%s']", name); lua_pushfstring(L, "\n\tno field package.preload['%s']", name);
return 1; return 1;
} }
@ -484,8 +483,7 @@ static void findloader (lua_State *L, const char *name) {
luaL_error(L, LUA_QL("package.searchers") " must be a table"); luaL_error(L, LUA_QL("package.searchers") " must be a table");
/* iterate over available searchers to find a loader */ /* iterate over available searchers to find a loader */
for (i = 1; ; i++) { for (i = 1; ; i++) {
lua_rawgeti(L, 3, i); /* get a searcher */ if (lua_rawgeti(L, 3, i) == LUA_TNIL) { /* no more searchers? */
if (lua_isnil(L, -1)) { /* no more searchers? */
lua_pop(L, 1); /* remove nil */ lua_pop(L, 1); /* remove nil */
luaL_pushresult(&msg); /* create error message */ luaL_pushresult(&msg); /* create error message */
luaL_error(L, "module " LUA_QS " not found:%s", luaL_error(L, "module " LUA_QS " not found:%s",
@ -520,8 +518,7 @@ static int ll_require (lua_State *L) {
lua_call(L, 2, 1); /* run loader to load module */ lua_call(L, 2, 1); /* run loader to load module */
if (!lua_isnil(L, -1)) /* non-nil return? */ if (!lua_isnil(L, -1)) /* non-nil return? */
lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ lua_setfield(L, 2, name); /* _LOADED[name] = returned value */
lua_getfield(L, 2, name); if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */
if (lua_isnil(L, -1)) { /* module did not set a value? */
lua_pushboolean(L, 1); /* use true as result */ lua_pushboolean(L, 1); /* use true as result */
lua_pushvalue(L, -1); /* extra copy to be returned */ lua_pushvalue(L, -1); /* extra copy to be returned */
lua_setfield(L, 2, name); /* _LOADED[name] = true */ lua_setfield(L, 2, name); /* _LOADED[name] = true */
@ -587,9 +584,8 @@ static int ll_module (lua_State *L) {
int lastarg = lua_gettop(L); /* last parameter */ int lastarg = lua_gettop(L); /* last parameter */
luaL_pushmodule(L, modname, 1); /* get/create module table */ luaL_pushmodule(L, modname, 1); /* get/create module table */
/* check whether table already has a _NAME field */ /* check whether table already has a _NAME field */
lua_getfield(L, -1, "_NAME"); if (lua_getfield(L, -1, "_NAME") != LUA_TNIL)
if (!lua_isnil(L, -1)) /* is table an initialized module? */ lua_pop(L, 1); /* table is an initialized module */
lua_pop(L, 1);
else { /* no; initialize it */ else { /* no; initialize it */
lua_pop(L, 1); lua_pop(L, 1);
modinit(L, modname); modinit(L, modname);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: loslib.c,v 1.42 2014/02/26 15:27:56 roberto Exp roberto $ ** $Id: loslib.c,v 1.43 2014/02/26 15:55:58 roberto Exp roberto $
** Standard Operating System library ** Standard Operating System library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -158,8 +158,7 @@ static void setboolfield (lua_State *L, const char *key, int value) {
static int getboolfield (lua_State *L, const char *key) { static int getboolfield (lua_State *L, const char *key) {
int res; int res;
lua_getfield(L, -1, key); res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1);
res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
lua_pop(L, 1); lua_pop(L, 1);
return res; return res;
} }

15
lua.h
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lua.h,v 1.299 2014/02/13 12:11:34 roberto Exp roberto $ ** $Id: lua.h,v 1.300 2014/02/25 14:30:21 roberto Exp roberto $
** Lua - A Scripting Language ** Lua - A Scripting Language
** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
** See Copyright Notice at the end of this file ** See Copyright Notice at the end of this file
@ -228,12 +228,13 @@ LUA_API int (lua_pushthread) (lua_State *L);
/* /*
** get functions (Lua -> stack) ** get functions (Lua -> stack)
*/ */
LUA_API void (lua_getglobal) (lua_State *L, const char *var); LUA_API int (lua_getglobal) (lua_State *L, const char *var);
LUA_API void (lua_gettable) (lua_State *L, int idx); LUA_API int (lua_gettable) (lua_State *L, int idx);
LUA_API void (lua_getfield) (lua_State *L, int idx, const char *k); LUA_API int (lua_getfield) (lua_State *L, int idx, const char *k);
LUA_API void (lua_rawget) (lua_State *L, int idx); LUA_API int (lua_rawget) (lua_State *L, int idx);
LUA_API void (lua_rawgeti) (lua_State *L, int idx, lua_Integer n); LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
LUA_API void (lua_rawgetp) (lua_State *L, int idx, const void *p); LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p);
LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec);
LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz);
LUA_API int (lua_getmetatable) (lua_State *L, int objindex); LUA_API int (lua_getmetatable) (lua_State *L, int objindex);