From a15c0267d785e612e021ac8429a6770a04589469 Mon Sep 17 00:00:00 2001 From: hackx2 <80620538+hackx2@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:18:05 +0000 Subject: [PATCH] import static functions from scripted classes --- TestHScript.hx | 5 +++++ hscript/Interp.hx | 24 +++++++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/TestHScript.hx b/TestHScript.hx index fd98d519..106d1e12 100644 --- a/TestHScript.hx +++ b/TestHScript.hx @@ -327,6 +327,11 @@ class TestHScript extends TestCase { assertScript('import Math as HaxeMath; HaxeMath', Math); assertScript('using StringTools; " hscript ".trim()', "hscript"); assertScript('using Lambda; [1, 2, 3, 4].find((i) -> return i == 2)', 2); + + // compiled class field imports + 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); } function testClass() { diff --git a/hscript/Interp.hx b/hscript/Interp.hx index 7f9152f2..efb2b68b 100644 --- a/hscript/Interp.hx +++ b/hscript/Interp.hx @@ -864,6 +864,7 @@ class Interp { /** Handles `import` / `using` statements (extracted from the expr() switch). **/ @:noInline + @:access(hscript.CustomClassHandler) function exprImport(clsName:String, aliasAs:Null, isUsing:Bool):Void { if(!importEnabled) return; @@ -920,13 +921,30 @@ class Interp { } if(cl == null && en == null) { - if(allowStaticImports) { //allows for static imports like "haxe.io.Path.normalize" + if(allowStaticImports && oldSplitName.length > 1) { //allows for static imports like "haxe.io.Path.normalize" for both compiled and scripted classes var clPth:Array = oldSplitName.copy(); var funcName:String = clPth.pop(); - var statField:Dynamic = Reflect.getProperty(Type.resolveClass(StringTools.trim(clPth.join("."))), funcName); + var statField:Dynamic = null; + + // scripted class + final handler = customClasses.get(clPth[clPth.length-1]); + if(handler != null) { + statField = handler.getField(funcName); + } else { + final parent = importResolve(clPth.join(".")); + if (parent!=null) { + statField = Reflect.getProperty(parent, funcName); + } + } if(statField != null) { - variables.set((toSetName != null && toSetName.length > 0 ? toSetName : funcName), statField); + final varName = (toSetName != null && toSetName.length > 0 ? toSetName : funcName); + variables.set(varName, statField); + + // clear cache + varLocationCache.remove(varName); + __typeResolveCache.remove(varName); + return; } }