views:

511

answers:

5

Hello,
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.

Thanks in advance,
Omer

+1  A: 

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?

Greg Hewgill
I'm looking for an actual native code compiler
the_drow
A: 

Java uses byte code which is interpreted by the VM, you can get wrappers which make what looks like a native exe - Launch4J for example.

Echilon
I'm talking about Javascript.
the_drow
+1  A: 

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?

Stobor
It's just out of curiosity.I like javascript a lot.
the_drow
+2  A: 

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

TheVillageIdiot
Not really. I haven't found anything useful there.
the_drow
+1  A: 

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 comtaining your script and an embedded Javascript VM(Sadly, I don't know any offhand)

Falaina