-
-
Notifications
You must be signed in to change notification settings - Fork 670
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[lua] refactor runtime tostring to separate lua file
- Loading branch information
1 parent
a0ed7f3
commit 0bab113
Showing
6 changed files
with
120 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,20 @@ | ||
local _hx_array_mt = { | ||
__newindex = function(t,k,v) | ||
local len = t.length | ||
t.length = k >= len and (k + 1) or len | ||
rawset(t,k,v) | ||
end | ||
_hx_array_mt = { | ||
__newindex = function(t,k,v) | ||
local len = t.length | ||
t.length = k >= len and (k + 1) or len | ||
rawset(t,k,v) | ||
end | ||
} | ||
|
||
local function _hx_tab_array(tab,length) | ||
tab.length = length | ||
return setmetatable(tab, _hx_array_mt) | ||
function _hx_is_array(o) | ||
return type(o) == "table" | ||
and o.__enum__ == nil | ||
and getmetatable(o) == _hx_array_mt | ||
end | ||
|
||
|
||
|
||
function _hx_tab_array(tab, length) | ||
tab.length = length | ||
return setmetatable(tab, _hx_array_mt) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
local _hx_hidden = {__id__=true, hx__closures=true, super=true, prototype=true, __fields__=true, __ifields__=true, __class__=true, __properties__=true} | ||
|
||
function _hx_print_class(obj, depth) | ||
local first = true | ||
local result = '' | ||
for k,v in pairs(obj) do | ||
if first then | ||
result = result .. ',' | ||
first = false | ||
end | ||
if _hx_hidden[k] == nil then | ||
result = result .. k .. ':' .. _hx_tostring(v, depth+1) | ||
end | ||
end | ||
return result | ||
end | ||
|
||
function _hx_print_enum(o, depth) | ||
if o.length == 2 then | ||
return o[0] | ||
else | ||
local str = o[0] .. "("; | ||
for i = 2, (o.length-1) do | ||
if i ~= 2 then | ||
str = str .. "," .. _hx_tostring(o[i], depth+1); | ||
else | ||
str = str .. _hx_tostring(o[i], depth+1); | ||
end | ||
end | ||
return str .. ")"; | ||
end | ||
end | ||
|
||
function _hx_tostring(obj, depth) | ||
if depth == nil then | ||
depth = 0 | ||
elseif depth > 5 then | ||
return "<...>" | ||
end | ||
|
||
local tstr = type(obj) | ||
if tstr == "string" then return obj | ||
elseif tstr == "nil" then return "null"; | ||
elseif tstr == "number" then | ||
if obj == _G.math.POSITIVE_INFINITY then return "Infinity"; | ||
elseif obj == _G.math.NEGATIVE_INFINITY then return "-Infinity"; | ||
elseif obj == 0 then return "0"; | ||
elseif obj ~= obj then return "NaN"; | ||
else return _G.tostring(obj); | ||
end | ||
elseif tstr == "boolean" then return _G.tostring(obj); | ||
elseif tstr == "userdata" then | ||
local mt = _G.getmetatable(obj); | ||
if mt ~= nil and mt.__tostring ~= nil then | ||
return _G.tostring(obj); | ||
else | ||
return "<userdata>"; | ||
end | ||
elseif tstr == "function" then return "<function>"; | ||
elseif tstr == "thread" then return "<thread>"; | ||
elseif tstr == "table" then | ||
if obj.__enum__ ~= nil then | ||
return _hx_print_enum(obj, depth) | ||
elseif obj.toString ~= nil and not _hx_is_array(obj) then return obj:toString() | ||
elseif _hx_is_array(obj) then | ||
if obj.length > 5 then | ||
return "[...]" | ||
else | ||
str = "" | ||
for i=0, (obj.length-1) do | ||
if i == 0 then | ||
str = str .. _hx_tostring(obj[i]) | ||
else | ||
str = str .. "," .. _hx_tostring(obj[i]) | ||
end | ||
end | ||
return "[" .. str .. "]" | ||
end | ||
elseif obj.__class__ ~= nil then | ||
return _hx_print_class(obj, depth) | ||
else | ||
first = true | ||
buffer = {} | ||
for k,v in pairs(obj) do | ||
_G.table.insert(buffer, _hx_tostring(k, depth) .. ' : ' .. _hx_tostring(obj[k], depth)) | ||
end | ||
return "{ " .. table.concat(buffer, ", ") .. " }" | ||
end | ||
else | ||
_G.error("Unknown Lua type", 0) | ||
return "" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters