58

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.

5
  • Were you talking about JScript.NET? Jul 13, 2009 at 7:52
  • I do ask a small clarification: By "native machine code compiler", do you mean a compiler that takes a Javascript program and produces an executable(Static Compilation), or do you just mean is it all possible to translate javascript code to machine code at all (For example the JIT compilers in Tracemonkey(Firefox) and V8(Chrome) will produced native machine code from currently executing javascript, so it doesn't have to interpret them multiple times.)
    – Falaina
    Jul 13, 2009 at 8:15
  • 2
    I'm asking for static compilation.
    – the_drow
    Jul 13, 2009 at 9:18
  • @Shimmy: No. It's not statically compiled.
    – the_drow
    Jul 13, 2009 at 9:30
  • assemblyscript.org is the closest I do know Mar 9 at 20:01

8 Answers 8

35

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 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).

3
  • 1
    The first browser-based JIT was released with FF 3.5 about a month before this question was answered. Jun 3, 2013 at 18:46
  • +1 for PyPy. RPython is not statically compiled per se, instead, RPython program is started in regular CPython, and a snapshot of all the objects currently in memory is taken. That ensures all type specialization etc is already done. That also means RPython program input is severely restricted, e.g. it's not possible to json.load something and get types on the fly. Jul 3, 2013 at 19:13
  • There are several JavaScript-to-C compilers, but they usually work with only a statically-typed subset of JavaScript. Sep 15, 2020 at 20:15
29

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?

2
  • 22
    It's just out of curiosity. I like javascript a lot.
    – the_drow
    Jul 13, 2009 at 7:51
  • How big would a simple "hello world" program binary be using this method? Jun 14, 2022 at 18:51
12

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.

1
  • 2
    (Meta) It doesn't and shouldn't be obtrusive, but a simple my at the right place suffices. I went ahead and edit your answer here. Please keep this in mind for future answers.
    – Artjom B.
    Aug 2, 2015 at 17:22
4

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

3

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?

1
  • 4
    I'm looking for an actual native code compiler
    – the_drow
    Jul 13, 2009 at 7:49
2

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.

2
  • 2
    Compiling to .net IL-code is not compiling to a native language as questioned, IL-code is actually an interpreted one (Just in time) as well. Just so you know ;)
    – Remco
    Aug 30, 2015 at 12:25
  • 1
    @Remco: IL is always compiled to machine code instructions, before it gets executed. Unlike Java bytecode, IL is never interpreted. Sep 6, 2016 at 7:50
2

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.

1

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

2
  • Not really. I haven't found anything useful there.
    – the_drow
    Jul 14, 2009 at 3:33
  • This doesn't help, as Tracemonkey is not able to do this to all parts of the code.
    – Bergi
    Aug 28, 2016 at 21:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.