Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: extract uncurryThis function for reuse #23081

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ const {
arrow_message_private_symbol: kArrowMessagePrivateSymbolIndex,
decorated_private_symbol: kDecoratedPrivateSymbolIndex
} = internalBinding('util');
const {
isNativeError
} = require('internal/util/types');
const { isNativeError } = internalBinding('types');

const noCrypto = !process.versions.openssl;

Expand Down Expand Up @@ -387,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,
Expand All @@ -407,6 +416,7 @@ module.exports = {
promisify,
spliceOne,
removeColors,
uncurryThis,

// Symbol used to customize promisify conversion
customPromisifyArgs: kCustomPromisifyArgsSymbol,
Expand Down
7 changes: 1 addition & 6 deletions lib/internal/util/comparisons.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,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;
Expand Down
14 changes: 2 additions & 12 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const {
customInspectSymbol,
isError,
join,
removeColors
removeColors,
uncurryThis
} = require('internal/util');

const {
Expand Down Expand Up @@ -68,17 +69,6 @@ const assert = require('internal/assert');
// Avoid monkey-patched built-ins.
const { Object } = primordials;

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);
Expand Down
11 changes: 1 addition & 10 deletions lib/internal/util/types.js
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
6 changes: 1 addition & 5 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down