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

Calling functions which receive shared_ptr<const TYPE>& or const shared_ptr<const TYPE>& doesn't work #1643

Open
EugenCazacu opened this issue Nov 6, 2024 · 0 comments

Comments

@EugenCazacu
Copy link

EugenCazacu commented Nov 6, 2024

Calling functions which receive shared_ptr<const TYPE>& or const shared_ptr<const TYPE>& doesn't work:

#include <iostream>
using namespace std;

#include "lua.hpp"

#define SOL_ALL_SAFETIES_ON 1
#include "sol3.hpp"

int main() {
    sol::state lua;
    lua.open_libraries(sol::lib::base);
    lua.set_function("intBuilder", []() -> shared_ptr<const int> { return make_shared<int>(10); });
    lua.set_function("intReader", [](shared_ptr<const int>& val) { cout << "intReader" << endl;} );
    lua.script(R"(
               print('hello from lua')
               local sharedInt = intBuilder()
               intReader(sharedInt)
               )");
    return 0;
}

The output I receive:

hello from lua
[sol2] An error occurred and has been passed to an error handler: sol: runtime error: stack index 1, expected userdata, received sol.sol::d::u<int>: unrecognized userdata (not pushed by sol?) (bad argument into 'void(std::shared_ptr<const int>&)')
stack traceback:
        [C]: in function 'base.intReader'
        [string "..."]:4: in main chunk
terminate called after throwing an instance of 'sol::error'
  what():  sol: runtime error: stack index 1, expected userdata, received sol.sol::d::u<int>: unrecognized userdata (not pushed by sol?) (bad argument into 'void(std::shared_ptr<const int>&)')
stack traceback:
        [C]: in function 'base.intReader'
        [string "..."]:4: in main chunk
Aborted (core dumped)

I use gcc 11.4.0 and sol2 v3.3.0 (revision eba8662), C++17.

For [v2.20.6] it's even worse as not even passing by value a shared_ptr<const TYPE> works.

I need to map some C++ interfaces to Lua and cannot change those interfaces. Many functions in those interfaces receive const shared_ptr<const TYPE>&.

For sol2 v3.3 I can work around this with a copy in a wrapped function and for 2.20.6, additionally I am wrapping the shared pointer into another struct and unwrapping that through the interfaces I need.

Can we get this fixed in the library or is there another 'simpler in some sense' way to work around this problem?
Am I missing something here?

Thank you

@EugenCazacu EugenCazacu changed the title Calling functions which receive shared_ptr<const TYPE>& o const shared_ptr<const TYPE>& doesn't work Calling functions which receive shared_ptr<const TYPE>& or const shared_ptr<const TYPE>& doesn't work Nov 6, 2024
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

No branches or pull requests

1 participant