gpt4 book ai didi

javascript - V8 实际上在哪里使用原始 javascript 代码?

转载 作者:行者123 更新时间:2023-12-05 02:06:25 25 4
gpt4 key购买 nike

我试图了解 v8 是如何工作的,但我无法在代码中找到它实际获取输入原始 js 脚本以对其进行解析并将其编译为 C++ 的位置。

我看过 api.cc 并尝试在编译器函数中设置一个断点但没有运气(我使用 chromium 这样做),它从来没有命中这个函数。

MaybeLocal<Script> ScriptCompiler::Compile(Local<Context> context,
Source* source,
CompileOptions options,
NoCacheReason no_cache_reason)

***** 更新 ****

@jmrk 回复后,我一直在试图弄清楚 JS 实际上是从哪里开始进入的,我真正感兴趣的是了解网站如何呈现,然后将脚本传递到 V8 中进行编译。我已经找到了很多关于该主题的信息,但我仍然无法理解全貌:

原来第一步不是解析器而是扫描器,它获取 UTF-16 流作为输入。

The source code is first broken up in chunks; each chunk may beassociated with a different encoding. A stream then unifies all chunksunder the UTF-16 encoding.

Prior to parsing, the scanner then breaks up the UTF-16 stream intotokens. A token is the smallest unit of a script that has semanticmeaning. There are several categories of tokens, including whitespace(used for automatic semicolon insertion), identifiers, keywords, andsurrogate pairs (combined to make identifiers only when the pair isnot recognized as anything else). These tokens are then fed first tothe preparser and then to the parser.

https://blog.logrocket.com/how-javascript-works-optimizing-for-parsing-efficiency/

我还发现它确实从 Blink 获取了这个流:

enter image description here

he UTF16CharacterStream provides a (possibly buffered) UTF-16 view over the underlying Latin1, UTF-8, or UTF-16 encoding that V8 receives from Chrome, which Chrome in turn received from the network. In addition to supporting more than one encoding, the separation between scanner and character stream allows V8 to transparently scan as if the entire source is available, even though we may only have received a portion of the data over the network so far.

https://v8.dev/blog/scanner

扫描器似乎也将 token 提供给解析器:

V8’s parser consumes ‘tokens’ provided by the ‘scanner’. Tokens areblocks of one or more characters that have a single semantic meaning:a string, an identifier, an operator like ++. The scanner constructsthese tokens by combining consecutive characters in an underlyingcharacter stream.

但问题仍然存在,从 blink 进入 V8 的 Javascript 原始代码在哪里?我如何查看 chrome 读取的内容以及它在何处初始化 v8

最佳答案

这很复杂:-)

ScriptCompiler::Compile 作为最外层的入口点通常是正确的。请注意,它有两个重载。此外,Chrome 在可能的情况下尝试进行流式编译,这采用了不同的路径。此外,在使用 Chrome/Chromium 时,请注意您必须在渲染器进程中设置断点,而不是浏览器进程。

在探索 V8 时,使用 d8 shell 会更容易。在 d8.cc 中查找 Shell::ExecuteString(它调用 ScriptCompiler::Compile)。

此外,需要澄清的是,V8 不会将 JavaScript 编译为 C++。它首先将其编译成自己的内部字节码格式,由“Ignition”解释器执行;热门函数随后由“Turbofan”优化编译器编译为机器代码。

如果您无法理解整个管道,请不要气馁。没有一个人这样做; V8 太大太复杂了。专注于您感兴趣的内容(解析器?解释器?优化编译器?)并深入研究。

关于javascript - V8 实际上在哪里使用原始 javascript 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62778540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com