1 /* Copyright (c) 2010-2014. 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, bindings, "Lua state management");
18 static void sglua_add_maestro_table(lua_State* L, int index, void* maestro_table_ptr);
19 static void sglua_remove_maestro_table(lua_State* L, int index, void* maestro_table_ptr);
20 static void* sglua_get_maestro_table_ptr(lua_State* L, int index);
21 static void sglua_get_table_by_ptr(lua_State* L, void* table_ptr);
22 static int l_get_from_maestro(lua_State* L);
24 static void sglua_copy_nil(lua_State* src, lua_State* dst);
25 static void sglua_copy_number(lua_State* src, lua_State* dst);
26 static void sglua_copy_boolean(lua_State* src, lua_State* dst);
27 static void sglua_copy_string(lua_State* src, lua_State* dst);
28 static void sglua_copy_table(lua_State* src, lua_State* dst);
29 static void sglua_copy_function(lua_State* src, lua_State* dst);
30 static void sglua_copy_lightuserdata(lua_State* src, lua_State* dst);
31 static void sglua_copy_userdata(lua_State* src, lua_State* dst);
32 static void sglua_copy_thread(lua_State* src, lua_State* dst);
35 * @brief Adds a reference to a maestro table to the list of known maestro
38 * TODO identify maestro's tables with my own IDs instead of pointers
41 * @param L a state (can be maestro itself)
42 * @param index index of the copy of the maestro table in the stack of L
43 * @param maestro_table_ptr pointer to the original table in maestro's world
45 static void sglua_add_maestro_table(lua_State* L, int index, void* maestro_table_ptr) {
47 /* we will set both [ptr] -> table and [table] -> ptr */
50 lua_pushvalue(L, index);
52 lua_pushstring(L, "simgrid.maestro_tables");
53 /* ... table "simgrid.maestro_tables" */
54 lua_rawget(L, LUA_REGISTRYINDEX);
55 /* ... table maestrotbs */
57 /* ... table maestrotbs table */
58 lua_pushlightuserdata(L, maestro_table_ptr);
59 /* ... table maestrotbs table tableptr */
61 /* ... table maestrotbs table tableptr tableptr */
63 /* ... table maestrotbs table tableptr tableptr table */
65 /* ... table maestrotbs table tableptr */
67 /* ... table maestrotbs */
73 * @brief Removes a reference to a maestro table to the list of known maestro
76 * @param L a state (can be maestro itself)
77 * @param index index of the copy of the maestro table in the stack of L
78 * @param maestro_table_ptr pointer to the original table in maestro's world
80 static void sglua_remove_maestro_table(lua_State* L, int index, void* maestro_table_ptr) {
82 /* we will unset both [ptr] -> table and [table] -> ptr */
85 lua_pushvalue(L, index);
87 lua_pushstring(L, "simgrid.maestro_tables");
88 /* ... table "simgrid.maestro_tables" */
89 lua_rawget(L, LUA_REGISTRYINDEX);
90 /* ... table maestrotbs */
92 /* ... table maestrotbs table */
94 /* ... table maestrotbs table nil */
95 lua_pushlightuserdata(L, maestro_table_ptr);
96 /* ... table maestrotbs table nil tableptr */
98 /* ... table maestrotbs table nil tableptr nil*/
100 /* ... table maestrotbs table nil */
102 /* ... table maestrotbs */
108 * @brief For a table in the stack of L, returns a pointer that identifies the
109 * same table in in maestro's world.
110 * @param L a Lua state
111 * @param index index of a table in the stack of L
112 * @return a pointer to maestro's copy of this table, or NULL if this table
113 * did not come from maestro
115 static void* sglua_get_maestro_table_ptr(lua_State* L, int index) {
117 void* maestro_table_ptr = NULL;
119 lua_pushvalue(L, index);
121 lua_pushstring(L, "simgrid.maestro_tables");
122 /* ... table "simgrid.maestro_tables" */
123 lua_rawget(L, LUA_REGISTRYINDEX);
124 /* ... table maestrotbs */
125 lua_pushvalue(L, -2);
126 /* ... table maestrotbs table */
128 /* ... table maestrotbs tableptr/nil */
129 if (!lua_isnil(L, -1)) {
130 /* ... table maestrotbs tableptr */
131 maestro_table_ptr = (void*) lua_topointer(L, -1);
136 return maestro_table_ptr;
140 * @brief Pushes a table knowing a pointer to maestro's copy of this table.
142 * Pushes nil if L does not know this table in maestro.
144 * @param L a Lua state
145 * @param maestro_table_ptr pointer that identifies a table in maestro's world
147 static void sglua_get_table_by_ptr(lua_State* L, void* maestro_table_ptr) {
150 lua_pushstring(L, "simgrid.maestro_tables");
151 /* ... "simgrid.maestro_tables" */
152 lua_rawget(L, LUA_REGISTRYINDEX);
154 lua_pushlightuserdata(L, maestro_table_ptr);
155 /* ... maestrotbs tableptr */
157 /* ... maestrotbs table/nil */
163 * @brief Pops a value from the stack of a source state and pushes it on the
164 * stack of another state.
165 * If the value is a table, its content is copied recursively.
167 * This function is similar to lua_xmove() but it allows to move a value
168 * between two different global states.
170 * @param src the source state (not necessarily maestro)
171 * @param dst the destination state
173 void sglua_move_value(lua_State* src, lua_State* dst) {
175 sglua_copy_value(src, dst);
180 * @brief Pushes onto the stack a copy of the value on top another stack.
181 * If the value is a table, its content is copied recursively.
183 * This function allows to move a value between two different global states.
185 * @param src the source state (not necessarily maestro)
186 * @param dst the destination state
188 void sglua_copy_value(lua_State* src, lua_State* dst) {
190 luaL_checkany(src, -1); /* check the value to copy */
192 int indent = (lua_gettop(dst) - 1) * 6;
193 XBT_DEBUG("%sCopying data %s", sglua_get_spaces(indent), sglua_tostring(src, -1));
195 sglua_stack_dump("src before copying a value (should be ... value): ", src);
196 sglua_stack_dump("dst before copying a value (should be ...): ", dst);
198 switch (lua_type(src, -1)) {
201 sglua_copy_nil(src, dst);
205 sglua_copy_number(src, dst);
209 sglua_copy_boolean(src, dst);
213 sglua_copy_string(src, dst);
217 sglua_copy_function(src, dst);
221 sglua_copy_table(src, dst);
224 case LUA_TLIGHTUSERDATA:
225 sglua_copy_lightuserdata(src, dst);
229 sglua_copy_userdata(src, dst);
233 sglua_copy_thread(src, dst);
237 XBT_ERROR("This index is acceptable but non-valid");
241 XBT_DEBUG("%sData copied", sglua_get_spaces(indent));
243 sglua_stack_dump("src after copying a value (should be ... value): ", src);
244 sglua_stack_dump("dst after copying a value (should be ... value): ", dst);
248 * @brief Copies the nil value on the top of src to the top of dst.
249 * @param src source state
250 * @param dst destination state
252 static void sglua_copy_nil(lua_State* src, lua_State* dst) {
257 * @brief Copies the number value on the top of src to the top of dst.
258 * @param src source state
259 * @param dst destination state
261 static void sglua_copy_number(lua_State* src, lua_State* dst) {
262 lua_Number n = lua_tonumber(src, -1);
263 if ( ((lua_Integer) n) == n) {
264 lua_pushinteger(dst, lua_tointeger(src, -1));
267 lua_pushnumber(dst, lua_tonumber(src, -1));
271 * @brief Copies the boolean value on the top of src to the top of dst.
272 * @param src source state
273 * @param dst destination state
275 static void sglua_copy_boolean(lua_State* src, lua_State* dst) {
276 lua_pushboolean(dst, lua_toboolean(src, -1));
280 * @brief Copies the string value on the top of src to the top of dst.
281 * @param src source state
282 * @param dst destination state
284 static void sglua_copy_string(lua_State* src, lua_State* dst) {
286 /* no worries about memory: lua_pushstring makes a copy */
287 lua_pushstring(dst, lua_tostring(src, -1));
291 * @brief Copies the table value on top of src to the top of dst.
293 * A deep copy of the table is made. If the table has a metatable, the
294 * metatable is also copied.
295 * If the table comes from maestro and is already known by the destination
296 * state, it is not copied again.
298 * @param src source state
299 * @param dst destination state
301 static void sglua_copy_table(lua_State* src, lua_State* dst) {
305 int indent = lua_gettop(dst) * 6 + 2;
307 /* get from maestro the pointer that identifies this table */
308 void* table_ptr = sglua_get_maestro_table_ptr(src, -1);
309 int known_by_maestro = (table_ptr != NULL);
311 if (!known_by_maestro) {
312 /* the table didn't come from maestro: nevermind, use the pointer of src */
313 table_ptr = (void*) lua_topointer(src, -1);
314 XBT_DEBUG("%sMaestro does not know this table",
315 sglua_get_spaces(indent));
318 if (sglua_is_maestro(src)) {
319 /* register the table in maestro itself */
320 XBT_DEBUG("%sKeeping track of this table in maestro itself",
321 sglua_get_spaces(indent));
322 sglua_add_maestro_table(src, -1, table_ptr);
323 known_by_maestro = 1;
324 xbt_assert(sglua_get_maestro_table_ptr(src, -1) == table_ptr);
327 /* to avoid infinite recursion, see if this table is already known by dst */
328 sglua_get_table_by_ptr(dst, table_ptr);
329 /* dst: ... table/nil */
330 if (!lua_isnil(dst, -1)) {
331 XBT_DEBUG("%sNothing to do: table already known (%p)",
332 sglua_get_spaces(indent), table_ptr);
336 XBT_DEBUG("%sFirst visit of this table (%p)", sglua_get_spaces(indent),
342 /* first visit: create the new table in dst */
346 /* mark the table as known right now to avoid infinite recursion */
347 sglua_add_maestro_table(dst, -1, table_ptr);
348 /* we may have added a table with a non-maestro pointer, but if it was the
349 * case, we will remove it later */
350 XBT_DEBUG("%sTable marked as known", sglua_get_spaces(indent));
351 xbt_assert(sglua_get_maestro_table_ptr(dst, -1) == table_ptr);
353 sglua_stack_dump("dst after marking the table as known (should be ... table): ", dst);
355 /* copy the metatable if any */
356 int has_meta_table = lua_getmetatable(src, -1);
357 /* src: ... table mt? */
358 if (has_meta_table) {
359 XBT_DEBUG("%sCopying the metatable", sglua_get_spaces(indent));
360 /* src: ... table mt */
361 sglua_copy_table(src, dst);
362 /* dst: ... table mt */
365 lua_setmetatable(dst, -2);
370 XBT_DEBUG("%sNo metatable", sglua_get_spaces(indent));
373 sglua_stack_dump("src before traversing the table (should be ... table): ", src);
374 sglua_stack_dump("dst before traversing the table (should be ... table): ", dst);
376 /* traverse the table of src and copy each element */
378 /* src: ... table nil */
379 while (lua_next(src, -2) != 0) {
380 /* src: ... table key value */
382 XBT_DEBUG("%sCopying table element %s", sglua_get_spaces(indent),
383 sglua_keyvalue_tostring(src, -2, -1));
385 sglua_stack_dump("src before copying table element (should be ... table key value): ", src);
386 sglua_stack_dump("dst before copying table element (should be ... table): ", dst);
389 lua_pushvalue(src, -2);
390 /* src: ... table key value key */
392 XBT_DEBUG("%sCopying the key part of the table element",
393 sglua_get_spaces(indent));
394 sglua_move_value(src, dst);
395 /* src: ... table key value
396 dst: ... table key */
397 XBT_DEBUG("%sCopied the key part of the table element",
398 sglua_get_spaces(indent));
401 XBT_DEBUG("%sCopying the value part of the table element",
402 sglua_get_spaces(indent));
403 sglua_move_value(src, dst);
404 /* src: ... table key
405 dst: ... table key value */
406 XBT_DEBUG("%sCopied the value part of the table element",
407 sglua_get_spaces(indent));
410 /* set the table element */
411 lua_settable(dst, -3);
414 /* the key stays on top of src for next iteration */
415 sglua_stack_dump("src before next iteration (should be ... table key): ", src);
416 sglua_stack_dump("dst before next iteration (should be ... table): ", dst);
418 XBT_DEBUG("%sTable element copied", sglua_get_spaces(indent));
420 XBT_DEBUG("%sFinished traversing the table", sglua_get_spaces(indent));
423 if (!known_by_maestro) {
424 /* actually,it was not a maestro table: forget the pointer */
425 sglua_remove_maestro_table(dst, -1, table_ptr);
430 * @brief Copies the function on the top of src to the top of dst.
432 * It can be a Lua function or a C function.
434 * @param src source state
435 * @param dst destination state
437 static void sglua_copy_function(lua_State* src, lua_State* dst) {
439 if (lua_iscfunction(src, -1)) {
440 /* it's a C function */
442 XBT_DEBUG("It's a C function");
443 sglua_stack_dump("src before copying upvalues: ", src);
445 /* get the function pointer */
446 int function_index = lua_gettop(src);
447 lua_CFunction f = lua_tocfunction(src, function_index);
449 /* copy the upvalues */
451 const char* upvalue_name = NULL;
454 upvalue_name = lua_getupvalue(src, function_index, i);
456 if (upvalue_name != NULL) {
457 XBT_DEBUG("Upvalue %s", upvalue_name);
458 sglua_move_value(src, dst);
460 } while (upvalue_name != NULL);
462 sglua_stack_dump("src before copying pointer: ", src);
464 /* set the function */
465 lua_pushcclosure(dst, f, i - 1);
466 XBT_DEBUG("Function pointer copied");
469 /* it's a Lua function: dump it from src */
471 s_sglua_buffer_t buffer;
472 buffer.capacity = 128; /* an empty function uses 77 bytes */
474 buffer.data = xbt_new(char, buffer.capacity);
476 /* copy the binary chunk from src into a buffer
477 * c.heinrich: Added parameter TRUE for Lua 5.3 - this strips all debug
478 * information from the function.
480 // Was before merge: XBT_GNUC_UNUSED and was replaced with XBT_ATTRIB_UNUSED
481 XBT_ATTRIB_UNUSED int error = lua_dump(src, sglua_memory_writer, &buffer, TRUE);
482 xbt_assert(!error, "Failed to dump the function from the source state: error %d",
484 XBT_DEBUG("Fonction dumped: %zu bytes", buffer.size);
487 fwrite(buffer.data, buffer.size, buffer.size, stderr);
488 fprintf(stderr, "\n");
491 /* load the chunk into dst */
492 error = luaL_loadbuffer(dst, buffer.data, buffer.size, "(dumped function)");
493 xbt_assert(!error, "Failed to load the function into the destination state: %s",
494 lua_tostring(dst, -1));
499 * @brief Copies the light userdata on the top of src to the top of dst.
500 * @param src source state
501 * @param dst destination state
503 static void sglua_copy_lightuserdata(lua_State* src, lua_State* dst) {
504 lua_pushlightuserdata(dst, lua_touserdata(src, -1));
508 * @brief Copies the full userdata on the top of src to the top of dst.
510 * If the userdata has a metatable, the metatable is also copied.
512 * @param src source state
513 * @param dst destination state
515 static void sglua_copy_userdata(lua_State* src, lua_State* dst) {
517 int indent = lua_gettop(dst) * 6 + 2;
522 size_t size = lua_rawlen(src, -1);
523 void* src_block = lua_touserdata(src, -1);
524 void* dst_block = lua_newuserdata(dst, size);
526 memcpy(dst_block, src_block, size);
528 /* copy the metatable if any */
529 int has_meta_table = lua_getmetatable(src, -1);
530 /* src: ... udata mt? */
531 if (has_meta_table) {
532 XBT_DEBUG("%sCopying metatable of userdata (%p)",
533 sglua_get_spaces(indent), lua_topointer(src, -1));
534 /* src: ... udata mt */
535 sglua_copy_table(src, dst);
540 lua_setmetatable(dst, -2);
543 XBT_DEBUG("%sMetatable of userdata copied", sglua_get_spaces(indent));
546 XBT_DEBUG("%sNo metatable for this userdata",
547 sglua_get_spaces(indent));
553 * @brief This operation is not supported (yet?) so it just pushes nil.
555 * @param src source state
556 * @param dst destination state
558 static void sglua_copy_thread(lua_State* src, lua_State* dst) {
560 XBT_WARN("Copying a thread from another state is not implemented (yet?).");
565 * @brief Copies a global value or a registry value from the maestro state.
567 * The state L must have been created by sglua_clone_maestro_state().
568 * This function is meant to be an __index metamethod.
569 * Consequently, it assumes that the stack has two elements:
570 * a table (either the environment or the registry of L) and the string key of
571 * a value that does not exist yet in this table. It copies the corresponding
572 * value from maestro and pushes it on the stack of L.
573 * If the value does not exist in maestro state either, nil is pushed.
575 * TODO: make this function thread safe. If the simulation runs in parallel,
576 * several simulated processes may trigger this __index metamethod at the same
577 * time and get globals from maestro.
579 * @param L the current state
580 * @return number of return values pushed (always 1)
582 static int l_get_from_maestro(lua_State *L) {
584 /* check the arguments */
585 luaL_checktype(L, 1, LUA_TTABLE);
586 const char* key = luaL_checkstring(L, 2);
588 XBT_DEBUG("__index of '%s' begins", key);
591 lua_State* maestro = sglua_get_maestro(); /* maestro: */
593 /* want a global or a registry value?
594 get the value from maestro */
595 if (lua_compare(L, 1, LUA_REGISTRYINDEX, LUA_OPEQ)) {
597 lua_getfield(maestro, LUA_REGISTRYINDEX, key); /* maestro: ... value */
598 XBT_DEBUG("Will get the value from the registry of maestro");
600 else { /* case: global */
601 lua_getglobal(maestro, key); /* maestro: ... value */
602 XBT_DEBUG("Will get the value from the globals of maestro");
608 /* push the value onto the stack of L */
609 sglua_move_value(maestro, L);
611 L: table key value */
613 /* prepare the return value of __index */
614 lua_pushvalue(L, -1);
615 /* L: table key value value */
617 /* L: value table key value */
619 /* save the copied value in the table for subsequent accesses */
625 XBT_DEBUG("__index of '%s' returns %s", key, sglua_tostring(L, -1));
631 * @brief Creates a new Lua state and get its environment from the maestro
634 * The state created is independent from maestro and has its own copies of
635 * global and registry values.
636 * However, the global and registry values are not copied right now from
637 * the original state; they are copied only the first time they are accessed.
638 * This behavior saves time and memory, and is okay for Simgrid's needs.
640 * TODO: if the simulation runs in parallel, copy everything right now?
642 * @return the state created
644 lua_State* sglua_clone_maestro(void) {
646 /* create the new state */
647 lua_State *L = luaL_newstate();
649 /* set its environment and its registry:
650 * - create a table newenv
651 * - create a metatable mt
652 * - set mt.__index = a function that copies the global from the father state
653 * - set mt as the metatable of the registry
654 * - set mt as the metatable of newenv
655 * - set newenv as the environment of the new state
657 lua_pushthread(L); /* thread */
658 lua_newtable(L); /* thread newenv */
659 lua_newtable(L); /* thread newenv mt */
660 lua_pushvalue(L, LUA_REGISTRYINDEX); /* thread newenv mt reg */
661 lua_pushcfunction(L, l_get_from_maestro); /* thread newenv mt reg f */
662 lua_setfield(L, -3, "__index"); /* thread newenv mt reg */
663 lua_pushvalue(L, -2); /* thread newenv mt reg mt */
664 lua_setmetatable(L, -2); /* thread newenv mt reg */
665 lua_pop(L, 1); /* thread newenv mt */
666 lua_setmetatable(L, -2); /* thread newenv */
667 lua_pushvalue(L, LUA_REGISTRYINDEX); /* thread newenv reg */
668 lua_insert(L, -2); /* thread reg newenv */
669 lua_seti(L, -2, LUA_RIDX_GLOBALS); /* thread reg */
670 lua_pop(L, 2); /* -- */
672 /* create the table of known tables from maestro */
673 lua_pushstring(L, "simgrid.maestro_tables");
674 /* "simgrid.maestro_tables" */
675 lua_newtable(L); /* "simgrid.maestro_tables" maestrotbs */
676 lua_rawset(L, LUA_REGISTRYINDEX);
679 /* opening the standard libs is not necessary as they are
680 * inherited like any global values */
681 /* luaL_openlibs(L); */
683 XBT_DEBUG("New state created");