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

131 Views Asked by At

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

2

There are 2 best solutions below

2
jmrk On BEST ANSWER

(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.)

2
The Jared Wilcurt On

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.