@@ -58,6 +58,7 @@ Runtime::Runtime(JNIEnv *env, jobject runtime, int id)
58
58
{
59
59
m_runtime = m_env.NewGlobalRef (runtime);
60
60
m_objectManager = new ObjectManager (m_runtime);
61
+ m_startupData = nullptr ;
61
62
s_id2RuntimeCache.insert (make_pair (id, this ));
62
63
}
63
64
@@ -335,26 +336,38 @@ void Runtime::PassUncaughtExceptionToJsNative(JNIEnv *env, jobject obj, jthrowab
335
336
}
336
337
}
337
338
339
+ void Runtime::ClearStartupData (JNIEnv *env, jobject obj) {
340
+ if (m_startupData) {
341
+ delete m_startupData->data ;
342
+ delete m_startupData;
343
+ }
344
+ }
345
+
338
346
Isolate* Runtime::PrepareV8Runtime (const string& filesPath, jstring packageName, jobject jsDebugger, jstring profilerOutputDir)
339
347
{
340
348
auto platform = v8::platform::CreateDefaultPlatform ();
341
349
V8::InitializePlatform (platform);
342
350
V8::Initialize ();
343
351
344
352
Isolate::CreateParams create_params;
345
- StartupData startup_data;
346
353
string customScript;
347
354
348
355
create_params.array_buffer_allocator = &g_allocator;
349
356
// prepare the snapshot blob
350
357
if (Constants::V8_HEAP_SNAPSHOT)
351
358
{
359
+ DEBUG_WRITE_FORCE (" Snapshot enabled." );
360
+
361
+ m_startupData = new StartupData ();
362
+
352
363
auto snapshotPath = filesPath + " /internal/snapshot.blob" ;
353
364
if ( File::Exists (snapshotPath))
354
365
{
355
366
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);
358
371
}
359
372
else
360
373
{
@@ -364,11 +377,25 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring packageName,
364
377
customScript = File::ReadText (Constants::V8_HEAP_SNAPSHOT_SCRIPT);
365
378
}
366
379
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
+ }
369
396
}
370
397
371
- create_params.snapshot_blob = &startup_data ;
398
+ create_params.snapshot_blob = m_startupData ;
372
399
}
373
400
374
401
auto isolate = Isolate::New (create_params);
@@ -402,12 +429,6 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring packageName,
402
429
Local<Context> context = Context::New (isolate, nullptr , globalTemplate);
403
430
PrimaryContext = new Persistent<Context>(isolate, context);
404
431
405
- if (Constants::V8_HEAP_SNAPSHOT)
406
- {
407
- // we own the snapshot buffer, delete it
408
- delete[] startup_data.data ;
409
- }
410
-
411
432
context_scope = new Context::Scope (context);
412
433
413
434
m_objectManager->Init (isolate);
0 commit comments