最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

compiler construction - Dart vs JavaScript - Are they compiled or interpreted languages? - Stack Overflow

programmeradmin7浏览0评论

Is Dart considered to be a piled or an interpreted language? The same question holds for JavaScript.

The reason for the question:

I've been watching an interview with the founders of dart, and in 7:10 Lars Bak said that:

"When you [...] in a JavaScript program, you actually execute JavaScript before you start running the real program. In Dart, you don't execute anything before the first instruction in main is being executed".

It sounded to me that he's saying that JavaScript is a piled language while Dart is an interpreted language. Is it true?

Isn't the Dart VM a piler?

Is Dart considered to be a piled or an interpreted language? The same question holds for JavaScript.

The reason for the question:

I've been watching an interview with the founders of dart, and in 7:10 Lars Bak said that:

"When you [...] in a JavaScript program, you actually execute JavaScript before you start running the real program. In Dart, you don't execute anything before the first instruction in main is being executed".

It sounded to me that he's saying that JavaScript is a piled language while Dart is an interpreted language. Is it true?

Isn't the Dart VM a piler?

Share Improve this question edited Jul 12, 2013 at 7:46 Scott 21.5k8 gold badges66 silver badges72 bronze badges asked Jul 11, 2013 at 19:39 CheshieCheshie 2,8376 gold badges36 silver badges52 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 14

Depends on the definition of "interpreted" and "piled" language. And even then, it always depends on the implementation.

What Lars meant is that JavaScript builds its class structures (and other global state) by executing code. In Dart the global state is described by the language syntax and thus only needs parsing (and even then most of it can be skipped first). As a consequence Dart programs can start executing "real" code faster than JavaScript programs.

This obviously only holds for the Dart VM, since programs that have been piled to JavaScript must use JavaScript mechanisms to build their classes.

Edit (more details):

Take, for example, the following extremely simple class A:

In Dart:

class A {
  final x;
  A(this.x);
  foo(y) => y + x;
}

In JavaScript:

function A(x) { this.x = x; }
A.prototype.foo = function(y) { return y + this.x; }

When the Dart VM starts up it starts by going through the program. It sees the class keyword, reads the class-name (A) and could then simply skip to the end of the class (by counting opening and closing braces, making sure they are not in strings). It does not care for the contents of A, until A is actually instantiated. Now, in reality, it actually looks through the class and finds all the members, but it does not read the contents of methods until they are needed. In any case: it does this in a very fast processing step.

In JavaScript things get more plicated: a fast VM can skip the actual body of the function A (similar to what Dart does), but when it sees A.prototype.foo = ... it needs to execute code to create the prototype object. That is, it needs to allocate a function object (A), look up its prototype property, change this object (adding a new property) with a new function object. In other words: in order to even see that you have a class you need to execute code.

Dart as programming language in its primary implementation can be presented as a virtual machine (VM), which is the runtime of programs written in that language.

Current virtual machine implemented as the "just-in-time" (JIT) runtime environment engine.

This means that program not interpreted but piled. But this pilation process (translating source code to machine instructions) is stretched in time for an unknown period.

This allows virtual machine to defer performing certain operations indefinitely or never performing them.

Assume you have a very big and plex program with a lot of classes which may be never be used in current short lifetime session of program execution.

JIT pilation allow not pile all unused classes but just parse it to special tokens. These tokens later will be used (on demand) for translating them to intermadiate language for constructing machine code.

This process is transparent for user of program. Compiled (to machine code) only that source code that required for the correct working of the program.

Some source code can be never piled what save a lot of time.

Conclusion:

If Dart language used in its primary state as virtual machine then it piled to machine code.

Dart piles into JavaScript, and JavaScript is an interpreted language. Usually, by 'piled' languages one understands languages which are piled into platform-specific machine code, run right on the CPU and don't require an interpreter to run, which is not the case for neither JS nor Dart. So I would say that both JS and Dart are interpreted.

发布评论

评论列表(0)

  1. 暂无评论