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

Returning nullptr from function doesn't give nil #434

Closed
okonk opened this issue Jun 28, 2017 · 5 comments
Closed

Returning nullptr from function doesn't give nil #434

okonk opened this issue Jun 28, 2017 · 5 comments
Assignees
Labels
Milestone

Comments

@okonk
Copy link

okonk commented Jun 28, 2017

Hi, I'm not sure what the expected behaviour is, but in an older version of sol2 this was giving nil as I expect, so maybe there's a bug here?

I've simplified it to the following test case:

#include <sol.hpp>
#include <memory>

class Test {

};

std::shared_ptr<Test> ReturnsNull() {
    return nullptr;
}

int main() {
    sol::state lua;
    lua.open_libraries(sol::lib::base);
    lua.new_usertype<Test>("Test");
    lua.set_function("returns_null", ReturnsNull);
    lua.script("print(returns_null() == nil)");
}

Lua is printing false when I expect it should be true.

@ThePhD ThePhD self-assigned this Jun 28, 2017
@ThePhD ThePhD added Bug.Well Shit Here we go again... Feature.Shiny labels Jun 28, 2017
@ThePhD ThePhD added this to the BugsDamnit milestone Jun 28, 2017
@ThePhD
Copy link
Owner

ThePhD commented Jun 28, 2017

This behavior was recently changed. When someone pushes a std::shared_ptr<T> that is nullptr, and then tries to get a std::shared_ptr out in a C++ function, it would crash and/or fail because we pushed nil and not a userdata that was nullptr.

This scenario, of course, now creates the problem you're seeing here.

I might be able to massage this to behave better by using a metatables == operator, but I need to test if it even kicks in for this case.

@ThePhD
Copy link
Owner

ThePhD commented Jun 28, 2017

An __eq metamethod cannot be used for this case:

Behavior similar to the addition operation, except that Lua will try a metamethod only when the values being compared are either both tables or both full userdata and they are not primitively equal. The result of the call is always converted to a boolean.

So we're a little screwed at this point. The best thing I can suggest is writing a function is_nil. That, or we would have to let returning a nullptr result in pushing nil into Lua, but ultimately disallow void func (std::shared_ptr<T>&/std::unique_ptr<T>&/my_special_ptr<T>&) from being a thing.

@ThePhD
Copy link
Owner

ThePhD commented Jun 28, 2017

That, or we demand people to do

void func( sol::optional<std::shared_ptr<T>&> maybe_ptr ) {
     ....
}

That way, if someone passes nil to that function, it won't crash? There's a lot of design choices here and I'm honestly not sure which is the right one.

@ThePhD
Copy link
Owner

ThePhD commented Jun 30, 2017

@okonk Pull from the latest. It exhibits the behavior you expect and want.

Have fun!

@ThePhD ThePhD closed this as completed Jun 30, 2017
@okonk
Copy link
Author

okonk commented Jun 30, 2017

Nice, thanks. :)

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

No branches or pull requests

2 participants