Kaynağa Gözat

moved scoping config to platform global config

Nicolas Cannasse 13 yıl önce
ebeveyn
işleme
1ce828f4f5
2 değiştirilmiş dosya ile 43 ekleme ve 34 silme
  1. 7 34
      codegen.ml
  2. 36 0
      common.ml

+ 7 - 34
codegen.ml

@@ -920,35 +920,8 @@ let captured_vars com e =
 (* RENAME LOCAL VARS *)
 
 let rename_local_vars com e =
-	(*
-		Tells if we have proper {} scoping. In that case we can assume two variables
-		in separate blocks will never conflict.
-
-		We will also make sure that a variable is declared before its init value is set.
-	*)
-	let has_scope = (match com.platform with
-		| Js | Java | Cs | Php -> false
-		| Flash -> not (Common.defined com "as3") (* VM is naturally scoped *)
-		| Flash8 -> com.flash_version > 6. (* depends on VM version *)
-		| Neko | Cross | Cpp -> true
-	) in
-	(*
-		Some compiler might require an unique local name for the whole function,
-		whatever the scoping rules
-	*)
-	let unique_var = (match com.platform with
-		| Cs -> true
-		| Flash -> Common.defined com "as3"
-		| _ -> false
-	) in
-	(*
-		Tells if captured variables are also scoped (or are global names)
-	*)
-	let capture_scope = (match com.platform with
-		| Js | Flash8 | Php -> false
-		| _ -> true
-	) in
-	let all_scope = not capture_scope || not has_scope in
+	let cfg = com.config in
+	let all_scope = (not cfg.pf_captured_scope) || (not cfg.pf_locals_scope) in
 	let vars = ref PMap.empty in
 	let all_vars = ref PMap.empty in
 	let vtemp = alloc_var "~" t_dynamic in
@@ -958,7 +931,7 @@ let rename_local_vars com e =
 	in
 	let save() = 
 		let old = !vars in 
-		if unique_var then (fun() -> ()) else (fun() -> vars := if !rebuild_vars then rebuild old else old)
+		if cfg.pf_unique_locals then (fun() -> ()) else (fun() -> vars := if !rebuild_vars then rebuild old else old)
 	in
 	let rename vars v =
 		let count = ref 1 in
@@ -970,7 +943,7 @@ let rename_local_vars com e =
 	let declare v =
 		(* chop escape char for all local variables generated *)
 		if String.unsafe_get v.v_name 0 = String.unsafe_get gen_local_prefix 0 then v.v_name <- "_g" ^ String.sub v.v_name 1 (String.length v.v_name - 1);
-		let look_vars = (if not capture_scope && v.v_capture then !all_vars else !vars) in
+		let look_vars = (if not cfg.pf_captured_scope && v.v_capture then !all_vars else !vars) in
 		(try
 			let v2 = PMap.find v.v_name look_vars in
 			(*
@@ -992,7 +965,7 @@ let rename_local_vars com e =
 	let check t =
 		match (t_infos t).mt_path with
 		| [], name | name :: _, _ ->
-			let vars = if has_scope then vars else all_vars in
+			let vars = if cfg.pf_locals_scope then vars else all_vars in
 			(try
 				let v = PMap.find name !vars in
 				if v == vtemp then raise Not_found; (* ignore *)
@@ -1014,9 +987,9 @@ let rename_local_vars com e =
 		match e.eexpr with
 		| TVars l ->
 			List.iter (fun (v,e) ->
-				if not has_scope then declare v;
+				if not cfg.pf_locals_scope then declare v;
 				(match e with None -> () | Some e -> loop e);
-				if has_scope then declare v;
+				if cfg.pf_locals_scope then declare v;
 			) l
 		| TFunction tf ->
 			let old = save() in

+ 36 - 0
common.ml

@@ -58,6 +58,12 @@ type platform_config = {
 	pf_static : bool;
 	(** has access to the "sys" package *)
 	pf_sys : bool;
+	(** local variables are block-scoped *)
+	pf_locals_scope : bool;
+	(** captured local variables are scoped *)
+	pf_captured_scope : bool;
+	(** generated locals must be absolutely unique wrt the current function *)
+	pf_unique_locals : bool;
 }
 
 type context = {
@@ -117,6 +123,9 @@ let default_config =
 	{
 		pf_static = true;
 		pf_sys = true;
+		pf_locals_scope = true;
+		pf_captured_scope = true;
+		pf_unique_locals = false;
 	}
 
 let get_config com =
@@ -128,46 +137,73 @@ let get_config com =
 		{
 			pf_static = false;
 			pf_sys = false;
+			pf_locals_scope = com.flash_version > 6.;
+			pf_captured_scope = false;
+			pf_unique_locals = false;
 		}
 	| Js ->
 		{
 			pf_static = false;
 			pf_sys = false;
+			pf_locals_scope = false;		
+			pf_captured_scope = false;
+			pf_unique_locals = false;
 		}
 	| Neko ->
 		{
 			pf_static = false;
 			pf_sys = true;
+			pf_locals_scope = true;
+			pf_captured_scope = true;
+			pf_unique_locals = false;
 		}
 	| Flash when defined "as3" ->
 		{
 			pf_static = true;
 			pf_sys = false;
+			pf_locals_scope = false;
+			pf_captured_scope = true;
+			pf_unique_locals = true;
 		}
 	| Flash ->
 		{
 			pf_static = true;
 			pf_sys = false;
+			pf_locals_scope = true;
+			pf_captured_scope = true; (* handled by genSwf9 *)
+			pf_unique_locals = false;
 		}
 	| Php ->
 		{
 			pf_static = false;
 			pf_sys = true;
+			pf_locals_scope = false; (* some duplicate work is done in genPhp *)
+			pf_captured_scope = false;
+			pf_unique_locals = false;
 		}
 	| Cpp ->
 		{
 			pf_static = true;
 			pf_sys = true;
+			pf_locals_scope = true;
+			pf_captured_scope = true;
+			pf_unique_locals = false;
 		}
 	| Cs ->
 		{
 			pf_static = true;
 			pf_sys = true;
+			pf_locals_scope = false;
+			pf_captured_scope = true;
+			pf_unique_locals = true;
 		}
 	| Java ->
 		{
 			pf_static = true;
 			pf_sys = true;
+			pf_locals_scope = false;
+			pf_captured_scope = true;
+			pf_unique_locals = false;
 		}
 
 let create v args =