ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.2k stars 516 forks source link

Can't pass nil for smart pointers like std::shared_ptr #1629

Open deadlocklogic opened 1 month ago

deadlocklogic commented 1 month ago

Why we can't pass nil to std::shared_ptr without registering a new overload. This behavior used to work previously according to my old tests, not anymore. Consider this example:

#define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>

struct Test {};

int main() {
    sol::state lua;
    lua.open_libraries(sol::lib::base);
    lua.new_usertype<::Test>("Test");

    lua["f"] = [](std::shared_ptr<Test> arg) {};
    lua.script("f(nil)");

    return 0;
}

Throws this error:

[sol2] An error occurred and has been passed to an error handler: sol: runtime error: stack index 1, expected userdata, received nil: value is not a userdata (bad argument into 'void(std::shared_ptr<Test>)')
stack traceback:
    [C]: in function 'base.f'
    [string "f(nil)"]:1: in main chunk
terminate called after throwing an instance of 'sol::error'
  what():  sol: runtime error: stack index 1, expected userdata, received nil: value is not a userdata (bad argument into 'void(std::shared_ptr<Test>)')
stack traceback:
    [C]: in function 'base.f'
    [string "f(nil)"]:1: in main chunk
Program terminated with signal: SIGSEGV

@ThePhD Any thoughts?

deadlocklogic commented 1 month ago

@Smertig Ever noticed this behavior change or I am missing something?

Smertig commented 1 month ago

@deadlocklogic I worked with this library too long ago, don't remember anything. Sorry.