Sfoglia il codice sorgente

[php] retun null for closure creation on a field with null value (fixes #9316)

Aleksandr Kuzmenko 5 anni fa
parent
commit
77468bc465
3 ha cambiato i file con 22 aggiunte e 4 eliminazioni
  1. 0 2
      src/generators/genphp7.ml
  2. 5 2
      std/php/Boot.hx
  3. 17 0
      tests/unit/src/unit/issues/Issue9316.hx

+ 0 - 2
src/generators/genphp7.ml

@@ -2237,7 +2237,6 @@ class code_writer (ctx:php_generator_context) hx_type_path php_name =
 		method write_static_method_closure expr field_name =
 			let expr = reveal_expr expr in
 			self#write ((self#use boot_type_path) ^ "::getStaticClosure(");
-			(* self#write ("new " ^ (self#use hxclosure_type_path) ^ "("); *)
 			(match (reveal_expr expr).eexpr with
 				| TTypeExpr (TClassDecl { cl_path = ([], "String") }) ->
 					self#write ((self#use hxstring_type_path) ^ "::class")
@@ -2262,7 +2261,6 @@ class code_writer (ctx:php_generator_context) hx_type_path php_name =
 						self#write_expr expr;
 						self#write ("->" ^ (field_name field))
 			else
-				(* let new_closure = "new " ^ (self#use hxclosure_type_path) in *)
 				let new_closure = ((self#use boot_type_path) ^ "::getInstanceClosure") in
 				match expr.eexpr with
 					| TTypeExpr mtype ->

+ 5 - 2
std/php/Boot.hx

@@ -589,11 +589,14 @@ class Boot {
 		Creates Haxe-compatible closure of an instance method.
 		@param obj - any object
 	**/
-	public static function getInstanceClosure(obj:{?__hx_closureCache:NativeAssocArray<HxClosure>}, methodName:String) {
+	public static function getInstanceClosure(obj:{?__hx_closureCache:NativeAssocArray<HxClosure>}, methodName:String):Null<HxClosure> {
 		var result = Syntax.coalesce(obj.__hx_closureCache[methodName], null);
 		if (result != null) {
 			return result;
 		}
+		if(!Global.method_exists(obj, methodName) && !Global.isset(Syntax.field(obj, methodName))) {
+			return null;
+		}
 		result = new HxClosure(obj, methodName);
 		if (!Global.property_exists(obj, '__hx_closureCache')) {
 			obj.__hx_closureCache = new NativeAssocArray();
@@ -675,7 +678,7 @@ private class HxClass {
 		} else if (Boot.hasGetter(phpClassName, property)) {
 			return Syntax.staticCall(phpClassName, 'get_$property');
 		} else if (phpClassName.method_exists(property)) {
-			return new HxClosure(phpClassName, property);
+			return Boot.getStaticClosure(phpClassName, property);
 		} else {
 			return Syntax.getStaticField(phpClassName, property);
 		}

+ 17 - 0
tests/unit/src/unit/issues/Issue9316.hx

@@ -0,0 +1,17 @@
+package unit.issues;
+
+class Issue9316 extends unit.Test {
+	var opt:Options = {};
+
+	function test() {
+		var fn = switch opt {
+			case {fn:null}: () -> 'ok';
+			case _: () -> 'fail';
+		}
+		eq('ok', fn());
+	}
+}
+
+private typedef Options = {
+	final ?fn:()->String;
+}