@abide/harness 0.0.0-stage → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +70 -5
- package/scripts/buildInjectable.ts +27 -0
- package/src/abide/index.ts +53 -0
- package/src/abide/preload.ts +6 -0
- package/src/engine/Trace.ts +54 -0
- package/src/engine/index.ts +18 -0
- package/src/engine/shares.ts +143 -0
- package/src/engine/traceOp.ts +139 -0
- package/src/gate/inAFreshProcess.ts +42 -0
- package/src/gate/index.ts +95 -0
- package/src/gate/spawned.ts +51 -0
- package/src/measure/PUBLISHED_WORK.ts +21 -0
- package/src/measure/Work.ts +71 -0
- package/src/measure/index.ts +40 -0
- package/src/measure/injectable.ts +21 -0
- package/src/measure/install.ts +559 -0
- package/src/measure/measure.ts +102 -0
- package/src/measure/preload.ts +36 -0
- package/src/report/Sample.ts +76 -0
- package/src/report/THRESHOLDS.ts +17 -0
- package/src/report/batch.ts +339 -0
- package/src/report/clock.ts +60 -0
- package/src/report/environment.ts +22 -0
- package/src/report/index.ts +17 -0
- package/src/report/ratio.ts +144 -0
- package/src/report/substrate.ts +26 -0
- package/src/rows/index.ts +91 -0
- package/src/runner/index.ts +9 -0
- package/src/runner/rowsCommand.ts +33 -0
- package/src/runner/runSuite.ts +179 -0
- package/src/runner/testRows.ts +168 -0
- package/src/server/allocated.ts +56 -0
- package/src/server/bytes.ts +21 -0
- package/src/server/closures.ts +18 -0
- package/src/server/index.ts +16 -0
- package/src/server/liveCounts.ts +40 -0
- package/src/server/retained.ts +41 -0
- package/src/server/ticks.ts +34 -0
- package/README.md +0 -3
|
@@ -0,0 +1,559 @@
|
|
|
1
|
+
// THE PATCH SET, and the four rules that make it mean the same thing in both
|
|
2
|
+
// substrates. The list is enumerated from the DOM INTERFACES rather than from the
|
|
3
|
+
// mutators this repo happens to use, and the rules are worth more than the list.
|
|
4
|
+
//
|
|
5
|
+
// *Patch the interface that OWNS the member.* `textContent` is defined on `Node`, and
|
|
6
|
+
// happy-dom ALSO defines own descriptors for it on `Element` and `CharacterData`. So
|
|
7
|
+
// a declaration naming only `Node` counts an element's write in a browser and misses
|
|
8
|
+
// it under happy-dom, where the subclass shadows; naming only the subclass does the
|
|
9
|
+
// reverse. The declaration names every interface the member can be reached through,
|
|
10
|
+
// and the installer shadows where the named interface does not own it — so one write
|
|
11
|
+
// crosses exactly one patched member in both places.
|
|
12
|
+
//
|
|
13
|
+
// *Installed once, armed per case.* Removing a patch around a case body cannot be
|
|
14
|
+
// done in the browser arm at all — `addInitScript` runs at document-start and has no
|
|
15
|
+
// removal hook — and re-patching per case invalidates the inline caches on those
|
|
16
|
+
// members once per case, for the process. So the prototype shape is fixed for the
|
|
17
|
+
// process lifetime and `arm()`/`disarm()` swap the live record for a discard.
|
|
18
|
+
//
|
|
19
|
+
// *An unpatched mutator is a THROW, not a zero.* A reconcile written with
|
|
20
|
+
// `node.normalize()` against a patch set that only knows `removeChild` would report
|
|
21
|
+
// `elementsMoved: 0` and turn every RENDERER gate green. The list below is what is
|
|
22
|
+
// COUNTED; the refused list is everything else that mutates, and it fails loudly.
|
|
23
|
+
//
|
|
24
|
+
// *Count the OUTERMOST patched call only.* happy-dom implements the aggregate members
|
|
25
|
+
// in JavaScript on top of the primitives this patches; Chromium implements them in
|
|
26
|
+
// C++ where no JS-visible call happens. Measured: `textContent = ''` on a node with
|
|
27
|
+
// two children reports 2 `removeChild` calls under happy-dom and 0 under Chromium. So
|
|
28
|
+
// the outermost armed call tallies and the nested ones tally nothing.
|
|
29
|
+
|
|
30
|
+
import type { Work } from './Work.ts'
|
|
31
|
+
|
|
32
|
+
type Role =
|
|
33
|
+
| 'insertArgs'
|
|
34
|
+
| 'insertFirstArg'
|
|
35
|
+
| 'insertSecondArg'
|
|
36
|
+
| 'removeArgs'
|
|
37
|
+
| 'removeReceiver'
|
|
38
|
+
| 'replaceChild'
|
|
39
|
+
| 'replaceReceiver'
|
|
40
|
+
| 'replaceChildren'
|
|
41
|
+
| 'clone'
|
|
42
|
+
| 'create'
|
|
43
|
+
| 'attribute'
|
|
44
|
+
| 'classWrite'
|
|
45
|
+
| 'styleWrite'
|
|
46
|
+
| 'styleObject'
|
|
47
|
+
| 'textContent'
|
|
48
|
+
| 'html'
|
|
49
|
+
| 'data'
|
|
50
|
+
| 'listener'
|
|
51
|
+
| 'refused'
|
|
52
|
+
|
|
53
|
+
// Every interface a declared member can be REACHED through, not only the one the
|
|
54
|
+
// specification hangs it off. `patchSet()` reports this list back and the
|
|
55
|
+
// both-substrates gate compares the two.
|
|
56
|
+
const DECLARED: [interfaceName: string, member: string, role: Role][] = [
|
|
57
|
+
['Node', 'insertBefore', 'insertFirstArg'],
|
|
58
|
+
['Node', 'appendChild', 'insertArgs'],
|
|
59
|
+
['Node', 'removeChild', 'removeArgs'],
|
|
60
|
+
['Node', 'replaceChild', 'replaceChild'],
|
|
61
|
+
['Node', 'cloneNode', 'clone'],
|
|
62
|
+
['Node', 'textContent', 'textContent'],
|
|
63
|
+
['Element', 'textContent', 'textContent'],
|
|
64
|
+
['CharacterData', 'textContent', 'textContent'],
|
|
65
|
+
['Element', 'remove', 'removeReceiver'],
|
|
66
|
+
['Element', 'before', 'insertArgs'],
|
|
67
|
+
['Element', 'after', 'insertArgs'],
|
|
68
|
+
['Element', 'replaceWith', 'replaceReceiver'],
|
|
69
|
+
['CharacterData', 'remove', 'removeReceiver'],
|
|
70
|
+
['CharacterData', 'before', 'insertArgs'],
|
|
71
|
+
['CharacterData', 'after', 'insertArgs'],
|
|
72
|
+
['CharacterData', 'replaceWith', 'replaceReceiver'],
|
|
73
|
+
['Element', 'append', 'insertArgs'],
|
|
74
|
+
['Element', 'prepend', 'insertArgs'],
|
|
75
|
+
['Element', 'replaceChildren', 'replaceChildren'],
|
|
76
|
+
// A KEYED REORDER IN CHROMIUM IS THIS CALL (`moveNode`), so leaving it off read
|
|
77
|
+
// every reorder there as 0 moves. One node moves, as with `insertBefore`.
|
|
78
|
+
['Element', 'moveBefore', 'insertFirstArg'],
|
|
79
|
+
['Document', 'moveBefore', 'insertFirstArg'],
|
|
80
|
+
['DocumentFragment', 'moveBefore', 'insertFirstArg'],
|
|
81
|
+
['Document', 'append', 'insertArgs'],
|
|
82
|
+
['Document', 'prepend', 'insertArgs'],
|
|
83
|
+
['Document', 'replaceChildren', 'replaceChildren'],
|
|
84
|
+
['Element', 'setAttribute', 'attribute'],
|
|
85
|
+
['Element', 'removeAttribute', 'attribute'],
|
|
86
|
+
['Element', 'toggleAttribute', 'attribute'],
|
|
87
|
+
['Element', 'className', 'attribute'],
|
|
88
|
+
['Element', 'insertAdjacentElement', 'insertSecondArg'],
|
|
89
|
+
['Element', 'insertAdjacentHTML', 'html'],
|
|
90
|
+
['Element', 'innerHTML', 'html'],
|
|
91
|
+
// happy-dom gives `HTMLTemplateElement` its OWN `innerHTML`, which shadows the one
|
|
92
|
+
// above — so a `template.innerHTML` write reaches no patched member, the
|
|
93
|
+
// outermost-only guard never engages, and the parse's internal `createElementNS`
|
|
94
|
+
// surfaces as the `refused` throw instead. The compiled arm makes every row from a
|
|
95
|
+
// template, so that is the whole arm. Chromium has no own descriptor here and the
|
|
96
|
+
// installer shadows, which is how the two lists stay identical.
|
|
97
|
+
['HTMLTemplateElement', 'innerHTML', 'html'],
|
|
98
|
+
['Element', 'outerHTML', 'html'],
|
|
99
|
+
['CharacterData', 'data', 'data'],
|
|
100
|
+
['Document', 'createElement', 'create'],
|
|
101
|
+
['Document', 'createTextNode', 'create'],
|
|
102
|
+
['Document', 'createComment', 'create'],
|
|
103
|
+
['Document', 'importNode', 'create'],
|
|
104
|
+
['EventTarget', 'addEventListener', 'listener'],
|
|
105
|
+
// COUNTED RATHER THAN REFUSED: a refused member is a throw in any arm that
|
|
106
|
+
// reaches it, and `markup-class-style`, whose whole subject is class and style,
|
|
107
|
+
// does — so the answer is a counter rather than an escape hatch.
|
|
108
|
+
['DOMTokenList', 'add', 'classWrite'],
|
|
109
|
+
['DOMTokenList', 'remove', 'classWrite'],
|
|
110
|
+
['DOMTokenList', 'toggle', 'classWrite'],
|
|
111
|
+
['DOMTokenList', 'replace', 'classWrite'],
|
|
112
|
+
// THE ONE MEMBER INTERCEPTED ON READ, and it is here because `style.paddingLeft =
|
|
113
|
+
// x` never reaches `setProperty`: it is an own accessor on `CSSStyleDeclaration`
|
|
114
|
+
// per CSS property, hundreds of them, and enumerating those at install would make
|
|
115
|
+
// the patch set a different list in each substrate — which 43.5 forbids and is the
|
|
116
|
+
// one thing the byte-identical gate exists to catch. So the getter hands back a
|
|
117
|
+
// counting proxy while armed and the real declaration otherwise, and every write
|
|
118
|
+
// is counted without naming a single property. `HTMLElement.style` resolves at the
|
|
119
|
+
// same depth with stable identity in happy-dom, chromium and webkit alike.
|
|
120
|
+
['HTMLElement', 'style', 'styleObject'],
|
|
121
|
+
['CSSStyleDeclaration', 'setProperty', 'styleWrite'],
|
|
122
|
+
['CSSStyleDeclaration', 'removeProperty', 'styleWrite'],
|
|
123
|
+
['CSSStyleDeclaration', 'cssText', 'styleWrite'],
|
|
124
|
+
// Everything below MUTATES and is not counted, so it is a loud failure rather
|
|
125
|
+
// than a silent zero. A member arriving here is the moment somebody decides
|
|
126
|
+
// whether it earns a counter.
|
|
127
|
+
['Node', 'normalize', 'refused'],
|
|
128
|
+
['Node', 'nodeValue', 'refused'],
|
|
129
|
+
['Element', 'insertAdjacentText', 'refused'],
|
|
130
|
+
['Element', 'setAttributeNS', 'refused'],
|
|
131
|
+
['Element', 'removeAttributeNS', 'refused'],
|
|
132
|
+
['Element', 'setAttributeNode', 'refused'],
|
|
133
|
+
['Element', 'removeAttributeNode', 'refused'],
|
|
134
|
+
['Element', 'id', 'refused'],
|
|
135
|
+
['CharacterData', 'appendData', 'refused'],
|
|
136
|
+
['CharacterData', 'insertData', 'refused'],
|
|
137
|
+
['CharacterData', 'deleteData', 'refused'],
|
|
138
|
+
['CharacterData', 'replaceData', 'refused'],
|
|
139
|
+
['Text', 'splitText', 'refused'],
|
|
140
|
+
['Document', 'write', 'refused'],
|
|
141
|
+
['Document', 'createElementNS', 'refused'],
|
|
142
|
+
]
|
|
143
|
+
|
|
144
|
+
/** A fresh `Work` with every counted row at 0 and the three published rows at `null`. */
|
|
145
|
+
export function blankWork(): Work {
|
|
146
|
+
// Fixed shape at construction — every field initialized, `undefined` included —
|
|
147
|
+
// because this object is read on every counted call.
|
|
148
|
+
return {
|
|
149
|
+
elementsMoved: 0,
|
|
150
|
+
markersMoved: 0,
|
|
151
|
+
textMoved: 0,
|
|
152
|
+
nodesCreated: 0,
|
|
153
|
+
attributesSet: 0,
|
|
154
|
+
classWrites: 0,
|
|
155
|
+
styleWrites: 0,
|
|
156
|
+
dataWrites: 0,
|
|
157
|
+
redundantDataWrites: 0,
|
|
158
|
+
listenersBound: 0,
|
|
159
|
+
// EMPTY, not zeroed — a source nobody published is absent (43.19). Replaced whole
|
|
160
|
+
// at disarm, so the shape is the same object type at every read.
|
|
161
|
+
published: {},
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const DISCARD = blankWork()
|
|
166
|
+
let live: Work = DISCARD
|
|
167
|
+
let depth = 0
|
|
168
|
+
let armed = false
|
|
169
|
+
let installed: string[] = []
|
|
170
|
+
|
|
171
|
+
/** Routes every counted call into `record` until `disarm()`. Throws when already armed. */
|
|
172
|
+
export function arm(record: Work): void {
|
|
173
|
+
if (armed)
|
|
174
|
+
throw new Error(
|
|
175
|
+
'measure() is already armed. Counting passes do not nest: the inner record would take the outer one’s calls.',
|
|
176
|
+
)
|
|
177
|
+
armed = true
|
|
178
|
+
live = record
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Stops counting: calls go back to a discarded record. */
|
|
182
|
+
export function disarm(): void {
|
|
183
|
+
armed = false
|
|
184
|
+
live = DISCARD
|
|
185
|
+
styleProxies = new WeakMap()
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export function isArmed(): boolean {
|
|
189
|
+
return armed
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* The installed patch list, as `Interface.member` strings.
|
|
194
|
+
*
|
|
195
|
+
* The both-substrates gate is asserted on THIS rather than on a count, because a count is what
|
|
196
|
+
* goes quietly to zero.
|
|
197
|
+
*/
|
|
198
|
+
export function patchSet(): string[] {
|
|
199
|
+
return installed.slice()
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const ELEMENT_NODE = 1
|
|
203
|
+
const TEXT_NODE = 3
|
|
204
|
+
const COMMENT_NODE = 8
|
|
205
|
+
const DOCUMENT_FRAGMENT_NODE = 11
|
|
206
|
+
|
|
207
|
+
function tallyNode(node: unknown): void {
|
|
208
|
+
if (typeof node === 'string') {
|
|
209
|
+
// A string handed to `append` becomes a text node the call created.
|
|
210
|
+
live.nodesCreated += 1
|
|
211
|
+
live.textMoved += 1
|
|
212
|
+
return
|
|
213
|
+
}
|
|
214
|
+
const kind = (node as Node | null)?.nodeType
|
|
215
|
+
if (kind === ELEMENT_NODE) live.elementsMoved += 1
|
|
216
|
+
else if (kind === TEXT_NODE) live.textMoved += 1
|
|
217
|
+
else if (kind === COMMENT_NODE) live.markersMoved += 1
|
|
218
|
+
else if (kind === DOCUMENT_FRAGMENT_NODE)
|
|
219
|
+
for (const child of (node as DocumentFragment).childNodes)
|
|
220
|
+
tallyNode(child)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function tallyChildren(parent: unknown): void {
|
|
224
|
+
const children = (parent as Node | null)?.childNodes
|
|
225
|
+
if (!children) return
|
|
226
|
+
for (const child of children) tallyNode(child)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Counts a subtree the call brought into existence, whole and INCLUDING its root.
|
|
231
|
+
*
|
|
232
|
+
* `innerHTML` creates every node under it, where an insert only MOVES the one node it was handed.
|
|
233
|
+
*/
|
|
234
|
+
function tallyCreatedSubtree(node: unknown): void {
|
|
235
|
+
live.nodesCreated += 1
|
|
236
|
+
const children = (node as Node | null)?.childNodes
|
|
237
|
+
if (!children) return
|
|
238
|
+
for (const child of children) tallyCreatedSubtree(child)
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// WHICH CONTAINER AN `html` WRITE LANDS IN, and which of its children were already
|
|
242
|
+
// there. The created nodes are the ones that were NOT — rooted at the container
|
|
243
|
+
// wholesale, every pre-existing sibling of an `insertAdjacentHTML` reads as created,
|
|
244
|
+
// which measured 7 created and 6 moved for a one-element insert beside five siblings.
|
|
245
|
+
// The set is also what lets `outerHTML` report anything at all: the receiver is
|
|
246
|
+
// detached by the time the write returns, so the replacement is only reachable through
|
|
247
|
+
// the parent it landed in.
|
|
248
|
+
const BESIDE_THE_RECEIVER = new Set(['beforebegin', 'afterend'])
|
|
249
|
+
let htmlContainer: Node | null = null
|
|
250
|
+
const htmlChildrenBefore = new Set<Node>()
|
|
251
|
+
|
|
252
|
+
function containerForHtml(
|
|
253
|
+
receiver: unknown,
|
|
254
|
+
member: string,
|
|
255
|
+
position: unknown,
|
|
256
|
+
) {
|
|
257
|
+
const element = receiver as Element
|
|
258
|
+
// A `<template>`'s `innerHTML` writes into its `content` fragment rather than into
|
|
259
|
+
// the element's own children, and the compiled arm makes every row from one.
|
|
260
|
+
if (member === 'innerHTML')
|
|
261
|
+
return (
|
|
262
|
+
((element as HTMLTemplateElement).content as Node | undefined) ??
|
|
263
|
+
element
|
|
264
|
+
)
|
|
265
|
+
if (member === 'outerHTML') return element.parentNode
|
|
266
|
+
return BESIDE_THE_RECEIVER.has(String(position).toLowerCase())
|
|
267
|
+
? element.parentNode
|
|
268
|
+
: element
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function isCharacterData(node: unknown): boolean {
|
|
272
|
+
const kind = (node as Node | null)?.nodeType
|
|
273
|
+
return kind === TEXT_NODE || kind === COMMENT_NODE
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function tallyBefore(
|
|
277
|
+
role: Role,
|
|
278
|
+
receiver: unknown,
|
|
279
|
+
args: unknown[],
|
|
280
|
+
member: string,
|
|
281
|
+
): void {
|
|
282
|
+
switch (role) {
|
|
283
|
+
case 'insertArgs':
|
|
284
|
+
for (const argument of args) tallyNode(argument)
|
|
285
|
+
return
|
|
286
|
+
case 'insertFirstArg':
|
|
287
|
+
// `insertBefore(node, reference)` moves one node; the reference is where,
|
|
288
|
+
// not what. Counting both is how a two-row swap reported 4 moves.
|
|
289
|
+
tallyNode(args[0])
|
|
290
|
+
return
|
|
291
|
+
case 'insertSecondArg':
|
|
292
|
+
tallyNode(args[1])
|
|
293
|
+
return
|
|
294
|
+
case 'removeArgs':
|
|
295
|
+
for (const argument of args) tallyNode(argument)
|
|
296
|
+
return
|
|
297
|
+
case 'removeReceiver':
|
|
298
|
+
tallyNode(receiver)
|
|
299
|
+
return
|
|
300
|
+
case 'replaceChild':
|
|
301
|
+
tallyNode(args[0])
|
|
302
|
+
tallyNode(args[1])
|
|
303
|
+
return
|
|
304
|
+
case 'replaceReceiver':
|
|
305
|
+
tallyNode(receiver)
|
|
306
|
+
for (const argument of args) tallyNode(argument)
|
|
307
|
+
return
|
|
308
|
+
case 'replaceChildren':
|
|
309
|
+
tallyChildren(receiver)
|
|
310
|
+
for (const argument of args) tallyNode(argument)
|
|
311
|
+
return
|
|
312
|
+
case 'clone':
|
|
313
|
+
case 'create':
|
|
314
|
+
live.nodesCreated += 1
|
|
315
|
+
return
|
|
316
|
+
case 'attribute':
|
|
317
|
+
live.attributesSet += 1
|
|
318
|
+
return
|
|
319
|
+
case 'classWrite':
|
|
320
|
+
live.classWrites += 1
|
|
321
|
+
return
|
|
322
|
+
case 'styleWrite':
|
|
323
|
+
live.styleWrites += 1
|
|
324
|
+
return
|
|
325
|
+
case 'styleObject':
|
|
326
|
+
// Counted by the proxy the getter hands back, not here.
|
|
327
|
+
return
|
|
328
|
+
case 'data': {
|
|
329
|
+
live.dataWrites += 1
|
|
330
|
+
if ((receiver as CharacterData).data === String(args[0]))
|
|
331
|
+
live.redundantDataWrites += 1
|
|
332
|
+
return
|
|
333
|
+
}
|
|
334
|
+
case 'textContent': {
|
|
335
|
+
// On a text node or a comment this IS a data write, and counting it as a
|
|
336
|
+
// subtree replacement would report a creation that never happened.
|
|
337
|
+
if (isCharacterData(receiver)) {
|
|
338
|
+
live.dataWrites += 1
|
|
339
|
+
if ((receiver as CharacterData).data === String(args[0]))
|
|
340
|
+
live.redundantDataWrites += 1
|
|
341
|
+
return
|
|
342
|
+
}
|
|
343
|
+
tallyChildren(receiver)
|
|
344
|
+
if (args[0] !== '' && args[0] !== null && args[0] !== undefined) {
|
|
345
|
+
live.nodesCreated += 1
|
|
346
|
+
live.textMoved += 1
|
|
347
|
+
}
|
|
348
|
+
return
|
|
349
|
+
}
|
|
350
|
+
case 'html': {
|
|
351
|
+
const container = containerForHtml(receiver, member, args[0])
|
|
352
|
+
htmlContainer = container
|
|
353
|
+
htmlChildrenBefore.clear()
|
|
354
|
+
if (container)
|
|
355
|
+
for (const child of container.childNodes)
|
|
356
|
+
htmlChildrenBefore.add(child)
|
|
357
|
+
// What the write REMOVES is a move: `innerHTML` drops the receiver's
|
|
358
|
+
// children, `outerHTML` drops the receiver itself, `insertAdjacentHTML`
|
|
359
|
+
// drops nothing.
|
|
360
|
+
if (member === 'innerHTML') tallyChildren(container)
|
|
361
|
+
else if (member === 'outerHTML') tallyNode(receiver)
|
|
362
|
+
return
|
|
363
|
+
}
|
|
364
|
+
case 'listener':
|
|
365
|
+
live.listenersBound += 1
|
|
366
|
+
return
|
|
367
|
+
case 'refused':
|
|
368
|
+
throw new Error(
|
|
369
|
+
`${member}() is a DOM mutation harness/measure does not count. A count it does not take reads 0 forever, and every gate over it is green forever — give it a counter or keep it out of the path.`,
|
|
370
|
+
)
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function tallyAfter(role: Role): void {
|
|
375
|
+
if (role !== 'html') return
|
|
376
|
+
const container = htmlContainer
|
|
377
|
+
htmlContainer = null
|
|
378
|
+
if (container)
|
|
379
|
+
for (const child of container.childNodes) {
|
|
380
|
+
if (htmlChildrenBefore.has(child)) continue
|
|
381
|
+
// Every node the parse produced is created, and the top of each one is
|
|
382
|
+
// also inserted — the same pair `createElement` + `appendChild` reports.
|
|
383
|
+
tallyNode(child)
|
|
384
|
+
tallyCreatedSubtree(child)
|
|
385
|
+
}
|
|
386
|
+
htmlChildrenBefore.clear()
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// Per armed case, so a proxy never outlives the record it counts into and `el.style`
|
|
390
|
+
// keeps one identity for the length of a case body.
|
|
391
|
+
let styleProxies = new WeakMap<object, object>()
|
|
392
|
+
|
|
393
|
+
function countingStyle(declaration: object): object {
|
|
394
|
+
const held = styleProxies.get(declaration)
|
|
395
|
+
if (held) return held
|
|
396
|
+
const proxy = new Proxy(declaration, {
|
|
397
|
+
get(target, key) {
|
|
398
|
+
const value = Reflect.get(target, key)
|
|
399
|
+
// A native CSSOM method rejects a Proxy as its `this` — "Illegal
|
|
400
|
+
// invocation" in chromium — so it comes back bound to the real
|
|
401
|
+
// declaration. `setProperty` is patched on the prototype and counts
|
|
402
|
+
// itself, so binding does not lose it.
|
|
403
|
+
return typeof value === 'function' ? value.bind(target) : value
|
|
404
|
+
},
|
|
405
|
+
set(target, key, value) {
|
|
406
|
+
// The trap TAKES the depth rather than only reading it. happy-dom
|
|
407
|
+
// implements a property write over its own `setProperty`, which is
|
|
408
|
+
// patched — measured, one `style.paddingLeft =` counted twice, and it
|
|
409
|
+
// counted once in chromium where the property is native. That is the
|
|
410
|
+
// textContent divergence again, one layer down.
|
|
411
|
+
if (depth > 0 || !armed)
|
|
412
|
+
return Reflect.set(target, key, value, target)
|
|
413
|
+
depth = 1
|
|
414
|
+
try {
|
|
415
|
+
live.styleWrites += 1
|
|
416
|
+
return Reflect.set(target, key, value, target)
|
|
417
|
+
} finally {
|
|
418
|
+
depth = 0
|
|
419
|
+
}
|
|
420
|
+
},
|
|
421
|
+
})
|
|
422
|
+
styleProxies.set(declaration, proxy)
|
|
423
|
+
return proxy
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
type AnyFunction = (this: unknown, ...args: unknown[]) => unknown
|
|
427
|
+
|
|
428
|
+
function counting(
|
|
429
|
+
role: Role,
|
|
430
|
+
member: string,
|
|
431
|
+
original: AnyFunction,
|
|
432
|
+
): AnyFunction {
|
|
433
|
+
return function (this: unknown, ...args: unknown[]): unknown {
|
|
434
|
+
// Nested inside an already-counted call: the outermost one has already said
|
|
435
|
+
// what this op did.
|
|
436
|
+
if (depth > 0) return original.apply(this, args)
|
|
437
|
+
if (!armed) return original.apply(this, args)
|
|
438
|
+
depth = 1
|
|
439
|
+
try {
|
|
440
|
+
tallyBefore(role, this, args, member)
|
|
441
|
+
const answer = original.apply(this, args)
|
|
442
|
+
tallyAfter(role)
|
|
443
|
+
return answer
|
|
444
|
+
} finally {
|
|
445
|
+
depth = 0
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
function prototypeNamed(
|
|
451
|
+
instance: object,
|
|
452
|
+
interfaceName: string,
|
|
453
|
+
): object | null {
|
|
454
|
+
let proto: object | null = Object.getPrototypeOf(instance)
|
|
455
|
+
while (proto) {
|
|
456
|
+
if (
|
|
457
|
+
(proto as { constructor?: { name?: string } }).constructor?.name ===
|
|
458
|
+
interfaceName
|
|
459
|
+
)
|
|
460
|
+
return proto
|
|
461
|
+
proto = Object.getPrototypeOf(proto)
|
|
462
|
+
}
|
|
463
|
+
return null
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
function descriptorFrom(
|
|
467
|
+
proto: object,
|
|
468
|
+
member: string,
|
|
469
|
+
): PropertyDescriptor | undefined {
|
|
470
|
+
let walk: object | null = proto
|
|
471
|
+
while (walk) {
|
|
472
|
+
const found = Object.getOwnPropertyDescriptor(walk, member)
|
|
473
|
+
if (found) return found
|
|
474
|
+
walk = Object.getPrototypeOf(walk)
|
|
475
|
+
}
|
|
476
|
+
return undefined
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* Patches every declared DOM member to count into the armed record. Idempotent.
|
|
481
|
+
*
|
|
482
|
+
* Throws when there is no DOM, or when a declared member is missing in this substrate.
|
|
483
|
+
*/
|
|
484
|
+
export function install(): void {
|
|
485
|
+
if (installed.length > 0) return
|
|
486
|
+
if (typeof document === 'undefined')
|
|
487
|
+
throw new Error(
|
|
488
|
+
'harness/measure needs a DOM at install time. Under bun that is the preload; in a browser it is `addInitScript`.',
|
|
489
|
+
)
|
|
490
|
+
|
|
491
|
+
const element = document.createElement('div')
|
|
492
|
+
const text = document.createTextNode('')
|
|
493
|
+
const instances: Record<string, object> = {
|
|
494
|
+
Node: element,
|
|
495
|
+
Element: element,
|
|
496
|
+
HTMLElement: element,
|
|
497
|
+
EventTarget: element,
|
|
498
|
+
CharacterData: text,
|
|
499
|
+
Text: text,
|
|
500
|
+
Document: document,
|
|
501
|
+
DOMTokenList: element.classList,
|
|
502
|
+
CSSStyleDeclaration: element.style,
|
|
503
|
+
HTMLTemplateElement: document.createElement('template'),
|
|
504
|
+
DocumentFragment: document.createDocumentFragment(),
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
const taken: string[] = []
|
|
508
|
+
for (const [interfaceName, member, role] of DECLARED) {
|
|
509
|
+
const instance = instances[interfaceName]
|
|
510
|
+
if (!instance)
|
|
511
|
+
throw new Error(
|
|
512
|
+
`harness/measure declares ${interfaceName}.${member} and has no instance to resolve ${interfaceName} from.`,
|
|
513
|
+
)
|
|
514
|
+
const proto = prototypeNamed(instance, interfaceName)
|
|
515
|
+
if (!proto)
|
|
516
|
+
throw new Error(
|
|
517
|
+
`harness/measure declares ${interfaceName}.${member} and this substrate has no prototype named ${interfaceName}.`,
|
|
518
|
+
)
|
|
519
|
+
const descriptor = descriptorFrom(proto, member)
|
|
520
|
+
// 43.5 — A MEMBER ONE SUBSTRATE LACKS IS LEFT ABSENT, and listed: happy-dom and WebKit have
|
|
521
|
+
// no `moveBefore`, and supplying one would turn every measured page's feature probe to the
|
|
522
|
+
// path it never takes there. Absent, nothing calls it, so it counts nothing — and the
|
|
523
|
+
// `insertBefore` a probe falls back to counts the one insert instead.
|
|
524
|
+
if (!descriptor && member === 'moveBefore') {
|
|
525
|
+
taken.push(`${interfaceName}.${member}`)
|
|
526
|
+
continue
|
|
527
|
+
}
|
|
528
|
+
if (!descriptor)
|
|
529
|
+
throw new Error(
|
|
530
|
+
`harness/measure declares ${interfaceName}.${member} and this substrate does not have it. The patch set has to be the same list in both substrates.`,
|
|
531
|
+
)
|
|
532
|
+
if (role === 'styleObject') {
|
|
533
|
+
const original = descriptor.get as () => object
|
|
534
|
+
Object.defineProperty(proto, member, {
|
|
535
|
+
...descriptor,
|
|
536
|
+
get(this: object): object {
|
|
537
|
+
const declaration = original.call(this)
|
|
538
|
+
return armed ? countingStyle(declaration) : declaration
|
|
539
|
+
},
|
|
540
|
+
})
|
|
541
|
+
} else if (descriptor.set) {
|
|
542
|
+
Object.defineProperty(proto, member, {
|
|
543
|
+
...descriptor,
|
|
544
|
+
set: counting(role, member, descriptor.set as AnyFunction),
|
|
545
|
+
})
|
|
546
|
+
} else if (typeof descriptor.value === 'function') {
|
|
547
|
+
Object.defineProperty(proto, member, {
|
|
548
|
+
...descriptor,
|
|
549
|
+
value: counting(role, member, descriptor.value as AnyFunction),
|
|
550
|
+
})
|
|
551
|
+
} else {
|
|
552
|
+
throw new Error(
|
|
553
|
+
`harness/measure declares ${interfaceName}.${member}, which is neither a setter nor a method here.`,
|
|
554
|
+
)
|
|
555
|
+
}
|
|
556
|
+
taken.push(`${interfaceName}.${member}`)
|
|
557
|
+
}
|
|
558
|
+
installed = taken
|
|
559
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// A CASE IS ARMED AND DISARMED, and `measure()` is those two around a body.
|
|
2
|
+
|
|
3
|
+
import { arm, blankWork, disarm } from './install.ts'
|
|
4
|
+
import { PUBLISHED_WORK_KEY, type PublishedWork } from './PUBLISHED_WORK.ts'
|
|
5
|
+
import type { Work } from './Work.ts'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Every record published on the global, or null when nothing is.
|
|
9
|
+
*
|
|
10
|
+
* READ RATHER THAN IMPORTED. A prototype patch cannot see an effect rerun and this lane may not
|
|
11
|
+
* import the subject to count one, so the subject puts fixed-shape records on the global and the
|
|
12
|
+
* lane zeroes them when a case opens and copies them when it closes. Reading a global is not an
|
|
13
|
+
* import edge, so the lane-isolation gate stays green.
|
|
14
|
+
*/
|
|
15
|
+
function sources(): PublishedWork | null {
|
|
16
|
+
const record = (globalThis as Record<string, unknown>)[PUBLISHED_WORK_KEY]
|
|
17
|
+
if (record === undefined || record === null) return null
|
|
18
|
+
return record as PublishedWork
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
let openCase: Work | null = null
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Opens a case: starts counting DOM calls and zeroes every published counter.
|
|
25
|
+
*
|
|
26
|
+
* ONLY A FIELD THAT IS THERE AND IS A NUMBER IS ZEROED. Writing a field the source never declared
|
|
27
|
+
* would create it at 0 and read it back at 0, and every gate asserting it would pass against a
|
|
28
|
+
* counter nothing increments — so a missing field stays missing, and `publishedBy` names it.
|
|
29
|
+
*/
|
|
30
|
+
function armCase(): void {
|
|
31
|
+
const record = blankWork()
|
|
32
|
+
const published = sources()
|
|
33
|
+
if (published)
|
|
34
|
+
for (const source in published) {
|
|
35
|
+
const counters = published[source] as Record<string, unknown>
|
|
36
|
+
for (const field in counters)
|
|
37
|
+
if (typeof counters[field] === 'number') counters[field] = 0
|
|
38
|
+
}
|
|
39
|
+
openCase = record
|
|
40
|
+
arm(record)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Closes the open case and returns its `Work`, with every published source copied in. */
|
|
44
|
+
function disarmCase(): Work {
|
|
45
|
+
const record = openCase
|
|
46
|
+
if (!record) throw new Error('disarmCase() was called with no case armed.')
|
|
47
|
+
disarm()
|
|
48
|
+
openCase = null
|
|
49
|
+
const published = sources()
|
|
50
|
+
if (published) {
|
|
51
|
+
const copied: PublishedWork = {}
|
|
52
|
+
for (const source in published) {
|
|
53
|
+
const counters = published[source] as Record<string, unknown>
|
|
54
|
+
const numbers: Record<string, number> = {}
|
|
55
|
+
for (const field in counters) {
|
|
56
|
+
const value = counters[field]
|
|
57
|
+
if (typeof value === 'number') numbers[field] = value
|
|
58
|
+
}
|
|
59
|
+
copied[source] = numbers
|
|
60
|
+
}
|
|
61
|
+
record.published = copied
|
|
62
|
+
}
|
|
63
|
+
return record
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Runs `body` inside an armed case and returns the `Work` it did. */
|
|
67
|
+
export function measure(body: () => void): Work {
|
|
68
|
+
armCase()
|
|
69
|
+
try {
|
|
70
|
+
body()
|
|
71
|
+
} catch (error) {
|
|
72
|
+
// The case is closed before the throw leaves, or the next `measure()` reports
|
|
73
|
+
// that one is already armed and the real failure is two frames down.
|
|
74
|
+
disarmCase()
|
|
75
|
+
throw error
|
|
76
|
+
}
|
|
77
|
+
return disarmCase()
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* One source's counters off a `Work`, or null where that source published nothing (43.19).
|
|
82
|
+
*
|
|
83
|
+
* Throws naming each of `fields` the record lacks as a number (43.20): two halves of a wire that
|
|
84
|
+
* cannot share a type agree on its shape here, at the read, or a misnamed field reads as a counter
|
|
85
|
+
* that never moved.
|
|
86
|
+
*/
|
|
87
|
+
export function publishedBy<Field extends string>(
|
|
88
|
+
work: Work,
|
|
89
|
+
source: string,
|
|
90
|
+
fields: readonly Field[],
|
|
91
|
+
): Record<Field, number> | null {
|
|
92
|
+
const counters = work.published[source]
|
|
93
|
+
if (counters === undefined) return null
|
|
94
|
+
const missing: string[] = []
|
|
95
|
+
for (const field of fields)
|
|
96
|
+
if (typeof counters[field] !== 'number') missing.push(field)
|
|
97
|
+
if (missing.length > 0)
|
|
98
|
+
throw new Error(
|
|
99
|
+
`${PUBLISHED_WORK_KEY}.${source} is published without a numeric ${missing.join(', ')}. A field the reader expects and the publisher does not declare would read as a counter that never moved.`,
|
|
100
|
+
)
|
|
101
|
+
return counters as Record<Field, number>
|
|
102
|
+
}
|