1

The question title speaks for itself. I'm specifically thinking in terms of a Node.js server. Additionally, this question is assuming that you don't need eval().

Also, if this is not possible, why not?

1
  • Uhm. Node.js uses the V8 engine, which is a compiler. Aug 28, 2014 at 22:23

3 Answers 3

6

It's easy to compile any language, no matter how dynamic, to machine code. It just won't be efficient machine code, since it will usually do a lot of dispatching based on run time type tags and looking up values in hash tables, much like a (bytecode) interpreter.

In the simplest case, just compile C code like

#include <some_js_vm.h>

static char[] source = "... JavaScript ...";

int main() {
  js_run(source);
  return 0;
}

For JavaScript without eval, there are still several features that require such inefficient machine code when compiled statically:

  • with, which modifies scope and semantics of variables based on an object's members. This is only a problem because of the next point.
  • The fields of any object are dynamic (and no, you can't always infer them), meaning code can and will add and remove fields at run time. So objects remain glorified hash tables, and since your compiler is static it can't really pull tricks like "hidden classes" in V8 (which greatly speeds up attribute access for most objects).
  • new Function(str) and other moral equivalents to eval.
  • The whole prototype system, and hence all methods are set up at run time, meaning you can't use vtables or such, or even assume you know all prototypes.
3
  • 3
    Your "some_js_vm" would be interpreting it anyway, so your javascript really isn't compiled. Aug 28, 2014 at 15:40
  • 1
    @whatsisname I guess it depends on how you define "compiled". Would unrolling the interpreter loop be compiled? Would slightly optimizing the unrolled interpreter loop make it compiled? How about if I optimize it some more? Where is the line? The program above is turned into a standalone binary that performs the same function as the JS program, and any such program will necessarily contain interpreter-like code sequences. Calling it compiled is justified IMHO.
    – user7043
    Aug 28, 2014 at 18:14
  • 3
    Obviously ignoring the semantic minefield we are approaching, since the major C implementations compile to native machine instructions, unless your some_js_vm.h results in your JS results in native machine instructions as well (through some amazing preprocessor magic), I don't think your JS is compiled in any meaningful sense. Aug 28, 2014 at 18:41
5

In fact, Javascript is (inside several major browsers) often "compiled" using Just-in-Time compilation techniques (read about the V8 Javascript engine). But as delnan explained, there are case where it is not worthwhile. AFAIK some implementations use Tracing JIT compilation techniques.

And eval could be implemented by calling the compiler. Several Lisp implementations (notably SBCL) are doing that (and Lisp can be as dynamic as Javascript).

And asmjs.org defines a specification of a subset of Javascript which is designed to be easy to compile.

1
  • 1
    Note however that asm.js is only a subset of JavaScript in the sense that asm.js code can be run by JavaScript implementations. It's not a subset in the sense of being able to compile some JavaScript programs (only very few exceedingly trivial ones, and that by coincidence rather than design). It's better to think of it as "assembly language encoded to run in legacy browsers", not "JavaScript variant made to be compiled"
    – user7043
    Aug 28, 2014 at 18:16
-2

Node.js uses the V8 engine, which is a pure compiler. It never interprets. There isn't even an interpreter in V8, just two compilers.

1
  • 1
    Whether the mesh of JIT-compilation, run-time checks and assumptions with fallbacks that make up V8 is a compiler depends on the exact definition of the word. But calling it a "pure compiler" is just plain wrong. Sep 1, 2014 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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