Skip to content

Commit

Permalink
add WebAssembly errors
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Nov 17, 2021
1 parent a8bdbd2 commit f4fb250
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var IS_PURE = require('../internals/is-pure');

module.exports = function (ERROR_NAME, wrapper, FORCED, IS_AGGREGATE_ERROR) {
var OPTIONS_POSITION = IS_AGGREGATE_ERROR ? 2 : 1;
var OriginalError = getBuiltIn(ERROR_NAME);
var OriginalError = apply(getBuiltIn, null, ERROR_NAME.split('.'));

if (!OriginalError) return;

Expand Down
70 changes: 46 additions & 24 deletions packages/core-js/modules/es.error.cause.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,56 @@
/* eslint-disable no-unused-vars -- required for functions `.length` */
var $ = require('../internals/export');
var global = require('../internals/global');
var apply = require('../internals/function-apply');
var wrapErrorConstructorWithCause = require('../internals/wrap-error-constructor-with-cause');

var WEB_ASSEMBLY = 'WebAssembly';
var WebAssembly = global[WEB_ASSEMBLY];

var FORCED = Error('e', { cause: 7 }).cause !== 7;

var exportGlobalErrorCauseWrapper = function (ERROR_NAME, wrapper) {
var O = {};
O[ERROR_NAME] = wrapErrorConstructorWithCause(ERROR_NAME, wrapper, FORCED);
$({ global: true, forced: FORCED }, O);
};

var exportWebAssemblyErrorCauseWrapper = function (ERROR_NAME, wrapper) {
if (WebAssembly && WebAssembly[ERROR_NAME]) {
var O = {};
O[ERROR_NAME] = wrapErrorConstructorWithCause(WEB_ASSEMBLY + '.' + ERROR_NAME, wrapper, FORCED);
$({ target: WEB_ASSEMBLY, stat: true, forced: FORCED }, O);
}
};

// https://github.com/tc39/proposal-error-cause
$({ global: true, forced: FORCED }, {
Error: wrapErrorConstructorWithCause('Error', function (init) {
return function Error(message) { return apply(init, this, arguments); };
}, FORCED)
exportGlobalErrorCauseWrapper('Error', function (init) {
return function Error(message) { return apply(init, this, arguments); };
});

$({ global: true, forced: FORCED }, {
EvalError: wrapErrorConstructorWithCause('EvalError', function (init) {
return function EvalError(message) { return apply(init, this, arguments); };
}, FORCED),
RangeError: wrapErrorConstructorWithCause('RangeError', function (init) {
return function RangeError(message) { return apply(init, this, arguments); };
}, FORCED),
ReferenceError: wrapErrorConstructorWithCause('ReferenceError', function (init) {
return function ReferenceError(message) { return apply(init, this, arguments); };
}, FORCED),
SyntaxError: wrapErrorConstructorWithCause('SyntaxError', function (init) {
return function SyntaxError(message) { return apply(init, this, arguments); };
}, FORCED),
TypeError: wrapErrorConstructorWithCause('TypeError', function (init) {
return function TypeError(message) { return apply(init, this, arguments); };
}, FORCED),
URIError: wrapErrorConstructorWithCause('URIError', function (init) {
return function URIError(message) { return apply(init, this, arguments); };
}, FORCED)
exportGlobalErrorCauseWrapper('EvalError', function (init) {
return function EvalError(message) { return apply(init, this, arguments); };
});
exportGlobalErrorCauseWrapper('RangeError', function (init) {
return function RangeError(message) { return apply(init, this, arguments); };
});
exportGlobalErrorCauseWrapper('ReferenceError', function (init) {
return function ReferenceError(message) { return apply(init, this, arguments); };
});
exportGlobalErrorCauseWrapper('SyntaxError', function (init) {
return function SyntaxError(message) { return apply(init, this, arguments); };
});
exportGlobalErrorCauseWrapper('TypeError', function (init) {
return function TypeError(message) { return apply(init, this, arguments); };
});
exportGlobalErrorCauseWrapper('URIError', function (init) {
return function URIError(message) { return apply(init, this, arguments); };
});
exportWebAssemblyErrorCauseWrapper('CompileError', function (init) {
return function CompileError(message) { return apply(init, this, arguments); };
});
exportWebAssemblyErrorCauseWrapper('LinkError', function (init) {
return function LinkError(message) { return apply(init, this, arguments); };
});
exportWebAssemblyErrorCauseWrapper('RuntimeError', function (init) {
return function RuntimeError(message) { return apply(init, this, arguments); };
});
11 changes: 9 additions & 2 deletions tests/pure/es.error.cause.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import { PROTO } from '../helpers/constants';
import path from 'core-js-pure/es/error';
import create from 'core-js-pure/es/object/create';

for (const ERROR_NAME of ['Error', 'EvalError', 'RangeError', 'ReferenceError', 'SyntaxError', 'TypeError', 'URIError']) {
function runErrorTestCase($Error, ERROR_NAME) {
QUnit.test(`${ ERROR_NAME } constructor with 'cause' param`, assert => {
const $Error = path[ERROR_NAME];
assert.isFunction($Error);
assert.arity($Error, 1);
assert.name($Error, ERROR_NAME);
Expand Down Expand Up @@ -44,3 +43,11 @@ for (const ERROR_NAME of ['Error', 'EvalError', 'RangeError', 'ReferenceError',
assert.ok(!error.hasOwnProperty('cause'), 'default instance cause missed');
});
}

for (const ERROR_NAME of ['Error', 'EvalError', 'RangeError', 'ReferenceError', 'SyntaxError', 'TypeError', 'URIError']) {
runErrorTestCase(path[ERROR_NAME], ERROR_NAME);
}

if (path.WebAssembly) for (const ERROR_NAME of ['CompileError', 'LinkError', 'RuntimeError']) {
if (path.WebAssembly[ERROR_NAME]) runErrorTestCase(path.WebAssembly[ERROR_NAME], ERROR_NAME);
}
11 changes: 9 additions & 2 deletions tests/tests/es.error.cause.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { GLOBAL, PROTO } from '../helpers/constants';

const { create } = Object;

for (const ERROR_NAME of ['Error', 'EvalError', 'RangeError', 'ReferenceError', 'SyntaxError', 'TypeError', 'URIError']) {
function runErrorTestCase($Error, ERROR_NAME) {
QUnit.test(`${ ERROR_NAME } constructor with 'cause' param`, assert => {
const $Error = GLOBAL[ERROR_NAME];
assert.isFunction($Error);
assert.arity($Error, 1);
assert.name($Error, ERROR_NAME);
Expand Down Expand Up @@ -48,3 +47,11 @@ for (const ERROR_NAME of ['Error', 'EvalError', 'RangeError', 'ReferenceError',
assert.ok(!error.hasOwnProperty('cause'), 'default instance cause missed');
});
}

for (const ERROR_NAME of ['Error', 'EvalError', 'RangeError', 'ReferenceError', 'SyntaxError', 'TypeError', 'URIError']) {
runErrorTestCase(GLOBAL[ERROR_NAME], ERROR_NAME);
}

if (GLOBAL.WebAssembly) for (const ERROR_NAME of ['CompileError', 'LinkError', 'RuntimeError']) {
if (GLOBAL.WebAssembly[ERROR_NAME]) runErrorTestCase(GLOBAL.WebAssembly[ERROR_NAME], ERROR_NAME);
}

0 comments on commit f4fb250

Please sign in to comment.