2626#include < console/Console.h>
2727#include " sys/system_properties.h"
2828#include " ManualInstrumentation.h"
29- #include < snapshot_blob.h>
3029#include " IsolateDisposer.h"
3130#include < unistd.h>
3231#include < thread>
@@ -186,15 +185,12 @@ void Runtime::Init(JNIEnv* env, jstring filesPath, jstring nativeLibDir, bool ve
186185
187186 Constants::APP_ROOT_FOLDER_PATH = filesRoot + " /app/" ;
188187 // read config options passed from Java
188+ // Indices correspond to positions in the com.tns.AppConfig.KnownKeys enum
189189 JniLocalRef v8Flags (env->GetObjectArrayElement (args, 0 ));
190190 Constants::V8_STARTUP_FLAGS = ArgConverter::jstringToString (v8Flags);
191191 JniLocalRef cacheCode (env->GetObjectArrayElement (args, 1 ));
192192 Constants::V8_CACHE_COMPILED_CODE = (bool ) cacheCode;
193- JniLocalRef snapshotScript (env->GetObjectArrayElement (args, 2 ));
194- Constants::V8_HEAP_SNAPSHOT_SCRIPT = ArgConverter::jstringToString (snapshotScript);
195- JniLocalRef snapshotBlob (env->GetObjectArrayElement (args, 3 ));
196- Constants::V8_HEAP_SNAPSHOT_BLOB = ArgConverter::jstringToString (snapshotBlob);
197- JniLocalRef profilerOutputDir (env->GetObjectArrayElement (args, 4 ));
193+ JniLocalRef profilerOutputDir (env->GetObjectArrayElement (args, 2 ));
198194
199195 DEBUG_WRITE (" Initializing Telerik NativeScript" );
200196
@@ -207,8 +203,6 @@ void Runtime::Init(JNIEnv* env, jstring filesPath, jstring nativeLibDir, bool ve
207203Runtime::~Runtime () {
208204 delete this ->m_objectManager ;
209205 delete this ->m_loopTimer ;
210- delete this ->m_heapSnapshotBlob ;
211- delete this ->m_startupData ;
212206 CallbackHandlers::RemoveIsolateEntries (m_isolate);
213207 if (m_isMainThread) {
214208 if (m_mainLooper_fd[0 ] != -1 ) {
@@ -460,11 +454,6 @@ void Runtime::PassUncaughtExceptionFromWorkerToMainHandler(Local<v8::String> mes
460454 env.CallStaticVoidMethod (runtimeClass, mId , (jstring) jMsgLocal, (jstring) jfileNameLocal, (jstring) stTrace, lineno);
461455}
462456
463- void Runtime::ClearStartupData (JNIEnv* env, jobject obj) {
464- delete m_heapSnapshotBlob;
465- delete m_startupData;
466- }
467-
468457static void InitializeV8 () {
469458 Runtime::platform = v8::platform::NewDefaultPlatform ().release ();
470459 V8::InitializePlatform (Runtime::platform);
@@ -475,117 +464,14 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, const string& native
475464 tns::instrumentation::Frame frame (" Runtime.PrepareV8Runtime" );
476465
477466 Isolate::CreateParams create_params;
478- bool didInitializeV8 = false ;
479467
480468 create_params.array_buffer_allocator = &g_allocator;
481469
482- m_startupData = new StartupData ();
483-
484- void * snapshotPtr = nullptr ;
485- string snapshotPath;
486- bool snapshotPathExists = false ;
487-
488- // If device isn't running on Sdk 17
489- if (m_androidVersion != 17 ) {
490- snapshotPath = " libsnapshot.so" ;
491- } else {
492- // If device is running on android Sdk 17
493- // dlopen reads relative path to dynamic libraries or reads from folder different than the nativeLibsDirs on the android device
494- snapshotPath = nativeLibDir + " /libsnapshot.so" ;
495- }
496-
497- snapshotPtr = dlopen (snapshotPath.c_str (), RTLD_LAZY | RTLD_LOCAL);
498- if (snapshotPtr == nullptr ) {
499- auto ignoredSearchValue = string (" library \" " + snapshotPath + " \" not found" );
500- auto currentError = dlerror ();
501- std::string stringError (currentError);
502- if (stringError.find (ignoredSearchValue) == std::string::npos) {
503- DEBUG_WRITE_FORCE (" Failed to load snapshot: %s" , currentError);
504- }
505- }
506-
507- bool isCustomSnapshotFound = false ;
508- if (snapshotPtr) {
509- m_startupData->data = static_cast <const char *>(dlsym (snapshotPtr, " TNSSnapshot_blob" ));
510- m_startupData->raw_size = *static_cast <const unsigned int *>(dlsym (snapshotPtr, " TNSSnapshot_blob_len" ));
511- V8::SetSnapshotDataBlob (m_startupData);
512- isCustomSnapshotFound = true ;
513- DEBUG_WRITE_FORCE (" Snapshot library read %p (%dB)." , m_startupData->data , m_startupData->raw_size );
514- } else if (!Constants::V8_HEAP_SNAPSHOT_BLOB.empty () || !Constants::V8_HEAP_SNAPSHOT_SCRIPT.empty ()) {
515- DEBUG_WRITE_FORCE (" Snapshot enabled." );
516-
517- bool saveSnapshot = true ;
518- // we have a precompiled snapshot blob provided - try to load it directly
519- if (!Constants::V8_HEAP_SNAPSHOT_BLOB.empty ()) {
520- snapshotPath = Constants::V8_HEAP_SNAPSHOT_BLOB;
521- snapshotPathExists = File::Exists (snapshotPath);
522- saveSnapshot = false ;
523- } else {
524- std::string oldSnapshotBlobPath = filesPath + " /internal/snapshot.blob" ;
525- std::string snapshotBlobPath = filesPath + " /internal/TNSSnapshot.blob" ;
526-
527- bool oldSnapshotExists = File::Exists (oldSnapshotBlobPath);
528- bool snapshotExists = File::Exists (snapshotBlobPath);
529-
530- snapshotPathExists = oldSnapshotExists || snapshotExists;
531- snapshotPath = oldSnapshotExists ? oldSnapshotBlobPath : snapshotBlobPath;
532- }
533-
534- if (snapshotPathExists) {
535- m_heapSnapshotBlob = new MemoryMappedFile (MemoryMappedFile::Open (snapshotPath.c_str ()));
536- m_startupData->data = static_cast <const char *>(m_heapSnapshotBlob->memory );
537- m_startupData->raw_size = m_heapSnapshotBlob->size ;
538- V8::SetSnapshotDataBlob (m_startupData);
539- isCustomSnapshotFound = true ;
540- DEBUG_WRITE_FORCE (" Snapshot read %s (%zuB)." , snapshotPath.c_str (), m_heapSnapshotBlob->size );
541- } else if (!saveSnapshot) {
542- throw NativeScriptException (" No snapshot file found at: " + snapshotPath);
543- } else {
544- InitializeV8 ();
545- didInitializeV8 = true ;
546-
547- string customScript;
548-
549- // check for custom script to include in the snapshot
550- if (!Constants::V8_HEAP_SNAPSHOT_SCRIPT.empty () && File::Exists (Constants::V8_HEAP_SNAPSHOT_SCRIPT)) {
551- customScript = ReadFileText (Constants::V8_HEAP_SNAPSHOT_SCRIPT);
552- }
553-
554- DEBUG_WRITE_FORCE (" Creating heap snapshot" );
555- *m_startupData = Runtime::CreateSnapshotDataBlob (customScript.c_str ());
556-
557- if (m_startupData->raw_size == 0 ) {
558- DEBUG_WRITE_FORCE (" Failed to create heap snapshot." );
559- } else {
560- bool writeSuccess = File::WriteBinary (snapshotPath, m_startupData->data , m_startupData->raw_size );
561-
562- if (!writeSuccess) {
563- DEBUG_WRITE_FORCE (" Failed to save created snapshot." );
564- } else {
565- DEBUG_WRITE_FORCE (" Saved snapshot of %s (%zuB) in %s (%dB)" ,
566- Constants::V8_HEAP_SNAPSHOT_SCRIPT.c_str (), customScript.size (),
567- snapshotPath.c_str (), m_startupData->raw_size );
568- }
569- }
570-
571- V8::SetSnapshotDataBlob (m_startupData);
572- isCustomSnapshotFound = true ;
573- }
574- }
575-
576- if (!isCustomSnapshotFound) {
577- // Load V8's built-in snapshot
578- auto * snapshotBlobStartupData = new StartupData ();
579- snapshotBlobStartupData->data = reinterpret_cast <const char *>(&snapshot_blob_bin[0 ]);
580- snapshotBlobStartupData->raw_size = snapshot_blob_bin_len;
581- V8::SetSnapshotDataBlob (snapshotBlobStartupData);
582- }
583-
584470 /*
585471 * Setup the V8Platform only once per process - once for the application lifetime
586472 * Don't execute again if main thread has already been initialized
587473 */
588- if (!didInitializeV8 && ! s_mainThreadInitialized) {
474+ if (!s_mainThreadInitialized) {
589475 InitializeV8 ();
590476 }
591477
@@ -782,60 +668,6 @@ void Runtime::SetManualInstrumentationMode(jstring mode) {
782668 }
783669}
784670
785- StartupData Runtime::CreateSnapshotDataBlob (const char * embedded_source) {
786- // Create a new isolate and a new context from scratch, optionally run
787- // a script to embed, and serialize to create a snapshot blob.
788- StartupData result = {nullptr , 0 };
789- {
790- SnapshotCreator snapshot_creator;
791- Isolate* isolate = snapshot_creator.GetIsolate ();
792- {
793- HandleScope scope (isolate);
794- Local<Context> context = Context::New (isolate);
795- if (embedded_source != nullptr &&
796- !Runtime::RunExtraCode (isolate, context, embedded_source, " <embedded>" )) {
797- return result;
798- }
799- snapshot_creator.SetDefaultContext (context);
800- }
801- result = snapshot_creator.CreateBlob (
802- SnapshotCreator::FunctionCodeHandling::kClear );
803- }
804-
805- return result;
806- }
807-
808- bool Runtime::RunExtraCode (Isolate* isolate, Local<Context> context, const char * utf8_source, const char * name) {
809- Context::Scope context_scope (context);
810- TryCatch try_catch (isolate);
811- Local<v8::String> source_string;
812- if (!v8::String::NewFromUtf8 (isolate, utf8_source, NewStringType::kNormal ).ToLocal (&source_string)) {
813- return false ;
814- }
815- Local<v8::String> resource_name = v8::String::NewFromUtf8 (isolate, name, NewStringType::kNormal ).ToLocalChecked ();
816- ScriptOrigin origin (isolate, resource_name);
817- ScriptCompiler::Source source (source_string, origin);
818- Local<Script> script;
819- if (!ScriptCompiler::Compile (context, &source).ToLocal (&script)) {
820- DEBUG_WRITE_FORCE (" # Script compile failed in %s@%d:%d\n %s\n " ,
821- *v8::String::Utf8Value (isolate, try_catch.Message ()->GetScriptResourceName ()),
822- try_catch.Message ()->GetLineNumber (context).FromJust (),
823- try_catch.Message ()->GetStartColumn (context).FromJust (),
824- *v8::String::Utf8Value (isolate, try_catch.Exception ()));
825- return false ;
826- }
827- if (script->Run (context).IsEmpty ()) {
828- DEBUG_WRITE_FORCE (" # Script run failed in %s@%d:%d\n %s\n " ,
829- *v8::String::Utf8Value (isolate, try_catch.Message ()->GetScriptResourceName ()),
830- try_catch.Message ()->GetLineNumber (context).FromJust (),
831- try_catch.Message ()->GetStartColumn (context).FromJust (),
832- *v8::String::Utf8Value (isolate, try_catch.Exception ()));
833- return false ;
834- }
835- // CHECK(!try_catch.HasCaught());
836- return true ;
837- }
838-
839671void Runtime::DestroyRuntime () {
840672 s_id2RuntimeCache.erase (m_id);
841673 s_isolate2RuntimesCache.erase (m_isolate);
0 commit comments