V8 snapshot with React Native not work as expected

37 views
Skip to first unread message

王梓童

unread,
Jun 10, 2020, 7:29:35 AM6/10/20
to v8-...@googlegroups.com
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;
}

Yang Guo

unread,
Jun 10, 2020, 8:00:58 AM6/10/20
to v8-...@googlegroups.com, Jakob Gruber
V8 has the ability to serialize function bindings to native modules, e.g. described here. I'm not sure whether React Native uses it. Maybe +Jakob Gruber has some insights from past interaction?

Cheers,

Yang

--
--
v8-dev mailing list
v8-...@googlegroups.com
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-dev+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/v8-dev/CABaRidW6PGK8%2B_19_TApbi_PAKZDOeH0p961EU0a-Ya8b%3D2vjA%40mail.gmail.com.

Jakob Gruber

unread,
Jun 10, 2020, 8:47:47 AM6/10/20
to Yang Guo, v8-...@googlegroups.com
On Wed, Jun 10, 2020 at 2:00 PM Yang Guo <yan...@chromium.org> wrote:
V8 has the ability to serialize function bindings to native modules, e.g. described here. I'm not sure whether React Native uses it. Maybe +Jakob Gruber has some insights from past interaction?

No sorry, no further insights.
Reply all
Reply to author
Forward
0 new messages