Ver Fonte

[as3] quote invalid as3 identifiers in object declaration (closes #2604)

Dan Korostelev há 11 anos atrás
pai
commit
5b81d9fc3a
2 ficheiros alterados com 27 adições e 1 exclusões
  1. 17 1
      genas3.ml
  2. 10 0
      tests/unit/issues/Issue2604.hx

+ 17 - 1
genas3.ml

@@ -119,6 +119,22 @@ let reserved =
 let s_ident n =
 	if Hashtbl.mem reserved n then "_" ^ n else n
 
+let valid_as3_ident s =
+	try
+		for i = 0 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
+
+let anon_field s =
+	let s = s_ident s in
+	if not (valid_as3_ident s) then "\"" ^ s ^ "\"" else s
+
 let rec create_dir acc = function
 	| [] -> ()
 	| d :: l ->
@@ -696,7 +712,7 @@ and gen_expr ctx e =
 		handle_break();
 	| TObjectDecl fields ->
 		spr ctx "{ ";
-		concat ctx ", " (fun (f,e) -> print ctx "%s : " (s_ident f); gen_value ctx e) fields;
+		concat ctx ", " (fun (f,e) -> print ctx "%s : " (anon_field f); gen_value ctx e) fields;
 		spr ctx "}"
 	| TFor (v,it,e) ->
 		let handle_break = handle_break ctx e in

+ 10 - 0
tests/unit/issues/Issue2604.hx

@@ -0,0 +1,10 @@
+package unit.issues;
+
+class Issue2604 extends unit.Test {
+
+	public function test() {
+		var v = {"a-b": 1};
+		eq(Reflect.field(v, "a-b"), 1);
+	}
+
+}