From 5232f069fbf136bcf9253cb7d271d5461a7ff162 Mon Sep 17 00:00:00 2001 From: Jaime Humberto Macias Bustamante Date: Thu, 17 Jul 2025 14:46:12 -0700 Subject: [PATCH] final classes + improved custom class aliases --- hscript/CustomClassHandler.hx | 12 +++++++++--- hscript/Interp.hx | 16 +++++++++++----- hscript/Printer.hx | 5 ++++- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/hscript/CustomClassHandler.hx b/hscript/CustomClassHandler.hx index 65afe6e..9566181 100644 --- a/hscript/CustomClassHandler.hx +++ b/hscript/CustomClassHandler.hx @@ -10,6 +10,7 @@ class CustomClassHandler implements IHScriptCustomConstructor implements IHScrip public var fields:Array; public var extend:Null; public var interfaces:Array; + public final isFinal:Bool; public var cl:Dynamic; @@ -18,16 +19,21 @@ class CustomClassHandler implements IHScriptCustomConstructor implements IHScrip public var __allowSetGet:Bool = true; - public function new(ogInterp:Interp, name:String, fields:Array, ?extend:String, ?interfaces:Array) { + public function new(ogInterp:Interp, name:String, fields:Array, ?extend:String, ?interfaces:Array, ?isFinal:Bool) { this.ogInterp = ogInterp; this.name = name; this.fields = fields; this.extend = extend; this.interfaces = interfaces; + this.isFinal = isFinal != null ? isFinal : false; if(extend != null) { - if(ogInterp.customClasses.exists(extend)) - this.cl = ogInterp.customClasses.get(extend); + if(ogInterp.customClasses.exists(extend)) { + var customCls:CustomClassHandler = ogInterp.customClasses.get(extend); + if(customCls.isFinal) + ogInterp.error(ECustom('Cannot extend a final class')); + this.cl = customCls; + } else this.cl = Type.resolveClass('${extend}_HSX'); diff --git a/hscript/Interp.hx b/hscript/Interp.hx index 71af3ef..a931466 100644 --- a/hscript/Interp.hx +++ b/hscript/Interp.hx @@ -702,9 +702,10 @@ class Interp { var e = e.e; #end switch (e) { - case EClass(name, fields, extend, interfaces): - var hasAlias:Bool = (setAlias != null && beforeAlias == name); - var toSetName:String = hasAlias ? setAlias : name; + case EClass(name, fields, extend, interfaces, isFinal): + var oldName:String = name; + var hasAlias:Bool = (setAlias != null && beforeAlias == oldName); + var toSetName:String = hasAlias ? setAlias : oldName; if (customClasses.exists(toSetName)) error(EAlreadyExistingClass(toSetName)); @@ -715,8 +716,13 @@ class Interp { final variable:Class = variables.exists(thing) ? cast variables.get(thing) : null; return variable == null ? thing : Type.getClassName(variable); } - customClasses.set(toSetName, new CustomClassHandler(this, name, fields, importVar(extend), [for (i in interfaces) importVar(i)])); - if(hasAlias) setAlias = null; + var cls:CustomClassHandler = new CustomClassHandler(this, oldName, fields, importVar(extend), [for (i in interfaces) importVar(i)], isFinal); + customClasses.set(toSetName, cls); + if(hasAlias) { + customClasses.set(oldName, cls); // Allow usage in the same module + beforeAlias = null; + setAlias = null; + } case EImport(c, n, isUsing): if (!importEnabled) return null; diff --git a/hscript/Printer.hx b/hscript/Printer.hx index 1189dc2..58bee51 100644 --- a/hscript/Printer.hx +++ b/hscript/Printer.hx @@ -120,7 +120,10 @@ class Printer { add('${u ? 'using' : 'import'} $c'); if(n != null) add(' as $n'); - case EClass(name, fields, extend, interfaces): + case EClass(name, fields, extend, interfaces, fnal): + var isFinal = fnal != null && fnal; + if(isFinal) + add('final '); add('class $name'); if (extend != null) add(' extends $extend');