libs-imp-mujs.lua /size: 2988 b    last modification: 2020-07-01 14:35
1
if
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
-- This is an experiment. When a new user knows \JAVASCRIPT\ it can be a
10
-- stepping stone to using \LUA.
11 12
-- local ecmascript = optional.mujs.initialize("libmujs")
13
-- local execute = optional.mujs.execute
14 15
local
libname
=
"
mujs
"
16
local
libfile
=
"
libmujs
"
17 18
if
package
.
loaded
[
libname
]
then
19
return
package
.
loaded
[
libname
]
20
end
21 22
local
mujslib
=
resolvers
.
libraries
.
validoptional
(
libname
)
23 24
if
not
mujslib
then
25
return
26
end
27 28
local
files
=
{
}
29
local
openfile
=
io
.
open
30
local
findfile
=
resolvers
.
findfile
31 32
local
mujs_execute
=
mujslib
.
execute
33
local
mujs_dofile
=
mujslib
.
dofile
34
local
mujs_reset
=
mujslib
.
reset
35 36
local
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
(
)
51
end
52 53
mujslib
.
setfindfile
(
findfile
)
54 55
mujslib
.
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
68
end
)
69 70
mujslib
.
setclosefile
(
function
(
id
)
71
local
f
=
files
[
id
]
72
if
f
then
73
f
:
close
(
)
74
files
[
id
]
=
false
75
end
76
end
)
77 78
mujslib
.
setreadfile
(
function
(
id
,
how
)
79
local
f
=
files
[
id
]
80
if
f
then
81
return
(
f
:
read
(
how
or
"
*l
"
)
)
82
end
83
end
)
84 85
mujslib
.
setseekfile
(
function
(
id
,
whence
,
offset
)
86
local
f
=
files
[
id
]
87
if
f
then
88
return
(
f
:
seek
(
whence
,
offset
)
)
89
end
90
end
)
91 92
local
reporters
=
{
93
console
=
logs
.
reporter
(
"
mujs
"
,
"
console
"
)
,
94
report
=
logs
.
reporter
(
"
mujs
"
,
"
report
"
)
,
95
}
96 97
mujslib
.
setconsole
(
function
(
category
,
name
)
98
reporters
[
category
]
(
name
)
99
end
)
100 101
local
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 107
package
.
loaded
[
libname
]
=
mujs
108 109
optional
.
loaded
.
mujs
=
mujs
110 111
interfaces
.
implement
{
112
name
=
"
ecmacode
"
,
113
actions
=
mujs
.
execute
,
114
arguments
=
"
string
"
,
115
public
=
true
,
116
}
117 118
interfaces
.
implement
{
119
name
=
"
ecmafile
"
,
120
actions
=
mujs
.
dofile
,
121
arguments
=
"
string
"
,
122
public
=
true
,
123
protected
=
true
,
124
}
125 126
return
mujs
127