Browse Source

quote as3 keywords when used as object key

Simon Krajewski 13 years ago
parent
commit
ee21ea3afa
3 changed files with 11 additions and 6 deletions
  1. 1 5
      codegen.ml
  2. 5 1
      genas3.ml
  3. 5 0
      tests/unit/TestBasetypes.hx

+ 1 - 5
codegen.ml

@@ -379,11 +379,7 @@ let build_metadata com t =
 		mk (TObjectDecl (List.map (fun (f,el,p) ->
 			if Hashtbl.mem h f then error ("Duplicate metadata '" ^ f ^ "'") p;
 			Hashtbl.add h f ();
-			let name = match com.platform with
-				| Flash when Common.defined com "as3" -> "\"" ^ f ^ "\""
-				| _ -> f
-			in
-			name, mk (match el with [] -> TConst TNull | _ -> TArrayDecl (List.map (type_constant_value com) el)) (api.tarray t_dynamic) p
+			f, mk (match el with [] -> TConst TNull | _ -> TArrayDecl (List.map (type_constant_value com) el)) (api.tarray t_dynamic) p
 		) ml)) (api.tarray t_dynamic) p
 	in
 	let make_meta l =

+ 5 - 1
genas3.ml

@@ -107,6 +107,9 @@ let reserved =
 	(* some globals give some errors with Flex SDK as well *)
 	"print";"trace";
 	(* we don't include get+set since they are not 'real' keywords, but they can't be used as method names *)
+	"function";"class";"var";"if";"else";"while";"do";"for";"break";"continue";"return";"extends";"implements";
+	"import";"switch";"case";"default";"static";"public";"private";"try";"catch";"new";"this";"throw";"interface";
+	"override";"package";"null";"true";"false"
 	];
 	h
 
@@ -672,7 +675,8 @@ 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;
+		let quote s = if Hashtbl.mem reserved s then "\"" ^ s ^ "\"" else s in 
+		concat ctx ", " (fun (f,e) -> print ctx "%s : " (quote f); gen_value ctx e) fields;
 		spr ctx "}"
 	| TFor (v,it,e) ->
 		let handle_break = handle_break ctx e in

+ 5 - 0
tests/unit/TestBasetypes.hx

@@ -289,4 +289,9 @@ class TestBasetypes extends Test {
 		t( h.remove(1) );
 		f( h.remove(1) );
 	}
+	
+	function testObject() {
+		var l = { "new": "test" };
+		eq(Reflect.field(l, "new"), "test");
+	}
 }