quickjspp icon indicating copy to clipboard operation
quickjspp copied to clipboard

Using lambda or function pointer for JS-callback

Open Maixmko opened this issue 4 years ago • 1 comments

From your example:

auto cb = (std::function<void(const std::string&)>) context.eval("my_callback");

is it possible instead to use lambda or function pointer? I tried

auto cb = (void(*)(const std::string&)) context.eval("my_callback");

It compiles, but on run gives TypeError: <null> object expected.

Maixmko avatar Jul 25 '20 05:07 Maixmko

You can't convert to function pointer. Now it's possible to convert to lambda:

auto cb = (std::function<void(const std::string&)>) context.eval("my_callback"); // cb is std::function
auto cb = context.eval("my_callback").as<std::function<void(const std::string&)>>(); // cb is lambda

ftk avatar Jul 28 '20 12:07 ftk