V8 has the ability to serialize function bindings to native modules, e.g. described here <https://docs.google.com/document/d/1YEIBdH7ocJfm6PWISKw03szNAgnstA2B3e8PZr_-Gp4/edit#heading=h.677lbnx3xmo2>. I'm not sure whether React Native uses it. Maybe +Jakob Gruber <[email protected]> has some insights from past interaction?
Cheers, Yang On Wed, Jun 10, 2020 at 1:29 PM 王梓童 <[email protected]> wrote: > 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; > } > > -- > -- > v8-dev mailing list > [email protected] > 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 [email protected]. > 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 > <https://groups.google.com/d/msgid/v8-dev/CABaRidW6PGK8%2B_19_TApbi_PAKZDOeH0p961EU0a-Ya8b%3D2vjA%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > -- -- v8-dev mailing list [email protected] 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/v8-dev/CAFSTc_gujnkemXBOnao8ksDQKMxGGzw1x1GYubdN%2BeoW6mVBGQ%40mail.gmail.com.
