Projekt

Obecné

Profil

Stáhnout (1.81 KB) Statistiky
| Větev: | Revize:
1
'use strict';
2

    
3
var GetIntrinsic = require('../GetIntrinsic');
4

    
5
var $SyntaxError = GetIntrinsic('%SyntaxError%');
6
var $TypeError = GetIntrinsic('%TypeError%');
7
var $preventExtensions = GetIntrinsic('%Object.preventExtensions%');
8
var $gOPD = require('../helpers/getOwnPropertyDescriptor');
9
var $gOPN = GetIntrinsic('%Object.getOwnPropertyNames%');
10

    
11
var forEach = require('../helpers/forEach');
12

    
13
var DefinePropertyOrThrow = require('./DefinePropertyOrThrow');
14
var IsAccessorDescriptor = require('./IsAccessorDescriptor');
15
var ToPropertyDescriptor = require('./ToPropertyDescriptor');
16
var Type = require('./Type');
17

    
18
// https://www.ecma-international.org/ecma-262/6.0/#sec-setintegritylevel
19

    
20
module.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
};
(78-78/115)