2020-08-25 23:57:08 +00:00
|
|
|
'use strict';
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
var GetIntrinsic = require('get-intrinsic');
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
var $SyntaxError = GetIntrinsic('%SyntaxError%');
|
|
|
|
var $TypeError = GetIntrinsic('%TypeError%');
|
2022-11-10 10:43:16 +00:00
|
|
|
var $preventExtensions = GetIntrinsic('%Object.preventExtensions%', true);
|
2020-08-25 23:57:08 +00:00
|
|
|
var $gOPD = require('../helpers/getOwnPropertyDescriptor');
|
2022-11-10 10:43:16 +00:00
|
|
|
var $gOPN = GetIntrinsic('%Object.getOwnPropertyNames%', true);
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
var forEach = require('../helpers/forEach');
|
|
|
|
|
|
|
|
var DefinePropertyOrThrow = require('./DefinePropertyOrThrow');
|
|
|
|
var IsAccessorDescriptor = require('./IsAccessorDescriptor');
|
|
|
|
var ToPropertyDescriptor = require('./ToPropertyDescriptor');
|
|
|
|
var Type = require('./Type');
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
// https://ecma-international.org/ecma-262/6.0/#sec-setintegritylevel
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
module.exports = function SetIntegrityLevel(O, level) {
|
|
|
|
if (Type(O) !== 'Object') {
|
|
|
|
throw new $TypeError('Assertion failed: Type(O) is not Object');
|
|
|
|
}
|
|
|
|
if (level !== 'sealed' && level !== 'frozen') {
|
|
|
|
throw new $TypeError('Assertion failed: `level` must be `"sealed"` or `"frozen"`');
|
|
|
|
}
|
|
|
|
if (!$preventExtensions) {
|
|
|
|
throw new $SyntaxError('SetIntegrityLevel requires native `Object.preventExtensions` support');
|
|
|
|
}
|
|
|
|
var status = $preventExtensions(O);
|
|
|
|
if (!status) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!$gOPN) {
|
|
|
|
throw new $SyntaxError('SetIntegrityLevel requires native `Object.getOwnPropertyNames` support');
|
|
|
|
}
|
|
|
|
var theKeys = $gOPN(O);
|
|
|
|
if (level === 'sealed') {
|
|
|
|
forEach(theKeys, function (k) {
|
|
|
|
DefinePropertyOrThrow(O, k, { configurable: false });
|
|
|
|
});
|
|
|
|
} else if (level === 'frozen') {
|
|
|
|
forEach(theKeys, function (k) {
|
|
|
|
var currentDesc = $gOPD(O, k);
|
|
|
|
if (typeof currentDesc !== 'undefined') {
|
|
|
|
var desc;
|
|
|
|
if (IsAccessorDescriptor(ToPropertyDescriptor(currentDesc))) {
|
|
|
|
desc = { configurable: false };
|
|
|
|
} else {
|
|
|
|
desc = { configurable: false, writable: false };
|
|
|
|
}
|
|
|
|
DefinePropertyOrThrow(O, k, desc);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|