Skip to content

Commit 97d40c7

Browse files
committed
hide caml_exceptions from user completely
1 parent 176abbe commit 97d40c7

17 files changed

+86
-19
lines changed

jscomp/core/lam_dispatch_primitive.ml

+2
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,8 @@ let translate loc (prim_name : string)
594594
->
595595
Js_of_lam_exception.caml_fresh_oo_id args
596596
#end
597+
| "caml_is_extension" ->
598+
call Js_runtime_modules.exceptions
597599
| "caml_set_oo_id" (* needed in {!camlinternalOO.set_id} *)
598600
->
599601
Js_of_lam_exception.caml_set_oo_id args

jscomp/runtime/caml_exceptions.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ let create (str : string) : Caml_builtin_exceptions.exception_block =
101101
102102
This is not a problem in `try .. with` since the logic above is not expressible, see more design in [destruct_exn.md]
103103
*)
104-
let isCamlExceptionOrOpenVariant e =
104+
let caml_is_extension e =
105105
if Obj.magic e = Js.undefined then false
106106
else
107107
Obj.tag (Obj.repr e) = object_tag (* nullary exception *)

jscomp/runtime/caml_exceptions.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ val caml_fresh_oo_id :
3636

3737
val create : string -> Caml_builtin_exceptions.exception_block
3838
(* val makeExtension : string -> Caml_builtin_exceptions.exception_block *)
39-
val isCamlExceptionOrOpenVariant : 'a -> bool
39+
val caml_is_extension : 'a -> bool

jscomp/runtime/caml_js_exceptions.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ exception Error of t
1414
]}
1515
*)
1616
let internalToOCamlException (e : Obj.t) =
17-
if Caml_exceptions.isCamlExceptionOrOpenVariant e then
17+
if Caml_exceptions.caml_is_extension e then
1818
(Obj.magic e : exn)
1919
else Error (Obj.magic (e : Obj.t) : t)

jscomp/runtime/js_exn.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ external fileName : t -> string option = ""
3737

3838
type error
3939
external makeError : string -> error = "Error" [@@bs.new]
40-
40+
external isCamlExceptionOrOpenVariant :
41+
'a -> bool = "caml_is_extension"
42+
4143
let raiseError str =
4244
raise (Obj.magic (makeError str : error) : exn)
4345

jscomp/runtime/js_exn.mli

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ external fileName : t -> string option = ""
3838
[@@bs.get] [@@bs.return undefined_to_opt]
3939

4040

41+
external isCamlExceptionOrOpenVariant:
42+
'a -> bool = "caml_is_extension"
43+
(** internal use only *)
44+
45+
4146
(** Raise Js exception Error object with stacktrace *)
4247
val raiseError : string -> 'a
4348
val raiseEvalError : string -> 'a

jscomp/syntax/ast_util.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,8 @@ let record_as_js_object
646646

647647

648648
let isCamlExceptionOrOpenVariant : Longident.t =
649-
Ldot (Lident "Caml_exceptions","isCamlExceptionOrOpenVariant")
649+
Ldot (Ldot (Lident "Js","Exn"), "isCamlExceptionOrOpenVariant")
650+
650651

651652
let obj_magic : Longident.t =
652653
Ldot (Lident "Obj", "magic")

jscomp/test/.depend

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ exception_rebind_test.cmj : exception_def.cmj
190190
exception_rebound_err_test.cmj : mt.cmj ../runtime/js.cmj
191191
exception_repr_test.cmj : mt.cmj exception_def.cmj
192192
exception_value_test.cmj : ../runtime/js.cmj
193-
exn_error_pattern.cmj :
193+
exn_error_pattern.cmj : mt.cmj ../runtime/js.cmj
194194
export_keyword.cmj :
195195
ext_array_test.cmj :
196196
ext_bytes_test.cmj :

jscomp/test/exn_error_pattern.js

+43-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
'use strict';
22

3+
var Mt = require("./mt.js");
34
var Caml_exceptions = require("../../lib/js/caml_exceptions.js");
5+
var Caml_js_exceptions = require("../../lib/js/caml_js_exceptions.js");
46
var Caml_builtin_exceptions = require("../../lib/js/caml_builtin_exceptions.js");
57

68
function f(match) {
7-
if (Caml_exceptions.isCamlExceptionOrOpenVariant(match)) {
9+
if (Caml_exceptions.caml_is_extension(match)) {
810
if (match === Caml_builtin_exceptions.not_found) {
911
return 0;
1012
} else if (match[0] === Caml_builtin_exceptions.invalid_argument || match === Caml_builtin_exceptions.stack_overflow) {
@@ -23,7 +25,7 @@ var A = Caml_exceptions.create("Exn_error_pattern.A");
2325
var B = Caml_exceptions.create("Exn_error_pattern.B");
2426

2527
function g(match) {
26-
if (Caml_exceptions.isCamlExceptionOrOpenVariant(match)) {
28+
if (Caml_exceptions.caml_is_extension(match)) {
2729
if (match === Caml_builtin_exceptions.not_found || match[0] === Caml_builtin_exceptions.invalid_argument) {
2830
return 0;
2931
} else if (match[0] === Caml_builtin_exceptions.sys_error) {
@@ -37,8 +39,46 @@ function g(match) {
3739

3840
}
3941

42+
var suites = /* record */[/* contents : [] */0];
43+
44+
var test_id = /* record */[/* contents */0];
45+
46+
function eq(loc, x, y) {
47+
return Mt.eq_suites(test_id, suites, loc, x, y);
48+
}
49+
50+
eq("File \"exn_error_pattern.ml\", line 34, characters 5-12", f(Caml_builtin_exceptions.not_found), 0);
51+
52+
eq("File \"exn_error_pattern.ml\", line 35, characters 5-12", f([
53+
Caml_builtin_exceptions.invalid_argument,
54+
""
55+
]), 1);
56+
57+
eq("File \"exn_error_pattern.ml\", line 36, characters 5-12", f(Caml_builtin_exceptions.stack_overflow), 1);
58+
59+
eq("File \"exn_error_pattern.ml\", line 37, characters 5-12", f([
60+
Caml_builtin_exceptions.sys_error,
61+
""
62+
]), 2);
63+
64+
var tmp;
65+
66+
try {
67+
throw new Error("x");
68+
}
69+
catch (raw_e){
70+
tmp = Caml_js_exceptions.internalToOCamlException(raw_e);
71+
}
72+
73+
eq("File \"exn_error_pattern.ml\", line 38, characters 5-12", f(tmp), undefined);
74+
75+
Mt.from_pair_suites("exn_error_pattern.ml", suites[0]);
76+
4077
exports.f = f;
4178
exports.A = A;
4279
exports.B = B;
4380
exports.g = g;
44-
/* No side effect */
81+
exports.suites = suites;
82+
exports.test_id = test_id;
83+
exports.eq = eq;
84+
/* Not a pure module */

jscomp/test/exn_error_pattern.ml

+13-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,19 @@ let g = function [@bs.open]
2626
| Sys_error _ -> 2
2727
| (A i | B i) -> i
2828

29-
29+
let suites : Mt.pair_suites ref = ref []
30+
let test_id = ref 0
31+
let eq loc x y = Mt.eq_suites ~test_id ~suites loc x y
32+
33+
let () =
34+
eq __LOC__ (f Not_found) (Some 0);
35+
eq __LOC__ (f (Invalid_argument "")) (Some 1);
36+
eq __LOC__ (f Stack_overflow) (Some 1);
37+
eq __LOC__ (f (Sys_error "")) (Some 2);
38+
eq __LOC__ (f (try Js.Exn.raiseError "x" with e -> e)) None
39+
40+
let () =
41+
Mt.from_pair_suites __FILE__ !suites
3042
(* type v = A *)
3143
(* let f = function [@bs.open] *)
3244
(* | A -> 1 *)

jscomp/test/js_promise_basic_test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function andThenTest(param) {
7070
var h = Promise.resolve(/* () */0);
7171

7272
function assertIsNotFound(x) {
73-
var match = Caml_exceptions.isCamlExceptionOrOpenVariant(x) && x === Caml_builtin_exceptions.not_found ? 0 : undefined;
73+
var match = Caml_exceptions.caml_is_extension(x) && x === Caml_builtin_exceptions.not_found ? 0 : undefined;
7474
if (match !== undefined) {
7575
return h;
7676
} else {
@@ -131,7 +131,7 @@ function orElseRejectedRejectTest(param) {
131131
return p.catch((function (param) {
132132
return Promise.reject(Caml_builtin_exceptions.stack_overflow);
133133
})).then(fail).catch((function (error) {
134-
var match = Caml_exceptions.isCamlExceptionOrOpenVariant(error) && error === Caml_builtin_exceptions.stack_overflow ? 0 : undefined;
134+
var match = Caml_exceptions.caml_is_extension(error) && error === Caml_builtin_exceptions.stack_overflow ? 0 : undefined;
135135
if (match !== undefined) {
136136
return h;
137137
} else {

jscomp/test/promise_catch_test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function handler(e) {
4949
}
5050

5151
function myHandler(match) {
52-
if (Caml_exceptions.isCamlExceptionOrOpenVariant(match)) {
52+
if (Caml_exceptions.caml_is_extension(match)) {
5353
if (match === Caml_builtin_exceptions.not_found) {
5454
return 1;
5555
} else if (match[0] === Js_exn.$$Error) {

lib/bsdep.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -36098,7 +36098,8 @@ let record_as_js_object
3609836098

3609936099

3610036100
let isCamlExceptionOrOpenVariant : Longident.t =
36101-
Ldot (Lident "Caml_exceptions","isCamlExceptionOrOpenVariant")
36101+
Ldot (Ldot (Lident "Js","Exn"), "isCamlExceptionOrOpenVariant")
36102+
3610236103

3610336104
let obj_magic : Longident.t =
3610436105
Ldot (Lident "Obj", "magic")

lib/bsppx.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -18099,7 +18099,8 @@ let record_as_js_object
1809918099

1810018100

1810118101
let isCamlExceptionOrOpenVariant : Longident.t =
18102-
Ldot (Lident "Caml_exceptions","isCamlExceptionOrOpenVariant")
18102+
Ldot (Ldot (Lident "Js","Exn"), "isCamlExceptionOrOpenVariant")
18103+
1810318104

1810418105
let obj_magic : Longident.t =
1810518106
Ldot (Lident "Obj", "magic")

lib/js/caml_exceptions.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function create(str) {
2424
return v;
2525
}
2626

27-
function isCamlExceptionOrOpenVariant(e) {
27+
function caml_is_extension(e) {
2828
if (e === undefined) {
2929
return false;
3030
} else if (e.tag === 248) {
@@ -42,5 +42,5 @@ function isCamlExceptionOrOpenVariant(e) {
4242
exports.caml_set_oo_id = caml_set_oo_id;
4343
exports.caml_fresh_oo_id = caml_fresh_oo_id;
4444
exports.create = create;
45-
exports.isCamlExceptionOrOpenVariant = isCamlExceptionOrOpenVariant;
45+
exports.caml_is_extension = caml_is_extension;
4646
/* No side effect */

lib/js/caml_js_exceptions.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var Caml_exceptions = require("./caml_exceptions.js");
55
var $$Error = Caml_exceptions.create("Caml_js_exceptions.Error");
66

77
function internalToOCamlException(e) {
8-
if (Caml_exceptions.isCamlExceptionOrOpenVariant(e)) {
8+
if (Caml_exceptions.caml_is_extension(e)) {
99
return e;
1010
} else {
1111
return [

lib/whole_compiler.ml

+4-1
Original file line numberDiff line numberDiff line change
@@ -95300,6 +95300,8 @@ let translate loc (prim_name : string)
9530095300
like normal one to set the identifier *)
9530195301

9530295302

95303+
| "caml_is_extension" ->
95304+
call Js_runtime_modules.exceptions
9530395305
| "caml_set_oo_id" (* needed in {!camlinternalOO.set_id} *)
9530495306
->
9530595307
Js_of_lam_exception.caml_set_oo_id args
@@ -107129,7 +107131,8 @@ let record_as_js_object
107129107131

107130107132

107131107133
let isCamlExceptionOrOpenVariant : Longident.t =
107132-
Ldot (Lident "Caml_exceptions","isCamlExceptionOrOpenVariant")
107134+
Ldot (Ldot (Lident "Js","Exn"), "isCamlExceptionOrOpenVariant")
107135+
107133107136

107134107137
let obj_magic : Longident.t =
107135107138
Ldot (Lident "Obj", "magic")

0 commit comments

Comments
 (0)