| 5 | #include <iostream> |
| 6 | |
| 7 | int main() { |
| 8 | sol::state lua; |
| 9 | lua.open_libraries(sol::lib::base); |
| 10 | |
| 11 | lua.script(R"( |
| 12 | obj = "please don't let me die"; |
| 13 | )"); |
| 14 | |
| 15 | sol::object keep_alive = lua["obj"]; |
| 16 | lua.script(R"( |
| 17 | obj = nil; |
| 18 | function say(msg) |
| 19 | print(msg) |
| 20 | end |
| 21 | )"); |
| 22 | |
| 23 | lua.collect_garbage(); |
| 24 | |
| 25 | lua["say"](lua["obj"]); |
| 26 | // still accessible here and still alive in Lua |
| 27 | // even though the name was cleared |
| 28 | std::string message = keep_alive.as<std::string>(); |
| 29 | std::cout << message << std::endl; |
| 30 | |
| 31 | // Can be pushed back into Lua as an argument |
| 32 | // or set to a new name, |
| 33 | // whatever you like! |
| 34 | lua["say"](keep_alive); |
| 35 | |
| 36 | return 0; |
| 37 | } |
nothing calls this directly
no test coverage detected