Skip to content

Commit

Permalink
Support luaffi.
Browse files Browse the repository at this point in the history
Update torch.pushudata and torch.pointer to work with LuaFFI cdata.
  • Loading branch information
colesbury committed May 29, 2015
1 parent e271689 commit 8a7dc48
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 16 deletions.
6 changes: 2 additions & 4 deletions FFI.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
if jit then

local ffi = require 'ffi'

local ok, ffi = pcall(require, 'ffi')
if ok then
local Real2real = {
Byte='unsigned char',
Char='char',
Expand Down
75 changes: 63 additions & 12 deletions lib/luaT/luaT.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,12 @@ static const char cdataname[] = ""
"if ffi then\n"
" local id2name = {}\n"
" return function(cdata, name)\n"
" local id = tonumber(ffi.typeof(cdata))\n"
" local id\n"
" if jit then\n"
" id = tonumber(ffi.typeof(cdata))\n"
" else\n"
" id = tostring(ffi.typeof(cdata))\n"
" end\n"
" if id then\n"
" if name then\n"
" id2name[id] = name\n"
Expand Down Expand Up @@ -208,9 +213,47 @@ static const char* luaT_cdataname(lua_State *L, int ud, const char *tname)
return tname;
}

static void* CDATA_MT_KEY = &CDATA_MT_KEY;
static const char cdatamt[] = ""
"local _, ffi = pcall(require, 'ffi')\n"
"if ffi and not jit then\n"
" return ffi.debug().cdata_mt\n"
"else\n"
" return {}\n"
"end\n";

static int luaT_iscdata(lua_State *L, int ud)
{
int type = lua_type(L, ud);
if(type == 10)
return 1;
if(type != LUA_TUSERDATA)
return 0;
if(!lua_getmetatable(L, ud))
return 0;

lua_pushlightuserdata(L, CDATA_MT_KEY);
lua_rawget(L, LUA_REGISTRYINDEX);
if (lua_isnil(L, -1))
{
// initialize cdata metatable
lua_pop(L, 1);
if(luaL_dostring(L, cdatamt))
luaL_error(L, "internal error (could not load cdata mt): %s", lua_tostring(L, -1));

lua_pushlightuserdata(L, CDATA_MT_KEY);
lua_pushvalue(L, -2);
lua_rawset(L, LUA_REGISTRYINDEX);
}

int iscdata = lua_rawequal(L, -1, -2);
lua_pop(L, 2);
return iscdata;
}

const char* luaT_typename(lua_State *L, int ud)
{
if(lua_type(L, ud) == 10)
if(luaT_iscdata(L, ud))
return luaT_cdataname(L, ud, NULL);
else if(lua_getmetatable(L, ud))
{
Expand Down Expand Up @@ -689,6 +732,8 @@ int luaT_lua_pushudata(lua_State *L)

if(lua_type(L, 1) == 10)
udata = *((void**)lua_topointer(L, 1));
else if(luaT_iscdata(L, 1))
udata = ((void**)lua_topointer(L, 1))[4];
else if(lua_isnumber(L, 1))
udata = (void*)(long)lua_tonumber(L, 1);
else
Expand Down Expand Up @@ -763,7 +808,21 @@ int luaT_lua_isequal(lua_State *L)

int luaT_lua_pointer(lua_State *L)
{
if(lua_isuserdata(L, 1))
if(lua_type(L, 1) == 10) /* luajit cdata */
{
/* we want the pointer holded by cdata */
/* not the pointer on the cdata object */
const void* ptr = *((void**)lua_topointer(L, 1));
lua_pushnumber(L, (long)(ptr));
return 1;
}
else if (luaT_iscdata(L, 1)) /* luaffi cdata */
{
void** ptr = (void**)lua_touserdata(L, 1);
lua_pushnumber(L, (long)(ptr[4]));
return 1;
}
else if(lua_isuserdata(L, 1))
{
void **ptr;
luaL_argcheck(L, luaT_typename(L, 1), 1, "Torch object expected");
Expand All @@ -777,14 +836,6 @@ int luaT_lua_pointer(lua_State *L)
lua_pushnumber(L, (long)(ptr));
return 1;
}
else if(lua_type(L, 1) == 10) /* cdata */
{
/* we want the pointer holded by cdata */
/* not the pointer on the cdata object */
const void* ptr = *((void**)lua_topointer(L, 1));
lua_pushnumber(L, (long)(ptr));
return 1;
}
else if(lua_isstring(L, 1))
{
const char* ptr = lua_tostring(L, 1);
Expand Down Expand Up @@ -826,7 +877,7 @@ int luaT_lua_version(lua_State *L)
{
luaL_checkany(L, 1);

if(lua_type(L, 1) == 10)
if(luaT_iscdata(L, 1))
{
const char *tname = luaT_cdataname(L, 1, NULL);
if(tname)
Expand Down

0 comments on commit 8a7dc48

Please sign in to comment.