diff options
author | Hans Hagen <pragma@wxs.nl> | 2007-08-07 01:37:00 +0200 |
---|---|---|
committer | Hans Hagen <pragma@wxs.nl> | 2007-08-07 01:37:00 +0200 |
commit | aacdde41ef02392949aee16b2e428a8913d27efe (patch) | |
tree | 1ca125418e41b0335ee115a24cf27acb8fa7eae9 /tex/context/base/luat-env.tex | |
parent | dbcaab8b8f76309b9fc4e05bf8a42f6b56e61893 (diff) | |
download | context-aacdde41ef02392949aee16b2e428a8913d27efe.tar.gz |
stable 2007.08.07 01:37
Diffstat (limited to 'tex/context/base/luat-env.tex')
-rw-r--r-- | tex/context/base/luat-env.tex | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/tex/context/base/luat-env.tex b/tex/context/base/luat-env.tex new file mode 100644 index 000000000..ec2b942eb --- /dev/null +++ b/tex/context/base/luat-env.tex @@ -0,0 +1,169 @@ +%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. + +% \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\ctxlua {\directlua\CTXlua} +\def\directctxlua{\directlua\CTXlua} +\def\latectxlua {\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 + if not environment then environment = { } end + environment.jobname = "\jobname" % tex.jobname + environment.formatname = "\contextformat" % tex.formatname + environment.initex = \ifproductionrun false \else true \fi % tex.formatname == "" + environment.version = "\contextversion" + dofile(input.find_file(texmf.instance,"luat-env.lua","tex")) +\stopruntimectxluacode + +% 0 = external compilation and loading +% 1 = runtime compilation and embedding + +\chardef\ctxluaembeddingmode \plusone +\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 + +\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 + environment.showmessage("executing byte code " .. "#1" .. str) + assert(lua.bytecode[#1])() + else + assert(lua.bytecode[#1])() + lua.bytecode[#1] = nil + end + else + environment.showmessage("invalid byte code " .. "#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\ctxluafileload#1#2% load a (either not compiled) chunk at runtime + {\doifelsenothing{#2} + {\ctxlua{environment.loadlucfile("#1")}} + {\ctxlua{environment.loadlucfile("#1",#2)}}} + +\def\registerctxluafile#1#2% name version + {\ifcase\ctxluaembeddingmode + \ifproductionrun \else + \global\everyloadluacode\expandafter{\the\everyloadluacode\ctxluafileload{#1}{#2}}% + \fi + \ifcase\ctxluaexecutionmode\or\ctxluafileload{#1}{#2}\fi + \else + \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 + \fi} + +\ifcase\ctxluaembeddingmode \else \registerctxluafile{luat-env}{1.001} \fi + +\chardef\ctxluaexecutionmode \plusone + +\endinput |