aboutsummaryrefslogtreecommitdiff
path: root/importer.lua
blob: 9b2c936b6da369ee03388218a1bc83dc4d461ef4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
local import_to_1_from
import_to_1_from = function(host, to_import)
  do
    local host_mt = getmetatable(host)
    if host_mt then
      if host_mt.__import then
        host_mt.__import(host, to_import)
        return 
      end
    end
  end
  for k, v in pairs(to_import) do
    host[k] = v
  end
end
local _imports = setmetatable({ }, {
  __mode = "k"
})
local Importer = setmetatable({
  __index = function(self, key)
    return _imports[self][key]
  end,
  __import = function(self, to_import)
    local imports = assert(_imports[self])
    for k, v in pairs(to_import) do
      local _continue_0 = false
      repeat
        imports[k] = v
        if v == to_import then
          _continue_0 = true
          break
        end
        local conflict = self[k]
        if type(conflict) == 'table' then
          import_to_1_from(conflict, v)
        end
        _continue_0 = true
      until true
      if not _continue_0 then
        break
      end
    end
  end
}, {
  __call = function(self, t)
    _imports[t] = { }
    setmetatable(t, self)
    return t
  end
})
local _1_forked
_1_forked = function(self)
  local f = Importer({ })
  _imports[f] = assert(_imports[self])
  import_to_1_from(f, self)
  return f
end
return {
  Importer = Importer,
  import_to_1_from = import_to_1_from,
  _1_forked = _1_forked
}