/metalualib/metalua/base.lua

http://github.com/davidm/lua-inspect · Lua · 107 lines · 72 code · 16 blank · 19 comment · 11 complexity · 2c88e61fef7bf3bf9f36435ee5ce0229 MD5 · raw file

  1. ----------------------------------------------------------------------
  2. ----------------------------------------------------------------------
  3. --
  4. -- Base library extension
  5. --
  6. ----------------------------------------------------------------------
  7. ----------------------------------------------------------------------
  8. if not metalua then metalua = {} end --PATCHED.. rawset(getfenv(), 'metalua', { }) end
  9. metalua.version = "v-0.5"
  10. if not rawpairs then
  11. rawpairs, rawipairs, rawtype = pairs, ipairs, type
  12. end
  13. function pairsmt(x) -- PATCHED:LuaInspect [*]
  14. assert(type(x)=='table', 'pairs() expects a table')
  15. local mt = getmetatable(x)
  16. if mt then
  17. local mtp = mt.__pairs
  18. if mtp then return mtp(x) end
  19. end
  20. return rawpairs(x)
  21. end
  22. function ipairsmt(x) --PATCHED:LuaInspect [*]
  23. assert(type(x)=='table', 'ipairs() expects a table')
  24. local mt = getmetatable(x)
  25. if mt then
  26. local mti = mt.__ipairs
  27. if mti then return mti(x) end
  28. end
  29. return rawipairs(x)
  30. end
  31. --PATCHED:LuaInspect: [*] For performance, compatibility,
  32. -- and debugging reasons, avoid overriding builtins.
  33. --[[
  34. function type(x)
  35. local mt = getmetatable(x)
  36. if mt then
  37. local mtt = mt.__type
  38. if mtt then return mtt end
  39. end
  40. return rawtype(x)
  41. end
  42. ]]
  43. function min (a, ...)
  44. for n in values{...} do if n<a then a=n end end
  45. return a
  46. end
  47. function max (a, ...)
  48. for n in values{...} do if n>a then a=n end end
  49. return a
  50. end
  51. function o (...)
  52. local args = {...}
  53. local function g (...)
  54. local result = {...}
  55. for i=#args, 1, -1 do result = {args[i](unpack(result))} end
  56. return unpack (result)
  57. end
  58. return g
  59. end
  60. function id (...) return ... end
  61. function const (k) return function () return k end end
  62. function printf(...) return print(string.format(...)) end
  63. function eprintf(...)
  64. io.stderr:write(string.format(...).."\n")
  65. end
  66. function ivalues (x)
  67. assert(type(x)=='table', 'ivalues() expects a table')
  68. local i = 1
  69. local function iterator ()
  70. local r = x[i]; i=i+1; return r
  71. end
  72. return iterator
  73. end
  74. function values (x)
  75. assert(type(x)=='table', 'values() expects a table')
  76. local function iterator (state)
  77. local it
  78. state.content, it = next(state.list, state.content)
  79. return it
  80. end
  81. return iterator, { list = x }
  82. end
  83. function keys (x)
  84. assert(type(x)=='table', 'keys() expects a table')
  85. local function iterator (state)
  86. local it = next(state.list, state.content)
  87. state.content = it
  88. return it
  89. end
  90. return iterator, { list = x }
  91. end