• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

overlookmotel / livepack / 7238563400

17 Dec 2023 12:26PM UTC coverage: 90.462% (+0.04%) from 90.424%
7238563400

push

github

overlookmotel
Tests for `eval`

4780 of 5140 branches covered (0.0%)

Branch coverage included in aggregate %.

12851 of 14350 relevant lines covered (89.55%)

8943.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.77
/lib/init/weak.js
1
/* --------------------
64✔
2
 * livepack module
64✔
3
 * Shim `WeakMap` + `WeakSet` to capture entries
64✔
4
 * ------------------*/
64✔
5

64✔
6
/* global WeakRef, FinalizationRegistry */
64✔
7

64✔
8
'use strict';
64✔
9

64✔
10
// Exports
64✔
11

64✔
12
module.exports = {shimWeakSet, shimWeakMap};
64✔
13

64✔
14
const WeakMapOriginal = WeakMap;
64✔
15

64✔
16
/**
64✔
17
 * Shim `WeakSet` with implementation which allow getting entries for serializing.
64✔
18
 * Uses `WeakRef` to avoid holding strong references to entries,
64✔
19
 * and allows them to be garbage collected.
64✔
20
 * Adapted from https://github.com/Lunuy/iterable-weak/blob/master/src/IterableWeakSet.ts
64✔
21
 *
64✔
22
 * @returns {Function} - Function which returns entries of a `WeakSet`
64✔
23
 */
64✔
24
function shimWeakSet() {
64✔
25
        let getWeakSetEntries;
64✔
26

64✔
27
        WeakSet = class WeakSet { // eslint-disable-line no-global-assign
64✔
28
                // Set of `WeakRefs` to entries
828✔
29
                #refs = new Set();
828✔
30
                // Mapping from entries to `WeakRef`s to those entries
828✔
31
                #mapping = new WeakMapOriginal();
828✔
32
                // FinalizationRegistry to delete `WeakRef`s from `#refs` when entries are garbage collected
828✔
33
                #finalizationRegistry = new FinalizationRegistry(ref => this.#refs.delete(ref));
828✔
34

828✔
35
                constructor(iterable = undefined) { // `= undefined` so `WeakSet.length === 0`
828✔
36
                        if (iterable != null) {
828✔
37
                                for (const element of iterable) {
24✔
38
                                        // NB: Original calls `WeakSet.prototype.add` even if it's been overwritten by user
40✔
39
                                        this.add(element);
40✔
40
                                }
40✔
41
                        }
24✔
42
                }
828✔
43

828✔
44
                delete(element) {
828✔
45
                        const ref = this.#mapping.get(element);
×
46
                        if (!ref) return false;
×
47

×
48
                        this.#mapping.delete(element);
×
49
                        this.#refs.delete(ref);
×
50
                        this.#finalizationRegistry.unregister(ref);
×
51
                        return true;
×
52
                }
×
53

828✔
54
                has(element) {
828✔
55
                        return this.#mapping.has(element);
23,360✔
56
                }
23,360✔
57

828✔
58
                add(element) {
828✔
59
                        if (!this.#mapping.has(element)) {
848✔
60
                                const ref = new WeakRef(element);
848✔
61
                                this.#mapping.set(element, ref);
848✔
62
                                this.#refs.add(ref);
848✔
63
                                this.#finalizationRegistry.register(element, ref, ref);
848✔
64
                        }
848✔
65
                        return this;
848✔
66
                }
848✔
67

828✔
68
                static {
828✔
69
                        getWeakSetEntries = (weakSet) => {
64✔
70
                                const entries = [];
32✔
71
                                for (const ref of weakSet.#refs) {
32✔
72
                                        const value = ref.deref();
40✔
73
                                        if (value) entries.push(value);
40✔
74
                                }
40✔
75
                                return entries;
32✔
76
                        };
64✔
77
                }
64✔
78
        };
64✔
79

64✔
80
        // eslint-disable-next-line no-extend-native
64✔
81
        Object.defineProperty(WeakSet.prototype, Symbol.toStringTag, {value: 'WeakSet', configurable: true});
64✔
82

64✔
83
        return getWeakSetEntries;
64✔
84
}
64✔
85

64✔
86
/**
64✔
87
 * Shim `WeakMap` with implementation which allow getting entries for serializing.
64✔
88
 * Uses `WeakRef` to avoid holding strong references to keys,
64✔
89
 * and allows them to be garbage collected.
64✔
90
 *
64✔
91
 * Adapted from https://github.com/tc39/proposal-weakrefs#iterable-weakmaps
64✔
92
 * and https://github.com/Lunuy/iterable-weak/blob/master/src/IterableWeakMap.ts
64✔
93
 *
64✔
94
 * @returns {Function} - Function which returns entries of a `WeakMap`
64✔
95
 */
64✔
96
function shimWeakMap() {
64✔
97
        let getWeakMapEntries;
64✔
98

64✔
99
        WeakMap = class WeakMap { // eslint-disable-line no-global-assign
64✔
100
                // Set of `WeakRefs` to keys
128✔
101
                #refs = new Set();
128✔
102
                // Mapping from keys to objects containing `WeakRef` to key, and value
128✔
103
                #mapping = new WeakMapOriginal();
128✔
104
                // FinalizationRegistry to delete `WeakRef`s from `#refs` when entries are garbage collected
128✔
105
                #finalizationRegistry = new FinalizationRegistry(ref => this.#refs.delete(ref));
128✔
106

128✔
107
                constructor(iterable = undefined) { // `= undefined` so `WeakMap.length === 0`
128✔
108
                        if (iterable != null) {
128✔
109
                                for (const [key, value] of iterable) {
24✔
110
                                        // NB: Original calls `WeakMap.prototype.set` even if it's been overwritten by user
40✔
111
                                        this.set(key, value);
40✔
112
                                }
40✔
113
                        }
24✔
114
                }
128✔
115

128✔
116
                delete(key) {
128✔
117
                        const entry = this.#mapping.get(key);
×
118
                        if (!entry) return false;
×
119

×
120
                        const {ref} = entry;
×
121
                        this.#refs.delete(ref);
×
122
                        this.#mapping.delete(key);
×
123
                        this.#finalizationRegistry.unregister(ref);
×
124

×
125
                        return true;
×
126
                }
×
127

128✔
128
                get(key) {
128✔
129
                        return this.#mapping.get(key)?.value;
1,096✔
130
                }
1,096✔
131

128✔
132
                set(key, value) {
128✔
133
                        const entry = this.#mapping.get(key);
666✔
134
                        if (!entry) {
666✔
135
                                const ref = new WeakRef(key);
666✔
136
                                this.#mapping.set(key, {ref, value});
666✔
137
                                this.#refs.add(ref);
666✔
138
                                this.#finalizationRegistry.register(key, ref, ref);
666✔
139
                        } else if (entry.value !== value) {
666!
140
                                entry.value = value;
×
141
                        }
×
142

666✔
143
                        return this;
666✔
144
                }
666✔
145

128✔
146
                has(key) {
128✔
147
                        return this.#mapping.has(key);
22,484✔
148
                }
22,484✔
149

128✔
150
                static {
128✔
151
                        getWeakMapEntries = (weakMap) => {
64✔
152
                                const mapping = weakMap.#mapping,
32✔
153
                                        entries = [];
32✔
154
                                for (const ref of weakMap.#refs) {
32✔
155
                                        const key = ref.deref();
56✔
156
                                        if (key) entries.push([key, mapping.get(key).value]);
56✔
157
                                }
56✔
158
                                return entries;
32✔
159
                        };
64✔
160
                }
64✔
161
        };
64✔
162

64✔
163
        // eslint-disable-next-line no-extend-native
64✔
164
        Object.defineProperty(WeakMap.prototype, Symbol.toStringTag, {value: 'WeakMap', configurable: true});
64✔
165

64✔
166
        return getWeakMapEntries;
64✔
167
}
64✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc