From d41654c0421095b48bfb5135b62d066ab4e9ec1b Mon Sep 17 00:00:00 2001 From: Hyper_ <40342021+NotHyper-474@users.noreply.github.com> Date: Wed, 18 Mar 2026 16:11:59 -0300 Subject: [PATCH] feat: `POLYMOD_REDIRECT_HSCRIPT` compile flag. Redirects references to `hscript` types to Polymod ones. --- include.xml | 3 +- .../_internal/HScriptRedirectDefines.hx | 59 +++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 polymod/hscript/_internal/HScriptRedirectDefines.hx diff --git a/include.xml b/include.xml index e28dae2b..16e21ce5 100644 --- a/include.xml +++ b/include.xml @@ -2,7 +2,8 @@ - + + diff --git a/polymod/hscript/_internal/HScriptRedirectDefines.hx b/polymod/hscript/_internal/HScriptRedirectDefines.hx new file mode 100644 index 00000000..e85f4e9a --- /dev/null +++ b/polymod/hscript/_internal/HScriptRedirectDefines.hx @@ -0,0 +1,59 @@ +package polymod.hscript._internal; + +#if macro +import haxe.macro.Context; +import haxe.macro.Compiler; +import haxe.macro.Expr.TypeDefinition; + +using StringTools; + +/** + * The macro for the `POLYMOD_REDIRECT_HSCRIPT` define functionality. + */ +class HScriptRedirectDefines +{ + static inline final DEFINE:String = 'POLYMOD_REDIRECT_HSCRIPT'; + + /** + * The entry point for the macro. + * Performs the preparations for the function that actually redirects the types. + */ + public static function run():Void + { + if (Context.defined('hscript') && Context.defined(DEFINE)) + { + Context.warning('"$DEFINE" present but hscript is installed. Not doing anything.', Context.currentPos()); + return; + } + + Compiler.define('hscript'); + Context.onTypeNotFound(generateHScriptRedirect); + } + + /** + * Returns a typedef where a referenced hscript type could not be found which redirects to a Polymod HScript class. + * @param typeName + * @return TypeDefinition + */ + static function generateHScriptRedirect(typeName:String):TypeDefinition + { + if (!typeName.startsWith('hscript')) return null; + + final POLY_PACK:Array = ['polymod', 'hscript', '_internal']; + var typePack:Array = typeName.split('.'); + var name:String = typePack.pop(); + + return { + pack: typePack, + name: name, + pos: Context.currentPos(), + kind: TDAlias(TPath( + { + pack: POLY_PACK, + name: name, + })), + fields: [] + }; + } +} +#end