From 6441acc93d2819034d57d02a3bc76b0a1a03b31e Mon Sep 17 00:00:00 2001 From: Jaime Humberto Macias Bustamante Date: Tue, 17 Jun 2025 15:27:21 -0700 Subject: [PATCH] fixes :3 --- hscript/CustomClass.hx | 32 ++++++++++++++++++++++++++------ hscript/CustomClassHandler.hx | 6 ++---- hscript/Interp.hx | 14 -------------- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/hscript/CustomClass.hx b/hscript/CustomClass.hx index c02a4f9..8e06a35 100644 --- a/hscript/CustomClass.hx +++ b/hscript/CustomClass.hx @@ -33,14 +33,14 @@ class CustomClass implements IHScriptCustomClassBehaviour { var __constructor:Function; var fields:Array; - public function new(__class:CustomClassHandler, args:Array) { + public function new(__class:CustomClassHandler, ?args:Array) { this.__class = __class; __interp = new Interp(); __interp.errorHandler = __class.staticInterp.errorHandler; __interp.importFailedCallback = __class.staticInterp.importFailedCallback; - __interp.variables = __class.staticInterp.variables; // This will access to static fields + // __interp.variables = __class.staticInterp.variables; __interp.publicVariables = __class.ogInterp.publicVariables; __interp.staticVariables = __class.ogInterp.staticVariables; @@ -56,6 +56,11 @@ class CustomClass implements IHScriptCustomClassBehaviour { @:privateAccess __interp.exprReturn(f); } + for (f => v in __class.staticInterp.variables) { + if (!__interp.variables.exists(f)) + __interp.variables.set(f, v); + } + __interp.scriptObject = this; if (hasField('new')) { @@ -105,10 +110,13 @@ class CustomClass implements IHScriptCustomClassBehaviour { } function hasField(name:String) { - // return __interp.variables.exists(name); return __class__fields.contains(name); } + function hasStaticField(name:String):Bool { + return __class.hasField(name); + } + function getField(name:String, allowProperty:Bool = true):Dynamic { var f = __interp.variables.get(name); if (allowProperty && f is Property) { @@ -146,12 +154,19 @@ class CustomClass implements IHScriptCustomClassBehaviour { public function hget(name:String):Dynamic { switch (name) { - case 'superClass': return __superClass; - case 'superConstructor': return __constructor; + case 'superClass': + return __superClass; + case 'superConstructor': + return __constructor; default: - if (hasField(name)) + if (hasField(name)) return getField(name); + if (hasStaticField(name)) { + __interp.error(ECustom('The field ${name} should be accessed in a static way.')); + return null; + } + if (__superClass != null) { if (superHasField(name)) { __superClass.__allowSetGet = this.__allowSetGet; @@ -174,6 +189,11 @@ class CustomClass implements IHScriptCustomClassBehaviour { return setField(name, val); } + if (hasStaticField(name)) { + __interp.error(ECustom('The field ${name} should be accessed in a static way.')); + return null; + } + if (__superClass != null) { if (superHasField(name)) { __superClass.__allowSetGet = this.__allowSetGet; diff --git a/hscript/CustomClassHandler.hx b/hscript/CustomClassHandler.hx index ff54c73..a4abd74 100644 --- a/hscript/CustomClassHandler.hx +++ b/hscript/CustomClassHandler.hx @@ -23,7 +23,7 @@ class CustomClassHandler implements IHScriptCustomConstructor implements IHScrip public var cl:Dynamic; private var staticInterp:Interp; - private var staticFields:Array; + private var staticFields:Array = []; public function new(ogInterp:Interp, name:String, fields:Array, ?extend:String, ?interfaces:Array) { this.ogInterp = ogInterp; @@ -200,9 +200,7 @@ class CustomClassHandler implements IHScriptCustomConstructor implements IHScrip */ // I can't believe all these lines of code above got reduced to this sentence. - var _class = new CustomClass(this, args); - - return _class; + return new CustomClass(this, args); } @:allow(hscript.Interp) diff --git a/hscript/Interp.hx b/hscript/Interp.hx index d8667d3..fe59f28 100644 --- a/hscript/Interp.hx +++ b/hscript/Interp.hx @@ -1280,13 +1280,6 @@ class Interp { if (o == null) error(EInvalidAccess(f)); - if(o is CustomClass) { - if(cast(o, CustomClass).__class.hasField(f)) { - var clsName = cast(o, CustomClass).className; - error(ECustom('The field ${clsName}.${f} should be accessed in a static way.')); - } - } - var cls = Type.getClass(o); if (useRedirects && { var cl:Null = getClassType(o, cls); @@ -1327,13 +1320,6 @@ class Interp { if (o == null) error(EInvalidAccess(f)); - if(o is CustomClass) { - if(cast(o, CustomClass).__class.hasField(f)) { - var clsName = cast(o, CustomClass).className; - error(ECustom('The field ${clsName}.${f} should be accessed in a static way.')); - } - } - if (useRedirects && { var cl:Null = getClassType(o); cl != null && setRedirects.exists(cl) && (_setRedirect = setRedirects[cl]) != null;