3ur3ka / web2js

web2js update
Other
0 stars 1 forks source link

web2js

This is a Pascal compiler that targets WebAssembly, designed specifically to compile TeX so it can be run inside the browser. More specifically, this repository includes a TeX engine called jsTeX which is like LuaTeX but instead of embedding Lua it embeds JavaScript.

Importantly, the jsTeX engine passes the trip tests which you can verify by running make test.

There is a live demo and a short overview available at

J. Fowler, Both TEX and DVI viewers inside the web browser, TUGboat, Volume 40 (2019), No. 1.

Prerequisites

This projects depends on NodeJS for executing the javascript. To post-process the WebAssembly, you will need wasm-opt on your path.

You will need a full TeX installation (e.g., TeX Live) with access to kpsewhich in order that library.js can find the necessary TeX files. You also need tie and tangle to turn the WEB sources into Pascal which can be fed to the compiler.

The contents of the texk, triptrap, and etexdir subdirectories were copied from tug.org via

mkdir texk
rsync -a --delete --exclude=.svn tug.org::tldevsrc/Build/source/texk/web2c/tex.web texk
rsync -a --delete --exclude=.svn tug.org::tldevsrc/Build/source/texk/web2c/triptrap .
rsync -a --delete --exclude=.svn tug.org::tldevsrc/Build/source/texk/web2c/etexdir .

Getting started

After cloning this repository, run npm install.

Then run

make core.dump

to apply the changefiles, compile the resulting Pascal source to WebAssembly, and run initex.js to produce a format file and its corresponding memory dump.

Then you can run TeX on a file called sample.tex with

node tex.js sample.tex

jsTeX

The main innovation of jsTeX is \directjs primitive similar to LuaTeX's \directlua. For example,

\documentclass[12pt]{article}

\newcommand{\cubeit}[1]{\directjs{
  tex.print('$');
  tex.print(`#1^3 = ${#1*#1*#1}`);
  tex.print('$');
}}

\begin{document}

Let's multiply eight by eight by eight.  \cubeit{8}

\end{document}

Inside \directjs, the JavaScript function tex.print can be used to emit strings back into TeX for further processing.