Browse Source

fix Compiler.includeFile not working when using from within a macro (closes #4660)

Dan Korostelev 9 years ago
parent
commit
cc477b98db

+ 8 - 8
std/haxe/macro/Compiler.hx

@@ -29,15 +29,15 @@ class Compiler {
 	/**
 		A conditional compiler flag can be set command line using
 		`-D key=value`.
-		
+
 		Returns the value of a compiler flag.
-		
-		If the compiler flag is defined but no value is set, 
+
+		If the compiler flag is defined but no value is set,
 		`Compiler.getDefine` returns `"1"` (e.g. `-D key`).
-		
+
 		If the compiler flag is not defined, `Compiler.getDefine` returns
 		`null`.
-		
+
 		@see http://haxe.org/manual/lf-condition-compilation.html
 	**/
 	macro static public function getDefine( key : String ) {
@@ -361,7 +361,7 @@ class Compiler {
 	/**
 		Embed a JavaScript file at compile time (can be called by `--macro` or within an `__init__` method).
 	**/
-	public static macro function includeFile( file : String, position:IncludePosition = Top ) {
+	public static #if !macro macro #end function includeFile( file : String, position:IncludePosition = Top ) {
 		return switch ((position:String).toLowerCase()) {
 			case Inline:
 				if (Context.getLocalModule() == "")
@@ -382,11 +382,11 @@ class Compiler {
 }
 
 @:enum abstract IncludePosition(String) from String to String {
-	/** 
+	/**
 		Prepend the file content to the output file.
 	*/
 	var Top = "top";
-	/** 
+	/**
 		Prepend the file content to the body of the top-level closure.
 
 		Since the closure is in strict-mode, there may be run-time error if the input is not strict-mode-compatible.

+ 1 - 0
tests/misc/projects/Issue4660/.gitignore

@@ -0,0 +1 @@
+/out.js

+ 5 - 0
tests/misc/projects/Issue4660/Include.hx

@@ -0,0 +1,5 @@
+class Include {
+    static function use() {
+        haxe.macro.Compiler.includeFile("include.js", Top);
+    }
+}

+ 22 - 0
tests/misc/projects/Issue4660/Test.hx

@@ -0,0 +1,22 @@
+class Test {
+    static function error(msg, code) {
+        Sys.stderr().writeString(msg);
+        Sys.exit(code);
+    }
+
+    static function main() {
+        var proc = new sys.io.Process("haxe", [
+            "-js", "out.js",
+            "--macro", "Include.use()"
+        ]);
+        var stderr = proc.stderr.readAll().toString();
+        var exit = proc.exitCode();
+        if (exit != 0) {
+            error(stderr, exit);
+        } else {
+            var out = sys.io.File.getContent("out.js");
+            if (out.indexOf("THIS IS INCLUDED") == -1)
+                error("File is NOT included\n", 1);
+        }
+    }
+}

+ 1 - 0
tests/misc/projects/Issue4660/compile.hxml

@@ -0,0 +1 @@
+--run Test

+ 1 - 0
tests/misc/projects/Issue4660/include.js

@@ -0,0 +1 @@
+"THIS IS INCLUDED";