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

Can addFunction support lambda? #245

Open
wenyg opened this issue Mar 12, 2021 · 6 comments
Open

Can addFunction support lambda? #245

wenyg opened this issue Mar 12, 2021 · 6 comments

Comments

@wenyg
Copy link

wenyg commented Mar 12, 2021

I want to pass some local variables from C++ to lua, but these variables can only be global variables.

This is what I want

void my_process(){
  std::string my_data;
  luabridge::getGlobalNamespace(L)
      .beginNamespace ("ctx")
          .addFunction("get", [&my_data](){ return my_data.c_str();})
          .AddFunction("set", [&my_data](const char *data){ my_data = std::string(data);})
      .endNamespace();
  // process my_data in lua
  luaL_dofile(L, "./test.lua"); 
  // now, my_data is the processed data
}

./test.lua like this

data = ctx.get()
-- do something in lua
ctx.set(processed_data)

What should I do to pass C++ local variables to Lua, and then get it from lua? Thank you very much

@kunitoki
Copy link

This is fully supported in https://github.com/kunitoki/LuaBridge3

@dmitry-t
Copy link
Collaborator

dmitry-t commented May 8, 2021

OK, once kunitoki's fork is fully compatible with the LuaBridge2, anybody can switch to the LuaBridge3.
@kunitoki Can you confirm your version is fully compatible?

@kunitoki
Copy link

kunitoki commented May 8, 2021

It's not fully compatible, i made some design decisions that were needed to support using the library without exceptions (games you know, game developers are allergic to exceptions), so some stuff has changed. There is a long list of breaking changes and removals. See https://kunitoki.github.io/LuaBridge3/CHANGES

@dmitry-t
Copy link
Collaborator

dmitry-t commented May 13, 2021

I see. And I'd prefer C++17 :)

Most of code including older compiler support and e.g. addData are just for backward compatibility.

BTW the most unpleasant thing for one who wants to migrate from the LB2 to the LB3 is a different stack push signature. I guess this can be simplified by adding support for methods with the old signature using a meta-programming trick.
And you could also add the aliases like addData back and mark them [[deprecated]].
The only really breaking change then would be mandatory C++17 support which isn't really rare nowadays :)

@kunitoki
Copy link

I want to polish the library, i'm seeking of removing legacy. If one wants addData can work with the older lb2.

@kunitoki
Copy link

Yeah for the stack changes, i might introduce more changes, like rolling an own expected as return value http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0323r8.html

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

3 participants