lib: fix AsyncResource.bind not using 'this' from the caller by default

PR-URL: https://github.com/nodejs/node/pull/42177
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: Bryan English <bryan@bryanenglish.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Roch Devost 2022-03-08 17:00:55 -05:00 committed by GitHub
parent 2f2f422ff8
commit 409c594887
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 10 deletions

View file

@ -5,6 +5,7 @@ const {
ArrayPrototypeIndexOf,
ArrayPrototypePush,
ArrayPrototypeSplice,
ArrayPrototypeUnshift,
FunctionPrototypeBind,
NumberIsSafeInteger,
ObjectDefineProperties,
@ -223,15 +224,19 @@ class AsyncResource {
return this[trigger_async_id_symbol];
}
bind(fn, thisArg = this) {
bind(fn, thisArg) {
validateFunction(fn, 'fn');
const ret =
FunctionPrototypeBind(
this.runInAsyncScope,
this,
fn,
thisArg);
ObjectDefineProperties(ret, {
let bound;
if (thisArg === undefined) {
const resource = this;
bound = function(...args) {
ArrayPrototypeUnshift(args, fn, this);
return ReflectApply(resource.runInAsyncScope, resource, args);
};
} else {
bound = FunctionPrototypeBind(this.runInAsyncScope, this, fn, thisArg);
}
ObjectDefineProperties(bound, {
'length': {
configurable: true,
enumerable: false,
@ -245,7 +250,7 @@ class AsyncResource {
writable: true,
}
});
return ret;
return bound;
}
static bind(fn, type, thisArg) {