Prechádzať zdrojové kódy

[macro] Update macro API restriction warnings (when using -D haxe-next) (#11377)

* [macro] Update macro API restriction warnings (when using -D haxe-next)

* Avoid changes in behavior without -D haxe-next
Rudy Ges 1 rok pred
rodič
commit
38c4d0c42a

+ 36 - 34
extra/ImportAll.hx

@@ -72,42 +72,44 @@ class ImportAll {
 			if (!Context.defined("neko") && !Context.defined("cpp")) return;
 		case "tools", "build-tool", "jar-tool": return;
 		}
-		for( p in Context.getClassPath() ) {
-			if( p == "/" || p == "" )
-				continue;
-			// skip if we have a classpath to haxe
-			if( pack.length == 0 && sys.FileSystem.exists(p+"std") )
-				continue;
-			var p = p + pack.split(".").join("/");
-			if( StringTools.endsWith(p,"/") )
-				p = p.substr(0,-1);
-			if( !sys.FileSystem.exists(p) || !sys.FileSystem.isDirectory(p) )
-				continue;
-			for( file in sys.FileSystem.readDirectory(p) ) {
-				if( file == ".svn" || file == "_std" )
+		Context.onAfterInitMacros(() -> {
+			for( p in Context.getClassPath() ) {
+				if( p == "/" || p == "" )
 					continue;
-				var full = (pack == "") ? file : pack + "." + file;
-				if( StringTools.endsWith(file, ".hx") && file.substr(0, file.length - 3).indexOf(".") < 0 ) {
-					var cl = full.substr(0, full.length - 3);
-					switch( cl ) {
-					case "ImportAll", "neko.db.MacroManager": continue;
-					case "haxe.TimerQueue": if( Context.defined("neko") || Context.defined("php") || Context.defined("cpp") ) continue;
-					case "Sys": if(!isSysTarget()) continue;
-					case "haxe.web.Request": if( !(Context.defined("neko") || Context.defined("php") || Context.defined("js")) ) continue;
-					case "haxe.macro.ExampleJSGenerator","haxe.macro.Context", "haxe.macro.Compiler": if( !Context.defined("eval") ) continue;
-					case "haxe.remoting.SocketWrapper": if( !Context.defined("flash") ) continue;
-					case "haxe.remoting.SyncSocketConnection": if( !(Context.defined("neko") || Context.defined("php") || Context.defined("cpp")) ) continue;
-					case "neko.vm.Ui" | "sys.db.Sqlite" | "sys.db.Mysql" if ( Context.defined("interp") ): continue;
-					case "sys.db.Sqlite" | "sys.db.Mysql" | "cs.db.AdoNet" if ( Context.defined("cs") ): continue;
-					case "haxe.atomic.AtomicBool" if(!Context.defined("target.atomics")): continue;
-					case "haxe.atomic.AtomicInt" if(!Context.defined("target.atomics")): continue;
-					case "haxe.atomic.AtomicObject" if(!Context.defined("target.atomics") || Context.defined("js") || Context.defined("cpp")): continue;
-					}
-					Context.getModule(cl);
-				} else if( sys.FileSystem.isDirectory(p + "/" + file) )
-					run(full);
+				// skip if we have a classpath to haxe
+				if( pack.length == 0 && sys.FileSystem.exists(p+"std") )
+					continue;
+				var p = p + pack.split(".").join("/");
+				if( StringTools.endsWith(p,"/") )
+					p = p.substr(0,-1);
+				if( !sys.FileSystem.exists(p) || !sys.FileSystem.isDirectory(p) )
+					continue;
+				for( file in sys.FileSystem.readDirectory(p) ) {
+					if( file == ".svn" || file == "_std" )
+						continue;
+					var full = (pack == "") ? file : pack + "." + file;
+					if( StringTools.endsWith(file, ".hx") && file.substr(0, file.length - 3).indexOf(".") < 0 ) {
+						var cl = full.substr(0, full.length - 3);
+						switch( cl ) {
+						case "ImportAll", "neko.db.MacroManager": continue;
+						case "haxe.TimerQueue": if( Context.defined("neko") || Context.defined("php") || Context.defined("cpp") ) continue;
+						case "Sys": if(!isSysTarget()) continue;
+						case "haxe.web.Request": if( !(Context.defined("neko") || Context.defined("php") || Context.defined("js")) ) continue;
+						case "haxe.macro.ExampleJSGenerator","haxe.macro.Context", "haxe.macro.Compiler": if( !Context.defined("eval") ) continue;
+						case "haxe.remoting.SocketWrapper": if( !Context.defined("flash") ) continue;
+						case "haxe.remoting.SyncSocketConnection": if( !(Context.defined("neko") || Context.defined("php") || Context.defined("cpp")) ) continue;
+						case "neko.vm.Ui" | "sys.db.Sqlite" | "sys.db.Mysql" if ( Context.defined("interp") ): continue;
+						case "sys.db.Sqlite" | "sys.db.Mysql" | "cs.db.AdoNet" if ( Context.defined("cs") ): continue;
+						case "haxe.atomic.AtomicBool" if(!Context.defined("target.atomics")): continue;
+						case "haxe.atomic.AtomicInt" if(!Context.defined("target.atomics")): continue;
+						case "haxe.atomic.AtomicObject" if(!Context.defined("target.atomics") || Context.defined("js") || Context.defined("cpp")): continue;
+						}
+						Context.getModule(cl);
+					} else if( sys.FileSystem.isDirectory(p + "/" + file) )
+						run(full);
+				}
 			}
-		}
+		});
 	}
 
 }

+ 3 - 1
std/haxe/macro/CompilationServer.hx

@@ -101,7 +101,9 @@ class CompilationServer {
 	**/
 	static public function setModuleCheckPolicy(pathFilters:Array<String>, policy:Array<ModuleCheckPolicy>, ?recursive = true,
 			?contextOptions:ContextOptions = NormalContext) {
-		@:privateAccess Compiler.load("server_add_module_check_policy", 4)(pathFilters, policy, recursive, contextOptions);
+		Context.onAfterInitMacros(() -> {
+			@:privateAccess Compiler.load("server_add_module_check_policy", 4)(pathFilters, policy, recursive, contextOptions);
+		});
 	}
 
 	/**

+ 13 - 5
std/haxe/macro/Compiler.hx

@@ -93,7 +93,11 @@ class Compiler {
 		if (!ident.match(field))
 			throw "Invalid " + field;
 		#if (neko || eval)
+		#if haxe_next
+		Context.onAfterInitMacros(() -> load("type_patch", 4)(className, field, isStatic == true, null));
+		#else
 		load("type_patch", 4)(className, field, isStatic == true, null);
+		#end
 		#else
 		typePatch(className, field, isStatic == true, null);
 		#end
@@ -109,7 +113,11 @@ class Compiler {
 		if (!ident.match((field.charAt(0) == "$") ? field.substr(1) : field))
 			throw "Invalid " + field;
 		#if (neko || eval)
+		#if haxe_next
+		Context.onAfterInitMacros(() -> load("type_patch", 4)(className, field, isStatic == true, type));
+		#else
 		load("type_patch", 4)(className, field, isStatic == true, type);
+		#end
 		#else
 		typePatch(className, field, isStatic == true, type);
 		#end
@@ -125,7 +133,11 @@ class Compiler {
 		if (field != null && !ident.match(field))
 			throw "Invalid " + field;
 		#if (neko || eval)
+		#if haxe_next
+		Context.onAfterInitMacros(() -> load("meta_patch", 4)(meta, className, field, isStatic == true));
+		#else
 		load("meta_patch", 4)(meta, className, field, isStatic == true);
+		#end
 		#else
 		metaPatch(meta, className, field, isStatic == true);
 		#end
@@ -284,11 +296,7 @@ class Compiler {
 				Context.error('Package "$pack" was not found in any of class paths', Context.currentPos());
 		}
 
-		if (!Context.initMacrosDone()) {
-			Context.onAfterInitMacros(() -> include(pack, rec, ignore, classPaths, strict));
-		} else {
-			include(pack, rec, ignore, classPaths, strict);
-		}
+		Context.onAfterInitMacros(() -> include(pack, rec, ignore, classPaths, strict));
 	}
 
 	/**

+ 18 - 5
std/haxe/macro/Context.hx

@@ -175,6 +175,7 @@ class Context {
 		Returns `null` if the current macro is not a `@:genericBuild` macro.
 	**/
 	public static function getCallArguments():Null<Array<Expr>> {
+		assertInitMacrosDone(false);
 		return load("get_call_arguments", 0)();
 	}
 
@@ -184,6 +185,7 @@ class Context {
 		If no such class exists, `null` is returned.
 	**/
 	public static function getLocalClass():Null<Type.Ref<Type.ClassType>> {
+		assertInitMacrosDone(false);
 		var l:Type = load("get_local_type", 0)();
 		if (l == null)
 			return null;
@@ -197,6 +199,7 @@ class Context {
 		Returns the current module path in/on which the macro was called.
 	**/
 	public static function getLocalModule():String {
+		assertInitMacrosDone(false);
 		return load("get_local_module", 0)();
 	}
 
@@ -206,6 +209,7 @@ class Context {
 		If no such type exists, `null` is returned.
 	**/
 	public static function getLocalType():Null<Type> {
+		assertInitMacrosDone(false);
 		return load("get_local_type", 0)();
 	}
 
@@ -215,6 +219,7 @@ class Context {
 		If no such method exists, `null` is returned.
 	**/
 	public static function getLocalMethod():Null<String> {
+		assertInitMacrosDone(false);
 		return load("get_local_method", 0)();
 	}
 
@@ -225,6 +230,7 @@ class Context {
 		Modifying the returned array has no effect on the compiler.
 	**/
 	public static function getLocalUsing():Array<Type.Ref<Type.ClassType>> {
+		assertInitMacrosDone(false);
 		return load("get_local_using", 0)();
 	}
 
@@ -234,6 +240,7 @@ class Context {
 		Modifying the returned array has no effect on the compiler.
 	**/
 	public static function getLocalImports():Array<ImportExpr> {
+		assertInitMacrosDone(false);
 		return load("get_local_imports", 0)();
 	}
 
@@ -248,6 +255,7 @@ class Context {
 	**/
 	@:deprecated("Use Context.getLocalTVars() instead")
 	public static function getLocalVars():Map<String, Type> {
+		assertInitMacrosDone(false);
 		return load("local_vars", 1)(false);
 	}
 
@@ -256,6 +264,7 @@ class Context {
 		of `Type`.
 	**/
 	public static function getLocalTVars():Map<String, Type.TVar> {
+		assertInitMacrosDone(false);
 		return load("local_vars", 1)(true);
 	}
 
@@ -459,8 +468,11 @@ class Context {
 		actual typing.
 	**/
 	public static function onAfterInitMacros(callback:Void->Void):Void {
-		assertInitMacro();
-		load("on_after_init_macros", 1)(callback);
+		if (Context.initMacrosDone()) {
+			callback();
+		} else {
+			load("on_after_init_macros", 1)(callback);
+		}
 	}
 
 	/**
@@ -619,6 +631,7 @@ class Context {
 		This is only defined for `@:build/@:autoBuild` macros.
 	**/
 	public static function getBuildFields():Array<Field> {
+		assertInitMacrosDone(false);
 		return load("get_build_fields", 0)();
 	}
 
@@ -760,8 +773,7 @@ class Context {
 		run your code once typer is ready to be used.
 	**/
 	public static function registerModuleDependency(modulePath:String, externFile:String) {
-		assertInitMacrosDone();
-		load("register_module_dependency", 2)(modulePath, externFile);
+		onAfterInitMacros(() -> load("register_module_dependency", 2)(modulePath, externFile));
 	}
 
 	/**
@@ -848,7 +860,7 @@ class Context {
 	}
 
 	private static function sExpr(e:TypedExpr, pretty:Bool):String {
-		return haxe.macro.Context.load("s_expr", 2)(e, pretty);
+		return load("s_expr", 2)(e, pretty);
 	}
 
 	@:allow(haxe.macro.Compiler)
@@ -863,6 +875,7 @@ class Context {
 		}
 	}
 
+	@:allow(haxe.macro.Compiler)
 	private static function assertInitMacrosDone(includeSuggestion = true):Void {
 		#if haxe_next
 		if (!initMacrosDone()) {

+ 1 - 1
tests/misc/projects/Issue10844/user-defined-define-json-fail.hxml.stderr

@@ -1,3 +1,3 @@
 (unknown) : Uncaught exception Could not read file define.jsno
-$$normPath(::std::)/haxe/macro/Compiler.hx:493: characters 11-39 : Called from here
+$$normPath(::std::)/haxe/macro/Compiler.hx:501: characters 11-39 : Called from here
 (unknown) : Called from here

+ 1 - 1
tests/misc/projects/Issue10844/user-defined-meta-json-fail.hxml.stderr

@@ -1,3 +1,3 @@
 (unknown) : Uncaught exception Could not read file meta.jsno
-$$normPath(::std::)/haxe/macro/Compiler.hx:483: characters 11-39 : Called from here
+$$normPath(::std::)/haxe/macro/Compiler.hx:491: characters 11-39 : Called from here
 (unknown) : Called from here

+ 1 - 1
tests/misc/projects/Issue10844/user-defined-meta-json-indent-fail.hxml.stderr

@@ -1,3 +1,3 @@
 (unknown) : Uncaught exception Could not read file meta.jsno
-  $$normPath(::std::)/haxe/macro/Compiler.hx:483: characters 11-39 : Called from here
+  $$normPath(::std::)/haxe/macro/Compiler.hx:491: characters 11-39 : Called from here
   (unknown) : Called from here

+ 2 - 2
tests/misc/projects/Issue10844/user-defined-meta-json-pretty-fail.hxml.stderr

@@ -2,9 +2,9 @@
 
    | Uncaught exception Could not read file meta.jsno
 
-    ->  $$normPath(::std::)/haxe/macro/Compiler.hx:483: characters 11-39
+    ->  $$normPath(::std::)/haxe/macro/Compiler.hx:491: characters 11-39
 
-    483 |   var f = sys.io.File.getContent(path);
+    491 |   var f = sys.io.File.getContent(path);
         |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         | Called from here
 

+ 0 - 1
tests/misc/projects/recursive-callbacks/compile.hxml.stderr

@@ -1,5 +1,4 @@
 Main.hx:5: characters 20-24 : Warning : 1
-Main.hx:9: lines 9-11 : Warning : This API should only be used from initialization macros.
 Main.hx:5: characters 20-24 : Warning : 2
 Main.hx:5: characters 20-24 : Warning : 3
 Main.hx:5: characters 20-24 : Warning : 4