Comments (14)
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.
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.
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.
Thank you!
from sol2.
Will sol::function take ellipsis?
from sol2.
No, it won't take ellipses, but you can feed it as many arguments as you like.
from sol2.
@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.
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.
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.
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.
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.
@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.
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.
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)
- Generating definition files
- Why sol3 is slower than Luabridge3 when both using LuaJIT, HOT 1
- add_package_loader without lua_CFunction HOT 1
- Handling opaque C pointers
- confusing behaviour of environments for lua > 5.2 HOT 1
- Error in emplace method
- const correctness of C++ object passed to sol::protected_function
- Using libraries which add searchers/loaders from C++ HOT 1
- Preserving ordering in sol2 tables and converting to ordered JSON HOT 2
- Getting compilation error when registering type HOT 3
- compilation issue: no member named 'construct' HOT 4
- Add mirrors for downloading lua
- SOL_SAFE_FUNCTION_CALLS does not recognize derived classes HOT 5
- Clang 18 support HOT 1
- How to secure such a pointer?
- Impossible to create an inner usertype/enum in a usertype
- in clang 19.1, i have error : no member named 'construct' in 'optional<type-parameter-0-0 &>' (line 6821 in sol.hpp) HOT 1
- sol3 crash/recursion after adding custom type with non-explicit constructors
- Clangd crash (LLVM issue?) when including sol headers in a C++20 module HOT 2
- Enhanced container operations
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sol2.