Skip to content
Merged
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
5 changes: 5 additions & 0 deletions TestHScript.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
24 changes: 21 additions & 3 deletions hscript/Interp.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>, isUsing:Bool):Void {
if(!importEnabled) return;

Expand Down Expand Up @@ -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<String> = 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;
}
}
Expand Down
Loading