mirror of
https://github.com/lua/lua
synced 2025-02-23 00:24:04 +03:00
new functions lua_getfield' and
lua_setfield'
This commit is contained in:
parent
a41d60e1d1
commit
533737f26e
29
lapi.c
29
lapi.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lapi.c,v 1.244 2003/08/27 21:01:44 roberto Exp roberto $
|
||||
** $Id: lapi.c,v 1.245 2003/10/07 20:13:41 roberto Exp roberto $
|
||||
** Lua API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -498,6 +498,19 @@ LUA_API void lua_gettable (lua_State *L, int idx) {
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
|
||||
StkId t;
|
||||
TObject key;
|
||||
lua_lock(L);
|
||||
t = luaA_index(L, idx);
|
||||
api_checkvalidindex(L, t);
|
||||
setsvalue(&key, luaS_new(L, k));
|
||||
luaV_gettable(L, t, &key, L->top);
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_rawget (lua_State *L, int idx) {
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
@ -582,6 +595,20 @@ LUA_API void lua_settable (lua_State *L, int idx) {
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
|
||||
StkId t;
|
||||
TObject key;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
t = luaA_index(L, idx);
|
||||
api_checkvalidindex(L, t);
|
||||
setsvalue(&key, luaS_new(L, k));
|
||||
luaV_settable(L, t, &key, L->top - 1);
|
||||
L->top--; /* pop value */
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_rawset (lua_State *L, int idx) {
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
|
43
lauxlib.c
43
lauxlib.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lauxlib.c,v 1.104 2003/10/02 20:31:17 roberto Exp roberto $
|
||||
** $Id: lauxlib.c,v 1.105 2003/10/07 20:13:41 roberto Exp roberto $
|
||||
** Auxiliary functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -108,15 +108,13 @@ LUALIB_API int luaL_findstring (const char *name, const char *const list[]) {
|
||||
|
||||
|
||||
LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
|
||||
lua_pushstring(L, tname);
|
||||
lua_rawget(L, LUA_REGISTRYINDEX); /* get registry.name */
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get registry.name */
|
||||
if (!lua_isnil(L, -1)) /* name already in use? */
|
||||
return 0; /* leave previous value on top, but return 0 */
|
||||
lua_pop(L, 1);
|
||||
lua_newtable(L); /* create metatable */
|
||||
lua_pushstring(L, tname);
|
||||
lua_pushvalue(L, -2);
|
||||
lua_rawset(L, LUA_REGISTRYINDEX); /* registry.name = metatable */
|
||||
lua_pushvalue(L, -1);
|
||||
lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */
|
||||
lua_pushvalue(L, -1);
|
||||
lua_pushstring(L, tname);
|
||||
lua_rawset(L, LUA_REGISTRYINDEX); /* registry[metatable] = name */
|
||||
@ -125,8 +123,7 @@ LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
|
||||
|
||||
|
||||
LUALIB_API void luaL_getmetatable (lua_State *L, const char *tname) {
|
||||
lua_pushstring(L, tname);
|
||||
lua_rawget(L, LUA_REGISTRYINDEX);
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, tname);
|
||||
}
|
||||
|
||||
|
||||
@ -215,8 +212,7 @@ LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg,
|
||||
LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
|
||||
if (!lua_getmetatable(L, obj)) /* no metatable? */
|
||||
return 0;
|
||||
lua_pushstring(L, event);
|
||||
lua_rawget(L, -2);
|
||||
lua_getfield(L, -1, event);
|
||||
if (lua_isnil(L, -1)) {
|
||||
lua_pop(L, 2); /* remove metatable and metafield */
|
||||
return 0;
|
||||
@ -241,24 +237,23 @@ LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
|
||||
LUALIB_API void luaL_openlib (lua_State *L, const char *libname,
|
||||
const luaL_reg *l, int nup) {
|
||||
if (libname) {
|
||||
lua_pushstring(L, libname);
|
||||
lua_gettable(L, LUA_GLOBALSINDEX); /* check whether lib already exists */
|
||||
/* check whether lib already exists */
|
||||
lua_getfield(L, LUA_GLOBALSINDEX, libname);
|
||||
if (lua_isnil(L, -1)) { /* no? */
|
||||
lua_pop(L, 1);
|
||||
lua_newtable(L); /* create it */
|
||||
lua_pushstring(L, libname);
|
||||
lua_pushvalue(L, -2);
|
||||
lua_settable(L, LUA_GLOBALSINDEX); /* register it with given name */
|
||||
lua_pushvalue(L, -1);
|
||||
/* register it with given name */
|
||||
lua_setfield(L, LUA_GLOBALSINDEX, libname);
|
||||
}
|
||||
lua_insert(L, -(nup+1)); /* move library table to below upvalues */
|
||||
}
|
||||
for (; l->name; l++) {
|
||||
int i;
|
||||
lua_pushstring(L, l->name);
|
||||
for (i=0; i<nup; i++) /* copy upvalues to the top */
|
||||
lua_pushvalue(L, -(nup+1));
|
||||
lua_pushvalue(L, -nup);
|
||||
lua_pushcclosure(L, l->func, nup);
|
||||
lua_settable(L, -(nup+3));
|
||||
lua_setfield(L, -(nup+2), l->name);
|
||||
}
|
||||
lua_pop(L, nup); /* remove upvalues */
|
||||
}
|
||||
@ -286,9 +281,8 @@ static void getsizes (lua_State *L) {
|
||||
lua_newtable(L); /* create it */
|
||||
lua_pushvalue(L, -1); /* `size' will be its own metatable */
|
||||
lua_setmetatable(L, -2);
|
||||
lua_pushliteral(L, "__mode");
|
||||
lua_pushliteral(L, "kv");
|
||||
lua_rawset(L, -3); /* metatable(N).__mode = "kv" */
|
||||
lua_setfield(L, -2, "__mode"); /* metatable(N).__mode = "kv" */
|
||||
lua_pushvalue(L, -1);
|
||||
lua_rawseti(L, LUA_REGISTRYINDEX, ARRAYSIZE_REF); /* store in register */
|
||||
}
|
||||
@ -297,12 +291,10 @@ static void getsizes (lua_State *L) {
|
||||
|
||||
void luaL_setn (lua_State *L, int t, int n) {
|
||||
t = abs_index(L, t);
|
||||
lua_pushliteral(L, "n");
|
||||
lua_rawget(L, t);
|
||||
lua_getfield(L, t, "n");
|
||||
if (checkint(L, 1) >= 0) { /* is there a numeric field `n'? */
|
||||
lua_pushliteral(L, "n"); /* use it */
|
||||
lua_pushinteger(L, n);
|
||||
lua_rawset(L, t);
|
||||
lua_setfield(L, t, "n");
|
||||
}
|
||||
else { /* use `sizes' */
|
||||
getsizes(L);
|
||||
@ -317,8 +309,7 @@ void luaL_setn (lua_State *L, int t, int n) {
|
||||
int luaL_getn (lua_State *L, int t) {
|
||||
int n;
|
||||
t = abs_index(L, t);
|
||||
lua_pushliteral(L, "n"); /* try t.n */
|
||||
lua_rawget(L, t);
|
||||
lua_getfield(L, t, "n"); /* try t.n */
|
||||
if ((n = checkint(L, 1)) >= 0) return n;
|
||||
getsizes(L); /* else try sizes[t] */
|
||||
lua_pushvalue(L, t);
|
||||
|
20
lbaselib.c
20
lbaselib.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lbaselib.c,v 1.133 2003/08/27 21:02:08 roberto Exp roberto $
|
||||
** $Id: lbaselib.c,v 1.134 2003/10/07 20:13:41 roberto Exp roberto $
|
||||
** Basic library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -220,8 +220,7 @@ static int luaB_next (lua_State *L) {
|
||||
|
||||
static int luaB_pairs (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
lua_pushliteral(L, "next");
|
||||
lua_rawget(L, LUA_GLOBALSINDEX); /* return generator, */
|
||||
lua_getfield(L, LUA_GLOBALSINDEX, "next"); /* return generator, */
|
||||
lua_pushvalue(L, 1); /* state, */
|
||||
lua_pushnil(L); /* and initial value */
|
||||
return 3;
|
||||
@ -232,8 +231,7 @@ static int luaB_ipairs (lua_State *L) {
|
||||
int i = (int)lua_tointeger(L, 2);
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
if (i == 0 && lua_isnone(L, 2)) { /* `for' start? */
|
||||
lua_pushliteral(L, "ipairs");
|
||||
lua_rawget(L, LUA_GLOBALSINDEX); /* return generator, */
|
||||
lua_getfield(L, LUA_GLOBALSINDEX, "ipairs"); /* return generator, */
|
||||
lua_pushvalue(L, 1); /* state, */
|
||||
lua_pushinteger(L, 0); /* and initial value */
|
||||
return 3;
|
||||
@ -693,23 +691,19 @@ static const luaL_reg co_funcs[] = {
|
||||
|
||||
|
||||
static void base_open (lua_State *L) {
|
||||
lua_pushliteral(L, "_G");
|
||||
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
||||
luaL_openlib(L, NULL, base_funcs, 0); /* open lib into global table */
|
||||
lua_pushliteral(L, "_VERSION");
|
||||
lua_pushliteral(L, LUA_VERSION);
|
||||
lua_rawset(L, -3); /* set global _VERSION */
|
||||
lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */
|
||||
/* `newproxy' needs a weaktable as upvalue */
|
||||
lua_pushliteral(L, "newproxy");
|
||||
lua_newtable(L); /* new table `w' */
|
||||
lua_pushvalue(L, -1); /* `w' will be its own metatable */
|
||||
lua_setmetatable(L, -2);
|
||||
lua_pushliteral(L, "__mode");
|
||||
lua_pushliteral(L, "kv");
|
||||
lua_rawset(L, -3); /* metatable(w).__mode = "kv" */
|
||||
lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */
|
||||
lua_pushcclosure(L, luaB_newproxy, 1);
|
||||
lua_rawset(L, -3); /* set global `newproxy' */
|
||||
lua_rawset(L, -1); /* set global _G */
|
||||
lua_setfield(L, -2, "newproxy"); /* set global `newproxy' */
|
||||
lua_setfield(L, -1, "_G"); /* set global _G */
|
||||
}
|
||||
|
||||
|
||||
|
16
ldblib.c
16
ldblib.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ldblib.c,v 1.81 2003/07/07 13:37:08 roberto Exp roberto $
|
||||
** $Id: ldblib.c,v 1.82 2003/10/07 20:13:41 roberto Exp roberto $
|
||||
** Interface from Lua to its debug API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -19,16 +19,14 @@
|
||||
|
||||
|
||||
static void settabss (lua_State *L, const char *i, const char *v) {
|
||||
lua_pushstring(L, i);
|
||||
lua_pushstring(L, v);
|
||||
lua_rawset(L, -3);
|
||||
lua_setfield(L, -2, i);
|
||||
}
|
||||
|
||||
|
||||
static void settabsi (lua_State *L, const char *i, int v) {
|
||||
lua_pushstring(L, i);
|
||||
lua_pushinteger(L, v);
|
||||
lua_rawset(L, -3);
|
||||
lua_setfield(L, -2, i);
|
||||
}
|
||||
|
||||
|
||||
@ -85,12 +83,11 @@ static int getinfo (lua_State *L) {
|
||||
settabss(L, "namewhat", ar.namewhat);
|
||||
break;
|
||||
case 'f':
|
||||
lua_pushliteral(L, "func");
|
||||
if (L == L1)
|
||||
lua_pushvalue(L, -3);
|
||||
lua_pushvalue(L, -2);
|
||||
else
|
||||
lua_xmove(L1, L, 1);
|
||||
lua_rawset(L, -3);
|
||||
lua_setfield(L, -2, "func");
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -341,9 +338,8 @@ static const luaL_reg dblib[] = {
|
||||
|
||||
LUALIB_API int luaopen_debug (lua_State *L) {
|
||||
luaL_openlib(L, LUA_DBLIBNAME, dblib, 0);
|
||||
lua_pushliteral(L, "_TRACEBACK");
|
||||
lua_pushcfunction(L, errorfb);
|
||||
lua_settable(L, LUA_GLOBALSINDEX);
|
||||
lua_setfield(L, LUA_GLOBALSINDEX, "_TRACEBACK");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
16
liolib.c
16
liolib.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: liolib.c,v 2.46 2003/08/25 19:49:47 roberto Exp roberto $
|
||||
** $Id: liolib.c,v 2.47 2003/10/07 20:13:41 roberto Exp roberto $
|
||||
** Standard I/O (and system) library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -236,8 +236,7 @@ static int io_readline (lua_State *L);
|
||||
|
||||
|
||||
static void aux_lines (lua_State *L, int idx, int close) {
|
||||
lua_pushliteral(L, FILEHANDLE);
|
||||
lua_rawget(L, LUA_REGISTRYINDEX);
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, FILEHANDLE);
|
||||
lua_pushvalue(L, idx);
|
||||
lua_pushboolean(L, close); /* close/not close file when finished */
|
||||
lua_pushcclosure(L, io_readline, 3);
|
||||
@ -512,9 +511,8 @@ static void createmeta (lua_State *L) {
|
||||
*newfile(L) = stdout;
|
||||
lua_rawseti(L, -2, IO_OUTPUT);
|
||||
/* file methods */
|
||||
lua_pushliteral(L, "__index");
|
||||
lua_pushvalue(L, -2); /* push metatable */
|
||||
lua_rawset(L, -3); /* metatable.__index = metatable */
|
||||
lua_pushvalue(L, -1); /* push metatable */
|
||||
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
|
||||
luaL_openlib(L, NULL, flib, 0);
|
||||
}
|
||||
|
||||
@ -594,8 +592,7 @@ static void setboolfield (lua_State *L, const char *key, int value) {
|
||||
|
||||
static int getboolfield (lua_State *L, const char *key) {
|
||||
int res;
|
||||
lua_pushstring(L, key);
|
||||
lua_gettable(L, -2);
|
||||
lua_getfield(L, -1, key);
|
||||
res = lua_toboolean(L, -1);
|
||||
lua_pop(L, 1);
|
||||
return res;
|
||||
@ -604,8 +601,7 @@ static int getboolfield (lua_State *L, const char *key) {
|
||||
|
||||
static int getfield (lua_State *L, const char *key, int d) {
|
||||
int res;
|
||||
lua_pushstring(L, key);
|
||||
lua_gettable(L, -2);
|
||||
lua_getfield(L, -1, key);
|
||||
if (lua_isnumber(L, -1))
|
||||
res = (int)lua_tointeger(L, -1);
|
||||
else {
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lmathlib.c,v 1.56 2003/03/11 12:30:37 roberto Exp roberto $
|
||||
** $Id: lmathlib.c,v 1.57 2003/10/07 20:13:41 roberto Exp roberto $
|
||||
** Standard mathematical library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -235,12 +235,10 @@ static const luaL_reg mathlib[] = {
|
||||
*/
|
||||
LUALIB_API int luaopen_math (lua_State *L) {
|
||||
luaL_openlib(L, LUA_MATHLIBNAME, mathlib, 0);
|
||||
lua_pushliteral(L, "pi");
|
||||
lua_pushnumber(L, PI);
|
||||
lua_settable(L, -3);
|
||||
lua_pushliteral(L, "__pow");
|
||||
lua_setfield(L, -2, "pi");
|
||||
lua_pushcfunction(L, math_pow);
|
||||
lua_settable(L, LUA_GLOBALSINDEX);
|
||||
lua_setfield(L, LUA_GLOBALSINDEX, "__pow");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
4
lua.h
4
lua.h
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lua.h,v 1.179 2003/10/02 20:31:17 roberto Exp roberto $
|
||||
** $Id: lua.h,v 1.180 2003/10/07 20:13:41 roberto Exp roberto $
|
||||
** Lua - An Extensible Extension Language
|
||||
** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil
|
||||
** http://www.lua.org mailto:info@lua.org
|
||||
@ -182,6 +182,7 @@ LUA_API void lua_pushlightuserdata (lua_State *L, void *p);
|
||||
** get functions (Lua -> stack)
|
||||
*/
|
||||
LUA_API void lua_gettable (lua_State *L, int idx);
|
||||
LUA_API void lua_getfield (lua_State *L, int idx, const char *k);
|
||||
LUA_API void lua_rawget (lua_State *L, int idx);
|
||||
LUA_API void lua_rawgeti (lua_State *L, int idx, int n);
|
||||
LUA_API void lua_newtable (lua_State *L);
|
||||
@ -194,6 +195,7 @@ LUA_API void lua_getfenv (lua_State *L, int idx);
|
||||
** set functions (stack -> Lua)
|
||||
*/
|
||||
LUA_API void lua_settable (lua_State *L, int idx);
|
||||
LUA_API void lua_setfield (lua_State *L, int idx, const char *k);
|
||||
LUA_API void lua_rawset (lua_State *L, int idx);
|
||||
LUA_API void lua_rawseti (lua_State *L, int idx, int n);
|
||||
LUA_API int lua_setmetatable (lua_State *L, int objindex);
|
||||
|
Loading…
x
Reference in New Issue
Block a user