浏览代码

[cs] Add uncheced block if needed to cl_init

 * Closes #4870
Cauê Waneck 9 年之前
父节点
当前提交
e980e3a181
共有 3 个文件被更改,包括 46 次插入29 次删除
  1. 35 28
      src/generators/gencs.ml
  2. 0 1
      tests/unit/compile-cs.hxml
  3. 11 0
      tests/unit/src/unit/issues/Issue4870.hx

+ 35 - 28
src/generators/gencs.ml

@@ -1982,6 +1982,34 @@ let configure gen =
 		end;
 	in
 
+	let needs_unchecked e =
+		let rec loop e = match e.eexpr with
+		(* a non-zero integer constant means that we want unchecked context *)
+		| TConst (TInt i) when i <> Int32.zero ->
+			raise Exit
+
+		(* don't recurse into explicit checked blocks *)
+		| TCall ({ eexpr = TLocal({ v_name = "__checked__" }) }, _) ->
+			()
+
+		(* skip reflection field hashes as they are safe *)
+		| TNew ({ cl_path = (["haxe"; "lang"],"DynamicObject") }, [], [_; e1; _; e2]) ->
+			loop e1;
+			loop e2
+		| TNew ({ cl_path = (["haxe"; "lang"],"Closure") }, [], [eo; _; _]) ->
+			loop eo
+		| TCall ({ eexpr = TField (_, FStatic ({ cl_path = ["haxe"; "lang"],"Runtime" },
+				 { cf_name = "getField" | "setField" | "getField_f" | "setField_f" | "callField" })) },
+				 eo :: _ :: _ :: rest) ->
+			loop eo;
+			List.iter loop rest
+
+		| _ ->
+			Type.iter loop e
+		in
+		try (loop e; false) with Exit -> true
+	in
+
 	let rec gen_class_field w ?(is_overload=false) is_static cl is_final cf =
 		gen_attributes w cf.cf_meta;
 		let is_interface = cl.cl_interface in
@@ -2097,33 +2125,6 @@ let configure gen =
 											| _ -> assert false (* FIXME *)
 								in
 
-								let needs_unchecked e =
-									let rec loop e = match e.eexpr with
-									(* a non-zero integer constant means that we want unchecked context *)
-									| TConst (TInt i) when i <> Int32.zero ->
-										raise Exit
-
-									(* don't recurse into explicit checked blocks *)
-									| TCall ({ eexpr = TLocal({ v_name = "__checked__" }) }, _) ->
-										()
-
-									(* skip reflection field hashes as they are safe *)
-									| TNew ({ cl_path = (["haxe"; "lang"],"DynamicObject") }, [], [_; e1; _; e2]) ->
-										loop e1;
-										loop e2
-									| TNew ({ cl_path = (["haxe"; "lang"],"Closure") }, [], [eo; _; _]) ->
-										loop eo
-									| TCall ({ eexpr = TField (_, FStatic ({ cl_path = ["haxe"; "lang"],"Runtime" },
-											 { cf_name = "getField" | "setField" | "getField_f" | "setField_f" | "callField" })) },
-											 eo :: _ :: _ :: rest) ->
-										loop eo;
-										List.iter loop rest
-
-									| _ ->
-										Type.iter loop e
-									in
-									try (loop e; false) with Exit -> true
-								in
 								let write_method_expr e =
 									match e.eexpr with
 									| TBlock [] ->
@@ -2445,7 +2446,13 @@ let configure gen =
 			| None -> ()
 			| Some init ->
 				print w "static %s() " (snd cl.cl_path);
-				expr_s w (mk_block init);
+				if needs_unchecked init then begin
+					begin_block w;
+					write w "unchecked ";
+					expr_s w (mk_block init);
+					end_block w;
+				end else
+					expr_s w (mk_block init);
 				line_reset_directive w;
 				newline w;
 				newline w

+ 0 - 1
tests/unit/compile-cs.hxml

@@ -7,4 +7,3 @@ compile-each.hxml
 -main unit.Test
 -cs bin/cs
 -net-lib native_cs/bin/native_cs.dll
--D dump

+ 11 - 0
tests/unit/src/unit/issues/Issue4870.hx

@@ -0,0 +1,11 @@
+package unit.issues;
+
+class Issue4870 extends Test {
+  @:keep static inline var VALUE:UInt = 0x9747b28c;
+  @:keep static var VALUE2:UInt = 0x9747b28c;
+  function test() {
+    t(VALUE == 0x9747b28c);
+    t(VALUE2 > 0);
+  }
+}
+