From 4aa757e625293e131c321df3843edb888f83ee64 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Sat, 6 Jan 2024 17:46:39 +0200 Subject: [PATCH] fix: Function.bind now passess this argument, instead of the function itself --- src/me/topchetoeu/jscript/lib/FunctionLib.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/me/topchetoeu/jscript/lib/FunctionLib.java b/src/me/topchetoeu/jscript/lib/FunctionLib.java index f79e5d1..7d42754 100644 --- a/src/me/topchetoeu/jscript/lib/FunctionLib.java +++ b/src/me/topchetoeu/jscript/lib/FunctionLib.java @@ -24,17 +24,20 @@ public class FunctionLib { } @Expose public static FunctionValue __bind(Arguments args) { var self = args.self(FunctionValue.class); + var thisArg = args.get(0); + var bindArgs = args.slice(1).args; + return new NativeFunction(self.name + " (bound)", callArgs -> { Object[] resArgs; - if (args.n() == 0) resArgs = callArgs.args; + if (args.n() == 0) resArgs = bindArgs; else { - resArgs = new Object[args.n() + callArgs.n()]; - System.arraycopy(args.args, 0, resArgs, 0, args.n()); - System.arraycopy(callArgs.args, 0, resArgs, args.n(), callArgs.n()); + resArgs = new Object[bindArgs.length + callArgs.n()]; + System.arraycopy(bindArgs, 0, resArgs, 0, bindArgs.length); + System.arraycopy(callArgs.args, 0, resArgs, bindArgs.length, callArgs.n()); } - return self.call(callArgs.ctx, self, resArgs); + return self.call(callArgs.ctx, thisArg, resArgs); }); } @Expose public static String __toString(Arguments args) {