From 5f032a7a269b66d48505869b0ae4fb1db403b118 Mon Sep 17 00:00:00 2001 From: ZYSzys Date: Tue, 19 Mar 2019 14:00:49 +0800 Subject: util: extract uncurryThis function for reuse PR-URL: https://github.com/nodejs/node/pull/23081 Reviewed-By: Ruben Bridgewater Reviewed-By: Sakthipriyan Vairamani Reviewed-By: Weijia Wang Reviewed-By: Joyee Cheung Reviewed-By: Refael Ackermann Reviewed-By: Denys Otrishko Reviewed-By: John-David Dalton Reviewed-By: Anna Henningsen --- lib/internal/util.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'lib/internal/util.js') diff --git a/lib/internal/util.js b/lib/internal/util.js index bb2a101c4e..f5b5fbedd4 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -385,6 +385,17 @@ function once(callback) { }; } +const ReflectApply = Reflect.apply; + +// This function is borrowed from the function with the same name on V8 Extras' +// `utils` object. V8 implements Reflect.apply very efficiently in conjunction +// with the spread syntax, such that no additional special case is needed for +// function calls w/o arguments. +// Refs: https://github.com/v8/v8/blob/d6ead37d265d7215cf9c5f768f279e21bd170212/src/js/prologue.js#L152-L156 +function uncurryThis(func) { + return (thisArg, ...args) => ReflectApply(func, thisArg, args); +} + module.exports = { assertCrypto, cachedResult, @@ -405,6 +416,7 @@ module.exports = { promisify, spliceOne, removeColors, + uncurryThis, // Symbol used to customize promisify conversion customPromisifyArgs: kCustomPromisifyArgsSymbol, -- cgit v1.2.3