Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Let the exception flow.
[simgrid.git] / src / bindings / lua / lua_utils.cpp
1 /* Copyright (c) 2010-2019. 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 /*
8  * This file contains functions that aid users to debug their lua scripts; for instance,
9  * tables can be easily output and values are represented in a human-readable way. (For instance,
10  * a nullptr value becomes the string "nil").
11  *
12  */
13 /* SimGrid Lua helper functions                                             */
14 #include "lua_utils.hpp"
15 #include <lauxlib.h>
16
17 /**
18  * @brief Returns a string representation of a value in the Lua stack.
19  *
20  * This function is for debugging purposes.
21  * It always returns the same pointer.
22  *
23  * @param L the Lua state
24  * @param index index in the stack
25  * @return a string representation of the value at this index
26  */
27 const char* sglua_tostring(lua_State* L, int index)
28 {
29
30   static char buff[64];
31
32   switch (lua_type(L, index)) {
33
34     case LUA_TNIL:
35       snprintf(buff, 4, "nil");
36       break;
37
38     case LUA_TNUMBER:
39       snprintf(buff, 64, "%.3f", lua_tonumber(L, index));
40       break;
41
42     case LUA_TBOOLEAN:
43       snprintf(buff, 64, "%s", lua_toboolean(L, index) ? "true" : "false");
44       break;
45
46     case LUA_TSTRING:
47       snprintf(buff, 63, "'%s'", lua_tostring(L, index));
48       break;
49
50     case LUA_TFUNCTION:
51       if (lua_iscfunction(L, index)) {
52         snprintf(buff, 11, "C-function");
53       } else {
54         snprintf(buff, 9, "function");
55       }
56       break;
57
58     case LUA_TTABLE:
59       snprintf(buff, 64, "table(%p)", lua_topointer(L, index));
60       break;
61
62     case LUA_TLIGHTUSERDATA:
63     case LUA_TUSERDATA:
64       snprintf(buff, 64, "userdata(%p)", lua_touserdata(L, index));
65       break;
66
67     case LUA_TTHREAD:
68       snprintf(buff, 7, "thread");
69       break;
70
71     default:
72       snprintf(buff, 64, "unknown(%d)", lua_type(L, index));
73       break;
74   }
75   return buff;
76 }
77
78 /**
79  * @brief Returns a string representation of a key-value pair.
80  *
81  * It always returns the same pointer.
82  *
83  * @param L the Lua state
84  * @param key_index index of the key (in the lua stack)
85  * @param value_index index of the value (in the lua stack)
86  * @return a string representation of the key-value pair
87  */
88 const char* sglua_keyvalue_tostring(lua_State* L, int key_index, int value_index)
89 {
90
91   static char buff[64];
92   /* value_tostring also always returns the same pointer */
93   int len = snprintf(buff, 63, "[%s] -> ", sglua_tostring(L, key_index));
94   snprintf(buff + len, 63 - len, "%s", sglua_tostring(L, value_index));
95   return buff;
96 }