MCPcopy Create free account
hub / github.com/crownengine/crown / luaB_print

Function luaB_print

src/lua/lua_environment.cpp:30–106  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28extern void load_api(LuaEnvironment &env);
29
30static int luaB_print(lua_State *L)
31{
32 TempAllocator2048 ta;
33 StringStream ss(ta);
34
35 int n = lua_gettop(L); /* number of arguments */
36 lua_getglobal(L, "tostring");
37
38 if (n == 0) {
39 logi(LUA, "");
40 return 0;
41 }
42
43 bool have_line = false;
44 for (int i = 1; i <= n; ++i) {
45 const char *s;
46 if (lua_istable(L, i)) {
47 if (have_line) {
48 logi(LUA, string_stream::c_str(ss));
49 array::clear(ss);
50 have_line = false;
51 }
52 size_t max_key_len = 0;
53 lua_pushnil(L);
54 while (lua_next(L, i) != 0) {
55 size_t key_len;
56 lua_pushvalue(L, n + 1); /* function to be called */
57 lua_pushvalue(L, -3); /* key to print */
58 lua_call(L, 1, 1);
59 s = lua_tolstring(L, -1, &key_len); /* get result */
60 if (s == NULL)
61 return luaL_error(L, LUA_QL("tostring") " must return a string to " LUA_QL("print"));
62 if (key_len > max_key_len)
63 max_key_len = key_len;
64 lua_pop(L, 2); /* pop result and value, keep key */
65 }
66 lua_pushnil(L);
67 while (lua_next(L, i) != 0) {
68 size_t key_len;
69 lua_pushvalue(L, n + 1); /* function to be called */
70 lua_pushvalue(L, -3); /* key to print */
71 lua_call(L, 1, 1);
72 s = lua_tolstring(L, -1, &key_len); /* get result */
73 if (s == NULL)
74 return luaL_error(L, LUA_QL("tostring") " must return a string to " LUA_QL("print"));
75 ss << s;
76 for (size_t column = key_len; column < max_key_len + 4; ++column)
77 ss << " ";
78 lua_pushvalue(L, n + 1); /* function to be called */
79 lua_pushvalue(L, -3); /* value to print */
80 lua_call(L, 1, 1);
81 s = lua_tostring(L, -1); /* get result */
82 if (s == NULL)
83 return luaL_error(L, LUA_QL("tostring") " must return a string to " LUA_QL("print"));
84 ss << s;
85 lua_pop(L, 3); /* pop result, key string and value, keep key */
86 logi(LUA, string_stream::c_str(ss));
87 array::clear(ss);

Callers

nothing calls this directly

Calls 8

lua_gettopFunction · 0.50
clearFunction · 0.50
lua_pushnilFunction · 0.50
lua_nextFunction · 0.50
lua_pushvalueFunction · 0.50
lua_callFunction · 0.50
lua_tolstringFunction · 0.50
luaL_errorFunction · 0.50

Tested by

no test coverage detected