Skip to content

Commit 8a31d02

Browse files
authored
Merge pull request #2771 from BuckleScript/debug
[part 1] [debug mode] provide runtime support
2 parents 92c8108 + 6921c7f commit 8a31d02

31 files changed

+994
-214
lines changed

jscomp/all.depend

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,8 @@ core/lam_compile_context.cmx : core/lam_stats.cmx core/lam.cmx core/j.cmx \
488488
core/js_map.cmx : core/j.cmx
489489
core/js_fold.cmx : core/j.cmx
490490
core/js_fold_basic.cmx : core/lam_module_ident.cmx \
491-
core/js_runtime_modules.cmx core/js_fold.cmx core/j.cmx ext/ident_set.cmx \
492-
core/js_fold_basic.cmi
491+
core/js_runtime_modules.cmx core/js_fold.cmx common/js_config.cmx \
492+
core/j.cmx ext/ident_set.cmx core/js_fold_basic.cmi
493493
core/js_pass_scope.cmx : core/js_fun_env.cmx core/js_fold.cmx \
494494
core/js_closure.cmx ext/ident_set.cmx common/ext_log.cmx ext/ext_list.cmx \
495495
core/js_pass_scope.cmi
@@ -595,9 +595,10 @@ core/js_dump_import_export.cmx : core/js_dump_string.cmx \
595595
core/js_dump_import_export.cmi
596596
core/js_dump.cmx : core/js_stmt_make.cmx core/js_runtime_modules.cmx \
597597
core/js_op_util.cmx core/js_op.cmx core/js_number.cmx core/js_fun_env.cmx \
598-
core/js_exp_make.cmx core/js_dump_string.cmx core/js_dump_property.cmx \
599-
core/js_dump_lit.cmx core/js_closure.cmx core/j.cmx ext/ident_set.cmx \
600-
ext/ext_pp_scope.cmx ext/ext_pp.cmx ext/ext_list.cmx ext/ext_ident.cmx \
598+
core/js_fold_basic.cmx core/js_exp_make.cmx core/js_dump_string.cmx \
599+
core/js_dump_property.cmx core/js_dump_lit.cmx common/js_config.cmx \
600+
core/js_closure.cmx core/j.cmx ext/ident_set.cmx ext/ext_pp_scope.cmx \
601+
ext/ext_pp.cmx ext/ext_list.cmx ext/ext_ident.cmx ext/ext_array.cmx \
601602
core/js_dump.cmi
602603
core/js_name_of_module_id.cmx : core/lam_compile_env.cmx \
603604
core/js_packages_state.cmx core/js_packages_info.cmx \

jscomp/bin/all_ounit_tests.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,7 @@ val range : int -> int -> int array
12921292

12931293
val map2i : (int -> 'a -> 'b -> 'c ) -> 'a array -> 'b array -> 'c array
12941294

1295+
val to_list_f : ('a -> 'b) -> 'a array -> 'b list
12951296
val to_list_map : ('a -> 'b option) -> 'a array -> 'b list
12961297

12971298
val to_list_map_acc :
@@ -1426,6 +1427,13 @@ let map2i f a b =
14261427
else
14271428
Array.mapi (fun i a -> f i a ( Array.unsafe_get b i )) a
14281429

1430+
let rec tolist_f_aux a f i res =
1431+
if i < 0 then res else
1432+
let v = Array.unsafe_get a i in
1433+
tolist_f_aux a f (i - 1)
1434+
(f v :: res)
1435+
1436+
let to_list_f f a = tolist_f_aux a f (Array.length a - 1) []
14291437

14301438
let rec tolist_aux a f i res =
14311439
if i < 0 then res else

jscomp/common/js_config.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,6 @@ let dump_js = ref false
104104
let syntax_only = ref false
105105
let binary_ast = ref false
106106

107-
let bs_suffix = ref false
107+
let bs_suffix = ref false
108+
109+
let debug = ref false

jscomp/common/js_config.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,4 @@ val binary_ast : bool ref
9494

9595

9696
val bs_suffix : bool ref
97+
val debug : bool ref

jscomp/core/js_dump.ml

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -798,25 +798,66 @@ and
798798
->
799799
(* Note that, if we ignore more than tag [0] we loose some information
800800
with regard tag *)
801-
begin match tag.expression_desc, tag_info with
802801

803-
| Number (Int { i = 0l ; _}) ,
804-
(Blk_tuple | Blk_array | Blk_variant _ | Blk_record _ | Blk_na | Blk_module _
805-
| Blk_constructor (_, 1) (* Sync up with {!Js_dump}*)
806-
)
807-
-> expression_desc cxt l f (Array (el, mutable_flag))
808802
(* TODO: for numbers like 248, 255 we can reverse engineer to make it
809803
[Obj.xx_flag], but we can not do this in runtime libraries
810804
*)
811805

812-
| _, _
813-
->
814-
P.string f L.caml_block;
815-
P.string f L.dot ;
816-
P.string f L.caml_block_create;
817-
P.paren_group f 1
818-
(fun _ -> arguments cxt f [tag; E.array mutable_flag el])
819-
end
806+
if Js_fold_basic.needBlockRuntime tag tag_info then begin
807+
match tag_info with
808+
| Blk_record labels ->
809+
P.string f L.caml_block;
810+
P.string f L.dot ;
811+
P.string f L.block_record;
812+
P.paren_group f 1
813+
(fun _ -> arguments cxt f
814+
[E.array Immutable
815+
(Ext_array.to_list_f E.str labels);
816+
E.array mutable_flag
817+
(List.map (fun (x : J.expression) -> {x with comment = None}) el) ]
818+
)
819+
| Blk_module (Some labels) ->
820+
P.string f L.caml_block;
821+
P.string f L.dot ;
822+
P.string f L.block_local_module;
823+
P.paren_group f 1
824+
(fun _ -> arguments cxt f
825+
[E.array Immutable
826+
(Ext_list.map E.str labels);
827+
E.array mutable_flag
828+
(List.map (fun (x :J.expression) -> {x with comment = None}) el)
829+
]
830+
)
831+
| Blk_variant name ->
832+
P.string f L.caml_block;
833+
P.string f L.dot ;
834+
P.string f L.block_poly_var;
835+
P.paren_group f 1
836+
(fun _ -> arguments cxt f
837+
[
838+
E.str name;
839+
E.array mutable_flag el]
840+
)
841+
| Blk_constructor(name,_) when !Js_config.debug ->
842+
P.string f L.caml_block;
843+
P.string f L.dot ;
844+
P.string f L.block_variant;
845+
P.paren_group f 1
846+
(fun _ -> arguments cxt f
847+
[E.str name; E.array mutable_flag el])
848+
849+
| _ ->
850+
begin
851+
P.string f L.caml_block;
852+
P.string f L.dot ;
853+
P.string f L.caml_block_create;
854+
P.paren_group f 1
855+
(fun _ -> arguments cxt f [tag; E.array mutable_flag el])
856+
end
857+
end
858+
else
859+
expression_desc cxt l f (Array (el, mutable_flag))
860+
820861
| Caml_block_tag e ->
821862
P.group f 1 (fun _ ->
822863
let cxt = expression 15 cxt f e in

jscomp/core/js_dump_lit.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,9 @@ let plus_plus = "++"
8484
let minus_minus = "--"
8585
let caml_block = "Block"
8686
let caml_block_create = "__"
87+
let block_record = "record"
88+
let block_local_module = "localModule"
89+
let block_poly_var = "polyVar"
90+
let block_variant = "variant"
91+
8792
let case = "case"

jscomp/core/js_fold_basic.ml

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,52 @@
2525

2626

2727

28+
let needBlockRuntimeInDebugMode
29+
(tag : J.expression)
30+
(tag_info : J.tag_info) =
31+
match tag_info with
32+
| Blk_variant _
33+
| Blk_module _
34+
| Blk_record _
35+
| Blk_constructor _ -> true
36+
| Blk_tuple
37+
| Blk_array
38+
| Blk_exception
39+
| Blk_extension -> false
40+
| Blk_na ->
41+
begin match tag.expression_desc with
42+
| Number (Int { i = 0l ; _})
43+
->
44+
false
45+
| _ -> true
46+
end
2847

48+
let needBlockRuntimeInReleaseMode (tag : J.expression) (tag_info : J.tag_info) =
49+
match tag_info with
50+
| Blk_module _
51+
| Blk_record _
52+
| Blk_constructor (_, 1)
53+
| Blk_variant _
54+
| Blk_na
55+
| Blk_tuple
56+
| Blk_array
57+
->
58+
begin match tag.expression_desc with
59+
| Number (Int { i = 0l ; _})
60+
->
61+
false
62+
| _ -> true
63+
end
64+
| Blk_constructor _ -> true
65+
| Blk_exception
66+
| Blk_extension -> false
67+
(* converted to [Pcreate_extension] in the beginning*)
68+
2969

30-
31-
70+
let needBlockRuntime tag info =
71+
if !Js_config.debug then
72+
needBlockRuntimeInDebugMode tag info
73+
else needBlockRuntimeInReleaseMode tag info
3274

3375
class count_deps (add : Ident.t -> unit ) =
3476
object(self)
@@ -76,18 +118,11 @@ class count_hard_dependencies =
76118
super#expression x
77119
| {expression_desc = Caml_block(_,_, tag, tag_info); _}
78120
->
79-
begin match tag.expression_desc, tag_info with
80-
| Number (Int { i = 0l ; _}) ,
81-
(Blk_tuple | Blk_array | Blk_variant _ | Blk_record _ | Blk_na | Blk_module _
82-
| Blk_constructor (_, 1)
83-
) (*Sync up with {!Js_dump}*)
84-
-> ()
85-
| _, _
86-
->
87-
add_lam_module_ident hard_dependencies
121+
if needBlockRuntime tag tag_info then
122+
add_lam_module_ident hard_dependencies
88123
(Lam_module_ident.of_runtime
89-
(Ident.create_persistent Js_runtime_modules.block));
90-
end;
124+
(Ident.create_persistent Js_runtime_modules.block))
125+
;
91126
super#expression x
92127
| _ -> super#expression x
93128
method get_hard_dependencies = hard_dependencies

jscomp/core/js_fold_basic.mli

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727

2828

2929

30-
30+
val needBlockRuntime :
31+
J.expression ->
32+
J.tag_info ->
33+
bool
3134

3235
(** A module to calculate hard dependency based on JS IR in module [J] *)
3336

jscomp/core/js_main.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ let buckle_script_flags : (string * Arg.spec * string) list =
145145
" (experimental) Set the string to be evaluated, note this flag will be conflicted with -bs-main"
146146
)
147147
::
148+
("-bs-g",
149+
Arg.Set Js_config.debug,
150+
" debug mode"
151+
)
152+
::
148153
(
149154
"-bs-sort-imports",
150155
Arg.Set Js_config.sort_imports,

jscomp/ext/ext_array.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ let map2i f a b =
9696
else
9797
Array.mapi (fun i a -> f i a ( Array.unsafe_get b i )) a
9898

99+
let rec tolist_f_aux a f i res =
100+
if i < 0 then res else
101+
let v = Array.unsafe_get a i in
102+
tolist_f_aux a f (i - 1)
103+
(f v :: res)
104+
105+
let to_list_f f a = tolist_f_aux a f (Array.length a - 1) []
99106

100107
let rec tolist_aux a f i res =
101108
if i < 0 then res else

0 commit comments

Comments
 (0)