* - Argument 2 (number): computation size
* - Argument 3 (number): communication size
* - Return value (task): the task created
+ *
+ * A Lua task is a regular table with a full userdata inside, and both share
+ * the same metatable. For the regular table, the metatable allows OO-style
+ * writing such as your_task:send(someone).
+ * For the userdata, the metatable is used to check its type.
*/
static int l_task_new(lua_State* L)
{
lua_newtable(L);
/* task */
+ luaL_getmetatable(L, TASK_MODULE_NAME);
+ /* task mt */
+ lua_setmetatable(L, -2);
+ /* task */
m_task_t* lua_task = (m_task_t*) lua_newuserdata(L, sizeof(m_task_t));
/* task ctask */
*lua_task = msg_task;
return 1;
}
+/**
+ * \brief Metamethods of both a task table and the userdata inside it.
+ */
static const luaL_reg task_meta[] = {
- {"__gc", l_task_gc},
+ {"__gc", l_task_gc}, /* will be called only for userdata */
{"__tostring", l_task_tostring},
{NULL, NULL}
};
* \param L a Lua state
* \return number of values returned to Lua
*/
-static int clean(lua_State * L)
+static int simgrid_gc(lua_State * L)
{
MSG_clean();
return 0;
{"debug", debug},
{"info", info},
{"run", run},
- {"clean", clean},
/* short names */
{"platform", create_environment},
{"application", launch_application},
*
* \param L a lua state
*/
-static void register_task_functions(lua_State* L) {
-
+static void register_task_functions(lua_State* L)
+{
/* create a table simgrid.task and fill it with task functions */
luaL_openlib(L, TASK_MODULE_NAME, task_functions, 0);
/* simgrid.task */
lua_pushvalue(L, -2);
/* simgrid.task mt simgrid.task */
/* metatable.__index = simgrid.task
- * we put the task functions inside the task userdata itself:
+ * we put the task functions inside the task itself:
* this allows to write task:method(args) for
* simgrid.task.method(task, args) */
- // FIXME: in the current implementation, a Lua task is a table with a
- // __simgrid_task field that contains the userdata, so the OO-style
- // writing doesn't work
lua_setfield(L, -2, "__index");
/* simgrid.task mt */
- lua_pushliteral(L, "__metatable");
- /* simgrid.task mt "__metatable" */
- lua_pushnumber(L, 0);
- /* simgrid.task mt "__metatable" 0 */
- /* protect the metatable from Lua code (by setting any value to __metatable) */
- lua_rawset(L, -3); /* simgrid.task mt */
lua_pop(L, 2);
/* -- */
}
*
* \param L a lua state
*/
-static void register_host_functions(lua_State* L) {
-
+static void register_host_functions(lua_State* L)
+{
/* create a table simgrid.host and fill it with host functions */
luaL_openlib(L, HOST_MODULE_NAME, host_functions, 0);
/* simgrid.host */
// FIXME: cannot work currently, same problem as tasks
lua_setfield(L, -2, "__index");
/* simgrid.host mt */
- lua_pushliteral(L, "__metatable");
- /* simgrid.host mt "__metatable" */
- lua_pushnumber(L, 0);
- /* simgrid.host mt "__metatable" 0 */
- /* protect the metatable from Lua code (by setting any value to __metatable) */
- lua_rawset(L, -3); /* simgrid.host mt */
lua_pop(L, 2);
/* -- */
}
* \brief Registers the platform functions into the table simgrid.platf.
* \param L a lua state
*/
-static void register_platf_functions(lua_State* L) {
-
+static void register_platf_functions(lua_State* L)
+{
luaL_openlib(L, PLATF_MODULE_NAME, platf_functions, 0);
/* simgrid.platf */
lua_pop(L, 1);
}
/**
- * \brief Makes the Simgrid functions available to the Lua world.
+ * \brief Makes the core functions available to the Lua world.
* \param L a Lua world
*/
-static void register_c_functions(lua_State *L) {
-
+static void register_core_functions(lua_State *L)
+{
/* register the core C functions to lua */
luaL_register(L, "simgrid", simgrid_functions);
/* simgrid */
+
+ /* set a finalizer that cleans simgrid, by adding to the simgrid module a
+ * dummy userdata whose __gc metamethod calls MSG_clean() */
+ lua_newuserdata(L, sizeof(void*));
+ /* simgrid udata */
+ lua_newtable(L);
+ /* simgrid udata mt */
+ lua_pushcfunction(L, simgrid_gc);
+ /* simgrid udata mt simgrid_gc */
+ lua_setfield(L, -2, "__gc");
+ /* simgrid udata mt */
+ lua_setmetatable(L, -2);
+ /* simgrid udata */
+ lua_setfield(L, -2, "__simgrid_loaded");
+ /* simgrid */
lua_pop(L, 1);
/* -- */
+}
+
+/**
+ * \brief Creates the simgrid module and make it available to Lua.
+ * \param L a Lua world
+ */
+static void register_c_functions(lua_State *L)
+{
+ register_core_functions(L);
register_task_functions(L);
register_host_functions(L);
register_platf_functions(L);