cxfeng1 closed pull request #1481: [iOS] Avoid exceptionHandler be invoked by 
self and cause stackoverflow.
URL: https://github.com/apache/incubator-weex/pull/1481
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/ios/sdk/WeexSDK/Sources/Bridge/WXBridgeContext.m 
b/ios/sdk/WeexSDK/Sources/Bridge/WXBridgeContext.m
index bf747fed0d..3e99033691 100644
--- a/ios/sdk/WeexSDK/Sources/Bridge/WXBridgeContext.m
+++ b/ios/sdk/WeexSDK/Sources/Bridge/WXBridgeContext.m
@@ -1008,54 +1008,67 @@ + (void)mountContextEnvironment:(JSContext*)context
                                    initWithData:nsdataFromBase64String 
encoding:NSISOLatin1StringEncoding];
         return base64Decoded;
     };
+    
+    // Avoid exceptionHandler be recursively invoked and finally cause stack 
overflow.
+    static BOOL gInExceptionHandler = NO;
     context.exceptionHandler = ^(JSContext *context, JSValue *exception){
-        context.exception = exception;
-        NSString *errorCode = [NSString stringWithFormat:@"%d", 
WX_KEY_EXCEPTION_WXBRIDGE];;
-        NSString *bundleUrl = nil;
-        NSString *message = nil;
-        NSDictionary *userInfo = nil;
-        BOOL commitException = YES;
-        WXSDKInstance * instance = nil;
-        if ([WXSDKManager sharedInstance].multiContext) {
-            if (context.instanceId) {
-                // instance page javaScript runtime exception
-                 instance = [WXSDKManager instanceForID:context.instanceId];
-                if (instance) {
-                    // instance already existed
-                    commitException = YES;
+        if (gInExceptionHandler) {
+            return;
+        }
+        gInExceptionHandler = YES;
+        
+        @try {
+            context.exception = exception;
+            NSString *errorCode = [NSString stringWithFormat:@"%d", 
WX_KEY_EXCEPTION_WXBRIDGE];;
+            NSString *bundleUrl = nil;
+            NSString *message = nil;
+            NSDictionary *userInfo = nil;
+            BOOL commitException = YES;
+            WXSDKInstance * instance = nil;
+            if ([WXSDKManager sharedInstance].multiContext) {
+                if (context.instanceId) {
+                    // instance page javaScript runtime exception
+                     instance = [WXSDKManager 
instanceForID:context.instanceId];
+                    if (instance) {
+                        // instance already existed
+                        commitException = YES;
+                    } else {
+                        // instance already destroyed
+                        commitException = NO;
+                    }
                 } else {
-                    // instance already destroyed
-                    commitException = NO;
+                    // weex-main-jsfm.js runtime exception throws
+                    message = [NSString 
stringWithFormat:@"[WX_KEY_EXCEPTION_WXBRIDGE] [%@:%@:%@] %@\n%@", 
exception[@"sourceURL"], exception[@"line"], exception[@"column"], [exception 
toString], [exception[@"stack"] toObject]];
+                    if (!JSValueIsUndefined(context.JSGlobalContextRef, 
exception[@"sourceURL"].JSValueRef)) {
+                        bundleUrl = exception[@"sourceURL"].toString;
+                    } else {
+                        bundleUrl = @"weex-main-jsfm";
+                    }
+                    userInfo = [NSDictionary dictionary];
                 }
             } else {
-                // weex-main-jsfm.js runtime exception throws
-                message = [NSString 
stringWithFormat:@"[WX_KEY_EXCEPTION_WXBRIDGE] [%@:%@:%@] %@\n%@", 
exception[@"sourceURL"], exception[@"line"], exception[@"column"], [exception 
toString], [exception[@"stack"] toObject]];
-                if (!JSValueIsUndefined(context.JSGlobalContextRef, 
exception[@"sourceURL"].JSValueRef)) {
-                    bundleUrl = exception[@"sourceURL"].toString;
-                } else {
-                    bundleUrl = @"weex-main-jsfm";
-                }
-                userInfo = [NSDictionary dictionary];
+                instance = [WXSDKEngine topInstance];
             }
-        } else {
-            instance = [WXSDKEngine topInstance];
-        }
-        
-        if (instance) {
-            bundleUrl = instance.pageName?:([instance.scriptURL 
absoluteString]?:@"WX_KEY_EXCEPTION_WXBRIDGE");
-            message = [NSString stringWithFormat:@"[WX_KEY_EXCEPTION_WXBRIDGE] 
[%@:%@:%@] %@\n%@\n%@\n%@\n%@\n%@", exception[@"sourceURL"], 
exception[@"line"], exception[@"column"], [exception toString], 
[exception[@"stack"] toObject], instance.scriptURL.absoluteString, 
instance.callCreateInstanceContext?:@"", 
instance.createInstanceContextResult?:@"", instance.executeRaxApiResult?:@""];
-            userInfo = 
@{@"jsMainBundleStringContentLength":instance.userInfo[@"jsMainBundleStringContentLength"]?:@"",
-                         
@"jsMainBundleStringContentMd5":instance.userInfo[@"jsMainBundleStringContentMd5"]?:@""};
-        }
-        
-        if (commitException) {
-            WXJSExceptionInfo * jsExceptionInfo = [[WXJSExceptionInfo alloc] 
initWithInstanceId:instance.instanceId bundleUrl:bundleUrl errorCode:errorCode 
functionName:@"" exception:message userInfo:[userInfo mutableCopy]];
             
-            [WXExceptionUtils commitCriticalExceptionRT:jsExceptionInfo];
-            WX_MONITOR_FAIL(WXMTJSBridge, WX_ERR_JS_EXECUTE, message);
-            if (instance.onJSRuntimeException) {
-                instance.onJSRuntimeException(jsExceptionInfo);
+            if (instance) {
+                bundleUrl = instance.pageName?:([instance.scriptURL 
absoluteString]?:@"WX_KEY_EXCEPTION_WXBRIDGE");
+                message = [NSString 
stringWithFormat:@"[WX_KEY_EXCEPTION_WXBRIDGE] [%@:%@:%@] 
%@\n%@\n%@\n%@\n%@\n%@", exception[@"sourceURL"], exception[@"line"], 
exception[@"column"], [exception toString], [exception[@"stack"] toObject], 
instance.scriptURL.absoluteString, instance.callCreateInstanceContext?:@"", 
instance.createInstanceContextResult?:@"", instance.executeRaxApiResult?:@""];
+                userInfo = 
@{@"jsMainBundleStringContentLength":instance.userInfo[@"jsMainBundleStringContentLength"]?:@"",
+                             
@"jsMainBundleStringContentMd5":instance.userInfo[@"jsMainBundleStringContentMd5"]?:@""};
             }
+            
+            if (commitException) {
+                WXJSExceptionInfo * jsExceptionInfo = [[WXJSExceptionInfo 
alloc] initWithInstanceId:instance.instanceId bundleUrl:bundleUrl 
errorCode:errorCode functionName:@"" exception:message userInfo:[userInfo 
mutableCopy]];
+                
+                [WXExceptionUtils commitCriticalExceptionRT:jsExceptionInfo];
+                WX_MONITOR_FAIL(WXMTJSBridge, WX_ERR_JS_EXECUTE, message);
+                if (instance.onJSRuntimeException) {
+                    instance.onJSRuntimeException(jsExceptionInfo);
+                }
+            }
+        }
+        @finally {
+            gInExceptionHandler = NO;
         }
     };
     


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to