es.error.cause.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use strict';
  2. /* eslint-disable no-unused-vars -- required for functions `.length` */
  3. var $ = require('../internals/export');
  4. var globalThis = require('../internals/global-this');
  5. var apply = require('../internals/function-apply');
  6. var wrapErrorConstructorWithCause = require('../internals/wrap-error-constructor-with-cause');
  7. var WEB_ASSEMBLY = 'WebAssembly';
  8. var WebAssembly = globalThis[WEB_ASSEMBLY];
  9. // eslint-disable-next-line es/no-error-cause -- feature detection
  10. var FORCED = new Error('e', { cause: 7 }).cause !== 7;
  11. var exportGlobalErrorCauseWrapper = function (ERROR_NAME, wrapper) {
  12. var O = {};
  13. O[ERROR_NAME] = wrapErrorConstructorWithCause(ERROR_NAME, wrapper, FORCED);
  14. $({ global: true, constructor: true, arity: 1, forced: FORCED }, O);
  15. };
  16. var exportWebAssemblyErrorCauseWrapper = function (ERROR_NAME, wrapper) {
  17. if (WebAssembly && WebAssembly[ERROR_NAME]) {
  18. var O = {};
  19. O[ERROR_NAME] = wrapErrorConstructorWithCause(WEB_ASSEMBLY + '.' + ERROR_NAME, wrapper, FORCED);
  20. $({ target: WEB_ASSEMBLY, stat: true, constructor: true, arity: 1, forced: FORCED }, O);
  21. }
  22. };
  23. // https://tc39.es/ecma262/#sec-nativeerror
  24. exportGlobalErrorCauseWrapper('Error', function (init) {
  25. return function Error(message) { return apply(init, this, arguments); };
  26. });
  27. exportGlobalErrorCauseWrapper('EvalError', function (init) {
  28. return function EvalError(message) { return apply(init, this, arguments); };
  29. });
  30. exportGlobalErrorCauseWrapper('RangeError', function (init) {
  31. return function RangeError(message) { return apply(init, this, arguments); };
  32. });
  33. exportGlobalErrorCauseWrapper('ReferenceError', function (init) {
  34. return function ReferenceError(message) { return apply(init, this, arguments); };
  35. });
  36. exportGlobalErrorCauseWrapper('SyntaxError', function (init) {
  37. return function SyntaxError(message) { return apply(init, this, arguments); };
  38. });
  39. exportGlobalErrorCauseWrapper('TypeError', function (init) {
  40. return function TypeError(message) { return apply(init, this, arguments); };
  41. });
  42. exportGlobalErrorCauseWrapper('URIError', function (init) {
  43. return function URIError(message) { return apply(init, this, arguments); };
  44. });
  45. exportWebAssemblyErrorCauseWrapper('CompileError', function (init) {
  46. return function CompileError(message) { return apply(init, this, arguments); };
  47. });
  48. exportWebAssemblyErrorCauseWrapper('LinkError', function (init) {
  49. return function LinkError(message) { return apply(init, this, arguments); };
  50. });
  51. exportWebAssemblyErrorCauseWrapper('RuntimeError', function (init) {
  52. return function RuntimeError(message) { return apply(init, this, arguments); };
  53. });