diff --git a/jscomp/others/js_exn.res b/jscomp/others/js_exn.res index 9cd37bc19b..126c4b9cab 100644 --- a/jscomp/others/js_exn.res +++ b/jscomp/others/js_exn.res @@ -36,42 +36,47 @@ external asJsExn: exn => option = "?as_js_exn" type error @new external makeError: string => error = "Error" + external isCamlExceptionOrOpenVariant: 'a => bool = "?is_extension" external anyToExnInternal: 'a => exn = "#wrap_exn" -let raiseError = str => raise((Obj.magic((makeError(str): error)): exn)) +let throw: exn => 'a = %raw("function (exn) { + throw exn; +}") + +let raiseError = str => throw((Obj.magic((makeError(str): error)): exn)) type eval_error @new external makeEvalError: string => eval_error = "EvalError" -let raiseEvalError = str => raise((Obj.magic((makeEvalError(str): eval_error)): exn)) +let raiseEvalError = str => throw((Obj.magic((makeEvalError(str): eval_error)): exn)) type range_error @new external makeRangeError: string => range_error = "RangeError" -let raiseRangeError = str => raise((Obj.magic((makeRangeError(str): range_error)): exn)) +let raiseRangeError = str => throw((Obj.magic((makeRangeError(str): range_error)): exn)) type reference_error @new external makeReferenceError: string => reference_error = "ReferenceError" -let raiseReferenceError = str => raise(Obj.magic(makeReferenceError(str))) +let raiseReferenceError = str => throw(Obj.magic(makeReferenceError(str))) type syntax_error @new external makeSyntaxError: string => syntax_error = "SyntaxError" -let raiseSyntaxError = str => raise(Obj.magic(makeSyntaxError(str))) +let raiseSyntaxError = str => throw(Obj.magic(makeSyntaxError(str))) type type_error @new external makeTypeError: string => type_error = "TypeError" -let raiseTypeError = str => raise(Obj.magic(makeTypeError(str))) +let raiseTypeError = str => throw(Obj.magic(makeTypeError(str))) type uri_error @new external makeURIError: string => uri_error = "URIError" -let raiseUriError = str => raise(Obj.magic(makeURIError(str))) +let raiseUriError = str => throw(Obj.magic(makeURIError(str))) /* TODO add predicate to tell which error is which " */ diff --git a/jscomp/test/js_exception_catch_test.js b/jscomp/test/js_exception_catch_test.js index 4f0fa303d2..756dab19be 100644 --- a/jscomp/test/js_exception_catch_test.js +++ b/jscomp/test/js_exception_catch_test.js @@ -205,9 +205,7 @@ eq("File \"js_exception_catch_test.res\", line 52, characters 5-12", test(functi }), "C_any"); eq("File \"js_exception_catch_test.res\", line 53, characters 5-12", test(function () { - throw new Error(new Error("x").RE_EXN_ID, { - cause: new Error("x") - }); + Js_exn.raiseError("x"); }), "Js_error"); eq("File \"js_exception_catch_test.res\", line 54, characters 5-12", test(function () { diff --git a/lib/es6/js_exn.js b/lib/es6/js_exn.js index 5d9a1f8fa9..4eed951c5b 100644 --- a/lib/es6/js_exn.js +++ b/lib/es6/js_exn.js @@ -1,46 +1,36 @@ +let $$throw = (function (exn) { + throw exn; +}); + function raiseError(str) { - throw new Error(new Error(str).RE_EXN_ID, { - cause: new Error(str) - }); + return $$throw(new Error(str)); } function raiseEvalError(str) { - throw new Error(new EvalError(str).RE_EXN_ID, { - cause: new EvalError(str) - }); + return $$throw(new EvalError(str)); } function raiseRangeError(str) { - throw new Error(new RangeError(str).RE_EXN_ID, { - cause: new RangeError(str) - }); + return $$throw(new RangeError(str)); } function raiseReferenceError(str) { - throw new Error(new ReferenceError(str).RE_EXN_ID, { - cause: new ReferenceError(str) - }); + return $$throw(new ReferenceError(str)); } function raiseSyntaxError(str) { - throw new Error(new SyntaxError(str).RE_EXN_ID, { - cause: new SyntaxError(str) - }); + return $$throw(new SyntaxError(str)); } function raiseTypeError(str) { - throw new Error(new TypeError(str).RE_EXN_ID, { - cause: new TypeError(str) - }); + return $$throw(new TypeError(str)); } function raiseUriError(str) { - throw new Error(new URIError(str).RE_EXN_ID, { - cause: new URIError(str) - }); + return $$throw(new URIError(str)); } let $$Error$1 = "JsError"; diff --git a/lib/es6/js_null.js b/lib/es6/js_null.js index 4a30b70360..fefa01beeb 100644 --- a/lib/es6/js_null.js +++ b/lib/es6/js_null.js @@ -1,5 +1,6 @@ +import * as Js_exn from "./js_exn.js"; import * as Caml_option from "./caml_option.js"; function test(x) { @@ -9,10 +10,9 @@ function test(x) { function getExn(f) { if (f !== null) { return f; + } else { + return Js_exn.raiseError("Js.Null.getExn"); } - throw new Error(new Error("Js.Null.getExn").RE_EXN_ID, { - cause: new Error("Js.Null.getExn") - }); } function bind(x, f) { diff --git a/lib/es6/js_option.js b/lib/es6/js_option.js index b24505acab..b6807fa942 100644 --- a/lib/es6/js_option.js +++ b/lib/es6/js_option.js @@ -1,5 +1,6 @@ +import * as Js_exn from "./js_exn.js"; import * as Caml_option from "./caml_option.js"; function some(x) { @@ -25,10 +26,9 @@ function isNone(x) { function getExn(x) { if (x !== undefined) { return Caml_option.valFromOption(x); + } else { + return Js_exn.raiseError("getExn"); } - throw new Error(new Error("getExn").RE_EXN_ID, { - cause: new Error("getExn") - }); } function equal(eq, a, b) { diff --git a/lib/es6/js_undefined.js b/lib/es6/js_undefined.js index 762283d3da..cb07253b25 100644 --- a/lib/es6/js_undefined.js +++ b/lib/es6/js_undefined.js @@ -1,5 +1,6 @@ +import * as Js_exn from "./js_exn.js"; import * as Caml_option from "./caml_option.js"; function test(x) { @@ -13,10 +14,9 @@ function testAny(x) { function getExn(f) { if (f !== undefined) { return f; + } else { + return Js_exn.raiseError("Js.Undefined.getExn"); } - throw new Error(new Error("Js.Undefined.getExn").RE_EXN_ID, { - cause: new Error("Js.Undefined.getExn") - }); } function bind(x, f) { diff --git a/lib/js/js_exn.js b/lib/js/js_exn.js index 706c4b6181..4b4c9883bf 100644 --- a/lib/js/js_exn.js +++ b/lib/js/js_exn.js @@ -1,46 +1,36 @@ 'use strict'; +let $$throw = (function (exn) { + throw exn; +}); + function raiseError(str) { - throw new Error(new Error(str).RE_EXN_ID, { - cause: new Error(str) - }); + return $$throw(new Error(str)); } function raiseEvalError(str) { - throw new Error(new EvalError(str).RE_EXN_ID, { - cause: new EvalError(str) - }); + return $$throw(new EvalError(str)); } function raiseRangeError(str) { - throw new Error(new RangeError(str).RE_EXN_ID, { - cause: new RangeError(str) - }); + return $$throw(new RangeError(str)); } function raiseReferenceError(str) { - throw new Error(new ReferenceError(str).RE_EXN_ID, { - cause: new ReferenceError(str) - }); + return $$throw(new ReferenceError(str)); } function raiseSyntaxError(str) { - throw new Error(new SyntaxError(str).RE_EXN_ID, { - cause: new SyntaxError(str) - }); + return $$throw(new SyntaxError(str)); } function raiseTypeError(str) { - throw new Error(new TypeError(str).RE_EXN_ID, { - cause: new TypeError(str) - }); + return $$throw(new TypeError(str)); } function raiseUriError(str) { - throw new Error(new URIError(str).RE_EXN_ID, { - cause: new URIError(str) - }); + return $$throw(new URIError(str)); } let $$Error$1 = "JsError"; diff --git a/lib/js/js_null.js b/lib/js/js_null.js index 7d8ddcb6e1..cd3023dca0 100644 --- a/lib/js/js_null.js +++ b/lib/js/js_null.js @@ -1,5 +1,6 @@ 'use strict'; +let Js_exn = require("./js_exn.js"); let Caml_option = require("./caml_option.js"); function test(x) { @@ -9,10 +10,9 @@ function test(x) { function getExn(f) { if (f !== null) { return f; + } else { + return Js_exn.raiseError("Js.Null.getExn"); } - throw new Error(new Error("Js.Null.getExn").RE_EXN_ID, { - cause: new Error("Js.Null.getExn") - }); } function bind(x, f) { diff --git a/lib/js/js_option.js b/lib/js/js_option.js index 0b876c2b9a..3d7e188ed4 100644 --- a/lib/js/js_option.js +++ b/lib/js/js_option.js @@ -1,5 +1,6 @@ 'use strict'; +let Js_exn = require("./js_exn.js"); let Caml_option = require("./caml_option.js"); function some(x) { @@ -25,10 +26,9 @@ function isNone(x) { function getExn(x) { if (x !== undefined) { return Caml_option.valFromOption(x); + } else { + return Js_exn.raiseError("getExn"); } - throw new Error(new Error("getExn").RE_EXN_ID, { - cause: new Error("getExn") - }); } function equal(eq, a, b) { diff --git a/lib/js/js_undefined.js b/lib/js/js_undefined.js index 567ce8fec5..9de3e4cb45 100644 --- a/lib/js/js_undefined.js +++ b/lib/js/js_undefined.js @@ -1,5 +1,6 @@ 'use strict'; +let Js_exn = require("./js_exn.js"); let Caml_option = require("./caml_option.js"); function test(x) { @@ -13,10 +14,9 @@ function testAny(x) { function getExn(f) { if (f !== undefined) { return f; + } else { + return Js_exn.raiseError("Js.Undefined.getExn"); } - throw new Error(new Error("Js.Undefined.getExn").RE_EXN_ID, { - cause: new Error("Js.Undefined.getExn") - }); } function bind(x, f) {