revolucas

proxy

Aug 24th, 2017
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.81 KB | None | 0 0
  1. function Node()
  2.     local node = {}
  3.    
  4.     local proxy = {}
  5.     setmetatable(proxy,{__newindex = function(t,k,v)
  6.             rawset(node,k,v)
  7.             if (type(v) == "table") then
  8.                 node[k].__parent = node
  9.             end
  10.         end,
  11.         __index = function(t,k,v)
  12.             return rawget(node,k) or rawget(_G,k)
  13.         end,
  14.         __pairs = function(t)
  15.             local function stateless_iter(t, k)
  16.                 local v
  17.                 k, v = next(node, k)
  18.                 if v then return k,v end
  19.             end
  20.             return stateless_iter, node, nil
  21.         end,
  22.         __ipairs = function(t)
  23.             local function stateless_iter(t, i)
  24.                 i = i + 1
  25.                 local v = node[i]
  26.                 if v then return i, v end
  27.             end
  28.             return stateless_iter, node, 0
  29.         end
  30.     })
  31.    
  32.     return proxy
  33. end
  34.  
  35. local top = Node()
  36. top.child = Node()
  37. top.child.child2 = Node()
  38. top.child.child2.child3 = Node()
  39. top.child.child2.child3 = Node()
Advertisement
Add Comment
Please, Sign In to add comment