From d05520355914c180b8afab9f43c0af092f19d5ec Mon Sep 17 00:00:00 2001 From: Jaime Humberto Macias Bustamante Date: Tue, 8 Apr 2025 11:48:31 -0700 Subject: [PATCH] Public vars for Custom Classes in scripts Uses public variables from the same scope as where the class was defined. --- hscript/Expr.hx | 2 ++ hscript/Interp.hx | 6 ++++-- hscript/customclass/CustomClass.hx | 6 +++++- hscript/customclass/CustomClassDecl.hx | 16 +++++++++++++--- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/hscript/Expr.hx b/hscript/Expr.hx index e2a7430..dfb93a7 100644 --- a/hscript/Expr.hx +++ b/hscript/Expr.hx @@ -222,6 +222,8 @@ typedef VarDecl = { var type : Null; } +typedef TypeParamDecl = {}; + enum EnumType { ESimple(name: String); EConstructor(name: String, args: Array); diff --git a/hscript/Interp.hx b/hscript/Interp.hx index ed87217..5805c3b 100644 --- a/hscript/Interp.hx +++ b/hscript/Interp.hx @@ -804,7 +804,7 @@ class Interp { if (customClasses.exists(name)) error(EAlreadyExistingClass(name)); - trace("WARNING: Old Custom Class system marked for removal.\n'newCustomClass = true' to use the new system (EXPERIMENTAL)"); + trace("WARNING: Old Custom Class system marked for removal.\n'newCustomClass = true' to use the new system (STILL IN DEVELOPMENT)"); customClasses.set(name, new CustomClassHandler(this, name, fields, importVar(extend), [for (i in interfaces) importVar(i)])); return null; } @@ -877,7 +877,9 @@ class Interp { classDecl: classDecl, imports: localImports, usings: [for(u in this.usings) u.name], - pkg: null + pkg: null, + ogInterp: this, + usePublicVars: true }; registerCustomClass(customClassDecl, null); diff --git a/hscript/customclass/CustomClass.hx b/hscript/customclass/CustomClass.hx index d9039df..cf8064d 100644 --- a/hscript/customclass/CustomClass.hx +++ b/hscript/customclass/CustomClass.hx @@ -44,12 +44,16 @@ class CustomClass implements IHScriptCustomAccessBehaviour { this.__class = __class; this.interp = new Interp(this); - if (ogInterp != null && ogInterp.importFailedCallback != null && ogInterp.errorHandler != null) { + if (ogInterp != null) { interp.importFailedCallback = ogInterp.importFailedCallback; interp.errorHandler = ogInterp.errorHandler; 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) { + interp.allowPublicVariables = __class.staticInterp.allowPublicVariables; + interp.publicVariables = __class.staticInterp.publicVariables; + } } buildImports(); diff --git a/hscript/customclass/CustomClassDecl.hx b/hscript/customclass/CustomClassDecl.hx index 22c43b6..7ef35d9 100644 --- a/hscript/customclass/CustomClassDecl.hx +++ b/hscript/customclass/CustomClassDecl.hx @@ -14,7 +14,8 @@ class CustomClassDecl implements IHScriptCustomAccessBehaviour { public var imports:Map; public var usings:Array; public var pkg:Null> = null; - public var ogInterp:Interp = null; + public var ogInterp:Null = null; + public var usePublicVars:Null = null; public var staticInterp:Interp = new Interp(); @@ -26,18 +27,27 @@ class CustomClassDecl implements IHScriptCustomAccessBehaviour { public var __allowSetGet:Bool = true; - public function new(classDecl:Expr.ClassDecl, imports:Map, usings:Array, pkg:Null>, ogInterp:Interp = null) { + public function new(classDecl:Expr.ClassDecl, imports:Map, usings:Array, ?pkg:Array, ?ogInterp:Interp, ?usePublicVars:Bool) { this.classDecl = classDecl; this.imports = imports; this.usings = usings; this.pkg = pkg; this.ogInterp = ogInterp; + this.usePublicVars = usePublicVars; - if(ogInterp != null && ogInterp.importFailedCallback != null && ogInterp.errorHandler != null) { + if(ogInterp != null) { staticInterp.importFailedCallback = ogInterp.importFailedCallback; staticInterp.errorHandler = ogInterp.errorHandler; staticInterp.allowStaticVariables = ogInterp.allowStaticVariables; staticInterp.staticVariables = ogInterp.staticVariables; + + if(usePublicVars != null && usePublicVars) { + // uses public variables from the same scope as where the class was defined + staticInterp.allowPublicVariables = ogInterp.allowPublicVariables; + staticInterp.publicVariables = ogInterp.publicVariables; + } + + // TODO: use normal variables, not just public } cacheImports();