summaryrefslogtreecommitdiff
path: root/src/lib/elua
diff options
context:
space:
mode:
authorDaniel Kolesa <d.kolesa@osg.samsung.com>2018-04-13 15:09:21 +0200
committerDaniel Kolesa <d.kolesa@osg.samsung.com>2018-04-13 15:40:05 +0200
commitb8f28e2a99535a8e9780f9ea15572693bf0f7148 (patch)
treeb4e6fbaec36ea955a04508cc42de8e6a69a8226d /src/lib/elua
parent749c5429a7271105d96e2c41831e10e7b08b7723 (diff)
elua: get rid of lightuserdata for state retrieval
In order to get around the lightuserdata 48-bit problem, split the state pointer into two and reconstruct it later as necessary.
Diffstat (limited to 'src/lib/elua')
-rw-r--r--src/lib/elua/elua.c42
-rw-r--r--src/lib/elua/elua_private.h1
2 files changed, 27 insertions, 16 deletions
diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c
index 3f3654e678..baaed81721 100644
--- a/src/lib/elua/elua.c
+++ b/src/lib/elua/elua.c
@@ -1,6 +1,6 @@
1#include "elua_private.h" 1#include <limits.h>
2
3#include <Ecore_File.h> 2#include <Ecore_File.h>
3#include "elua_private.h"
4 4
5static Eina_Prefix *_elua_pfx = NULL; 5static Eina_Prefix *_elua_pfx = NULL;
6 6
@@ -70,7 +70,6 @@ EAPI Elua_State *
70elua_state_new(const char *progname) 70elua_state_new(const char *progname)
71{ 71{
72 Elua_State *ret = NULL; 72 Elua_State *ret = NULL;
73 Eina_Safepointer *sp;
74 lua_State *L = luaL_newstate(); 73 lua_State *L = luaL_newstate();
75 if (!L) 74 if (!L)
76 return NULL; 75 return NULL;
@@ -78,10 +77,21 @@ elua_state_new(const char *progname)
78 ret->luastate = L; 77 ret->luastate = L;
79 if (progname) ret->progname = eina_stringshare_add(progname); 78 if (progname) ret->progname = eina_stringshare_add(progname);
80 luaL_openlibs(L); 79 luaL_openlibs(L);
81 sp = (Eina_Safepointer *)eina_safepointer_register(ret); 80 /* on 64-bit, split the state pointer into two and reconstruct later */
82 ret->sp = sp; 81 size_t retn = (size_t)ret;
83 lua_pushlightuserdata(L, sp); 82 if (sizeof(void *) < sizeof(lua_Number))
84 lua_setfield(L, LUA_REGISTRYINDEX, "elua_ptr"); 83 {
84 lua_pushnumber(L, 0);
85 lua_pushnumber(L, (lua_Number)retn);
86 }
87 else
88 {
89 size_t hbits = (sizeof(void *) / 2) * CHAR_BIT;
90 lua_pushnumber(L, (lua_Number)(retn >> hbits));
91 lua_pushnumber(L, (lua_Number)(retn & (((size_t)1 << hbits) - 1)));
92 }
93 lua_setfield(L, LUA_REGISTRYINDEX, "elua_ptr1");
94 lua_setfield(L, LUA_REGISTRYINDEX, "elua_ptr2");
85 return ret; 95 return ret;
86} 96}
87 97
@@ -109,7 +119,6 @@ elua_state_free(Elua_State *es)
109 eina_stringshare_del(es->coredir); 119 eina_stringshare_del(es->coredir);
110 eina_stringshare_del(es->moddir); 120 eina_stringshare_del(es->moddir);
111 eina_stringshare_del(es->appsdir); 121 eina_stringshare_del(es->appsdir);
112 eina_safepointer_unregister(es->sp);
113 free(es); 122 free(es);
114} 123}
115 124
@@ -261,15 +270,18 @@ EAPI Elua_State *
261elua_state_from_lua_state_get(lua_State *L) 270elua_state_from_lua_state_get(lua_State *L)
262{ 271{
263 EINA_SAFETY_ON_NULL_RETURN_VAL(L, NULL); 272 EINA_SAFETY_ON_NULL_RETURN_VAL(L, NULL);
264 lua_getfield(L, LUA_REGISTRYINDEX, "elua_ptr"); 273 lua_getfield(L, LUA_REGISTRYINDEX, "elua_ptr1");
265 if (!lua_isnil(L, -1)) 274 lua_getfield(L, LUA_REGISTRYINDEX, "elua_ptr2");
275 if (!lua_isnil(L, -1) && !lua_isnil(L, -2))
266 { 276 {
267 Eina_Safepointer *sp = lua_touserdata(L, -1); 277 size_t p1 = (size_t)lua_tonumber(L, -2),
268 void *st = eina_safepointer_get(sp); 278 p2 = (size_t)lua_tonumber(L, -1);
269 lua_pop(L, 1); 279 if (p2 && (sizeof(void *) >= sizeof(lua_Number)))
270 return (Elua_State *)st; 280 p1 |= p2 << ((sizeof(void *) / 2) * CHAR_BIT);
281 lua_pop(L, 2);
282 return (Elua_State *)p1;
271 } 283 }
272 lua_pop(L, 1); 284 lua_pop(L, 2);
273 return NULL; 285 return NULL;
274} 286}
275 287
diff --git a/src/lib/elua/elua_private.h b/src/lib/elua/elua_private.h
index 1fd955c3ea..f4392f604b 100644
--- a/src/lib/elua/elua_private.h
+++ b/src/lib/elua/elua_private.h
@@ -40,7 +40,6 @@ struct _Elua_State
40 Eina_List *lmods; 40 Eina_List *lmods;
41 Eina_List *cmods; 41 Eina_List *cmods;
42 Eina_List *lincs; 42 Eina_List *lincs;
43 Eina_Safepointer *sp;
44 int requireref, apploadref; 43 int requireref, apploadref;
45}; 44};
46 45