0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/lib/internal/safe_globals.js
Vse Mozhet Byt f2432a4761 lib: simplify own keys retrieval
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>
2018-11-26 01:31:49 +02:00

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 {});