implemented import system for modules

This commit is contained in:
Manuel Simon Hirsig
2016-08-19 17:09:44 +02:00
parent 37e789184b
commit e334a444df
4 changed files with 42 additions and 24 deletions
+2 -2
View File
@@ -25,8 +25,8 @@ function UPDATE()
end end
end end
m.listeners = aux.util.filter(m.listeners, function(l) return not l.killed end) m.listeners = filter(m.listeners, function(l) return not l.killed end)
m.threads = aux.util.filter(m.threads, function(th) return not th.killed end) m.threads = filter(m.threads, function(th) return not th.killed end)
for thread_id, thread in m.threads do for thread_id, thread in m.threads do
if not thread.killed then if not thread.killed then
+3 -2
View File
@@ -1,4 +1,4 @@
setglobal('aux', aux_module()) setglobal('aux', aux_module('core'))
public.version = '4.0.0' public.version = '4.0.0'
@@ -15,7 +15,8 @@ function public.module(path)
local qualified_name = prefix and prefix..'.'..name or name local qualified_name = prefix and prefix..'.'..name or name
env = module_envs[qualified_name] env = module_envs[qualified_name]
if not env then if not env then
(prefix and module_envs[prefix].public or public)[name], env = (function() return aux_module(), getfenv() end)() (prefix and module_envs[prefix].public or public)[name], env = (function() return aux_module(qualified_name), getfenv() end)()
env.import 'util'
env.LOAD = nil env.LOAD = nil
module_envs[qualified_name] = env module_envs[qualified_name] = env
end end
+29 -12
View File
@@ -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 DECLARED, ACCESSOR, MUTABLE, PUBLIC = 1, 2, 4, 8
local ACCESSOR_KEY, MUTABLE_KEY, PUBLIC_KEY = 'accessor', 'mutable', 'public' local ACCESSOR_KEY, MUTABLE_KEY, PUBLIC_KEY = 'accessor', 'mutable', 'public'
local PROPERTY = {[ACCESSOR_KEY]=ACCESSOR, [MUTABLE_KEY]=MUTABLE, [PUBLIC_KEY]=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 = { local metadata_mt = {
__index=function() return 0 end, __index=function() return 0 end,
__newindex=function(self, key, value) __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) rawset(self, key, value)
end, end,
} }
@@ -20,6 +19,12 @@ local env_mt = {
elseif mask(ACCESSOR, properties) == ACCESSOR then elseif mask(ACCESSOR, properties) == ACCESSOR then
return value(key) return value(key)
else 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) return g[key] or error('No key "'..key..'".', 2)
end end
end, end,
@@ -39,7 +44,7 @@ local interface_mt = {
local value, properties = _data[self][key], _metadata[self][key] local value, properties = _data[self][key], _metadata[self][key]
if mask(ACCESSOR+PUBLIC, properties) == PUBLIC then if mask(ACCESSOR+PUBLIC, properties) == PUBLIC then
return value return value
elseif mask(PUBLIC, properties) == PUBLIC then elseif mask(PUBLIC, properties) ~= 0 then
return value(key) return value(key)
else else
error('No key "'..key..'".', 2) error('No key "'..key..'".', 2)
@@ -61,17 +66,29 @@ local modifier_mt = {
_metadata[self][key] = _modifier_properties[self] _metadata[self][key] = _modifier_properties[self]
end, end,
} }
function g.aux_module() local importer_mt = {
local data, metadata, modifier, env, interface __call = function(self, ...)
modifier = setmetatable({}, modifier_mt) env = setmetatable({}, env_mt) interface = setmetatable({}, interface_mt) for i=1,arg.n do
local function modifier_accessor(key) _modifier_properties[modifier] = DECLARED+PROPERTY[key] return modifier end tinsert(_imports[self], arg[i])
data = {g=g, m=env, [ACCESSOR_KEY]=modifier_accessor, [MUTABLE_KEY]=modifier_accessor, [PUBLIC_KEY]=modifier_accessor} end
metadata = setmetatable({g=DECLARED, m=DECLARED, [ACCESSOR_KEY]=ACCESSOR, [MUTABLE_KEY]=ACCESSOR, [PUBLIC_KEY]=ACCESSOR}, metadata_mt) 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 _data[modifier], _data[env], _data[interface] = data, data, data
_metadata[modifier], _metadata[env], _metadata[interface] = metadata, metadata, metadata _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) setfenv(2, env)
return interface return interface
+8 -8
View File
@@ -54,7 +54,7 @@ public.search_config = {
{ {
title = 'Item', title = 'Item',
width = 0.35, width = 0.35,
init = m.item_column_init, init = item_column_init,
set = function(cell, record, _, _, _, indented) set = function(cell, record, _, _, _, indented)
cell.icon:SetTexture(record.texture) cell.icon:SetTexture(record.texture)
if indented then if indented then
@@ -209,12 +209,12 @@ public.search_config = {
width = 0.08, width = 0.08,
align = 'CENTER', align = 'CENTER',
set = function(cell, record) set = function(cell, record)
local pct, bidPct = m.record_percentage(record) local pct, bidPct = record_percentage(record)
cell:SetText((pct or bidPct) and m.percentage_historical(pct or bidPct, not pct) or '---') cell:SetText((pct or bidPct) and percentage_historical(pct or bidPct, not pct) or '---')
end, end,
cmp = function(record_a, record_b, desc) cmp = function(record_a, record_b, desc)
local pct_a = m.record_percentage(record_a) or (desc and -aux.huge or aux.huge) local pct_a = record_percentage(record_a) or (desc and -aux.huge or aux.huge)
local pct_b = m.record_percentage(record_b) or (desc and -aux.huge or aux.huge) local pct_b = record_percentage(record_b) or (desc and -aux.huge or aux.huge)
return aux.sorting.compare(pct_a, pct_b, desc) return aux.sorting.compare(pct_a, pct_b, desc)
end, end,
}, },
@@ -224,7 +224,7 @@ public.auctions_config = {
{ {
title = 'Item', title = 'Item',
width = 0.35, width = 0.35,
init = m.item_column_init, init = item_column_init,
set = function(cell, record, _, _, _, indented) set = function(cell, record, _, _, _, indented)
cell.icon:SetTexture(record.texture) cell.icon:SetTexture(record.texture)
if indented then if indented then
@@ -371,7 +371,7 @@ public.bids_config = {
{ {
title = 'Item', title = 'Item',
width = 0.35, width = 0.35,
init = m.item_column_init, init = item_column_init,
set = function(cell, record, _, _, _, indented) set = function(cell, record, _, _, _, indented)
cell.icon:SetTexture(record.texture) cell.icon:SetTexture(record.texture)
if indented then if indented then
@@ -539,7 +539,7 @@ function percentage_color(pct)
end end
function public.percentage_historical(pct, bid) function public.percentage_historical(pct, bid)
return (bid and aux.gui.inline_color.gray or m.percentage_color(pct))..(pct > 10000 and '>10000' or pct)..'%'..FONT_COLOR_CODE_CLOSE return (bid and aux.gui.inline_color.gray or percentage_color(pct))..(pct > 10000 and '>10000' or pct)..'%'..FONT_COLOR_CODE_CLOSE
end end
function public.time_left(code) function public.time_left(code)