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
+36 -2
View File
@@ -20,6 +20,11 @@
#include "wrap_Data.h"
// Put the Lua code directly into a raw string literal.
static const char data_lua[] =
#include "wrap_Data.lua"
;
namespace love
{
namespace data
@@ -44,6 +49,13 @@ int w_Data_getPointer(lua_State *L)
return 1;
}
// Placeholder, overridden by the FFI code when the FFI is available.
int w_Data_getFFIPointer(lua_State *L)
{
lua_pushnil(L);
return 1;
}
int w_Data_getSize(lua_State *L)
{
Data *t = luax_checkdata(L, 1);
@@ -51,18 +63,40 @@ int w_Data_getSize(lua_State *L)
return 1;
}
// C functions in a struct, necessary for the FFI versions of Data methods.
struct FFI_Data
{
void *(*getFFIPointer)(Proxy *p);
};
static FFI_Data ffifuncs =
{
[](Proxy *p) -> void * // getFFIPointer
{
auto data = luax_ffi_checktype<Data>(p);
return data != nullptr ? data->getData() : nullptr;
}
};
const luaL_Reg w_Data_functions[] =
{
{ "getString", w_Data_getString },
{ "getPointer", w_Data_getPointer },
{ "getFFIPointer", w_Data_getFFIPointer },
{ "getSize", w_Data_getSize },
{ 0, 0 }
};
void luax_rundatawrapper(lua_State *L, const love::Type &type)
{
luax_runwrapper(L, data_lua, sizeof(data_lua), "Data.lua", type, &ffifuncs);
}
int luaopen_data(lua_State *L)
{
luax_register_type(L, &Data::type, w_Data_functions, nullptr);
return 0;
int n = luax_register_type(L, &Data::type, w_Data_functions, nullptr);
luax_rundatawrapper(L, Data::type);
return n;
}
} // data