ソースを参照

[php] use cached closures for Reflect.field(obj, method)

Alexander Kuzmenko 6 年 前
コミット
b9e83afab2
2 ファイル変更18 行追加11 行削除
  1. 16 9
      std/php/Boot.hx
  2. 2 2
      std/php/_std/Reflect.hx

+ 16 - 9
std/php/Boot.hx

@@ -257,15 +257,6 @@ class Boot {
 		return phpParts.join('\\');
 		return phpParts.join('\\');
 	}
 	}
 
 
-	/**
-		Creates Haxe-compatible closure.
-		@param type `this` for instance methods; full php class name for static methods
-		@param func Method name
-	**/
-	public static inline function closure( target:Dynamic, func:Dynamic ) : HxClosure {
-		return new HxClosure(target, func);
-	}
-
 	/**
 	/**
 		Unsafe cast to HxClosure
 		Unsafe cast to HxClosure
 	**/
 	**/
@@ -537,6 +528,10 @@ class Boot {
 		return chars == false ? null : (chars:NativeArray)[index];
 		return chars == false ? null : (chars:NativeArray)[index];
 	}
 	}
 
 
+	/**
+		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) {
 		var result = Syntax.coalesce(obj.__hx_closureCache[methodName], null);
 		var result = Syntax.coalesce(obj.__hx_closureCache[methodName], null);
 		if(result != null) {
 		if(result != null) {
@@ -550,6 +545,9 @@ class Boot {
 		return result;
 		return result;
 	}
 	}
 
 
+	/**
+		Creates Haxe-compatible closure of a static method.
+	**/
 	public static function getStaticClosure(phpClassName:String, methodName:String) {
 	public static function getStaticClosure(phpClassName:String, methodName:String) {
 		var result = Syntax.coalesce(staticClosures[phpClassName][methodName], null);
 		var result = Syntax.coalesce(staticClosures[phpClassName][methodName], null);
 		if(result != null) {
 		if(result != null) {
@@ -562,6 +560,15 @@ class Boot {
 		staticClosures[phpClassName][methodName] = result;
 		staticClosures[phpClassName][methodName] = result;
 		return result;
 		return result;
 	}
 	}
+
+	/**
+		Creates Haxe-compatible closure.
+		@param type `this` for instance methods; full php class name for static methods
+		@param func Method name
+	**/
+	public static inline function closure( target:Dynamic, func:String ) : HxClosure {
+		return target.is_string() ? getStaticClosure(target, func) : getInstanceClosure(target, func);
+	}
 }
 }
 
 
 /**
 /**

+ 2 - 2
std/php/_std/Reflect.hx

@@ -51,7 +51,7 @@ using php.Global;
 			return Syntax.field(o, field);
 			return Syntax.field(o, field);
 		}
 		}
 		if (o.method_exists(field)) {
 		if (o.method_exists(field)) {
-			return Boot.closure(o, field);
+			return Boot.getInstanceClosure(o, field);
 		}
 		}
 
 
 		if (Boot.isClass(o)) {
 		if (Boot.isClass(o)) {
@@ -63,7 +63,7 @@ using php.Global;
 				return Syntax.field(o, field);
 				return Syntax.field(o, field);
 			}
 			}
 			if (Global.method_exists(phpClassName, field)) {
 			if (Global.method_exists(phpClassName, field)) {
-				return Boot.closure(phpClassName, field);
+				return Boot.getStaticClosure(phpClassName, field);
 			}
 			}
 		}
 		}