+
+/**
+ * \brief Returns whether a Lua state is the maestro state.
+ * \param L a Lua state
+ * \return true if this is maestro
+ */
+int sglua_is_maestro(lua_State* L) {
+ return L == sglua_maestro_state;
+}
+
+/**
+ * \brief Returns the maestro state.
+ * \return the maestro Lua state
+ */
+lua_State* sglua_get_maestro(void) {
+ return sglua_maestro_state;
+}
+
+/**
+ * \brief Makes the appropriate Simgrid functions available to the Lua world.
+ * \param L a Lua world
+ */
+void register_c_functions(lua_State *L) {
+
+ /* register the core C functions to lua */
+ luaL_register(L, "simgrid", simgrid_functions);
+
+ /* register the task methods to lua */
+ luaL_openlib(L, TASK_MODULE_NAME, task_functions, 0); // create methods table, add it to the globals
+ luaL_newmetatable(L, TASK_MODULE_NAME); // create metatable for Task, add it to the Lua registry
+ luaL_openlib(L, 0, task_meta, 0); // fill metatable
+ lua_pushliteral(L, "__index");
+ lua_pushvalue(L, -3); // dup methods table
+ lua_rawset(L, -3); // metatable.__index = methods
+ lua_pushliteral(L, "__metatable");
+ lua_pushvalue(L, -3); // dup methods table
+ lua_rawset(L, -3); // hide metatable:metatable.__metatable = methods
+ lua_pop(L, 1); // drop metatable
+
+ /* register the hosts methods to lua */
+ luaL_openlib(L, HOST_MODULE_NAME, host_functions, 0);
+ luaL_newmetatable(L, HOST_MODULE_NAME);
+ luaL_openlib(L, 0, host_meta, 0);
+ lua_pushliteral(L, "__index");
+ lua_pushvalue(L, -3);
+ lua_rawset(L, -3);
+ lua_pushliteral(L, "__metatable");
+ lua_pushvalue(L, -3);
+ lua_rawset(L, -3);
+ lua_pop(L, 1);
+
+ /* register the platform methods to lua */
+ luaL_openlib(L, PLATF_MODULE_NAME, platf_functions, 0);
+ luaL_newmetatable(L, PLATF_MODULE_NAME);
+ lua_pop(L, 1);
+}
+
+/**
+ * \brief Runs a Lua function as a new simulated process.
+ * \param argc number of arguments of the function
+ * \param argv name of the Lua function and array of its arguments
+ * \return result of the function
+ */
+static int run_lua_code(int argc, char **argv)
+{
+ XBT_DEBUG("Run lua code %s", argv[0]);
+
+ lua_State *L = sglua_clone_maestro();
+ int res = 1;
+
+ /* start the function */
+ lua_getglobal(L, argv[0]);
+ xbt_assert(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 */
+ _XBT_GNUC_UNUSED int err;
+ err = lua_pcall(L, argc - 1, 1, 0);
+ xbt_assert(err == 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 */
+ }
+
+ XBT_DEBUG("Execution of Lua code %s is over", (argv ? argv[0] : "(null)"));
+
+ return res;
+}