lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 31 August 2013 23:07, Tim Hill <drtimhill@gmail.com> wrote:
> Anyone else see this as a useful (and cheap) nice to have in the API?

Yes, I often wish standard Lua would have something along the lines of this API:
(WARNING totally untested patch)

diff --git a/src/lapi.c b/src/lapi.c
index 791d854..f3ea7c4 100644
--- a/src/lapi.c
+++ b/src/lapi.c
@@ -1282,3 +1282,10 @@ LUA_API void lua_upvaluejoin (lua_State *L, int
fidx1, int n1,
   luaC_objbarrier(L, f1, *up2);
 }

+LUA_API void lua_setstatedata (lua_State *L, void *sd) {
+  L->l_G->sd = sd;
+}
+
+LUA_API void *lua_getstatedata (lua_State *L) {
+  return L->l_G->sd;
+}
diff --git a/src/lstate.h b/src/lstate.h
index c8a31f5..b24afb5 100644
--- a/src/lstate.h
+++ b/src/lstate.h
@@ -112,6 +112,7 @@ typedef struct CallInfo {
 typedef struct global_State {
   lua_Alloc frealloc;  /* function to reallocate memory */
   void *ud;         /* auxiliary data to `frealloc' */
+  void *sd;         /* auxiliary per-state data */
   lu_mem totalbytes;  /* number of bytes currently allocated - GCdebt */
   l_mem GCdebt;  /* bytes allocated not yet compensated by the collector */
   lu_mem GCmemtrav;  /* memory traversed by the GC */
diff --git a/src/lua.h b/src/lua.h
index eb0482b..a319fd6 100644
--- a/src/lua.h
+++ b/src/lua.h
@@ -308,6 +308,8 @@ LUA_API void  (lua_len)    (lua_State *L, int idx);
 LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
 LUA_API void      (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);

+LUA_API void  (lua_setstatedata) (lua_State *L, void *sd);
+LUA_API void *(lua_getstatedata) (lua_State *L);


 /*