Browse Source

[cs] don't call static init twice in generic classes (fixes #6845)

Aleksandr Kuzmenko 6 years ago
parent
commit
185904d1b6
2 changed files with 40 additions and 4 deletions
  1. 23 4
      src/generators/gencs.ml
  2. 17 0
      tests/unit/src/unit/issues/Issue6845.hx

+ 23 - 4
src/generators/gencs.ml

@@ -2512,14 +2512,33 @@ let generate con =
 			(match cl.cl_init with
 				| None -> ()
 				| Some init ->
+					let needs_block,write_expr =
+						let unchecked = needs_unchecked init in
+						if cl.cl_params = [] then
+							unchecked, (fun() ->
+								if unchecked then write w "unchecked";
+								expr_s false w (mk_block init)
+							)
+						else begin
+							write w "static bool __hx_init_called = false;";
+							newline w;
+							true, (fun() ->
+								let flag = (t_s (TInst(cl, List.map (fun _ -> t_empty) cl.cl_params))) ^ ".__hx_init_called" in
+								write w ("if(" ^ flag ^ ") return;");
+								newline w;
+								write w (flag ^ " = true;");
+								if unchecked then write w "unchecked";
+								newline w
+							)
+						end
+					in
 					print w "static %s() " (snd cl.cl_path);
-					if needs_unchecked init then begin
+					if needs_block then begin
 						begin_block w;
-						write w "unchecked ";
-						expr_s false w (mk_block init);
+						write_expr();
 						end_block w;
 					end else
-						expr_s false w (mk_block init);
+						write_expr();
 					line_reset_directive w;
 					newline w;
 					newline w

+ 17 - 0
tests/unit/src/unit/issues/Issue6845.hx

@@ -0,0 +1,17 @@
+package unit.issues;
+
+class Issue6845 extends unit.Test {
+	static var tmp:Any;
+	function test() {
+		tmp = new Sample<Float>();
+		tmp = new Sample<Bool>();
+		eq(2, Sample.n);
+	}
+}
+
+private class Sample<T> {
+	public static var n:Int =0;
+	public function new() {
+		n++;
+	}
+}