github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/runtime/lua/metamethods.lua (about)

     1  local meta = {}
     2  local function w(x)
     3      local t = {x=x}
     4      setmetatable(t, meta)
     5      return t
     6  end
     7  
     8  function meta.__tostring(x) return "<" .. x.x .. ">" end
     9  
    10  function meta.__add(x, y) return w(x.x + y.x) end
    11  function meta.__sub(x, y) return w(x.x - y.x) end
    12  function meta.__mul(x, y) return w(x.x * y.x) end
    13  function meta.__div(x, y) return w(x.x / y.x) end
    14  function meta.__mod(x, y) return w(x.x % y.x) end
    15  function meta.__pow(x, y) return w(x.x ^ y.x) end
    16  function meta.__idiv(x, y) return w(x.x // y.x) end
    17  
    18  function meta.__unm(x) return w(-x.x) end
    19  
    20  function meta.__band(x, y) return w(x.x & y.x) end
    21  function meta.__bor(x, y) return w(x.x | y.x) end
    22  function meta.__bxor(x, y) return w(x.x ~ y.x) end
    23  function meta.__shl(x, y) return w(x.x << y.x) end
    24  function meta.__shr(x, y) return w(x.x >> y.x) end
    25  
    26  function meta.__bnot(x) return w(~x.x) end
    27  
    28  function meta.__eq(x, y) return x.x == y.x end
    29  function meta.__lt(x, y) return x.x < y.x end
    30  function meta.__le(x, y) return x.x <= y.x end
    31  
    32  print(w(1))
    33  --> =<1>
    34  
    35  print(w(2)+w(3))
    36  --> =<5>
    37  
    38  print(w(10)*w(20))
    39  --> =<200>
    40  
    41  print((w(10) - w(2)) ^ w(2))
    42  --> =<64>
    43  
    44  print(w(51) // w(2) % w(7))
    45  --> =<4>
    46  
    47  print(-w(1))
    48  --> =<-1>
    49  
    50  print(w(1) << w(4) == w(2) << w(3))
    51  --> =true
    52  
    53  print(w(8) >> w(3) == w(1))
    54  --> =true
    55  
    56  print(w(~10) == ~w(10))
    57  --> =true
    58  
    59  print(w(8) | w(4))
    60  --> =<12>
    61  
    62  print(w(3) & w(5))
    63  --> =<1>
    64  
    65  print(w(10) ~ w(9))
    66  --> =<3>
    67  
    68  print(w(1) < w(2), w(3) <= w(3), w(4) > w(3), w(6) >= w(1))
    69  --> =true	true	true	true
    70  
    71  print(w(1) > w(2), w(3) >= w(3), w(4) < w(3), w(6) <= w(1))
    72  --> =false	true	false	false
    73  
    74  local tbl = {x=5}
    75  local m = {__index={x=1, y=2}}
    76  setmetatable(tbl, m)
    77  
    78  print(tbl.x, tbl.y, tbl.z)
    79  --> =5	2	nil
    80  
    81  function m.__index(t, n) return "[" .. tostring(n) .. "]" end
    82  
    83  print(tbl.x, tbl.y, tbl.z)
    84  --> =5	[y]	[z]
    85  
    86  function m.__newindex(t, k, v) rawset(t, k, "new:" .. tostring(v)) end
    87  
    88  tbl.x = 44
    89  tbl.y = 11
    90  print(tbl.x, tbl.y)
    91  --> =44	new:11
    92  
    93  tbl.y = 33
    94  print(tbl.y)
    95  --> =33
    96  
    97  local indexTbl = {}
    98  m.__newindex = indexTbl
    99  
   100  tbl.a = "hello"
   101  print(indexTbl.a)
   102  --> =hello
   103  
   104  tbl.a = "bye"
   105  print(indexTbl.a)
   106  --> =bye
   107  
   108  function m.__concat(x, y) return "..." end
   109  print(tbl .. 1, 1 .. tbl)
   110  --> =...	...
   111  
   112  print(pcall(function() return tbl(1, 2) end))
   113  --> ~false\t.*attempt to call a table.*
   114  
   115  function m.__call(t, x, y) return "call(" .. tostring(x) .. "," .. tostring(y) .. ")" end
   116  print(tbl(1, 2))
   117  --> =call(1,2)
   118  
   119  do
   120      local t = {}
   121      setmetatable(t, {__index=t, __newindex=t})
   122      print(pcall(function() return t[1] end))
   123      --> ~^false\t.*chain too long
   124  
   125      print(pcall(function() t[1] = 2 end))
   126      --> ~^false\t.*chain too long
   127  end
   128  
   129  do
   130      local t = {}
   131      setmetatable(t, {
   132          __index = function(t, x) error(x) end,
   133          __newindex = function(t, x) error(x) end,
   134      })
   135      print(pcall(function() return t.booo end))
   136      --> ~false\t.* booo
   137      print(pcall(function() t.arghh = 42 end))
   138      --> ~false\t.* arghh
   139  end