diff options
author | Sam Gross <colesbury@gmail.com> | 2015-04-23 04:35:10 +0300 |
---|---|---|
committer | Sam Gross <colesbury@gmail.com> | 2015-04-28 20:58:46 +0300 |
commit | c5d1abcefb7e0d9967591ab073976cbd173ca626 (patch) | |
tree | 1d0958556dc283fa09559d237ce70637b8f18d77 | |
parent | 58670e9c6f40fb436886190b52853698c969c8a7 (diff) |
Overload type() to return "cdata" for cdata.
-rw-r--r-- | ffi.c | 23 |
1 files changed, 23 insertions, 0 deletions
@@ -2506,6 +2506,24 @@ static int ffi_errno(lua_State* L) return 1; } +static int ffi_type(lua_State* L) +{ + if (lua_isuserdata(L, 1) && lua_getmetatable(L, 1)) { + if (equals_upval(L, -1, &cdata_mt_key) || equals_upval(L, -1, &ctype_mt_key)) { + lua_pushstring(L, "cdata"); + return 1; + } + lua_pop(L, 1); /* mt */ + } + + /* call the old _G.type, we use an upvalue as _G.type is set + * to this function */ + lua_pushvalue(L, lua_upvalueindex(1)); + lua_insert(L, 1); + lua_call(L, lua_gettop(L)-1, LUA_MULTRET); + return lua_gettop(L); +} + static int ffi_number(lua_State* L) { struct ctype ct; @@ -3413,5 +3431,10 @@ int luaopen_ffi(lua_State* L) lua_setglobal(L, "tonumber"); lua_setfield(L, -2, "number"); /* ffi.number */ + lua_getglobal(L, "type"); + lua_pushcclosure(L, &ffi_type, 1); + lua_pushvalue(L, -1); + lua_setglobal(L, "type"); + lua_setfield(L, -2, "type"); /* ffi.type */ return 1; } |