@@ -53,16 +53,44 @@ extern "C" void Java_com_tns_Runtime_initNativeScript(JNIEnv *_env, jobject obj,
5353 }
5454}
5555
56+ Runtime* TryGetRuntime (int runtimeId)
57+ {
58+ Runtime *runtime = nullptr ;
59+ try
60+ {
61+ runtime = Runtime::GetRuntime (runtimeId);
62+ }
63+ catch (NativeScriptException& e)
64+ {
65+ e.ReThrowToJava ();
66+ }
67+ catch (std::exception e) {
68+ stringstream ss;
69+ ss << " Error: c++ exception: " << e.what () << endl;
70+ NativeScriptException nsEx (ss.str ());
71+ nsEx.ReThrowToJava ();
72+ }
73+ catch (...) {
74+ NativeScriptException nsEx (std::string (" Error: c++ exception!" ));
75+ nsEx.ReThrowToJava ();
76+ }
77+ return runtime;
78+ }
79+
5680extern " C" void Java_com_tns_Runtime_runModule (JNIEnv *_env, jobject obj, jint runtimeId, jstring scriptFile)
5781{
58- auto runtime = Runtime::GetRuntime (runtimeId);
82+ auto runtime = TryGetRuntime (runtimeId);
83+ if (runtime == nullptr )
84+ {
85+ return ;
86+ }
87+
5988 auto isolate = runtime->GetIsolate ();
6089 v8::Isolate::Scope isolate_scope (isolate);
6190 v8::HandleScope handleScope (isolate);
6291
6392 try
6493 {
65- auto runtime = Runtime::GetRuntime (runtimeId);
6694 runtime->RunModule (_env, obj, scriptFile);
6795 }
6896 catch (NativeScriptException& e)
@@ -83,13 +111,18 @@ extern "C" void Java_com_tns_Runtime_runModule(JNIEnv *_env, jobject obj, jint r
83111
84112extern " C" jobject Java_com_tns_Runtime_runScript (JNIEnv *_env, jobject obj, jint runtimeId, jstring scriptFile)
85113{
86- auto runtime = Runtime::GetRuntime (runtimeId);
87- auto isolate = runtime->GetIsolate ();
114+ jobject o = nullptr ;
88115
116+ auto runtime = TryGetRuntime (runtimeId);
117+ if (runtime == nullptr )
118+ {
119+ return o;
120+ }
121+
122+ auto isolate = runtime->GetIsolate ();
89123 v8::Isolate::Scope isolate_scope (isolate);
90124 v8::HandleScope handleScope (isolate);
91125
92- jobject o = nullptr ;
93126 try
94127 {
95128 o = runtime->RunScript (_env, obj, scriptFile);
@@ -113,13 +146,18 @@ extern "C" jobject Java_com_tns_Runtime_runScript(JNIEnv *_env, jobject obj, jin
113146
114147extern " C" jobject Java_com_tns_Runtime_callJSMethodNative (JNIEnv *_env, jobject obj, jint runtimeId, jint javaObjectID, jstring methodName, jint retType, jboolean isConstructor, jobjectArray packagedArgs)
115148{
116- auto runtime = Runtime::GetRuntime (runtimeId);
117- auto isolate = runtime->GetIsolate ();
149+ jobject o = nullptr ;
150+
151+ auto runtime = TryGetRuntime (runtimeId);
152+ if (runtime == nullptr )
153+ {
154+ return o;
155+ }
118156
157+ auto isolate = runtime->GetIsolate ();
119158 v8::Isolate::Scope isolate_scope (isolate);
120159 v8::HandleScope handleScope (isolate);
121160
122- jobject o = nullptr ;
123161 try
124162 {
125163 o = runtime->CallJSMethodNative (_env, obj, javaObjectID, methodName, retType, isConstructor, packagedArgs);
@@ -143,9 +181,13 @@ extern "C" jobject Java_com_tns_Runtime_callJSMethodNative(JNIEnv *_env, jobject
143181
144182extern " C" void Java_com_tns_Runtime_createJSInstanceNative (JNIEnv *_env, jobject obj, jint runtimeId, jobject javaObject, jint javaObjectID, jstring className)
145183{
146- auto runtime = Runtime::GetRuntime (runtimeId);
147- auto isolate = runtime->GetIsolate ();
184+ auto runtime = TryGetRuntime (runtimeId);
185+ if (runtime == nullptr )
186+ {
187+ return ;
188+ }
148189
190+ auto isolate = runtime->GetIsolate ();
149191 v8::Isolate::Scope isolate_scope (isolate);
150192 v8::HandleScope handleScope (isolate);
151193
@@ -173,7 +215,11 @@ extern "C" jint Java_com_tns_Runtime_generateNewObjectId(JNIEnv *env, jobject ob
173215{
174216 try
175217 {
176- auto runtime = Runtime::GetRuntime (runtimeId);
218+ auto runtime = TryGetRuntime (runtimeId);
219+ if (runtime == nullptr )
220+ {
221+ return 0 ;
222+ }
177223 return runtime->GenerateNewObjectId (env, obj);
178224 }
179225 catch (NativeScriptException& e)
@@ -194,9 +240,13 @@ extern "C" jint Java_com_tns_Runtime_generateNewObjectId(JNIEnv *env, jobject ob
194240
195241extern " C" void Java_com_tns_Runtime_adjustAmountOfExternalAllocatedMemoryNative (JNIEnv *env, jobject obj, jint runtimeId, jlong usedMemory)
196242{
197- auto runtime = Runtime::GetRuntime (runtimeId);
198- auto isolate = runtime->GetIsolate ();
243+ auto runtime = TryGetRuntime (runtimeId);
244+ if (runtime == nullptr )
245+ {
246+ return ;
247+ }
199248
249+ auto isolate = runtime->GetIsolate ();
200250 v8::Isolate::Scope isolate_scope (isolate);
201251 v8::HandleScope handleScope (isolate);
202252
@@ -222,9 +272,13 @@ extern "C" void Java_com_tns_Runtime_adjustAmountOfExternalAllocatedMemoryNative
222272
223273extern " C" void Java_com_tns_Runtime_passUncaughtExceptionToJsNative (JNIEnv *env, jobject obj, jint runtimeId, jthrowable exception, jstring stackTrace)
224274{
225- auto runtime = Runtime::GetRuntime (runtimeId);
226- auto isolate = runtime->GetIsolate ();
275+ auto runtime = TryGetRuntime (runtimeId);
276+ if (runtime == nullptr )
277+ {
278+ return ;
279+ }
227280
281+ auto isolate = runtime->GetIsolate ();
228282 v8::Isolate::Scope isolate_scope (isolate);
229283 v8::HandleScope handleScope (isolate);
230284
0 commit comments