1 | 'use strict';
|
---|
2 |
|
---|
3 | var GetIntrinsic = require('get-intrinsic');
|
---|
4 |
|
---|
5 | var $SyntaxError = require('es-errors/syntax');
|
---|
6 | var $TypeError = require('es-errors/type');
|
---|
7 | var $preventExtensions = GetIntrinsic('%Object.preventExtensions%', true);
|
---|
8 | var $gOPD = require('gopd');
|
---|
9 | var $gOPN = GetIntrinsic('%Object.getOwnPropertyNames%', true);
|
---|
10 |
|
---|
11 | var forEach = require('../helpers/forEach');
|
---|
12 |
|
---|
13 | var DefinePropertyOrThrow = require('./DefinePropertyOrThrow');
|
---|
14 | var IsAccessorDescriptor = require('./IsAccessorDescriptor');
|
---|
15 | var ToPropertyDescriptor = require('./ToPropertyDescriptor');
|
---|
16 |
|
---|
17 | var isObject = require('../helpers/isObject');
|
---|
18 |
|
---|
19 | // https://262.ecma-international.org/6.0/#sec-setintegritylevel
|
---|
20 |
|
---|
21 | module.exports = function SetIntegrityLevel(O, level) {
|
---|
22 | if (!isObject(O)) {
|
---|
23 | throw new $TypeError('Assertion failed: Type(O) is not Object');
|
---|
24 | }
|
---|
25 | if (level !== 'sealed' && level !== 'frozen') {
|
---|
26 | throw new $TypeError('Assertion failed: `level` must be `"sealed"` or `"frozen"`');
|
---|
27 | }
|
---|
28 | if (!$preventExtensions) {
|
---|
29 | throw new $SyntaxError('SetIntegrityLevel requires native `Object.preventExtensions` support');
|
---|
30 | }
|
---|
31 | var status = $preventExtensions(O);
|
---|
32 | if (!status) {
|
---|
33 | return false;
|
---|
34 | }
|
---|
35 | if (!$gOPN) {
|
---|
36 | throw new $SyntaxError('SetIntegrityLevel requires native `Object.getOwnPropertyNames` support');
|
---|
37 | }
|
---|
38 | var theKeys = $gOPN(O);
|
---|
39 | if (level === 'sealed') {
|
---|
40 | forEach(theKeys, function (k) {
|
---|
41 | DefinePropertyOrThrow(O, k, { configurable: false });
|
---|
42 | });
|
---|
43 | } else if (level === 'frozen') {
|
---|
44 | forEach(theKeys, function (k) {
|
---|
45 | var currentDesc = $gOPD(O, k);
|
---|
46 | if (typeof currentDesc !== 'undefined') {
|
---|
47 | var desc;
|
---|
48 | if (IsAccessorDescriptor(ToPropertyDescriptor(currentDesc))) {
|
---|
49 | desc = { configurable: false };
|
---|
50 | } else {
|
---|
51 | desc = { configurable: false, writable: false };
|
---|
52 | }
|
---|
53 | DefinePropertyOrThrow(O, k, desc);
|
---|
54 | }
|
---|
55 | });
|
---|
56 | }
|
---|
57 | return true;
|
---|
58 | };
|
---|