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

Function main

examples/source/dump.cpp:7–45  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5
6
7int main() {
8 std::cout << "=== dump (serialize between states) ==="
9 << std::endl;
10
11 // 2 states, transferring function from 1 to another
12 sol::state lua;
13 sol::state lua2;
14
15 // we're not going to run the code on the first
16 // state, so we only actually need
17 // the base lib on the second state
18 // (where we will run the serialized bytecode)
19 lua2.open_libraries(sol::lib::base);
20
21 // load this code (but do not run)
22 sol::load_result lr
23 = lua.load("a = function (v) print(v) return v end");
24 // check if it's sucessfully loaded
25 SOL_ASSERT(lr.valid());
26
27 // turn it into a function, then dump the bytecode
28 sol::protected_function target
29 = lr.get<sol::protected_function>();
30 sol::bytecode target_bc = target.dump();
31
32 // reload the byte code
33 // in the SECOND state
34 auto result2 = lua2.safe_script(
35 target_bc.as_string_view(), sol::script_pass_on_error);
36 // check if it was done properly
37 SOL_ASSERT(result2.valid());
38
39 // check in the second state if it was valid
40 sol::protected_function pf = lua2["a"];
41 int v = pf(25557);
42 SOL_ASSERT(v == 25557);
43
44 return 0;
45}

Callers

nothing calls this directly

Calls 6

open_librariesMethod · 0.80
loadMethod · 0.80
safe_scriptMethod · 0.80
as_string_viewMethod · 0.80
validMethod · 0.45
dumpMethod · 0.45

Tested by

no test coverage detected