Updated 2017-05-01 20:49:05 by LarrySmith

emscripten is an interesting idea, but the concept of compiling Tcl, a very high-level language, down to an "assembler" version, also implemented in a high level language, but not using most of the features of it, just seems wonky to me. Would it not make more sense to implement Tcl as a transpiler taking it's high-level lists, dicts, namespaces, and other such notions directly into javascript, which also implements all these features - albeit as ugly as hell. It seems to me that taking javascript libraries and implementing only a scanner, it shouldn't be hard to "compile" to js source. Larry Smith

emscripten compiles "regular" C/C++ projects to JavaScript via LLVM. This is how marvels such as PCE.js (A Mac Plus/IBM PC emulator in the browser) can exist. This evening, I've made a few attempts at compiling a number of Tcl versions (from 8.6.1 back to 8.2, didn't get further) using emscripten. The steps for compiling are in theory easy and well-documented. However, emmake make fails every time in the Network code. It fails once when trying to solve "services" to port number (something that can easily be commented away) but also later on in the platform implementation of the socket command. It got too late, so I gave up. Anybody who wants to continue? This should provide an alternative implementation of Tcl in the browser. EF

TP You might want to start smaller. I have compiled and run Picol as an experiment back when emscripten was just getting started. I know of other Tclers who have also compiled Jim. Jim is a good candidate for emscripten, since it is built as a core plus several modules that can be switched on or off with configure directives. To compile Tcl, you will probably have to start with the ./unix/ directory, gut most of the C files in that directory, leaving the internal interfaces but return TCL_ERROR or similar.

EF I am aware of those smaller alternatives. Thing is that I was impressed that the whole "generic" went without any real glitches (or almost). unix didn't seem that bad either... But I'll look into Jim again, as it's much closer to the "real thing" than picol is as far as I can remember.

stevel I've done it to Jim Tcl (see Tcl 2011 paper) but it was slow, although it would be interesting to see how a more recent emscripten would perform. A better approach is Arnulf Wiedemann's APWTCL

TP I was able to compile and run Jim with these steps. You should already have emscripten installed and be able to compile a simple hello world or equivalent, and have the emscripten commands available on your PATH. See Emscripten-SDK. I cloned the jimtcl repository from Github.
cd jimtcl
emconfigure ./configure --without-ext="aio load signal" --disable-shared --disable-lineedit
emmake make
mv jimsh jimsh.bc
emcc jimsh.bc -o jimsh.html

Now load up the jimsh.html page in your browser (e.g., file:///home/user/jimtcl/jimsh.html). This page has a rudimentary dialog box for standard input. If you have time, you might try interfacing with one of the nicer repl webpages, see the Emscripten wiki for examples.

aidanhs I created http://aidanhs.github.io/emtcl/ a while ago. It shows both Tcl and JimTcl running in the browser. In addition, it comes with an extension to Tcl for manipulating the dom with inline JS - https://github.com/aidanhs/emtcl/blob/master/opt/dom.c

Wacl is a Tcl distribution for [WebAssembly]. It enables Web developers to embed a Tcl interpreter in the browser and integrate Tcl with JavaScript.

It is an extension of the Emtcl project. But Wacl takes things a few steps further: it integrates a fully featured Tcl interpreter into the webpage and adds the following features:

  • A main tclsh interpreter and capability to get it via JavaScript
  • An event loop to process all Tcl events (timer events, fileevents, custom events)
  • Client sockets. The socket -async ... command connects to websocket servers with the binary protocol. Then the resulting handle can be used to transmit binary data as with normal TCP sockets.
  • The Tcl library: modules and packages in the Emscripten virtual filesystem. You can add your own packages!
  • Proper initialization via Tcl_Init()
  • An extension to call javascript functions from Tcl