This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
Editorial: Fix another IterableWeakMap
leak
#216
Open
ExE-Boss
wants to merge
1
commit into
tc39:master
Choose a base branch
from
ExE-Boss:fix/gh-213
base: master
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 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 |
---|---|---|
|
@@ -243,26 +243,36 @@ class IterableWeakMap { | |
set.delete(ref); | ||
} | ||
|
||
constructor(iterable) { | ||
for (const [key, value] of iterable) { | ||
this.set(key, value); | ||
constructor(iterable = null) { | ||
if (iterable !== null) { | ||
for (const { 0: key, 1: value } of iterable) { | ||
this.set(key, value); | ||
} | ||
} | ||
} | ||
Comment on lines
+246
to
252
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using object destructuring approximates the |
||
|
||
set(key, value) { | ||
const ref = new WeakRef(key); | ||
|
||
this.#weakMap.set(key, { value, ref }); | ||
this.#refSet.add(ref); | ||
this.#finalizationGroup.register(key, { | ||
set: this.#refSet, | ||
ref | ||
}, ref); | ||
const entry = this.#weakMap.get(key); | ||
if (entry) { | ||
entry.value = value; | ||
} else { | ||
const ref = new WeakRef(key); | ||
|
||
this.#weakMap.set(key, { value, ref }); | ||
this.#refSet.add(ref); | ||
this.#finalizationGroup.register(key, { | ||
set: this.#refSet, | ||
ref | ||
}, ref); | ||
} | ||
} | ||
|
||
get(key) { | ||
const entry = this.#weakMap.get(key); | ||
return entry && entry.value; | ||
return this.#weakMap.get(key)?.value; | ||
} | ||
|
||
has(key) { | ||
return this.#weakMap.has(key); | ||
} | ||
|
||
delete(key) { | ||
|
@@ -291,13 +301,13 @@ class IterableWeakMap { | |
} | ||
|
||
*keys() { | ||
for (const [key, value] of this) { | ||
for (const { 0: key } of this) { | ||
yield key; | ||
} | ||
} | ||
|
||
*values() { | ||
for (const [key, value] of this) { | ||
for (const { 1: value } of this) { | ||
yield value; | ||
} | ||
} | ||
Comment on lines
303
to
313
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using object destructuring doesn’t require an inner iteration of the |
||
|
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.
if leaks are a concern, for..of is not an available option. Perhaps
Array.from()
?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.
This is the constructor,
Array.from
is no different when it comes to iterables.Also, if it’s invoked as:
then that’s no different from:
which also performs
for...of
‑like iterationThere 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.
On arraylikes, Array.from doesn’t invoke the iterator protocol, so it’s actually robust for the common case.
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.
@ljharb
Array.from
prefers using the iterator protocol: https://tc39.es/ecma262/#sec-array.fromThere 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.
ah, you’re right, i was thinking only of a missing Symbol.iterator i suppose.
in that case there’s no pragmatic way to use a robust iteration mechanism in the readme (altho https://npmjs.com/iterate-value does exist) so this can be resolved.