-
-extern lua_State *simgrid_lua_state;
-
-static int run_lua_code(int argc, char **argv)
-{
- DEBUG1("Run lua code %s", argv[0]);
- lua_State *L = lua_newthread(simgrid_lua_state);
- int ref = luaL_ref(simgrid_lua_state, LUA_REGISTRYINDEX); // protect the thread from being garbage collected
- int res = 1;
-
- /* Start the co-routine */
- lua_getglobal(L, argv[0]);
- xbt_assert1(lua_isfunction(L, -1),
- "The lua function %s does not seem to exist", argv[0]);
-
- // push arguments onto the stack
- int i;
- for (i = 1; i < argc; i++)
- lua_pushstring(L, argv[i]);
-
- // Call the function (in resume)
- xbt_assert2(lua_pcall(L, argc - 1, 1, 0) == 0,
- "error running function `%s': %s", argv[0], lua_tostring(L,
- -1));
-
- /* retrieve result */
- if (lua_isnumber(L, -1)) {
- res = lua_tonumber(L, -1);
- lua_pop(L, 1); /* pop returned value */
- }
- // cleanups
- luaL_unref(simgrid_lua_state, LUA_REGISTRYINDEX, ref);
- DEBUG1("Execution of lua code %s is over", (argv ? argv[0] : "(null)"));
- return res;
-}
-