-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
module: refactor to use iterable-weak-map
Using an iterable WeakMap (a data-structure that uses WeakRef and WeakMap), we are able to: stop relying on Module._cache to serialize source maps; stop requiring an error object when calling findSourceMap(). PR-URL: #35915 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
- Loading branch information
1 parent
1dd744a
commit 8d76db8
Showing
16 changed files
with
281 additions
and
62 deletions.
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,86 @@ | ||
'use strict'; | ||
|
||
const { | ||
makeSafe, | ||
Object, | ||
SafeSet, | ||
SafeWeakMap, | ||
SymbolIterator, | ||
} = primordials; | ||
|
||
// TODO(aduh95): Add FinalizationRegistry to primordials | ||
const SafeFinalizationRegistry = makeSafe( | ||
globalThis.FinalizationRegistry, | ||
class SafeFinalizationRegistry extends globalThis.FinalizationRegistry {} | ||
); | ||
|
||
// TODO(aduh95): Add WeakRef to primordials | ||
const SafeWeakRef = makeSafe( | ||
globalThis.WeakRef, | ||
class SafeWeakRef extends globalThis.WeakRef {} | ||
); | ||
|
||
// This class is modified from the example code in the WeakRefs specification: | ||
// https://github.com/tc39/proposal-weakrefs | ||
// Licensed under ECMA's MIT-style license, see: | ||
// https://github.com/tc39/ecma262/blob/master/LICENSE.md | ||
class IterableWeakMap { | ||
#weakMap = new SafeWeakMap(); | ||
#refSet = new SafeSet(); | ||
#finalizationGroup = new SafeFinalizationRegistry(cleanup); | ||
|
||
set(key, value) { | ||
const entry = this.#weakMap.get(key); | ||
if (entry) { | ||
// If there's already an entry for the object represented by "key", | ||
// the value can be updated without creating a new WeakRef: | ||
this.#weakMap.set(key, { value, ref: entry.ref }); | ||
} else { | ||
const ref = new SafeWeakRef(key); | ||
this.#weakMap.set(key, { value, ref }); | ||
this.#refSet.add(ref); | ||
this.#finalizationGroup.register(key, { | ||
set: this.#refSet, | ||
ref | ||
}, ref); | ||
} | ||
} | ||
|
||
get(key) { | ||
return this.#weakMap.get(key)?.value; | ||
} | ||
|
||
has(key) { | ||
return this.#weakMap.has(key); | ||
} | ||
|
||
delete(key) { | ||
const entry = this.#weakMap.get(key); | ||
if (!entry) { | ||
return false; | ||
} | ||
this.#weakMap.delete(key); | ||
this.#refSet.delete(entry.ref); | ||
this.#finalizationGroup.unregister(entry.ref); | ||
return true; | ||
} | ||
|
||
*[SymbolIterator]() { | ||
for (const ref of this.#refSet) { | ||
const key = ref.deref(); | ||
if (!key) continue; | ||
const { value } = this.#weakMap.get(key); | ||
yield value; | ||
} | ||
} | ||
} | ||
|
||
function cleanup({ set, ref }) { | ||
set.delete(ref); | ||
} | ||
|
||
Object.freeze(IterableWeakMap.prototype); | ||
|
||
module.exports = { | ||
IterableWeakMap, | ||
}; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,12 @@ | ||
enum ATrue { | ||
IsTrue = 1, | ||
IsFalse = 0 | ||
} | ||
|
||
if (false) { | ||
console.info('unreachable') | ||
} else if (true) { | ||
throw Error('throw early') | ||
} else { | ||
console.info('unreachable') | ||
} |
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
Oops, something went wrong.