Skip to content

Commit 34067d1

Browse files
committed
Fix different function (address) return on use bind, on object method
1 parent a20cb1e commit 34067d1

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

src/lualib/FunctionBind.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@ export function __TS__FunctionBind(
33
fn: (this: void, ...argArray: any[]) => any,
44
...boundArgs: any[]
55
): (...args: any[]) => any {
6+
if (typeof boundArgs[0] === "object") {
7+
const address = tostring(fn);
8+
const bound = boundArgs[0];
9+
if (!bound.__TS__wrappedMethods) bound.__TS__wrappedMethods = {};
10+
if (!bound.__TS__wrappedMethods[address]) {
11+
bound.__TS__wrappedMethods[address] = (...args: any[]) => {
12+
args.unshift(...boundArgs);
13+
return fn(...args);
14+
};
15+
}
16+
return bound.__TS__wrappedMethods[address];
17+
}
18+
619
return (...args: any[]) => {
720
args.unshift(...boundArgs);
821
return fn(...args);

test/unit/classes/classes.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,20 @@ test("ClassMethodCall", () => {
315315
`.expectToMatchJsResult();
316316
});
317317

318+
test("ClassMethodHasSameAddressAfterBinding", () => {
319+
util.testFunction`
320+
class a {
321+
public isSame() {
322+
let cb1 = '' + this.method.bind(this)
323+
let cb2 = '' + this.method.bind(this)
324+
return cb1 === cb2
325+
}
326+
public method() {}
327+
}
328+
return new a().isSame();
329+
`.expectToEqual(true);
330+
});
331+
318332
test("ClassNumericLiteralMethodCall", () => {
319333
util.testFunction`
320334
class a {

0 commit comments

Comments
 (0)