Giter Site home page Giter Site logo

Callbacks? about sol2 HOT 14 CLOSED

thephd avatar thephd commented on May 17, 2024
Callbacks?

from sol2.

Comments (14)

ThePhD avatar ThePhD commented on May 17, 2024 1

You can replace luaFncPointer cb with one of 2 types: std::function<ReturnType(Arguments...)> OR sol::function. sol::function is the most flexible (can pass it different number of args and stuff, while std::function has a fixed signature).

Code would look like...

lua.set_function("print", [](const char *  y){ std::cout << y << std::endl; });
lua.set_function("getUser", [](int id, sol::function cb)
{
/* ... */
cb( 24, some_ptr, ... ); // whatever you like
/* ... */
 });

Sorry about not having a place this is clear: I gotta pump up the docs to be a LOT more specific with this stuff so that people get the proper guidance.

from sol2.

ThePhD avatar ThePhD commented on May 17, 2024 1

NULL is a macro from the C standard that, in C++, usually resolves to 0. This will push an integer, which isn't what you want: try doing cb( sol::nil, "THE USER" ) or cb( nullptr, "THE USER" )

from sol2.

EToreo avatar EToreo commented on May 17, 2024

This is great! But how do I return null or empty to the first argument of the lua callback?

lua.set_function("getUser", [](int id, sol::function cb)
{
    cb(NULL, "THE USER");
});

from sol2.

EToreo avatar EToreo commented on May 17, 2024

Thank you!

from sol2.

EToreo avatar EToreo commented on May 17, 2024

Will sol::function take ellipsis?

from sol2.

ThePhD avatar ThePhD commented on May 17, 2024

No, it won't take ellipses, but you can feed it as many arguments as you like.

from sol2.

EToreo avatar EToreo commented on May 17, 2024

@ThePhD Thanks for all your help. I'm really enjoying your library. I have one more big question for you...

I am trying to call a sol::function callback from an asynchronous thread. My lua.set_function("getUser" [&](int id, sol::function gotUserDataCb {...}) has to asynchronously ask nodejs for some information, and in that response (using uv_queue_work, uv_after_work_cb, etc) I need to execute gotUserDataCb passing the data I received on the worker thread. My issue is: that causes a segmentation fault. I think this is because the sol::state goes out of scope long before getUser tries to respond.

Do you have any tips here? Am I on the correct track, should I try to find a way to keep the sol::state from going out of scope (make it a pointer and manually delete it)?

from sol2.

ThePhD avatar ThePhD commented on May 17, 2024

You need to shift the lifetime of the sol::state you set that function on. Move its scope up your application's stack a bit, so it will last. Make it a shared_ptr if you really need to keep it alive. There's no reason to manually track the lieftime with those kind of tools.

from sol2.

EToreo avatar EToreo commented on May 17, 2024

I took your suggestion and made it a shared pointer (moving the sol::state up in scope can't be done in my case). I am now getting an exception thrown from sol->script:

terminate called after throwing an instance of 'sol::error'
what(): lua: error: attempt to call a nil value

(The lua script is the same as the original posting in this issue.)

from sol2.

ThePhD avatar ThePhD commented on May 17, 2024

That could happen from a number of things. I have no idea what would be the problem, and I'm not sure I can reason about how your entire application stack sets up things.

from sol2.

EToreo avatar EToreo commented on May 17, 2024

Fair enough. I think the problem is that the actually sol::function gotUserDataCb is being called after lua->script is done executing. Is there a lua lock offered by sol? I have head mention of locking and unlocking in other discussions about lua hooks.

from sol2.

EToreo avatar EToreo commented on May 17, 2024

@ThePhD hey, I just wanted to say thank you. I got where I needed to go with the help of your library and advice. To solve the last problem I was having I had to implement lua_lock and lua_unlock in a custom lua build and bind that custom built lua library and header files into my program along with sol2. I now have a fully functioning bridge between nodejs and lua.

from sol2.

ThePhD avatar ThePhD commented on May 17, 2024

Nice! If you think there's a nice way to do lua_lock that can be added to sol2, feel free to open up another Bikeshed issue.

from sol2.

EToreo avatar EToreo commented on May 17, 2024

I appreciate your willingness but I doubt there is a way to do this. The lua team seems adamant that tread locking be a custom modification and build. The locking and unlocking functions are not even public, they expect you to modify the lua internal lua_state struct and interact with your modification. So, I really don't think there is a way to wrap customization like that.

If I think of something while I am cleaning up my experiment for prime-time, I will let you know.

from sol2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.