1 /* Copyright (c) 2010-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 /* SimGrid Lua bindings */
9 #include "lua_private.h"
10 #include "lua_utils.h"
12 #include "simgrid/msg.h"
13 #include "simgrid/simdag.h"
14 #include "surf/surfxml_parse.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua, bindings, "Lua Bindings");
20 static lua_State* sglua_maestro_state;
22 int luaopen_simgrid(lua_State *L);
23 static void sglua_register_c_functions(lua_State *L);
25 /* ********************************************************************************* */
27 /* ********************************************************************************* */
30 * \brief Prints a log string with debug level.
31 * \param L a Lua state
32 * \return number of values returned to Lua
34 * - Argument 1 (string): the text to print
36 static int debug(lua_State* L) {
38 const char* str = luaL_checkstring(L, 1);
44 * \brief Prints a log string with info level.
45 * \param L a Lua state
46 * \return number of values returned to Lua
48 * - Argument 1 (string): the text to print
50 static int info(lua_State* L) {
52 const char* str = luaL_checkstring(L, 1);
57 static int error(lua_State* L) {
59 const char* str = luaL_checkstring(L, 1);
64 static int critical(lua_State* L) {
66 const char* str = luaL_checkstring(L, 1);
67 XBT_CRITICAL("%s", str);
71 static const luaL_Reg simgrid_functions[] = {
74 {"critical", critical},
77 /* methods to bypass XML parser */
81 /* ********************************************************************************* */
82 /* module management functions */
83 /* ********************************************************************************* */
85 #define LUA_MAX_ARGS_COUNT 10 /* maximum amount of arguments we can get from lua on command line */
88 * \brief Opens the simgrid Lua module.
90 * This function is called automatically by the Lua interpreter when some
91 * Lua code requires the "simgrid" module.
93 * \param L the Lua state
96 int luaopen_simgrid(lua_State *L)
98 XBT_DEBUG("luaopen_simgrid *****");
100 /* Get the command line arguments from the lua interpreter */
101 char **argv = xbt_malloc(sizeof(char *) * LUA_MAX_ARGS_COUNT);
103 argv[0] = (char *) "/usr/bin/lua"; /* Lie on the argv[0] so that the stack dumping facilities find the right binary. FIXME: what if lua is not in that location? */
105 lua_getglobal(L, "arg");
106 /* if arg is a null value, it means we use lua only as a script to init platform
107 * else it should be a table and then take arg in consideration
109 if (lua_istable(L, -1)) {
113 lua_pushinteger(L, argc - 2);
115 if (lua_isnil(L, -1)) {
118 xbt_assert(lua_isstring(L, -1),
119 "argv[%d] got from lua is no string", argc - 1);
120 xbt_assert(argc < LUA_MAX_ARGS_COUNT,
121 "Too many arguments, please increase LUA_MAX_ARGS_COUNT in %s before recompiling SimGrid if you insist on having more than %d args on command line",
122 __FILE__, LUA_MAX_ARGS_COUNT - 1);
123 argv[argc - 1] = (char *) luaL_checkstring(L, -1);
125 XBT_DEBUG("Got command line argument %s from lua", argv[argc - 1]);
130 /* Initialize the MSG core */
131 XBT_DEBUG("Still %d arguments on command line", argc); // FIXME: update the lua's arg table to reflect the changes from SimGrid
133 MSG_init(&argc, argv);
134 MSG_process_set_data_cleanup((void_f_pvoid_t) lua_close);
136 /* Keep the context mechanism informed of our lua world today */
137 sglua_maestro_state = L;
139 /* initialize access to my tables by children Lua states */
141 lua_setfield(L, LUA_REGISTRYINDEX, "simgrid.maestro_tables");
143 sglua_register_c_functions(L);
150 * \brief Makes the core functions available to the Lua world.
151 * \param L a Lua world
153 static void sglua_register_core_functions(lua_State *L)
155 /* register the core C functions to lua */
156 luaL_newlib(L, simgrid_functions); /* simgrid */
157 lua_pushvalue(L, -1); /* simgrid simgrid */
158 lua_setglobal(L, "simgrid"); /* simgrid */
162 * \brief Creates the simgrid module and make it available to Lua.
163 * \param L a Lua world
165 static void sglua_register_c_functions(lua_State *L)
167 sglua_register_core_functions(L);
168 sglua_register_host_functions(L);
169 sglua_register_platf_functions(L);