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.

 

odindiagram

 

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.

 

odinbenchsmall

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.

odindiagram

Banner


EFF Asks For The Right To Access Software
07/11/2014

The DMCA law is designed to act against copyright infringement, but it is having some unexpected side effects. Now the EFF wants exceptions to allow people to fix their cars and keep abandoned games r [ ... ]



Azure Plugin For Android Studio And IntelliJ
04/11/2014

Google owns Android, but that doesn't stop Microsoft from trying to get Android apps to make use of Azure and Office 365. Does the new plugin make a Microsoft-flavoured Lollipop any more likely?


More News

Last Updated ( Wednesday, 06 March 2013 )
 
 

   
RSS feed of news items only
I Programmer News
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.