Skip to content

Commit 69a3792

Browse files
rochdevbengl
authored andcommitted
lib: fix AsyncResource.bind not using 'this' from the caller by default
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>
1 parent 70c0758 commit 69a3792

3 files changed

Lines changed: 28 additions & 10 deletions

File tree

‎doc/api/async_context.md‎

Lines changed: 8 additions & 0 deletions
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‎

Lines changed: 14 additions & 9 deletions
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
returnthis[trigger_async_id_symbol];
224225
}
225226

226-
bind(fn,thisArg=this){
227+
bind(fn,thisArg){
227228
validateFunction(fn,'fn');
228-
constret=
229-
FunctionPrototypeBind(
230-
this.runInAsyncScope,
231-
this,
232-
fn,
233-
thisArg);
234-
ObjectDefineProperties(ret,{
229+
letbound;
230+
if(thisArg===undefined){
231+
constresource=this;
232+
bound=function(...args){
233+
ArrayPrototypeUnshift(args,fn,this);
234+
returnReflectApply(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-
returnret;
253+
returnbound;
249254
}
250255

251256
staticbind(fn,type,thisArg){

‎test/parallel/test-asyncresource-bind.js‎

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

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

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

0 commit comments

Comments
 (0)