Преглед изворни кода

[js] perform a runtime check if an exception needs HaxeError wrapping if it's Dynamic (closes #4644)

Dan Korostelev пре 10 година
родитељ
комит
003b9f6c84
3 измењених фајлова са 32 додато и 2 уклоњено
  1. 9 2
      filters.ml
  2. 4 0
      std/js/Boot.hx
  3. 19 0
      tests/unit/src/unit/issues/Issue4644.hx

+ 9 - 2
filters.ml

@@ -205,8 +205,15 @@ let rec wrap_js_exceptions com e =
 		| TThrow eerr when not (is_error eerr.etype) ->
 		| TThrow eerr when not (is_error eerr.etype) ->
 			let terr = List.find (fun mt -> match mt with TClassDecl {cl_path = ["js";"_Boot"],"HaxeError"} -> true | _ -> false) com.types in
 			let terr = List.find (fun mt -> match mt with TClassDecl {cl_path = ["js";"_Boot"],"HaxeError"} -> true | _ -> false) com.types in
 			let cerr = match terr with TClassDecl c -> c | _ -> assert false in
 			let cerr = match terr with TClassDecl c -> c | _ -> assert false in
-			let ewrap = { eerr with eexpr = TNew (cerr,[],[eerr]) } in
-			{ e with eexpr = TThrow ewrap }
+			(match eerr.etype with
+			| TDynamic _ ->
+				let eterr = Codegen.ExprBuilder.make_static_this cerr e.epos in
+				let ewrap = Codegen.fcall eterr "wrap" [eerr] t_dynamic e.epos in
+				{ e with eexpr = TThrow ewrap }
+			| _ ->
+				let ewrap = { eerr with eexpr = TNew (cerr,[],[eerr]) } in
+				{ e with eexpr = TThrow ewrap }
+			)
 		| _ ->
 		| _ ->
 			Type.map_expr loop e
 			Type.map_expr loop e
 	in
 	in

+ 4 - 0
std/js/Boot.hx

@@ -31,6 +31,10 @@ private class HaxeError extends js.Error {
 		this.message = String(val);
 		this.message = String(val);
 		if (js.Error.captureStackTrace) js.Error.captureStackTrace(this, HaxeError);
 		if (js.Error.captureStackTrace) js.Error.captureStackTrace(this, HaxeError);
 	}
 	}
+
+	public static function wrap(val:Dynamic):Dynamic untyped {
+		return if (__instanceof__(val, js.Error)) val else new HaxeError(val);
+	}
 }
 }
 
 
 @:dox(hide)
 @:dox(hide)

+ 19 - 0
tests/unit/src/unit/issues/Issue4644.hx

@@ -0,0 +1,19 @@
+package unit.issues;
+
+class Issue4644 extends Test {
+    function test() {
+        #if js
+        var isHaxeError;
+        untyped __js__(
+            "try {{
+                {0};
+            }} catch (e) {{
+                {1} = (e instanceof js__$Boot_HaxeError);
+            }}",
+            throw (new js.Error() : Dynamic),
+            isHaxeError
+        );
+        f(isHaxeError);
+        #end
+    }
+}