1 /* Copyright (c) 2010. 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 state management */
9 #include "lua_state_cloner.h"
10 #include "lua_utils.h"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_state_cloner, lua, "Lua state management");
18 static void sglua_add_maestro_table(lua_State* L, int index, void* maestro_table_ptr);
19 static void* sglua_get_maestro_table_ptr(lua_State* L, int index);
20 static void sglua_get_table_by_ptr(lua_State* L, void* table_ptr);
21 static int l_get_from_maestro(lua_State* L);
23 static void sglua_copy_nil(lua_State* src, lua_State* dst);
24 static void sglua_copy_number(lua_State* src, lua_State* dst);
25 static void sglua_copy_boolean(lua_State* src, lua_State* dst);
26 static void sglua_copy_string(lua_State* src, lua_State* dst);
27 static void sglua_copy_table(lua_State* src, lua_State* dst);
28 static void sglua_copy_function(lua_State* src, lua_State* dst);
29 static void sglua_copy_lightuserdata(lua_State* src, lua_State* dst);
30 static void sglua_copy_userdata(lua_State* src, lua_State* dst);
31 static void sglua_copy_thread(lua_State* src, lua_State* dst);
34 * @brief Returns the father of a state, i.e. the state that created it.
35 * @param L a Lua state
36 * @return its father, or NULL if the state was not created by sglua_clone_state()
38 static lua_State* sglua_get_father(lua_State* L) {
41 lua_pushstring(L, "simgrid.father");
42 /* ... "simgrid.father" */
43 lua_rawget(L, LUA_REGISTRYINDEX);
45 lua_State* father = lua_touserdata(L, -1);
52 * @brief Adds a reference to a maestro table to the list of known maestro
55 * TODO identify maestro's tables with my own IDs instead of pointers
58 * @param L a state (can be maestro itself)
59 * @param index index of the copy of the maestro table in the stack of L
60 * @param maestro_table_ptr pointer to the original table in maestro's world
62 void sglua_add_maestro_table(lua_State* L, int index, void* maestro_table_ptr) {
64 /* we will set both [ptr] -> table and [table] -> ptr */
67 lua_pushvalue(L, index);
69 lua_getfield(L, LUA_REGISTRYINDEX, "simgrid.maestro_tables");
70 /* ... table maestrotbs */
72 /* ... table maestrotbs table */
73 lua_pushlightuserdata(L, maestro_table_ptr);
74 /* ... table maestrotbs table tableptr */
76 /* ... table maestrotbs table tableptr tableptr */
78 /* ... table maestrotbs table tableptr tableptr table */
80 /* ... table maestrotbs table tableptr */
82 /* ... table maestrotbs */
88 * @brief For a table in the stack of L, returns a pointer that identifies the
89 * same table in in maestro's world.
90 * @param L a Lua state
91 * @param index index of a table in the stack of L
92 * @return a pointer to maestro's copy of this table, or NULL if this table
93 * did not come from maestro
95 static void* sglua_get_maestro_table_ptr(lua_State* L, int index) {
97 void* maestro_table_ptr = NULL;
99 lua_pushstring(L, "simgrid.maestro_tables");
100 /* ... "simgrid.maestro_tables" */
101 lua_rawget(L, LUA_REGISTRYINDEX);
103 lua_pushvalue(L, index);
104 /* ... maestrotbs table */
106 /* ... maestrotbs tableptr/nil */
107 if (!lua_isnil(L, -1)) {
108 /* ... maestrotbs tableptr */
109 maestro_table_ptr = (void*) lua_topointer(L, -1);
114 return maestro_table_ptr;
118 * @brief Pushes a table knowing a pointer to maestro's copy of this table.
120 * Pushes nil if L does not know this table in maestro.
122 * @param L a Lua state
123 * @param maestro_table_ptr pointer that identifies a table in maestro's world
125 static void sglua_get_table_by_ptr(lua_State* L, void* maestro_table_ptr) {
128 lua_pushstring(L, "simgrid.maestro_tables");
129 /* ... "simgrid.maestro_tables" */
130 lua_rawget(L, LUA_REGISTRYINDEX);
132 lua_pushlightuserdata(L, maestro_table_ptr);
133 /* ... maestrotbs tableptr */
135 /* ... maestrotbs table/nil */
141 * @brief Pops a value from the stack of a source state and pushes it on the
142 * stack of another state.
144 * If the value is a table, its content is copied recursively.
146 * TODO: add support of closures
148 * @param src the source state (not necessarily maestro)
149 * @param dst the destination state
151 void sglua_move_value(lua_State* src, lua_State *dst) {
153 luaL_checkany(src, -1); /* check the value to copy */
155 int indent = (lua_gettop(dst) - 1) * 6;
156 XBT_DEBUG("%sCopying data %s", sglua_get_spaces(indent), sglua_tostring(src, -1));
158 sglua_stack_dump("src before copying a value (should be ... value): ", src);
159 sglua_stack_dump("dst before copying a value (should be ...): ", dst);
161 switch (lua_type(src, -1)) {
164 sglua_copy_nil(src, dst);
168 sglua_copy_number(src, dst);
172 sglua_copy_boolean(src, dst);
176 sglua_copy_string(src, dst);
180 sglua_copy_function(src, dst);
184 sglua_copy_table(src, dst);
187 case LUA_TLIGHTUSERDATA:
188 sglua_copy_lightuserdata(src, dst);
192 sglua_copy_userdata(src, dst);
196 sglua_copy_thread(src, dst);
200 /* the value has been copied to dst: remove it from src */
204 XBT_DEBUG("%sData copied", sglua_get_spaces(indent));
206 sglua_stack_dump("src after copying a value (should be ...): ", src);
207 sglua_stack_dump("dst after copying a value (should be ... value): ", dst);
211 * @brief Copies the nil value on the top of src to the top of dst.
212 * @param src source state
213 * @param dst destination state
215 static void sglua_copy_nil(lua_State* src, lua_State* dst) {
220 * @brief Copies the number value on the top of src to the top of dst.
221 * @param src source state
222 * @param dst destination state
224 static void sglua_copy_number(lua_State* src, lua_State* dst) {
225 lua_pushnumber(dst, lua_tonumber(src, -1));
229 * @brief Copies the boolean value on the top of src to the top of dst.
230 * @param src source state
231 * @param dst destination state
233 static void sglua_copy_boolean(lua_State* src, lua_State* dst) {
234 lua_pushboolean(dst, lua_toboolean(src, -1));
238 * @brief Copies the string value on the top of src to the top of dst.
239 * @param src source state
240 * @param dst destination state
242 static void sglua_copy_string(lua_State* src, lua_State* dst) {
244 /* no worries about memory: lua_pushstring makes a copy */
245 lua_pushstring(dst, lua_tostring(src, -1));
249 * @brief Copies the table value on the top of src to the top of dst.
251 * A deep copy of the table is made. If the table has a metatable, the
252 * metatable is also copied.
253 * If the table is already known by the destination state, it is not copied
256 * @param src source state
257 * @param dst destination state
259 static void sglua_copy_table(lua_State* src, lua_State* dst) {
263 int indent = lua_gettop(dst) * 6 + 2;
265 /* get from maestro the pointer that identifies this table */
266 void* table_ptr = sglua_get_maestro_table_ptr(src, -1);
267 if (table_ptr == NULL) {
268 /* the table didn't come from maestro: nevermind, use the pointer of src */
269 table_ptr = (void*) lua_topointer(src, -1);
271 if (!sglua_is_maestro(src)) {
272 XBT_DEBUG("Using a non-maestro table pointer");
275 else if (sglua_is_maestro(src)) {
276 /* register the table in maestro itself */
277 sglua_add_maestro_table(src, -1, table_ptr);
280 /* to avoid infinite recursion, see if this table is already known by dst */
281 sglua_get_table_by_ptr(dst, table_ptr);
282 /* dst: ... table/nil */
283 if (!lua_isnil(dst, -1)) {
284 XBT_DEBUG("%sNothing to do: table already known (%p)",
285 sglua_get_spaces(indent), table_ptr);
289 XBT_DEBUG("%sFirst visit of this table (%p)", sglua_get_spaces(indent),
295 /* first visit: create the new table in dst */
299 /* mark the table as known right now to avoid infinite recursion */
300 sglua_add_maestro_table(dst, -1, table_ptr);
301 /* FIXME: we may have added a table with a non-maestro pointer, is this a
303 XBT_DEBUG("%sTable marked as known", sglua_get_spaces(indent));
305 sglua_stack_dump("dst after marking the table as known (should be ... table): ", dst);
307 /* copy the metatable if any */
308 int has_meta_table = lua_getmetatable(src, -1);
309 /* src: ... table mt? */
310 if (has_meta_table) {
311 XBT_DEBUG("%sCopying the metatable", sglua_get_spaces(indent));
312 /* src: ... table mt */
313 sglua_copy_table(src, dst);
314 /* dst: ... table mt */
317 lua_setmetatable(dst, -2);
322 XBT_DEBUG("%sNo metatable", sglua_get_spaces(indent));
325 sglua_stack_dump("src before traversing the table (should be ... table): ", src);
326 sglua_stack_dump("dst before traversing the table (should be ... table): ", dst);
328 /* traverse the table of src and copy each element */
330 /* src: ... table nil */
331 while (lua_next(src, -2) != 0) {
332 /* src: ... table key value */
334 XBT_DEBUG("%sCopying table element %s", sglua_get_spaces(indent),
335 sglua_keyvalue_tostring(src, -2, -1));
337 sglua_stack_dump("src before copying table element (should be ... table key value): ", src);
338 sglua_stack_dump("dst before copying table element (should be ... table): ", dst);
341 lua_pushvalue(src, -2);
342 /* src: ... table key value key */
344 XBT_DEBUG("%sCopying the key part of the table element",
345 sglua_get_spaces(indent));
346 sglua_move_value(src, dst);
347 /* src: ... table key value
348 dst: ... table key */
349 XBT_DEBUG("%sCopied the key part of the table element",
350 sglua_get_spaces(indent));
353 XBT_DEBUG("%sCopying the value part of the table element",
354 sglua_get_spaces(indent));
355 sglua_move_value(src, dst);
356 /* src: ... table key
357 dst: ... table key value */
358 XBT_DEBUG("%sCopied the value part of the table element",
359 sglua_get_spaces(indent));
362 /* set the table element */
363 lua_settable(dst, -3);
366 /* the key stays on top of src for next iteration */
367 sglua_stack_dump("src before next iteration (should be ... table key): ", src);
368 sglua_stack_dump("dst before next iteration (should be ... table): ", dst);
370 XBT_DEBUG("%sTable element copied", sglua_get_spaces(indent));
372 XBT_DEBUG("%sFinished traversing the table", sglua_get_spaces(indent));
377 * @brief Copies the function on the top of src to the top of dst.
379 * It can be a Lua function or a C function.
380 * Copying upvalues is not implemented yet (TODO).
382 * @param src source state
383 * @param dst destination state
385 static void sglua_copy_function(lua_State* src, lua_State* dst) {
387 if (lua_iscfunction(src, -1)) {
388 /* it's a C function: just copy the pointer */
389 lua_CFunction f = lua_tocfunction(src, -1);
390 lua_pushcfunction(dst, f);
393 /* it's a Lua function: dump it from src */
395 s_sglua_buffer_t buffer;
396 buffer.capacity = 64;
398 buffer.data = xbt_new(char, buffer.capacity);
400 /* copy the binary chunk from src into a buffer */
401 int error = lua_dump(src, sglua_memory_writer, &buffer);
402 xbt_assert(!error, "Failed to dump the function from the source state: error %d",
405 /* load the chunk into dst */
406 error = luaL_loadbuffer(dst, buffer.data, buffer.size, "(dumped function)");
407 xbt_assert(!error, "Failed to load the function into the destination state: %s",
408 lua_tostring(dst, -1));
413 * @brief Copies the light userdata on the top of src to the top of dst.
414 * @param src source state
415 * @param dst destination state
417 static void sglua_copy_lightuserdata(lua_State* src, lua_State* dst) {
418 lua_pushlightuserdata(dst, lua_touserdata(src, -1));
422 * @brief Copies the full userdata on the top of src to the top of dst.
424 * If the userdata has a metatable, the metatable is also copied.
426 * @param src source state
427 * @param dst destination state
429 static void sglua_copy_userdata(lua_State* src, lua_State* dst) {
431 int indent = lua_gettop(dst) * 6 + 2;
436 size_t size = lua_objlen(src, -1);
437 void* src_block = lua_touserdata(src, -1);
438 void* dst_block = lua_newuserdata(dst, size);
440 memcpy(dst_block, src_block, size);
442 /* copy the metatable if any */
443 int has_meta_table = lua_getmetatable(src, -1);
444 /* src: ... udata mt? */
445 if (has_meta_table) {
446 XBT_DEBUG("%sCopying metatable of userdata (%p)",
447 sglua_get_spaces(indent), lua_topointer(src, -1));
448 /* src: ... udata mt */
449 sglua_copy_table(src, dst);
454 lua_setmetatable(dst, -2);
457 XBT_DEBUG("%sMetatable of userdata copied", sglua_get_spaces(indent));
460 XBT_DEBUG("%sNo metatable for this userdata",
461 sglua_get_spaces(indent));
467 * @brief This operation is not supported (yet?) so it just pushes nil.
469 * @param src source state
470 * @param dst destination state
472 static void sglua_copy_thread(lua_State* src, lua_State* dst) {
474 XBT_WARN("Copying a thread from another state is not implemented (yet?).");
479 * @brief Copies a global value or a registry value from the maestro state.
481 * The state L must have been created by sglua_clone_maestro_state().
482 * This function is meant to be an __index metamethod.
483 * Consequently, it assumes that the stack has two elements:
484 * a table (either the environment or the registry of L) and the string key of
485 * a value that does not exist yet in this table. It copies the corresponding
486 * value from maestro and pushes it on the stack of L.
487 * If the value does not exist in maestro state either, nil is pushed.
489 * TODO: make this function thread safe. If the simulation runs in parallel,
490 * several simulated processes may trigger this __index metamethod at the same
491 * time and get globals from maestro.
493 * @param L the current state
494 * @return number of return values pushed (always 1)
496 static int l_get_from_maestro(lua_State *L) {
498 /* check the arguments */
499 luaL_checktype(L, 1, LUA_TTABLE);
500 const char* key = luaL_checkstring(L, 2);
502 XBT_DEBUG("__index of '%s' begins", key);
504 /* want a global or a registry value? */
506 if (lua_equal(L, 1, LUA_REGISTRYINDEX)) {
508 pseudo_index = LUA_REGISTRYINDEX;
509 XBT_DEBUG("Will get the value from the registry of maestro");
513 pseudo_index = LUA_GLOBALSINDEX;
514 XBT_DEBUG("Will get the value from the globals of maestro");
518 lua_State* maestro = sglua_get_maestro();
522 /* get the value from maestro */
523 lua_getfield(maestro, pseudo_index, key);
524 /* maestro: ... value */
526 /* push the value onto the stack of L */
527 sglua_move_value(maestro, L);
529 L: table key value */
531 /* prepare the return value of __index */
532 lua_pushvalue(L, -1);
533 /* L: table key value value */
535 /* L: value table key value */
537 /* save the copied value in the table for subsequent accesses */
543 XBT_DEBUG("__index of '%s' returns %s", key, sglua_tostring(L, -1));
549 * @brief Creates a new Lua state and get its environment from the maestro
552 * The state created is independent from maestro and has its own copies of
553 * global and registry values.
554 * However, the global and registry values are not copied right now from
555 * the original state; they are copied only the first time they are accessed.
556 * This behavior saves time and memory, and is okay for Simgrid's needs.
558 * TODO: if the simulation runs in parallel, copy everything right now?
560 * @return the state created
562 lua_State* sglua_clone_maestro(void) {
564 /* create the new state */
565 lua_State *L = luaL_newstate();
567 /* set its environment and its registry:
568 * - create a table newenv
569 * - create a metatable mt
570 * - set mt.__index = a function that copies the global from the father state
571 * - set mt as the metatable of the registry
572 * - set mt as the metatable of newenv
573 * - set newenv as the environment of the new state
575 lua_pushthread(L); /* thread */
576 lua_newtable(L); /* thread newenv */
577 lua_newtable(L); /* thread newenv mt */
578 lua_pushvalue(L, LUA_REGISTRYINDEX); /* thread newenv mt reg */
579 lua_pushcfunction(L, l_get_from_maestro); /* thread newenv mt reg f */
580 lua_setfield(L, -3, "__index"); /* thread newenv mt reg */
581 lua_pushvalue(L, -2); /* thread newenv mt reg mt */
582 lua_setmetatable(L, -2); /* thread newenv mt reg */
583 lua_pop(L, 1); /* thread newenv mt */
584 lua_setmetatable(L, -2); /* thread newenv */
585 lua_setfenv(L, -2); /* thread */
586 lua_pop(L, 1); /* -- */
588 /* create the table of known tables from maestro */
589 lua_pushstring(L, "simgrid.maestro_tables");
590 /* "simgrid.maestro_tables" */
591 lua_newtable(L); /* "simgrid.maestro_tables" maestrotbs*/
592 lua_rawset(L, LUA_REGISTRYINDEX);
595 /* open the standard libs (theoretically, this is not necessary as they can
596 * be inherited like any global values, but without a proper support of
597 * closures, iterators like ipairs don't work). */
598 XBT_DEBUG("Metatable of globals and registry set, opening standard libraries now");
601 XBT_DEBUG("New state created");