Function Call reimplemented

The previous function call system has a serious issue. It can not differentiate between class fields and function argument fields.
This commit is contained in:
Jaime Humberto Macias Bustamante
2025-01-21 22:07:41 -07:00
parent c45373aa55
commit b25ecb08bc
4 changed files with 85 additions and 103 deletions
+11 -32
View File
@@ -1,44 +1,23 @@
package hscript.customclass.utils;
import hscript.Expr.FunctionDecl;
import hscript.utils.UnsafeReflect;
import haxe.Constraints.Function;
@:access(hscript.customclass.CustomClassDecl)
class FunctionUtils {
public static function callStaticFunction(name:String, classDecl:CustomClassDecl, interp:Interp, fn:FunctionDecl, args:Array<Dynamic> = null) {
public static inline function callStaticFunction(name:String, classDecl:CustomClassDecl, interp:Interp, fn:Function, args:Array<Dynamic> = null) {
var r:Dynamic = null;
var previousValues:Map<String, Dynamic> = [];
var i = 0;
for (a in fn.args) {
var value:Dynamic = null;
if (args != null && i < args.length) {
value = args[i];
} else if (a.value != null) {
value = interp.expr(a.value);
}
// NOTE: We assign these as variables rather than locals because those get wiped when we enter the function.
if (interp.variables.exists(a.name)) {
previousValues.set(a.name, interp.variables.get(a.name));
}
interp.variables.set(a.name, value);
i++;
}
try {
r = interp.execute(fn.body);
} catch (e:hscript.Expr.Error) {
// A script error occurred while executing the script function.
// Purge the function from the cache so it is not called again.
classDecl.purgeFunction(name);
interp.error(#if hscriptPos e.e #else e #end);
}
if (fn == null)
interp.error(ECustom('${name} is not a function'));
for (a in fn.args) {
if (previousValues.exists(a.name)) {
interp.variables.set(a.name, previousValues.get(a.name));
} else {
interp.variables.remove(a.name);
}
r = UnsafeReflect.callMethodUnsafe(null, fn, args);
} catch (e:hscript.Expr.Error) {
// A script error occurred while executing the custom class function.
// Purge the function from the cache so it is not called again.
classDecl.purgeFunction(name);
interp.error(#if hscriptPos e.e #else e #end);
}
return r;