Ver código fonte

[lua] refactor the rest of the tostring functionality

Justin Donaldson 5 anos atrás
pai
commit
ca30fda989

+ 22 - 2
std/lua/_lua/_hx_anon.lua

@@ -1,5 +1,10 @@
-local function _hx_obj_newindex(t,k,v) t.__fields__[k] = true; rawset(t,k,v); end
+local function _hx_obj_newindex(t,k,v)
+    t.__fields__[k] = true
+    rawset(t,k,v)
+end
+
 local _hx_obj_mt = {__newindex=_hx_obj_newindex, __tostring=_hx_tostring}
+
 local function _hx_a(...)
   local __fields__ = {};
   local ret = {__fields__ = __fields__};
@@ -24,5 +29,20 @@ local function _hx_o(obj)
 end
 
 local function _hx_new(prototype)
-  return setmetatable({__fields__ = {}}, {__newindex=_hx_anon_newindex, __index=prototype, __tostring=_hx_tostring})
+  return setmetatable({__fields__ = {}}, {__newindex=_hx_obj_newindex, __index=prototype, __tostring=_hx_tostring})
+end
+
+function _hx_field_arr(obj)
+    res = {}
+    idx = 0
+    if obj.__fields__ ~= nil then
+        obj = obj.__fields__
+    end
+    for k,v in pairs(obj) do
+        if _hx_hidden[k] == nil then
+            res[idx] = k
+            idx = idx + 1
+        end
+    end
+    return _hx_tab_array(res, idx)
 end

+ 3 - 0
std/lua/_lua/_hx_tab_array.lua

@@ -1,3 +1,5 @@
+local _hx_hidden = {__id__=true, hx__closures=true, super=true, prototype=true, __fields__=true, __ifields__=true, __class__=true, __properties__=true, __fields__=true, __name__=true}
+
 _hx_array_mt = {
     __newindex = function(t,k,v)
         local len = t.length
@@ -18,3 +20,4 @@ function _hx_tab_array(tab, length)
     tab.length = length
     return setmetatable(tab, _hx_array_mt)
 end
+

+ 22 - 21
std/lua/_lua/_hx_tostring.lua

@@ -1,4 +1,3 @@
-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
@@ -19,15 +18,15 @@ function _hx_print_enum(o, depth)
     if o.length == 2 then
         return o[0]
     else
-        local str = o[0] .. "(";
+        local str = o[0] .. "("
         for i = 2, (o.length-1) do
             if i ~= 2 then
-                str = str .. "," .. _hx_tostring(o[i], depth+1);
+                str = str .. "," .. _hx_tostring(o[i], depth+1)
             else
-                str = str .. _hx_tostring(o[i], depth+1);
+                str = str .. _hx_tostring(o[i], depth+1)
             end
         end
-        return str .. ")";
+        return str .. ")"
     end
 end
 
@@ -38,26 +37,26 @@ function _hx_tostring(obj, depth)
         return "<...>"
     end
 
-    local tstr = type(obj)
+    local tstr = _G.type(obj)
     if tstr == "string" then return obj
-    elseif tstr == "nil" then return "null";
+    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);
+        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 == "boolean" then return _G.tostring(obj)
     elseif tstr == "userdata" then
-        local mt = _G.getmetatable(obj);
+        local mt = _G.getmetatable(obj)
         if mt ~= nil and mt.__tostring ~= nil then
-            return _G.tostring(obj);
+            return _G.tostring(obj)
         else
-            return "<userdata>";
+            return "<userdata>"
         end
-    elseif tstr == "function" then return "<function>";
-    elseif tstr == "thread" then return "<thread>";
+    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)
@@ -69,9 +68,9 @@ function _hx_tostring(obj, depth)
                 str = ""
                 for i=0, (obj.length-1) do
                     if i == 0 then
-                        str = str .. _hx_tostring(obj[i])
+                        str = str .. _hx_tostring(obj[i], depth+1)
                     else
-                        str = str .. "," .. _hx_tostring(obj[i])
+                        str = str .. "," .. _hx_tostring(obj[i], depth+1)
                     end
                 end
                 return "[" .. str .. "]"
@@ -82,7 +81,9 @@ function _hx_tostring(obj, depth)
             first = true
             buffer = {}
             for k,v in pairs(obj) do
-                _G.table.insert(buffer, _hx_tostring(k, depth) .. ' : ' .. _hx_tostring(obj[k], depth))
+                if _hx_hidden[k] == nil then
+                    _G.table.insert(buffer, _hx_tostring(k, depth+1) .. ' : ' .. _hx_tostring(obj[k], depth+1))
+                end
             end
             return "{ " .. table.concat(buffer, ", ") .. " }"
         end

+ 1 - 1
std/lua/_std/Reflect.hx

@@ -89,7 +89,7 @@ import lua.Boot;
 		if (lua.Lua.type(o) == "string") {
 			return Reflect.fields(untyped String.prototype);
 		} else {
-			return [for (f in lua.Boot.fieldIterator(o)) f];
+			return untyped _hx_field_arr(o);
 		}
 	}
 

+ 2 - 1
std/lua/_std/Type.hx

@@ -121,8 +121,9 @@ enum ValueType {
 	public static function getInstanceFields(c:Class<Dynamic>):Array<String> {
 		var p:Dynamic = untyped c.prototype;
 		var a:Array<String> = [];
+
 		while (p != null) {
-			for (f in lua.Boot.fieldIterator(p)) {
+			for (f in Reflect.fields(p)) {
 				if (!Lambda.has(a, f))
 					a.push(f);
 			}