瀏覽代碼

Merge pull request #2476 from nadako/js_std_opts

[js] Optimize Std.string for basic types
Nicolas Cannasse 11 年之前
父節點
當前提交
de44be085c
共有 2 個文件被更改,包括 31 次插入0 次删除
  1. 17 0
      optimizer.ml
  2. 14 0
      tests/optimization/src/Test.hx

+ 17 - 0
optimizer.ml

@@ -62,6 +62,23 @@ let api_inline ctx c field params p =
 			Some { eexpr = TConst (TString (if b then "true" else "false")); epos = p; etype = ctx.t.tstring }
 		| _ ->
 			None)
+	| ([],"Std"),"string",[v] when ctx.com.platform = Js ->
+		let pos = v.epos in
+		let stringt = ctx.com.basic.tstring in
+		let stringv = mk (TBinop (Ast.OpAdd, mk (TConst (TString "")) stringt pos, v)) stringt pos in
+		(match follow v.etype with
+		| TInst ({ cl_path = [],"String" }, []) ->
+			Some v
+		| TAbstract ({ a_path = [],"Float" }, []) ->
+			Some stringv
+		| TAbstract ({ a_path = [],"Int" }, []) ->
+			Some stringv
+		| TAbstract ({ a_path = [],"UInt" }, []) ->
+			Some stringv
+		| TAbstract ({ a_path = [],"Bool" }, []) ->
+			Some stringv
+		| _ ->
+			None)
 	| ([],"Std"),"int",[{ eexpr = TConst (TFloat f) }] ->
 		let f = float_of_string f in
 		(match classify_float f with

+ 14 - 0
tests/optimization/src/Test.hx

@@ -53,4 +53,18 @@ class Test {
 		}
 		b.x = a;
 	}
+
+	@:js("var x = 10;\"\" + x;var x1 = 10;\"\" + x1;var x2 = 10.0;\"\" + x2;var x3 = \"10\";x3;var x4 = true;\"\" + x4;")
+	static function testStdString() {
+        var x = 10;
+        Std.string(x);
+        var x:UInt = 10;
+        Std.string(x);
+        var x = 10.0;
+        Std.string(x);
+        var x = "10";
+        Std.string(x);
+        var x = true;
+        Std.string(x);
+	}
 }