# HG changeset patch # User Matthew Wild # Date 1327261733 0 # Node ID 3cbfa768eb06a5782732161f693bb641a7716833 # Parent d0a89c1c43fd0321ac778877605afcd4133aaa4b moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules diff -r d0a89c1c43fd -r 3cbfa768eb06 core/moduleapi.lua --- a/core/moduleapi.lua Sun Jan 22 19:35:50 2012 +0000 +++ b/core/moduleapi.lua Sun Jan 22 19:48:53 2012 +0000 @@ -129,6 +129,29 @@ return mod; end +-- Returns one or more shared tables at the specified virtual paths +-- Intentionally does not allow the table at a path to be _set_, it +-- is auto-created if it does not exist. +function api:shared(...) + local paths = { n = select("#", ...), ... }; + local data_array = {}; + local default_path_components = { self.host, self.name }; + for i = 1, paths.n do + local path = paths[i]; + if path:sub(1,1) ~= "/" then -- Prepend default components + local n_components = select(2, path:gsub("/", "%1")); + path = (n_components<#default_path_components and "/" or "")..t_concat(default_path_components, "/", 1, #default_path_components-n_components).."/"..path; + end + local shared = shared_data[path]; + if not shared then + shared = {}; + shared_data[path] = shared; + end + t_insert(data_array, shared); + end + return unpack(data_array); +end + function api:get_option(name, default_value) local value = config.get(self.host, self.name, name); if value == nil then