NativeScript / ios-jsc

NativeScript for iOS using JavaScriptCore
http://docs.nativescript.org/runtimes/ios
Apache License 2.0
298 stars 59 forks source link

Sporadically native exceptions cannot be caught in JS try/catch #1207

Closed vtrifonov closed 4 years ago

vtrifonov commented 5 years ago

Environment Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project):

Describe the bug If there is an exception raised in the native code there is a chance that exception not to be caught if using try/catch or if discardUncaughtJsExceptions is enabled.

To Reproduce

  1. tns create myApp --template https://github.com/rosen-vladimirov/appNativeCode/tarball/master
  2. Open app/App_Resources/iOS/src folder and edit change the content of the following files to:
    • MyClass.h:
      
      #import <Foundation/Foundation.h>

@interface MyClass : NSObject

@end


  * **MyClass.m**:

  ```ObjectiveC
#import "MyClass.h"

@implementation MyClass

- (void)logInfo {
    NSLog(@"NativeScript logInfo method called");
}

- (id)initThrowing {
    [NSException raise:@"Custom Exception" format:@"Custom Reason"];
    return self;
}

@end
  1. Add the following code in the app/main-view-model.js onTap handler:

    for (var i = 0; i < 20; i++) {
    try {
        MyClass.alloc().initThrowing();
    } catch (ex) {
        console.log(ex);
    
    }
    }

this will call 20 times the initThrowing initializer which raises an exception and when executing the app on a device tapping on the button crashes the application. The reason to make 20 calls is that the issue is sporadic and it might not happen with the first call.

Expected behavior The application shouldn't crash because the exception is caught and the reason should be output in the console 20 times.

Additional context The issue is valid only on real devices, as there's a known issue in libffi causing the application to crash when an exception is thrown on Simulators (#1044).