Browse Source

new function `lua_replace'

Roberto Ierusalimschy 23 years ago
parent
commit
0a87d9d334
2 changed files with 14 additions and 16 deletions
  1. 11 14
      lapi.c
  2. 3 2
      lua.h

+ 11 - 14
lapi.c

@@ -137,6 +137,15 @@ LUA_API void lua_insert (lua_State *L, int index) {
 }
 }
 
 
 
 
+LUA_API void lua_replace (lua_State *L, int index) {
+  lua_lock(L);
+  api_checknelems(L, 1);
+  setobj(luaA_index(L, index), L->top - 1);
+  L->top--;
+  lua_unlock(L);
+}
+
+
 LUA_API void lua_pushvalue (lua_State *L, int index) {
 LUA_API void lua_pushvalue (lua_State *L, int index) {
   lua_lock(L);
   lua_lock(L);
   setobj(L->top, luaA_index(L, index));
   setobj(L->top, luaA_index(L, index));
@@ -172,7 +181,7 @@ LUA_API int lua_iscfunction (lua_State *L, int index) {
 LUA_API int lua_isnumber (lua_State *L, int index) {
 LUA_API int lua_isnumber (lua_State *L, int index) {
   TObject n;
   TObject n;
   TObject *o = luaA_indexAcceptable(L, index);
   TObject *o = luaA_indexAcceptable(L, index);
-  return (o != NULL && (ttype(o) == LUA_TNUMBER || luaV_tonumber(o, &n)));
+  return (o != NULL && luaV_tonumber(o, &n));
 }
 }
 
 
 
 
@@ -213,8 +222,7 @@ LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
 LUA_API lua_Number lua_tonumber (lua_State *L, int index) {
 LUA_API lua_Number lua_tonumber (lua_State *L, int index) {
   TObject n;
   TObject n;
   const TObject *o = luaA_indexAcceptable(L, index);
   const TObject *o = luaA_indexAcceptable(L, index);
-  if (o != NULL &&
-      (ttype(o) == LUA_TNUMBER || (o = luaV_tonumber(o, &n)) != NULL))
+  if (o != NULL && (o = luaV_tonumber(o, &n)) != NULL)
     return nvalue(o);
     return nvalue(o);
   else
   else
     return 0;
     return 0;
@@ -477,17 +485,6 @@ LUA_API void lua_rawseti (lua_State *L, int index, int n) {
 }
 }
 
 
 
 
-LUA_API void lua_setglobals (lua_State *L) {
-  StkId newtable;
-  lua_lock(L);
-  api_checknelems(L, 1);
-  newtable = --L->top;
-  api_check(L, ttype(newtable) == LUA_TTABLE);
-  setobj(gt(L), newtable);
-  lua_unlock(L);
-}
-
-
 LUA_API void lua_setmetatable (lua_State *L, int objindex) {
 LUA_API void lua_setmetatable (lua_State *L, int objindex) {
   StkId obj, mt;
   StkId obj, mt;
   lua_lock(L);
   lua_lock(L);

+ 3 - 2
lua.h

@@ -108,6 +108,7 @@ LUA_API void  lua_settop (lua_State *L, int index);
 LUA_API void  lua_pushvalue (lua_State *L, int index);
 LUA_API void  lua_pushvalue (lua_State *L, int index);
 LUA_API void  lua_remove (lua_State *L, int index);
 LUA_API void  lua_remove (lua_State *L, int index);
 LUA_API void  lua_insert (lua_State *L, int index);
 LUA_API void  lua_insert (lua_State *L, int index);
+LUA_API void  lua_replace (lua_State *L, int index);
 LUA_API int   lua_stackspace (lua_State *L);
 LUA_API int   lua_stackspace (lua_State *L);
 
 
 
 
@@ -163,7 +164,6 @@ LUA_API void  lua_setstr (lua_State *L, int index, const char *name);
 LUA_API void  lua_settable (lua_State *L, int index);
 LUA_API void  lua_settable (lua_State *L, int index);
 LUA_API void  lua_rawset (lua_State *L, int index);
 LUA_API void  lua_rawset (lua_State *L, int index);
 LUA_API void  lua_rawseti (lua_State *L, int index, int n);
 LUA_API void  lua_rawseti (lua_State *L, int index, int n);
-LUA_API void  lua_setglobals (lua_State *L);
 LUA_API void  lua_setmetatable (lua_State *L, int objindex);
 LUA_API void  lua_setmetatable (lua_State *L, int objindex);
 
 
 
 
@@ -186,7 +186,7 @@ LUA_API int   lua_dobuffer (lua_State *L, const char *buff, size_t size,
 */
 */
 LUA_API void lua_cobegin (lua_State *L, int nargs);
 LUA_API void lua_cobegin (lua_State *L, int nargs);
 LUA_API int  lua_yield (lua_State *L, int nresults);
 LUA_API int  lua_yield (lua_State *L, int nresults);
-LUA_API void lua_resume (lua_State *L, lua_State *co);
+LUA_API int  lua_resume (lua_State *L, lua_State *co);
 
 
 /*
 /*
 ** Garbage-collection functions
 ** Garbage-collection functions
@@ -234,6 +234,7 @@ LUA_API void  lua_newuserdatabox (lua_State *L, void *u);
 
 
 #define lua_getregistry(L)	lua_pushvalue(L, LUA_REGISTRYINDEX)
 #define lua_getregistry(L)	lua_pushvalue(L, LUA_REGISTRYINDEX)
 #define lua_getglobals(L)	lua_pushvalue(L, LUA_GLOBALSINDEX)
 #define lua_getglobals(L)	lua_pushvalue(L, LUA_GLOBALSINDEX)
+#define lua_setglobals(L)	lua_replace(L, LUA_GLOBALSINDEX)
 #define lua_getglobal(L,s)	lua_getstr(L, LUA_GLOBALSINDEX, s)
 #define lua_getglobal(L,s)	lua_getstr(L, LUA_GLOBALSINDEX, s)
 #define lua_setglobal(L,s)	lua_setstr(L, LUA_GLOBALSINDEX, s)
 #define lua_setglobal(L,s)	lua_setstr(L, LUA_GLOBALSINDEX, s)