Hello!
A “work-in-progress” often leaves its documentation behind - Corona is certainly no exception from this rule…
Fortunately, however, Lua offers really useful introspection capabilities, which may also be used to inspect the Corona run-time environment.
The author has tried to inspect the “Corona-Terminal”, here are the results.
The code used was the following (unfortunately, the forum does no longer display any indentations - is there a special trick?) :
[code]
io.stdout:setvbuf(“no”);
io.stderr:setvbuf(“no”);
– inform[ln] writes the given arguments onto stdout –
function inform (…)
for i = 1,select(’#’,…) do – even “nil” is handled properly
io.stdout:write(tostring(select(i,…)));
end;
end;
function informln (…)
for i = 1,select(’#’,…) do – even “nil” is handled properly
io.stdout:write(tostring(select(i,…)));
end;
io.stdout:write("\n");
end;
– say[ln] just synonyms for inform[ln] –
say = inform;
sayln = informln;
local Label = display.newText("(see Terminal)", 60,50, nil, 28);
Label:setTextColor(255,255,255);
– Table Tracking (never inspect any table twice) –
local inspectedTables = {};
local pendingTables = {};
– inspectTable inspects a given table –
function inspectTable (TablePrefix, Table)
inspectedTables[Table] = TablePrefix; – mark this table as inspected
---- construct a sorted list with the keys of this table ----
local KeyTable = {}; local hasNonPrintableKeys = false;
for Key,Value in pairs(Table) do
if (type(Key) == “string”) then
KeyTable[#KeyTable+1] = Key;
elseif (type(Key) == “number”) then
KeyTable[#KeyTable+1] = tostring(Key); – not really fool-proof, I know
else
hasNonPrintableKeys = true; – just remember any non-printable keys
end;
end;
table.sort(KeyTable);
---- determine the length of the longest key ----
local maxKeyLength = 0;
for i = 1,#KeyTable do
if (#KeyTable[i] > maxKeyLength) then
maxKeyLength = #KeyTable[i]
end;
end;
---- finally print the list of global keys ----
sayln();
sayln(string.rep("-",80));
sayln(" Contents of “,TablePrefix);
sayln(string.rep(”-",80));
sayln();
if (#KeyTable == 0) then
if (hasNonPrintableKeys) then
sayln(" (this table seems to contain non-printable keys only)");
else
sayln(" (this table is empty)");
end;
end;
for i = 1,#KeyTable do
local currentKey = KeyTable[i]; – just a shortcut
say(" “, string.rep(” ",maxKeyLength-#currentKey), currentKey, ": ");
local currentType = type(Table[currentKey]);
if (currentType == “nil”) then
sayln(“nil”);
elseif (currentType == “boolean”) then
sayln(Table[currentKey]);
elseif (currentType == “number”) then
sayln(Table[currentKey]);
elseif (currentType == “string”) then
sayln("’",Table[currentKey],"’");
elseif (currentType == “table”) then
if (inspectedTables[Table[currentKey]] == nil) then
sayln("(table, will be inspected later)");
pendingTables[#pendingTables+1] = {
Prefix = TablePrefix…"[’"…currentKey…"’]",
Value = Table[currentKey]
};
else
sayln("(table, same as “…inspectedTables[Table[currentKey]]…”)");
end;
else
sayln("(",currentType,")");
end;
end;
if (hasNonPrintableKeys) then
sayln();
sayln(“this table also seems to contain non-printable keys!”)
end;
end;
– Inspection of the Corona Runtime Environment –
sayln("Lua Version: ", _VERSION);
inspectTable("_G",_G);
while (#pendingTables > 0) do
local nextTable = table.remove(pendingTables,1);
inspectTable(nextTable.Prefix,nextTable.Value);
end;
[/code] [import]uid: 4331 topic_id: 359 reply_id: 300359[/import]