Node.js在将JavaScript传递给V8之前进行解析吗?

huangapple go评论62阅读模式
英文:

Does nodejs parse JavaScript before feeding it to V8?

问题

Node.js有自己的解析器/解释器,能识别非JS标记并在将其余JavaScript传递给V8之前处理它们。

英文:

V8 does not understand things like "require" and "setTimeout", but Nodejs does. Hence, am I right that Nodejs has its own parser/interpreter, that identifies the non-js tokens and processes them before feeding the rest of the JavaScript to V8?

答案1

得分: 2

V8的嵌入器向全局对象添加嵌入式提供的函数(和/或其他属性)。它们可以使用V8的嵌入式API添加在C++中实现的功能;它们还可以执行定义全局函数和/或属性的JavaScript文件(就像在用户空间中执行的方式)。Node.js结合了这两种技术。

有关介绍,请参阅V8的文档

英文:

Embedders of V8 add embedder-provided functions (and/or other properties) to the global object. They can use V8's embedding API to add features that are implemented in C++; they can also execute JavaScript files that define global functions and/or properties (just like you would in user-space). Node.js uses a combination of both techniques.

See V8's docs for an introduction.

答案2

得分: 2

Node提供了一个“主机环境”,因此提供了全局对象(包括函数对象)和与JavaScript引擎的钩子。这涉及解析JavaScript。ECMA Script规范仅关注核心语言,并明确提到主机的功能超出了其范围。

您可以在ECMA Script规范的第4章中阅读有关此内容。以下是该部分的一些引用 - 但完整阅读它是值得的:

此处定义的ECMAScript并不打算成为计算自给自足的[...]。

ECMAScript现在用于为各种主机环境提供核心脚本功能。因此,核心语言在此文档中进行了规定,与任何特定的主机环境无关[...]。

为了帮助将ECMAScript集成到主机环境中,本规范将某些设施(例如,抽象操作)的定义推迟到本规范之外的源中,整体或部分推迟[...]。

换句话说,由主机定义的设施通常在外部规范中进一步定义[...]。

主机环境是所有由主机定义的设施的定义的特定选择。主机环境通常包括允许获取输入和提供输出的对象或函数,这些是全局对象的主机定义属性。

英文:

Node offers a "host environment", and as such provides global objects (including function objects) and hooks to the JavaScript engine. This does not involve parsing JavaScript. The ECMA Script specification focuses on the core language only, and specifically mentions that the capabilities of the host are out of its scope.

You can read about this in the ECMA Script specification, in chapter 4. Here are a few quotes from that section -- but it is worth to read it in full:

> ECMAScript as defined here is not intended to be computationally self-sufficient [...]
>
> ECMAScript is now used to provide core scripting capabilities for a variety of host environments. Therefore the core language is specified in this document apart from any particular host environment. [...]
>
> To aid integrating ECMAScript into host environments, this specification defers the definition of certain facilities (e.g., abstract operations), either in whole or in part, to a source outside of this specification. [...]
>
> In other words, facilities that are host-defined are often further defined in external specifications. [...]
>
> A host environment is a particular choice of definition for all host-defined facilities. A host environment typically includes objects or functions which allow obtaining input and providing output as host-defined properties of the global object.

huangapple
  • 本文由 发表于 2023年5月13日 21:32:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定