TestIntegrityLevel.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $gOPD = require('gopd');
  4. var $TypeError = GetIntrinsic('%TypeError%');
  5. var every = require('../helpers/every');
  6. var OwnPropertyKeys = require('../helpers/OwnPropertyKeys');
  7. var IsDataDescriptor = require('./IsDataDescriptor');
  8. var IsExtensible = require('./IsExtensible');
  9. var ToPropertyDescriptor = require('./ToPropertyDescriptor');
  10. var Type = require('./Type');
  11. // https://262.ecma-international.org/6.0/#sec-testintegritylevel
  12. module.exports = function TestIntegrityLevel(O, level) {
  13. if (Type(O) !== 'Object') {
  14. throw new $TypeError('Assertion failed: Type(O) is not Object');
  15. }
  16. if (level !== 'sealed' && level !== 'frozen') {
  17. throw new $TypeError('Assertion failed: `level` must be `"sealed"` or `"frozen"`');
  18. }
  19. var status = IsExtensible(O);
  20. if (status) {
  21. return false;
  22. }
  23. var theKeys = OwnPropertyKeys(O);
  24. return theKeys.length === 0 || every(theKeys, function (k) {
  25. var currentDesc = $gOPD(O, k);
  26. if (typeof currentDesc !== 'undefined') {
  27. if (currentDesc.configurable) {
  28. return false;
  29. }
  30. if (level === 'frozen' && IsDataDescriptor(ToPropertyDescriptor(currentDesc)) && currentDesc.writable) {
  31. return false;
  32. }
  33. }
  34. return true;
  35. });
  36. };