web.dom-exception.stack.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var globalThis = require('../internals/global-this');
  4. var getBuiltIn = require('../internals/get-built-in');
  5. var createPropertyDescriptor = require('../internals/create-property-descriptor');
  6. var defineProperty = require('../internals/object-define-property').f;
  7. var hasOwn = require('../internals/has-own-property');
  8. var anInstance = require('../internals/an-instance');
  9. var inheritIfRequired = require('../internals/inherit-if-required');
  10. var normalizeStringArgument = require('../internals/normalize-string-argument');
  11. var DOMExceptionConstants = require('../internals/dom-exception-constants');
  12. var clearErrorStack = require('../internals/error-stack-clear');
  13. var DESCRIPTORS = require('../internals/descriptors');
  14. var IS_PURE = require('../internals/is-pure');
  15. var DOM_EXCEPTION = 'DOMException';
  16. var Error = getBuiltIn('Error');
  17. var NativeDOMException = getBuiltIn(DOM_EXCEPTION);
  18. var $DOMException = function DOMException() {
  19. anInstance(this, DOMExceptionPrototype);
  20. var argumentsLength = arguments.length;
  21. var message = normalizeStringArgument(argumentsLength < 1 ? undefined : arguments[0]);
  22. var name = normalizeStringArgument(argumentsLength < 2 ? undefined : arguments[1], 'Error');
  23. var that = new NativeDOMException(message, name);
  24. var error = new Error(message);
  25. error.name = DOM_EXCEPTION;
  26. defineProperty(that, 'stack', createPropertyDescriptor(1, clearErrorStack(error.stack, 1)));
  27. inheritIfRequired(that, this, $DOMException);
  28. return that;
  29. };
  30. var DOMExceptionPrototype = $DOMException.prototype = NativeDOMException.prototype;
  31. var ERROR_HAS_STACK = 'stack' in new Error(DOM_EXCEPTION);
  32. var DOM_EXCEPTION_HAS_STACK = 'stack' in new NativeDOMException(1, 2);
  33. // eslint-disable-next-line es/no-object-getownpropertydescriptor -- safe
  34. var descriptor = NativeDOMException && DESCRIPTORS && Object.getOwnPropertyDescriptor(globalThis, DOM_EXCEPTION);
  35. // Bun ~ 0.1.1 DOMException have incorrect descriptor and we can't redefine it
  36. // https://github.com/Jarred-Sumner/bun/issues/399
  37. var BUGGY_DESCRIPTOR = !!descriptor && !(descriptor.writable && descriptor.configurable);
  38. var FORCED_CONSTRUCTOR = ERROR_HAS_STACK && !BUGGY_DESCRIPTOR && !DOM_EXCEPTION_HAS_STACK;
  39. // `DOMException` constructor patch for `.stack` where it's required
  40. // https://webidl.spec.whatwg.org/#es-DOMException-specialness
  41. $({ global: true, constructor: true, forced: IS_PURE || FORCED_CONSTRUCTOR }, { // TODO: fix export logic
  42. DOMException: FORCED_CONSTRUCTOR ? $DOMException : NativeDOMException
  43. });
  44. var PolyfilledDOMException = getBuiltIn(DOM_EXCEPTION);
  45. var PolyfilledDOMExceptionPrototype = PolyfilledDOMException.prototype;
  46. if (PolyfilledDOMExceptionPrototype.constructor !== PolyfilledDOMException) {
  47. if (!IS_PURE) {
  48. defineProperty(PolyfilledDOMExceptionPrototype, 'constructor', createPropertyDescriptor(1, PolyfilledDOMException));
  49. }
  50. for (var key in DOMExceptionConstants) if (hasOwn(DOMExceptionConstants, key)) {
  51. var constant = DOMExceptionConstants[key];
  52. var constantName = constant.s;
  53. if (!hasOwn(PolyfilledDOMException, constantName)) {
  54. defineProperty(PolyfilledDOMException, constantName, createPropertyDescriptor(6, constant.c));
  55. }
  56. }
  57. }