TestIntegrityLevel.js 1.2 KB

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