From af00a0772ca1a37f6d8cce5b6c03cc86db0389c3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 24 Oct 2011 12:54:05 -0200 Subject: [PATCH] new functions lua_rawsetp/lua_rawgetp --- lapi.c | 48 ++++++++++++++++++++++++++++++++++++++---------- ldblib.c | 11 ++++------- lua.h | 4 +++- 3 files changed, 45 insertions(+), 18 deletions(-) diff --git a/lapi.c b/lapi.c index 61a80085..ab3e92ff 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.152 2011/09/26 20:17:27 roberto Exp roberto $ +** $Id: lapi.c,v 2.153 2011/09/30 12:43:17 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -629,11 +629,24 @@ LUA_API void lua_rawget (lua_State *L, int idx) { LUA_API void lua_rawgeti (lua_State *L, int idx, int n) { - StkId o; + StkId t; lua_lock(L); - o = index2addr(L, idx); - api_check(L, ttistable(o), "table expected"); - setobj2s(L, L->top, luaH_getint(hvalue(o), n)); + t = index2addr(L, idx); + api_check(L, ttistable(t), "table expected"); + setobj2s(L, L->top, luaH_getint(hvalue(t), n)); + api_incr_top(L); + lua_unlock(L); +} + + +LUA_API void lua_rawgetp (lua_State *L, int idx, const void *p) { + StkId t; + TValue k; + lua_lock(L); + t = index2addr(L, idx); + api_check(L, ttistable(t), "table expected"); + setpvalue(&k, cast(void *, p)); + setobj2s(L, L->top, luaH_get(hvalue(t), &k)); api_incr_top(L); lua_unlock(L); } @@ -741,13 +754,28 @@ LUA_API void lua_rawset (lua_State *L, int idx) { LUA_API void lua_rawseti (lua_State *L, int idx, int n) { - StkId o; + StkId t; lua_lock(L); api_checknelems(L, 1); - o = index2addr(L, idx); - api_check(L, ttistable(o), "table expected"); - luaH_setint(L, hvalue(o), n, L->top - 1); - luaC_barrierback(L, gcvalue(o), L->top-1); + t = index2addr(L, idx); + api_check(L, ttistable(t), "table expected"); + luaH_setint(L, hvalue(t), n, L->top - 1); + luaC_barrierback(L, gcvalue(t), L->top-1); + L->top--; + lua_unlock(L); +} + + +LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { + StkId t; + TValue k; + lua_lock(L); + api_checknelems(L, 1); + t = index2addr(L, idx); + api_check(L, ttistable(t), "table expected"); + setpvalue(&k, cast(void *, p)); + setobj2t(L, luaH_set(L, hvalue(t), &k), L->top - 1); + luaC_barrierback(L, gcvalue(t), L->top - 1); L->top--; lua_unlock(L); } diff --git a/ldblib.c b/ldblib.c index 571f9773..35c47f81 100644 --- a/ldblib.c +++ b/ldblib.c @@ -1,5 +1,5 @@ /* -** $Id: ldblib.c,v 1.129 2011/01/26 16:30:02 roberto Exp roberto $ +** $Id: ldblib.c,v 1.130 2011/04/08 19:17:36 roberto Exp roberto $ ** Interface from Lua to its debug API ** See Copyright Notice in lua.h */ @@ -260,8 +260,7 @@ static void hookf (lua_State *L, lua_Debug *ar) { static const char *const hooknames[] = {"call", "return", "line", "count", "tail call"}; gethooktable(L); - lua_pushlightuserdata(L, L); - lua_rawget(L, -2); + lua_rawgetp(L, -1, L); if (lua_isfunction(L, -1)) { lua_pushstring(L, hooknames[(int)ar->event]); if (ar->currentline >= 0) @@ -308,9 +307,8 @@ static int db_sethook (lua_State *L) { func = hookf; mask = makemask(smask, count); } gethooktable(L); - lua_pushlightuserdata(L, L1); lua_pushvalue(L, arg+1); - lua_rawset(L, -3); /* set new hook */ + lua_rawsetp(L, -2, L1); /* set new hook */ lua_pop(L, 1); /* remove hook table */ lua_sethook(L1, func, mask, count); /* set hooks */ return 0; @@ -327,8 +325,7 @@ static int db_gethook (lua_State *L) { lua_pushliteral(L, "external hook"); else { gethooktable(L); - lua_pushlightuserdata(L, L1); - lua_rawget(L, -2); /* get hook */ + lua_rawgetp(L, -1, L1); /* get hook */ lua_remove(L, -2); /* remove hook table */ } lua_pushstring(L, unmakemask(mask, buff)); diff --git a/lua.h b/lua.h index 349ef6ed..6e3ff629 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.278 2011/07/02 16:00:15 roberto Exp roberto $ +** $Id: lua.h,v 1.279 2011/08/23 17:24:34 roberto Exp roberto $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -219,6 +219,7 @@ 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_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_newuserdata) (lua_State *L, size_t sz); LUA_API int (lua_getmetatable) (lua_State *L, int objindex); @@ -232,6 +233,7 @@ 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 void (lua_rawsetp) (lua_State *L, int idx, const void *p); LUA_API int (lua_setmetatable) (lua_State *L, int objindex); LUA_API void (lua_setuservalue) (lua_State *L, int idx);