浏览代码

Restore public access to JavaScriptException ctor (#1295)

This is the same as pull request #1259 with requested test added.
Genteure 2 年之前
父节点
当前提交
2b58d122e7
共有 2 个文件被更改,包括 40 次插入2 次删除
  1. 38 0
      Jint.Tests.PublicInterface/JavaScriptExceptionTests.cs
  2. 2 2
      Jint/Runtime/JavaScriptException.cs

+ 38 - 0
Jint.Tests.PublicInterface/JavaScriptExceptionTests.cs

@@ -0,0 +1,38 @@
+using Jint.Native;
+using Jint.Native.Error;
+using Jint.Runtime;
+
+namespace Jint.Tests.PublicInterface
+{
+    public class JavaScriptExceptionTests
+    {
+        [Fact]
+        public void CanCreateAndThrowJavaScriptException()
+        {
+            var engine = new Engine();
+
+            engine.SetValue("throw1", () =>
+            {
+                throw new JavaScriptException(engine.Realm.Intrinsics.Error, "message 1");
+            });
+
+            engine.SetValue("throw2", () =>
+            {
+                throw new JavaScriptException(new JsString("message 2"));
+            });
+
+            Assert.Throws<JavaScriptException>(() =>
+            {
+                engine.Evaluate(@"throw1()");
+            });
+
+            var result1 = engine.Evaluate(@"try { throw1() } catch (e) { return e; }");
+            var error1 = Assert.IsType<ErrorInstance>(result1);
+            Assert.Equal("message 1", error1.Get("message").ToString());
+
+            var result2 = engine.Evaluate(@"try { throw2() } catch (e) { return e; }");
+            var jsString = Assert.IsType<JsString>(result2);
+            Assert.Equal("message 2", jsString.ToString());
+        }
+    }
+}

+ 2 - 2
Jint/Runtime/JavaScriptException.cs

@@ -35,13 +35,13 @@ public class JavaScriptException : JintException
         _jsErrorException = (JavaScriptErrorWrapperException) InnerException!;
     }
 
-    internal JavaScriptException(ErrorConstructor errorConstructor, string? message)
+    public JavaScriptException(ErrorConstructor errorConstructor, string? message)
         : base(message, new JavaScriptErrorWrapperException(errorConstructor.Construct(new JsValue[] { message }), message))
     {
         _jsErrorException = (JavaScriptErrorWrapperException) InnerException!;
     }
 
-    internal JavaScriptException(JsValue error)
+    public JavaScriptException(JsValue error)
         : base(GetMessage(error), new JavaScriptErrorWrapperException(error, GetMessage(error)))
     {
         _jsErrorException = (JavaScriptErrorWrapperException) InnerException!;