From e334a444dff419f290704e3aa654d4f5519c2b45 Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Fri, 19 Aug 2016 17:09:44 +0200 Subject: [PATCH] implemented import system for modules --- control.lua | 4 ++-- init.lua | 5 +++-- module.lua | 41 +++++++++++++++++++++++++++----------- tables/auction_listing.lua | 16 +++++++-------- 4 files changed, 42 insertions(+), 24 deletions(-) diff --git a/control.lua b/control.lua index eb6230b..2032c23 100644 --- a/control.lua +++ b/control.lua @@ -25,8 +25,8 @@ function UPDATE() end end - m.listeners = aux.util.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.listeners = filter(m.listeners, function(l) return not l.killed end) + m.threads = filter(m.threads, function(th) return not th.killed end) for thread_id, thread in m.threads do if not thread.killed then diff --git a/init.lua b/init.lua index 2805bab..26ca646 100644 --- a/init.lua +++ b/init.lua @@ -1,4 +1,4 @@ -setglobal('aux', aux_module()) +setglobal('aux', aux_module('core')) public.version = '4.0.0' @@ -15,7 +15,8 @@ function public.module(path) local qualified_name = prefix and prefix..'.'..name or name env = module_envs[qualified_name] 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 module_envs[qualified_name] = env end diff --git a/module.lua b/module.lua index add6a7c..04e5733 100644 --- a/module.lua +++ b/module.lua @@ -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 diff --git a/tables/auction_listing.lua b/tables/auction_listing.lua index 5ffb442..2b3f9eb 100644 --- a/tables/auction_listing.lua +++ b/tables/auction_listing.lua @@ -54,7 +54,7 @@ public.search_config = { { title = 'Item', width = 0.35, - init = m.item_column_init, + init = item_column_init, set = function(cell, record, _, _, _, indented) cell.icon:SetTexture(record.texture) if indented then @@ -209,12 +209,12 @@ public.search_config = { width = 0.08, align = 'CENTER', set = function(cell, record) - local pct, bidPct = m.record_percentage(record) - cell:SetText((pct or bidPct) and m.percentage_historical(pct or bidPct, not pct) or '---') + local pct, bidPct = record_percentage(record) + cell:SetText((pct or bidPct) and percentage_historical(pct or bidPct, not pct) or '---') end, 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_b = m.record_percentage(record_b) 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 = record_percentage(record_b) or (desc and -aux.huge or aux.huge) return aux.sorting.compare(pct_a, pct_b, desc) end, }, @@ -224,7 +224,7 @@ public.auctions_config = { { title = 'Item', width = 0.35, - init = m.item_column_init, + init = item_column_init, set = function(cell, record, _, _, _, indented) cell.icon:SetTexture(record.texture) if indented then @@ -371,7 +371,7 @@ public.bids_config = { { title = 'Item', width = 0.35, - init = m.item_column_init, + init = item_column_init, set = function(cell, record, _, _, _, indented) cell.icon:SetTexture(record.texture) if indented then @@ -539,7 +539,7 @@ function percentage_color(pct) end 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 function public.time_left(code)