From ca0599b180e57a298c8e001eef3ee11bb6ab913c Mon Sep 17 00:00:00 2001 From: Jaime Humberto Macias Bustamante Date: Wed, 9 Apr 2025 11:43:06 -0700 Subject: [PATCH] capture variables for inline Custom Classes --- hscript/Interp.hx | 4 ++-- hscript/customclass/CustomClass.hx | 21 ++++++++++++++++++--- hscript/customclass/CustomClassDecl.hx | 13 +++++++------ 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/hscript/Interp.hx b/hscript/Interp.hx index 5805c3b..5fc3206 100644 --- a/hscript/Interp.hx +++ b/hscript/Interp.hx @@ -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 diff --git a/hscript/customclass/CustomClass.hx b/hscript/customclass/CustomClass.hx index cf8064d..9a87122 100644 --- a/hscript/customclass/CustomClass.hx +++ b/hscript/customclass/CustomClass.hx @@ -40,6 +40,9 @@ class CustomClass implements IHScriptCustomAccessBehaviour { public var __allowSetGet:Bool = false; + private var isInline:Bool = false; + private var ogVariables:Map; + public function new(__class:CustomClassDecl, args:Array, ?extendFieldDecl:Map, ?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); diff --git a/hscript/customclass/CustomClassDecl.hx b/hscript/customclass/CustomClassDecl.hx index 7ef35d9..cfd1d51 100644 --- a/hscript/customclass/CustomClassDecl.hx +++ b/hscript/customclass/CustomClassDecl.hx @@ -15,7 +15,7 @@ class CustomClassDecl implements IHScriptCustomAccessBehaviour { public var usings:Array; public var pkg:Null> = null; public var ogInterp:Null = null; - public var usePublicVars:Null = null; + public var isInline:Null = 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, usings:Array, ?pkg:Array, ?ogInterp:Interp, ?usePublicVars:Bool) { + public function new(classDecl:Expr.ClassDecl, imports:Map, usings:Array, ?pkg:Array, ?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;