From 21486e5c97cb449f2e9a6dffc7240dcf796e0231 Mon Sep 17 00:00:00 2001 From: ZYSzys Date: Tue, 19 Mar 2019 14:00:49 +0800 Subject: [PATCH] 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 ++++++++++++ lib/internal/util/comparisons.js | 7 +------ lib/internal/util/inspect.js | 14 ++------------ lib/internal/util/types.js | 11 +---------- lib/util.js | 6 +----- 5 files changed, 17 insertions(+), 33 deletions(-) diff --git a/lib/internal/util.js b/lib/internal/util.js index bb2a101c4e8648..f5b5fbedd46b4f 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, diff --git a/lib/internal/util/comparisons.js b/lib/internal/util/comparisons.js index d0726089063852..f99260f13a7125 100644 --- a/lib/internal/util/comparisons.js +++ b/lib/internal/util/comparisons.js @@ -22,12 +22,7 @@ const { ONLY_ENUMERABLE } } = internalBinding('util'); - -const ReflectApply = Reflect.apply; - -function uncurryThis(func) { - return (thisArg, ...args) => ReflectApply(func, thisArg, args); -} +const { uncurryThis } = require('internal/util'); const kStrict = true; const kLoose = false; diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index b330b5905d0784..d52e976c0bb449 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -17,7 +17,8 @@ const { customInspectSymbol, isError, join, - removeColors + removeColors, + uncurryThis } = require('internal/util'); const { @@ -63,17 +64,6 @@ const { isBigUint64Array } = require('internal/util/types'); -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); -} - const propertyIsEnumerable = uncurryThis(Object.prototype.propertyIsEnumerable); const regExpToString = uncurryThis(RegExp.prototype.toString); const dateToISOString = uncurryThis(Date.prototype.toISOString); diff --git a/lib/internal/util/types.js b/lib/internal/util/types.js index 865818c661b78d..f41d11443f2bb5 100644 --- a/lib/internal/util/types.js +++ b/lib/internal/util/types.js @@ -1,15 +1,6 @@ 'use strict'; -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); -} +const { uncurryThis } = require('internal/util'); const TypedArrayPrototype = Object.getPrototypeOf(Uint8Array.prototype); diff --git a/lib/util.js b/lib/util.js index 6a3936856940ee..c292758d1fae3c 100644 --- a/lib/util.js +++ b/lib/util.js @@ -42,13 +42,9 @@ const { deprecate, getSystemErrorName: internalErrorName, promisify, + uncurryThis } = require('internal/util'); -const ReflectApply = Reflect.apply; - -function uncurryThis(func) { - return (thisArg, ...args) => ReflectApply(func, thisArg, args); -} const objectToString = uncurryThis(Object.prototype.toString); let internalDeepEqual;