Hi, I am trying to use V8 snapshot to reduce my React-Native app's startup time. Unfortunately, when I create an Isolate and a context, then run a .js file, I just get the error
"__fbBatchedBridgeConfig is not set, cannot invoke native modules". It seems the context needs some native modules. I wonder is it possible to use V8 snapshot on a React Native app? I would appreciate it for your reply.
Here is my code:
v8::StartupData CreateSnapshotDataBlob(const char* embedded_source) {
// Create a new isolate and a new context from scratch, optionally run
// a script to embed, and serialize to create a snapshot blob.
v8::StartupData result = {nullptr, 0};
{
v8::Isolate* isolate = v8::Isolate::Allocate();
v8::SnapshotCreator snapshot_creator(isolate);
{
v8::HandleScope scope(isolate);
v8::Local<v8::Context> context = v8::Context::New(isolate);
if (embedded_source != nullptr &&
!RunExtraCode(isolate, context, embedded_source, "<embedded>")) {
return result;
}
snapshot_creator.SetDefaultContext(context);
}
result = snapshot_creator.CreateBlob(
v8::SnapshotCreator::FunctionCodeHandling::kClear);
}
return result;
}
bool RunExtraCode(v8::Isolate* isolate, v8::Local<v8::Context> context, const char* utf8_source, const char* name) {
v8::Context::Scope context_scope(context);
v8::TryCatch try_catch(isolate);
v8::Local<v8::String> source_string;
if (!v8::String::NewFromUtf8(isolate, utf8_source, v8::NewStringType::kNormal).ToLocal(&source_string)) {
return false;
}
v8::Local<v8::String> resource_name = v8::String::NewFromUtf8(isolate, name, v8::NewStringType::kNormal).ToLocalChecked();
v8::ScriptOrigin origin(resource_name);
v8::ScriptCompiler::Source source(source_string, origin);
v8::Local<v8::Script> compileScript;
if (!v8::ScriptCompiler::Compile(context, &source).ToLocal(&compileScript)) {
return false;
}
if (compileScript->Run(context).IsEmpty()) {
// this line will be executed, and the exception is "__fbBatchedBridgeConfig is not set, cannot invoke native modules". It seems the context need some native modules"
std::string exception = *v8::String::Utf8Value(isolate, try_catch.Exception());
return false;
}
std::cout << !try_catch.HasCaught() << std::endl;
return true;
}