1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
if not modules then modules = { } end modules ['luat-ini'] = {
version = 1.001,
comment = "companion to luat-lib.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
-- 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 rawset, rawget, next, setmetatable = rawset, rawget, next, setmetatable
--[[ldx--
<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]]--
userdata = userdata or { } -- for users (e.g. functions etc)
thirddata = thirddata or { } -- only for third party modules
moduledata = moduledata or { } -- only for development team
documentdata = documentdata or { } -- for users (e.g. raw data)
parametersets = parametersets or { } -- experimental for team
table.setmetatableindex(moduledata,table.autokey)
table.setmetatableindex(thirddata, table.autokey)
--[[ldx--
<p>Please create a namespace within these tables before using them!</p>
<typing>
userdata ['my.name'] = { }
thirddata['tricks' ] = { }
</typing>
--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>
--ldx]]--
local global = _G
global.global = global
local dummy = function() end
--[[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
--[[ldx--
<p>We could have a metatable that automaticaly creates a top level namespace.</p>
--ldx]]--
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 f_message = string.formatters["=[instance: %s]"] -- the = controls the lua error / see: lobject.c
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
return t
end
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
protect = function(name)
if name == "isolateddata" then
setfenv(2,protect_full(name))
else
setfenv(2,protect_part(name or "shareddata"))
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
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
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] = f_message(message)
context(lnn)
end
else
protect = dummy
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] = f_message(message)
context(lnn)
end
end
|