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

allow getting at the Lua state from a Lua object #161

Merged
merged 2 commits into from
Sep 19, 2017

Conversation

sunshowers
Copy link
Contributor

This is most useful when the caller wants to do low-level operations on
the state that aren't supported through the main API.

Copy link
Owner

@tomaka tomaka left a comment

Choose a reason for hiding this comment

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

Thanks, looks good

hlua/src/lib.rs Outdated
/// higher-level APIs. For example, pushing a value onto the Lua stack will cause `PushGuard`s
/// in Rust code to be out of sync with the Lua stack.
#[inline]
pub fn state_ptr(&self) -> *mut ffi::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.

This shouldn't return a *mut ffi::lua_State, otherwise people are going to run into semver issues.
Instead I think it should return a *mut c_void, where c_void is imported from std::os::raw::c_void.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, by "semver issues" do you mean: say hlua moves to lua52-sys 0.2, but the user's using lua52-sys 0.1?

If so then I think this is still preferable to returning a void*. For a void*, to do anything useful a cast is always necessary. For a ffi::lua_State pointer a cast is only necessary if the version has changed.

(I might be misunderstanding what you're saying -- in that case feel free to correct me!)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Per some discussion here, an alternate solution would be to export the entire ffi module. That way callers that want to use the version of lua52-sys that corresponds to the hlua in use can just use it through hlua::ffi.

What do you think?

sunshowers added a commit to sunshowers/hlua that referenced this pull request Aug 29, 2017
In tomaka#161 there was some concern that
exporting a pointer to the underlying Lua state directly would cause
semver issues. This neatly bypasses those issues.
sunshowers added a commit to sunshowers/hlua that referenced this pull request Aug 29, 2017
In tomaka#161 there was some concern that
exporting a pointer to the underlying Lua state directly would cause
semver issues. This neatly bypasses those issues.
@sunshowers
Copy link
Contributor Author

Hey :) not sure if you saw it, but I pushed updated commits. Let me know what you think, thanks.

@tomaka
Copy link
Owner

tomaka commented Aug 31, 2017

I'm really cautious about doing this.
Do you actually need the change, or are you just submitting this PR because "why not"?

@sunshowers
Copy link
Contributor Author

sunshowers commented Aug 31, 2017 via email

@tomaka
Copy link
Owner

tomaka commented Aug 31, 2017

In my opinion if you need to call raw Lua functions, it means that hlua is missing the functionality you need.
Exposing the ffi kind of shows that the library fails at its goal.

It also means that it could be dangerous in the future to change the internal behaviour of hlua (eg. change so that some function pushes two elements on the stack instead of one), because it may break code that uses the FFI.

@sunshowers
Copy link
Contributor Author

sunshowers commented Aug 31, 2017 via email

@sunshowers
Copy link
Contributor Author

Hi @tomaka -- did you come to a discussion here? In the worst case I guess I'll keep on using mem::transmute.

@tomaka
Copy link
Owner

tomaka commented Sep 17, 2017

Since I'm still undecided, please add #[doc(hidden)] on both the extern crate and the state_ptr method, so that they exist but are not advertised in the docs.
Once that's done I'll merge.

In tomaka#161 there was some concern that
exporting a pointer to the underlying Lua state directly would cause
semver issues. This neatly bypasses those issues.
This is most useful when the caller wants to do low-level operations on
the state that aren't supported through the main API.

Also add a test to verify that low-level access works.
@sunshowers
Copy link
Contributor Author

Done! Thanks so much for your time dealing with this.

(If you could get a release out after this lands, that would be awesome! I can get rid of all our hacks at that point. :) )

@tomaka tomaka merged commit 6ac0dc2 into tomaka:master Sep 19, 2017
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