MCPcopy Create free account
hub / github.com/Kudo/react-native-v8 / SaveCodeCacheIfNeeded

Method SaveCodeCacheIfNeeded

src/v8runtime/V8Runtime.cpp:341–377  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

339}
340
341bool V8Runtime::SaveCodeCacheIfNeeded(
342 const v8::Local<v8::Script> &script,
343 const std::string &sourceURL,
344 v8::ScriptCompiler::CachedData *cachedData) {
345 // caching is for main runtime only
346 if (isSharedRuntime_) {
347 return false;
348 }
349
350 if (cachedData && !cachedData->rejected) {
351 return false;
352 }
353
354 if (config_->codecacheMode == V8RuntimeConfig::CodecacheMode::kNone) {
355 return false;
356 }
357
358 v8::HandleScope scopedHandle(isolate_);
359
360 v8::Local<v8::UnboundScript> unboundScript = script->GetUnboundScript();
361 std::unique_ptr<v8::ScriptCompiler::CachedData> newCachedData;
362 newCachedData.reset(v8::ScriptCompiler::CreateCodeCache(unboundScript));
363 if (!newCachedData) {
364 return false;
365 }
366
367 std::filesystem::path codecachePath(config_->codecacheDir);
368 codecachePath /= std::filesystem::path(sourceURL).filename();
369 if (auto *file = std::fopen(codecachePath.string().c_str(), "wb")) {
370 std::fwrite(newCachedData->data, 1, newCachedData->length, file);
371 std::fclose(file);
372 return true;
373 } else {
374 LOG(ERROR) << "Cannot save codecache file: " << codecachePath.string();
375 return false;
376 }
377}
378
379std::unique_ptr<v8::ScriptCompiler::Source> V8Runtime::UseFakeSourceIfNeeded(
380 const v8::ScriptOrigin &origin,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected