瀏覽代碼

[js] Optimize Std.string for basic types

Dan Korostelev 11 年之前
父節點
當前提交
e8280e6ddd
共有 1 個文件被更改,包括 17 次插入0 次删除
  1. 17 0
      optimizer.ml

+ 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