I'm struggling a bit with the code base of nodejs + v8.
The Goal is to get the bytecode of a function / module (looking at the code they are the same) and disassemble it using the BytecodeArray::Disassemble
function, possibly, without side effects, a.k.a, executing the code.
The problem is that it's not clear how to get the bytecode in the first place.
(V8 developer here.) V8's API does not provide access to functions' bytecode. That's intentional, because bytecode is an internal implementation detail. For inspecting bytecode, the --print-bytecode
flag is the way to go.
If you insist on mucking with internal details, then of course you can circumvent the public API and poke at V8's internals. From a v8::internal::JSFunction
you can get to the v8::internal::SharedFunctionInfo
, check whether it HasBytecodeArray()
, and if so, call GetBytecodeArray()
on it. Disassembling bytecode never has side effects, and never executes the bytecode. It's entirely possible that a function doesn't have bytecode at a given moment in time -- bytecode is created lazily when it's needed, and thrown away if it hasn't been used in a while. If you dig far enough, you can interfere with those mechanisms too, but...:
Needless to say, accessing internal details is totally unsupported, not recommended, and even if you get it to work in Node version x.y, it may break in x.(y+1), because that's what "internal details" means.