1if not modules then modules = { } end modules ['libs-imp-mujs'] = {
2 version = 1.001,
3 comment = "companion to luat-imp-mujs.mkxl",
4 author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5 copyright = "PRAGMA ADE / ConTeXt Development Team",
6 license = "see context related readme files"
7}
8
9
10
11
12
13
14
15local libname = "mujs"
16local libfile = "libmujs"
17
18if package.loaded[libname] then
19 return package.loaded[libname]
20end
21
22local mujslib = resolvers.libraries.validoptional(libname)
23
24if not mujslib then
25 return
26end
27
28local files = { }
29local openfile = io.open
30local findfile = resolvers.findfile
31
32local mujs_execute = mujslib.execute
33local mujs_dofile = mujslib.dofile
34local mujs_reset = mujslib.reset
35
36local function okay()
37 if resolvers.libraries.optionalloaded(libname,libfile) then
38 mujs_execute(
39 "var catcodes = { " ..
40 "'tex': " .. tex.texcatcodes .. "," ..
41 "'ctx': " .. tex.ctxcatcodes .. "," ..
42 "'prt': " .. tex.prtcatcodes .. "," ..
43 "'vrb': " .. tex.vrbcatcodes .. "," ..
44 "};"
45 )
46 okay = function() return true end
47 else
48 okay = function() return false end
49 end
50 return okay()
51end
52
53mujslib.setfindfile(findfile)
54
55mujslib.setopenfile(function(name)
56 local full = findfile(name)
57 if full then
58 local f = openfile(full,"rb")
59 if f then
60 for i=1,100 do
61 if not files[i] then
62 files[i] = f
63 return i
64 end
65 end
66 end
67 end
68end)
69
70mujslib.setclosefile(function(id)
71 local f = files[id]
72 if f then
73 f:close()
74 files[id] = false
75 end
76end)
77
78mujslib.setreadfile(function(id,how)
79 local f = files[id]
80 if f then
81 return (f:read(how or "*l"))
82 end
83end)
84
85mujslib.setseekfile(function(id,whence,offset)
86 local f = files[id]
87 if f then
88 return (f:seek(whence,offset))
89 end
90end)
91
92local reporters = {
93 console = logs.reporter("mujs","console"),
94 report = logs.reporter("mujs","report"),
95}
96
97mujslib.setconsole(function(category,name)
98 reporters[category](name)
99end)
100
101local mujs = {
102 ["execute"] = function(c,s) if okay() then mujs_execute(c,s) end end,
103 ["dofile"] = function(n) if okay() then mujs_dofile(n) end end,
104 ["reset"] = function(n) if okay() then mujs_reset(n) end end,
105}
106
107package.loaded[libname] = mujs
108
109optional.loaded.mujs = mujs
110
111interfaces.implement {
112 name = "ecmacode",
113 actions = mujs.execute,
114 arguments = "string",
115 public = true,
116}
117
118interfaces.implement {
119 name = "ecmafile",
120 actions = mujs.dofile,
121 arguments = "string",
122 public = true,
123 protected = true,
124}
125
126return mujs
127 |