mascarenhas / alien

A C FFI for Lua
http://mascarenhas.github.com/alien/
143 stars 45 forks source link

alien.callback not working #41

Open mikestar opened 10 years ago

mikestar commented 10 years ago

alien.callback seem to be ignoring the parameter type info and always passing 0 parameters to the Lua calback function. I'm unsure how to fix this.

mikestar commented 10 years ago

The changes below seem to fix this problem, I will do further testing. Not sure how to do a pull request of if its worth doing to fix a single function.

static int alien_callback_new(lua_State _L) { alien_Function ac; ffi_status status; luaL_checktype(L, 1, LUA_TFUNCTION); ac = (alien_Function )lua_newuserdata(L, sizeof(alien_Function)); if(!ac) return luaL_error(L, "alien: out of memory"); ac->fn = ffi_closure_alloc(sizeof(ffi_closure), &ac->ffi_codeloc); if(ac->fn == NULL) return luaL_error(L, "alien: cannot allocate callback"); ac->L = L; /_start delete ac->ret_type = AT_void; ac->ffi_ret_type = &ffi_type_void; ac->nparams = 0; ac->params = NULL; ac->ffiparams = NULL; end delete start add/ int ret_type = luaL_checkoption(L, 2, "int", alien_typenames); ac->ret_type = ret_type; ac->ffi_ret_type = ffitypes[ret_type]; ac->nparams = lua_gettop(L) - 3; if(ac->nparams > 0) { void aud; lua_Alloc lalloc = lua_getallocf(L, &aud); ac->ffi_params = (ffi_type _)lalloc(aud, NULL, 0, sizeof(ffi_type ) * ac->nparams); if(!ac->ffi_params) return luaL_error(L, "alien: out of memory"); ac->params = (alien_Type )lalloc(aud, NULL, 0, ac->nparams * sizeof(alien_Type)); if(!ac->params) return luaL_error(L, "alien: out of memory"); } else { ac->ffi_params = NULL; ac->params = NULL; } int i; for(i = 0; i < ac->nparams; i++) { int type = luaL_checkoption(L, i + 3, "int", alien_typenames); ac->ffi_params[i] = ffitypes[type]; ac->params[i] = type; } //end add lua_pushvalue(L, 1); ac->fn_ref = luaL_ref(L, LUA_REGISTRYINDEX); luaL_getmetatable(L, ALIEN_FUNCTION_META); lua_setmetatable(L, -2); status = ffi_prep_cif(&(ac->cif), FFI_DEFAULT_ABI, ac->nparams, ac->ffi_ret_type, ac->ffi_params); if(status == FFI_OK) status = ffi_prep_closure_loc(ac->fn, &(ac->cif), &alien_callback_call, ac, ac->ffi_codeloc); if(status != FFI_OK) { ffi_closure_free(ac->fn); return luaL_error(L, "alien: cannot create callback"); } ac->lib = NULL; ac->name = NULL;

return 1; }

mascarenhas commented 10 years ago

Hi Mike, if you send the change as a pull request it is easier for me to evaluate it. Thanks!