| 4 | #include <iostream> |
| 5 | |
| 6 | int main() { |
| 7 | const auto& co_lua_script = R"( |
| 8 | function loop() |
| 9 | while counter ~= 30 |
| 10 | do |
| 11 | coroutine.yield(counter); |
| 12 | counter = counter + 1; |
| 13 | end |
| 14 | return counter |
| 15 | end |
| 16 | )"; |
| 17 | |
| 18 | sol::state lua; |
| 19 | lua.open_libraries(sol::lib::base, sol::lib::coroutine); |
| 20 | /* |
| 21 | lua.script_file("co.lua"); |
| 22 | we load string directly rather than use a file |
| 23 | */ |
| 24 | lua.script(co_lua_script); |
| 25 | sol::thread runner = sol::thread::create(lua.lua_state()); |
| 26 | sol::state_view runnerstate = runner.state(); |
| 27 | sol::coroutine loop_coroutine = runnerstate["loop"]; |
| 28 | lua["counter"] = 20; |
| 29 | |
| 30 | for (int counter = 0; counter < 10 && loop_coroutine; |
| 31 | ++counter) { |
| 32 | // Call the coroutine, does the computation and then |
| 33 | // suspends |
| 34 | int value = loop_coroutine(); |
| 35 | std::cout << "value is " << value << std::endl; |
| 36 | } |
| 37 | |
| 38 | return 0; |
| 39 | } |
nothing calls this directly
no test coverage detected