Skip to content

Commit 4bb4320

Browse files
committed
improve exception creation (#277)
* improve exception creation * clean up
1 parent a0f1296 commit 4bb4320

Some content is hidden

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

48 files changed

+310
-453
lines changed

jscomp/js_config.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ let runtime_set = String_set.of_list [
146146
let prim = "Caml_primitive"
147147

148148
let builtin_exceptions = "Caml_builtin_exceptions"
149-
149+
let exceptions = "Caml_exceptions"
150150

151151
let io = "Caml_io"
152152

jscomp/js_config.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ val stdlib_set : String_set.t
3737
val prim : string
3838

3939
val builtin_exceptions : string
40-
40+
val exceptions : string
4141
val io : string
4242

4343
val oo : string

jscomp/js_of_lam_exception.ml

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,15 @@ let match_exception_def (args : J.expression list) =
3939
None
4040
| _ -> None
4141

42-
(* Sync up with [caml_set_oo_id] *)
42+
(* Sync up with [caml_set_oo_id]
43+
Note if we inline {!Caml_exceptions.create},
44+
it seems can be useful for optimizations in theory,
45+
in practice, it never happen, since the pattern match
46+
never dig into it internally, so maybe {!Obj.set_tag}
47+
is not necessary at all
48+
*)
4349
let make_exception exception_str mutable_flag : J.expression =
44-
{ expression_desc =
45-
Caml_block (
46-
[exception_str ;
47-
E.runtime_call Js_config.builtin_exceptions "get_id" []
48-
],
49-
mutable_flag,
50-
(* TODO: combined with `_001` optimization *)
51-
E.obj_int_tag_literal (* (Obj.object_tag) *),
52-
Blk_na
53-
);
54-
comment = None
55-
}
50+
E.runtime_call Js_config.exceptions Literals.create [exception_str]
5651

5752

5853

@@ -70,5 +65,5 @@ let caml_set_oo_id args =
7065
If we can guarantee this code path is never hit, we can do
7166
a better job for encoding of exception and extension?
7267
*)
73-
E.runtime_call Js_config.builtin_exceptions "caml_set_oo_id" args
68+
E.runtime_call Js_config.exceptions "caml_set_oo_id" args
7469
end

jscomp/literals.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ let prim = "prim"
3030
let param = "param"
3131
let partial_arg = "partial_arg"
3232
let tmp = "tmp"
33+
34+
let create = "create" (* {!Caml_exceptions.create}*)

jscomp/literals.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ val prim : string
3131

3232
(**temporary varaible used in {!Js_ast_util} *)
3333
val tmp : string
34+
35+
val create : string

jscomp/runtime/.depend

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
caml_array.cmi :
22
caml_bigarray.cmi :
33
caml_builtin_exceptions.cmi :
4+
caml_exceptions.cmi : caml_builtin_exceptions.cmi
45
caml_float.cmi :
56
caml_format.cmi :
67
caml_hash.cmi :
@@ -23,6 +24,8 @@ caml_builtin_exceptions.cmo : caml_builtin_exceptions.cmi
2324
caml_builtin_exceptions.cmx : caml_builtin_exceptions.cmi
2425
caml_curry.cmo : js.cmo caml_oo.cmi
2526
caml_curry.cmx : js.cmx caml_oo.cmx
27+
caml_exceptions.cmo : caml_builtin_exceptions.cmi caml_exceptions.cmi
28+
caml_exceptions.cmx : caml_builtin_exceptions.cmx caml_exceptions.cmi
2629
caml_float.cmo : typed_array.cmo js.cmo caml_float.cmi
2730
caml_float.cmx : typed_array.cmx js.cmx caml_float.cmi
2831
caml_format.cmo : js.cmo caml_utils.cmi caml_format.cmi
@@ -69,6 +72,8 @@ caml_builtin_exceptions.cmo : caml_builtin_exceptions.cmi
6972
caml_builtin_exceptions.cmj : caml_builtin_exceptions.cmi
7073
caml_curry.cmo : js.cmo caml_oo.cmi
7174
caml_curry.cmj : js.cmj caml_oo.cmj
75+
caml_exceptions.cmo : caml_builtin_exceptions.cmi caml_exceptions.cmi
76+
caml_exceptions.cmj : caml_builtin_exceptions.cmj caml_exceptions.cmi
7277
caml_float.cmo : typed_array.cmo js.cmo caml_float.cmi
7378
caml_float.cmj : typed_array.cmj js.cmj caml_float.cmi
7479
caml_format.cmo : js.cmo caml_utils.cmi caml_format.cmi

jscomp/runtime/caml_builtin_exceptions.js

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,87 +2,62 @@
22
'use strict';
33

44

5-
var id = [0];
6-
7-
function caml_set_oo_id(b) {
8-
b[1] = id[0];
9-
id[0] += 1;
10-
return b;
11-
}
12-
13-
function get_id() {
14-
id[0] += 1;
15-
return id[0];
16-
}
17-
185
var out_of_memory = /* tuple */[
19-
248,
206
"Out_of_memory",
217
0
228
];
239

2410
var sys_error = /* tuple */[
25-
248,
2611
"Sys_error",
2712
-1
2813
];
2914

3015
var failure = /* tuple */[
31-
248,
3216
"Failure",
3317
-2
3418
];
3519

3620
var invalid_argument = /* tuple */[
37-
248,
3821
"Invalid_argument",
3922
-3
4023
];
4124

4225
var end_of_file = /* tuple */[
43-
248,
4426
"End_of_file",
4527
-4
4628
];
4729

4830
var division_by_zero = /* tuple */[
49-
248,
5031
"Division_by_zero",
5132
-5
5233
];
5334

5435
var not_found = /* tuple */[
55-
248,
5636
"Not_found",
5737
-6
5838
];
5939

6040
var match_failure = /* tuple */[
61-
248,
6241
"Match_failure",
6342
-7
6443
];
6544

6645
var stack_overflow = /* tuple */[
67-
248,
6846
"Stack_overflow",
6947
-8
7048
];
7149

7250
var sys_blocked_io = /* tuple */[
73-
248,
7451
"Sys_blocked_io",
7552
-9
7653
];
7754

7855
var assert_failure = /* tuple */[
79-
248,
8056
"Assert_failure",
8157
-10
8258
];
8359

8460
var undefined_recursive_module = /* tuple */[
85-
248,
8661
"Undefined_recursive_module",
8762
-11
8863
];
@@ -99,6 +74,4 @@ exports.stack_overflow = stack_overflow;
9974
exports.sys_blocked_io = sys_blocked_io;
10075
exports.assert_failure = assert_failure;
10176
exports.undefined_recursive_module = undefined_recursive_module;
102-
exports.caml_set_oo_id = caml_set_oo_id;
103-
exports.get_id = get_id;
10477
/* No side effect */

jscomp/runtime/caml_builtin_exceptions.ml

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,23 @@
1818

1919
(* Author: Hongbo Zhang *)
2020

21-
type exception_block = int * string * int
22-
let out_of_memory = 248, "Out_of_memory", 0
23-
let sys_error = 248, "Sys_error", -1
24-
let failure = 248, "Failure", -2
25-
let invalid_argument = 248, "Invalid_argument", -3
26-
let end_of_file = 248, "End_of_file",-4
27-
let division_by_zero = 248, "Division_by_zero", -5
28-
let not_found = 248, "Not_found", -6
29-
let match_failure = 248, "Match_failure", -7
30-
let stack_overflow = 248, "Stack_overflow",-8
31-
let sys_blocked_io = 248, "Sys_blocked_io", -9
32-
let assert_failure = 248, "Assert_failure", -10
33-
let undefined_recursive_module = 248, "Undefined_recursive_module", -11
34-
35-
36-
37-
(* Exported for better inlining *)
38-
(* It's common that we have <code> a = caml_set_oo_id([248,"string",0])</code> *)
39-
(* This can be inlined as <code> a = caml_set_oo_id([248,"tag", caml_oo_last_id++])</code> *)
40-
(* @type {number} *)
41-
42-
let id = ref 0n
43-
44-
45-
(* see #251
46-
{[
47-
CAMLprim value caml_set_oo_id (value obj) {
48-
Field(obj, 1) = oo_last_id;
49-
oo_last_id += 2;
50-
return obj;
51-
}
52-
53-
]}*)
54-
let caml_set_oo_id (b : exception_block) =
55-
Obj.set_field (Obj.repr b) 1 (Obj.repr !id);
56-
id := Nativeint.add !id 1n;
57-
b
58-
59-
let get_id () =
60-
id := Nativeint.add !id 1n; !id
21+
type exception_block = string * nativeint
22+
23+
24+
let out_of_memory = "Out_of_memory", 0n
25+
let sys_error = "Sys_error", -1n
26+
let failure = "Failure", -2n
27+
let invalid_argument = "Invalid_argument", -3n
28+
let end_of_file = "End_of_file",-4n
29+
let division_by_zero = "Division_by_zero", -5n
30+
let not_found = "Not_found", -6n
31+
let match_failure = "Match_failure", -7n
32+
let stack_overflow = "Stack_overflow",-8n
33+
let sys_blocked_io = "Sys_blocked_io", -9n
34+
let assert_failure = "Assert_failure", -10n
35+
let undefined_recursive_module = "Undefined_recursive_module", -11n
36+
37+
(* TODO:
38+
1. is it necessary to tag [248] here
39+
2. is it okay to remove the negative value
40+
*)

jscomp/runtime/caml_builtin_exceptions.mli

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type exception_block = int * string * int
1+
type exception_block = string * nativeint
22
val out_of_memory : exception_block
33
val sys_error : exception_block
44
val failure : exception_block
@@ -12,6 +12,5 @@ val sys_blocked_io : exception_block
1212
val assert_failure : exception_block
1313
val undefined_recursive_module : exception_block
1414

15-
val caml_set_oo_id : exception_block -> exception_block
16-
val get_id : unit -> nativeint
15+
1716

jscomp/runtime/caml_exceptions.js

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,31 @@
11
// Generated CODE, PLEASE EDIT WITH CARE
22
'use strict';
33

4-
var Caml_builtin_exceptions = require("./caml_builtin_exceptions");
54

6-
function caml_raise_sys_error(msg) {
7-
throw [
8-
Caml_builtin_exceptions.sys_error,
9-
msg
10-
];
11-
}
12-
13-
function caml_failwith(s) {
14-
throw [
15-
Caml_builtin_exceptions.failure,
16-
s
17-
];
18-
}
19-
20-
function caml_invalid_argument(s) {
21-
throw [
22-
Caml_builtin_exceptions.invalid_argument,
23-
s
24-
];
25-
}
26-
27-
function caml_array_bound_error() {
28-
throw [
29-
Caml_builtin_exceptions.invalid_argument,
30-
"index out of bounds"
31-
];
32-
}
5+
var id = [0];
336

34-
function caml_raise_zero_divide() {
35-
throw Caml_builtin_exceptions.division_by_zero;
7+
function caml_set_oo_id(b) {
8+
b[1] = id[0];
9+
id[0] += 1;
10+
return b;
3611
}
3712

38-
function caml_raise_not_found() {
39-
throw Caml_builtin_exceptions.not_found;
13+
function get_id() {
14+
id[0] += 1;
15+
return id[0];
4016
}
4117

42-
function caml_undef_module(loc) {
43-
throw [
44-
Caml_builtin_exceptions.undefined_recursive_module,
45-
loc
46-
];
18+
function create(str) {
19+
var v_001 = get_id(/* () */0);
20+
var v = /* tuple */[
21+
str,
22+
v_001
23+
];
24+
v.tag = 248;
25+
return v;
4726
}
4827

49-
exports.caml_raise_sys_error = caml_raise_sys_error;
50-
exports.caml_failwith = caml_failwith;
51-
exports.caml_invalid_argument = caml_invalid_argument;
52-
exports.caml_array_bound_error = caml_array_bound_error;
53-
exports.caml_raise_zero_divide = caml_raise_zero_divide;
54-
exports.caml_raise_not_found = caml_raise_not_found;
55-
exports.caml_undef_module = caml_undef_module;
28+
exports.caml_set_oo_id = caml_set_oo_id;
29+
exports.get_id = get_id;
30+
exports.create = create;
5631
/* No side effect */

jscomp/runtime/caml_exceptions.ml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
(* BuckleScript compiler
2+
* Copyright (C) 2015-2016 Bloomberg Finance L.P.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, with linking exception;
7+
* either version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17+
*)
18+
19+
(* Author: Hongbo Zhang *)
20+
21+
(**
22+
Could be exported for better inlining
23+
It's common that we have
24+
{[ a = caml_set_oo_id([248,"string",0]) ]}
25+
This can be inlined as
26+
{[ a = caml_set_oo_id([248,"tag", caml_oo_last_id++]) ]}
27+
*)
28+
29+
let id = ref 0n
30+
31+
32+
(* see #251
33+
{[
34+
CAMLprim value caml_set_oo_id (value obj) {
35+
Field(obj, 1) = oo_last_id;
36+
oo_last_id += 2;
37+
return obj;
38+
}
39+
40+
]}*)
41+
let caml_set_oo_id (b : Caml_builtin_exceptions.exception_block) =
42+
Obj.set_field (Obj.repr b) 1 (Obj.repr !id);
43+
id := Nativeint.add !id 1n;
44+
b
45+
46+
let get_id () =
47+
id := Nativeint.add !id 1n; !id
48+
49+
let create (str : string) : Caml_builtin_exceptions.exception_block =
50+
let v = ( str, get_id ()) in
51+
Obj.set_tag (Obj.repr v) 248 (* Obj.object_tag*);
52+
v

0 commit comments

Comments
 (0)