Skip to content

Commit e1c5d29

Browse files
committed
Add a special module for js exception
1 parent 6aa6de9 commit e1c5d29

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+330
-301
lines changed

jscomp/core/js_runtime_modules.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ let block = "Block"
5050
let js_primitive = "Js_primitive"
5151
let module_ = "Caml_module"
5252
let missing_polyfill = "Caml_missing_polyfill"
53-
let exn = "Js_exn"
53+
let caml_js_exceptions = "Caml_js_exceptions"

jscomp/core/lam_compile_primitive.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ let translate loc
5252
->
5353
Js_of_lam_exception.make (E.str s)
5454
| Pwrap_exn ->
55-
E.runtime_call Js_runtime_modules.exn "internalToOCamlException" args
55+
E.runtime_call Js_runtime_modules.caml_js_exceptions "internalToOCamlException" args
5656
| Praw_js_function(arg,block) ->
5757
E.raw_js_function arg block
5858
| Praw_js_code_exp s ->

jscomp/runtime/.depend

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ caml_int64.cmj : js.cmj caml_utils.cmj caml_int32.cmj bs_string.cmj \
66
caml_int64.cmi
77
caml_exceptions.cmj : js.cmj caml_builtin_exceptions.cmj bs_obj.cmj \
88
caml_exceptions.cmi
9+
caml_js_exceptions.cmj : caml_exceptions.cmj
910
caml_utils.cmj : caml_utils.cmi
1011
caml_sys.cmj : js.cmj caml_sys.cmi
1112
caml_io.cmj : js.cmj bs_string.cmj
@@ -30,7 +31,7 @@ caml_oo_curry.cmj : curry.cmj caml_oo.cmj
3031
caml_module.cmj :
3132
caml_missing_polyfill.cmj : js_exn.cmj caml_missing_polyfill.cmi
3233
bs_string.cmj :
33-
js_exn.cmj : caml_exceptions.cmj js_exn.cmi
34+
js_exn.cmj : caml_js_exceptions.cmj js_exn.cmi
3435
js_nativeint.cmj :
3536
js_int64.cmj :
3637
block.cmj : js.cmj bs_obj.cmj block.cmi

jscomp/runtime/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ COMPILER=../../lib/bsc.exe
44

55
OTHERS= caml_array caml_string caml_bytes\
66
caml_obj caml_int64 \
7-
caml_exceptions caml_utils caml_sys caml_io\
7+
caml_exceptions caml_js_exceptions caml_utils caml_sys caml_io\
88
caml_float caml_lexer caml_parser \
99
caml_format caml_md5 caml_queue \
1010
caml_hash_primitive\
@@ -29,7 +29,7 @@ caml_primitive.cmj: caml_primitive.cmi
2929
caml_int64.cmj : caml_obj.cmj
3030
bs_obj.cmj: js.cmi
3131
# or we can do a post-processing to add missing cmj dependency manually
32-
js_exn.cmj : caml_exceptions.cmj
32+
js_exn.cmj caml_js_exceptions.cmj : caml_exceptions.cmj
3333
$(addsuffix .cmj, $(OTHERS)): caml_builtin_exceptions.cmj block.cmj js.cmj js_unsafe.cmj caml_primitive.cmj
3434
$(addsuffix .cmj, $(OTHERS)) caml_builtin_exceptions.cmj block.cmj js.cmj js_unsafe.cmj : js_internal.cmi
3535
## since we use ppx

jscomp/runtime/caml_js_exceptions.ml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
3+
type t
4+
5+
exception Error of t
6+
7+
8+
(**
9+
{[
10+
exception A of int;;
11+
let v = A 3 ;;
12+
Obj.tag (Obj.field (Obj.repr v) 0);;
13+
- : int = 248
14+
]}
15+
*)
16+
let internalToOCamlException (e : Obj.t) =
17+
if Caml_exceptions.isCamlExceptionOrOpenVariant e then
18+
(Obj.magic e : exn)
19+
else Error (Obj.magic (e : Obj.t) : t)

jscomp/runtime/js_exn.ml

+2-16
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525

26-
type t
27-
28-
type exn += Error of t
26+
type t = Caml_js_exceptions.t
2927

28+
exception Error = Caml_js_exceptions.Error
3029
external stack : t -> string option = ""
3130
[@@bs.get] [@@bs.return undefined_to_opt]
3231
external message : t -> string option = ""
@@ -36,19 +35,6 @@ external name : t -> string option = ""
3635
external fileName : t -> string option = ""
3736
[@@bs.get] [@@bs.return undefined_to_opt]
3837

39-
(**
40-
{[
41-
exception A of int;;
42-
let v = A 3 ;;
43-
Obj.tag (Obj.field (Obj.repr v) 0);;
44-
- : int = 248
45-
]}
46-
*)
47-
let internalToOCamlException (e : Obj.t) =
48-
if Caml_exceptions.isCamlExceptionOrOpenVariant e then
49-
(Obj.magic e : exn)
50-
else Error (Obj.magic (e : Obj.t) : t)
51-
5238
type error
5339
external makeError : string -> error = "Error" [@@bs.new]
5440

jscomp/runtime/js_exn.mli

-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ external name : t -> string option = ""
3737
external fileName : t -> string option = ""
3838
[@@bs.get] [@@bs.return undefined_to_opt]
3939

40-
(** Used by the compiler internally *)
41-
val internalToOCamlException : Obj.t -> exn
4240

4341
(** Raise Js exception Error object with stacktrace *)
4442
val raiseError : string -> 'a

jscomp/test/caml_compare_test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
var Mt = require("./mt.js");
44
var Block = require("../../lib/js/block.js");
5-
var Js_exn = require("../../lib/js/js_exn.js");
65
var Caml_obj = require("../../lib/js/caml_obj.js");
6+
var Caml_js_exceptions = require("../../lib/js/caml_js_exceptions.js");
77
var Caml_builtin_exceptions = require("../../lib/js/caml_builtin_exceptions.js");
88

99
var function_equal_test;
@@ -16,7 +16,7 @@ try {
1616
}));
1717
}
1818
catch (raw_exn){
19-
var exn = Js_exn.internalToOCamlException(raw_exn);
19+
var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
2020
function_equal_test = exn[0] === Caml_builtin_exceptions.invalid_argument && exn[1] === "equal: functional value" ? true : false;
2121
}
2222

jscomp/test/custom_error_test.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var Js_exn = require("../../lib/js/js_exn.js");
44
var Js_primitive = require("../../lib/js/js_primitive.js");
5+
var Caml_js_exceptions = require("../../lib/js/caml_js_exceptions.js");
56

67
function test_js_error(param) {
78
var exit = 0;
@@ -11,7 +12,7 @@ function test_js_error(param) {
1112
exit = 1;
1213
}
1314
catch (raw_exn){
14-
var exn = Js_exn.internalToOCamlException(raw_exn);
15+
var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
1516
if (exn[0] === Js_exn.$$Error) {
1617
console.log(Js_primitive.undefined_to_opt(exn[1].stack));
1718
return undefined;
@@ -30,7 +31,7 @@ function test_js_error2(param) {
3031
return JSON.parse(" {\"x\" : }");
3132
}
3233
catch (raw_e){
33-
var e = Js_exn.internalToOCamlException(raw_e);
34+
var e = Caml_js_exceptions.internalToOCamlException(raw_e);
3435
if (e[0] === Js_exn.$$Error) {
3536
console.log(Js_primitive.undefined_to_opt(e[1].stack));
3637
throw e;
@@ -48,7 +49,7 @@ function example1(param) {
4849
exit = 1;
4950
}
5051
catch (raw_exn){
51-
var exn = Js_exn.internalToOCamlException(raw_exn);
52+
var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
5253
if (exn[0] === Js_exn.$$Error) {
5354
console.log(Js_primitive.undefined_to_opt(exn[1].stack));
5455
return undefined;
@@ -67,7 +68,7 @@ function example2(param) {
6768
return Js_primitive.some(JSON.parse(" {\"x\"}"));
6869
}
6970
catch (raw_exn){
70-
var exn = Js_exn.internalToOCamlException(raw_exn);
71+
var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
7172
if (exn[0] === Js_exn.$$Error) {
7273
return undefined;
7374
} else {

jscomp/test/equal_exception_test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
var Mt = require("./mt.js");
44
var Bytes = require("../../lib/js/bytes.js");
5-
var Js_exn = require("../../lib/js/js_exn.js");
65
var Caml_bytes = require("../../lib/js/caml_bytes.js");
76
var Caml_exceptions = require("../../lib/js/caml_exceptions.js");
7+
var Caml_js_exceptions = require("../../lib/js/caml_js_exceptions.js");
88
var Caml_builtin_exceptions = require("../../lib/js/caml_builtin_exceptions.js");
99

1010
var v = "gso";
@@ -68,7 +68,7 @@ function is_normal_exception(_x) {
6868
throw v;
6969
}
7070
catch (raw_exn){
71-
var exn = Js_exn.internalToOCamlException(raw_exn);
71+
var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
7272
if (exn[0] === A) {
7373
if (exn[1] !== 3) {
7474
throw exn;

jscomp/test/exception_raise_test.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var Block = require("../../lib/js/block.js");
55
var Curry = require("../../lib/js/curry.js");
66
var Js_exn = require("../../lib/js/js_exn.js");
77
var Caml_exceptions = require("../../lib/js/caml_exceptions.js");
8+
var Caml_js_exceptions = require("../../lib/js/caml_js_exceptions.js");
89
var Caml_builtin_exceptions = require("../../lib/js/caml_builtin_exceptions.js");
910

1011
var Local = Caml_exceptions.create("Exception_raise_test.Local");
@@ -21,7 +22,7 @@ function appf(g, x) {
2122
return Curry._1(g, x);
2223
}
2324
catch (raw_exn){
24-
var exn = Js_exn.internalToOCamlException(raw_exn);
25+
var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
2526
var exit = 0;
2627
if (exn === Local) {
2728
return 3;
@@ -70,7 +71,7 @@ try {
7071
f = ( function () {throw (new Error ("x"))} ());
7172
}
7273
catch (raw_exn){
73-
var exn = Js_exn.internalToOCamlException(raw_exn);
74+
var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
7475
f = exn[0] === A ? exn[1] : 2;
7576
}
7677

@@ -80,7 +81,7 @@ try {
8081
ff = ( function () {throw 3} ());
8182
}
8283
catch (raw_exn$1){
83-
var exn$1 = Js_exn.internalToOCamlException(raw_exn$1);
84+
var exn$1 = Caml_js_exceptions.internalToOCamlException(raw_exn$1);
8485
ff = exn$1[0] === A ? exn$1[1] : 2;
8586
}
8687

@@ -90,7 +91,7 @@ try {
9091
fff = ( function () {throw 2} ());
9192
}
9293
catch (raw_exn$2){
93-
var exn$2 = Js_exn.internalToOCamlException(raw_exn$2);
94+
var exn$2 = Caml_js_exceptions.internalToOCamlException(raw_exn$2);
9495
fff = exn$2[0] === A ? exn$2[1] : 2;
9596
}
9697

@@ -100,7 +101,7 @@ try {
100101
a0 = ( function (){throw 2} () );
101102
}
102103
catch (raw_exn$3){
103-
var exn$3 = Js_exn.internalToOCamlException(raw_exn$3);
104+
var exn$3 = Caml_js_exceptions.internalToOCamlException(raw_exn$3);
104105
if (exn$3[0] === A || exn$3[0] === Js_exn.$$Error) {
105106
a0 = exn$3[1];
106107
} else {
@@ -121,7 +122,7 @@ try {
121122
a1 = ( function (){throw 2} () );
122123
}
123124
catch (raw_e){
124-
a1 = Js_exn.internalToOCamlException(raw_e);
125+
a1 = Caml_js_exceptions.internalToOCamlException(raw_e);
125126
}
126127

127128
var a2;
@@ -130,7 +131,7 @@ try {
130131
a2 = ( function (){throw (new Error("x"))} () );
131132
}
132133
catch (raw_e$1){
133-
a2 = Js_exn.internalToOCamlException(raw_e$1);
134+
a2 = Caml_js_exceptions.internalToOCamlException(raw_e$1);
134135
}
135136

136137
Mt.from_pair_suites("exception_raise_test.ml", /* :: */[

jscomp/test/exception_rebound_err_test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
var Mt = require("./mt.js");
44
var Block = require("../../lib/js/block.js");
55
var Curry = require("../../lib/js/curry.js");
6-
var Js_exn = require("../../lib/js/js_exn.js");
76
var Caml_exceptions = require("../../lib/js/caml_exceptions.js");
7+
var Caml_js_exceptions = require("../../lib/js/caml_js_exceptions.js");
88
var Caml_builtin_exceptions = require("../../lib/js/caml_builtin_exceptions.js");
99

1010
var suites = /* record */[/* contents : [] */0];
@@ -40,7 +40,7 @@ function test_js_error4(param) {
4040
return 1;
4141
}
4242
catch (raw_e){
43-
var e = Js_exn.internalToOCamlException(raw_e);
43+
var e = Caml_js_exceptions.internalToOCamlException(raw_e);
4444
var exit = 0;
4545
var exit$1 = 0;
4646
if (e === Caml_builtin_exceptions.not_found) {

jscomp/test/exception_value_test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var Curry = require("../../lib/js/curry.js");
44
var Js_exn = require("../../lib/js/js_exn.js");
55
var Js_primitive = require("../../lib/js/js_primitive.js");
66
var Caml_exceptions = require("../../lib/js/caml_exceptions.js");
7+
var Caml_js_exceptions = require("../../lib/js/caml_js_exceptions.js");
78
var Caml_builtin_exceptions = require("../../lib/js/caml_builtin_exceptions.js");
89

910
function f(param) {
@@ -57,7 +58,7 @@ function test_js_error2(param) {
5758
return JSON.parse(" {\"x\" : }");
5859
}
5960
catch (raw_e){
60-
var e = Js_exn.internalToOCamlException(raw_e);
61+
var e = Caml_js_exceptions.internalToOCamlException(raw_e);
6162
if (e[0] === Js_exn.$$Error) {
6263
console.log(Js_primitive.undefined_to_opt(e[1].stack));
6364
throw e;

jscomp/test/ext_filename_test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ var Block = require("../../lib/js/block.js");
66
var Bytes = require("../../lib/js/bytes.js");
77
var Curry = require("../../lib/js/curry.js");
88
var Format = require("../../lib/js/format.js");
9-
var Js_exn = require("../../lib/js/js_exn.js");
109
var $$String = require("../../lib/js/string.js");
1110
var Caml_sys = require("../../lib/js/caml_sys.js");
1211
var Filename = require("../../lib/js/filename.js");
@@ -15,6 +14,7 @@ var Caml_string = require("../../lib/js/caml_string.js");
1514
var Test_literals = require("./test_literals.js");
1615
var Ext_string_test = require("./ext_string_test.js");
1716
var CamlinternalLazy = require("../../lib/js/camlinternalLazy.js");
17+
var Caml_js_exceptions = require("../../lib/js/caml_js_exceptions.js");
1818
var Ext_pervasives_test = require("./ext_pervasives_test.js");
1919
var Caml_missing_polyfill = require("../../lib/js/caml_missing_polyfill.js");
2020
var Caml_builtin_exceptions = require("../../lib/js/caml_builtin_exceptions.js");
@@ -74,7 +74,7 @@ function chop_extension($staropt$star, name) {
7474
return Filename.chop_extension(name);
7575
}
7676
catch (raw_exn){
77-
var exn = Js_exn.internalToOCamlException(raw_exn);
77+
var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
7878
if (exn[0] === Caml_builtin_exceptions.invalid_argument) {
7979
return Curry._2(Format.ksprintf(Pervasives.invalid_arg, /* Format */[
8080
/* String_literal */Block.__(11, [
@@ -106,7 +106,7 @@ function chop_extension_if_any(fname) {
106106
return Filename.chop_extension(fname);
107107
}
108108
catch (raw_exn){
109-
var exn = Js_exn.internalToOCamlException(raw_exn);
109+
var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
110110
if (exn[0] === Caml_builtin_exceptions.invalid_argument) {
111111
return fname;
112112
} else {

jscomp/test/flow_parser_reg_test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ var Bytes = require("../../lib/js/bytes.js");
1212
var Curry = require("../../lib/js/curry.js");
1313
var Queue = require("../../lib/js/queue.js");
1414
var $$Buffer = require("../../lib/js/buffer.js");
15-
var Js_exn = require("../../lib/js/js_exn.js");
1615
var Lexing = require("../../lib/js/lexing.js");
1716
var Printf = require("../../lib/js/printf.js");
1817
var $$String = require("../../lib/js/string.js");
@@ -28,6 +27,7 @@ var Caml_string = require("../../lib/js/caml_string.js");
2827
var Js_primitive = require("../../lib/js/js_primitive.js");
2928
var Caml_primitive = require("../../lib/js/caml_primitive.js");
3029
var Caml_exceptions = require("../../lib/js/caml_exceptions.js");
30+
var Caml_js_exceptions = require("../../lib/js/caml_js_exceptions.js");
3131
var Caml_builtin_exceptions = require("../../lib/js/caml_builtin_exceptions.js");
3232

3333
var none = /* record */[
@@ -1083,7 +1083,7 @@ function parse_exponent(f) {
10831083
exponent = Caml_format.caml_int_of_string(todo_str);
10841084
}
10851085
catch (raw_exn){
1086-
var exn = Js_exn.internalToOCamlException(raw_exn);
1086+
var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
10871087
if (exn[0] === Caml_builtin_exceptions.failure) {
10881088
throw No_good;
10891089
} else {
@@ -16039,7 +16039,7 @@ function parse(content, options) {
1603916039
return ret;
1604016040
}
1604116041
catch (raw_exn){
16042-
var exn = Js_exn.internalToOCamlException(raw_exn);
16042+
var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
1604316043
if (exn[0] === $$Error) {
1604416044
var e = new Error(String(List.length(exn[1])) + " errors");
1604516045
e["name"] = "Parse Error";

jscomp/test/gpr_2316_test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
var Mt = require("./mt.js");
44
var Block = require("../../lib/js/block.js");
5-
var Js_exn = require("../../lib/js/js_exn.js");
5+
var Caml_js_exceptions = require("../../lib/js/caml_js_exceptions.js");
66
var Caml_builtin_exceptions = require("../../lib/js/caml_builtin_exceptions.js");
77

88
var suites = /* record */[/* contents : [] */0];
@@ -35,7 +35,7 @@ try {
3535
];
3636
}
3737
catch (raw_exn){
38-
var exn = Js_exn.internalToOCamlException(raw_exn);
38+
var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
3939
if (exn[0] === Caml_builtin_exceptions.failure) {
4040
y = exn[1];
4141
} else {
@@ -54,7 +54,7 @@ try {
5454
];
5555
}
5656
catch (raw_exn$1){
57-
var exn$1 = Js_exn.internalToOCamlException(raw_exn$1);
57+
var exn$1 = Caml_js_exceptions.internalToOCamlException(raw_exn$1);
5858
if (exn$1[0] === Caml_builtin_exceptions.failure) {
5959
x = exn$1[1];
6060
} else {

0 commit comments

Comments
 (0)