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

Javascript - Compiled language? - Stack Overflow

programmeradmin2浏览0评论

I am new to Web development, and I am studying JavaScript.

From a course at Stanford:

JavaScript is an interpreted language, not a piled language. A program such as C++ or Java needs to be piled before it is run. The source code is passed through a program called a piler, which translates it into bytecode that the machine understands and can execute. In contrast, JavaScript has no pilation step. Instead, an interpreter in the browser reads over the JavaScript code, interprets each line, and runs it. More modern browsers use a technology known as Just-In-Time (JIT) pilation, which piles JavaScript to executable bytecode just as it is about to run.

And from You Don't Know JS: Scope & Closures by Kyle Simpson:

... but despite the fact that JavaScript falls under the general category of “dynamic” or “interpreted” languages, it is in fact a piled language.

Let’s just say, for simplicity sake, that any snippet of JavaScript has to be piled before (usually right before!) it’s executed. So, the JS piler will take the program var a = 2; and pile it first, and then be ready to execute it, usually right away.

And from some questions at Stack Overflow, there are some ideas like: It depend on an actual implementation of the language.

Do you have any ideas?

I am new to Web development, and I am studying JavaScript.

From a course at Stanford:

JavaScript is an interpreted language, not a piled language. A program such as C++ or Java needs to be piled before it is run. The source code is passed through a program called a piler, which translates it into bytecode that the machine understands and can execute. In contrast, JavaScript has no pilation step. Instead, an interpreter in the browser reads over the JavaScript code, interprets each line, and runs it. More modern browsers use a technology known as Just-In-Time (JIT) pilation, which piles JavaScript to executable bytecode just as it is about to run.

And from You Don't Know JS: Scope & Closures by Kyle Simpson:

... but despite the fact that JavaScript falls under the general category of “dynamic” or “interpreted” languages, it is in fact a piled language.

Let’s just say, for simplicity sake, that any snippet of JavaScript has to be piled before (usually right before!) it’s executed. So, the JS piler will take the program var a = 2; and pile it first, and then be ready to execute it, usually right away.

And from some questions at Stack Overflow, there are some ideas like: It depend on an actual implementation of the language.

Do you have any ideas?

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Nov 3, 2014 at 7:20 haitranhaitran 7351 gold badge6 silver badges19 bronze badges 9
  • stackoverflow./questions/9623813/… – Thilo Commented Nov 3, 2014 at 7:24
  • @Thilo, I have read this yet. But how about the book and the course's definition? – haitran Commented Nov 3, 2014 at 7:25
  • Can you paste these two definitions here? – Thilo Commented Nov 3, 2014 at 7:29
  • Yes, I'm already quoted those definitions in my questions, but if you need, I will edit with the full version! – haitran Commented Nov 3, 2014 at 7:34
  • Do you have the next sentence from that book, where they actually explain this assertion about "piled language"? – Thilo Commented Nov 3, 2014 at 7:42
 |  Show 4 more ments

3 Answers 3

Reset to default 5

Chrome browser uses V8 engine for piling Javascript just as other browsers may use Rhino Or SpiderMonkey.

V8 is a JavaScript engine built by Google written in C++. It is used for piling JS, in both client-side (Google Chrome) and server-side (node.js) applications. In order to obtain speed, V8 translates JavaScript code into more efficient machine code instead of using an interpreter.

V8 piles JavaScript code into machine code at script execution by implementing a JIT (Just-In-Time) piler like a lot of modern JavaScript engines such as SpiderMonkey or Rhino (Mozilla) are doing. The main difference with V8 is that it doesn’t produce bytecode or any intermediate code. It just piles JavaScript on the fly.

Hope this helps!

Well, you can probably get into semantics and terminology differences, but two important points:

  • Javascript (in a web page) is distributed in its source code form (or at least in minimized text form) and not as a binary piled ahead-of-time

  • Javascript is not piled into executable machine code even by the browser (although some parts of it may be these days as a performance optimization), but executed via a virtual machine

The term "piled" used in this context refers to a language that is normally assumed to be translated directly into the language or format native to the machine it is going to be run on. Typical cases include both C and C++. But, understand that both of these languages can also be interpreted. An example of a C interpreter is Pico C, which handles a subset of C.

So, the real question is about environments. A language may be run in a piled or interpreted environment. A clear distinction between the two cases can be made by the following test:

    Does the language possess a "mand level" mode
    in which forward references are inherently impossible?

Think about this for a moment. A language that is interpreted is reading its specification in real time. A forward reference is to something that does not exist at the time the specification is made. Since machines have not (yet) been endowed with the facility of precognition or time travel (i.e. "time loop logic"), then such references are inherently unresolvable.

If such a level is defined as a mandatory part of the language, then the language may be said to be interpreted; otherwise, it may be said to be piled. BASIC is interpreted, as some of its mands make direct reference to this layer (e.g. the "list" mand). Similarly, the high-level AI language, Prolog, is - by this criterion - an interpreted language, since it also possesses mands that make direct reference to this layer. The "?-" mand, itself, is an actual prompt, for instance; but its database mands also refer to and maintain the current state of the mand-level layer.

However, this does not preclude parts of an interpreted language from being subject to pilation or to the methods used by pilers, or a piled language from being run at a mand mode level. In effect, that's what a debugger for a language like C or C++ already is, just to give an example.

Most languages that are defined to have a mand level layer, normally have to pile to something. In particular, if the language satisfies the following condition, then it is almost mandatory that at least parts of it pile into something:

    Does the language possess a facility for user-defined codelets,
    for instance: subroutines, functions, lambdas, etc.?

The reason is simple: where are you going to put that code, after it's defined before it's used, and in what format? It is extremely inefficient to save and run it verbatim, so normally it will be translated into another form that is either: (a) a language-internal normal form (which which case, the rest of the language may be considered as "syntactic sugar" for the reduced subset language that the normal forms reside in), (b) into a language-external normal form (i.e. "byte-code"), or (c) a bination of both - it may do language-internal normalization first, before translating it into byte code.

So, most "interpreted" languages are piled - into something. The only real question is: (1) what they are piled into, and (2) when/how does the code that it is piled into run - which is connected to the issue of the above-mentioned "mand level" mode.

If the codelets are being piled into a target-independent form - which is what is normally what is referred to when speaking of "byte code" - then it is not "piled" in the sense the term is normally taken to refer to. The term piled normally refers to translation into the language that is native to the machine that the language is being run on. In that case, there will be as many translators are there are types of machines that the language may run on - the translator is inherently machine-dependent.

The two cases are not mutually exclusive. So, a byte-code translator may appear as a stage for native-code pilation, whereby the codelets of an interpreted language are translated and stored directly in the native language of the machine that the language is being run on. That's called "Just In Time" pilation (or JIT).

The distinction is a bit blurry. Even piled languages, like C or C++, may run on systems that have codelets that are either piled or even pre-piled that are loaded while the program is running.

I don't know enough about JS (yet) to say anything definitive about it - other than what can be inferred from observation.

First, since JS code is stored as codelets and is normally run in web clients on a need-to-use basis, it is likely that an implementation of will pile (or pre-pile) the codelets into an intermediate byte-code form.

Second, for reasons of security, it is unlikely that it will pile directly into the native code of the machine it is running on, since this may promise the security of the machine by providing leaks through which malicious code can be sneaked into and through. That's the "sandbox" feature that browsers are supposed to adhere to.

Third, it is not normally used directly by a person on the other end as a language like Basic or even Prolog is used. However, in many (or even most) implementations it does have a "debug" mode. The browser, for instance, may allow even an ordinary user to both view and edit/debug JS code. Notwithstanding that, there really isn't a mand-layer, per se, other than what appears in a web browser itself. Unresolved here is the question of whether the browser allows forward references in JS code. If it does, then it's not really a mand level environment. But it may be browser-dependent. It might, for instance, load in an entire web page before ever starting up any JS code, rather than trying to run the JS in real time while a page is loading, in which case forward references would be possible.

Fourth, if the language wants to be efficient in terms of its execution speed, it will have some form of JIT - but this would require stringent validation of the JS piler itself to ensure that nothing can slip out of the "sandbox" through the JIT into forbidden code on the host machine.

I'm pretty sure there are JS editors/interpreters out there, simply to have a way to develop JS. But I don't know if any references to a mand-layer are a mandatory part of the specification for JS. If such specifications exist, then we can call it a bona fide interpreted language. Otherwise, it straddles the border line between the two language types as a language meant to be run in real time like an interpreted language, but which permits pilation directly to the native code of the machine it is running on.

The issue came to a head, for me, recently when I tried to directly translate an old stand-by text-based game (lunar lander) directly from the (interpreted) language FOCAL into C-BC (a C-like extension to POSIX BC whose source is located on GitHub here https://github./RockBrentwood/CBC). C-BC, like POSIX BC, is interpreted but allows user-defined codelets, so that implementations of BC normally define a "byte code" language to go with it (historically: this was "dc").

The FOCAL language has a run-time language - which theoretically could be piled, but also a mand-layer subset (e.g. the "library" or "erase" mands) which does not permit forward references that haven't yet been defined, though the run-time language permits forward references.

Unlike GNU-BC, C-BC has goto statements and labels, so it is possible to directly translate the game. However, at the mand level (which in a BC file is the top level in the file's scope), this is not possible, since the the top-level of a file's code is - as far as a BC interpreter is concerned - making reference to things that might not yet exist, since the program could just as well have been being entered by a user in real-time. Instead, the entire source would have to be enclosed into { ... } brackets - which gets piled, in its entirety, to byte-code first, before being executed. So, that's an example of a user-defined codelet, and a text-book example of why most interpreted languages have to have some facility for piling into something.

发布评论

评论列表(0)

  1. 暂无评论