| 5 | #include <iostream> |
| 6 | |
| 7 | int main(int, char*[]) { |
| 8 | |
| 9 | sol::state lua; |
| 10 | lua.open_libraries(); |
| 11 | sol::function transferred_into; |
| 12 | lua["f"] = [&lua, &transferred_into]( |
| 13 | sol::object t, sol::this_state this_L) { |
| 14 | std::cout << "state of main : " |
| 15 | << (void*)lua.lua_state() << std::endl; |
| 16 | std::cout << "state of function : " |
| 17 | << (void*)this_L.lua_state() << std::endl; |
| 18 | // pass original lua_State* (or |
| 19 | // sol::state/sol::state_view) transfers ownership from |
| 20 | // the state of "t", to the "lua" sol::state |
| 21 | transferred_into = sol::function(lua, t); |
| 22 | }; |
| 23 | |
| 24 | lua.script(R"( |
| 25 | i = 0 |
| 26 | function test() |
| 27 | co = coroutine.create( |
| 28 | function() |
| 29 | local g = function() i = i + 1 end |
| 30 | f(g) |
| 31 | g = nil |
| 32 | collectgarbage() |
| 33 | end |
| 34 | ) |
| 35 | coroutine.resume(co) |
| 36 | co = nil |
| 37 | collectgarbage() |
| 38 | end |
| 39 | )"); |
| 40 | |
| 41 | // give it a try |
| 42 | lua.safe_script("test()"); |
| 43 | // should call 'g' from main thread, increment i by 1 |
| 44 | transferred_into(); |
| 45 | // check |
| 46 | int i = lua["i"]; |
| 47 | SOL_ASSERT(i == 1); |
| 48 | |
| 49 | return 0; |
| 50 | } |
nothing calls this directly
no test coverage detected