| 5 | |
| 6 | |
| 7 | int 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 | } |
nothing calls this directly
no test coverage detected