@@ -58,6 +58,7 @@ Runtime::Runtime(JNIEnv *env, jobject runtime, int id)
5858{
5959 m_runtime = m_env.NewGlobalRef (runtime);
6060 m_objectManager = new ObjectManager (m_runtime);
61+ m_startupData = nullptr ;
6162 s_id2RuntimeCache.insert (make_pair (id, this ));
6263}
6364
@@ -335,26 +336,38 @@ void Runtime::PassUncaughtExceptionToJsNative(JNIEnv *env, jobject obj, jthrowab
335336 }
336337}
337338
339+ void Runtime::ClearStartupData (JNIEnv *env, jobject obj) {
340+ if (m_startupData) {
341+ delete m_startupData->data ;
342+ delete m_startupData;
343+ }
344+ }
345+
338346Isolate* Runtime::PrepareV8Runtime (const string& filesPath, jstring packageName, jobject jsDebugger, jstring profilerOutputDir)
339347{
340348 auto platform = v8::platform::CreateDefaultPlatform ();
341349 V8::InitializePlatform (platform);
342350 V8::Initialize ();
343351
344352 Isolate::CreateParams create_params;
345- StartupData startup_data;
346353 string customScript;
347354
348355 create_params.array_buffer_allocator = &g_allocator;
349356 // prepare the snapshot blob
350357 if (Constants::V8_HEAP_SNAPSHOT)
351358 {
359+ DEBUG_WRITE_FORCE (" Snapshot enabled." );
360+
361+ m_startupData = new StartupData ();
362+
352363 auto snapshotPath = filesPath + " /internal/snapshot.blob" ;
353364 if ( File::Exists (snapshotPath))
354365 {
355366 int length;
356- startup_data.data = reinterpret_cast <char *>(File::ReadBinary (snapshotPath, length));
357- startup_data.raw_size = length;
367+ m_startupData->data = reinterpret_cast <char *>(File::ReadBinary (snapshotPath, length));
368+ m_startupData->raw_size = length;
369+
370+ DEBUG_WRITE_FORCE (" Snapshot read %s (%dB)." , snapshotPath.c_str (), length);
358371 }
359372 else
360373 {
@@ -364,11 +377,25 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring packageName,
364377 customScript = File::ReadText (Constants::V8_HEAP_SNAPSHOT_SCRIPT);
365378 }
366379
367- startup_data = V8::CreateSnapshotDataBlob (customScript.c_str ());
368- File::WriteBinary (snapshotPath, startup_data.data , startup_data.raw_size );
380+ DEBUG_WRITE_FORCE (" Creating heap snapshot" );
381+ *m_startupData = V8::CreateSnapshotDataBlob (customScript.c_str ());
382+
383+ if (m_startupData->raw_size == 0 ) {
384+ DEBUG_WRITE_FORCE (" Failed to create heap snapshot." );
385+ } else {
386+ bool writeSuccess = File::WriteBinary (snapshotPath, m_startupData->data , m_startupData->raw_size );
387+
388+ if (!writeSuccess) {
389+ DEBUG_WRITE_FORCE (" Failed to save created snapshot." );
390+ } else {
391+ DEBUG_WRITE_FORCE (" Saved snapshot of %s (%dB) in %s (%dB)" ,
392+ Constants::V8_HEAP_SNAPSHOT_SCRIPT.c_str (), customScript.size (),
393+ snapshotPath.c_str (), m_startupData->raw_size );
394+ }
395+ }
369396 }
370397
371- create_params.snapshot_blob = &startup_data ;
398+ create_params.snapshot_blob = m_startupData ;
372399 }
373400
374401 auto isolate = Isolate::New (create_params);
@@ -402,12 +429,6 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring packageName,
402429 Local<Context> context = Context::New (isolate, nullptr , globalTemplate);
403430 PrimaryContext = new Persistent<Context>(isolate, context);
404431
405- if (Constants::V8_HEAP_SNAPSHOT)
406- {
407- // we own the snapshot buffer, delete it
408- delete[] startup_data.data ;
409- }
410-
411432 context_scope = new Context::Scope (context);
412433
413434 m_objectManager->Init (isolate);
0 commit comments