incr Tcl in Javascript

An enhanced version of Tcl in Javascript also named itcl in Javascript.

This is work in progress and is the attempt to have a more complete version of the above reference running interpreted by Javascript. There now exist a lot of Tcl 8.6/8.5 features like namespaces, dicts, the {*} operator and a lot of missing features in the original like upvar, uplevel, switch, variable and an expr command which can expand its arguments (variables, braced commands etc.) on the fly and array names and array variables.

Enhancements for regexp, string, return command

Based on that I am trying to add an itcl version completely written in Tcl based on itclng where the TclOO part and the remaining C part is implemented in Javascript too.

I hope this makes it possible to run a (subset) of Tcl and itcl in every browser that implements Javascript.

This will be more than 3 times slower than perl :) or is that comparable? I do not know of a perl implementation running in Javascript.

The project started about February 2011.

I started with the existing about 1000 LOC in Javascript.

 History of the development steps

apw 2011-05-08: today I finally succeeded to parse and create the first simple itcl class and instantiate an object. Using the object to call a class method works too, but there is no class resolving/protection check yet. The mechanism for that is foreseen, but not yet completely implemented (to be done next).

The source files now have about 7000 LOC in Javascript including comments.

apw 2011-05-15: parsing of itcl class constructor, destructor and variable/common parts now working, inheritance for classes working, variable and command resolution for itcl classes working. (working here means the first tests are running successfully, still a lot of work to make that completely running ok).

I have decided to implement a simple version of a test framework like the Tcl tcltest command (a small subset thereof) to be able to run for example itcl test suite parts (I hope most of them should work when implemented, which means the corresponding code for itcl can be implemented). First (very small) version of ::tcltest and some of the sub commands thereof as ensemble commands are implemented and running.

apw 2011-05-21: have today coded something similar to the itcl stubs mechanism, which lets me load some parts of the commands to be loaded only when used for the first time. That also allows the javascript itcl parts only to be loaded when package require Itcl is done. Also parts of the package sub commands are now done, so that package require Itcl works together with a pkgIndex.tcl file, which is sourced with a source command.

apw 2011-05-29: have built a little Tcl socket server for being able to debug and play with itcl in Javascript. The connection is done using XmlHttpRequest functionality. Did take me 2 days as it is very hard to eval on toplevel with Javascript and for the other problems with Javascript (the differences between running Javascript with rhino and in firefox). Now I can load and drive itcl in Javascript from the command line interface of the little server and then i can type in Tcl/itcl code and test it working in the browser. there is a second browser window opened for logging all the input and output + debugging output. Now is the time for building itcl classes to build/map Tk commands/widgets/objects into the DOM and javascript environment. I will go a similar way as does WubWidgets.tcl, but I can directly access and modify the DOM parts in the browser. I have already played with the server and have built dynamically some new buttons and modified their properties like style background and the text of the TextNode. Seems to work. I hope it will also run with IE and the other browsers with some modifications to adapt to their specialties.

apw 2011-06-04: constructor calls of itcl classes are now working (also for inherited classes). Constructor init code is still missing. Implementation of some ::jsdom sub commands for access to Javascript DOM from Tcl is done. That allows building of some simple Tk widget like classes in itcl namely toplevel, button, entry and label (and others something like tktable is the most ambitious one I am thinking of at the moment and maybe something like tktree). These create Javascript DOM nodes using ::jsdom. The pack or grid command is still missing, these would (in the background) append the created nodes namely toplevel, button etc. to the <body> DOM element, so that these elements would become visible in the browser. The configure command (the interface to the Javascript DOM part has still to be implemented) will then get and modify the style attributes and the other attributes of a Javascript DOM element.

Implementation of trace add execution is done and the functionality is working for enter/enterstep/leave/leavstep for Tcl commands(procs) and itcl methods.

The source files now have hit the "magic" number of 10.000 LOC in Javascript including comments.

apw 2011-06-13: started to implement code for "precompiling" (to tokenize Tcl code only once) because of massive performance problems. Might be, that I have to rewrite the itcl parser in Javascript too for the same reason.

apw 2011-06-22: precompiling makes progress splitting up into Tcl statements and words mostly done (code is now over 12.000 LOC)

apw 2011-06-25: big part of itcl class parsing with javascript now works, itcl object creation also done and a call of a class method using that object works again. So nearly back to the state as before starting to handle performance problems. Performance seems to be a little bit better now, but there is mostly the basics done. Next step is to use the tokenized code on all places (not yet done).

apw 2011-06-26: current version with a little example now running again in firefox, added a mini demo in directory examples_for_firefox in the repository: itcl_in_javascript

apw 2011-06-28: first mini demo generating a button and an entry (html input) element in the browser using Tk syntax "button .b" and "entry .e1" and making them visible with pack command "pack .b1 .e1"

apw 2011-07-01: The first small part of setting html "style" properties driven by Tk options is now implemented and running with the mini example. "onclick" javascript event of a button is calling and evaluating the "-command" option script of the button options now (code is now about 15.000 LOC). Next implementation step will be first limited version of "bind" command.

apw 2011-07-02: Tk entry command with bind (example Key-Return) works and also general mechanism for Tk (widget-)object commands is implemented and running.

apw 2011-07-03: Implemented "interp alias" command (with current interp only) so the normal Tk commands like button are available, as they now are implemented as a namespace ensemble commands in ::tk namespace.

apw 2011-08-17: I am planning to give a presentation on this project at Eighteenth Annual Tcl/Tk Conference (2011). After that I will install demos on a server.

References to javascript Objects used for implementation of this project. Will be filled soon with details:

javascript Object, Tcl js Object Types, Tcl js Tokens, Tcl js Call Types

Paper at Eighteenth Annual Tcl/Tk Conference (2011)

itcl in Javascript Paper contains some more details.

Small Demos

In the Tcl Source Code widget there is the Tcl source code, used for the demo, the second shows the "real" code needed without the filling of the widgets with data.

An itcl class with inheritance and some execution and variable traces:

A "mini" test suite using a tcltest command very similar to the original. Not all test cases are running with success!

A list of small examples (the parts of the test suite), which handle mostly "corner cases" and code parts having problems during the implementation:


itcl in javascript Select files tab for the sources.

See also

apw 2012-01-15: There is a reimplementation of incr Tcl in Javascript ongoing based mostly on implementing Jim Tcl in Javascript with a lot of enhancements in the Javascript technique used for better performance and less memory consumption. It is called RAPL. It will support full namespace handling as in Tcl and an implementation of itcl-ng 4.0x (all that written in Javascript). It is generally based on incr Tcl in Javascript but more "object oriented" as far as Javascript does support that.


Googie 2011-08-08: This looks very interesting. Is there possibility to run some examples out of box? I mean is there any example ready to use, so I just click on the link to see it? I saw examples in source directory but I'm not sure how to configure environment for it.

apw 2011-08-08: Sorry there is no standard demo yet. I will try to enhance the README file in examples_for_firefox and I will try to setup a demo on a server on a host I have so you can see a working example soon.

Ro Where do I download this or try a demo on a live web server?

apw: 2011-08-17: you can download it from the repository , but as it is in pre alpha state, you cannot do a lot with that right now.

vkvalli - 2013-07-25 05:03:23

Another simpler route seems to be feasible. Mozilla gives LLVM bytecode to optimised javascript converter. Please refer

It is a LLVM bytecode to javascript compiler.

Using this technology, they have ported C binaries to browser. Example, games, sqlite etc.

Now all that is required is, we need to build Tcl from llvm target, rather than native machine. It seems , gcc has an option to build for llvm target.

It seems the future is in this direction.

Maybe using the llvmtcl extension, it seems to be able to convert Tcl to LLVM ?