Skip to content

lib: add SafeSet.prototype.toUnsafeSet #57723

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

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 6 additions & 0 deletions lib/internal/per_context/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,12 @@ primordials.SafeSet = makeSafe(
Set,
class SafeSet extends Set {
constructor(i) { super(i); } // eslint-disable-line no-useless-constructor

toUnsafeSet() {
const set = new Set();
this.forEach((value) => primordials.SetPrototypeAdd(set, value));
return set;
Comment on lines +425 to +427
Copy link
Contributor

@aduh95 aduh95 Apr 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we write tests that validates no user-code would be run? I think this can be simplified to

Suggested change
const set = new Set();
this.forEach((value) => primordials.SetPrototypeAdd(set, value));
return set;
return new Set(this);

(given the triviality of the implementation, I wonder if it actually makes sense to add a method for that)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! I couldn't find anywhere SafeSet is already tested, so i'm not sure where to add them - any suggestions?

I guess this is a valid simplification since it has a safe [Symbol.iterator] on it, in which case you're right, it's probably not needed. I was assuming add-at-construction-time wasn't an option.

}
},
);
primordials.SafeWeakSet = makeSafe(
Expand Down
Loading