Skip to content

Commit 409c594

Browse files
authored
lib: fix AsyncResource.bind not using 'this' from the caller by default
PR-URL: #42177 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]> Reviewed-By: Bradley Farias <[email protected]> Reviewed-By: Bryan English <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 2f2f422 commit 409c594

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

doc/api/async_context.md

+8
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,10 @@ added:
446446
- v14.8.0
447447
- v12.19.0
448448
changes:
449+
- version: REPLACEME
450+
pr-url: https://github.com/nodejs/node/pull/42177
451+
description: Changed the default when `thisArg` is undefined to use `this`
452+
from the caller.
449453
- version: v16.0.0
450454
pr-url: https://github.com/nodejs/node/pull/36782
451455
description: Added optional thisArg.
@@ -468,6 +472,10 @@ added:
468472
- v14.8.0
469473
- v12.19.0
470474
changes:
475+
- version: REPLACEME
476+
pr-url: https://github.com/nodejs/node/pull/42177
477+
description: Changed the default when `thisArg` is undefined to use `this`
478+
from the caller.
471479
- version: v16.0.0
472480
pr-url: https://github.com/nodejs/node/pull/36782
473481
description: Added optional thisArg.

lib/async_hooks.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const {
55
ArrayPrototypeIndexOf,
66
ArrayPrototypePush,
77
ArrayPrototypeSplice,
8+
ArrayPrototypeUnshift,
89
FunctionPrototypeBind,
910
NumberIsSafeInteger,
1011
ObjectDefineProperties,
@@ -223,15 +224,19 @@ class AsyncResource {
223224
return this[trigger_async_id_symbol];
224225
}
225226

226-
bind(fn, thisArg = this) {
227+
bind(fn, thisArg) {
227228
validateFunction(fn, 'fn');
228-
const ret =
229-
FunctionPrototypeBind(
230-
this.runInAsyncScope,
231-
this,
232-
fn,
233-
thisArg);
234-
ObjectDefineProperties(ret, {
229+
let bound;
230+
if (thisArg === undefined) {
231+
const resource = this;
232+
bound = function(...args) {
233+
ArrayPrototypeUnshift(args, fn, this);
234+
return ReflectApply(resource.runInAsyncScope, resource, args);
235+
};
236+
} else {
237+
bound = FunctionPrototypeBind(this.runInAsyncScope, this, fn, thisArg);
238+
}
239+
ObjectDefineProperties(bound, {
235240
'length': {
236241
configurable: true,
237242
enumerable: false,
@@ -245,7 +250,7 @@ class AsyncResource {
245250
writable: true,
246251
}
247252
});
248-
return ret;
253+
return bound;
249254
}
250255

251256
static bind(fn, type, thisArg) {

test/parallel/test-asyncresource-bind.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,16 @@ const fn3 = asyncResource.bind(common.mustCall(function() {
4141
fn3();
4242

4343
const fn4 = asyncResource.bind(common.mustCall(function() {
44-
assert.strictEqual(this, asyncResource);
44+
assert.strictEqual(this, undefined);
4545
}));
4646
fn4();
4747

4848
const fn5 = asyncResource.bind(common.mustCall(function() {
4949
assert.strictEqual(this, false);
5050
}), false);
5151
fn5();
52+
53+
const fn6 = asyncResource.bind(common.mustCall(function() {
54+
assert.strictEqual(this, 'test');
55+
}));
56+
fn6.call('test');

0 commit comments

Comments
 (0)