Browse Source

revert the fix for "X redefined from X"
#8368, closes #8631, closes #8640

Aleksandr Kuzmenko 6 years ago
parent
commit
6ff9ee5f0a

+ 4 - 11
src/macro/eval/evalContext.ml

@@ -206,7 +206,7 @@ end
 
 
 class static_prototypes = object(self)
 class static_prototypes = object(self)
 	val mutable prototypes : vprototype IntMap.t = IntMap.empty
 	val mutable prototypes : vprototype IntMap.t = IntMap.empty
-	val mutable inits : (bool ref * vprototype * (vprototype -> unit) list) IntMap.t = IntMap.empty
+	val mutable inits : (vprototype * (vprototype -> unit) list) IntMap.t = IntMap.empty
 
 
 	method add proto =
 	method add proto =
 		prototypes <- IntMap.add proto.ppath proto prototypes
 		prototypes <- IntMap.add proto.ppath proto prototypes
@@ -215,20 +215,13 @@ class static_prototypes = object(self)
 		inits <- IntMap.remove path inits;
 		inits <- IntMap.remove path inits;
 		prototypes <- IntMap.remove path prototypes
 		prototypes <- IntMap.remove path prototypes
 
 
-	method set_needs_reset =
-		IntMap.iter (fun path (needs_reset, _, _) -> needs_reset := true) inits
+	method reset =
+		IntMap.iter (fun _ (proto, delays) -> List.iter (fun f -> f proto) delays) inits
 
 
 	method add_init proto delays =
 	method add_init proto delays =
-		inits <- IntMap.add proto.ppath (ref false, proto, delays) inits
+		inits <- IntMap.add proto.ppath (proto, delays) inits
 
 
 	method get path =
 	method get path =
-		(try
-			let (needs_reset, proto, delays) = IntMap.find path inits in
-			if !needs_reset then begin
-				needs_reset := false;
-				List.iter (fun f -> f proto) delays
-			end
-		with Not_found -> ());
 		IntMap.find path prototypes
 		IntMap.find path prototypes
 end
 end
 
 

+ 1 - 1
src/macro/eval/evalMain.ml

@@ -374,7 +374,7 @@ let setup get_api =
 
 
 let do_reuse ctx api =
 let do_reuse ctx api =
 	ctx.curapi <- api;
 	ctx.curapi <- api;
-	ctx.static_prototypes#set_needs_reset
+	ctx.static_prototypes#reset
 
 
 let set_error ctx b =
 let set_error ctx b =
 	(* TODO: Have to reset this somewhere if running compilation server. But where... *)
 	(* TODO: Have to reset this somewhere if running compilation server. But where... *)

+ 14 - 0
tests/server/src/Main.hx

@@ -193,6 +193,8 @@ class ServerTests extends HaxeServerTestCase {
 		utest.Assert.equals("function() {_Vector.Vector_Impl_.toIntVector(null);}", moreHack(type.args.statics[0].expr.testHack)); // lmao
 		utest.Assert.equals("function() {_Vector.Vector_Impl_.toIntVector(null);}", moreHack(type.args.statics[0].expr.testHack)); // lmao
 	}
 	}
 
 
+//See https://github.com/HaxeFoundation/haxe/issues/8368#issuecomment-525379060
+#if false
 	function testXRedefinedFromX() {
 	function testXRedefinedFromX() {
 		vfs.putContent("Main.hx", getTemplate("issues/Issue8368/Main.hx"));
 		vfs.putContent("Main.hx", getTemplate("issues/Issue8368/Main.hx"));
 		vfs.putContent("MyMacro.hx", getTemplate("issues/Issue8368/MyMacro.hx"));
 		vfs.putContent("MyMacro.hx", getTemplate("issues/Issue8368/MyMacro.hx"));
@@ -203,6 +205,18 @@ class ServerTests extends HaxeServerTestCase {
 		runHaxe(args);
 		runHaxe(args);
 		assertSuccess();
 		assertSuccess();
 	}
 	}
+#end
+
+	function testMacroStaticsReset() {
+		vfs.putContent("Main.hx", getTemplate("issues/Issue8631/Main.hx"));
+		vfs.putContent("Init.hx", getTemplate("issues/Issue8631/Init.hx"));
+		vfs.putContent("Macro.hx", getTemplate("issues/Issue8631/Macro.hx"));
+		var hxml = ["-main", "Main", "--macro", "Init.callMacro()", "--interp"];
+		runHaxe(hxml);
+		runHaxe(hxml);
+		var counter = vfs.getContent("counter.txt");
+		utest.Assert.equals('2', counter);
+	}
 }
 }
 
 
 class Main {
 class Main {

+ 6 - 0
tests/server/src/Vfs.hx

@@ -39,6 +39,12 @@ class Vfs {
 		Fs.writeFileSync(path.toString(), content);
 		Fs.writeFileSync(path.toString(), content);
 	}
 	}
 
 
+	public function getContent(path:String) {
+		var path = getPhysicalPath(path);
+		FileSystem.createDirectory(path.dir);
+		return Fs.readFileSync(path.toString());
+	}
+
 	public function close() {
 	public function close() {
 		removeDir(physicalPath);
 		removeDir(physicalPath);
 	}
 	}

+ 5 - 0
tests/server/test/templates/issues/Issue8631/Init.hx

@@ -0,0 +1,5 @@
+class Init {
+	static public function callMacro() {
+		Macro.call();
+	}
+}

+ 15 - 0
tests/server/test/templates/issues/Issue8631/Macro.hx

@@ -0,0 +1,15 @@
+using sys.FileSystem;
+using sys.io.File;
+
+
+ class Macro {
+	static var called:Bool = false;
+	static inline var FILE = 'counter.txt';
+
+ 	static public function call() {
+		if(called) return;
+		called = true;
+		var cnt = FILE.exists() ? Std.parseInt(FILE.getContent()) + 1 : 1;
+		FILE.saveContent('$cnt');
+	}
+}

+ 3 - 0
tests/server/test/templates/issues/Issue8631/Main.hx

@@ -0,0 +1,3 @@
+class Main {
+	static function main() {}
+}