| 81 | |
| 82 | |
| 83 | std::shared_ptr<lua_State> init_lua_state() |
| 84 | { |
| 85 | // Create a new lua_State, we'll use std::shared_ptr for automatic cleanup. |
| 86 | std::shared_ptr<lua_State> L(lua_open(), &lua_close); |
| 87 | |
| 88 | // Register the do_work function (above) as a C callback, under the global |
| 89 | // Lua name "do_work". With this, calls from Lua programs to do_work will |
| 90 | // land in the do_work C function we've registered. |
| 91 | lua_register( &*L, "do_work", &wrap_lua_CFunction<&do_work> ); |
| 92 | |
| 93 | // Pass some Lua code as a C string literal to Lua. This creates a global |
| 94 | // Lua function called "call_do_work", which we will later ask Lua to |
| 95 | // execute. |
| 96 | luaL_dostring( &*L, "\ |
| 97 | \n function call_do_work()\ |
| 98 | \n return do_work()\ |
| 99 | \n end" ); |
| 100 | |
| 101 | return L; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | // Here we will ask Lua to execute the function call_do_work, which is written |