V8的ScriptCompiler::CachedData是字节码还是机器代码?

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

Is V8's ScriptCompiler::CachedData bytecode or machine code?

问题

更一般地说,由V8编译的JavaScript文件是以V8字节码形式还是特定主机的机器码形式保存到磁盘的?

我正在尝试理解node-webkit的nwjc是如何工作的。我指的是这个函数:https://github.com/nwjs/v8/blob/nw75/src/nwjc.cc#L168-L183

英文:

Or rather more generally, are the javascript files compiled by V8 saved to disk as V8 bytecode or as host-specific machine code?

I'm trying to understand how node-webkit's nwjc works. The function I'm referring to is https://github.com/nwjs/v8/blob/nw75/src/nwjc.cc#L168-L183

答案1

得分: 1

你必须在每个操作系统和架构上运行nwjc(包括Win 32位、Win 64位等)。它生成的bin快照文件只能在相同平台上的NW.js上运行。而且它必须与NW.js版本匹配(以确保V8版本一致)。根据我的理解(可能有误),它会将你的JS代码加载到V8引擎中,然后对其进行解析,并为在操作系统内存中运行的V8引擎创建快照。因此,它听起来是与机器相关的代码。如果只是V8字节码,那么你可以在具有相同V8版本的任何操作系统上运行它,但实际情况并非如此。

英文:

You must run nwjc on each OS, and arch (Win 32-Bit, Win 64-bit, etc). The bin snapshot file it generates will only run in NW.js on that platform. Also it has to match the NW.js version (so the V8 versions match). From my understanding (may be wrong), it loads your JS into the V8 engine which then parses it, then it snapshots that parsed version for V8 that is running in the OS's memory. So it sounds like machine-specific code. If it was just V8 bytecode, then you'd be able to run it on any OS's with the same version of V8, but that isn't the case.

答案2

得分: 1

(V8开发人员。)

我自己没有在V8的这个领域工作过,但就我从查看代码(v8::internal::CodeSerializer::Serialize及其调用者)中了解的情况来看,缓存的数据只包含字节码,没有机器代码。也就是说,有一些评论指出未来可能还会缓存基线机器代码。格式肯定是自定义的(未指定);除了字节码本身,它还包含字节码引用的特定对象。

我不知道缓存数据中实际上是否有与特定平台相关的内容。很可能有。或者过去可能有。正如有关基线机器代码的注释所示,未来可能也会有一些。这确实是V8未设计的用例,也没有官方支持,因此V8不承诺缓存数据的可移植性有多强,最好谨慎行事。
(与版本相关的情况类似。对于大多数版本升级,缓存的数据可能是兼容的。但有时可能不兼容,没有人跟踪这种情况何时发生,因此安全起见,最好始终要求精确的版本匹配。)

英文:

(V8 developer here.)

I haven't worked on this area of V8 myself, but as far as I can tell from looking at the code (v8::internal::CodeSerializer::Serialize and its callees), the cached data contains only bytecode, no machine code. That said, there are a few comments indicating that in the future, baseline machine code might also get cached. The format is definitely custom (and unspecified); along with the bytecode itself it contains certain objects that are referenced from the bytecode.

I don't know whether there actually is anything platform-specific in the cached data. There might well be. Or maybe there was in the past. As the comments about baseline machine code indicate, there might also be some in the future. This really is a use case that V8 wasn't designed for, and that isn't officially supported, so V8 makes no promises about just how portable the cached data might be, and it's better to be safe than sorry.
(Something similar holds for the version-specificity. For most version upgrades, the cached data will probably be compatible. But sometimes it won't be, and nobody tracks when that will be the case, so the safe move is to always require an exact version match.)

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

发表评论

匿名网友

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

确定