aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-03-12 17:57:40 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-03-12 17:57:40 -0300
commita3addae03634794b841b6c8c4dd8ff83542d8896 (patch)
tree66695864f7bd56c295ad7cd6a6465845103cd424
parentad40bb1181d08821af6789147a28aa8370533d11 (diff)
downloadlua-a3addae03634794b841b6c8c4dd8ff83542d8896.tar.gz
lua-a3addae03634794b841b6c8c4dd8ff83542d8896.tar.bz2
lua-a3addae03634794b841b6c8c4dd8ff83542d8896.zip
lua_gettable and similars return type of gotten value
-rw-r--r--lapi.c20
-rw-r--r--lauxlib.c18
-rw-r--r--lbaselib.c5
-rw-r--r--ldblib.c5
-rw-r--r--loadlib.c16
-rw-r--r--loslib.c5
-rw-r--r--lua.h15
7 files changed, 40 insertions, 44 deletions
diff --git a/lapi.c b/lapi.c
index cc521b31..ae0da8c4 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 2.199 2014/02/25 14:30:21 roberto Exp roberto $ 2** $Id: lapi.c,v 2.200 2014/02/26 12:39:30 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*/
@@ -628,7 +628,7 @@ LUA_API int lua_pushthread (lua_State *L) {
628*/ 628*/
629 629
630 630
631LUA_API void lua_getglobal (lua_State *L, const char *var) { 631LUA_API int lua_getglobal (lua_State *L, const char *var) {
632 Table *reg = hvalue(&G(L)->l_registry); 632 Table *reg = hvalue(&G(L)->l_registry);
633 const TValue *gt; /* global table */ 633 const TValue *gt; /* global table */
634 lua_lock(L); 634 lua_lock(L);
@@ -636,19 +636,21 @@ LUA_API void lua_getglobal (lua_State *L, const char *var) {
636 setsvalue2s(L, L->top++, luaS_new(L, var)); 636 setsvalue2s(L, L->top++, luaS_new(L, var));
637 luaV_gettable(L, gt, L->top - 1, L->top - 1); 637 luaV_gettable(L, gt, L->top - 1, L->top - 1);
638 lua_unlock(L); 638 lua_unlock(L);
639 return ttnov(L->top - 1);
639} 640}
640 641
641 642
642LUA_API void lua_gettable (lua_State *L, int idx) { 643LUA_API int lua_gettable (lua_State *L, int idx) {
643 StkId t; 644 StkId t;
644 lua_lock(L); 645 lua_lock(L);
645 t = index2addr(L, idx); 646 t = index2addr(L, idx);
646 luaV_gettable(L, t, L->top - 1, L->top - 1); 647 luaV_gettable(L, t, L->top - 1, L->top - 1);
647 lua_unlock(L); 648 lua_unlock(L);
649 return ttnov(L->top - 1);
648} 650}
649 651
650 652
651LUA_API void lua_getfield (lua_State *L, int idx, const char *k) { 653LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
652 StkId t; 654 StkId t;
653 lua_lock(L); 655 lua_lock(L);
654 t = index2addr(L, idx); 656 t = index2addr(L, idx);
@@ -656,20 +658,22 @@ LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
656 api_incr_top(L); 658 api_incr_top(L);
657 luaV_gettable(L, t, L->top - 1, L->top - 1); 659 luaV_gettable(L, t, L->top - 1, L->top - 1);
658 lua_unlock(L); 660 lua_unlock(L);
661 return ttnov(L->top - 1);
659} 662}
660 663
661 664
662LUA_API void lua_rawget (lua_State *L, int idx) { 665LUA_API int lua_rawget (lua_State *L, int idx) {
663 StkId t; 666 StkId t;
664 lua_lock(L); 667 lua_lock(L);
665 t = index2addr(L, idx); 668 t = index2addr(L, idx);
666 api_check(L, ttistable(t), "table expected"); 669 api_check(L, ttistable(t), "table expected");
667 setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1)); 670 setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
668 lua_unlock(L); 671 lua_unlock(L);
672 return ttnov(L->top - 1);
669} 673}
670 674
671 675
672LUA_API void lua_rawgeti (lua_State *L, int idx, lua_Integer n) { 676LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
673 StkId t; 677 StkId t;
674 lua_lock(L); 678 lua_lock(L);
675 t = index2addr(L, idx); 679 t = index2addr(L, idx);
@@ -677,10 +681,11 @@ LUA_API void lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
677 setobj2s(L, L->top, luaH_getint(hvalue(t), n)); 681 setobj2s(L, L->top, luaH_getint(hvalue(t), n));
678 api_incr_top(L); 682 api_incr_top(L);
679 lua_unlock(L); 683 lua_unlock(L);
684 return ttnov(L->top - 1);
680} 685}
681 686
682 687
683LUA_API void lua_rawgetp (lua_State *L, int idx, const void *p) { 688LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
684 StkId t; 689 StkId t;
685 TValue k; 690 TValue k;
686 lua_lock(L); 691 lua_lock(L);
@@ -690,6 +695,7 @@ LUA_API void lua_rawgetp (lua_State *L, int idx, const void *p) {
690 setobj2s(L, L->top, luaH_get(hvalue(t), &k)); 695 setobj2s(L, L->top, luaH_get(hvalue(t), &k));
691 api_incr_top(L); 696 api_incr_top(L);
692 lua_unlock(L); 697 lua_unlock(L);
698 return ttnov(L->top - 1);
693} 699}
694 700
695 701
diff --git a/lauxlib.c b/lauxlib.c
index 06f02f6f..a0d5aff8 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.258 2014/02/11 17:39:15 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.259 2014/02/19 13:48:53 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -172,8 +172,7 @@ static int typeerror (lua_State *L, int arg, const char *tname) {
172 const char *msg; 172 const char *msg;
173 const char *typearg = luaL_typename(L, arg); 173 const char *typearg = luaL_typename(L, arg);
174 if (lua_getmetatable(L, arg)) { 174 if (lua_getmetatable(L, arg)) {
175 lua_getfield(L, -1, "__name"); 175 if (lua_getfield(L, -1, "__name") == LUA_TSTRING)
176 if (lua_isstring(L, -1))
177 typearg = lua_tostring(L, -1); 176 typearg = lua_tostring(L, -1);
178 } 177 }
179 else if (lua_type(L, arg) == LUA_TLIGHTUSERDATA) 178 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) {
719 if (!lua_getmetatable(L, obj)) /* no metatable? */ 718 if (!lua_getmetatable(L, obj)) /* no metatable? */
720 return 0; 719 return 0;
721 lua_pushstring(L, event); 720 lua_pushstring(L, event);
722 lua_rawget(L, -2); 721 if (lua_rawget(L, -2) == LUA_TNIL) { /* is metafield nil? */
723 if (lua_isnil(L, -1)) {
724 lua_pop(L, 2); /* remove metatable and metafield */ 722 lua_pop(L, 2); /* remove metatable and metafield */
725 return 0; 723 return 0;
726 } 724 }
@@ -802,8 +800,7 @@ static const char *luaL_findtable (lua_State *L, int idx,
802 e = strchr(fname, '.'); 800 e = strchr(fname, '.');
803 if (e == NULL) e = fname + strlen(fname); 801 if (e == NULL) e = fname + strlen(fname);
804 lua_pushlstring(L, fname, e - fname); 802 lua_pushlstring(L, fname, e - fname);
805 lua_rawget(L, -2); 803 if (lua_rawget(L, -2) == LUA_TNIL) { /* no such field? */
806 if (lua_isnil(L, -1)) { /* no such field? */
807 lua_pop(L, 1); /* remove this nil */ 804 lua_pop(L, 1); /* remove this nil */
808 lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */ 805 lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
809 lua_pushlstring(L, fname, e - fname); 806 lua_pushlstring(L, fname, e - fname);
@@ -840,8 +837,7 @@ static int libsize (const luaL_Reg *l) {
840LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname, 837LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname,
841 int sizehint) { 838 int sizehint) {
842 luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); /* get _LOADED table */ 839 luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); /* get _LOADED table */
843 lua_getfield(L, -1, modname); /* get _LOADED[modname] */ 840 if (lua_getfield(L, -1, modname) != LUA_TTABLE) { /* no _LOADED[modname]? */
844 if (!lua_istable(L, -1)) { /* not found? */
845 lua_pop(L, 1); /* remove previous result */ 841 lua_pop(L, 1); /* remove previous result */
846 /* try global variable (and create one if it does not exist) */ 842 /* try global variable (and create one if it does not exist) */
847 lua_pushglobaltable(L); 843 lua_pushglobaltable(L);
@@ -893,8 +889,8 @@ LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
893** into the stack 889** into the stack
894*/ 890*/
895LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) { 891LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) {
896 lua_getfield(L, idx, fname); 892 if (lua_getfield(L, idx, fname) == LUA_TTABLE)
897 if (lua_istable(L, -1)) return 1; /* table already there */ 893 return 1; /* table already there */
898 else { 894 else {
899 lua_pop(L, 1); /* remove previous result */ 895 lua_pop(L, 1); /* remove previous result */
900 idx = lua_absindex(L, idx); 896 idx = lua_absindex(L, idx);
diff --git a/lbaselib.c b/lbaselib.c
index 55bd086f..e0e6a66c 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbaselib.c,v 1.283 2014/02/13 12:11:34 roberto Exp roberto $ 2** $Id: lbaselib.c,v 1.284 2014/02/14 16:45:38 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*/
@@ -246,8 +246,7 @@ static int ipairsaux (lua_State *L) {
246 luaL_checktype(L, 1, LUA_TTABLE); 246 luaL_checktype(L, 1, LUA_TTABLE);
247 i++; /* next value */ 247 i++; /* next value */
248 lua_pushinteger(L, i); 248 lua_pushinteger(L, i);
249 lua_rawgeti(L, 1, i); 249 return (lua_rawgeti(L, 1, i) == LUA_TNIL) ? 1 : 2;
250 return (lua_isnil(L, -1)) ? 1 : 2;
251} 250}
252 251
253 252
diff --git a/ldblib.c b/ldblib.c
index 10328f3a..09e1c252 100644
--- a/ldblib.c
+++ b/ldblib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldblib.c,v 1.135 2013/07/22 16:05:53 roberto Exp roberto $ 2** $Id: ldblib.c,v 1.136 2014/02/19 13:51:09 roberto Exp roberto $
3** Interface from Lua to its debug API 3** Interface from Lua to its debug API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -270,8 +270,7 @@ static void hookf (lua_State *L, lua_Debug *ar) {
270 {"call", "return", "line", "count", "tail call"}; 270 {"call", "return", "line", "count", "tail call"};
271 gethooktable(L); 271 gethooktable(L);
272 lua_pushthread(L); 272 lua_pushthread(L);
273 lua_rawget(L, -2); 273 if (lua_rawget(L, -2) == LUA_TFUNCTION) {
274 if (lua_isfunction(L, -1)) {
275 lua_pushstring(L, hooknames[(int)ar->event]); 274 lua_pushstring(L, hooknames[(int)ar->event]);
276 if (ar->currentline >= 0) 275 if (ar->currentline >= 0)
277 lua_pushinteger(L, ar->currentline); 276 lua_pushinteger(L, ar->currentline);
diff --git a/loadlib.c b/loadlib.c
index bc6d9e02..3695a061 100644
--- a/loadlib.c
+++ b/loadlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: loadlib.c,v 1.111 2012/05/30 12:33:44 roberto Exp roberto $ 2** $Id: loadlib.c,v 1.112 2013/10/07 14:20:31 roberto Exp roberto $
3** Dynamic library loader for Lua 3** Dynamic library loader for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5** 5**
@@ -468,8 +468,7 @@ static int searcher_Croot (lua_State *L) {
468static int searcher_preload (lua_State *L) { 468static int searcher_preload (lua_State *L) {
469 const char *name = luaL_checkstring(L, 1); 469 const char *name = luaL_checkstring(L, 1);
470 lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD"); 470 lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD");
471 lua_getfield(L, -1, name); 471 if (lua_getfield(L, -1, name) == LUA_TNIL) /* not found? */
472 if (lua_isnil(L, -1)) /* not found? */
473 lua_pushfstring(L, "\n\tno field package.preload['%s']", name); 472 lua_pushfstring(L, "\n\tno field package.preload['%s']", name);
474 return 1; 473 return 1;
475} 474}
@@ -484,8 +483,7 @@ static void findloader (lua_State *L, const char *name) {
484 luaL_error(L, LUA_QL("package.searchers") " must be a table"); 483 luaL_error(L, LUA_QL("package.searchers") " must be a table");
485 /* iterate over available searchers to find a loader */ 484 /* iterate over available searchers to find a loader */
486 for (i = 1; ; i++) { 485 for (i = 1; ; i++) {
487 lua_rawgeti(L, 3, i); /* get a searcher */ 486 if (lua_rawgeti(L, 3, i) == LUA_TNIL) { /* no more searchers? */
488 if (lua_isnil(L, -1)) { /* no more searchers? */
489 lua_pop(L, 1); /* remove nil */ 487 lua_pop(L, 1); /* remove nil */
490 luaL_pushresult(&msg); /* create error message */ 488 luaL_pushresult(&msg); /* create error message */
491 luaL_error(L, "module " LUA_QS " not found:%s", 489 luaL_error(L, "module " LUA_QS " not found:%s",
@@ -520,8 +518,7 @@ static int ll_require (lua_State *L) {
520 lua_call(L, 2, 1); /* run loader to load module */ 518 lua_call(L, 2, 1); /* run loader to load module */
521 if (!lua_isnil(L, -1)) /* non-nil return? */ 519 if (!lua_isnil(L, -1)) /* non-nil return? */
522 lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ 520 lua_setfield(L, 2, name); /* _LOADED[name] = returned value */
523 lua_getfield(L, 2, name); 521 if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */
524 if (lua_isnil(L, -1)) { /* module did not set a value? */
525 lua_pushboolean(L, 1); /* use true as result */ 522 lua_pushboolean(L, 1); /* use true as result */
526 lua_pushvalue(L, -1); /* extra copy to be returned */ 523 lua_pushvalue(L, -1); /* extra copy to be returned */
527 lua_setfield(L, 2, name); /* _LOADED[name] = true */ 524 lua_setfield(L, 2, name); /* _LOADED[name] = true */
@@ -587,9 +584,8 @@ static int ll_module (lua_State *L) {
587 int lastarg = lua_gettop(L); /* last parameter */ 584 int lastarg = lua_gettop(L); /* last parameter */
588 luaL_pushmodule(L, modname, 1); /* get/create module table */ 585 luaL_pushmodule(L, modname, 1); /* get/create module table */
589 /* check whether table already has a _NAME field */ 586 /* check whether table already has a _NAME field */
590 lua_getfield(L, -1, "_NAME"); 587 if (lua_getfield(L, -1, "_NAME") != LUA_TNIL)
591 if (!lua_isnil(L, -1)) /* is table an initialized module? */ 588 lua_pop(L, 1); /* table is an initialized module */
592 lua_pop(L, 1);
593 else { /* no; initialize it */ 589 else { /* no; initialize it */
594 lua_pop(L, 1); 590 lua_pop(L, 1);
595 modinit(L, modname); 591 modinit(L, modname);
diff --git a/loslib.c b/loslib.c
index 3e9a1bf8..b301681f 100644
--- a/loslib.c
+++ b/loslib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: loslib.c,v 1.42 2014/02/26 15:27:56 roberto Exp roberto $ 2** $Id: loslib.c,v 1.43 2014/02/26 15:55:58 roberto Exp roberto $
3** Standard Operating System library 3** Standard Operating System library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -158,8 +158,7 @@ static void setboolfield (lua_State *L, const char *key, int value) {
158 158
159static int getboolfield (lua_State *L, const char *key) { 159static int getboolfield (lua_State *L, const char *key) {
160 int res; 160 int res;
161 lua_getfield(L, -1, key); 161 res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1);
162 res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
163 lua_pop(L, 1); 162 lua_pop(L, 1);
164 return res; 163 return res;
165} 164}
diff --git a/lua.h b/lua.h
index 1738b6d8..1d76115f 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.299 2014/02/13 12:11:34 roberto Exp roberto $ 2** $Id: lua.h,v 1.300 2014/02/25 14:30:21 roberto Exp roberto $
3** Lua - A Scripting Language 3** Lua - A Scripting Language
4** Lua.org, PUC-Rio, Brazil (http://www.lua.org) 4** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
5** See Copyright Notice at the end of this file 5** See Copyright Notice at the end of this file
@@ -228,12 +228,13 @@ LUA_API int (lua_pushthread) (lua_State *L);
228/* 228/*
229** get functions (Lua -> stack) 229** get functions (Lua -> stack)
230*/ 230*/
231LUA_API void (lua_getglobal) (lua_State *L, const char *var); 231LUA_API int (lua_getglobal) (lua_State *L, const char *var);
232LUA_API void (lua_gettable) (lua_State *L, int idx); 232LUA_API int (lua_gettable) (lua_State *L, int idx);
233LUA_API void (lua_getfield) (lua_State *L, int idx, const char *k); 233LUA_API int (lua_getfield) (lua_State *L, int idx, const char *k);
234LUA_API void (lua_rawget) (lua_State *L, int idx); 234LUA_API int (lua_rawget) (lua_State *L, int idx);
235LUA_API void (lua_rawgeti) (lua_State *L, int idx, lua_Integer n); 235LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
236LUA_API void (lua_rawgetp) (lua_State *L, int idx, const void *p); 236LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p);
237
237LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); 238LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec);
238LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); 239LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz);
239LUA_API int (lua_getmetatable) (lua_State *L, int objindex); 240LUA_API int (lua_getmetatable) (lua_State *L, int objindex);