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, bindings, "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 Adds a reference to a maestro table to the list of known maestro
37 * TODO identify maestro's tables with my own IDs instead of pointers
40 * @param L a state (can be maestro itself)
41 * @param index index of the copy of the maestro table in the stack of L
42 * @param maestro_table_ptr pointer to the original table in maestro's world
44 void sglua_add_maestro_table(lua_State* L, int index, void* maestro_table_ptr) {
46 /* we will set both [ptr] -> table and [table] -> ptr */
49 lua_pushvalue(L, index);
51 lua_pushstring(L, "simgrid.maestro_tables");
52 /* ... table "simgrid.maestro_tables" */
53 lua_rawget(L, LUA_REGISTRYINDEX);
54 /* ... table maestrotbs */
56 /* ... table maestrotbs table */
57 lua_pushlightuserdata(L, maestro_table_ptr);
58 /* ... table maestrotbs table tableptr */
60 /* ... table maestrotbs table tableptr tableptr */
62 /* ... table maestrotbs table tableptr tableptr table */
64 /* ... table maestrotbs table tableptr */
66 /* ... table maestrotbs */
72 * @brief For a table in the stack of L, returns a pointer that identifies the
73 * same table in in maestro's world.
74 * @param L a Lua state
75 * @param index index of a table in the stack of L
76 * @return a pointer to maestro's copy of this table, or NULL if this table
77 * did not come from maestro
79 static void* sglua_get_maestro_table_ptr(lua_State* L, int index) {
81 void* maestro_table_ptr = NULL;
83 lua_pushvalue(L, index);
85 lua_pushstring(L, "simgrid.maestro_tables");
86 /* ... table "simgrid.maestro_tables" */
87 lua_rawget(L, LUA_REGISTRYINDEX);
88 /* ... table maestrotbs */
90 /* ... table maestrotbs table */
92 /* ... table maestrotbs tableptr/nil */
93 if (!lua_isnil(L, -1)) {
94 /* ... table maestrotbs tableptr */
95 maestro_table_ptr = (void*) lua_topointer(L, -1);
100 return maestro_table_ptr;
104 * @brief Pushes a table knowing a pointer to maestro's copy of this table.
106 * Pushes nil if L does not know this table in maestro.
108 * @param L a Lua state
109 * @param maestro_table_ptr pointer that identifies a table in maestro's world
111 static void sglua_get_table_by_ptr(lua_State* L, void* maestro_table_ptr) {
114 lua_pushstring(L, "simgrid.maestro_tables");
115 /* ... "simgrid.maestro_tables" */
116 lua_rawget(L, LUA_REGISTRYINDEX);
118 lua_pushlightuserdata(L, maestro_table_ptr);
119 /* ... maestrotbs tableptr */
121 /* ... maestrotbs table/nil */
127 * @brief Pops a value from the stack of a source state and pushes it on the
128 * stack of another state.
129 * If the value is a table, its content is copied recursively.
131 * This function is similar to lua_xmove() but it allows to move a value
132 * between two different global states.
134 * @param src the source state (not necessarily maestro)
135 * @param dst the destination state
137 void sglua_move_value(lua_State* src, lua_State* dst) {
139 sglua_copy_value(src, dst);
144 * @brief Pushes onto the stack a copy of the value on top another stack.
145 * If the value is a table, its content is copied recursively.
147 * This function allows to move a value between two different global states.
149 * @param src the source state (not necessarily maestro)
150 * @param dst the destination state
152 void sglua_copy_value(lua_State* src, lua_State* dst) {
154 luaL_checkany(src, -1); /* check the value to copy */
156 int indent = (lua_gettop(dst) - 1) * 6;
157 XBT_DEBUG("%sCopying data %s", sglua_get_spaces(indent), sglua_tostring(src, -1));
159 sglua_stack_dump("src before copying a value (should be ... value): ", src);
160 sglua_stack_dump("dst before copying a value (should be ...): ", dst);
162 switch (lua_type(src, -1)) {
165 sglua_copy_nil(src, dst);
169 sglua_copy_number(src, dst);
173 sglua_copy_boolean(src, dst);
177 sglua_copy_string(src, dst);
181 sglua_copy_function(src, dst);
185 sglua_copy_table(src, dst);
188 case LUA_TLIGHTUSERDATA:
189 sglua_copy_lightuserdata(src, dst);
193 sglua_copy_userdata(src, dst);
197 sglua_copy_thread(src, dst);
202 XBT_DEBUG("%sData copied", sglua_get_spaces(indent));
204 sglua_stack_dump("src after copying a value (should be ... value): ", src);
205 sglua_stack_dump("dst after copying a value (should be ... value): ", dst);
209 * @brief Copies the nil value on the top of src to the top of dst.
210 * @param src source state
211 * @param dst destination state
213 static void sglua_copy_nil(lua_State* src, lua_State* dst) {
218 * @brief Copies the number value on the top of src to the top of dst.
219 * @param src source state
220 * @param dst destination state
222 static void sglua_copy_number(lua_State* src, lua_State* dst) {
223 lua_pushnumber(dst, lua_tonumber(src, -1));
227 * @brief Copies the boolean value on the top of src to the top of dst.
228 * @param src source state
229 * @param dst destination state
231 static void sglua_copy_boolean(lua_State* src, lua_State* dst) {
232 lua_pushboolean(dst, lua_toboolean(src, -1));
236 * @brief Copies the string value on the top of src to the top of dst.
237 * @param src source state
238 * @param dst destination state
240 static void sglua_copy_string(lua_State* src, lua_State* dst) {
242 /* no worries about memory: lua_pushstring makes a copy */
243 lua_pushstring(dst, lua_tostring(src, -1));
247 * @brief Copies the table value on the top of src to the top of dst.
249 * A deep copy of the table is made. If the table has a metatable, the
250 * metatable is also copied.
251 * If the table is already known by the destination state, it is not copied
254 * @param src source state
255 * @param dst destination state
257 static void sglua_copy_table(lua_State* src, lua_State* dst) {
261 int indent = lua_gettop(dst) * 6 + 2;
263 /* get from maestro the pointer that identifies this table */
264 void* table_ptr = sglua_get_maestro_table_ptr(src, -1);
265 if (table_ptr == NULL) {
266 /* the table didn't come from maestro: nevermind, use the pointer of src */
267 table_ptr = (void*) lua_topointer(src, -1);
269 if (!sglua_is_maestro(src)) {
270 XBT_DEBUG("%sMaestro does not know this table",
271 sglua_get_spaces(indent));
275 if (sglua_is_maestro(src)) {
276 /* register the table in maestro itself */
277 XBT_DEBUG("%sKeeping track of this table in maestro itself",
278 sglua_get_spaces(indent));
279 sglua_add_maestro_table(src, -1, table_ptr);
280 xbt_assert(sglua_get_maestro_table_ptr(src, -1) == table_ptr);
283 /* to avoid infinite recursion, see if this table is already known by dst */
284 sglua_get_table_by_ptr(dst, table_ptr);
285 /* dst: ... table/nil */
286 if (!lua_isnil(dst, -1)) {
287 XBT_DEBUG("%sNothing to do: table already known (%p)",
288 sglua_get_spaces(indent), table_ptr);
292 XBT_DEBUG("%sFirst visit of this table (%p)", sglua_get_spaces(indent),
298 /* first visit: create the new table in dst */
302 /* mark the table as known right now to avoid infinite recursion */
303 sglua_add_maestro_table(dst, -1, table_ptr);
304 /* FIXME: we may have added a table with a non-maestro pointer, is this a
306 XBT_DEBUG("%sTable marked as known", sglua_get_spaces(indent));
307 xbt_assert(sglua_get_maestro_table_ptr(dst, -1) == table_ptr);
309 sglua_stack_dump("dst after marking the table as known (should be ... table): ", dst);
311 /* copy the metatable if any */
312 int has_meta_table = lua_getmetatable(src, -1);
313 /* src: ... table mt? */
314 if (has_meta_table) {
315 XBT_DEBUG("%sCopying the metatable", sglua_get_spaces(indent));
316 /* src: ... table mt */
317 sglua_copy_table(src, dst);
318 /* dst: ... table mt */
321 lua_setmetatable(dst, -2);
326 XBT_DEBUG("%sNo metatable", sglua_get_spaces(indent));
329 sglua_stack_dump("src before traversing the table (should be ... table): ", src);
330 sglua_stack_dump("dst before traversing the table (should be ... table): ", dst);
332 /* traverse the table of src and copy each element */
334 /* src: ... table nil */
335 while (lua_next(src, -2) != 0) {
336 /* src: ... table key value */
338 XBT_DEBUG("%sCopying table element %s", sglua_get_spaces(indent),
339 sglua_keyvalue_tostring(src, -2, -1));
341 sglua_stack_dump("src before copying table element (should be ... table key value): ", src);
342 sglua_stack_dump("dst before copying table element (should be ... table): ", dst);
345 lua_pushvalue(src, -2);
346 /* src: ... table key value key */
348 XBT_DEBUG("%sCopying the key part of the table element",
349 sglua_get_spaces(indent));
350 sglua_move_value(src, dst);
351 /* src: ... table key value
352 dst: ... table key */
353 XBT_DEBUG("%sCopied the key part of the table element",
354 sglua_get_spaces(indent));
357 XBT_DEBUG("%sCopying the value part of the table element",
358 sglua_get_spaces(indent));
359 sglua_move_value(src, dst);
360 /* src: ... table key
361 dst: ... table key value */
362 XBT_DEBUG("%sCopied the value part of the table element",
363 sglua_get_spaces(indent));
366 /* set the table element */
367 lua_settable(dst, -3);
370 /* the key stays on top of src for next iteration */
371 sglua_stack_dump("src before next iteration (should be ... table key): ", src);
372 sglua_stack_dump("dst before next iteration (should be ... table): ", dst);
374 XBT_DEBUG("%sTable element copied", sglua_get_spaces(indent));
376 XBT_DEBUG("%sFinished traversing the table", sglua_get_spaces(indent));
381 * @brief Copies the function on the top of src to the top of dst.
383 * It can be a Lua function or a C function.
384 * Copying upvalues is not implemented yet.
386 * @param src source state
387 * @param dst destination state
389 static void sglua_copy_function(lua_State* src, lua_State* dst) {
391 if (lua_iscfunction(src, -1)) {
392 /* it's a C function */
394 XBT_DEBUG("It's a C function");
395 sglua_stack_dump("src before copying upvalues: ", src);
397 /* get the function pointer */
398 int function_index = lua_gettop(src);
399 lua_CFunction f = lua_tocfunction(src, function_index);
401 /* copy the upvalues */
403 const char* upvalue_name = NULL;
406 upvalue_name = lua_getupvalue(src, function_index, i);
408 if (upvalue_name != NULL) {
409 XBT_DEBUG("Upvalue %s", upvalue_name);
410 sglua_move_value(src, dst);
412 } while (upvalue_name != NULL);
414 sglua_stack_dump("src before copying pointer: ", src);
416 /* set the function */
417 lua_pushcclosure(dst, f, i - 1);
418 XBT_DEBUG("Function pointer copied");
421 /* it's a Lua function: dump it from src */
423 s_sglua_buffer_t buffer;
424 buffer.capacity = 128; /* an empty function uses 77 bytes */
426 buffer.data = xbt_new(char, buffer.capacity);
428 /* copy the binary chunk from src into a buffer */
429 _XBT_GNUC_UNUSED int error = lua_dump(src, sglua_memory_writer, &buffer);
430 xbt_assert(!error, "Failed to dump the function from the source state: error %d",
432 XBT_DEBUG("Fonction dumped: %zu bytes", buffer.size);
435 fwrite(buffer.data, buffer.size, buffer.size, stderr);
436 fprintf(stderr, "\n");
439 /* load the chunk into dst */
440 error = luaL_loadbuffer(dst, buffer.data, buffer.size, "(dumped function)");
441 xbt_assert(!error, "Failed to load the function into the destination state: %s",
442 lua_tostring(dst, -1));
447 * @brief Copies the light userdata on the top of src to the top of dst.
448 * @param src source state
449 * @param dst destination state
451 static void sglua_copy_lightuserdata(lua_State* src, lua_State* dst) {
452 lua_pushlightuserdata(dst, lua_touserdata(src, -1));
456 * @brief Copies the full userdata on the top of src to the top of dst.
458 * If the userdata has a metatable, the metatable is also copied.
460 * @param src source state
461 * @param dst destination state
463 static void sglua_copy_userdata(lua_State* src, lua_State* dst) {
465 int indent = lua_gettop(dst) * 6 + 2;
470 size_t size = lua_objlen(src, -1);
471 void* src_block = lua_touserdata(src, -1);
472 void* dst_block = lua_newuserdata(dst, size);
474 memcpy(dst_block, src_block, size);
476 /* copy the metatable if any */
477 int has_meta_table = lua_getmetatable(src, -1);
478 /* src: ... udata mt? */
479 if (has_meta_table) {
480 XBT_DEBUG("%sCopying metatable of userdata (%p)",
481 sglua_get_spaces(indent), lua_topointer(src, -1));
482 /* src: ... udata mt */
483 sglua_copy_table(src, dst);
488 lua_setmetatable(dst, -2);
491 XBT_DEBUG("%sMetatable of userdata copied", sglua_get_spaces(indent));
494 XBT_DEBUG("%sNo metatable for this userdata",
495 sglua_get_spaces(indent));
501 * @brief This operation is not supported (yet?) so it just pushes nil.
503 * @param src source state
504 * @param dst destination state
506 static void sglua_copy_thread(lua_State* src, lua_State* dst) {
508 XBT_WARN("Copying a thread from another state is not implemented (yet?).");
513 * @brief Copies a global value or a registry value from the maestro state.
515 * The state L must have been created by sglua_clone_maestro_state().
516 * This function is meant to be an __index metamethod.
517 * Consequently, it assumes that the stack has two elements:
518 * a table (either the environment or the registry of L) and the string key of
519 * a value that does not exist yet in this table. It copies the corresponding
520 * value from maestro and pushes it on the stack of L.
521 * If the value does not exist in maestro state either, nil is pushed.
523 * TODO: make this function thread safe. If the simulation runs in parallel,
524 * several simulated processes may trigger this __index metamethod at the same
525 * time and get globals from maestro.
527 * @param L the current state
528 * @return number of return values pushed (always 1)
530 static int l_get_from_maestro(lua_State *L) {
532 /* check the arguments */
533 luaL_checktype(L, 1, LUA_TTABLE);
534 const char* key = luaL_checkstring(L, 2);
536 XBT_DEBUG("__index of '%s' begins", key);
538 /* want a global or a registry value? */
540 if (lua_equal(L, 1, LUA_REGISTRYINDEX)) {
542 pseudo_index = LUA_REGISTRYINDEX;
543 XBT_DEBUG("Will get the value from the registry of maestro");
547 pseudo_index = LUA_GLOBALSINDEX;
548 XBT_DEBUG("Will get the value from the globals of maestro");
552 lua_State* maestro = sglua_get_maestro();
556 /* get the value from maestro */
557 lua_getfield(maestro, pseudo_index, key);
558 /* maestro: ... value */
560 /* push the value onto the stack of L */
561 sglua_move_value(maestro, L);
563 L: table key value */
565 /* prepare the return value of __index */
566 lua_pushvalue(L, -1);
567 /* L: table key value value */
569 /* L: value table key value */
571 /* save the copied value in the table for subsequent accesses */
577 XBT_DEBUG("__index of '%s' returns %s", key, sglua_tostring(L, -1));
583 * @brief Creates a new Lua state and get its environment from the maestro
586 * The state created is independent from maestro and has its own copies of
587 * global and registry values.
588 * However, the global and registry values are not copied right now from
589 * the original state; they are copied only the first time they are accessed.
590 * This behavior saves time and memory, and is okay for Simgrid's needs.
592 * TODO: if the simulation runs in parallel, copy everything right now?
594 * @return the state created
596 lua_State* sglua_clone_maestro(void) {
598 /* create the new state */
599 lua_State *L = luaL_newstate();
601 /* set its environment and its registry:
602 * - create a table newenv
603 * - create a metatable mt
604 * - set mt.__index = a function that copies the global from the father state
605 * - set mt as the metatable of the registry
606 * - set mt as the metatable of newenv
607 * - set newenv as the environment of the new state
609 lua_pushthread(L); /* thread */
610 lua_newtable(L); /* thread newenv */
611 lua_newtable(L); /* thread newenv mt */
612 lua_pushvalue(L, LUA_REGISTRYINDEX); /* thread newenv mt reg */
613 lua_pushcfunction(L, l_get_from_maestro); /* thread newenv mt reg f */
614 lua_setfield(L, -3, "__index"); /* thread newenv mt reg */
615 lua_pushvalue(L, -2); /* thread newenv mt reg mt */
616 lua_setmetatable(L, -2); /* thread newenv mt reg */
617 lua_pop(L, 1); /* thread newenv mt */
618 lua_setmetatable(L, -2); /* thread newenv */
619 lua_setfenv(L, -2); /* thread */
620 lua_pop(L, 1); /* -- */
622 /* create the table of known tables from maestro */
623 lua_pushstring(L, "simgrid.maestro_tables");
624 /* "simgrid.maestro_tables" */
625 lua_newtable(L); /* "simgrid.maestro_tables" maestrotbs*/
626 lua_rawset(L, LUA_REGISTRYINDEX);
629 /* opening the standard libs is not necessary as they are
630 * inherited like any global values */
631 /* luaL_openlibs(L); */
633 XBT_DEBUG("New state created");