Search code examples
c++embedded-v8

How am I expected to use v8::SetNativesDataBlob?


Investigating a segfault in my code, looking for GetBuiltinsCount in the V8 source code leads me to this comment:

/**
 * NativesStore stores the 'native' (builtin) JS libraries.
 *
 * NativesStore needs to be initialized before using V8, usually by the
 * embedder calling v8::SetNativesDataBlob, which calls SetNativesFromFile
 * below.
 */

How am I, the embedder supposed to use v8::SetNativesDataBlob?

The d8 interpreter does call this method, but it's not at all clear what it's doing and why. The basic samples do not call this method.


Solution

  • A bit over a year later, I find myself back at this question, and now I know the answer.

    You can compile V8 with external startup data ("snapshot") or without.

    If you compiled with snapshot data, call V8::InitializeExternalStartupData as shown in the Hello World example code. You don't call v8::SetNativesDataBlob directly.

    Otherwise, the solution is to compile without snapshot data. Then, you don't need to call either of the aforementioned functions at all. Here's one answer on how to configure this in your build process. Note, using snapshot data decreases process start-up time.