| 64 | |
| 65 | |
| 66 | std::shared_ptr<lua_State> init_lua_state() |
| 67 | { |
| 68 | // Create a new lua_State, we'll use std::shared_ptr for automatic cleanup. |
| 69 | std::shared_ptr<lua_State> L(lua_open(), &lua_close); |
| 70 | |
| 71 | // Register the do_work function (above) as a C callback, under the global |
| 72 | // Lua name "do_work". With this, calls from Lua programs to do_work will |
| 73 | // land in the do_work C function we've registered. |
| 74 | lua_register( &*L, "do_work", &do_work ); |
| 75 | |
| 76 | // Pass some Lua code as a C string literal to Lua. This creates a global |
| 77 | // Lua function called "call_do_work", which we will later ask Lua to |
| 78 | // execute. |
| 79 | luaL_dostring( &*L, "\ |
| 80 | \n function call_do_work()\ |
| 81 | \n return do_work()\ |
| 82 | \n end" ); |
| 83 | |
| 84 | return L; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | // Here we will ask Lua to execute the function call_do_work, which is written |