Browse Source

adapt captured vars

Simon Krajewski 5 tháng trước cách đây
mục cha
commit
cbee5cbc8c
3 tập tin đã thay đổi với 45 bổ sung30 xóa
  1. 26 26
      src/filters/capturedVars.ml
  2. 8 1
      src/filters/filters.ml
  3. 11 3
      src/typing/macroContext.ml

+ 26 - 26
src/filters/capturedVars.ml

@@ -17,32 +17,13 @@
 	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  *)
 open Globals
+open SafeCom
 open Type
-open Common
 open LocalUsage
 
-(* BLOCK VARIABLES CAPTURE *)
-(*
-	For some platforms, it will simply mark the variables which are used in closures
-	using the v_capture flag so it can be processed in a more optimized
-
-	For Flash/JS platforms, it will ensure that variables used in loop sub-functions
-	have an unique scope. It transforms the following expression :
-
-	for( x in array )
-		funs.push(function() return x++);
-
-	Into the following :
-
-	for( _x in array ) {
-		var x = [_x];
-		funs.push(function(x) { function() return x[0]++; }(x));
-	}
-*)
-let captured_vars com e =
-	let t = com.basic in
-
-	let impl = match com.platform with
+let get_wrapper_implementation com =
+	let t = com.Common.basic in
+	match com.platform with
 	(* optimized version for Java - use native arrays *)
 	| Jvm ->
 		let cnativearray =
@@ -84,7 +65,26 @@ let captured_vars com e =
 			method mk_init av v pos =
 				mk (TVar (av,Some (mk (TArrayDecl [mk (TLocal v) v.v_type pos]) av.v_type pos))) t.tvoid pos
 		end
-	in
+
+(* BLOCK VARIABLES CAPTURE *)
+(*
+	For some platforms, it will simply mark the variables which are used in closures
+	using the v_capture flag so it can be processed in a more optimized
+
+	For Flash/JS platforms, it will ensure that variables used in loop sub-functions
+	have an unique scope. It transforms the following expression :
+
+	for( x in array )
+		funs.push(function() return x++);
+
+	Into the following :
+
+	for( _x in array ) {
+		var x = [_x];
+		funs.push(function(x) { function() return x[0]++; }(x));
+	}
+*)
+let captured_vars scom impl e =
 
 	let mk_var v used =
 		let v2 = alloc_var v.v_kind v.v_name (PMap.find v.v_id used) v.v_pos in
@@ -146,7 +146,7 @@ let captured_vars com e =
 				Create a new function scope to make sure that the captured loop variable
 				will not be overwritten in next loop iteration
 			*)
-			if com.config.pf_capture_policy = CPLoopVars then
+			if scom.platform_config.pf_capture_policy = CPLoopVars then
 				(* We don't want to duplicate any variable declarations, so let's make copies (issue #3902). *)
 				let new_vars = List.map (fun v -> v.v_id,alloc_var v.v_kind v.v_name v.v_type v.v_pos) vars in
 				let rec loop e = match e.eexpr with
@@ -273,7 +273,7 @@ let captured_vars com e =
 		!assigned
 	in
 	let captured = all_vars e in
-	match com.config.pf_capture_policy with
+	match scom.platform_config.pf_capture_policy with
 	| CPNone -> e
 	| CPWrapRef -> do_wrap captured e
 	| CPLoopVars -> out_loop e

+ 8 - 1
src/filters/filters.ml

@@ -467,9 +467,16 @@ let run tctx ectx main before_destruction =
 		"reduce_expression",Optimizer.reduce_expression;
 		"inline_constructors",InlineConstructors.inline_constructors;
 		"Exceptions_filter",(fun _ -> Exceptions.filter ectx);
-		"captured_vars",(fun _ -> CapturedVars.captured_vars com);
 	] in
 	List.iter (run_expression_filters tctx detail_times filters) new_types;
+
+	let cv_wrapper_impl = CapturedVars.get_wrapper_implementation com in
+	let filters = [
+		"captured_vars",(fun scom -> CapturedVars.captured_vars scom cv_wrapper_impl);
+	] in
+	run_parallel_safe com scom (fun pool ->
+		Parallel.ParallelArray.iter pool (SafeCom.run_expression_filters_safe scom detail_times filters) new_types_array
+	);
 	enter_stage com CAnalyzerStart;
 	if com.platform <> Cross then Analyzer.Run.run_on_types com new_types;
 	enter_stage com CAnalyzerDone;

+ 11 - 3
src/typing/macroContext.ml

@@ -652,15 +652,23 @@ and flush_macro_context mint mctx =
 		in
 		if apply_native then Native.apply_native_paths t
 	in
+	let scom_from_tctx tctx =
+		let scom = to_safe_com mctx.com in
+		let scom = {scom with curclass = tctx.c.curclass; curfield = tctx.f.curfield} in (* This isn't great *)
+		scom
+	in
+	let cv_wrapper_impl = CapturedVars.get_wrapper_implementation mctx.com in
 	let expr_filters = [
 		"handle_abstract_casts",AbstractCast.handle_abstract_casts;
 		"local_statics",(fun tctx ->
-			let scom = to_safe_com mctx.com in
-			let scom = {scom with curclass = tctx.c.curclass; curfield = tctx.f.curfield} in (* This isn't great *)
+			let scom = scom_from_tctx tctx in
 			LocalStatic.run scom
 		);
 		"Exceptions",(fun _ -> Exceptions.filter ectx);
-		"captured_vars",(fun _ -> CapturedVars.captured_vars mctx.com);
+		"captured_vars",(fun tctx ->
+			let scom = scom_from_tctx tctx in
+			CapturedVars.captured_vars scom cv_wrapper_impl
+		);
 	] in
 	let type_filters = [
 		FiltersCommon.remove_generic_base;