Skip to content

Commit 44c40aa

Browse files
committed
fixup! lib: allow CJS source map cache to be reclaimed
1 parent af592c5 commit 44c40aa

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

lib/internal/source_map/source_map_cache_map.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,22 @@ const {
55
ObjectFreeze,
66
SafeFinalizationRegistry,
77
SafeMap,
8-
SafeWeakMap,
98
SafeWeakRef,
109
SymbolIterator,
1110
} = primordials;
11+
const {
12+
privateSymbols: {
13+
source_map_data_private_symbol,
14+
},
15+
} = internalBinding('util');
1216

1317
/**
1418
* Specialized WeakMap that caches source map entries by `filename` and `sourceURL`.
1519
* Cached entries can be iterated with `for..of`.
1620
*
1721
* The cache map maintains the cache entries by:
1822
* - `weakTargetMap`(Map): a strong sourceURL -> WeakRef(Module),
19-
* - `weakMap`(WeakMap): a Module instance object -> source map data.
23+
* - WeakRef(Module[source_map_data_private_symbol]): source map data.
2024
*
2125
* Obsolete `weakTargetMap` entries are removed by the `finalizationRegistry` callback.
2226
* This pattern decouples the strong url reference to the source map data and allow the
@@ -33,7 +37,6 @@ class SourceMapCacheMap {
3337
* the cache by the `finalizationRegistry`.
3438
*/
3539
#weakTargetMap = new SafeMap();
36-
#weakMap = new SafeWeakMap();
3740

3841
#cleanup = ({ keys }) => {
3942
// Delete the entry if the weak target has been reclaimed.
@@ -58,7 +61,7 @@ class SourceMapCacheMap {
5861
set(keys, value, weakTarget) {
5962
const weakRef = new SafeWeakRef(weakTarget);
6063
ArrayPrototypeForEach(keys, (key) => this.#weakTargetMap.set(key, weakRef));
61-
this.#weakMap.set(weakTarget, value);
64+
weakTarget[source_map_data_private_symbol] = value;
6265
this.#finalizationRegistry.register(weakTarget, { keys });
6366
}
6467

@@ -72,7 +75,7 @@ class SourceMapCacheMap {
7275
if (target === undefined) {
7376
return;
7477
}
75-
return this.#weakMap.get(target);
78+
return target[source_map_data_private_symbol];
7679
}
7780

7881
/**
@@ -92,7 +95,7 @@ class SourceMapCacheMap {
9295
const { 0: key, 1: weakRef } = result.value;
9396
const target = weakRef.deref();
9497
if (target == null) return next();
95-
const value = this.#weakMap.get(target);
98+
const value = target[source_map_data_private_symbol];
9699
return { done: false, value: [key, value] };
97100
};
98101

src/env_properties.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
V(untransferable_object_private_symbol, "node:untransferableObject") \
3030
V(exit_info_private_symbol, "node:exit_info_private_symbol") \
3131
V(promise_trace_id, "node:promise_trace_id") \
32-
V(require_private_symbol, "node:require_private_symbol")
32+
V(require_private_symbol, "node:require_private_symbol") \
33+
V(source_map_data_private_symbol, "node:source_map_data_private_symbol")
3334

3435
// Symbols are per-isolate primitives but Environment proxies them
3536
// for the sake of convenience.

src/module_wrap.cc

+8
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,14 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
248248
return;
249249
}
250250

251+
// Initialize an empty slot for source map cache before the object is frozen.
252+
if (that->SetPrivate(context,
253+
realm->isolate_data()->source_map_data_private_symbol(),
254+
Undefined(isolate))
255+
.IsNothing()) {
256+
return;
257+
}
258+
251259
// Use the extras object as an object whose GetCreationContext() will be the
252260
// original `context`, since the `Context` itself strictly speaking cannot
253261
// be stored in an internal field.

0 commit comments

Comments
 (0)