web.dom-exception.constructor.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var tryNodeRequire = require('../internals/try-node-require');
  4. var getBuiltIn = require('../internals/get-built-in');
  5. var fails = require('../internals/fails');
  6. var create = require('../internals/object-create');
  7. var createPropertyDescriptor = require('../internals/create-property-descriptor');
  8. var defineProperty = require('../internals/object-define-property').f;
  9. var defineBuiltIn = require('../internals/define-built-in');
  10. var defineBuiltInAccessor = require('../internals/define-built-in-accessor');
  11. var hasOwn = require('../internals/has-own-property');
  12. var anInstance = require('../internals/an-instance');
  13. var anObject = require('../internals/an-object');
  14. var errorToString = require('../internals/error-to-string');
  15. var normalizeStringArgument = require('../internals/normalize-string-argument');
  16. var DOMExceptionConstants = require('../internals/dom-exception-constants');
  17. var clearErrorStack = require('../internals/error-stack-clear');
  18. var InternalStateModule = require('../internals/internal-state');
  19. var DESCRIPTORS = require('../internals/descriptors');
  20. var IS_PURE = require('../internals/is-pure');
  21. var DOM_EXCEPTION = 'DOMException';
  22. var DATA_CLONE_ERR = 'DATA_CLONE_ERR';
  23. var Error = getBuiltIn('Error');
  24. // NodeJS < 17.0 does not expose `DOMException` to global
  25. var NativeDOMException = getBuiltIn(DOM_EXCEPTION) || (function () {
  26. try {
  27. // NodeJS < 15.0 does not expose `MessageChannel` to global
  28. var MessageChannel = getBuiltIn('MessageChannel') || tryNodeRequire('worker_threads').MessageChannel;
  29. // eslint-disable-next-line es/no-weak-map, unicorn/require-post-message-target-origin -- safe
  30. new MessageChannel().port1.postMessage(new WeakMap());
  31. } catch (error) {
  32. if (error.name == DATA_CLONE_ERR && error.code == 25) return error.constructor;
  33. }
  34. })();
  35. var NativeDOMExceptionPrototype = NativeDOMException && NativeDOMException.prototype;
  36. var ErrorPrototype = Error.prototype;
  37. var setInternalState = InternalStateModule.set;
  38. var getInternalState = InternalStateModule.getterFor(DOM_EXCEPTION);
  39. var HAS_STACK = 'stack' in Error(DOM_EXCEPTION);
  40. var codeFor = function (name) {
  41. return hasOwn(DOMExceptionConstants, name) && DOMExceptionConstants[name].m ? DOMExceptionConstants[name].c : 0;
  42. };
  43. var $DOMException = function DOMException() {
  44. anInstance(this, DOMExceptionPrototype);
  45. var argumentsLength = arguments.length;
  46. var message = normalizeStringArgument(argumentsLength < 1 ? undefined : arguments[0]);
  47. var name = normalizeStringArgument(argumentsLength < 2 ? undefined : arguments[1], 'Error');
  48. var code = codeFor(name);
  49. setInternalState(this, {
  50. type: DOM_EXCEPTION,
  51. name: name,
  52. message: message,
  53. code: code
  54. });
  55. if (!DESCRIPTORS) {
  56. this.name = name;
  57. this.message = message;
  58. this.code = code;
  59. }
  60. if (HAS_STACK) {
  61. var error = Error(message);
  62. error.name = DOM_EXCEPTION;
  63. defineProperty(this, 'stack', createPropertyDescriptor(1, clearErrorStack(error.stack, 1)));
  64. }
  65. };
  66. var DOMExceptionPrototype = $DOMException.prototype = create(ErrorPrototype);
  67. var createGetterDescriptor = function (get) {
  68. return { enumerable: true, configurable: true, get: get };
  69. };
  70. var getterFor = function (key) {
  71. return createGetterDescriptor(function () {
  72. return getInternalState(this)[key];
  73. });
  74. };
  75. if (DESCRIPTORS) {
  76. defineBuiltInAccessor(DOMExceptionPrototype, 'code', getterFor('code'));
  77. defineBuiltInAccessor(DOMExceptionPrototype, 'message', getterFor('message'));
  78. defineBuiltInAccessor(DOMExceptionPrototype, 'name', getterFor('name'));
  79. }
  80. defineProperty(DOMExceptionPrototype, 'constructor', createPropertyDescriptor(1, $DOMException));
  81. // FF36- DOMException is a function, but can't be constructed
  82. var INCORRECT_CONSTRUCTOR = fails(function () {
  83. return !(new NativeDOMException() instanceof Error);
  84. });
  85. // Safari 10.1 / Chrome 32- / IE8- DOMException.prototype.toString bugs
  86. var INCORRECT_TO_STRING = INCORRECT_CONSTRUCTOR || fails(function () {
  87. return ErrorPrototype.toString !== errorToString || String(new NativeDOMException(1, 2)) !== '2: 1';
  88. });
  89. // Deno 1.6.3- DOMException.prototype.code just missed
  90. var INCORRECT_CODE = INCORRECT_CONSTRUCTOR || fails(function () {
  91. return new NativeDOMException(1, 'DataCloneError').code !== 25;
  92. });
  93. // Deno 1.6.3- DOMException constants just missed
  94. var MISSED_CONSTANTS = INCORRECT_CONSTRUCTOR
  95. || NativeDOMException[DATA_CLONE_ERR] !== 25
  96. || NativeDOMExceptionPrototype[DATA_CLONE_ERR] !== 25;
  97. var FORCED_CONSTRUCTOR = IS_PURE ? INCORRECT_TO_STRING || INCORRECT_CODE || MISSED_CONSTANTS : INCORRECT_CONSTRUCTOR;
  98. // `DOMException` constructor
  99. // https://webidl.spec.whatwg.org/#idl-DOMException
  100. $({ global: true, constructor: true, forced: FORCED_CONSTRUCTOR }, {
  101. DOMException: FORCED_CONSTRUCTOR ? $DOMException : NativeDOMException
  102. });
  103. var PolyfilledDOMException = getBuiltIn(DOM_EXCEPTION);
  104. var PolyfilledDOMExceptionPrototype = PolyfilledDOMException.prototype;
  105. if (INCORRECT_TO_STRING && (IS_PURE || NativeDOMException === PolyfilledDOMException)) {
  106. defineBuiltIn(PolyfilledDOMExceptionPrototype, 'toString', errorToString);
  107. }
  108. if (INCORRECT_CODE && DESCRIPTORS && NativeDOMException === PolyfilledDOMException) {
  109. defineBuiltInAccessor(PolyfilledDOMExceptionPrototype, 'code', createGetterDescriptor(function () {
  110. return codeFor(anObject(this).name);
  111. }));
  112. }
  113. for (var key in DOMExceptionConstants) if (hasOwn(DOMExceptionConstants, key)) {
  114. var constant = DOMExceptionConstants[key];
  115. var constantName = constant.s;
  116. var descriptor = createPropertyDescriptor(6, constant.c);
  117. if (!hasOwn(PolyfilledDOMException, constantName)) {
  118. defineProperty(PolyfilledDOMException, constantName, descriptor);
  119. }
  120. if (!hasOwn(PolyfilledDOMExceptionPrototype, constantName)) {
  121. defineProperty(PolyfilledDOMExceptionPrototype, constantName, descriptor);
  122. }
  123. }