1

I'm wondering if its possible to write a javascript program and have it compiled and linked into an executable?

If so would it be possible to create a libjs that would be the equivalent of libc for the c/c++ world? wouldn't creating something like this make javascript a full fledged language that could then be compiled and run directly on the target hardware?

If you had a compiler for javascript, couldn't you write a new compiler in javascript?

Justin808
  • 20,859
  • 46
  • 160
  • 265
  • I am not aware of a Javascript compiler that compile to an executable, but modern browser Javascript engines are JIT compilers that turn Javscript directly into machine language. However, Javascript is so dynamic, I don't know how well it would do if compiled statically. – Mike Caron Aug 04 '11 at 00:47
  • "wouldn't creating something like this make javascript a full fledged language" - JavaScript is _already_ a full-fledged language. – nnnnnn Aug 04 '11 at 01:02
  • @nnnnnn - True, I guess what I ment was a full fledged compiled-language (C/C++) as opposed to a full fledged interpreted-language (javascript/perl/python/php). – Justin808 Aug 04 '11 at 01:07

3 Answers3

4

Yes, you could write a js compiler. Not sure how popular it would be:

  1. js engines are very fast these days, so you're not gaining much speed.

  2. It would be platform specific, or you would have to support multiple platforms. Not pleasant.

  3. What would it be useful for? The great thing about an interpreted language is the very fact that it doesn't need to be compiled. It shortens development cycles and build times (ever sat in front of a C program and had to change a file that the entire project relies on and had to run and rerun makes that take minutes to compile everything?).

Regarding your last point, you're correct. Had you one of these compilers, you could indeed write another one in javascript.

davin
  • 44,863
  • 9
  • 78
  • 78
  • I think the question is one of what is possible, since moving from a completely device independent, JITted, dynamic language, running in a browser TO an old-fashioned OS-specific, native app does seem like it is going backwards in time. Then again, apps are big in mobile and native desktop apps do access the real filesystem.... – Ray Toal Aug 04 '11 at 01:06
  • @Ray, there's lots of server side js stuff, which includes everything from file access to database drivers and everything you know from java or any other such server/normal technology (except concurrency control, but that doesn't affect the question of whether it can be compiled). – davin Aug 04 '11 at 01:15
  • Yep, used Node. Thanks, though. :-) – Ray Toal Aug 04 '11 at 01:18
1

Read this ... and do not miss the comments.

Here are also some options.

Community
  • 1
  • 1
Alfonso Rubalcava
  • 2,237
  • 17
  • 26
  • This looks interesting, though it compiles into a .NET application to run on the CLR, not on the hardware. – Justin808 Aug 04 '11 at 01:33
0

Yes you have something called Google Closure Compiler but its not a compiler in the conventional sense,it doesnt convert javascript into machine code but converts javascript into javascript but highly optimized javascript. Its actually an optimizing compiler.Also the compiler runs some tests to detect errors like typos much like the tool JSLint.But Google advises to use this compiler on javascript written in Closure Library. see this for more on Closure Compiler.

But i dont think compiling client-side javascript to machine code is a good idea because machine code is machine dependent so then before you send javascript to the client you have to detects its OS and its processor architecture. So this would become like javascipt for firefox on linux,javascipt for firefox on windows,javascipt for firefox on x86,etc

lovesh
  • 5,235
  • 9
  • 62
  • 93
  • Yeah, I saw lots of these types of compilers, not any that compile down to machine executable code though. – Justin808 Aug 04 '11 at 01:03
  • Very true, I wasn't thinking of using it in that situation though. I was thinking more of using javascript as a language to program a device with an intel or other cpu in it. – Justin808 Aug 04 '11 at 03:39