es.error.cause.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. // eslint-disable-next-line unicorn/no-immediate-mutation -- ES3 syntax limitation
  14. O[ERROR_NAME] = wrapErrorConstructorWithCause(ERROR_NAME, wrapper, FORCED);
  15. $({ global: true, constructor: true, arity: 1, forced: FORCED }, O);
  16. };
  17. var exportWebAssemblyErrorCauseWrapper = function (ERROR_NAME, wrapper) {
  18. if (WebAssembly && WebAssembly[ERROR_NAME]) {
  19. var O = {};
  20. // eslint-disable-next-line unicorn/no-immediate-mutation -- ES3 syntax limitation
  21. O[ERROR_NAME] = wrapErrorConstructorWithCause(WEB_ASSEMBLY + '.' + ERROR_NAME, wrapper, FORCED);
  22. $({ target: WEB_ASSEMBLY, stat: true, constructor: true, arity: 1, forced: FORCED }, O);
  23. }
  24. };
  25. // https://tc39.es/ecma262/#sec-nativeerror
  26. exportGlobalErrorCauseWrapper('Error', function (init) {
  27. return function Error(message) { return apply(init, this, arguments); };
  28. });
  29. exportGlobalErrorCauseWrapper('EvalError', function (init) {
  30. return function EvalError(message) { return apply(init, this, arguments); };
  31. });
  32. exportGlobalErrorCauseWrapper('RangeError', function (init) {
  33. return function RangeError(message) { return apply(init, this, arguments); };
  34. });
  35. exportGlobalErrorCauseWrapper('ReferenceError', function (init) {
  36. return function ReferenceError(message) { return apply(init, this, arguments); };
  37. });
  38. exportGlobalErrorCauseWrapper('SyntaxError', function (init) {
  39. return function SyntaxError(message) { return apply(init, this, arguments); };
  40. });
  41. exportGlobalErrorCauseWrapper('TypeError', function (init) {
  42. return function TypeError(message) { return apply(init, this, arguments); };
  43. });
  44. exportGlobalErrorCauseWrapper('URIError', function (init) {
  45. return function URIError(message) { return apply(init, this, arguments); };
  46. });
  47. exportWebAssemblyErrorCauseWrapper('CompileError', function (init) {
  48. return function CompileError(message) { return apply(init, this, arguments); };
  49. });
  50. exportWebAssemblyErrorCauseWrapper('LinkError', function (init) {
  51. return function LinkError(message) { return apply(init, this, arguments); };
  52. });
  53. exportWebAssemblyErrorCauseWrapper('RuntimeError', function (init) {
  54. return function RuntimeError(message) { return apply(init, this, arguments); };
  55. });