source: imaps-frontend/node_modules/es-abstract/2019/SetIntegrityLevel.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

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