Browse Source

[js] use array access instead of dot access for $hx_exports (closes #4185)

Dan Korostelev 10 years ago
parent
commit
de427d45dc
1 changed files with 10 additions and 5 deletions
  1. 10 5
      genjs.ml

+ 10 - 5
genjs.ml

@@ -938,6 +938,11 @@ let check_field_name c f =
 		error ("The field name '" ^ f.cf_name ^ "'  is not allowed in JS") (match f.cf_expr with None -> c.cl_pos | Some e -> e.epos);
 		error ("The field name '" ^ f.cf_name ^ "'  is not allowed in JS") (match f.cf_expr with None -> c.cl_pos | Some e -> e.epos);
 	| _ -> ()
 	| _ -> ()
 
 
+(* convert a.b.c to ["a"]["b"]["c"] *)
+let path_to_brackets path =
+	let parts = ExtString.String.nsplit path "." in
+	"[\"" ^ (String.concat "\"][\"" parts) ^ "\"]"
+
 let gen_class_static_field ctx c f =
 let gen_class_static_field ctx c f =
 	match f.cf_expr with
 	match f.cf_expr with
 	| None | Some { eexpr = TConst TNull } when not (has_feature ctx "Type.getClassFields") ->
 	| None | Some { eexpr = TConst TNull } when not (has_feature ctx "Type.getClassFields") ->
@@ -954,7 +959,7 @@ let gen_class_static_field ctx c f =
 			let dot_path = (dot_path c.cl_path) ^ (static_field c f.cf_name) in
 			let dot_path = (dot_path c.cl_path) ^ (static_field c f.cf_name) in
 			ctx.id_counter <- 0;
 			ctx.id_counter <- 0;
 			print ctx "%s = " path;
 			print ctx "%s = " path;
-			(match (get_exposed ctx dot_path f.cf_meta) with [s] -> print ctx "$hx_exports.%s = " s | _ -> ());
+			(match (get_exposed ctx dot_path f.cf_meta) with [s] -> print ctx "$hx_exports%s = " (path_to_brackets s) | _ -> ());
 			gen_value ctx e;
 			gen_value ctx e;
 			newline ctx;
 			newline ctx;
 		| _ ->
 		| _ ->
@@ -1007,7 +1012,7 @@ let generate_class ctx c =
 		print ctx "%s = " p
 		print ctx "%s = " p
 	else
 	else
 		print ctx "%s = $hxClasses[\"%s\"] = " p (dot_path c.cl_path);
 		print ctx "%s = $hxClasses[\"%s\"] = " p (dot_path c.cl_path);
-	(match (get_exposed ctx (dot_path c.cl_path) c.cl_meta) with [s] -> print ctx "$hx_exports.%s = " s | _ -> ());
+	(match (get_exposed ctx (dot_path c.cl_path) c.cl_meta) with [s] -> print ctx "$hx_exports%s = " (path_to_brackets s) | _ -> ());
 	(match c.cl_kind with
 	(match c.cl_kind with
 		| KAbstractImpl _ ->
 		| KAbstractImpl _ ->
 			(* abstract implementations only contain static members and don't need to have constructor functions *)
 			(* abstract implementations only contain static members and don't need to have constructor functions *)
@@ -1320,7 +1325,7 @@ let generate com =
 		print ctx "(function (%s) { \"use strict\"" (String.concat ", " (List.map fst closureArgs));
 		print ctx "(function (%s) { \"use strict\"" (String.concat ", " (List.map fst closureArgs));
 		newline ctx;
 		newline ctx;
 		let rec print_obj f root = (
 		let rec print_obj f root = (
-			let path = root ^ "." ^ f.os_name in
+			let path = root ^ (path_to_brackets f.os_name) in
 			print ctx "%s = %s || {}" path path;
 			print ctx "%s = %s || {}" path path;
 			ctx.separator <- true;
 			ctx.separator <- true;
 			newline ctx;
 			newline ctx;
@@ -1394,12 +1399,12 @@ let generate com =
 		newline ctx;
 		newline ctx;
 		if (anyExposed && (Common.defined com Define.ShallowExpose)) then (
 		if (anyExposed && (Common.defined com Define.ShallowExpose)) then (
 			List.iter (fun f ->
 			List.iter (fun f ->
-				print ctx "var %s = $hx_exports.%s" f.os_name f.os_name;
+				print ctx "var %s = $hx_exports%s" f.os_name (path_to_brackets f.os_name);
 				ctx.separator <- true;
 				ctx.separator <- true;
 				newline ctx
 				newline ctx
 			) exposedObject.os_fields;
 			) exposedObject.os_fields;
 			List.iter (fun f ->
 			List.iter (fun f ->
-				print ctx "var %s = $hx_exports.%s" f f;
+				print ctx "var %s = $hx_exports%s" f (path_to_brackets f);
 				ctx.separator <- true;
 				ctx.separator <- true;
 				newline ctx
 				newline ctx
 			) !toplevelExposed
 			) !toplevelExposed