Browse Source

do not inline structures which have invalid field names (closes #2475)

Simon Krajewski 11 years ago
parent
commit
d72f1ccbba
3 changed files with 44 additions and 2 deletions
  1. 1 0
      .gitignore
  2. 21 1
      optimizer.ml
  3. 22 1
      tests/optimization/src/Test.hx

+ 1 - 0
.gitignore

@@ -49,3 +49,4 @@
 build.bat
 tests/unit/compile.php.hxml
 /doc/*.xml
+tests/optimization/testopt.js

+ 21 - 1
optimizer.ml

@@ -1024,6 +1024,23 @@ type inline_kind =
 
 let inline_constructors ctx e =
 	let vars = ref PMap.empty in
+	let is_valid_ident s =
+		try
+			if String.length s = 0 then raise Exit;
+			begin match String.unsafe_get s 0 with
+				| 'a'..'z' | 'A'..'Z' | '_' -> ()
+				| _ -> raise Exit
+			end;
+			for i = 1 to String.length s - 1 do
+				match String.unsafe_get s i with
+				| 'a'..'z' | 'A'..'Z' | '_' -> ()
+				| '0'..'9' when i > 0 -> ()
+				| _ -> raise Exit
+			done;
+			true
+		with Exit ->
+			false
+	in
 	let rec get_inline_ctor_info e = match e.eexpr with
 		| TNew ({ cl_constructor = Some ({ cf_kind = Method MethInline; cf_expr = Some { eexpr = TFunction f } } as cst) } as c,_,pl) ->
 			IKCtor (f,cst,c,pl,[])
@@ -1032,7 +1049,10 @@ let inline_constructors ctx e =
 		| TArrayDecl el ->
 			IKArray el
 		| TObjectDecl fl ->
-			IKStructure fl
+			if (List.exists (fun (s,_) -> not (is_valid_ident s)) fl) then
+				IKNone
+			else
+				IKStructure fl
 		| TCast(e,None) | TParenthesis e ->
 			get_inline_ctor_info e
 		| TBlock el ->

+ 22 - 1
tests/optimization/src/Test.hx

@@ -78,7 +78,6 @@ 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;
@@ -92,4 +91,26 @@ class Test {
         var x = true;
         Std.string(x);
 	}
+	
+	@:js('
+		var x_foo = 1;
+		var x_bar = 2;
+		var y = x_foo;
+		var z = x_bar;
+	')
+	static function testStructureInline1() {
+		var x = {
+			foo: 1,
+			bar: 2
+		}
+		var y = x.foo;
+		var z = x.bar;
+	}
+	
+	@:js('var x = { \'oh-my\' : "god"};')
+	static function testStructureInlineInvalidField() {
+        var x = {
+            "oh-my": "god"
+        };
+	}
 }