Parcourir la source

Don't call CatchClrExceptions for JavaScriptExceptions (#1502)

Marko Lahma il y a 2 ans
Parent
commit
6a838cb6e1

+ 25 - 0
Jint.Tests/Runtime/InteropTests.cs

@@ -2071,6 +2071,31 @@ namespace Jint.Tests.Runtime
             Assert.Equal(engine.Invoke("throwException2").AsString(), exceptionMessage);
         }
 
+        [Fact]
+        public void ShouldNotCatchClrFromApply()
+        {
+            var engine = new Engine(options =>
+            {
+                options.CatchClrExceptions(e =>
+                {
+                    Assert.Fail("was called");
+                    return true;
+                });
+            });
+
+            engine.Execute(@"
+                function throwError() {
+                    throw new Error();
+                }
+
+                // doesn't cause ExceptionDelegateHandler call
+                try { throwError(); } catch {}
+
+                // does cause ExceptionDelegateHandler call
+                try { throwError.apply(); } catch {}
+            ");
+        }
+
         private class MemberExceptionTest
         {
             public MemberExceptionTest(bool throwOnCreate)

+ 4 - 4
Jint/Runtime/Interop/ClrFunctionInstance.cs

@@ -37,15 +37,15 @@ namespace Jint.Runtime.Interop
             {
                 return _func(thisObject, arguments);
             }
-            catch (Exception exc)
+            catch (Exception e) when (e is not JavaScriptException)
             {
-                if (_engine.Options.Interop.ExceptionHandler(exc))
+                if (_engine.Options.Interop.ExceptionHandler(e))
                 {
-                    ExceptionHelper.ThrowJavaScriptException(_realm.Intrinsics.Error, exc.Message);
+                    ExceptionHelper.ThrowJavaScriptException(_realm.Intrinsics.Error, e.Message);
                 }
                 else
                 {
-                    ExceptionDispatchInfo.Capture(exc).Throw();
+                    ExceptionDispatchInfo.Capture(e).Throw();
                 }
 
                 return Undefined;