Initial revision of lua 'doc' function
This commit is contained in:
@@ -5,14 +5,19 @@
|
||||
#include <iostream>
|
||||
|
||||
|
||||
void atomic_print(LuaStack &LS, LuaSlot val, std::ostream *os) {
|
||||
switch (LS.type(val)) {
|
||||
void atomic_print(LuaStack &LS, LuaSlot val, bool quote, std::ostream *os) {
|
||||
int tt = LS.type(val);
|
||||
switch (tt) {
|
||||
case LUA_TNIL:
|
||||
(*os) << "nil";
|
||||
return;
|
||||
case LUA_TSTRING:
|
||||
// TODO: this could be more efficient.
|
||||
(*os) << LS.ckstring(val);
|
||||
if (quote) {
|
||||
util::quote_string(LS.ckstring(val), os);
|
||||
} else {
|
||||
// TODO: this could be more efficient.
|
||||
(*os) << LS.ckstring(val);
|
||||
}
|
||||
return;
|
||||
case LUA_TNUMBER: {
|
||||
double value = LS.cknumber(val);
|
||||
@@ -27,38 +32,18 @@ void atomic_print(LuaStack &LS, LuaSlot val, std::ostream *os) {
|
||||
case LUA_TBOOLEAN:
|
||||
(*os) << (LS.ckboolean(val) ? "true" : "false");
|
||||
return;
|
||||
case LUA_TTABLE:
|
||||
(*os) << "table";
|
||||
return;
|
||||
default:
|
||||
(*os) << "unknown";
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static 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: {
|
||||
double value = LS.cknumber(val);
|
||||
int64_t ivalue = int64_t(value);
|
||||
if (double(ivalue) == value) {
|
||||
(*os) << ivalue;
|
||||
case LUA_TFUNCTION: {
|
||||
std::string name = LS.get_function_name(val);
|
||||
if (name.empty()) {
|
||||
(*os) << "<fn>";
|
||||
} else {
|
||||
(*os) << value;
|
||||
(*os) << "<fn." << name << ">";
|
||||
}
|
||||
return true;
|
||||
return;
|
||||
}
|
||||
case LUA_TBOOLEAN:
|
||||
(*os) << (LS.ckboolean(val) ? "true" : "false");
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
(*os) << "<" << lua_typename(LS.state(), tt) << ">";
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,7 +90,7 @@ static void findtables(LuaStack &LS0, LuaSlot root, LuaSlot tabcount) {
|
||||
LS.result();
|
||||
}
|
||||
|
||||
LuaDefine(table_findtables, "c") {
|
||||
LuaDefine(table_findtables, "root", "recursively find tables (debugging only)") {
|
||||
LuaArg root;
|
||||
LuaRet tabcount;
|
||||
LuaStack LS(L, root, tabcount);
|
||||
@@ -140,16 +125,9 @@ 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) << ">";
|
||||
// If it's anything but a table, use 'atomic_print'.
|
||||
if (!LS.istable(root)) {
|
||||
atomic_print(LS, root, true, insp.stream);
|
||||
LS.result();
|
||||
return;
|
||||
}
|
||||
@@ -272,7 +250,7 @@ void pprint(LuaStack &LS0, LuaSlot root, bool indent, std::ostream *os) {
|
||||
LS.result();
|
||||
}
|
||||
|
||||
LuaDefine(string_isidentifier, "c") {
|
||||
LuaDefine(string_isidentifier, "str", "return true if the string is a valid lua identifier") {
|
||||
LuaArg str;
|
||||
LuaRet result;
|
||||
LuaStack LS(L, str, result);
|
||||
@@ -285,17 +263,17 @@ LuaDefine(string_isidentifier, "c") {
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
LuaDefine(string_print, "c") {
|
||||
LuaDefine(string_print, "obj", "print the specified object into a string") {
|
||||
LuaArg val;
|
||||
LuaRet result;
|
||||
LuaStack LS(L, val, result);
|
||||
std::ostringstream oss;
|
||||
atomic_print(LS, val, &oss);
|
||||
atomic_print(LS, val, false, &oss);
|
||||
LS.set(result, oss.str());
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
LuaDefine(string_pprint, "c") {
|
||||
LuaDefine(string_pprint, "obj,indent", "pretty-print the specified object into a string") {
|
||||
LuaArg root, indent;
|
||||
LuaRet result;
|
||||
LuaStack LS(L, root, indent, result);
|
||||
@@ -306,12 +284,12 @@ LuaDefine(string_pprint, "c") {
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
LuaDefine(string_tostring, "f") {
|
||||
LuaDefine(tostring, "obj", "print the specified object into a string") {
|
||||
LuaArg val;
|
||||
LuaRet result;
|
||||
LuaStack LS(L, val, result);
|
||||
std::ostringstream oss;
|
||||
atomic_print(LS, val, &oss);
|
||||
atomic_print(LS, val, false, &oss);
|
||||
LS.set(result, oss.str());
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user