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

Fail to convert std::shared_ptr -> sol::nil -> std::shared_ptr #419

Closed
noGithub-oO opened this issue Jun 7, 2017 · 2 comments
Closed

Fail to convert std::shared_ptr -> sol::nil -> std::shared_ptr #419

noGithub-oO opened this issue Jun 7, 2017 · 2 comments
Assignees
Labels
Bike.Shed WHY IS THE TAG GOLD I WANTED TO PAINT IT RED-- Help.Desk Maybe.Bug...?

Comments

@noGithub-oO
Copy link

I have a C++ function, that returns std::shared_ptr (factory). From lua code I call that function, store returned value in local var and then call another C++ function, that accepts std::shared_ptr. When factory returns nullptr, it gets pushed in lua as sol::nil, but when I later try to use that native lua nil in a call, it is not converted to std::shared_ptr, but rather causes application to crash. When compiling with type checks, sol detects type mismatch and panics.
Is there any way to instruct sol to automatically convert nil into deafult-constructed std::shared_ptr when forwarding it as an argument? Anyway, it seems strange, that such thing causes total crash, and not even a normal error (which can be checked and caught).

@ThePhD ThePhD self-assigned this Jun 7, 2017
@ThePhD ThePhD added Bike.Shed WHY IS THE TAG GOLD I WANTED TO PAINT IT RED-- Help.Desk Maybe.Bug...? labels Jun 7, 2017
@ThePhD
Copy link
Owner

ThePhD commented Jun 7, 2017

TL;DR: this is going to take a lot of effort to figure out the proper semantics for.

The glib answer to this question is "No". That is, when we get a std::shared_ptr out of Lua using sol2, we assume that lua_touserdata works and thusly we return you a reference to the actual memory (std::shared_ptr<stuff>&). This was done so that you could manipulate the shared_ptr directly in C++ without having to force a copy. The downside is that, because we return a reference, we can't "convert" anything: either it's there or it isn't, and when sol::lua_nil is there, then the obvious answer is you can't reference memory that doesn't exist (because it's a nullptr, and not a std::shared_ptr<stuff> that happens to have nullptr inside of it).

If you took a T* for your function argument, this problem goes away because we detect null and can hand you the value of a pointer (that is to say, T* is a trivial copy and if it's nullptr we don't have to care: it doesn't have to be a reference to some existing blob of memory and it is trivially copyable so you can take is by value in your function argument). If you take a shared_ptr by value, that means that a bogus, null reference can't be pulled successfully out of Lua.

"But what if you detect when I want something by-value, and when I want something by-reference, and then do The Right Thing™ when you can?" Difficult to do, and error-prone for everyone who wants to add their own pointer-types/unique_usertypes to sol2 or otherwise. They'd need to know the two type categories and how to handle them, plus constness and all, and that's a heavy burden of implementation alongside customization burden to give to the user.

@noGithub-oO
Copy link
Author

Ok, thank you very much for the explanation. Will use the raw-pointer-accepting API then.

@ThePhD ThePhD closed this as completed in 02110a5 Jun 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bike.Shed WHY IS THE TAG GOLD I WANTED TO PAINT IT RED-- Help.Desk Maybe.Bug...?
Projects
None yet
Development

No branches or pull requests

2 participants