diff --git a/src/ctx.nobj.lua b/src/ctx.nobj.lua index adc0c42..52f5252 100644 --- a/src/ctx.nobj.lua +++ b/src/ctx.nobj.lua @@ -19,6 +19,7 @@ -- THE SOFTWARE. object "ZMQ_Ctx" { + sys_include"string.h", error_on_null = "get_zmq_strerror()", c_source [[ typedef struct ZMQ_Ctx ZMQ_Ctx; @@ -26,10 +27,19 @@ typedef struct ZMQ_Ctx ZMQ_Ctx; destructor "term" { c_method_call "ZMQ_Error" "zmq_term" {} }, - method "lightuserdata" { + method "lightuserdata" { override_this = true, + var_in{ "", "this" }, var_out{ "void *", "ptr" }, c_source[[ - ${ptr} = ${this}; + if(lua_isuserdata(L, ${this::idx})) { + ${ptr} = lua_touserdata(L, ${this::idx}); + } else { + /* check for LuaJIT's cdata. */ + int tp = lua_type(L, ${this::idx}); + if(strncmp("cdata", lua_typename(L, tp), 5) == 0) { + ${ptr} = *((void **)lua_topointer(L, ${this::idx})); + } + } ]] }, method "socket" { diff --git a/zmq.nobj.lua b/zmq.nobj.lua index bf8711d..6a2c00f 100644 --- a/zmq.nobj.lua +++ b/zmq.nobj.lua @@ -145,9 +145,9 @@ c_function "init_ctx" { ffi_source[[ local p_type = type(${ptr}) if p_type == 'userdata' then - ${ctx} = ffi.cast('void *', ${ptr}); - elseif p_type == 'cdata' then - ${ctx} = ${ptr}; + ${ctx} = ffi.cast('ZMQ_Ctx *', ${ptr}); + elseif p_type == 'cdata' and ffi.istype('void *', ${ptr}) then + ${ctx} = ffi.cast('ZMQ_Ctx *', ${ptr}); else return error("expected lightuserdata/cdata"); end