diff options
| author | ruki <[email protected]> | 2020-08-04 22:45:20 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2020-08-04 22:45:20 +0800 |
| commit | 81c072eaa6eadde5f6427e0109226a7fa04691b0 (patch) | |
| tree | 7243963c4614209145b441144093889fface5102 /core/src/xmake/prefix.h | |
| parent | 8b08fbb9f5f0f54dd1232f4bf387932116dde47f (diff) | |
improve pushpointer
Diffstat (limited to 'core/src/xmake/prefix.h')
| -rw-r--r-- | core/src/xmake/prefix.h | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/core/src/xmake/prefix.h b/core/src/xmake/prefix.h index b63bf6679..1b3d519ea 100644 --- a/core/src/xmake/prefix.h +++ b/core/src/xmake/prefix.h @@ -29,6 +29,78 @@ #include "lualib.h" #include "lauxlib.h" +/* ////////////////////////////////////////////////////////////////////////////////////// + * private interfaces + */ + +#if TB_CPU_BIT64 +/* we use this interface instead of lua_pushlightuserdata() to fix bad light userdata pointer bug + * + * @see https://github.com/xmake-io/xmake/issues/914 + * + * @note we cannot lua_newuserdata() because we need pass this pointer to lua code in poller_wait()/event_callback, but lua_pushuserdata does not exists + */ +static __tb_inline__ tb_void_t xm_lua_pushpointer(lua_State* lua, tb_pointer_t ptr) +{ + tb_uint64_t ptrval = (tb_uint64_t)ptr; + tb_trace_i("ptr: %p %llx", ptrval >> 47); + if (0)//(ptrval >> 47) == 0) + lua_pushlightuserdata(lua, ptr); + else + { + tb_char_t str[64]; + tb_long_t len = tb_snprintf(str, sizeof(str), "%p", ptr); + lua_pushlstring(lua, str, len); + tb_trace_i("push ptr: %p, str: %s", ptr, str); + } +} +static __tb_inline__ tb_bool_t xm_lua_ispointer(lua_State* lua, tb_int_t idx) +{ + return lua_isuserdata(lua, idx) || lua_isstring(lua, idx); +} +static __tb_inline__ tb_pointer_t xm_lua_topointer2(lua_State* lua, tb_int_t idx, tb_char_t const** pstr) +{ + tb_pointer_t ptr = tb_null; + if (lua_isuserdata(lua, idx)) + { + ptr = lua_touserdata(lua, idx); + if (pstr) *pstr = tb_null; + } + else + { + size_t len = 0; + tb_char_t const* str = luaL_checklstring(lua, idx, &len); + if (str && len > 2 && str[0] == '0' && str[1] == 'x') + ptr = (tb_pointer_t)tb_s16tou64(str); + if (pstr) *pstr = str; + tb_trace_i("to ptr: %p, str: %s", ptr, str); + } + return ptr; +} +static __tb_inline__ tb_pointer_t xm_lua_topointer(lua_State* lua, tb_int_t idx) +{ + return xm_lua_topointer2(lua, idx, tb_null); +} +#else +static __tb_inline__ tb_void_t xm_lua_pushpointer(lua_State* lua, tb_pointer_t ptr) +{ + lua_pushlightuserdata(lua, ptr); +} +static __tb_inline__ tb_bool_t xm_lua_ispointer(lua_State* lua, tb_int_t idx) +{ + return lua_isuserdata(lua, idx); +} +static __tb_inline__ tb_pointer_t xm_lua_topointer2(lua_State* lua, tb_int_t idx, tb_char_t const** pstr) +{ + if (pstr) *pstr = tb_null; + return lua_touserdata(lua, idx); +} +static __tb_inline__ tb_pointer_t xm_lua_topointer(lua_State* lua, tb_int_t idx) +{ + return lua_touserdata(lua, idx); +} +#endif + #endif |
