mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
f2432a4761
This replacement can save us a function call, two array spreadings, and an array concatenation. PR-URL: https://github.com/nodejs/node/pull/24582 Refs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/ownKeys#Description Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
25 lines
717 B
JavaScript
25 lines
717 B
JavaScript
'use strict';
|
|
|
|
const copyProps = (unsafe, safe) => {
|
|
for (const key of Reflect.ownKeys(unsafe)) {
|
|
if (!Object.getOwnPropertyDescriptor(safe, key)) {
|
|
Object.defineProperty(
|
|
safe,
|
|
key,
|
|
Object.getOwnPropertyDescriptor(unsafe, key));
|
|
}
|
|
}
|
|
};
|
|
const makeSafe = (unsafe, safe) => {
|
|
copyProps(unsafe.prototype, safe.prototype);
|
|
copyProps(unsafe, safe);
|
|
Object.setPrototypeOf(safe.prototype, null);
|
|
Object.freeze(safe.prototype);
|
|
Object.freeze(safe);
|
|
return safe;
|
|
};
|
|
|
|
exports.SafeMap = makeSafe(Map, class SafeMap extends Map {});
|
|
exports.SafeSet = makeSafe(Set, class SafeSet extends Set {});
|
|
exports.SafePromise = makeSafe(Promise, class SafePromise extends Promise {});
|