_hx_tostring.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. function _hx_print_class(obj, depth)
  2. local first = true
  3. local result = ''
  4. for k,v in pairs(obj) do
  5. if _hx_hidden[k] == nil then
  6. if first then
  7. first = false
  8. else
  9. result = result .. ', '
  10. end
  11. if _hx_hidden[k] == nil then
  12. result = result .. k .. ':' .. _hx_tostring(v, depth+1)
  13. end
  14. end
  15. end
  16. return '{ ' .. result .. ' }'
  17. end
  18. function _hx_print_enum(o, depth)
  19. if o.length == 2 then
  20. return o[0]
  21. else
  22. local str = o[0] .. "("
  23. for i = 2, (o.length-1) do
  24. if i ~= 2 then
  25. str = str .. "," .. _hx_tostring(o[i], depth+1)
  26. else
  27. str = str .. _hx_tostring(o[i], depth+1)
  28. end
  29. end
  30. return str .. ")"
  31. end
  32. end
  33. function _hx_tostring(obj, depth)
  34. if depth == nil then
  35. depth = 0
  36. elseif depth > 5 then
  37. return "<...>"
  38. end
  39. local tstr = _G.type(obj)
  40. if tstr == "string" then return obj
  41. elseif tstr == "nil" then return "null"
  42. elseif tstr == "number" then
  43. if obj == _G.math.POSITIVE_INFINITY then return "Infinity"
  44. elseif obj == _G.math.NEGATIVE_INFINITY then return "-Infinity"
  45. elseif obj == 0 then return "0"
  46. elseif obj ~= obj then return "NaN"
  47. else return _G.tostring(obj)
  48. end
  49. elseif tstr == "boolean" then return _G.tostring(obj)
  50. elseif tstr == "userdata" then
  51. local mt = _G.getmetatable(obj)
  52. if mt ~= nil and mt.__tostring ~= nil then
  53. return _G.tostring(obj)
  54. else
  55. return "<userdata>"
  56. end
  57. elseif tstr == "function" then return "<function>"
  58. elseif tstr == "thread" then return "<thread>"
  59. elseif tstr == "table" then
  60. if obj.__enum__ ~= nil then
  61. return _hx_print_enum(obj, depth)
  62. elseif obj.toString ~= nil and not _hx_is_array(obj) then return obj:toString()
  63. elseif _hx_is_array(obj) then
  64. if obj.length > 5 then
  65. return "[...]"
  66. else
  67. str = ""
  68. for i=0, (obj.length-1) do
  69. if i == 0 then
  70. str = str .. _hx_tostring(obj[i], depth+1)
  71. else
  72. str = str .. "," .. _hx_tostring(obj[i], depth+1)
  73. end
  74. end
  75. return "[" .. str .. "]"
  76. end
  77. elseif obj.__class__ ~= nil then
  78. return _hx_print_class(obj, depth)
  79. else
  80. first = true
  81. buffer = {}
  82. for k,v in pairs(obj) do
  83. if _hx_hidden[k] == nil then
  84. _G.table.insert(buffer, _hx_tostring(k, depth+1) .. ' : ' .. _hx_tostring(obj[k], depth+1))
  85. end
  86. end
  87. return "{ " .. table.concat(buffer, ", ") .. " }"
  88. end
  89. else
  90. _G.error("Unknown Lua type", 0)
  91. return ""
  92. end
  93. end
  94. function _hx_error(obj)
  95. print(obj)
  96. if obj.value then
  97. _G.print("Runtime Error: " .. _hx_tostring(obj.value));
  98. else
  99. _G.print("Runtime Error: " .. tostring(obj));
  100. end
  101. if _G.debug and _G.debug.traceback then
  102. _G.print(debug.traceback());
  103. end
  104. end