Skip to content

[FEATURE] Import Scripted Classes #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: experiment/static-fields
Choose a base branch
from
Open
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 polymod/Polymod.hx
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,8 @@ class Polymod
polymod.hscript._internal.PolymodScriptClass.registerScriptClassByPath(path);
}
}

polymod.hscript._internal.PolymodInterpEx.validateImports();
}
#else
Polymod.warning(SCRIPT_HSCRIPT_NOT_INSTALLED, "Cannot register script classes, HScript is not available.");
Expand Down Expand Up @@ -728,6 +730,9 @@ class Polymod
if (future != null) futures.push(future);
}
}

polymod.hscript._internal.PolymodInterpEx.validateImports();

return futures;
#else
Polymod.warning(SCRIPT_HSCRIPT_NOT_INSTALLED, "Cannot register script classes, HScript is not available.");
Expand Down
1 change: 1 addition & 0 deletions polymod/hscript/_internal/PolymodClassDeclEx.hx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ typedef PolymodClassDeclEx =
* Save performance and improve sandboxing by resolving imports at interpretation time.
*/
@:optional var imports:Map<String, PolymodClassImport>;
@:optional var importsToValidate:Map<String, PolymodClassImport>;
@:optional var pkg:Array<String>;

@:optional var staticFields:Array<FieldDecl>;
Expand Down
31 changes: 30 additions & 1 deletion polymod/hscript/_internal/PolymodInterpEx.hx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ class PolymodInterpEx extends Interp
var clsRef = PolymodStaticClassReference.tryBuild(cl);
if (clsRef != null) return clsRef.instantiate(args);

if (getClassDecl().imports != null && getClassDecl().imports.exists(cl))
{
var clsRef = PolymodStaticClassReference.tryBuild(getClassDecl().imports.get(cl).fullPath);
if (clsRef != null) return clsRef.instantiate(args);
}

if (_proxy != null)
{
@:privateAccess
Expand Down Expand Up @@ -194,6 +200,26 @@ class PolymodInterpEx extends Interp
return _scriptClassDescriptors.get(name);
}

public static function validateImports()
{
for (cls in _scriptClassDescriptors)
{
var clsPath = cls.pkg != null ? (cls.pkg.join(".") + ".") : "";
clsPath += cls.name;

for (key => imp in cls.importsToValidate)
{
if (_scriptClassDescriptors.exists(imp.fullPath))
{
cls.imports.set(key, imp);
continue;
}

Polymod.error(SCRIPT_CLASS_MODULE_NOT_FOUND, 'Could not import class ${imp.fullPath}', clsPath);
}
}
}

override function setVar(id:String, v:Dynamic)
{
if (_proxy != null && _proxy.superClass != null)
Expand Down Expand Up @@ -1255,6 +1281,7 @@ class PolymodInterpEx extends Interp
{
var pkg:Array<String> = null;
var imports:Map<String, PolymodClassImport> = [];
var importsToValidate:Map<String, PolymodClassImport> = [];

for (importPath in PolymodScriptClass.defaultImports.keys())
{
Expand Down Expand Up @@ -1316,7 +1343,8 @@ class PolymodInterpEx extends Interp

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

var classDecl:PolymodClassDeclEx = {
imports: imports,
importsToValidate: importsToValidate,
pkg: pkg,
name: c.name,
params: c.params,
Expand Down