Pārlūkot izejas kodu

[js] still optimize `Std.is(v, cast SomeType)`, because the cast can be inserted by the compiler

Dan Korostelev 5 gadi atpakaļ
vecāks
revīzija
ba1a229e84
2 mainītis faili ar 17 papildinājumiem un 1 dzēšanām
  1. 6 1
      src/optimization/inline.ml
  2. 11 0
      tests/optimization/src/TestJs.hx

+ 6 - 1
src/optimization/inline.ml

@@ -136,7 +136,12 @@ let api_inline ctx c field params p =
 			mk (TBinop (Ast.OpEq, tof, (mk (TConst (TString t)) tstring p))) tbool p
 		in
 
-		(match t.eexpr with
+		let rec skip_cast = function
+			| { eexpr = TCast (e, None) } -> skip_cast e
+			| e -> e
+		in
+
+		(match (skip_cast t).eexpr with
 		(* generate simple typeof checks for basic types *)
 		| TTypeExpr (TClassDecl ({ cl_path = [],"String" })) -> Some (typeof "string")
 		| TTypeExpr (TAbstractDecl ({ a_path = [],"Bool" })) -> Some (typeof "boolean")

+ 11 - 0
tests/optimization/src/TestJs.hx

@@ -552,6 +552,17 @@ class TestJs {
 	static function testIssue8751() {
 		(2:Issue8751Int) * 3;
 	}
+
+	@:js('var v = "hi";TestJs.use(typeof(v) == "string" ? v : null);')
+	static function testStdIsOptimizationSurvivesCast() {
+		var value = "hi";
+		use(as(value, String));
+	}
+
+	static inline function as<T>(v:Dynamic, c:Class<T>):Null<T> {
+		return if (Std.is(v, c)) v else null;
+	}
+
 }
 
 extern class Extern {