3737import java .util .concurrent .RunnableFuture ;
3838import java .util .concurrent .atomic .AtomicInteger ;
3939
40- import dalvik .annotation .optimization .FastNative ;
41-
4240public class Runtime {
4341 private native void initNativeScript (int runtimeId , String filesPath , String nativeLibDir , boolean verboseLoggingEnabled , boolean isDebuggable , String packageName ,
4442 Object [] v8Options , String callingDir , int maxLogcatObjectSize , boolean forceLog );
@@ -61,7 +59,7 @@ private native void initNativeScript(int runtimeId, String filesPath, String nat
6159
6260 private native void unlock (int runtimeId );
6361
64- private native void passExceptionToJsNative (int runtimeId , Throwable ex , String message , String fullStackTrace , String jsStackTrace , boolean isDiscarded );
62+ private native void passExceptionToJsNative (int runtimeId , Throwable ex , String message , String fullStackTrace , String jsStackTrace , boolean isDiscarded , boolean isPendingError );
6563
6664 private static native int getCurrentRuntimeId ();
6765
@@ -82,21 +80,21 @@ private native void initNativeScript(int runtimeId, String filesPath, String nat
8280 private static native void ResetDateTimeConfigurationCache (int runtimeId );
8381
8482 void passUncaughtExceptionToJs (Throwable ex , String message , String fullStackTrace , String jsStackTrace ) {
85- passExceptionToJsNative (getRuntimeId (), ex , message , fullStackTrace , jsStackTrace , false );
83+ passExceptionToJsNative (getRuntimeId (), ex , message , fullStackTrace , jsStackTrace , false , false );
8684 }
8785
88- void passDiscardedExceptionToJs (Throwable ex , String prefix ) {
86+ void passExceptionToJS (Throwable ex , boolean isPendingError , boolean isDiscarded ) {
8987 //String message = prefix + ex.getMessage();
9088 // we'd better not prefix the error with something like - Error on "main" thread for reportSupressedException
9189 // as it doesn't seem very useful for the users
92- passExceptionToJsNative (getRuntimeId (), ex , ex .getMessage (), Runtime .getStackTraceErrorMessage (ex ), Runtime .getJSStackTrace (ex ), true );
90+ passExceptionToJsNative (getRuntimeId (), ex , ex .getMessage (), Runtime .getStackTraceErrorMessage (ex ), Runtime .getJSStackTrace (ex ), isDiscarded , isPendingError );
9391 }
9492
9593 public static void passSuppressedExceptionToJs (Throwable ex , String methodName ) {
9694 com .tns .Runtime runtime = com .tns .Runtime .getCurrentRuntime ();
9795 if (runtime != null ) {
9896 String errorMessage = "Error on \" " + Thread .currentThread ().getName () + "\" thread for " + methodName + "\n " ;
99- runtime .passDiscardedExceptionToJs (ex , "" );
97+ runtime .passExceptionToJS (ex , false , false );
10098 }
10199 }
102100
@@ -668,78 +666,91 @@ private void init(Logger logger, String appName, String nativeLibDir, File rootD
668666
669667 @ RuntimeCallable
670668 public void enableVerboseLogging () {
671-
672669 logger .setEnabled (true );
673670 ProxyGenerator .IsLogEnabled = true ;
674671 }
675672
676673
677674 @ RuntimeCallable
678675 public void disableVerboseLogging () {
679- // logger.setEnabled(false);
680- // ProxyGenerator.IsLogEnabled = false;
676+ logger .setEnabled (false );
677+ ProxyGenerator .IsLogEnabled = false ;
681678 }
682679
683- public void run () throws NativeScriptException {
680+ public void run () {
684681 ManualInstrumentation .Frame frame = ManualInstrumentation .start ("Runtime.run" );
685682 try {
686683 String mainModule = Module .bootstrapApp ();
687684 runModule (new File (mainModule ));
685+ } catch (NativeScriptException e ){
686+ passExceptionToJS (e , false , false );
688687 } finally {
689688 frame .close ();
690689 }
691690 }
692691
693- public void runModule (File jsFile ) throws NativeScriptException {
694- String filePath = jsFile .getPath ();
695- runModule (getRuntimeId (), filePath );
692+ public void runModule (File jsFile ) {
693+ try {
694+ String filePath = jsFile .getPath ();
695+ runModule (getRuntimeId (), filePath );
696+ } catch (NativeScriptException ex ) {
697+ passExceptionToJS (ex , false , false );
698+ }
696699 }
697700
698- public Object runScript (File jsFile ) throws NativeScriptException {
699- return this .runScript (jsFile , true );
701+ public Object runScript (File jsFile ) {
702+ try {
703+ return this .runScript (jsFile , true );
704+ } catch (NativeScriptException ex ) {
705+ passExceptionToJS (ex , false , false );
706+ return null ;
707+ }
700708 }
701709
702- public Object runScript (File jsFile , final boolean waitForResultOnMainThread ) throws NativeScriptException {
710+ public Object runScript (File jsFile , final boolean waitForResultOnMainThread ) {
703711 Object result = null ;
712+ try {
713+ if (jsFile .exists () && jsFile .isFile ()) {
714+ final String filePath = jsFile .getAbsolutePath ();
704715
705- if (jsFile .exists () && jsFile .isFile ()) {
706- final String filePath = jsFile .getAbsolutePath ();
707-
708- boolean isWorkThread = threadScheduler .getThread ().equals (Thread .currentThread ());
716+ boolean isWorkThread = threadScheduler .getThread ().equals (Thread .currentThread ());
709717
710- if (isWorkThread ) {
711- result = runScript (getRuntimeId (), filePath );
712- } else {
713- final Object [] arr = new Object [2 ];
714-
715- Runnable r = new Runnable () {
716- @ Override
717- public void run () {
718- synchronized (this ) {
719- try {
720- arr [0 ] = runScript (getRuntimeId (), filePath );
721- } finally {
722- this .notify ();
723- arr [1 ] = Boolean .TRUE ;
718+ if (isWorkThread ) {
719+ result = runScript (getRuntimeId (), filePath );
720+ } else {
721+ final Object [] arr = new Object [2 ];
722+
723+ Runnable r = new Runnable () {
724+ @ Override
725+ public void run () {
726+ synchronized (this ) {
727+ try {
728+ arr [0 ] = runScript (getRuntimeId (), filePath );
729+ } finally {
730+ this .notify ();
731+ arr [1 ] = Boolean .TRUE ;
732+ }
724733 }
725734 }
726- }
727- };
735+ };
728736
729- boolean success = threadScheduler .post (r );
737+ boolean success = threadScheduler .post (r );
730738
731- if (success ) {
732- synchronized (r ) {
733- try {
734- if (arr [1 ] == null && waitForResultOnMainThread ) {
735- r .wait ();
739+ if (success ) {
740+ synchronized (r ) {
741+ try {
742+ if (arr [1 ] == null && waitForResultOnMainThread ) {
743+ r .wait ();
744+ }
745+ } catch (InterruptedException e ) {
746+ result = e ;
736747 }
737- } catch (InterruptedException e ) {
738- result = e ;
739748 }
740749 }
741750 }
742751 }
752+ } catch (NativeScriptException ex ) {
753+ passExceptionToJS (ex , false , false );
743754 }
744755
745756 return result ;
@@ -1116,9 +1127,6 @@ private Object getJavaObjectByID(int javaObjectID) throws Exception {
11161127 }
11171128 }
11181129
1119- // Log.d(DEFAULT_LOG_TAG,
1120- // "Platform.getJavaObjectByID found strong object with id:" +
1121- // javaObjectID);
11221130 return instance ;
11231131 }
11241132
@@ -1344,13 +1352,13 @@ private Object dispatchCallJSMethodNative(final int javaObjectID, Class<?> claz,
13441352 try {
13451353 ret = callJSMethodNative (getRuntimeId (), javaObjectID , claz , methodName , returnType , isConstructor , packagedArgs );
13461354 } catch (NativeScriptException e ) {
1347- if (discardUncaughtJsExceptions ) {
1355+ // if (discardUncaughtJsExceptions) {
13481356 String errorMessage = "Error on \" " + Thread .currentThread ().getName () + "\" thread for callJSMethodNative\n " ;
1349- android .util .Log .w ("Warning" , "NativeScript discarding uncaught JS exception!" );
1350- passDiscardedExceptionToJs (e , errorMessage );
1351- } else {
1352- throw e ;
1353- }
1357+ // android.util.Log.w("Warning", "NativeScript discarding uncaught JS exception!");
1358+ passExceptionToJS (e , true , true );
1359+ // } else {
1360+ // throw e;
1361+ // }
13541362 }
13551363 } else {
13561364 final Object [] arr = new Object [2 ];
@@ -1365,13 +1373,13 @@ public void run() {
13651373 final Object [] packagedArgs = packageArgs (tmpArgs );
13661374 arr [0 ] = callJSMethodNative (getRuntimeId (), javaObjectID , claz , methodName , returnType , isCtor , packagedArgs );
13671375 } catch (NativeScriptException e ) {
1368- if (discardUncaughtJsExceptions ) {
1376+ // if (discardUncaughtJsExceptions) {
13691377 String errorMessage = "Error on \" " + Thread .currentThread ().getName () + "\" thread for callJSMethodNative\n " ;
1370- passDiscardedExceptionToJs (e , errorMessage );
1371- android .util .Log .w ("Warning" , "NativeScript discarding uncaught JS exception!" );
1372- } else {
1373- throw e ;
1374- }
1378+ passExceptionToJS (e , true , false );
1379+ // android.util.Log.w("Warning", "NativeScript discarding uncaught JS exception!");
1380+ // } else {
1381+ // throw e;
1382+ // }
13751383 } finally {
13761384 this .notify ();
13771385 arr [1 ] = Boolean .TRUE ;
0 commit comments