Is there a native machine code compiler for JavaScript?

JavascriptCompilationNativeNative Code

Javascript Problem Overview


Is there a native machine code compiler for JavaScript? I'm not talking about a VM. If it doesn't exist can it be done?
I am wondering if it can be compiled to binary due to the dynamic nature of the language.

Javascript Solutions


Solution 1 - Javascript

As far as I know, there are no static compilers for JavaScript. It is certainly theoretically possible; however, a static compilation of JavaScript would need a very heavyweight runtime to support all of its features (such as dynamic typing and eval). As a small aside, when presented with the need to statically compile Python (another dynamic language), the [PyPy][1] developers ended up creating a language which was a very restricted subset of Python (called RPython), void of some of Python's more dynamic features, that was capable of being statically compiled.

If you're asking this for the purpose of creating a standalone executable from JavaScript code, I'm sure there must be wrappers which essentially would create an executable containing your script and an embedded JavaScript VM (sadly, I don't know any offhand).

[1]: http://codespeak.net/pypy/dist/pypy/doc/ "PyPy"

Solution 2 - Javascript

It's definitely doable, although the only way I know how to do it at the moment is a two step process...

  1. Compile the javascript to Java using Mozilla Rhino JSC.
  2. Compile the resulting java class file to executable using something like GNU's GCJ.

Why would you want to, though? What advantage do you expect to find?

Solution 3 - Javascript

Google V8 engine compiles JavaScript into native machine code. This feature is used in the EncloseJS compiler which I wrote for for node.js and io.js projects.

Solution 4 - Javascript

Please note that all of these solutions are DOMless, so no libraries like angular.js or jquery, only underscore.js/lodash

Following up to Falaina's answer, PyPy does have a dist for JavaScript

Also, Appcelerator Titanium has a js > JavaBit > android

Finally, node.js can use nexe Explained in this other answer

Solution 5 - Javascript

It is theoretically possible, but there will be a lot of runtime support baggage involved (and even a full Javascript compiler or interpreter to support eval).

Are you looking for an actual native code compiler, or are you looking for something that can bundle Javascript code along with a runtime into a single executable binary?

Solution 6 - Javascript

I'm sure microsoft has a downloadable Jscript ("Microsoft's javascript") compiler

if you have windows: If you write a javascript text file you can compile it by: opening the command prompt using cd to get to the text file directory (if you have windows) type: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\jsc.exe textfile.js

otherwise: download a jscript compiler type the directory of the jscript compiler and then the name of the javascript text file

you can add environment variables if you don't want to type out the whole directory name to the compiler.

this guy explains it better: http://www.phpied.com/make-your-javascript-a-windows-exe/

I personally think this is really cool. I just wish there was more documentation for it.

I'm pretty sure that is what you're looking for, but I'm a bit new.

Solution 7 - Javascript

Adobe AIR's AOT compiler for iOS statically compiles a superset of JavaScript called Actionscript 3.0 down to ABC bytecode, then machine code through LLVM. If you were to write your AS3 code without classes and without type annotation, it would essentially be JavaScript, and then the compiler would happily compile it down to machine code. Sadly this is not open source software, and you don't get access to any DOM (which many people think of when they think of JavaScript) because it's running inside essentially a Flash Player instance.

Solution 8 - Javascript

TraceMonkey in FF3.5 do this to some parts of the javascript code. You may be able to get some directions from there!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionthe_drowView Question on Stackoverflow
Solution 1 - JavascriptFalainaView Answer on Stackoverflow
Solution 2 - JavascriptStoborView Answer on Stackoverflow
Solution 3 - JavascriptIgor KlopovView Answer on Stackoverflow
Solution 4 - JavascriptpercebusView Answer on Stackoverflow
Solution 5 - JavascriptGreg HewgillView Answer on Stackoverflow
Solution 6 - JavascriptDaniel LadnerView Answer on Stackoverflow
Solution 7 - JavascriptKevin NewmanView Answer on Stackoverflow
Solution 8 - JavascriptTheVillageIdiotView Answer on Stackoverflow