2022-02-24 02:17:41 -05:00
|
|
|
|
2022-02-23 23:08:28 -05:00
|
|
|
#include "wrap-ostream.hpp"
|
|
|
|
|
|
2021-10-21 13:15:04 -04:00
|
|
|
#include "pprint.hpp"
|
2021-09-07 17:37:23 -04:00
|
|
|
#include "util.hpp"
|
2021-09-08 01:32:08 -04:00
|
|
|
#include "table.hpp"
|
2022-02-23 23:08:28 -05:00
|
|
|
|
2021-09-07 17:37:23 -04:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
|
2022-02-24 02:17:41 -05:00
|
|
|
void atomic_print(LuaStack &LS, LuaSlot val, bool quote, eng::ostream *os) {
|
2021-12-15 23:03:43 -05:00
|
|
|
int tt = LS.type(val);
|
|
|
|
|
switch (tt) {
|
2021-10-21 14:22:06 -04:00
|
|
|
case LUA_TNIL:
|
|
|
|
|
(*os) << "nil";
|
|
|
|
|
return;
|
|
|
|
|
case LUA_TSTRING:
|
2021-12-15 23:03:43 -05:00
|
|
|
if (quote) {
|
|
|
|
|
util::quote_string(LS.ckstring(val), os);
|
|
|
|
|
} else {
|
|
|
|
|
// TODO: this could be more efficient.
|
|
|
|
|
(*os) << LS.ckstring(val);
|
|
|
|
|
}
|
2021-10-21 14:22:06 -04:00
|
|
|
return;
|
2021-12-14 10:51:00 -05:00
|
|
|
case LUA_TNUMBER: {
|
|
|
|
|
double value = LS.cknumber(val);
|
|
|
|
|
int64_t ivalue = int64_t(value);
|
|
|
|
|
if (double(ivalue) == value) {
|
|
|
|
|
(*os) << ivalue;
|
|
|
|
|
} else {
|
|
|
|
|
(*os) << value;
|
|
|
|
|
}
|
2021-10-21 14:22:06 -04:00
|
|
|
return;
|
2021-12-14 10:51:00 -05:00
|
|
|
}
|
2021-10-21 14:22:06 -04:00
|
|
|
case LUA_TBOOLEAN:
|
|
|
|
|
(*os) << (LS.ckboolean(val) ? "true" : "false");
|
|
|
|
|
return;
|
2021-12-15 23:03:43 -05:00
|
|
|
case LUA_TFUNCTION: {
|
2021-12-17 14:04:34 -05:00
|
|
|
(*os) << "<function>";
|
2021-12-15 23:03:43 -05:00
|
|
|
return;
|
2021-11-26 12:28:59 -05:00
|
|
|
}
|
2021-09-08 01:32:08 -04:00
|
|
|
default:
|
2021-12-15 23:03:43 -05:00
|
|
|
(*os) << "<" << lua_typename(LS.state(), tt) << ">";
|
|
|
|
|
return;
|
2021-09-08 01:32:08 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-07 17:37:23 -04:00
|
|
|
|
2021-09-08 01:32:08 -04:00
|
|
|
// Find tables recursively.
|
|
|
|
|
//
|
|
|
|
|
// Builds a table (tabcount) whose keys are tables. If a table
|
|
|
|
|
// is visited exactly once, then tabcount[t]=0. If a table is
|
|
|
|
|
// visited more than once, then tabcount[t]=-1.
|
|
|
|
|
//
|
|
|
|
|
static void findtables(LuaStack &LS0, LuaSlot root, LuaSlot tabcount) {
|
|
|
|
|
lua_State *L = LS0.state();
|
|
|
|
|
LuaVar key, val, tab, count;
|
|
|
|
|
LuaStack LS(L, key, val, tab, count);
|
2021-09-07 17:37:23 -04:00
|
|
|
|
2021-09-08 01:32:08 -04:00
|
|
|
LS.newtable(tabcount);
|
|
|
|
|
int top = lua_gettop(L);
|
|
|
|
|
if (LS.istable(root)) {
|
|
|
|
|
lua_pushvalue(L, root.index());
|
|
|
|
|
}
|
|
|
|
|
while (lua_gettop(L) > top) {
|
|
|
|
|
lua_checkstack(L, 10);
|
|
|
|
|
lua_replace(L, tab.index());
|
|
|
|
|
LS.rawget(count, tabcount, tab);
|
|
|
|
|
if (LS.isnil(count)) {
|
|
|
|
|
LS.rawset(tabcount, tab, 0);
|
|
|
|
|
LS.set(key, LuaNil);
|
|
|
|
|
while (LS.next(tab, key, val)) {
|
|
|
|
|
if (LS.istable(key)) {
|
|
|
|
|
lua_pushvalue(L, key.index());
|
|
|
|
|
}
|
|
|
|
|
if (LS.istable(val)) {
|
|
|
|
|
lua_pushvalue(L, val.index());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
LS.getmetatable(val, tab);
|
|
|
|
|
if (LS.istable(val)) {
|
|
|
|
|
lua_pushvalue(L, val.index());
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
LS.rawset(tabcount, tab, -1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 23:03:43 -05:00
|
|
|
LuaDefine(table_findtables, "root", "recursively find tables (debugging only)") {
|
2021-09-08 01:32:08 -04:00
|
|
|
LuaArg root;
|
|
|
|
|
LuaRet tabcount;
|
|
|
|
|
LuaStack LS(L, root, tabcount);
|
|
|
|
|
findtables(LS, root, tabcount);
|
|
|
|
|
return LS.result();
|
|
|
|
|
}
|
2021-09-07 17:37:23 -04:00
|
|
|
|
2021-09-08 01:32:08 -04:00
|
|
|
struct Inspector {
|
|
|
|
|
lua_State *L;
|
|
|
|
|
LuaVar ids;
|
|
|
|
|
int nextid;
|
|
|
|
|
bool indent;
|
|
|
|
|
int maxlen;
|
|
|
|
|
bool anyindent;
|
2022-02-24 02:17:41 -05:00
|
|
|
eng::ostream *stream;
|
2021-09-08 01:32:08 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void tabify(Inspector &insp, int level) {
|
|
|
|
|
if (insp.indent) {
|
|
|
|
|
(*insp.stream) << std::endl;
|
|
|
|
|
for (int i = 0; i < level; i++) {
|
|
|
|
|
(*insp.stream) << " ";
|
|
|
|
|
}
|
|
|
|
|
insp.anyindent = true;
|
|
|
|
|
} else {
|
|
|
|
|
(*insp.stream) << " ";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void pprint_r(Inspector &insp, int level, LuaSlot root) {
|
2021-11-23 14:38:08 -05:00
|
|
|
lua_checkstack(insp.L, 20);
|
2021-09-08 01:32:08 -04:00
|
|
|
LuaVar idv, pairs, key, val;
|
|
|
|
|
LuaStack LS(insp.L, idv, pairs, key, val);
|
|
|
|
|
|
2021-12-15 23:03:43 -05:00
|
|
|
// If it's anything but a table, use 'atomic_print'.
|
|
|
|
|
if (!LS.istable(root)) {
|
|
|
|
|
atomic_print(LS, root, true, insp.stream);
|
2021-09-08 01:32:08 -04:00
|
|
|
LS.result();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-26 12:28:59 -05:00
|
|
|
// Determine the table's ID, allocating an ID if necessary.
|
2021-09-08 01:32:08 -04:00
|
|
|
LS.rawget(idv, insp.ids, root);
|
|
|
|
|
int id = LS.ckint(idv);
|
2021-11-26 12:28:59 -05:00
|
|
|
bool new_id = false;
|
2021-09-08 01:32:08 -04:00
|
|
|
if (id < 0) {
|
2021-11-26 12:28:59 -05:00
|
|
|
new_id = true;
|
2021-09-08 01:32:08 -04:00
|
|
|
id = insp.nextid++;
|
|
|
|
|
LS.rawset(insp.ids, root, id);
|
|
|
|
|
}
|
2021-11-26 12:28:59 -05:00
|
|
|
|
|
|
|
|
// Print the table's name, if any.
|
|
|
|
|
bool is_class = false;
|
|
|
|
|
bool is_tangible = false;
|
2022-02-24 02:17:41 -05:00
|
|
|
eng::string cname = LS.classname(root);
|
2021-11-26 12:28:59 -05:00
|
|
|
if (cname != "") {
|
|
|
|
|
is_class = true;
|
|
|
|
|
(*insp.stream) << "<class " << cname << ">";
|
|
|
|
|
} else {
|
|
|
|
|
int64_t tid = LS.tanid(root);
|
|
|
|
|
if (tid > 0) {
|
|
|
|
|
is_tangible = true;
|
|
|
|
|
(*insp.stream) << "<tangible " << tid << ">";
|
|
|
|
|
} else if (id > 0) {
|
|
|
|
|
(*insp.stream) << "<table " << id << ">";
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-08 01:32:08 -04:00
|
|
|
|
2021-11-26 12:28:59 -05:00
|
|
|
// If this is a class, and we're not at the top level, truncate.
|
|
|
|
|
if ((is_class || is_tangible) && (level > 0)) {
|
|
|
|
|
LS.result();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If this is a table we've already printed, truncate it.
|
|
|
|
|
if ((id > 0) && (!new_id)) {
|
|
|
|
|
if (lua_nkeys(insp.L, root.index())==0) {
|
|
|
|
|
(*insp.stream) << "{}";
|
|
|
|
|
} else {
|
|
|
|
|
(*insp.stream) << "{...}";
|
|
|
|
|
}
|
|
|
|
|
LS.result();
|
|
|
|
|
return;
|
2021-09-08 01:32:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// State variables.
|
|
|
|
|
int nextseq = 1;
|
|
|
|
|
bool needcomma = false;
|
|
|
|
|
bool multiline = false;
|
|
|
|
|
|
|
|
|
|
// Open the brackets.
|
|
|
|
|
(*insp.stream) << "{";
|
|
|
|
|
|
|
|
|
|
// Output the table keys.
|
|
|
|
|
table_getpairs(LS, root, pairs, true);
|
|
|
|
|
for (int i = 2; ; i+=2) {
|
2021-11-17 15:11:55 -05:00
|
|
|
LS.rawget(key, pairs, i);
|
2021-09-08 01:32:08 -04:00
|
|
|
if (LS.isnil(key)) break;
|
2021-11-17 15:11:55 -05:00
|
|
|
LS.rawget(val, pairs, i+1);
|
2021-09-08 01:32:08 -04:00
|
|
|
if (needcomma) (*insp.stream) << ",";
|
|
|
|
|
needcomma = true;
|
|
|
|
|
if (LS.isnumber(key) && (LS.ckint(key) == nextseq)) {
|
|
|
|
|
(*insp.stream) << " ";
|
|
|
|
|
pprint_r(insp, level + 1, val);
|
|
|
|
|
nextseq = nextseq + 1;
|
|
|
|
|
} else {
|
|
|
|
|
multiline = true;
|
|
|
|
|
tabify(insp, level + 1);
|
|
|
|
|
if (LS.isstring(key) && util::is_identifier(LS.ckstring(key))) {
|
|
|
|
|
(*insp.stream) << LS.ckstring(key);
|
|
|
|
|
} else {
|
|
|
|
|
(*insp.stream) << "[";
|
|
|
|
|
pprint_r(insp, level + 1, key);
|
|
|
|
|
(*insp.stream) << "]";
|
|
|
|
|
}
|
2021-09-09 18:23:17 -04:00
|
|
|
if (insp.indent) {
|
|
|
|
|
(*insp.stream) << " = ";
|
|
|
|
|
} else {
|
|
|
|
|
(*insp.stream) << "=";
|
|
|
|
|
}
|
2021-09-08 01:32:08 -04:00
|
|
|
pprint_r(insp, level + 1, val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Output the metatable.
|
|
|
|
|
LS.getmetatable(val, root);
|
|
|
|
|
if (LS.istable(val)) {
|
|
|
|
|
multiline = true;
|
|
|
|
|
if (needcomma) (*insp.stream) << ",";
|
|
|
|
|
needcomma = true;
|
|
|
|
|
tabify(insp, level + 1);
|
|
|
|
|
(*insp.stream) << "<meta> = ";
|
|
|
|
|
pprint_r(insp, level + 1, val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close the brackets.
|
|
|
|
|
if (multiline) {
|
|
|
|
|
tabify(insp, level);
|
|
|
|
|
} else if (nextseq > 1) {
|
|
|
|
|
(*insp.stream) << " ";
|
|
|
|
|
}
|
|
|
|
|
(*insp.stream) << "}";
|
|
|
|
|
|
|
|
|
|
LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 02:17:41 -05:00
|
|
|
void pprint(LuaStack &LS0, LuaSlot root, bool indent, eng::ostream *os) {
|
2021-09-08 01:32:08 -04:00
|
|
|
Inspector insp;
|
|
|
|
|
LuaStack LS(LS0.state(), insp.ids);
|
|
|
|
|
findtables(LS, root, insp.ids);
|
|
|
|
|
insp.L = LS0.state();
|
|
|
|
|
insp.nextid = 1;
|
|
|
|
|
insp.indent = indent;
|
|
|
|
|
insp.anyindent = false;
|
|
|
|
|
insp.stream = os;
|
|
|
|
|
pprint_r(insp, 0, root);
|
|
|
|
|
LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 23:03:43 -05:00
|
|
|
LuaDefine(string_isidentifier, "str", "return true if the string is a valid lua identifier") {
|
2021-10-21 14:22:06 -04:00
|
|
|
LuaArg str;
|
|
|
|
|
LuaRet result;
|
|
|
|
|
LuaStack LS(L, str, result);
|
|
|
|
|
if (LS.isstring(str)) {
|
2022-02-24 02:17:41 -05:00
|
|
|
eng::string s = LS.ckstring(str);
|
2021-10-21 14:22:06 -04:00
|
|
|
LS.set(result, util::is_identifier(s));
|
|
|
|
|
} else {
|
|
|
|
|
LS.set(result, false);
|
|
|
|
|
}
|
|
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 23:03:43 -05:00
|
|
|
LuaDefine(string_print, "obj", "print the specified object into a string") {
|
2021-10-21 14:22:06 -04:00
|
|
|
LuaArg val;
|
|
|
|
|
LuaRet result;
|
|
|
|
|
LuaStack LS(L, val, result);
|
2022-02-24 02:17:41 -05:00
|
|
|
eng::ostringstream oss;
|
2021-12-15 23:03:43 -05:00
|
|
|
atomic_print(LS, val, false, &oss);
|
2021-10-21 14:22:06 -04:00
|
|
|
LS.set(result, oss.str());
|
|
|
|
|
return LS.result();
|
|
|
|
|
}
|
2021-09-08 01:32:08 -04:00
|
|
|
|
2021-12-15 23:03:43 -05:00
|
|
|
LuaDefine(string_pprint, "obj,indent", "pretty-print the specified object into a string") {
|
2021-09-08 01:32:08 -04:00
|
|
|
LuaArg root, indent;
|
|
|
|
|
LuaRet result;
|
|
|
|
|
LuaStack LS(L, root, indent, result);
|
|
|
|
|
bool ind = LS.ckboolean(indent);
|
2022-02-24 02:17:41 -05:00
|
|
|
eng::ostringstream oss;
|
2021-09-08 01:32:08 -04:00
|
|
|
pprint(LS, root, ind, &oss);
|
|
|
|
|
LS.set(result, oss.str());
|
|
|
|
|
return LS.result();
|
2021-09-07 17:37:23 -04:00
|
|
|
}
|
2021-10-21 14:22:06 -04:00
|
|
|
|
2021-12-15 23:03:43 -05:00
|
|
|
LuaDefine(tostring, "obj", "print the specified object into a string") {
|
2021-10-21 14:22:06 -04:00
|
|
|
LuaArg val;
|
|
|
|
|
LuaRet result;
|
|
|
|
|
LuaStack LS(L, val, result);
|
2022-02-24 02:17:41 -05:00
|
|
|
eng::ostringstream oss;
|
2021-12-15 23:03:43 -05:00
|
|
|
atomic_print(LS, val, false, &oss);
|
2021-10-21 14:22:06 -04:00
|
|
|
LS.set(result, oss.str());
|
|
|
|
|
return LS.result();
|
|
|
|
|
}
|