Add Data:getFFIPointer. Similar to Data:getPointer but returns a cdata pointer directly (or nil if the FFI isn't available).

It should be preferred instead of Data:getPointer because the latter uses lightuserdata which can't store more all possible memory addresses on some new arm64 architectures, when LuaJIT is used.
This commit is contained in:
Alex Szpakowski
2019-07-20 10:47:58 -03:00
parent d700800888
commit cb4b45dcf1
17 changed files with 170 additions and 64 deletions
+29 -1
View File
@@ -209,6 +209,13 @@ void luax_pushstring(lua_State *L, const std::string &str)
lua_pushlstring(L, str.data(), str.size());
}
void luax_pushpointerasstring(lua_State *L, const void *pointer)
{
char str[sizeof(void *)];
memcpy(str, &pointer, sizeof(void *));
lua_pushlstring(L, str, sizeof(void *));
}
bool luax_boolflag(lua_State *L, int table_index, const char *key, bool defaultValue)
{
lua_getfield(L, table_index, key);
@@ -452,7 +459,7 @@ int luax_register_type(lua_State *L, love::Type *type, ...)
return 0;
}
void luax_gettypemetatable(lua_State *L, love::Type &type)
void luax_gettypemetatable(lua_State *L, const love::Type &type)
{
const char *name = type.getName();
lua_getfield(L, LUA_REGISTRYINDEX, name);
@@ -886,6 +893,27 @@ void luax_register(lua_State *L, const char *name, const luaL_Reg *l)
}
}
void luax_runwrapper(lua_State *L, const char *filedata, size_t datalen, const char *filename, const love::Type &type, void *ffifuncs)
{
luax_gettypemetatable(L, type);
// Load and execute ImageData.lua, sending the metatable and the ffi
// functions struct pointer as arguments.
if (lua_istable(L, -1))
{
luaL_loadbuffer(L, filedata, datalen, filename);
lua_pushvalue(L, -2);
if (ffifuncs != nullptr)
luax_pushpointerasstring(L, ffifuncs);
else
lua_pushnil(L);
lua_call(L, 2, 0);
}
// Pop the metatable.
lua_pop(L, 1);
}
Type *luax_type(lua_State *L, int idx)
{
return Type::byName(luaL_checkstring(L, idx));