Parcourir la source

[js] use lazy getter for HaxeError.message instead of calling String(val) in the ctor (closes #6754)

Dan Korostelev il y a 7 ans
Parent
commit
2402c58403
2 fichiers modifiés avec 13 ajouts et 1 suppressions
  1. 4 1
      std/js/Boot.hx
  2. 9 0
      tests/unit/src/unit/issues/Issue6754.hx

+ 4 - 1
std/js/Boot.hx

@@ -30,13 +30,16 @@ private class HaxeError extends js.Error {
 	public function new(val:Dynamic) {
 		super();
 		this.val = val;
-		this.message = (cast String)(val);
 		if ((cast js.Error).captureStackTrace) (cast js.Error).captureStackTrace(this, HaxeError);
 	}
 
 	public static function wrap(val:Dynamic):js.Error {
 		return if (js.Syntax.instanceof(val, js.Error)) val else new HaxeError(val);
 	}
+
+	static function __init__() {
+		js.Object.defineProperty((cast HaxeError).prototype, "message", {get: () -> (cast String)(js.Lib.nativeThis.val)});
+	}
 }
 
 @:dox(hide)

+ 9 - 0
tests/unit/src/unit/issues/Issue6754.hx

@@ -0,0 +1,9 @@
+package unit.issues;
+
+class Issue6754 extends Test {
+	#if js
+	function test() {
+		try throw "hello" catch (e:Any) eq(js.Lib.getOriginalException().message, "hello");
+	}
+	#end
+}