NativeScript / ios-jsc

NativeScript for iOS using JavaScriptCore

Home Page:http://docs.nativescript.org/runtimes/ios

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sporadically native exceptions cannot be caught in JS try/catch

vtrifonov opened this issue · comments

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):

  • CLI: 6.1.0
  • Cross-platform modules: 6.1.0
  • iOS Runtime: 6.1.0

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
- (void)logInfo;
- (id)initThrowing;

@end
  • MyClass.m:
#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).