mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
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:
@@ -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.
|
||||
@@ -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.
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -41,7 +41,7 @@ const fn3 = asyncResource.bind(common.mustCall(function() {
|
||||
fn3();
|
||||
|
||||
const fn4 = asyncResource.bind(common.mustCall(function() {
|
||||
assert.strictEqual(this, asyncResource);
|
||||
assert.strictEqual(this, undefined);
|
||||
}));
|
||||
fn4();
|
||||
|
||||
@@ -49,3 +49,8 @@ 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');
|
||||
|
||||
Reference in New Issue
Block a user