- * \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
+ * \brief Registers the task functions into the table simgrid.task.
+ *
+ * Also initialize the metatable of the task userdata type.
+ *
+ * \param L a lua state
+ */
+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 */
+
+ /* create the metatable for tasks, add it to the Lua registry */
+ luaL_newmetatable(L, TASK_MODULE_NAME);
+ /* simgrid.task mt */
+ /* fill the metatable */
+ luaL_openlib(L, NULL, task_meta, 0);
+ /* simgrid.task mt */
+ lua_pushvalue(L, -2);
+ /* simgrid.task mt simgrid.task */
+ /* metatable.__index = simgrid.task
+ * we put the task functions inside the task userdata 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 */