Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alien.callback not working #41

Open
mikestar opened this issue Jun 12, 2014 · 2 comments
Open

alien.callback not working #41

mikestar opened this issue Jun 12, 2014 · 2 comments

Comments

@mikestar
Copy link

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
Copy link
Author

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->ffi_params = 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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants