Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am cautious of asking this question because it might appear overly fastidious. I just opened up JavaScript: The Definitive Guide, and it states of the first page of chapter 1

"JavaScript is a high-level, dynamic, untyped interpreted programming language”

So am I to take it that the interpreted part is a requirement in the language specification, or is it misleading to say that the language is an interpreted programming language when respecting the difference between a language and its many implementations?

There are no static compilers for JavaScript apparently - Is there a native machine code compiler for JavaScript? so maybe it's just a reflection of this.

share|improve this question

migrated from stackoverflow.com Mar 6 '12 at 15:58

This question came from our site for professional and enthusiast programmers.

    
There was a jscript.net for a while that was similar to AS3 / the "lost" ES4. It was bytecode-compiled to CIL. –  Hey Mar 6 '12 at 15:53
4  
V8 explicitly claims not to be an interpreter but a compiler. –  pimvdb Mar 6 '12 at 15:56
    
@GGG JScript.Net is still alive and...sickly. But still alive. msdn.microsoft.com/en-us/library/72bd815a.aspx –  Jetti Mar 6 '12 at 16:18
    
FWIW, the "untyped" bit isn't strictly true either –  Rob Agar Mar 6 '12 at 16:54
    
Firefox had just released the first browser-based JIT compiler the year that question was answered in FF 3.5 so it probably wasn't widely known about at the time. I believe modern JITs actually do a lot of compiling (or at least prep for compile) on the first pass of a JS document to do things like identify and cache methods that are isolated to a given scope. –  Erik Reppen Jun 3 '13 at 18:45

3 Answers 3

up vote 11 down vote accepted

So am I to take it that the interpreted part is a requirement in the language specification, or is it misleading to say that the language is an interpreted programming language when respecting the difference between a language and its many implementations?

EcmaScript language geeks often use the term "ES interpreter" to refer to an implementation of EcmaScript, but the spec does not use that term. The language overview in particular describes the language in interpreter-agnostic terms:

ECMAScript is object-based: basic language and host facilities are provided by objects, and an ECMAScript program is a cluster of communicating objects.

So EcmaScript assumes a "host environment" which is defined as a provider of object definitions including all those that allow I/O or any other links to the outside world, but does not require an interpreter.

The semantics of statements and expressions in the language are defined in terms of completion specification which are trivially implemented in an interpreter, but the specification does not require that.

8.9 The Completion Specification Type

The Completion type is used to explain the behaviour of statements (break, continue, return and throw) that perform nonlocal transfers of control. Values of the Completion type are triples of the form (type, value, target), where type is one of normal, break, continue, return, or throw, value is any ECMAScript language value or empty, and target is any ECMAScript identifier or empty.

The term “abrupt completion” refers to any completion with a type other than normal.

The non-local transfers of control can be converted to arrays of instructions with jumps allowing for native or byte-code compilation.


There are no static compilers for JavaScript apparently

This is not true. The V8 "interpreter" compiles to native code internally, Rhino optionally compiles to Java bytecode internally, and various Mozilla interpreters ({Trace,Spider,Jager}Monkey) use a JIT compiler.

V8:

V8 increases performance by compiling JavaScript to native machine code before executing it, versus executing bytecode or interpreting it.

Rhino:

public final void setOptimizationLevel(int optimizationLevel)

Set the current optimization level. The optimization level is expected to be an integer between -1 and 9. Any negative values will be interpreted as -1, and any values greater than 9 will be interpreted as 9. An optimization level of -1 indicates that interpretive mode will always be used. Levels 0 through 9 indicate that class files may be generated. Higher optimization levels trade off compile time performance for runtime performance. The optimizer level can't be set greater than -1 if the optimizer package doesn't exist at run time.

TraceMonkey:

TraceMonkey adds native‐code compilation to Mozilla’s JavaScript® engine (known as “SpiderMonkey”). It is based on a technique developed at UC Irvine called “trace trees”, and building on code and ideas shared with the Tamarin Tracing project. The net result is a massive speed increase both in the browser chrome and Web‐page content.

share|improve this answer
    
Thanks for this answer, it actually answers the question. I suppose the final comment about no static compilation is what caused the buzz about which implementations actually compile code and which ones do not. All I was interested in was the validity of the statement "JavaScript is an interpreted language" which, given implementation citations and the lack of definition by the spec, appears to be false. Not encouraging for the second paragraph of a "Definitive Guide", but I guess I will stick with it. –  Matt Esch Mar 6 '12 at 18:30
    
@me232, the statement was substantially true prior to 2008. Rhino pre-dates that but was not a major interpreter and so few would have faulted "the Definitive Guide" at the time for ignoring it. I haven't read the book, so I can't comment on how representative that sentence is of its overall quality. –  Mike Samuel Mar 6 '12 at 20:58

The V8 JavaScript VM used in Chrome doesn't include an interpreter. Instead it consists of two compilers and compiles the code on the fly. One of the compilers runs quickly but generates inefficient code, the other is an optimizing compiler.

I can understand why some people would consider this "cheating", since V8 takes source code as input every time the code runs and the user has to have V8 installed. But consider a compiler which emits an executable which includes a complete interpreter and bytecode. Then you would have a stand-alone program. It just wouldn't be very efficient.

share|improve this answer

No, this is interpreted by design, otherwise you couldn't as simply work with the variables, objects, delegates and other things.

So if you write a compiler for javascript you'll probably lose a lot of its features.

There is a "compiler" but anyways it just "compile" from javascript to javascript ;)

https://developers.google.com/closure/compiler/

share|improve this answer
1  
Nobody would miss eval() though... –  Phil H Mar 6 '12 at 15:55
4  
"otherwise you couldn't as simply work with the variables, objects, delegates and other things." - many functional languages have these items and have been compiled for a long time. –  Jetti Mar 6 '12 at 16:20
2  
Closure Compiler is not for the purpose of compiling JS for execution. It's for minimizing JS code. –  Orbling Mar 6 '12 at 16:41
    
@Orbling Closure is actually about parsing JS to an AST and turning the AST into optimized JavaScript. It's a full compiler, not just a minifier. It's just that the backend of the closure compiler outputs minified javascript –  Raynos Mar 6 '12 at 18:27
    
@Raynos: Yes, that's why I carefully worded my expression to say "for execution". As it is not used for that purpose, just for minimizing/optimising. –  Orbling Mar 6 '12 at 19:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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