web.dom-exception.stack.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var getBuiltIn = require('../internals/get-built-in');
  4. var createPropertyDescriptor = require('../internals/create-property-descriptor');
  5. var defineProperty = require('../internals/object-define-property').f;
  6. var hasOwn = require('../internals/has-own-property');
  7. var anInstance = require('../internals/an-instance');
  8. var inheritIfRequired = require('../internals/inherit-if-required');
  9. var normalizeStringArgument = require('../internals/normalize-string-argument');
  10. var DOMExceptionConstants = require('../internals/dom-exception-constants');
  11. var clearErrorStack = require('../internals/clear-error-stack');
  12. var IS_PURE = require('../internals/is-pure');
  13. var DOM_EXCEPTION = 'DOMException';
  14. var Error = getBuiltIn('Error');
  15. var NativeDOMException = getBuiltIn(DOM_EXCEPTION);
  16. var $DOMException = function DOMException() {
  17. anInstance(this, DOMExceptionPrototype);
  18. var argumentsLength = arguments.length;
  19. var message = normalizeStringArgument(argumentsLength < 1 ? undefined : arguments[0]);
  20. var name = normalizeStringArgument(argumentsLength < 2 ? undefined : arguments[1], 'Error');
  21. var that = new NativeDOMException(message, name);
  22. var error = Error(message);
  23. error.name = DOM_EXCEPTION;
  24. defineProperty(that, 'stack', createPropertyDescriptor(1, clearErrorStack(error.stack, 1)));
  25. inheritIfRequired(that, this, $DOMException);
  26. return that;
  27. };
  28. var DOMExceptionPrototype = $DOMException.prototype = NativeDOMException.prototype;
  29. var ERROR_HAS_STACK = 'stack' in Error(DOM_EXCEPTION);
  30. var DOM_EXCEPTION_HAS_STACK = 'stack' in new NativeDOMException(1, 2);
  31. var FORCED_CONSTRUCTOR = ERROR_HAS_STACK && !DOM_EXCEPTION_HAS_STACK;
  32. // `DOMException` constructor patch for `.stack` where it's required
  33. // https://webidl.spec.whatwg.org/#es-DOMException-specialness
  34. $({ global: true, forced: IS_PURE || FORCED_CONSTRUCTOR }, { // TODO: fix export logic
  35. DOMException: FORCED_CONSTRUCTOR ? $DOMException : NativeDOMException
  36. });
  37. var PolyfilledDOMException = getBuiltIn(DOM_EXCEPTION);
  38. var PolyfilledDOMExceptionPrototype = PolyfilledDOMException.prototype;
  39. if (PolyfilledDOMExceptionPrototype.constructor !== PolyfilledDOMException) {
  40. if (!IS_PURE) {
  41. defineProperty(PolyfilledDOMExceptionPrototype, 'constructor', createPropertyDescriptor(1, PolyfilledDOMException));
  42. }
  43. for (var key in DOMExceptionConstants) if (hasOwn(DOMExceptionConstants, key)) {
  44. var constant = DOMExceptionConstants[key];
  45. var constantName = constant.s;
  46. if (!hasOwn(PolyfilledDOMException, constantName)) {
  47. defineProperty(PolyfilledDOMException, constantName, createPropertyDescriptor(6, constant.c));
  48. }
  49. }
  50. }