Skip to content

Commit

Permalink
perf_hooks: use spec-compliant structuredClone
Browse files Browse the repository at this point in the history
Serialize PerformanceMark's `detail` correctly.

Fixes: #40840

PR-URL: #40904
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
targos authored and danielleadams committed Dec 13, 2021
1 parent f0d8743 commit 93ea166
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 18 deletions.
3 changes: 2 additions & 1 deletion lib/internal/perf/usertiming.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ const {
},
} = require('internal/errors');

const { structuredClone, lazyDOMException } = require('internal/util');
const { structuredClone } = require('internal/structured_clone');
const { lazyDOMException } = require('internal/util');

const markTimings = new SafeMap();

Expand Down
16 changes: 0 additions & 16 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,21 +477,6 @@ const lazyDOMException = hideStackFrames((message, name) => {
return new _DOMException(message, name);
});

function structuredClone(value) {
const {
DefaultSerializer,
DefaultDeserializer,
} = require('v8');
const ser = new DefaultSerializer();
ser._getDataCloneError = hideStackFrames((message) =>
lazyDOMException(message, 'DataCloneError'));
ser.writeValue(value);
const serialized = ser.releaseBuffer();

const des = new DefaultDeserializer(serialized);
return des.readValue();
}

module.exports = {
assertCrypto,
cachedResult,
Expand All @@ -515,7 +500,6 @@ module.exports = {
promisify,
sleep,
spliceOne,
structuredClone,
toUSVString,
removeColors,

Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-perf-hooks-usertiming.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,15 @@ assert.throws(() => mark(Symbol('a')), {
assert.strictEqual(m.entryType, 'mark');
assert.strictEqual(m.detail, null);
});
[1, 'any', {}, []].forEach((detail) => {
[1, 'any', {}, [], /a/].forEach((detail) => {
const m = mark('a', { detail });
assert.strictEqual(m.name, 'a');
assert.strictEqual(m.entryType, 'mark');
// Value of detail is structured cloned.
assert.deepStrictEqual(m.detail, detail);
if (typeof detail === 'object') {
assert.notStrictEqual(m.detail, detail);
}
});

clearMarks();
Expand Down

0 comments on commit 93ea166

Please sign in to comment.