Repository navigation
Bump WebKit (oven-sh/WebKit#596 preview): Set methods compare a set-like size of 2^32 or more correctly #42080
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
Open
robobun
wants to merge
2
commits into
main
Choose a base branch
from
robobun/1332550e/set-like-size-beyond-uint32
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 hidden or 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 hidden or 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,157 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
|
|
||
| // The Set methods take any "set-like" with a numeric `size`, a `has()` and a | ||
| // `keys()`. GetSetRecord (https://tc39.es/ecma262/#sec-getsetrecord) keeps that | ||
| // size as an unbounded integer or +Infinity, and each method compares it with | ||
| // the receiver's size to pick a strategy: ask `other.has()` about each of the | ||
| // receiver's elements, or iterate `other.keys()`. JSC used to truncate the size | ||
| // to uint32 first, so a set-like reporting 2^32 or more elements compared as | ||
| // size mod 2^32 and the methods consulted the wrong side. | ||
|
|
||
| const sizes = [ | ||
| 0, | ||
| 1, | ||
| 2, | ||
| 3, | ||
| 2 ** 31, | ||
| 2 ** 32 - 1, | ||
| 2 ** 32, | ||
| 2 ** 32 + 1, | ||
| 2 ** 32 + 2, | ||
| 2 ** 33, | ||
| 2 ** 53 - 1, | ||
| 2 ** 53, | ||
| 2 ** 53 + 2, | ||
| 2 ** 64, | ||
| 1e300, | ||
| Number.MAX_VALUE, | ||
| Infinity, | ||
| ]; | ||
|
|
||
| // A set-like whose has() claims exactly the value 1 and whose keys() yields | ||
| // exactly the value 1, independent of the size it reports. It logs every | ||
| // observable interaction so the chosen strategy is visible. | ||
| function makeSetLike(size: number) { | ||
| const log: string[] = []; | ||
| const setLike = { | ||
| get size() { | ||
| log.push("size"); | ||
| return size; | ||
| }, | ||
| has(v: unknown) { | ||
| log.push("has:" + String(v)); | ||
| return v === 1; | ||
| }, | ||
| keys() { | ||
| log.push("keys"); | ||
| let done = false; | ||
| return { | ||
| next() { | ||
| log.push("next"); | ||
| if (done) return { value: undefined, done: true }; | ||
| done = true; | ||
| return { value: 1, done: false }; | ||
| }, | ||
| return() { | ||
| log.push("return"); | ||
| return {}; | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
| return { setLike, log }; | ||
| } | ||
|
|
||
| // What the spec algorithms do for a receiver of `thisSize` elements against a | ||
| // set-like reporting `otherSize`, given the has()/keys() behaviour above. | ||
| function expected(method: string, receiver: number[], otherSize: number) { | ||
| const thisSize = receiver.length; | ||
| const hasCalls = receiver.map(v => "has:" + v); | ||
| switch (method) { | ||
| case "union": | ||
| return { result: [...new Set([...receiver, 1])], log: ["size", "keys", "next", "next"] }; | ||
| case "symmetricDifference": | ||
| return { | ||
| result: receiver.includes(1) ? receiver.filter(v => v !== 1) : [...receiver, 1], | ||
| log: ["size", "keys", "next", "next"], | ||
| }; | ||
| case "intersection": | ||
| return thisSize <= otherSize | ||
| ? { result: receiver.filter(v => v === 1), log: ["size", ...hasCalls] } | ||
| : { result: receiver.includes(1) ? [1] : [], log: ["size", "keys", "next", "next"] }; | ||
| case "difference": | ||
| return thisSize <= otherSize | ||
| ? { result: receiver.filter(v => v !== 1), log: ["size", ...hasCalls] } | ||
| : { result: receiver.filter(v => v !== 1), log: ["size", "keys", "next", "next"] }; | ||
| case "isSubsetOf": { | ||
| if (thisSize > otherSize) return { result: false, log: ["size"] }; | ||
| // Stops at the first element other.has() rejects. | ||
| const i = receiver.findIndex(v => v !== 1); | ||
| return { result: i === -1, log: ["size", ...hasCalls.slice(0, i === -1 ? receiver.length : i + 1)] }; | ||
| } | ||
| case "isSupersetOf": | ||
| if (thisSize < otherSize) return { result: false, log: ["size"] }; | ||
| // keys() yields 1; if the receiver lacks it the iterator is closed early. | ||
| return receiver.includes(1) | ||
| ? { result: true, log: ["size", "keys", "next", "next"] } | ||
| : { result: false, log: ["size", "keys", "next", "return"] }; | ||
| case "isDisjointFrom": | ||
| if (thisSize <= otherSize) { | ||
| // Stops at the first element other.has() accepts. | ||
| const i = receiver.indexOf(1); | ||
| return { result: i === -1, log: ["size", ...hasCalls.slice(0, i === -1 ? receiver.length : i + 1)] }; | ||
| } | ||
| return receiver.includes(1) | ||
| ? { result: false, log: ["size", "keys", "next", "return"] } | ||
| : { result: true, log: ["size", "keys", "next", "next"] }; | ||
| } | ||
| throw new Error(method); | ||
| } | ||
|
|
||
| const methods = [ | ||
| "union", | ||
| "intersection", | ||
| "difference", | ||
| "symmetricDifference", | ||
| "isSubsetOf", | ||
| "isSupersetOf", | ||
| "isDisjointFrom", | ||
| ] as const; | ||
|
|
||
| const receivers = [[], [1], [2], [1, 2], [2, 3], [2, 1, 3]]; | ||
|
|
||
| describe.each(methods)("Set.prototype.%s", method => { | ||
| test.each(sizes)("set-like with size %p", size => { | ||
| for (const receiver of receivers) { | ||
| const { setLike, log } = makeSetLike(size); | ||
| const raw = (new Set(receiver) as any)[method](setLike); | ||
| const result = raw instanceof Set ? [...raw] : raw; | ||
| expect({ receiver, size, result, log }).toEqual({ receiver, size, ...expected(method, receiver, size) }); | ||
| } | ||
| }); | ||
|
|
||
| test("negative sizes throw RangeError, even beyond -2^32", () => { | ||
| for (const size of [-1, -(2 ** 31), -(2 ** 32), -(2 ** 32) - 1, -(2 ** 53), -1e300, -Infinity]) { | ||
| const { setLike, log } = makeSetLike(size); | ||
| expect(() => (new Set([1]) as any)[method](setLike)).toThrow(RangeError); | ||
| expect(log).toEqual(["size"]); | ||
| } | ||
| }); | ||
|
|
||
| test("sizes that coerce to NaN or cannot coerce throw TypeError", () => { | ||
| // NaN after ToNumber, or ToNumber itself throws (BigInt, Symbol). | ||
| for (const size of [NaN, undefined, "x", {}, 1n, Symbol("size")]) { | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| const { setLike, log } = makeSetLike(size as number); | ||
| expect(() => (new Set([1]) as any)[method](setLike)).toThrow(TypeError); | ||
| expect(log).toEqual(["size"]); | ||
| } | ||
| }); | ||
|
|
||
| test("fractional sizes truncate toward zero before the comparison", () => { | ||
| // 1.9 truncates to 1, so a two-element receiver is larger than the set-like. | ||
| const { setLike, log } = makeSetLike(1.9); | ||
| const raw = (new Set([1, 2]) as any)[method](setLike); | ||
| const result = raw instanceof Set ? [...raw] : raw; | ||
| expect({ result, log }).toEqual(expected(method, [1, 2], 1)); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.