Skip to content

Commit

Permalink
lib: fix AsyncResource.bind not using 'this' from the caller by default
Browse files Browse the repository at this point in the history
PR-URL: #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>
  • Loading branch information
rochdev authored Mar 8, 2022
1 parent 2f2f422 commit 409c594
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
8 changes: 8 additions & 0 deletions doc/api/async_context.md
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,10 @@ added:
- v14.8.0
- v12.19.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/42177
description: Changed the default when `thisArg` is undefined to use `this`
from the caller.
- version: v16.0.0
pr-url: https://github.com/nodejs/node/pull/36782
description: Added optional thisArg.
Expand All @@ -468,6 +472,10 @@ added:
- v14.8.0
- v12.19.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/42177
description: Changed the default when `thisArg` is undefined to use `this`
from the caller.
- version: v16.0.0
pr-url: https://github.com/nodejs/node/pull/36782
description: Added optional thisArg.
Expand Down
23 changes: 14 additions & 9 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
ArrayPrototypeIndexOf,
ArrayPrototypePush,
ArrayPrototypeSplice,
ArrayPrototypeUnshift,
FunctionPrototypeBind,
NumberIsSafeInteger,
ObjectDefineProperties,
Expand Down Expand Up @@ -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,
Expand All @@ -245,7 +250,7 @@ class AsyncResource {
writable: true,
}
});
return ret;
return bound;
}

static bind(fn, type, thisArg) {
Expand Down
7 changes: 6 additions & 1 deletion test/parallel/test-asyncresource-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,16 @@ const fn3 = asyncResource.bind(common.mustCall(function() {
fn3();

const fn4 = asyncResource.bind(common.mustCall(function() {
assert.strictEqual(this, asyncResource);
assert.strictEqual(this, undefined);
}));
fn4();

const fn5 = asyncResource.bind(common.mustCall(function() {
assert.strictEqual(this, false);
}), false);
fn5();

const fn6 = asyncResource.bind(common.mustCall(function() {
assert.strictEqual(this, 'test');
}));
fn6.call('test');

0 comments on commit 409c594

Please sign in to comment.