0

Can a compiler be written that took javascript and compiled it into a fluent program, making it more efficient?

I know of just-in-time compilation, but what I'm talking about is taking the javascript and compiling it similar to the way an OS application is compiled.

Just a question for an experiment.

  • How would you pass the compiled code to the browser? – John Dvorak Jan 08 '14 at 20:28
  • @JanDvorak: OP didn't talk about a browser environment specifically. – Bergi Jan 08 '14 at 20:35
  • 1
    @bergi et al this is *not* the same question as the one referenced. This question asks "is it possible in principle" while the referenced one is about "can it be done with V8". Different scopes makes for different questions. – miraculixx Jan 09 '14 at 09:40
  • @miraculixx: Yeah, maybe (I have voted for reopening now since there's a good answer as well). Still, I think that the other question answers the basic question (It's possible, but you need some way to handle the dynamic aspects). See also [Is there a native machine code compiler for JavaScript?](http://stackoverflow.com/questions/1118138/is-there-a-native-machine-code-compiler-for-javascript) – Bergi Jan 09 '14 at 11:19
  • You may find [this answer](https://stackoverflow.com/questions/67897358/compile-nodejs-to-binary/69954743#69954743) useful. – Adonis Gaitatzis Nov 13 '21 at 14:24

1 Answers1

2

This question has been previously discussed here and here (and presumably in many other places).

The gist of it is this:

  • JavaScript's language specification does not claim for it to be an interpreted language. It merely defines the syntax and semantics of the language.

  • Most interpreted languages are first parsed and translated into an intermediary form a.k.a. Byte Code. This process is commonly referred to as "compiling" and that's the job of a compiler *)

  • Some interpreters of JavaScript are actually compilers / byte code executors, e.g. Google's V8 ("V8 compiles and executes JavaScript source code,..."). Microsoft's Jurassic for compiling .NET to CLI bytecode on the other hand claims to be just a compiler.

  • Subsequent execution of the program uses the byte code and makes no reference back to the program's JavaScript source code. Jurasic claims to distribute a .NET CLI assembly.

The answer to your question is yes and no, depending on the specific platform you want to target:

  • yes, JavaScript code can be compiled into intermediary forms that can then be executed by an engine that knows to process this particular form of byte code.

  • no, there is no generally accepted byte code nor a generic interpreter (a.k.a. Virtual Machine) of such byte code.

Having said that, the issue of "compiled" v.s. "interpreted" languages often seems very much overstated. It is important to realize that there are only gradual differences on how a "binary" as a result of compilation differs from its corresponding "source code". At the end, source and binary are the same thing in different forms, or to paraphrase: it's symbols all the way down.

*) Wikipedia has this definition of a compiler:

A compiler is a computer program (or set of programs) that transforms source code written in a programming language (the source language) into another computer language (the target language, often having a binary form known as object code).

Community
  • 1
  • 1
miraculixx
  • 10,034
  • 2
  • 41
  • 60
  • 1
    Thank you. I did some searching, clearly not thoroughly enough, although it's worth pointing out that these questions ( http://stackoverflow.com/questions/9623813/is-javascript-compiled-or-an-interpreted-language and http://programmers.stackexchange.com/questions/138521/is-javascript-interpreted-by-design ) do not give answers nearly as satisfactory nor well-rounded as this one. –  Jan 08 '14 at 23:48