implemented import system for modules
This commit is contained in:
+29
-12
@@ -1,13 +1,12 @@
|
||||
local setfenv, rawget, rawset, setmetatable, mask, g = setfenv, rawget, rawset, setmetatable, bit.band, getfenv(0)
|
||||
local tinsert, setfenv, rawget, rawset, setmetatable, mask, g = tinsert, setfenv, rawget, rawset, setmetatable, bit.band, getfenv(0)
|
||||
local DECLARED, ACCESSOR, MUTABLE, PUBLIC = 1, 2, 4, 8
|
||||
local ACCESSOR_KEY, MUTABLE_KEY, PUBLIC_KEY = 'accessor', 'mutable', 'public'
|
||||
local PROPERTY = {[ACCESSOR_KEY]=ACCESSOR, [MUTABLE_KEY]=MUTABLE, [PUBLIC_KEY]=PUBLIC}
|
||||
local _data, _metadata, _modifier_properties = {}, {}, {}
|
||||
|
||||
local _data, _metadata, _modifier_properties, _imports, _interfaces = {}, {}, {}, {}, {}
|
||||
local metadata_mt = {
|
||||
__index=function() return 0 end,
|
||||
__newindex=function(self, key, value)
|
||||
if rawget(self, key) then error('Duplicate key "'..key..'".', 2) end
|
||||
if rawget(self, key) then error('Duplicate key "'..key..'".', 3) end
|
||||
rawset(self, key, value)
|
||||
end,
|
||||
}
|
||||
@@ -20,6 +19,12 @@ local env_mt = {
|
||||
elseif mask(ACCESSOR, properties) == ACCESSOR then
|
||||
return value(key)
|
||||
else
|
||||
for _, name in _imports[self] do
|
||||
local interface = _interfaces[name]
|
||||
if interface and mask(PUBLIC, _metadata[interface][key]) ~= 0 then
|
||||
return interface[key]
|
||||
end
|
||||
end
|
||||
return g[key] or error('No key "'..key..'".', 2)
|
||||
end
|
||||
end,
|
||||
@@ -39,7 +44,7 @@ local interface_mt = {
|
||||
local value, properties = _data[self][key], _metadata[self][key]
|
||||
if mask(ACCESSOR+PUBLIC, properties) == PUBLIC then
|
||||
return value
|
||||
elseif mask(PUBLIC, properties) == PUBLIC then
|
||||
elseif mask(PUBLIC, properties) ~= 0 then
|
||||
return value(key)
|
||||
else
|
||||
error('No key "'..key..'".', 2)
|
||||
@@ -61,17 +66,29 @@ local modifier_mt = {
|
||||
_metadata[self][key] = _modifier_properties[self]
|
||||
end,
|
||||
}
|
||||
function g.aux_module()
|
||||
local data, metadata, modifier, env, interface
|
||||
modifier = setmetatable({}, modifier_mt) env = setmetatable({}, env_mt) interface = setmetatable({}, interface_mt)
|
||||
local function modifier_accessor(key) _modifier_properties[modifier] = DECLARED+PROPERTY[key] return modifier end
|
||||
data = {g=g, m=env, [ACCESSOR_KEY]=modifier_accessor, [MUTABLE_KEY]=modifier_accessor, [PUBLIC_KEY]=modifier_accessor}
|
||||
metadata = setmetatable({g=DECLARED, m=DECLARED, [ACCESSOR_KEY]=ACCESSOR, [MUTABLE_KEY]=ACCESSOR, [PUBLIC_KEY]=ACCESSOR}, metadata_mt)
|
||||
local importer_mt = {
|
||||
__call = function(self, ...)
|
||||
for i=1,arg.n do
|
||||
tinsert(_imports[self], arg[i])
|
||||
end
|
||||
end,
|
||||
}
|
||||
function g.aux_module(name)
|
||||
if _interfaces[name] then error('Duplicate module "'..name..'".', 2) end
|
||||
|
||||
local data, metadata, imports, importer, modifier, env, interface, modifier_accessor
|
||||
modifier, importer, env, interface = setmetatable({}, modifier_mt), setmetatable({}, importer_mt), setmetatable({}, env_mt), setmetatable({}, interface_mt)
|
||||
function modifier_accessor(key) _modifier_properties[modifier] = DECLARED+PROPERTY[key] return modifier end
|
||||
data = {g=g, m=env, import=importer, [ACCESSOR_KEY]=modifier_accessor, [MUTABLE_KEY]=modifier_accessor, [PUBLIC_KEY]=modifier_accessor}
|
||||
metadata = setmetatable({g=DECLARED, m=DECLARED, import=DECLARED, [ACCESSOR_KEY]=ACCESSOR, [MUTABLE_KEY]=ACCESSOR, [PUBLIC_KEY]=ACCESSOR}, metadata_mt)
|
||||
imports = {}
|
||||
|
||||
_interfaces[name] = interface
|
||||
_data[modifier], _data[env], _data[interface] = data, data, data
|
||||
_metadata[modifier], _metadata[env], _metadata[interface] = metadata, metadata, metadata
|
||||
_imports[importer], _imports[env] = imports, imports
|
||||
|
||||
env.mutable.__ = nil
|
||||
env.mutable.__ = nil -- TODO
|
||||
|
||||
setfenv(2, env)
|
||||
return interface
|
||||
|
||||
Reference in New Issue
Block a user