From 1e58314605f7cf516afba50ab17c11a75e452f9d Mon Sep 17 00:00:00 2001 From: hackx2 <80620538+hackx2@users.noreply.github.com> Date: Sat, 21 Nov 2026 16:25:15 +0000 Subject: [PATCH 1/3] support wildcard imports `-D hscriptNoWildcardImports` btw --- TestHScript.hx | 9 ++++ hscript/Bytes.hx | 6 ++- hscript/Expr.hx | 2 +- hscript/Interp.hx | 47 ++++++++++++++++-- hscript/Parser.hx | 7 ++- hscript/Printer.hx | 3 +- hscript/macros/WildcardMacro.hx | 81 +++++++++++++++++++++++++++++++ hscript/utils/TypeListRegistry.hx | 14 ++++++ 8 files changed, 160 insertions(+), 9 deletions(-) create mode 100644 hscript/macros/WildcardMacro.hx create mode 100644 hscript/utils/TypeListRegistry.hx diff --git a/TestHScript.hx b/TestHScript.hx index 68bba3a9..7232712e 100644 --- a/TestHScript.hx +++ b/TestHScript.hx @@ -332,6 +332,15 @@ class TestHScript extends TestCase { assertScript('import StringTools.contains; contains("hscript", "hscript")', true); // scripted class field import assertScript('class M {public static function double(val:Float){return val * 2;}} import M.double; double(10)', 20, null, true); + #if !hscriptNoWildcardImports + // wildcard imports + assertScript("import haxe.ds.*; StringMap", haxe.ds.StringMap); + final script = "import haxe.ds.*; var a = new StringMap(); a.set('hiii', 0xF0); a.get('hiii')"; + assertScript(script, 0xF0); + assertScript(script+"; StringTools.trim(' hello ')","hello"); + assertScript("import haxe.ds.*; Option.Some(123)", haxe.ds.Option.Some(123)); + assertScript("import haxe.ds.*; switch Option.Some(321) { case Option.Some(v): v; case None: 0; }", 321); + #end } function testClass() { diff --git a/hscript/Bytes.hx b/hscript/Bytes.hx index 33272061..c157563d 100644 --- a/hscript/Bytes.hx +++ b/hscript/Bytes.hx @@ -193,10 +193,11 @@ class Bytes { switch( e ) { case EPackage(n): doEncodeString(n == null ? "" : n); - case EImport(c, asName, isUsing): + case EImport(c, asName, isUsing, isWildcard): doEncodeBoolean(isUsing ?? false); doEncodeString(c); doEncodeString(asName == null ? "" : asName); + doEncodeBoolean(isWildcard ?? false); case EClass(name, fields, extend, interfaces, isFinal, _): doEncodeString(name); doEncodeString(extend == null ? "" : extend); @@ -608,7 +609,8 @@ class Bytes { final isUsing:Bool = doDecodeBoolean(); final cls = doDecodeString(); final asName = doDecodeString(); - EImport(cls, asName == "" ? null : asName, isUsing); + final isWildcard:Bool = doDecodeBoolean(); + EImport(cls, asName == "" ? null : asName, isUsing, isWildcard); case BCLASS: final name:String = doDecodeString(); final extend:String = doDecodeString(); diff --git a/hscript/Expr.hx b/hscript/Expr.hx index c1c89045..d8172973 100644 --- a/hscript/Expr.hx +++ b/hscript/Expr.hx @@ -238,7 +238,7 @@ enum Expr { ECheckType( e : Expr, t : CType ); EPackage( ?n:String ); - EImport( c : String, ?asname:String, ?isUsing:Bool ); + EImport( c : String, ?asname:String, ?isUsing:Bool, ?isWildcard:Bool ); EClass( name:String, fields:Array, ?extend:String, interfaces:Array, ?isFinal:Bool, ?isPrivate:Bool ); EEnum( en:EnumDecl, ?isAbstract:Bool ); ECast(e:Expr, ?t:CType); diff --git a/hscript/Interp.hx b/hscript/Interp.hx index 71007a8a..17b5f71b 100644 --- a/hscript/Interp.hx +++ b/hscript/Interp.hx @@ -28,6 +28,7 @@ */ package hscript; +import hscript.utils.TypeListRegistry; import haxe.Exception; import haxe.ds.StringMap; import haxe.ds.ObjectMap; @@ -172,6 +173,9 @@ class Interp { public var allowStaticVariables:Bool = false; public var allowPublicVariables:Bool = false; + #if !hscriptNoWildcardImports + public var allowWildcardImports:Bool = true; + #end @:deprecated("refer to 'hscript.Config.IMPORT_BLACKLIST'") public var importBlocklist(get, never):Array; @@ -875,8 +879,21 @@ class Interp { /** Handles `import` / `using` statements (extracted from the expr() switch). **/ @:noInline @:access(hscript.CustomClassHandler) - function exprImport(clsName:String, aliasAs:Null, isUsing:Bool):Void { + function exprImport(clsName:String, aliasAs:Null, isUsing:Bool, isWildCard:Bool):Void { if(!importEnabled) return; + + if (isWildCard) { + #if !hscriptNoWildcardImports + if(!allowWildcardImports) { + error(ECustom('Wildcard imports are disabled')); + } + exprImportWildcard(clsName.trim()); + return; + #else + error(ECustom('Wildcard imports are omitted from this build')); + return; + #end + } var splitClassName:Array = [for (e in clsName.split(".")) e.trim()]; var realClassName = splitClassName.join("."); @@ -1002,6 +1019,28 @@ class Interp { } } + #if !hscriptNoWildcardImports + /** Handles `*`(wildcard) imports **/ + @:noInline // whatever this does + function exprImportWildcard(pkg):Void { + final types = TypeListRegistry.get(pkg); + + if(types == null) { + // copying the invalid class error + error(ECustom('Invalid package: $pkg was not found')); + return; + } + + for (type in types) { + final full = pkg + '.'+ type; + + if(isBlocked(full)) continue; + if(variables.exists(type) || customClasses.exists(type)) continue; + try exprImport(full, null, false, false) catch(e) {} + } + } + #end + /** Handles `enum` declarations (extracted from the expr() switch). **/ @:noInline function exprEnum(en:EnumDecl, isAbstract:Bool):Void { @@ -1117,8 +1156,8 @@ class Interp { case EPackage(_): case EClass(name, fields, extend, interfaces, isFinal): exprClass(name, fields, extend, interfaces, isFinal); - case EImport(clsName, aliasAs, isUsing): - exprImport(clsName, aliasAs, isUsing); + case EImport(clsName, aliasAs, isUsing, isWildcard): + exprImport(clsName, aliasAs, isUsing, isWildcard); case EEnum(en, isAbstract): exprEnum(en, isAbstract); case ERegex(e, f): @@ -1803,7 +1842,7 @@ class Interp { static function walkHasFunctionLiteral(e:Expr):Bool { return switch (Tools.expr(e)) { case EFunction(_, _, _, _, _, _, _, _, _, _): true; - case EConst(_) | EIdent(_) | EBreak | EContinue | EPackage(_) | ERegex(_, _) | EImport(_, _, _): false; + case EConst(_) | EIdent(_) | EBreak | EContinue | EPackage(_) | ERegex(_, _) | EImport(_, _, _, _): false; case EVar(_, _, ev, _, _, _, _, _, _, _): ev != null && walkHasFunctionLiteral(ev); case EParent(x) | EUnop(_, _, x) | EThrow(x) | EMeta(_, _, x) | ECast(x, _): walkHasFunctionLiteral(x); diff --git a/hscript/Parser.hx b/hscript/Parser.hx index d776ca10..3a4bb024 100644 --- a/hscript/Parser.hx +++ b/hscript/Parser.hx @@ -984,6 +984,7 @@ class Parser { var isUsing = id == "using"; var oldReadPos = readPos; var tk = token(); + var isWildcard = false; switch( tk ) { case TPOpen: if(isUsing) error(ECustom('Expected identifier'),tokenMin,tokenMax); @@ -1013,6 +1014,7 @@ class Parser { if( t != TDot ) { if(t.match(TId("as"))) { if(isUsing) error(ECustom('Expected . or ;'),tokenMin,tokenMax); + if(isWildcard) error(ECustom('Expected . or ; or as'),tokenMin,tokenMax); t = token(); switch( t ) { case TId(id): @@ -1030,6 +1032,9 @@ class Parser { switch( t ) { case TId(id): path.push(id); + case TOp("*") if(!isUsing): + isWildcard = true; + break; default: unexpected(t); } @@ -1037,7 +1042,7 @@ class Parser { ensure(TSemicolon); push(TSemicolon); var p = path.join("."); - mk(EImport(p, asname, isUsing),p1); + mk(EImport(p, asname, isUsing, isWildcard),p1); default: unexpected(tk); null; diff --git a/hscript/Printer.hx b/hscript/Printer.hx index eae05250..b27cb680 100644 --- a/hscript/Printer.hx +++ b/hscript/Printer.hx @@ -121,10 +121,11 @@ class Printer { if(n != null) add(' $n'); add(';\n'); - case EImport(c, n, u): + case EImport(c, n, u, w): add('${u ? 'using' : 'import'} $c'); if(n != null) add(' as $n'); + if(w==true) add('.*'); case EClass(name, fields, extend, interfaces, fnal): var isFinal = fnal != null && fnal; if(isFinal) diff --git a/hscript/macros/WildcardMacro.hx b/hscript/macros/WildcardMacro.hx new file mode 100644 index 00000000..16945e26 --- /dev/null +++ b/hscript/macros/WildcardMacro.hx @@ -0,0 +1,81 @@ +package hscript.macros; + +#if !hscriptNoWildcardImports +import haxe.ds.StringMap; +import haxe.rtti.Meta; + +#if macro +import haxe.macro.Context; +import haxe.macro.Expr; +#end + +using StringTools; + +/** @author hackx2 **/ +@:keep +final class WildcardMacro { // uses rtti to store type names + static final INDEX_META_NAME= "__indexed_types"; + static final SEPARATOR = '~'; + #if macro + static var __has_gen = false; + #end + + public static macro function getTypes() { + #if macro + if(!__has_gen) { + __has_gen = true; + Context.onGenerate(_ -> { + switch Context.getType('hscript.macros.WildcardMacro') { + case TInst(t, _): + final meta = t.get().meta; + if(!meta.has(INDEX_META_NAME)) { + final arr:Array = [ + macro $v { + [ + for(i in Context.getAllModuleTypes()) { + final typeName = switch i { + case TClassDecl(cls): + cls.toString(); + case TEnumDecl(enum_): + enum_.toString(); + case _: null; + } + + if(typeName != null && !typeName.endsWith('_Fields_') && !typeName.endsWith('_Impl_') && !typeName.endsWith(ClassExtendMacro.CLASS_SUFFIX)) { + typeName; + } + } + ].join(SEPARATOR) + } + ]; + + meta.add(INDEX_META_NAME, arr,Context.currentPos()); + } + case _: // nothin + } + }); + } + #end + + return macro { + final types = new haxe.ds.StringMap>(); + + final rttiType = haxe.rtti.Meta.getType(hscript.macros.WildcardMacro); + final raw:Dynamic = Reflect.field(rttiType, $v{INDEX_META_NAME}); + + if(raw != null) { + for (t in (raw[0] : String).split($v{SEPARATOR})) { + final dot = t.lastIndexOf('.'); + final pack = dot == -1 ? '' : t.substr(0, dot); + final name = t.substr(dot + 1); + + final type_pack = types.get(pack); + if (type_pack == null) types.set(pack, [name]) else type_pack.push(name); + } + } + + types; + } + } +} +#end \ No newline at end of file diff --git a/hscript/utils/TypeListRegistry.hx b/hscript/utils/TypeListRegistry.hx new file mode 100644 index 00000000..641dde33 --- /dev/null +++ b/hscript/utils/TypeListRegistry.hx @@ -0,0 +1,14 @@ +package hscript.utils; + +#if !hscriptNoWildcardImports +import haxe.ds.StringMap; +import hscript.macros.WildcardMacro; + +class TypeListRegistry { + static var list(default, null):StringMap>; + public static function get(pkg:String) { + final map = list ??= WildcardMacro.getTypes(); + return map != null ? map.get(pkg) : null; + } +} +#end \ No newline at end of file From b5597eb4497068267d890dfeba6a34caebb9f689 Mon Sep 17 00:00:00 2001 From: hackx2 <80620538+hackx2@users.noreply.github.com> Date: Sat, 21 Nov 2026 17:28:13 +0000 Subject: [PATCH 2/3] remove assertions that cause an unrelated type issue --- TestHScript.hx | 2 -- 1 file changed, 2 deletions(-) diff --git a/TestHScript.hx b/TestHScript.hx index 7232712e..53cea30e 100644 --- a/TestHScript.hx +++ b/TestHScript.hx @@ -338,8 +338,6 @@ class TestHScript extends TestCase { final script = "import haxe.ds.*; var a = new StringMap(); a.set('hiii', 0xF0); a.get('hiii')"; assertScript(script, 0xF0); assertScript(script+"; StringTools.trim(' hello ')","hello"); - assertScript("import haxe.ds.*; Option.Some(123)", haxe.ds.Option.Some(123)); - assertScript("import haxe.ds.*; switch Option.Some(321) { case Option.Some(v): v; case None: 0; }", 321); #end } From bca36378be17353f4f737c12ac051fd45efc710d Mon Sep 17 00:00:00 2001 From: hackx2 <80620538+hackx2@users.noreply.github.com> Date: Sat, 21 Nov 2026 17:38:10 +0000 Subject: [PATCH 3/3] fix assertScript --- TestHScript.hx | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/TestHScript.hx b/TestHScript.hx index 53cea30e..b5b07fc2 100644 --- a/TestHScript.hx +++ b/TestHScript.hx @@ -33,7 +33,13 @@ class TestHScript extends TestCase { for( v in Reflect.fields(vars) ) interp.variables.set(v,Reflect.field(vars,v)); var ret : Dynamic = interp.execute(program); - assertEquals(v, ret, pos); + if(Reflect.isEnumValue(v) && Reflect.isEnumValue(ret)) { + if(!Type.enumEq(v, ret)) { + assertEquals(Std.string(v), Std.string(ret), pos); + } else { + assertTrue(true, pos); + } + } else assertEquals(v, ret, pos); assertPos.lineNumber += 1; } @@ -338,6 +344,7 @@ class TestHScript extends TestCase { final script = "import haxe.ds.*; var a = new StringMap(); a.set('hiii', 0xF0); a.get('hiii')"; assertScript(script, 0xF0); assertScript(script+"; StringTools.trim(' hello ')","hello"); + assertScript("import haxe.ds.*; Option.Some(123)", haxe.ds.Option.Some(123)); #end }