summaryrefslogtreecommitdiff
path: root/tex/context/base/luat-env.tex
blob: 164be174cbee9664c2d2f460417f283315d437be (plain)
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
%D \module
%D   [       file=luat-env,
%D        version=2005.05.26,
%D          title=\CONTEXT\ Lua Macros,
%D       subtitle=ConTeXt features,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright=PRAGMA]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D Originally we compiled the lua files externally and loaded
%D then at runtime, but when the amount grew, we realized that
%D we needed away to store them in the format, which is what
%D bytecode arrays do. And so the following is obsolete:
%D
%D \starttyping
%D \chardef\ctxluaembeddingmode \plusone
%D
%D 0 = external compilation and loading
%D 1 = runtime compilation and embedding
%D \stoptyping

% \writestatus{loading}{Lua Support Macros (environment)}

% print (lua.id)
% print (lua.version)
% print (lua.startupfile)

%D Allocation of \LUA\ engines.

\newcount\luadefcounter

\ifx\zerocount\undefined \chardef\zerocount=0 \fi
\ifx\plusone  \undefined \chardef\plusone  =1 \fi

\def\newlua#1%
  {\global\advance\luadefcounter\plusone
   \mathchardef#1\luadefcounter}

%D We use a dedicated instance for \CONTEXT\ core functionality. In
%D \CONTEXT we also use this as callback instance. Instance 0 is
%D the scratch instance.

\ifx\luastartup\undefined \newcount\luastartup \fi

\chardef\CTXlua\zerocount \luadefcounter\CTXlua \luastartup\CTXlua

\def\ctxdirectlua{\directlua\CTXlua}  \let\ctxlua\ctxdirectlua
\def\ctxlatelua  {\latelua  \CTXlua}

%D The simple \type {\lua} command is just a shortcut to the
%D zero instance. Beware, we don't use the 0--9 range for
%D scratch purposes as we do with other registers. First of all
%D we want to avoid the overhead, but mostly, users can just define
%D their own.

\newlua \luadefault

\def \lua       {\directlua\luadefault} % zero is the main one, and reserved for ctx
\edef\luaversion{\ctxlua{tex.print(_VERSION)}}

%D We want to define \LUA\ related things in the format but
%D need to reluad code because \LUA\ instances are not dumped
%D into the format.

\ifx\undefined\normaleveryjob \let\normaleveryjob\everyjob \newtoks\everyjob \fi

\newtoks\everyloadluacode
\newtoks\everyfinalizeluacode

\normaleveryjob{\the\everyloadluacode\the\everyfinalizeluacode\the\everyjob}

\newif\ifproductionrun

\long\def\startruntimeluacode#1\stopruntimeluacode % only simple code (load +init)
  {\ifproductionrun
     \global\let\startruntimeluacode\relax
     \global\let\stopruntimeluacode \relax
   \else
     \global\everyloadluacode\expandafter{\the\everyloadluacode#1}%
   \fi
   #1} % maybe no interference

\long\def\startruntimectxluacode#1\stopruntimectxluacode
  {\startruntimeluacode\ctxlua{#1}\stopruntimeluacode}

%D Next we load the initialization code.

\startruntimectxluacode
    environment = environment or { }
    environment.jobname    = "\jobname"                            % tex.jobname
    environment.initex     = \ifproductionrun false \else true \fi % tex.formatname == ""
    environment.version    = "\contextversion"
\stopruntimectxluacode

\chardef\ctxluaexecutionmode \zerocount % private

% we start at 500, below this, we store predefined data (dumps)

\newcount\luabytecodecounter \luabytecodecounter=500

\startruntimectxluacode
    if not lua.bytedata then lua.bytedata = { } end
\stopruntimectxluacode

%D Handy when we expand:

\let\stopruntimeluacode   \relax
\let\stopruntimectxluacode\relax

\ifx\normalprotected \undefined \let\normalprotected \protected  \fi
\ifx\normalunexpanded\undefined \let\normalunexpanded\unexpanded \fi
\ifx\normalexpanded  \undefined \let\normalexpanded  \expanded   \fi

\long\def\lastexpanded{} % todo: elsewhere we use \@@expanded

\long\def\expanded#1{\long\xdef\lastexpanded{\noexpand#1}\lastexpanded}

%D More code:

\def\ctxluabytecode#1% executes an already loaded chunk
  {\ctxlua {
    do
        local str = ''
        if lua.bytedata[#1] then
            str = " from file " .. lua.bytedata[#1][1] .. " version " .. lua.bytedata[#1][2]
        end
        if lua.bytecode[#1] then
            if environment.initex then
            %   logs.report("bytecode","executing blob " .. "#1" .. str)
                assert(lua.bytecode[#1])()
            else
                assert(lua.bytecode[#1])()
                lua.bytecode[#1] = nil
            end
        else
        %   logs.report("bytecode", "invalid blob " .. "#1" .. str)
        end
    end
  }}

\def\ctxluabyteload#1#2% registers and compiles chunk
  {\global\advance\luabytecodecounter \plusone
   \expanded{\startruntimectxluacode
     lua.bytedata[\the\luabytecodecounter] = { "#1", "#2" }
   \stopruntimectxluacode}%
   \ctxlua {
      lua.bytedata[\the\luabytecodecounter] = { "#1", "#2" }
      lua.bytecode[\the\luabytecodecounter] = environment.luafilechunk("#1")
    }}

\def\ctxloadluafile#1#2% load a (either not compiled) chunk at runtime
  {\doifelsenothing{#2}
     {\ctxlua{environment.loadluafile("#1")}}
     {\ctxlua{environment.loadluafile("#1",#2)}}}

\def\registerctxluafile#1#2% name version
  {\ifproductionrun \else
     \ctxluabyteload{#1}{#2}%
   \fi
   \global\everyloadluacode\expandafter\expandafter\expandafter{\expandafter\the\expandafter\everyloadluacode
      \expandafter\ctxluabytecode\expandafter{\the\luabytecodecounter}}%
   \ifcase\ctxluaexecutionmode\or\ctxluabytecode{\the\luabytecodecounter}\fi}

\registerctxluafile{luat-env}{1.001}

\chardef\ctxluaexecutionmode \plusone

\endinput