ソースを参照

[flash] don't swallow exceptions thrown in getters/setters when invoked with Reflect methods (fixes #6871, fixes #5460)

Alexander Kuzmenko 7 年 前
コミット
90272e4b1a
2 ファイル変更36 行追加11 行削除
  1. 10 11
      std/flash/_std/Reflect.hx
  2. 26 0
      tests/unit/src/unit/issues/Issue6871.hx

+ 10 - 11
std/flash/_std/Reflect.hx

@@ -26,8 +26,7 @@
 	}
 
 	public static function field( o : Dynamic, field : String ) : Dynamic untyped {
-		// sealed classes will throw an exception
-		return try o[field] catch( e : Dynamic ) null;
+		return o != null && __in__(field, o) ? o[field] : null;
 	}
 
 	public inline static function setField( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
@@ -35,19 +34,19 @@
 	}
 
 	public static function getProperty( o : Dynamic, field : String ) : Dynamic untyped {
-		try {
-			return o["get_" + field]();
-		} catch( e : Dynamic ) try {
-			return o[field];
-		} catch( e : Dynamic ) {
-			return null;
+		if(o == null) return null;
+		var getter = 'get_$field';
+		if(__in__(getter, o)) {
+			return o[getter]();
 		}
+		return __in__(field, o) ? o[field] : null;
 	}
 
 	public static function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
-		try {
-			o["set_" + field](value);
-		} catch( e : Dynamic ) {
+		var setter = 'set_$field';
+		if(__in__(setter, o)) {
+			o[setter](value);
+		} else {
 			o[field] = value;
 		}
 	}

+ 26 - 0
tests/unit/src/unit/issues/Issue6871.hx

@@ -0,0 +1,26 @@
+package unit.issues;
+
+class Issue6871 extends unit.Test {
+	static inline var GETTER_ERROR = 'getter exception';
+	static inline var SETTER_ERROR = 'setter exception';
+
+	@:keep static public var field(get,set):Int;
+	static function get_field():Int throw GETTER_ERROR;
+	static function set_field(v:Int):Int throw SETTER_ERROR;
+
+	function test() {
+		try {
+			Reflect.getProperty(Issue6871, 'field');
+			t(false);
+		} catch(e:Dynamic) {
+			eq(GETTER_ERROR, e);
+		}
+
+		try {
+			Reflect.setProperty(Issue6871, 'field', 123);
+			t(false);
+		} catch(e:Dynamic) {
+			eq(SETTER_ERROR, e);
+		}
+	}
+}