\documentclass{beamer}
\title{Compiling \texttt{luaexpat} into Lua\TeX}
\author{David Kastrup}
\date{May 3, 2009}
\begin{document}
\maketitle
\begin{frame}
  \frametitle{Why \texttt{luaexpat}?}
  \begin{itemize}[<+->]
  \item \TeX\ does not get to see XML at all
  \item no messing with catcodes
  \item full XML compliance
  \item full validating parser
  \item no encoding problems
  \item no interlocking of parsing and typesetting in \TeX
  \item high performance
  \item maintenance does require less hit points
  \end{itemize}
\end{frame}
\begin{frame}
  \frametitle{Compiling it into Lua\TeX}
  \begin{itemize}[<+->]
  \item just \texttt{find}/\texttt{grep} for comparable library?
  \item Peter changed \texttt{web2c} compilation schemes a few weeks ago
  \item everything is \texttt{automake}-based right now
  \item the old build infrastructure is still there but unused
  \item generated files from \texttt{automake} are checked into the repository
  \item \texttt{automake} is not run automatically by the build scripts
  \item lots of wrong leads
  \end{itemize}
\end{frame}
\begin{frame}[fragile]
  \frametitle{So what was the actual beef? -- 1}
\begin{verbatim}
--- a/source/texk/web2c/luatexdir/am/luamisc.am
+++ b/source/texk/web2c/luatexdir/am/luamisc.am
@@ -21,6 +21,8 @@ libluamisc_a_SOURCES = \
        luatexdir/luamd5/md5.h \
        luatexdir/luamd5/md5lib.c \
        luatexdir/luapeg/lpeg.c \
+       luatexdir/luaexpat/src/lxplib.c \
+       luatexdir/luaexpat/src/lxplib.h \
        luatexdir/luaprofiler/clocks.c \
        luatexdir/luaprofiler/clocks.h \
        luatexdir/luaprofiler/core_profiler.c \
\end{verbatim}
\end{frame}
\begin{frame}[fragile]
  \frametitle{The beef -- 2}  
\begin{verbatim}
--- a/source/texk/web2c/luatexdir/am/luatex.am
+++ b/source/texk/web2c/luatexdir/am/luatex.am
@@ -21,7 +21,7 @@ luatex_ldadd = libluatex.a libff.a libluamisc.a libzzip.a libl
 luatex_ldadd += $(LIBPNG_LIBS) $(ZLIB_LIBS) $(XPDF_LIBS)
 luatex_ldadd += $(OBSDCOMPAT_LIBS) libmd5.a libmplib.a 
 
-luatex_LDADD = $(luatex_ldadd) $(LDADD) $(socketlibs)
+luatex_LDADD = $(luatex_ldadd) $(LDADD) $(socketlibs) -lexpat
 
 luatex_DEPENDENCIES = $(proglib) libluatex.a
 luatex_DEPENDENCIES += $(LIBPNG_DEPEND) $(ZLIB_DEPEND) $(XPDF_DEPEND)
\end{verbatim}
\end{frame}
\begin{frame}[fragile]
  \frametitle{The beef -- 3}  
\begin{verbatim}
--- a/source/texk/web2c/luatexdir/lua/luastuff.c
+++ b/source/texk/web2c/luatexdir/lua/luastuff.c
@@ -100,6 +100,7 @@ static const luaL_Reg lualibs[] = {
     {"unicode", luaopen_unicode},
     {"zip", luaopen_zip},
     {"lpeg", luaopen_lpeg},
+    {"lxp", luaopen_lxp},
     {"md5", luaopen_md5},
     {"lfs", luaopen_lfs},
     {"profiler", luaopen_profiler},
\end{verbatim}
\end{frame}
\begin{frame}[fragile]
  \frametitle{The beef -- 4}  
\begin{verbatim}
--- a/source/texk/web2c/luatexdir/luatex-api.h
+++ b/source/texk/web2c/luatexdir/luatex-api.h
@@ -58,6 +58,7 @@ extern int luaopen_lfs(lua_State * L);
 extern int luaopen_lpeg(lua_State * L);
 extern int luaopen_md5(lua_State * L);
 extern int luaopen_zlib(lua_State * L);
+extern int luaopen_lxp(lua_State * L);
 extern int luaopen_gzip(lua_State * L);
 extern int luaopen_ff(lua_State * L);
 extern int luaopen_profiler(lua_State * L);
\end{verbatim}
\end{frame}
\begin{frame}
  \frametitle{What else?}
  \begin{itemize}[<+->]
  \item run \texttt{automake} in \texttt{source/texk/web2c/luatexdir}
  \item or use \texttt{configure}/\texttt{build} in maintainer mode
  \item note: all of the build scripts and similar of the
    \texttt{luaexpat} package are not being used.
  \item note: no changes were necessary in the \texttt{luaexpat}
    directory itself
  \item note: a `proper' solution would compile and link a static
    version of \texttt{libexpat}, not just of \texttt{luaexpat}
  \end{itemize}
\end{frame}
\begin{frame}[fragile]
  \frametitle{Implementing a \TeX\ executor}
  \begin{itemize}[<+->]
  \item \texttt{tex.print} has delayed action
  \item \verb+\directlua+ has
    to finish before \TeX\ resumes
  \item we want a \texttt{texexec} function that returns \emph{after}
    processing its argument as a \TeX\ command
  \item coroutines can do this, but our main executor must be wrapped
    into a function body
  \end{itemize}
\end{frame}
\begin{frame}[fragile]
  \frametitle{Implementation of \TeX\ executor}
\begin{verbatim}
\directlua{
function main()
   ...
end
texresumer = coroutine.wrap(main)
function texexec(text, ...)
  tex.print(text)
  tex.print("\directlua{texresumer(", ...)
  tex.print(")}")
  return coroutine.yield()
end
}
\end{verbatim}
\end{frame}
\begin{frame}
  \frametitle{The library docs}
  \texttt{http://www.keplerproject.org/luaexpat/manual.html\#parser}
\end{frame}

\end{document}

%%% Local Variables: 
%%% mode: latex
%%% TeX-master: t
%%% End: 
