2021-09-07 17:37:23 -04:00
|
|
|
#include "print.hpp"
|
|
|
|
|
#include "util.hpp"
|
2021-09-08 01:32:08 -04:00
|
|
|
#include "table.hpp"
|
2021-09-07 17:37:23 -04:00
|
|
|
#include <ostream>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
void luai_writestring(const char *s, size_t len) {
|
|
|
|
|
std::cout.write(s, len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void luai_writeline() {
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
std::cout.flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaDefine(string_isidentifier, "c") {
|
|
|
|
|
LuaArg str;
|
|
|
|
|
LuaRet result;
|
|
|
|
|
LuaStack LS(L, str, result);
|
|
|
|
|
if (LS.isstring(str)) {
|
|
|
|
|
std::string s = LS.ckstring(str);
|
|
|
|
|
LS.set(result, util::is_identifier(s));
|
|
|
|
|
} else {
|
|
|
|
|
LS.set(result, false);
|
|
|
|
|
}
|
|
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-08 01:32:08 -04:00
|
|
|
bool string_quote(LuaStack &LS, LuaSlot val, std::ostream *os) {
|
|
|
|
|
switch (LS.type(val)) {
|
|
|
|
|
case LUA_TNIL:
|
|
|
|
|
(*os) << "nil";
|
|
|
|
|
return true;
|
|
|
|
|
case LUA_TSTRING:
|
|
|
|
|
util::quote_string(LS.ckstring(val), os);
|
|
|
|
|
return true;
|
|
|
|
|
case LUA_TNUMBER:
|
|
|
|
|
(*os) << LS.ckinteger(val);
|
|
|
|
|
return true;
|
|
|
|
|
case LUA_TBOOLEAN:
|
|
|
|
|
(*os) << (LS.ckboolean(val) ? "true" : "false");
|
|
|
|
|
return true;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaDefine(string_quote, "c") {
|
|
|
|
|
LuaArg val;
|
|
|
|
|
LuaRet result;
|
|
|
|
|
LuaStack LS(L, val, result);
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
|
string_quote(LS, val, &oss);
|
|
|
|
|
LS.set(result, oss.str());
|
|
|
|
|
return LS.result();
|
|
|
|
|
}
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaDefine(table_findtables, "c") {
|
|
|
|
|
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;
|
|
|
|
|
std::ostream *stream;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
LuaVar idv, pairs, key, val;
|
|
|
|
|
LuaStack LS(insp.L, idv, pairs, key, val);
|
|
|
|
|
|
|
|
|
|
// If it's a simple type, print it quoted.
|
|
|
|
|
if (string_quote(LS, root, insp.stream)) {
|
|
|
|
|
LS.result();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If it's not a table, just print the typename.
|
|
|
|
|
int t = LS.type(root);
|
|
|
|
|
if (t != LUA_TTABLE) {
|
|
|
|
|
(*insp.stream) << "<" << lua_typename(insp.L, t) << ">";
|
|
|
|
|
LS.result();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the table ID is greater than zero, then we've already
|
|
|
|
|
// printed it. Just print the table ID.
|
|
|
|
|
LS.rawget(idv, insp.ids, root);
|
|
|
|
|
int id = LS.ckint(idv);
|
|
|
|
|
if (id > 0) {
|
|
|
|
|
if (lua_nkeys(insp.L, root.index())==0) {
|
|
|
|
|
(*insp.stream) << "<table " << id << ">{}";
|
|
|
|
|
} else {
|
|
|
|
|
(*insp.stream) << "<table " << id << ">{...}";
|
|
|
|
|
}
|
|
|
|
|
LS.result();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Allocate an ID for the table, if necessary.
|
|
|
|
|
if (id < 0) {
|
|
|
|
|
id = insp.nextid++;
|
|
|
|
|
LS.rawset(insp.ids, root, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Print the table ID if greater than zero.
|
|
|
|
|
if (id > 0) {
|
|
|
|
|
(*insp.stream) << "<table " << id << ">";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
|
LS.rawgeti(key, pairs, i);
|
|
|
|
|
if (LS.isnil(key)) break;
|
|
|
|
|
LS.rawgeti(val, pairs, i+1);
|
|
|
|
|
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> = ";
|
|
|
|
|
|
|
|
|
|
// if isclass(mt) then
|
|
|
|
|
// self:puts('<class ')
|
|
|
|
|
// self:puts(rawget(mt, "__class"))
|
|
|
|
|
// self:puts('>')
|
|
|
|
|
// end
|
|
|
|
|
pprint_r(insp, level + 1, val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close the brackets.
|
|
|
|
|
if (multiline) {
|
|
|
|
|
tabify(insp, level);
|
|
|
|
|
} else if (nextseq > 1) {
|
|
|
|
|
(*insp.stream) << " ";
|
|
|
|
|
}
|
|
|
|
|
(*insp.stream) << "}";
|
|
|
|
|
|
|
|
|
|
LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pprint(LuaStack &LS0, LuaSlot root, bool indent, std::ostream *os) {
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaDefine(pprint_pprint, "f") {
|
|
|
|
|
LuaStack LS(L);
|
|
|
|
|
for (int i = 1; i <= lua_gettop(L); i++) {
|
|
|
|
|
LuaSpecial root(i);
|
|
|
|
|
pprint(LS, root, true, &std::cout);
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
}
|
|
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaDefine(string_pprint, "c") {
|
|
|
|
|
LuaArg root, indent;
|
|
|
|
|
LuaRet result;
|
|
|
|
|
LuaStack LS(L, root, indent, result);
|
|
|
|
|
bool ind = LS.ckboolean(indent);
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
|
pprint(LS, root, ind, &oss);
|
|
|
|
|
LS.set(result, oss.str());
|
|
|
|
|
return LS.result();
|
2021-09-07 17:37:23 -04:00
|
|
|
}
|