JavaScript Assembly Language |
Written by Ian Elliot |
Friday, 22 February 2013 |
The idea of using JavaScript as a modern day assembly language for browser code is being taken very seriously by Mozilla. Asm.js is a specification for a high performance JavaScript assembly language and OdinMonkey is an engine that runs it really fast. A compiler doesn't have to target a low level machine code. For example, the Java compiler creates byte code which is then run using a virtual machine. JavaScript is so ubiquitous that it makes sense to use it as a target for compilers of other languages so that web apps can be written using them. The best known example is probably CoffeeScript which compiles to standard JavaScript. However, CoffeeScript is already quite close to JavaScript and the compilation is a fairly easy task. More difficult is to compile C++ to JavaScript, which is what compilers like Emscripten or Mandreel do. At the moment these compilers attempt to render C++ into JavaScript so that the result runs reasonably fast. A much better idea is to define a subset of JavaScript, which is complete in the sense that you can do anything you need to using it, but one that can be implemented efficiently.
This is what asm.js is all about. It is a detailed specification for a subset of JavaScript. Notice that while this means that this isn't a new language there are new features which would be ignored by a standard JavaScript engine. For example, the language only has strictly-typed integers, floats, arithmetic, function calls, and heap accesses. This is achieved using annotations in the form of comments and a restricted set of operations which only work with the data type - for example logical operators convert numeric values to int32. The fact that asm.js is a subset of JavaScript means that you can take an asm.js program and run it in a standard unmodified browser or JavaScript engine. However, to get any real value from using it you need it to be run using an optimizing JavaScript engine. This is the second half of the project. Mozilla is working on OdinMonkey, a JavaScript engine that recognizes when it has been fed an asm.js program and can apply automatic optimizations. The target for for OdinMonkey is to run compiled C++ code from Emscripten at no worse than half the speed. Current benchmarks seems to prove that it is possible. You can also try it out by compiling using emscripten with the ASM_JS=1 switch and then run it using a special Firefox build.
Click for larger version
So, putting the ideas together: build compilers from C++ and other languages to asm.js and use optimized JavaScript engines to run the code at near native speeds. If the browser doesn't have an optimized engine then the code still runs because its just JavaScript. It remains to be seen if other JavaScript engines take up asm.js. The alternative is of course Google's NaCL and PNaCL, but again only one browser manufacturer is supporting it. We will have to wait while they fight it out. More InformationRelated ArticlesPit - F# to JavaScript Compiler JavaScript to be the Default Language for Gnome Chrome Native Client Goes ARM and Portable Chrome native apps split the browser world
To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin, or sign up for our weekly newsletter.
Comments
or email your comment to: comments@i-programmer.info
|
Last Updated ( Wednesday, 06 March 2013 ) |