-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
lib: add bound apply
variants of varargs primordials
#37005
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Flags: --expose-internals | ||
'use strict'; | ||
|
||
require('../common'); | ||
|
||
const assert = require('assert'); | ||
const { | ||
ArrayOfApply, | ||
ArrayPrototypePushApply, | ||
ArrayPrototypeUnshiftApply, | ||
MathMaxApply, | ||
MathMinApply, | ||
StringPrototypeConcatApply, | ||
TypedArrayOfApply, | ||
} = require('internal/test/binding').primordials; | ||
|
||
{ | ||
const arr1 = [1, 2, 3]; | ||
const arr2 = ArrayOfApply(arr1); | ||
|
||
assert.deepStrictEqual(arr2, arr1); | ||
assert.notStrictEqual(arr2, arr1); | ||
} | ||
|
||
{ | ||
const array = [1, 2, 3]; | ||
const i32Array = TypedArrayOfApply(Int32Array, array); | ||
|
||
assert(i32Array instanceof Int32Array); | ||
assert.strictEqual(i32Array.length, array.length); | ||
for (let i = 0, { length } = array; i < length; i++) { | ||
assert.strictEqual(i32Array[i], array[i], `i32Array[${i}] === array[${i}]`); | ||
} | ||
} | ||
|
||
{ | ||
const arr1 = [1, 2, 3]; | ||
const arr2 = [4, 5, 6]; | ||
|
||
const expected = [...arr1, ...arr2]; | ||
|
||
assert.strictEqual(ArrayPrototypePushApply(arr1, arr2), expected.length); | ||
assert.deepStrictEqual(arr1, expected); | ||
} | ||
|
||
{ | ||
const arr1 = [1, 2, 3]; | ||
const arr2 = [4, 5, 6]; | ||
|
||
const expected = [...arr2, ...arr1]; | ||
|
||
assert.strictEqual(ArrayPrototypeUnshiftApply(arr1, arr2), expected.length); | ||
assert.deepStrictEqual(arr1, expected); | ||
} | ||
|
||
{ | ||
const array = [1, 2, 3]; | ||
assert.strictEqual(MathMaxApply(array), 3); | ||
assert.strictEqual(MathMinApply(array), 1); | ||
} | ||
|
||
{ | ||
let hint; | ||
const obj = { [Symbol.toPrimitive](h) { | ||
hint = h; | ||
return '[object Object]'; | ||
} }; | ||
|
||
const args = ['foo ', obj, ' bar']; | ||
const result = StringPrototypeConcatApply('', args); | ||
|
||
assert.strictEqual(hint, 'string'); | ||
assert.strictEqual(result, 'foo [object Object] bar'); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add a comment like the
uncurryThis
one which explains this is equivalent toThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it’s closer to:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, shouldn't we prefer using
Reflect.apply
instead ofFunction.prototype.apply
? I think if someone calls those methods without a argument array, it's almost certainly a bug, and usingReflect.apply
would help spot that. Or is there a performance reason to use one or the other?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well,
Reflect.apply
isn’t very optimised in comparison to boundFunction.prototype.call
andFunction.prototype.apply
calls.Also, engines are able to generate better code for native bound functions compared to arrow functions that replicate the behaviour of a bound function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant to use
const { apply } = Reflect
compared toconst { apply } = Function.prototype
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That wouldn’t work, since then
applyBind(foo)
would create a function that roughly does:(...args) => Reflect.apply.call(foo, ...args)
, instead of(...args) => Reflect.apply(foo, ...args)
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha, the equivalent would have to be:
Is that generally true statement or is it specific to V8? When reading the spec,
Reflect.apply
seems to be doing less things thanFunction.prototype.apply
, that's a bit counter-intuitive.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That’s mostly specific to V8, but I assume that it also applies to other C++-based engines, since V8 hasn’t put as much effort into optimising
Reflect
as into optimisingObject
andFunction
methods.Refs: #36740 (comment)