Classes work more reliably + Optimizations

This commit is contained in:
Ne_Eo
2024-08-20 04:50:20 +02:00
parent f18c7a2210
commit 13bdf30055
7 changed files with 650 additions and 185 deletions
+71 -24
View File
@@ -1,5 +1,7 @@
package hscript;
import hscript.Interp.DeclaredVar;
import hscript.utils.UnsafeReflect;
using StringTools;
class CustomClassHandler implements IHScriptCustomConstructor {
@@ -11,12 +13,18 @@ class CustomClassHandler implements IHScriptCustomConstructor {
public var extend:String;
public var interfaces:Array<String>;
public var cl:Class<Dynamic>;
public function new(ogInterp:Interp, name:String, fields:Array<Expr>, ?extend:String, ?interfaces:Array<String>) {
this.ogInterp = ogInterp;
this.name = name;
this.fields = fields;
this.extend = extend;
this.interfaces = interfaces;
this.cl = extend == null ? TemplateClass : Type.resolveClass('${extend}_HSX');
if(cl == null)
ogInterp.error(EInvalidClass(extend));
}
public function hnew(args:Array<Dynamic>):Dynamic {
@@ -24,14 +32,10 @@ class CustomClassHandler implements IHScriptCustomConstructor {
interp.errorHandler = ogInterp.errorHandler;
var cl = extend == null ? TemplateClass : Type.resolveClass('${extend}_HSX');
if(cl == null)
ogInterp.error(EInvalidClass(extend));
var _class = Type.createInstance(cl, args);
var _class:IHScriptCustomClassBehaviour = Type.createInstance(cl, args);
var __capturedLocals = ogInterp.duplicate(ogInterp.locals);
var capturedLocals:Map<String, {r:Dynamic, depth:Int}> = [];
var capturedLocals:Map<String, DeclaredVar> = [];
for(k=>e in __capturedLocals)
if (e != null && e.depth <= 0)
capturedLocals.set(k, e);
@@ -49,6 +53,15 @@ class CustomClassHandler implements IHScriptCustomConstructor {
}
}
var comparisonMap = new Map();
for(key => value in interp.variables) {
comparisonMap.set(key, value);
}
_class.__custom__variables = interp.variables;
//trace(fields);
for(expr in fields) {
@:privateAccess
interp.exprReturn(expr);
@@ -58,14 +71,33 @@ class CustomClassHandler implements IHScriptCustomConstructor {
_class.__interp = interp;
interp.scriptObject = _class;
var newFunc = interp.variables.get("new");
if(newFunc != null) {
Reflect.callMethod(null, newFunc, args);
// get only variables that were not set before
var classVariables = [for(key => value in interp.variables) if(!comparisonMap.exists(key) || comparisonMap[key] != value) key => value];
for(variable => value in classVariables) {
if(variable == "this" || variable == "super") continue;
@:privateAccess
if(!interp.__instanceFields.contains(variable)) {
interp.__instanceFields.push(variable);
}
}
//trace([for(key => value in classVariables) key]);
//@:privateAccess
//trace(interp.__instanceFields);
_class.__allowSetGet = false;
for(variable => value in interp.variables) {
if(variable == "this") continue;
if(variable.startsWith("set_") || variable.startsWith("get_")) {
_class.__allowSetGet = true;
}
}
var newFunc = interp.variables.get("new");
if(newFunc != null) {
UnsafeReflect.callMethodUnsafe(null, newFunc, args);
}
return _class;
@@ -76,29 +108,44 @@ class CustomClassHandler implements IHScriptCustomConstructor {
}
}
class TemplateClass implements IHScriptCustomBehaviour {
class TemplateClass implements IHScriptCustomClassBehaviour implements IHScriptCustomBehaviour {
public var __interp:Interp;
public var __allowSetGet:Bool = true;
public var __custom__variables:Map<String, Dynamic>;
public function hset(name:String, val:Dynamic):Dynamic {
if(this.__interp.variables.exists("set_" + name)) {
return this.__interp.variables.get("set_" + name)(val); // TODO: Prevent recursion from setting it in the function
}
if (this.__interp.variables.exists(name)) {
this.__interp.variables.set(name, val);
if(__allowSetGet && __custom__variables.exists("set_" + name))
return __callSetter(name, val);
if (__custom__variables.exists(name)) {
__custom__variables.set(name, val);
return val;
}
Reflect.setProperty(this, name, val);
return Reflect.field(this, name);
UnsafeReflect.setProperty(this, name, val);
return UnsafeReflect.field(this, name);
}
public function hget(name:String):Dynamic {
if(this.__interp.variables.exists("get_" + name))
return this.__interp.variables.get("get_" + name)();
if (this.__interp.variables.exists(name))
return this.__interp.variables.get(name);
return Reflect.getProperty(this, name);
if(__allowSetGet && __custom__variables.exists("get_" + name))
return __callGetter(name);
if (__custom__variables.exists(name))
return __custom__variables.get(name);
return UnsafeReflect.getProperty(this, name);
}
public function __callGetter(name:String):Dynamic {
__allowSetGet = false;
var v = __custom__variables.get("get_" + name)();
__allowSetGet = true;
return v;
}
public function __callSetter(name:String, val:Dynamic):Dynamic {
__allowSetGet = false;
var v = __custom__variables.get("set_" + name)(val);
__allowSetGet = true;
return v;
}
}
class StaticHandler {
final class StaticHandler {
public function new() {}
}