Skip to content
Open
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
119 changes: 89 additions & 30 deletions polymod/hscript/_internal/PolymodInterpEx.hx
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ class PolymodInterpEx extends Interp
// Clear the script class descriptors.
_scriptClassDescriptors.clear();

// Also clear the imports from the import.hx files.
_scriptClassImports.clear();
_scriptClassUsings.clear();

// Also destroy local variable scope.
this.resetVariables();
}
Expand Down Expand Up @@ -296,6 +300,22 @@ class PolymodInterpEx extends Interp
this.resetVariables();
}

private static var _scriptClassImports:Map<String, Array<PolymodClassImport>> = new Map<String, Array<PolymodClassImport>>();
private static var _scriptClassUsings:Map<String, Array<PolymodClassImport>> = new Map<String, Array<PolymodClassImport>>();

private static function registerImportForPackage(pkg:Null<Array<String>>, imp:PolymodClassImport, isUsing:Bool = false)
{
var impFilePkg:String = pkg?.join(".") ?? "";
var map:Map<String, Array<PolymodClassImport>> = isUsing ? _scriptClassUsings : _scriptClassImports;

if (!map.exists(impFilePkg))
{
map.set(impFilePkg, []);
}

map.get(impFilePkg).push(imp);
}

public static function validateImports():Void
{
for (cls in _scriptClassDescriptors)
Expand Down Expand Up @@ -328,6 +348,26 @@ class PolymodInterpEx extends Interp
}
}

// Import classes from the import.hx files.
var pkg:String = cls.pkg?.join(".") ?? "";

for (key => imps in _scriptClassImports)
{
if (!pkg.startsWith(key) && key.length != 0) continue;

for (imp in imps)
cls.imports.set(imp.name, imp);
}

for (key => imps in _scriptClassUsings)
{
if (!pkg.startsWith(key) && key.length != 0) continue;

for (imp in imps)
cls.usings.set(imp.name, imp);
}

// Add the scripted imports.
for (key => imp in cls.importsToValidate)
{
if (_scriptClassDescriptors.exists(imp.fullPath))
Expand Down Expand Up @@ -660,9 +700,8 @@ class PolymodInterpEx extends Interp
// Using a clone to prevent locals getting wiped out.
var clone = this.clone();

// This CREATES a new function in memory, that we call later.
var newFun:Dynamic = function(args:Array<Dynamic>)
{
// This CREATES a new function in memory, that we call later.
var newFun:Dynamic = function(args:Array<Dynamic>) {
if (args == null) args = [];

validateArgumentCount(params, args, name);
Expand Down Expand Up @@ -1723,13 +1762,13 @@ class PolymodInterpEx extends Interp
}

/**
* Initializes function arguments within the interpreter scope.
* Initializes function arguments within the interpreter scope.
*
* @param fn The function declaration to extract arguments from.
* @param args The arguments to pass to the function.
* @param name The function's name
* @return The Map containing the variable values before they are shadowed in the local scope.
*/
* @param fn The function declaration to extract arguments from.
* @param args The arguments to pass to the function.
* @param name The function's name
* @return The Map containing the variable values before they are shadowed in the local scope.
*/
public function setFunctionValues(fn:Null<FunctionDecl>, args:Array<Dynamic> = null, name:String = "Unknown"):Map<String, Dynamic>
{
var previousValues:Map<String, Dynamic> = [];
Expand Down Expand Up @@ -1982,23 +2021,29 @@ class PolymodInterpEx extends Interp

public function registerModules(module:Array<ModuleDecl>, ?origin:String = "hscript")
{
var isImportFile:Bool = (new haxe.io.Path(origin).file == "import");

var pkg:Array<String> = null;
var imports:Map<String, ClassImport> = [];
var importsToValidate:Map<String, ClassImport> = [];
var usings:Map<String, ClassImport> = [];

for (importPath in PolymodScriptClass.defaultImports.keys())
// Don't add the default imports to import.hx since they're added to other script classes anyways.
if (!isImportFile)
{
var splitPath = importPath.split(".");
var clsName = splitPath[splitPath.length - 1];
for (importPath in PolymodScriptClass.defaultImports.keys())
{
var splitPath = importPath.split(".");
var clsName = splitPath[splitPath.length - 1];

imports.set(clsName,
{
name: clsName,
pkg: splitPath.slice(0, splitPath.length - 1),
fullPath: importPath,
cls: PolymodScriptClass.defaultImports.get(importPath),
});
imports.set(clsName,
{
name: clsName,
pkg: splitPath.slice(0, splitPath.length - 1),
fullPath: importPath,
cls: PolymodScriptClass.defaultImports.get(importPath),
});
}
}

for (decl in module)
Expand Down Expand Up @@ -2062,6 +2107,12 @@ class PolymodInterpEx extends Interp
// If the class is still not found, skip this import entirely.
if (resultCls == null && resultEnm == null)
{
if (isImportFile)
{
registerImportForPackage(pkg, importedClass);
continue;
}

// Polymod.error(SCRIPT_CLASS_MODULE_NOT_FOUND, 'Could not import class ${importedClass.fullPath}', origin);
// this could be a scripted class or enum that hasn't been registered yet
importsToValidate.set(importedClass.name, importedClass);
Expand All @@ -2077,6 +2128,12 @@ class PolymodInterpEx extends Interp
}
}

if (isImportFile)
{
registerImportForPackage(pkg, importedClass);
continue;
}

Polymod.debug('Imported class ${importedClass.name} from ${importedClass.fullPath}');
imports.set(importedClass.name, importedClass);
case DUsing(path):
Expand Down Expand Up @@ -2127,22 +2184,22 @@ class PolymodInterpEx extends Interp
var resultCls:Class<Dynamic> = Type.resolveClass(importedClass.fullPath);

// If the class is still not found, skip this import entirely.
if (resultCls == null)
{
// Polymod.error(SCRIPT_CLASS_MODULE_NOT_FOUND, 'Could not import class ${importedClass.fullPath}', origin);
// this could be a scripted class that hasn't been registered yet
importsToValidate.set(importedClass.name, importedClass);
continue;
}
else
{
importedClass.cls = resultCls;
}
if (resultCls == null) continue;

importedClass.cls = resultCls;
}

if (isImportFile)
{
registerImportForPackage(pkg, importedClass, true);
continue;
}

Polymod.debug('Using class ${importedClass.name} from ${importedClass.fullPath}');
usings.set(importedClass.name, importedClass);
case DClass(c):
if (isImportFile) continue;

var instanceFields = [];
var staticFields = [];
for (f in c.fields)
Expand Down Expand Up @@ -2175,6 +2232,8 @@ class PolymodInterpEx extends Interp
};
registerScriptClass(classDecl);
case DEnum(e):
if (isImportFile) continue;

if (pkg != null)
{
imports.set(e.name,
Expand Down