MCPcopy Create free account
hub / github.com/ThePhD/sol2 / main

Function main

examples/source/indirect_function_calls.cpp:38–77  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

36}
37
38int main(int, char*[]) {
39 std::cout << "=== indirect function calls ===" << std::endl;
40
41 sol::state lua;
42 lua.open_libraries(sol::lib::base);
43
44 lua["call_it"] = call_it;
45
46 // some functions to call
47 lua.script(R"(
48function add (a, b)
49 return a + b;
50end
51
52function subtract (a, b)
53 return a - b;
54end
55
56function log (x)
57 print(x)
58end
59)");
60
61 // call the functions indirectly, using a name
62 lua.script(R"(
63 call_it("log", "hiyo")
64 call_it("log", 24)
65 subtract_result = call_it("subtract", 5, 1)
66 add_result = call_it("add", 5, 1)
67 )");
68
69 int subtract_result = lua["subtract_result"];
70 int add_result = lua["add_result"];
71
72 SOL_ASSERT(add_result == 6);
73 SOL_ASSERT(subtract_result == 4);
74
75 std::cout << std::endl;
76 return 0;
77}

Callers

nothing calls this directly

Calls 2

open_librariesMethod · 0.80
scriptMethod · 0.80

Tested by

no test coverage detected