Quellcode durchsuchen

[php7] use Std.is() instead of Syntax.instanceof() in std lib

Alexander Kuzmenko vor 8 Jahren
Ursprung
Commit
58dec04c2d
5 geänderte Dateien mit 36 neuen und 25 gelöschten Zeilen
  1. 4 2
      src/generators/genphp7.ml
  2. 24 10
      std/php7/Boot.hx
  3. 6 11
      std/php7/_std/Reflect.hx
  4. 1 1
      std/php7/_std/Type.hx
  5. 1 1
      std/php7/_std/haxe/Json.hx

+ 4 - 2
src/generators/genphp7.ml

@@ -778,7 +778,7 @@ let is_std_is expr =
 *)
 let instanceof_compatible (subject_arg:texpr) (type_arg:texpr) : bool =
 	match (reveal_expr_with_parenthesis type_arg).eexpr with
-		| TTypeExpr (TClassDecl { cl_path = path }) when path <> ([], "String") ->
+		| TTypeExpr (TClassDecl { cl_path = path }) when path <> ([], "String") && path <> ([], "Class") ->
 			let subject_arg = reveal_expr_with_parenthesis subject_arg in
 			(match subject_arg.eexpr with
 				| TLocal _ | TField _ | TCall _ | TArray _ -> not (is_magic subject_arg)
@@ -2583,6 +2583,7 @@ class virtual type_builder ctx wrapper =
 		method private write_expr_lang_instanceof args =
 			match args with
 				| val_expr :: type_expr :: [] ->
+					self#write "(";
 					self#write_expr val_expr;
 					self#write " instanceof ";
 					(match (reveal_expr type_expr).eexpr with
@@ -2591,7 +2592,8 @@ class virtual type_builder ctx wrapper =
 						| _ ->
 							self#write_expr type_expr;
 							if not (is_string type_expr) then self#write "->phpClassName"
-					)
+					);
+					self#write ")"
 				| _ -> fail self#pos (try assert false with Assert_failure mlpos -> mlpos)
 		(**
 			Writes `foreach` expression to output buffer (for `php.Syntax.foreach()`)

+ 24 - 10
std/php7/Boot.hx

@@ -340,7 +340,7 @@ class Boot {
 			if (value.method_exists('__toString')) {
 				return value.__toString();
 			}
-			if (Syntax.instanceof(value, StdClass)) {
+			if (Std.is(value, StdClass)) {
 				if (value.toString.isset() && value.toString.is_callable()) {
 					return value.toString();
 				}
@@ -351,10 +351,10 @@ class Boot {
 				}
 				return '{ ' + Global.implode(', ', result) + ' }';
 			}
-			if (Syntax.instanceof(value, HxClosure) || Syntax.instanceof(value, Closure)) {
+			if (isFunction(value)) {
 				return '<function>';
 			}
-			if (Syntax.instanceof(value, HxClass)) {
+			if (Std.is(value, HxClass)) {
 				return '[class ' + getClassName((value:HxClass).phpClassName) + ']';
 			} else {
 				return '[object ' + getClassName(Global.get_class(value)) + ']';
@@ -417,7 +417,7 @@ class Boot {
 			case 'php\\NativeArray', 'php\\_NativeArray\\NativeArray_Impl_':
 				return value.is_array();
 			case 'Enum', 'Class':
-				if (Syntax.instanceof(value, HxClass)) {
+				if (Std.is(value, HxClass)) {
 					var valuePhpClass = (cast value:HxClass).phpClassName;
 					var enumPhpClass = (cast HxEnum:HxClass).phpClassName;
 					var isEnumType = Global.is_subclass_of(valuePhpClass, enumPhpClass);
@@ -435,15 +435,29 @@ class Boot {
 	/**
 		Check if `value` is a `Class<T>`
 	**/
-	public static function isClass(value:Dynamic) : Bool {
-		return Syntax.instanceof(value, HxClass);
+	public static inline function isClass(value:Dynamic) : Bool {
+		return Std.is(value, HxClass);
 	}
 
 	/**
 		Check if `value` is an enum constructor instance
 	**/
-	public static function isEnumValue(value:Dynamic) : Bool {
-		return Syntax.instanceof(value, HxEnum);
+	public static inline function isEnumValue(value:Dynamic) : Bool {
+		return Std.is(value, HxEnum);
+	}
+
+	/**
+		Check if `value` is a function
+	**/
+	public static inline function isFunction(value:Dynamic) : Bool {
+		return Std.is(value, Closure) || Std.is(value, HxClosure);
+	}
+
+	/**
+		Check if `value` is an instance of `HxClosure`
+	**/
+	public static inline function isHxClosure(value:Dynamic) : Bool {
+		return Std.is(value, HxClosure);
 	}
 
 	/**
@@ -799,8 +813,8 @@ private class HxClosure {
 		if (eThis == null) {
 			eThis = target;
 		}
-		if (Syntax.instanceof(eThis, StdClass)) {
-			if (Syntax.instanceof(eThis, HxAnon)) {
+		if (Std.is(eThis, StdClass)) {
+			if (Std.is(eThis, HxAnon)) {
 				return Syntax.getField(eThis, func);
 			}
 		}

+ 6 - 11
std/php7/_std/Reflect.hx

@@ -33,7 +33,7 @@ using php.Global;
 		if (!o.is_object()) return false;
 		if (o.property_exists(field)) return true;
 
-		if (Syntax.instanceof(o, cast Boot.getHxClass())) {
+		if (Boot.isClass(o)) {
 			if (Global.property_exists(o.phpClassName, field)) return true;
 			return Global.method_exists(o.phpClassName, field);
 		}
@@ -51,7 +51,7 @@ using php.Global;
 			return Boot.closure(o, field);
 		}
 
-		if (Syntax.instanceof(o, cast Boot.getHxClass())) {
+		if (Boot.isClass(o)) {
 			if (Global.property_exists(o.phpClassName, field)) {
 				return Syntax.getField(o, field);
 			}
@@ -94,7 +94,7 @@ using php.Global;
 	}
 
 	public static function callMethod( o : Dynamic, func : Function, args : Array<Dynamic> ) : Dynamic {
-		if (Syntax.instanceof(func, Closure)) {
+		if (Std.is(func, Closure)) {
 			if (o != null) {
 				func = cast cast(func, Closure).bindTo(o);
 			}
@@ -111,12 +111,8 @@ using php.Global;
 		return [];
 	}
 
-	public static function isFunction( f : Dynamic ) : Bool {
-		if (Syntax.instanceof(f, Closure)) {
-			return true;
-		} else {
-			return Syntax.instanceof(f, cast Boot.closureHxClass());
-		}
+	public static inline function isFunction( f : Dynamic ) : Bool {
+		return Boot.isFunction(f);
 	}
 
 	public static function compare<T>( a : T, b : T ) : Int {
@@ -129,8 +125,7 @@ using php.Global;
 	}
 
 	public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool {
-		var hxClosure : Class<Dynamic> = cast Boot.closureHxClass();
-		if (Syntax.instanceof(f1, hxClosure) && Syntax.instanceof(f2, hxClosure)) {
+		if (Boot.isHxClosure(f1) && Boot.isHxClosure(f2)) {
 			return f1.equals(f2);
 		} else {
 			return f1 == f2;

+ 1 - 1
std/php7/_std/Type.hx

@@ -258,7 +258,7 @@ enum ValueType {
 
 		if (v.is_object()) {
 			if (Reflect.isFunction(v)) return TFunction;
-			if (Syntax.instanceof(v, StdClass)) return TObject;
+			if (Std.is(v, StdClass)) return TObject;
 			if (Boot.isClass(v)) return TObject;
 
 			var hxClass = Boot.getClass(Global.get_class(v));

+ 1 - 1
std/php7/_std/haxe/Json.hx

@@ -87,7 +87,7 @@ class Json {
 	}
 
 	static function convertBeforeEncode(value:Dynamic):Dynamic {
-		if (Syntax.instanceof(value, Array)) {
+		if (Std.is(value, Array)) {
 			var result = new NativeIndexedArray();
 			Syntax.foreach(value.arr, function(index:Int, item:Dynamic) {
 				result[index] = convertBeforeEncode(item);