From 251306a3d3ae2040cf72835d652f63f002b74e2c Mon Sep 17 00:00:00 2001 From: Jaime Humberto Macias Bustamante Date: Tue, 17 Jun 2025 23:50:00 -0700 Subject: [PATCH] A bit if documentation + cleanup --- hscript/CustomClass.hx | 21 +++++++-------------- hscript/CustomClassHandler.hx | 15 ++------------- hscript/Property.hx | 21 +++++++++++++++++++++ hscript/macros/ClassExtendMacro.hx | 1 + 4 files changed, 31 insertions(+), 27 deletions(-) diff --git a/hscript/CustomClass.hx b/hscript/CustomClass.hx index d549ba4..e21ce15 100644 --- a/hscript/CustomClass.hx +++ b/hscript/CustomClass.hx @@ -31,7 +31,6 @@ class CustomClass implements IHScriptCustomClassBehaviour { var __class:CustomClassHandler; var __superClass:IHScriptCustomClassBehaviour; var __constructor:Function; - var fields:Array; public function new(__class:CustomClassHandler, ?args:Array) { this.__class = __class; @@ -44,14 +43,11 @@ class CustomClass implements IHScriptCustomClassBehaviour { __interp.publicVariables = __class.ogInterp.publicVariables; __interp.staticVariables = __class.ogInterp.staticVariables; - this.fields = __class.fields; - for (f in fields) { + for (f in __class.fields) { switch (Tools.expr(f)) { - case EVar(n): - __class__fields.push(n); - case EFunction(_, _, n): - __class__fields.push(n); - default: + case EVar(n): __class__fields.push(n); + case EFunction(_, _, n): __class__fields.push(n); + default: continue; } @:privateAccess __interp.exprReturn(f); } @@ -154,10 +150,8 @@ 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)) return getField(name); @@ -185,9 +179,8 @@ class CustomClass implements IHScriptCustomClassBehaviour { } public function hset(name:String, val:Dynamic):Dynamic { - if (hasField(name)) { + if (hasField(name)) return setField(name, val); - } if (hasStaticField(name)) { __interp.error(ECustom('The field ${name} should be accessed in a static way.')); diff --git a/hscript/CustomClassHandler.hx b/hscript/CustomClassHandler.hx index a4abd74..aa5dd00 100644 --- a/hscript/CustomClassHandler.hx +++ b/hscript/CustomClassHandler.hx @@ -1,19 +1,10 @@ package hscript; -import hscript.Interp.DeclaredVar; -import hscript.utils.UnsafeReflect; -using StringTools; - -// BIG TODO: revamp this /** * Provides handlers for static custom class fields and instantiation. */ @:access(hscript.Property) class CustomClassHandler implements IHScriptCustomConstructor implements IHScriptCustomAccessBehaviour{ - public static var staticHandler = new StaticHandler(); - - public var __allowSetGet:Bool = true; - public var ogInterp:Interp; public var name:String; public var fields:Array; @@ -25,6 +16,8 @@ class CustomClassHandler implements IHScriptCustomConstructor implements IHScrip private var staticInterp:Interp; private var staticFields:Array = []; + public var __allowSetGet:Bool = true; + public function new(ogInterp:Interp, name:String, fields:Array, ?extend:String, ?interfaces:Array) { this.ogInterp = ogInterp; this.name = name; @@ -346,7 +339,3 @@ class TemplateClass implements IHScriptCustomBehaviour implements IHScriptCustom return v; } } - -final class StaticHandler { - public function new() {} -} \ No newline at end of file diff --git a/hscript/Property.hx b/hscript/Property.hx index 074ba10..c167a89 100644 --- a/hscript/Property.hx +++ b/hscript/Property.hx @@ -4,6 +4,27 @@ import hscript.utils.UnsafeReflect; import hscript.Interp; import hscript.Expr.FieldPropertyAccess; +/** + * Special variable that handles 'getter/setter' function calls + * depending of the read/write access combination. + * + * Example: + * ```haxe + * public var myvar(get, set):Int; + * var _myvar:Int = 10; + * + * function get_myvar():Int { + * return _myvar; + * } + * + * function set_myvar(val:Int):Int { + * if(val > 10) return _myvar = val; + * return val; + * } + * ``` + * + * @see https://haxe.org/manual/class-field-property.html + */ @:access(hscript.Interp) @:structInit class Property { diff --git a/hscript/macros/ClassExtendMacro.hx b/hscript/macros/ClassExtendMacro.hx index c5d2088..54b607a 100644 --- a/hscript/macros/ClassExtendMacro.hx +++ b/hscript/macros/ClassExtendMacro.hx @@ -14,6 +14,7 @@ import Sys; using StringTools; +// BIG TODO: make typed classes scriptable class ClassExtendMacro { public static inline final FUNC_PREFIX = "_HX_SUPER__"; public static inline final CLASS_SUFFIX = "_HSX";