aboutsummaryrefslogtreecommitdiff
path: root/importer.moon
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2018-11-17 14:38:05 -0800
committerBruce Hill <bruce@bruce-hill.com>2018-11-17 14:39:08 -0800
commit7f47d4204039258cec78c767f489b7809b4257ff (patch)
treec8533068b75ab453accfe1f688705e9e94c9e279 /importer.moon
parent34a3dd22a4e132bd4e0fe3ce89831c3fe761d3d9 (diff)
In-progress (but working) overhaul of some elements including: function
calls, lib/thing.nom API, multi-assignments, varargs, etc.
Diffstat (limited to 'importer.moon')
-rw-r--r--importer.moon26
1 files changed, 20 insertions, 6 deletions
diff --git a/importer.moon b/importer.moon
index 48a3800..26b78f7 100644
--- a/importer.moon
+++ b/importer.moon
@@ -1,22 +1,36 @@
-- This file defines Importer, which is a type of table that can import from other tables
-import_to_1_from = (host, to_import)->
+import_to_1_from = (host, to_import, prefix=nil)->
if host_mt = getmetatable(host)
if host_mt.__import
- host_mt.__import(host, to_import)
+ host_mt.__import(host, to_import, prefix)
return
for k,v in pairs(to_import)
+ if k == to_import then k = host
+ if v == to_import then v = host
+ if prefix and type(k) == 'string'
+ --print "PREFIXING #{k} -> #{prefix..k}"
+ k = prefix..k
+ --print("IMPORTED (#{k})")
host[k] = v
_imports = setmetatable({}, {__mode:"k"})
Importer = setmetatable({
__index: (key)=> _imports[@][key]
- __import: (to_import)=>
+ __import: (to_import, prefix=nil)=>
imports = assert _imports[@]
for k,v in pairs(to_import)
+ if prefix and type(k) == 'string'
+ k = prefix..k
+ --print("IMPORTED (#{k})")
imports[k] = v
continue if v == to_import
conflict = @[k]
- import_to_1_from(conflict, v) if type(conflict) == 'table'
+ if conflict_mt = getmetatable(host)
+ if conflict_mt.__import
+ conflict_mt.__import(conflict, v, prefix)
+ --__newindex: (k,v)=>
+ -- print("DEFINED (#{k})")
+ -- rawset(@, k, v)
}, {
__call: (t)=>
_imports[t] = {}
@@ -24,8 +38,8 @@ Importer = setmetatable({
return t
})
-_1_forked = =>
- f = Importer{}
+_1_forked = (t)=>
+ f = Importer(t or {})
_imports[f] = assert _imports[@]
import_to_1_from(f, @)
return f