Horizon Official Technical Documentation
Sol2Test.cpp File Reference
#include <boost/test/unit_test.hpp>
#include <cstring>
#include <fstream>
#include <iostream>
#include <sol/sol.hpp>
+ Include dependency graph for Sol2Test.cpp:

Macros

#define BOOST_TEST_MODULE   "Sol2Test"
 

Functions

 BOOST_AUTO_TEST_CASE (Sol2Test)
 

Macro Definition Documentation

◆ BOOST_TEST_MODULE

#define BOOST_TEST_MODULE   "Sol2Test"

Function Documentation

◆ BOOST_AUTO_TEST_CASE()

BOOST_AUTO_TEST_CASE ( Sol2Test  )
42{
43 std::cout << "=== coroutine ===" << std::endl;
44
45 sol::state lua;
46 std::vector<sol::coroutine> tasks;
47
48 lua.open_libraries(sol::lib::base, sol::lib::coroutine, sol::lib::jit, sol::lib::math);
49
50 sol::thread runner_thread = sol::thread::create(lua);
51
52 lua.set_function("start_task",
53 [&runner_thread, &tasks](sol::function f, sol::variadic_args va) {
54 // You must ALWAYS get the current state
55 sol::state_view runner_thread_state = runner_thread.state();
56 // Put the task in our task list to keep it alive and track it
57 std::size_t task_index = tasks.size();
58 tasks.emplace_back(runner_thread_state, f);
59 sol::coroutine& f_on_runner_thread = tasks[task_index];
60 // call coroutine with arguments that came
61 // from main thread / other thread
62 // pusher for `variadic_args` and other sol types will transfer the
63 // arguments from the calling thread to
64 // the runner thread automatically for you
65 // using `lua_xmove` internally
66 int wait = f_on_runner_thread(va);
67 std::cout << "First return: " << wait << std::endl;
68 // When you call it again, you don't need new arguments
69 // (they remain the same from the first call)
70 f_on_runner_thread();
71 std::cout << "Second run complete: " << wait << std::endl;
72 });
73
74 lua.script(
75 R"(
76 function main(x, y, z)
77 -- do something
78 coroutine.yield(20)
79 -- do something else
80 -- do ...
81 print(x, y, z)
82 end
83 function main2(x, y)
84 coroutine.yield(10)