Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[Lua] Removed lua simulation support
[simgrid.git] / src / bindings / lua / simgrid_lua.c
1 /* Copyright (c) 2010-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 /* SimGrid Lua bindings                                                     */
8
9 #include "lua_private.h"
10 #include "lua_utils.h"
11 #include "xbt.h"
12 #include "simgrid/msg.h"
13 #include "simgrid/simdag.h"
14 #include "surf/surfxml_parse.h"
15 #include <lauxlib.h>
16
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua, bindings, "Lua Bindings");
19
20 static lua_State* sglua_maestro_state;
21
22 int luaopen_simgrid(lua_State *L);
23 static void sglua_register_c_functions(lua_State *L);
24
25 /* ********************************************************************************* */
26 /*                                  simgrid API                                      */
27 /* ********************************************************************************* */
28
29 /**
30  * \brief Prints a log string with debug level.
31  * \param L a Lua state
32  * \return number of values returned to Lua
33  *
34  * - Argument 1 (string): the text to print
35  */
36 static int debug(lua_State* L) {
37
38   const char* str = luaL_checkstring(L, 1);
39   XBT_DEBUG("%s", str);
40   return 0;
41 }
42
43 /**
44  * \brief Prints a log string with info level.
45  * \param L a Lua state
46  * \return number of values returned to Lua
47  *
48  * - Argument 1 (string): the text to print
49  */
50 static int info(lua_State* L) {
51
52   const char* str = luaL_checkstring(L, 1);
53   XBT_INFO("%s", str);
54   return 0;
55 }
56
57 static int error(lua_State* L) {
58
59   const char* str = luaL_checkstring(L, 1);
60   XBT_ERROR("%s", str);
61   return 0;
62 }
63
64 static int critical(lua_State* L) {
65
66   const char* str = luaL_checkstring(L, 1);
67   XBT_CRITICAL("%s", str);
68   return 0;
69 }
70
71 static const luaL_Reg simgrid_functions[] = {
72   {"debug", debug},
73   {"info", info},
74   {"critical", critical},
75   {"error", error},
76   /* short names */
77   /* methods to bypass XML parser */
78   {NULL, NULL}
79 };
80
81 /* ********************************************************************************* */
82 /*                           module management functions                             */
83 /* ********************************************************************************* */
84
85 #define LUA_MAX_ARGS_COUNT 10   /* maximum amount of arguments we can get from lua on command line */
86
87 /**
88  * \brief Opens the simgrid Lua module.
89  *
90  * This function is called automatically by the Lua interpreter when some
91  * Lua code requires the "simgrid" module.
92  *
93  * \param L the Lua state
94  */
95
96 int luaopen_simgrid(lua_State *L)
97 {
98   XBT_DEBUG("luaopen_simgrid *****");
99
100   /* Get the command line arguments from the lua interpreter */
101   char **argv = xbt_malloc(sizeof(char *) * LUA_MAX_ARGS_COUNT);
102   int argc = 1;
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? */
104
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
108    */
109   if (lua_istable(L, -1)) {
110     int done = 0;
111     while (!done) {
112       argc++;
113       lua_pushinteger(L, argc - 2);
114       lua_gettable(L, -2);
115       if (lua_isnil(L, -1)) {
116         done = 1;
117       } else {
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);
124         lua_pop(L, 1);
125         XBT_DEBUG("Got command line argument %s from lua", argv[argc - 1]);
126       }
127     }
128     argv[argc--] = NULL;
129
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
132   }
133   MSG_init(&argc, argv);
134   MSG_process_set_data_cleanup((void_f_pvoid_t) lua_close);
135
136   /* Keep the context mechanism informed of our lua world today */
137   sglua_maestro_state = L;
138
139   /* initialize access to my tables by children Lua states */
140   lua_newtable(L);
141   lua_setfield(L, LUA_REGISTRYINDEX, "simgrid.maestro_tables");
142
143   sglua_register_c_functions(L);
144
145   return 1;
146 }
147
148
149 /**
150  * \brief Makes the core functions available to the Lua world.
151  * \param L a Lua world
152  */
153 static void sglua_register_core_functions(lua_State *L)
154 {
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 */
159 }
160
161 /**
162  * \brief Creates the simgrid module and make it available to Lua.
163  * \param L a Lua world
164  */
165 static void sglua_register_c_functions(lua_State *L)
166 {
167   sglua_register_core_functions(L);
168   sglua_register_host_functions(L);
169   sglua_register_platf_functions(L);
170 }