0

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

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
MrSomeone
  • 71
  • 1
  • 11

2 Answers2

1

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.

  • I suppose so. But that's just speculation (and honestly I don't trust nodejs users). Is there anything in the actual V8 docs? – MrSomeone May 29 '23 at 18:16
  • Additionally, I don't think it's a snapshot file. I believe that's a separate binary format from what I'm looking at. – MrSomeone May 29 '23 at 18:19
1

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

jmrk
  • 34,271
  • 7
  • 59
  • 74
  • The format is unspecified, as in there's no document detailing what each opcode does. Is it possible to parse a header to at least identify all possible opcodes? – MrSomeone May 29 '23 at 22:49
  • 1
    @MrSomeone Sure, `src/interpreter/bytecodes.h` has them all, see `enum class Bytecode`. Of course this is considered an internal implementation detail, not part of the public API, and could change at any time without warning. – jmrk May 30 '23 at 16:55