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
|
if not modules then modules = { } end modules ['libs-imp-mujs'] = {
version = 1.001,
comment = "companion to luat-imp-mujs.mkxl",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
-- This is an experiment. When a new user knows \JAVASCRIPT\ it can be a
-- stepping stone to using \LUA.
-- local ecmascript = optional.mujs.initialize("libmujs")
-- local execute = optional.mujs.execute
local libname = "mujs"
local libfile = "libmujs"
if package.loaded[libname] then
return package.loaded[libname]
end
local mujslib = resolvers.libraries.validoptional(libname)
if not mujslib then
return
end
local files = { }
local openfile = io.open
local findfile = resolvers.findfile
local mujs_execute = mujslib.execute
local mujs_dofile = mujslib.dofile
local mujs_reset = mujslib.reset
local function okay()
if resolvers.libraries.optionalloaded(libname,libfile) then
mujs_execute(
"var catcodes = { " ..
"'tex': " .. tex.texcatcodes .. "," ..
"'ctx': " .. tex.ctxcatcodes .. "," ..
"'prt': " .. tex.prtcatcodes .. "," ..
"'vrb': " .. tex.vrbcatcodes .. "," ..
"};"
)
okay = function() return true end
else
okay = function() return false end
end
return okay()
end
mujslib.setfindfile(findfile)
mujslib.setopenfile(function(name)
local full = findfile(name)
if full then
local f = openfile(full,"rb")
if f then
for i=1,100 do
if not files[i] then
files[i] = f
return i
end
end
end
end
end)
mujslib.setclosefile(function(id)
local f = files[id]
if f then
f:close()
files[id] = false
end
end)
mujslib.setreadfile(function(id,how)
local f = files[id]
if f then
return (f:read(how or "*l"))
end
end)
mujslib.setseekfile(function(id,whence,offset)
local f = files[id]
if f then
return (f:seek(whence,offset))
end
end)
local reporters = {
console = logs.reporter("mujs","console"),
report = logs.reporter("mujs","report"),
}
mujslib.setconsole(function(category,name)
reporters[category](name)
end)
local mujs = {
["execute"] = function(c,s) if okay() then mujs_execute(c,s) end end,
["dofile"] = function(n) if okay() then mujs_dofile(n) end end,
["reset"] = function(n) if okay() then mujs_reset(n) end end,
}
package.loaded[libname] = mujs
optional.loaded.mujs = mujs
interfaces.implement {
name = "ecmacode",
actions = mujs.execute,
arguments = "string",
public = true,
}
interfaces.implement {
name = "ecmafile",
actions = mujs.dofile,
arguments = "string",
public = true,
protected = true,
}
return mujs
|