Skip to content

Commit 8cee453

Browse files
committed
ability to import scripted classes
1 parent 958b916 commit 8cee453

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

polymod/Polymod.hx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,8 @@ class Polymod
691691
polymod.hscript._internal.PolymodScriptClass.registerScriptClassByPath(path);
692692
}
693693
}
694+
695+
polymod.hscript._internal.PolymodInterpEx.validateImports();
694696
}
695697
#else
696698
Polymod.warning(SCRIPT_HSCRIPT_NOT_INSTALLED, "Cannot register script classes, HScript is not available.");
@@ -728,6 +730,9 @@ class Polymod
728730
if (future != null) futures.push(future);
729731
}
730732
}
733+
734+
polymod.hscript._internal.PolymodInterpEx.validateImports();
735+
731736
return futures;
732737
#else
733738
Polymod.warning(SCRIPT_HSCRIPT_NOT_INSTALLED, "Cannot register script classes, HScript is not available.");

polymod/hscript/_internal/PolymodClassDeclEx.hx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ typedef PolymodClassDeclEx =
1414
* Save performance and improve sandboxing by resolving imports at interpretation time.
1515
*/
1616
@:optional var imports:Map<String, PolymodClassImport>;
17+
@:optional var importsToValidate:Map<String, PolymodClassImport>;
1718
@:optional var pkg:Array<String>;
1819

1920
@:optional var staticFields:Array<FieldDecl>;

polymod/hscript/_internal/PolymodInterpEx.hx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ class PolymodInterpEx extends Interp
6262
var clsRef = PolymodStaticClassReference.tryBuild(cl);
6363
if (clsRef != null) return clsRef.instantiate(args);
6464

65+
if (getClassDecl().imports != null && getClassDecl().imports.exists(cl))
66+
{
67+
var clsRef = PolymodStaticClassReference.tryBuild(getClassDecl().imports.get(cl).fullPath);
68+
if (clsRef != null) return clsRef.instantiate(args);
69+
}
70+
6571
if (_proxy != null)
6672
{
6773
@:privateAccess
@@ -194,6 +200,26 @@ class PolymodInterpEx extends Interp
194200
return _scriptClassDescriptors.get(name);
195201
}
196202

203+
public static function validateImports()
204+
{
205+
for (cls in _scriptClassDescriptors)
206+
{
207+
var clsPath = cls.pkg != null ? (cls.pkg.join(".") + ".") : "";
208+
clsPath += cls.name;
209+
210+
for (key => imp in cls.importsToValidate)
211+
{
212+
if (_scriptClassDescriptors.exists(imp.fullPath))
213+
{
214+
cls.imports.set(key, imp);
215+
continue;
216+
}
217+
218+
Polymod.error(SCRIPT_CLASS_MODULE_NOT_FOUND, 'Could not import class ${imp.fullPath}', clsPath);
219+
}
220+
}
221+
}
222+
197223
override function setVar(id:String, v:Dynamic)
198224
{
199225
if (_proxy != null && _proxy.superClass != null)
@@ -1255,6 +1281,7 @@ class PolymodInterpEx extends Interp
12551281
{
12561282
var pkg:Array<String> = null;
12571283
var imports:Map<String, PolymodClassImport> = [];
1284+
var importsToValidate:Map<String, PolymodClassImport> = [];
12581285

12591286
for (importPath in PolymodScriptClass.defaultImports.keys())
12601287
{
@@ -1316,7 +1343,8 @@ class PolymodInterpEx extends Interp
13161343

13171344
// If the class is still not found, skip this import entirely.
13181345
if (resultCls == null && resultEnm == null) {
1319-
Polymod.error(SCRIPT_CLASS_MODULE_NOT_FOUND, 'Could not import class ${importedClass.fullPath}', origin);
1346+
//Polymod.error(SCRIPT_CLASS_MODULE_NOT_FOUND, 'Could not import class ${importedClass.fullPath}', origin);
1347+
importsToValidate.set(importedClass.name, importedClass);
13201348
continue;
13211349
} else if (resultCls != null) {
13221350
importedClass.cls = resultCls;
@@ -1372,6 +1400,7 @@ class PolymodInterpEx extends Interp
13721400

13731401
var classDecl:PolymodClassDeclEx = {
13741402
imports: imports,
1403+
importsToValidate: importsToValidate,
13751404
pkg: pkg,
13761405
name: c.name,
13771406
params: c.params,

0 commit comments

Comments
 (0)