Skip to content

Conversation

giodamelio
Copy link

@giodamelio giodamelio commented May 7, 2017

See #146.

I think I got this mostly working, but since I have never programmed in C or used the Rust FFI before things could be totally broken.

The Lua 5.2 Docs say that you should use requiref to require individual libraries. So I basically implement this. My understanding of Lua internals is practically non-existent, so I hope this is right.

Just one part I'm not sure how to do cleanly. See the line comment below.

pub struct lua_State;

pub type lua_CFunction = extern "C" fn(L: *mut lua_State) -> c_int;
pub type lua_CFunction2 = unsafe extern "C" fn(*mut lua_State);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why I needed to create this type, I see other FFI declerations use the lua_CFunction type, and the docs for requiref don't seem any different to me.

Copy link
Owner

@tomaka tomaka May 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not change the definition of luaL_requiref to use lua_CFunction instead?

EDIT: Ah nevermind, I see. I suppose you could also cast the function pointers instead.
I'd prefer if bindings remained identical to the C header.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is basically what I cobbled together from reading Rust's error messages and trying a bunch of random things. I really don't have the expertise to go much further.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should change open_helper to accept a unsafe extern "C" fn(*mut lua_State) instead of a ffi::lua_CFunction2 (which allows you to remove lua_CFunction2 from the bindings).

Then in open_helper you should be able to do something like func as ffi::lua_CFunction. If it doesn't work, use mem::transmute(func). It's the last resort solution, but at least it would compile.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That still won't help the luaL_requiref binding that needs the lua_CFunction2 though?

pub struct lua_State;

pub type lua_CFunction = extern "C" fn(L: *mut lua_State) -> c_int;
pub type lua_CFunction2 = unsafe extern "C" fn(*mut lua_State);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should change open_helper to accept a unsafe extern "C" fn(*mut lua_State) instead of a ffi::lua_CFunction2 (which allows you to remove lua_CFunction2 from the bindings).

Then in open_helper you should be able to do something like func as ffi::lua_CFunction. If it doesn't work, use mem::transmute(func). It's the last resort solution, but at least it would compile.

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

Successfully merging this pull request may close these issues.

2 participants