Skip to content

Commit 6daa1b6

Browse files
committed
finish the new encoding, prepare to add stacktrace under a flag
1 parent e6d0ece commit 6daa1b6

File tree

266 files changed

+14778
-14148
lines changed

Some content is hidden

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

266 files changed

+14778
-14148
lines changed

jscomp/core/js_dump.ml

+11-4
Original file line numberDiff line numberDiff line change
@@ -819,13 +819,20 @@ and expression_desc cxt ~(level:int) f x : cxt =
819819
(* name convention of Record is slight different from modules
820820
*)
821821
| Caml_block(el,_, _, (Blk_extension | Blk_record_ext _ as ext )) ->
822+
let len = List.length el in
822823
let field_name =
823824
match ext with
824-
| Blk_extension -> (fun i -> if i = 0 then "CamlExt" else "_" ^ string_of_int i)
825+
| Blk_extension -> (fun i ->
826+
match i with
827+
| 0 -> Literals.exception_id
828+
| i ->
829+
if i < len - 1 then "_" ^ string_of_int i
830+
else Literals.exception_debug)
825831
| Blk_record_ext ss ->
826-
fun i ->
827-
if i = 0 then "CamlExt"
828-
else ss.(i-1)
832+
(fun i ->
833+
match i with
834+
| 0 -> Literals.exception_id
835+
| i -> if i < len - 1 then ss.(i-1) else Literals.exception_debug)
829836
| _ -> assert false in
830837
expression_desc cxt ~level f (Object (
831838
(Ext_list.mapi el (fun i e -> field_name i, e

jscomp/core/js_exp_make.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -413,13 +413,13 @@ let extension_access (e : t) name (pos : int32) : t =
413413
| None ->
414414
let name =
415415
match name with Some n -> n | None ->
416-
if pos = 0l then "CamlExt" else "_" ^ Int32.to_string pos in
416+
"_" ^ Int32.to_string pos in
417417
{ expression_desc = Static_index (e, name, Some pos); comment = None}
418418
)
419419
| _ ->
420420
let name =
421421
match name with Some n -> n | None ->
422-
if pos = 0l then "CamlExt" else "_" ^ Int32.to_string pos in
422+
"_" ^ Int32.to_string pos in
423423
{ expression_desc = Static_index (e, name, Some pos); comment = None}
424424

425425
let string_index ?comment (e0 : t) (e1 : t) : t =

jscomp/core/lam_convert.ml

+61-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,56 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
let caml_id_field_info : Lam_primitive.t = (Pfield (0, Fld_record {name = "CamlId"; mutable_flag = Mutable;}))
25+
26+
let caml_id_field_info : Lambda.field_dbg_info = Fld_record {name = Literals.exception_id; mutable_flag = Mutable;}
27+
let name_info : Lambda.field_dbg_info = Fld_record {name = Literals.exception_debug; mutable_flag = Immutable;}
28+
let lam_caml_id : Lam_primitive.t = Pfield(0, caml_id_field_info)
29+
let lam_name : Lam_primitive.t = Pfield (1, name_info)
2630
let prim = Lam.prim
2731

32+
33+
let num_of_ident name =
34+
begin match name with
35+
| "Out_of_memory" -> 0
36+
| "Sys_error" -> -1
37+
| "Failure" -> -2
38+
| "Invalid_argument" -> -3
39+
| "End_of_file" -> -4
40+
| "Division_by_zero" -> -5
41+
| "Not_found" -> -6
42+
| "Match_failure" -> -7
43+
| "Stack_overflow" -> -8
44+
| "Assert_failure" -> -9
45+
| "Sys_blocked_io" -> -10
46+
| "Undefined_recursive_module" -> -11
47+
| _ -> assert false
48+
end
49+
50+
51+
let lam_extension_id loc (head : Lam.t) =
52+
match head with
53+
| Lprim {primitive = Pglobal_exception id} ->
54+
Lam.const (Const_int {value = num_of_ident id.name; comment = Some id.name} )
55+
| _ -> prim ~primitive:lam_caml_id ~args:[head] loc
56+
57+
58+
let unbox_extension info (args : Lam.t list) mutable_flag loc =
59+
begin match args with
60+
| head :: rest ->
61+
let args =
62+
match head with
63+
| Lprim {primitive = Pglobal_exception id} ->
64+
let name = Ident.name id in
65+
let id = num_of_ident name in
66+
Lam.const (Const_int {value = id; comment = None})
67+
:: (Ext_list.append_one rest (Lam.const (Const_string (name))))
68+
| _ ->
69+
prim ~primitive:lam_caml_id ~args:[head] loc ::
70+
(Ext_list.append_one rest (prim ~primitive:lam_name ~args:[head] loc))
71+
in
72+
prim ~primitive:(Pmakeblock (0,info,mutable_flag)) ~args loc
73+
| _ -> assert false end
74+
2875
(** A conservative approach to avoid packing exceptions
2976
for lambda expression like {[
3077
try { ... }catch(id){body}
@@ -227,7 +274,19 @@ let lam_prim ~primitive:( p : Lambda.primitive) ~args loc : Lam.t =
227274
prim ~primitive:(Pmakeblock (tag,info,mutable_flag)) ~args loc
228275
| Blk_extension ->
229276
let info : Lam_tag_info.t = Blk_extension in
230-
prim ~primitive:(Pmakeblock (tag,info,mutable_flag)) ~args loc
277+
unbox_extension info args mutable_flag loc
278+
| Blk_record_ext s ->
279+
let info : Lam_tag_info.t = Blk_record_ext s in
280+
unbox_extension info args mutable_flag loc
281+
| Blk_extension_slot ->
282+
(
283+
match args with
284+
| [ Lconst (Const_string name)] ->
285+
prim ~primitive:(Pcreate_extension name) ~args:[] loc
286+
| _ ->
287+
assert false
288+
)
289+
231290
| Blk_class ->
232291
let info : Lam_tag_info.t = Blk_class in
233292
prim ~primitive:(Pmakeblock (tag,info,mutable_flag)) ~args loc

jscomp/ext/literals.ml

+2
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,5 @@ let sourcedirs_meta = ".sourcedirs.json"
139139
*)
140140
let ns_sep_char = '-'
141141
let ns_sep = "-"
142+
let exception_id = "ExceptionID"
143+
let exception_debug = "Debug"

jscomp/ext/literals.mli

+4-1
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,7 @@ val bsbuild_cache : string
135135
val sourcedirs_meta : string
136136

137137
val ns_sep_char : char
138-
val ns_sep : string
138+
val ns_sep : string
139+
140+
val exception_id : string
141+
val exception_debug : string

jscomp/main/builtin_cmj_datasets.ml

+31-31
Large diffs are not rendered by default.

jscomp/runtime/caml_exceptions.ml

+16-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
(** *)
25+
type t = {
26+
mutable id : nativeint [@bs.as "ExceptionID"];
27+
name : string [@bs.as "Debug"];
28+
}
29+
30+
31+
let make name id = {
32+
name ; id }
33+
2634

2735

2836

@@ -55,9 +63,9 @@ let caml_set_oo_id (b : Caml_obj_extern.t) : Caml_obj_extern.t =
5563

5664
(* let object_tag = 248 *)
5765

58-
let create (str : string) : Caml_builtin_exceptions.t =
66+
let create (str : string) : t =
5967
id .contents <- Caml_nativeint_extern.add id.contents 1n;
60-
Caml_builtin_exceptions.make str id.contents
68+
make str id.contents
6169

6270
(* let makeExtension (str : string) : Caml_builtin_exceptions.exception_block = *)
6371
(* let v = ( str, get_id ()) in *)
@@ -98,14 +106,14 @@ let create (str : string) : Caml_builtin_exceptions.t =
98106
This is not a problem in `try .. with` since the logic above is not expressible, see more design in [destruct_exn.md]
99107
*)
100108
let caml_is_extension = [%raw {|function (e){
101-
if(e == null || e.CamlExt == null) {
109+
if(e == null ) {
102110
return false
103111
}
104-
return typeof e.CamlExt.CamlId === "number"
112+
return typeof e.ExceptionID === "number"
105113
}
106114
|}]
107115

108-
type exn = { exn : Caml_builtin_exceptions.t [@bs.as "CamlExt"]}
116+
(* type exn = { exn : Caml_builtin_exceptions.t [@bs.as "CamlExt"]} *)
109117

110-
let caml_exn_slot_id x = x.exn.id
111-
let caml_exn_slot_name x = x.exn.name
118+
let caml_exn_slot_id (x : t) = x.id
119+
let caml_exn_slot_name (x : t) = x.name

jscomp/runtime/caml_exceptions.mli

+8-6
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,23 @@
2424

2525

2626

27+
type t
2728

28-
29-
29+
val make :
30+
string ->
31+
nativeint ->
32+
t
3033
(* It is not relevant to exception, just to piggy back on uuid mechanism *)
3134
val caml_set_oo_id :
3235
Caml_obj_extern.t ->
3336
Caml_obj_extern.t
3437

3538

36-
val create : string -> Caml_builtin_exceptions.t
39+
val create : string -> t
3740

3841
val caml_is_extension :
3942
'a -> bool
4043

41-
type exn
4244

43-
val caml_exn_slot_id : exn -> nativeint
44-
val caml_exn_slot_name : exn -> string
45+
val caml_exn_slot_id : t -> nativeint
46+
val caml_exn_slot_name : t -> string

0 commit comments

Comments
 (0)