capture variables for inline Custom Classes

This commit is contained in:
Jaime Humberto Macias Bustamante
2025-04-09 11:43:06 -07:00
parent d055203559
commit ca0599b180
3 changed files with 27 additions and 11 deletions
+2 -2
View File
@@ -879,7 +879,7 @@ class Interp {
usings: [for(u in this.usings) u.name],
pkg: null,
ogInterp: this,
usePublicVars: true
isInline: true
};
registerCustomClass(customClassDecl, null);
@@ -1182,7 +1182,7 @@ class Interp {
me.depth = depth;
return r;
};
var f = Reflect.makeVarArgs(f);
var f:Function = Reflect.makeVarArgs(f);
if (name != null) {
if (depth == 0) {
// global function
+18 -3
View File
@@ -40,6 +40,9 @@ class CustomClass implements IHScriptCustomAccessBehaviour {
public var __allowSetGet:Bool = false;
private var isInline:Bool = false;
private var ogVariables:Map<String, Dynamic>;
public function new(__class:CustomClassDecl, args:Array<Dynamic>, ?extendFieldDecl:Map<String, Dynamic>, ?ogInterp:Interp, ?callNew:Bool = true) {
this.__class = __class;
this.interp = new Interp(this);
@@ -50,7 +53,9 @@ class CustomClass implements IHScriptCustomAccessBehaviour {
interp.allowStaticVariables = ogInterp.allowStaticVariables;
interp.staticVariables = ogInterp.staticVariables;
// todo: make it so you can use variables from the same scope as where the class was defined
if(__class.usePublicVars) {
if(__class.isInline != null && __class.isInline) {
isInline = __class.isInline;
ogVariables = ogInterp.variables;
interp.allowPublicVariables = __class.staticInterp.allowPublicVariables;
interp.publicVariables = __class.staticInterp.publicVariables;
}
@@ -131,15 +136,25 @@ class CustomClass implements IHScriptCustomAccessBehaviour {
if (args == null)
args = [];
if(__class.superClassDecl is CustomClassDecl)
// TODO: disallow copy for super Custom Class
if(__class.superClassDecl is CustomClassDecl)
superClass = new CustomClass(__class.superClassDecl, args, _cachedFieldDecls, this.interp);
else {
if (_cachedSuperFields != null) {
Reflect.setField(__class.superClassDecl, "__cachedFields", _cachedSuperFields); // Static field
}
var disallowCopy = Type.getInstanceFields(__class.superClassDecl);
superClass = Type.createInstance(__class.superClassDecl, args);
superClass.__customClass = this;
superClass.__real_fields = Type.getInstanceFields(__class.superClassDecl);
superClass.__real_fields = disallowCopy;
if(isInline) {
for(s => v in ogVariables)
if(!disallowCopy.contains(s))
interp.variables.set(s, v);
}
}
/*
var extendString = new Printer().typeToString(__class.classDecl.extend);
+7 -6
View File
@@ -15,7 +15,7 @@ class CustomClassDecl implements IHScriptCustomAccessBehaviour {
public var usings:Array<String>;
public var pkg:Null<Array<String>> = null;
public var ogInterp:Null<Interp> = null;
public var usePublicVars:Null<Bool> = null;
public var isInline:Null<Bool> = null;
public var staticInterp:Interp = new Interp();
@@ -27,13 +27,13 @@ class CustomClassDecl implements IHScriptCustomAccessBehaviour {
public var __allowSetGet:Bool = true;
public function new(classDecl:Expr.ClassDecl, imports:Map<String, CustomClassImport>, usings:Array<String>, ?pkg:Array<String>, ?ogInterp:Interp, ?usePublicVars:Bool) {
public function new(classDecl:Expr.ClassDecl, imports:Map<String, CustomClassImport>, usings:Array<String>, ?pkg:Array<String>, ?ogInterp:Interp, ?isInline:Bool) {
this.classDecl = classDecl;
this.imports = imports;
this.usings = usings;
this.pkg = pkg;
this.ogInterp = ogInterp;
this.usePublicVars = usePublicVars;
this.isInline = isInline;
if(ogInterp != null) {
staticInterp.importFailedCallback = ogInterp.importFailedCallback;
@@ -41,13 +41,12 @@ class CustomClassDecl implements IHScriptCustomAccessBehaviour {
staticInterp.allowStaticVariables = ogInterp.allowStaticVariables;
staticInterp.staticVariables = ogInterp.staticVariables;
if(usePublicVars != null && usePublicVars) {
if(isInline != null && isInline) {
// uses public variables from the same scope as where the class was defined
staticInterp.variables = ogInterp.variables;
staticInterp.allowPublicVariables = ogInterp.allowPublicVariables;
staticInterp.publicVariables = ogInterp.publicVariables;
}
// TODO: use normal variables, not just public
}
cacheImports();
@@ -64,6 +63,8 @@ class CustomClassDecl implements IHScriptCustomAccessBehaviour {
var importedClass = imp.fullPath;
var importAlias = imp.as;
if(this.staticInterp.variables.exists(imp.name)) continue; // class is already imported
if (Interp.customClassExist(importedClass) && this.staticInterp.importFailedCallback != null) {
this.staticInterp.importFailedCallback(importedClass.split("."), importAlias);
continue;