summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/luat-ini.lua
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/tex/context/base/luat-ini.lua')
-rw-r--r--Master/texmf-dist/tex/context/base/luat-ini.lua267
1 files changed, 142 insertions, 125 deletions
diff --git a/Master/texmf-dist/tex/context/base/luat-ini.lua b/Master/texmf-dist/tex/context/base/luat-ini.lua
index 204cc7bd175..587214b93a1 100644
--- a/Master/texmf-dist/tex/context/base/luat-ini.lua
+++ b/Master/texmf-dist/tex/context/base/luat-ini.lua
@@ -6,16 +6,16 @@ if not modules then modules = { } end modules ['luat-ini'] = {
license = "see context related readme files"
}
--- rather experimental down here ... will change with lua 5.2 --
+-- rather experimental down here ... adapted to lua 5.2 ... but still
+-- experimental
-local debug = require "debug"
-local string, table, lpeg, math, io, system = string, table, lpeg, math, io, system
-local next, setfenv = next, setfenv or debug.setfenv
+local debug = require("debug")
-local mark = utilities.storage.mark
+local string, table, lpeg, math, io, system = string, table, lpeg, math, io, system
+local rawset, rawget, next, setmetatable = rawset, rawget, next, setmetatable
--[[ldx--
-<p>We cannot load anything yet. However what we will do us reserve a fewtables.
+<p>We cannot load anything yet. However what we will do us reserve a few tables.
These can be used for runtime user data or third party modules and will not be
cluttered by macro package code.</p>
--ldx]]--
@@ -26,14 +26,8 @@ moduledata = moduledata or { } -- only for development team
documentdata = documentdata or { } -- for users (e.g. raw data)
parametersets = parametersets or { } -- experimental for team
-document = document or { } -- only for context itself
-
---[[ldx--
-<p>These can be used/set by the caller program; <t>mtx-context.lua</t> does it.</p>
---ldx]]--
-
-document.arguments = mark(document.arguments or { })
-document.files = mark(document.files or { })
+table.setmetatableindex(moduledata,table.autokey)
+table.setmetatableindex(thirddata, table.autokey)
--[[ldx--
<p>Please create a namespace within these tables before using them!</p>
@@ -45,145 +39,168 @@ thirddata['tricks' ] = { }
--ldx]]--
--[[ldx--
-<p>We could cook up a readonly model for global tables but it
-makes more sense to invite users to use one of the predefined
-namespaces. One can redefine the protector. After all, it's
-just a lightweight suggestive system, not a watertight
-one.</p>
+<p>We could cook up a readonly model for global tables but it makes more sense
+to invite users to use one of the predefined namespaces. One can redefine the
+protector. After all, it's just a lightweight suggestive system, not a
+watertight one.</p>
--ldx]]--
--- this will change when we move on to lua 5.2+
-
-local global = _G
-
+local global = _G
global.global = global
---~ rawset(global,"global",global)
local dummy = function() end
--- another approach is to freeze tables by using a metatable, this will be
--- implemented stepwise
-
-local protected = {
- -- global table
- global = global,
- -- user tables
- -- moduledata = moduledata,
- userdata = userdata,
- thirddata = thirddata,
- documentdata = documentdata,
- -- reserved
- protect = dummy,
- unprotect = dummy,
- -- luatex
- tex = tex,
- -- lua
- string = string,
- table = table,
- lpeg = lpeg,
- math = math,
- io = io,
- --
- -- maybe other l-*, xml etc
-}
+--[[ldx--
+<p>Another approach is to freeze tables by using a metatable, this will be
+implemented stepwise.</p>
+--ldx]]--
-- moduledata : no need for protection (only for developers)
-- isolatedata : full protection
-- userdata : protected
-- thirddata : protected
-userdata, thirddata = nil, nil
-
--- we could have a metatable that automaticaly creates a top level namespace
-
-if not setfenv then
- texio.write_nl("warning: we need to fix setfenv by using 'load in' or '_ENV'")
-end
-
-local function protect_full(name)
- local t = { }
- for k, v in next, protected do
- t[k] = v
- end
- return t
-end
+--[[ldx--
+<p>We could have a metatable that automaticaly creates a top level namespace.</p>
+--ldx]]--
-local function protect_part(name)
---~ local t = global[name]
- local t = rawget(global,name)
- if not t then
- t = { }
+local luanames = lua.name -- luatex itself
+
+lua.numbers = lua.numbers or { } local numbers = lua.numbers
+lua.messages = lua.messages or { } local messages = lua.messages
+
+storage.register("lua/numbers", numbers, "lua.numbers" )
+storage.register("lua/messages", messages, "lua.messages")
+
+local setfenv = setfenv or debug.setfenv -- < 5.2
+
+if setfenv then
+
+ local protected = {
+ -- global table
+ global = global,
+ -- user tables
+ -- moduledata = moduledata,
+ userdata = userdata,
+ thirddata = thirddata,
+ documentdata = documentdata,
+ -- reserved
+ protect = dummy,
+ unprotect = dummy,
+ -- luatex
+ tex = tex,
+ -- lua
+ string = string,
+ table = table,
+ lpeg = lpeg,
+ math = math,
+ io = io,
+ file = file,
+ bit32 = bit32,
+ --
+ context = context,
+ }
+
+ local protect_full = function(name)
+ local t = { }
for k, v in next, protected do
t[k] = v
end
---~ global[name] = t
- rawset(global,name,t)
+ return t
end
- return t
-end
-function protect(name)
- if name == "isolateddata" then
- setfenv(2,protect_full(name))
- else
- setfenv(2,protect_part(name or "shareddata"))
+ local protect_part = function(name) -- adds
+ local t = rawget(global,name)
+ if not t then
+ t = { }
+ for k, v in next, protected do
+ t[k] = v
+ end
+ rawset(global,name,t)
+ end
+ return t
end
-end
-
-lua.numbers = { }
-lua.messages = { }
-function lua.registername(name,message)
- local lnn = lua.numbers[name]
- if not lnn then
- lnn = #lua.messages + 1
- lua.messages[lnn] = message
- lua.numbers[name] = lnn
- end
- lua.name[lnn] = message
- context(lnn)
- -- initialize once
- if name ~= "isolateddata" then
- protect_full(name or "shareddata")
+ protect = function(name)
+ if name == "isolateddata" then
+ setfenv(2,protect_full(name))
+ else
+ setfenv(2,protect_part(name or "shareddata"))
+ end
end
-end
---~ function lua.checknames()
---~ lua.name[0] = "ctx"
---~ for k, v in next, lua.messages do
---~ lua.name[k] = v
---~ end
---~ end
+ function lua.registername(name,message)
+ local lnn = lua.numbers[name]
+ if not lnn then
+ lnn = #messages + 1
+ messages[lnn] = message
+ numbers[name] = lnn
+ end
+ luanames[lnn] = message
+ context(lnn)
+ -- initialize once
+ if name ~= "isolateddata" then
+ protect_full(name or "shareddata")
+ end
+ end
-storage.register("lua/numbers", lua.numbers, "lua.numbers")
-storage.register("lua/messages", lua.messages, "lua.messages")
+elseif libraries then -- assume >= 5.2
+
+ local shared
+
+ protect = function(name)
+ if not shared then
+ -- e.g. context is not yet known
+ local public = {
+ global = global,
+ -- moduledata = moduledata,
+ userdata = userdata,
+ thirddata = thirddata,
+ documentdata = documentdata,
+ protect = dummy,
+ unprotect = dummy,
+ context = context,
+ }
+ --
+ for k, v in next, libraries.builtin do public[k] = v end
+ for k, v in next, libraries.functions do public[k] = v end
+ for k, v in next, libraries.obsolete do public[k] = nil end
+ --
+ shared = { __index = public }
+ protect = function(name)
+ local t = global[name] or { }
+ setmetatable(t,shared) -- set each time
+ return t
+ end
+ end
+ return protect(name)
+ end
---~ local arguments, files = document.arguments, document.files -- set later
+ function lua.registername(name,message)
+ local lnn = lua.numbers[name]
+ if not lnn then
+ lnn = #messages + 1
+ messages[lnn] = message
+ numbers[name] = lnn
+ end
+ luanames[lnn] = message
+ context(lnn)
+ end
-function document.setargument(key,value)
- document.arguments[key] = value
-end
+else
-function document.setdefaultargument(key,default)
- local v = document.arguments[key]
- if v == nil or v == "" then
- document.arguments[key] = default
- end
-end
+ protect = dummy
-function document.getargument(key,default)
- local v = document.arguments[key]
- if type(v) == "boolean" then
- v = (v and "yes") or "no"
- document.arguments[key] = v
+ function lua.registername(name,message)
+ local lnn = lua.numbers[name]
+ if not lnn then
+ lnn = #messages + 1
+ messages[lnn] = message
+ numbers[name] = lnn
+ end
+ luanames[lnn] = message
+ context(lnn)
end
- context(v or default or "")
-end
-function document.setfilename(i,name)
- document.files[tonumber(i)] = name
end
-function document.getfilename(i)
- context(document.files[i] or "")
-end