Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<project>
<!-- These values are added to the project XML when you include this library. -->
<haxelib name="thx.semver" />
<!-- <haxelib name="hscript" /> This library is now OPTIONAL. -->

<haxeflag name="--macro" value="polymod.hscript._internal.HScriptRedirectDefines.run()" if="POLYMOD_REDIRECT_HSCRIPT" />
<!--
<haxeflag name="- -macro" value="polymod.hscript._internal.PolymodFinalMacro.locateAllFinals()" />
-->
Expand Down
59 changes: 59 additions & 0 deletions polymod/hscript/_internal/HScriptRedirectDefines.hx
Original file line number Diff line number Diff line change
@@ -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<String> = ['polymod', 'hscript', '_internal'];
var typePack:Array<String> = 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