Skip to content

Commit 9282418

Browse files
Hongbo Zhangbobzhang
Hongbo Zhang
authored andcommitted
refactoring scope printer and print [require] and [exports] with alignment
TODO: work with amd module system as well
1 parent 79ae97d commit 9282418

File tree

209 files changed

+2257
-2000
lines changed

Some content is hidden

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

209 files changed

+2257
-2000
lines changed

jscomp/ext_list.ml

+8
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,11 @@ let rev_map_acc acc f l =
180180
| a::l -> rmap_f (f a :: accu) l
181181
in
182182
rmap_f acc l
183+
184+
let rec rev_iter f xs =
185+
match xs with
186+
| [] -> ()
187+
| y :: ys ->
188+
rev_iter f ys ;
189+
f y
190+

jscomp/ext_list.mli

+2
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,5 @@ val fold : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
6767
val rev_map_append : ('a -> 'b) -> 'a list -> 'b list -> 'b list
6868

6969
val rev_map_acc : 'a list -> ('b -> 'a) -> 'b list -> 'a list
70+
71+
val rev_iter : ('a -> unit) -> 'a list -> unit

jscomp/ext_pp.ml

+3
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ let force_newline t =
8080
let space t =
8181
string t L.space
8282

83+
let nspace t n =
84+
string t (String.make n ' ')
85+
8386
let group t i action =
8487
if i = 0 then action ()
8588
else

jscomp/ext_pp.mli

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ val string : t -> string -> unit
3939

4040
val space : t -> unit
4141

42+
val nspace : t -> int -> unit
43+
4244
val group : t -> int -> (unit -> 'a) -> 'a
4345
(** [group] will record current indentation
4446
and indent futher

jscomp/ext_pp_scope.ml

+11-20
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ type t =
2626
let empty =
2727
String_map.empty
2828

29+
let rec print fmt v =
30+
Format.fprintf fmt "@[<v>{" ;
31+
String_map.iter (fun k m ->
32+
Format.fprintf fmt "%s: @[%a@],@ " k print_int_map m
33+
) v;
34+
Format.fprintf fmt "}@]"
35+
and print_int_map fmt m =
36+
Int_map.iter (fun k v ->
37+
Format.fprintf fmt "%d - %d" k v
38+
) m
39+
2940
let add_ident (id : Ident.t) (cxt : t) : int * t =
3041
match String_map.find id.name cxt with
3142
| exception Not_found -> (0, String_map.add id.name Int_map.(add id.stamp 0 empty) cxt )
@@ -60,26 +71,6 @@ let sub_scope (scope : t) ident_collection : t =
6071
)
6172
) ident_collection cxt
6273

63-
(* purely functional environment *)
6474

65-
module P = Ext_format
6675

67-
let string_of_id (id : Ident.t) ( cxt : t ) : int * t =
6876

69-
match String_map.find id.name cxt with
70-
| exception Not_found ->
71-
(
72-
0,
73-
String_map.add id.name Int_map.(add id.stamp 0 empty) cxt
74-
)
75-
| imap -> (* stamp -> print id *)
76-
begin
77-
match Int_map.find id.stamp imap with
78-
| exception Not_found ->
79-
let v = Int_map.cardinal imap in
80-
(
81-
v,
82-
String_map.add id.name (Int_map.(add id.stamp v imap) : int Int_map.t) cxt
83-
)
84-
| i -> (i, cxt)
85-
end

jscomp/ext_pp_scope.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ val sub_scope : t -> Ident_set.t -> t
3838

3939
val merge : Ident_set.t -> t -> t
4040

41-
val string_of_id : Ident.t -> t -> int * t
41+
val print : Format.formatter -> t -> unit

jscomp/ext_string.ml

+9
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,12 @@ let for_all (p : char -> bool) s =
9191
in aux 0
9292

9393
let is_empty s = String.length s = 0
94+
95+
96+
let repeat n s =
97+
let len = String.length s in
98+
let res = Bytes.create(n * len) in
99+
for i = 0 to pred n do
100+
String.blit s 0 res (i * len) len
101+
done;
102+
Bytes.to_string res

jscomp/ext_string.mli

+2
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,5 @@ val escaped : string -> string
3737
val for_all : (char -> bool) -> string -> bool
3838

3939
val is_empty : string -> bool
40+
41+
val repeat : int -> string -> string

jscomp/js_dump.ml

+83-65
Original file line numberDiff line numberDiff line change
@@ -103,22 +103,24 @@ let best_string_quote s =
103103
then '\''
104104
else '"'
105105

106-
let ident (cxt : Ext_pp_scope.t) f (id : Ident.t) : Ext_pp_scope.t =
106+
107+
(**
108+
same as {!Js_dump.ident} except it generates a string instead of doing the printing
109+
*)
110+
let str_of_ident (cxt : Ext_pp_scope.t) (id : Ident.t) =
107111
if Ext_ident.is_js id then (* reserved by compiler *)
108-
begin P.string f id.name ; cxt end
112+
( id.name , cxt)
109113
else
110-
(* if false then *)
111-
(* (\** Faster print .. *)
112-
(* Also for debugging *)
113-
(* *\) *)
114-
(* let name = Ext_ident.convert id.name in *)
115-
(* ( P.string f (Printf.sprintf "%s$%d" name id.stamp ); cxt) *)
116-
(* else *)
114+
(* For fast/debug mode, we can generate the name as
115+
[Printf.sprintf "%s$%d" name id.stamp] which is
116+
not relevant to the context
117+
*)
117118
let name = Ext_ident.convert id.name in
119+
let i,new_cxt = Ext_pp_scope.add_ident id cxt in
118120
(* Attention:
119-
$$Array.length, there is an invariant: that global module is
120-
always printed in the begining(in the imports), so you get a gurantee,
121-
(global modules can not be renamed like List$1)
121+
$$Array.length, due to the fact that global module is
122+
always printed in the begining(via imports), so you get a gurantee,
123+
(global modules will not be printed as [List$1])
122124
123125
However, this means we loose the ability of dynamic loading, is it a big
124126
deal? we can fix this by a scanning first, since we already know which
@@ -127,14 +129,16 @@ let ident (cxt : Ext_pp_scope.t) f (id : Ident.t) : Ext_pp_scope.t =
127129
check [test/test_global_print.ml] for regression
128130
129131
*)
130-
let i,new_cxt = Ext_pp_scope.string_of_id id cxt in
131-
let () =
132-
P.string f
133-
(if i == 0 then
134-
name (* var $$String = require("String")*)
135-
else
136-
Printf.sprintf"%s$%d" name i) in
137-
new_cxt
132+
(if i == 0 then
133+
name
134+
else
135+
Printf.sprintf"%s$%d" name i), new_cxt
136+
137+
138+
let ident (cxt : Ext_pp_scope.t) f (id : Ident.t) : Ext_pp_scope.t =
139+
let str, cxt = str_of_ident cxt id in
140+
P.string f str;
141+
cxt
138142

139143
let pp_string f ?(quote='"') ?(utf=false) s =
140144
let array_str1 =
@@ -200,7 +204,9 @@ f/122 -->
200204
else check last bumped id, increase it and register
201205
*)
202206

203-
let rec pp_function cxt (f : P.t) ?name return (l : Ident.t list) (b : J.block) (env : Js_fun_env.t ) =
207+
let rec pp_function
208+
cxt (f : P.t) ?name return
209+
(l : Ident.t list) (b : J.block) (env : Js_fun_env.t ) =
204210
let ipp_ident cxt f id un_used =
205211
if un_used then
206212
ident cxt f (Ext_ident.make_unused ())
@@ -241,7 +247,13 @@ let rec pp_function cxt (f : P.t) ?name return (l : Ident.t list) (b : J.block)
241247
in
242248
(* the context will be continued after this function *)
243249
let outer_cxt = Ext_pp_scope.merge set_env cxt in
244-
(* the context used to be printed inside this function*)
250+
251+
(* the context used to be printed inside this function
252+
253+
when printing a function,
254+
only the enclosed variables and function name matters,
255+
if the function does not capture any variable, then the context is empty
256+
*)
245257
let inner_cxt = Ext_pp_scope.sub_scope outer_cxt set_env in
246258

247259
(
@@ -1227,64 +1239,70 @@ and statement_list top cxt f b =
12271239
(if top then P.force_newline f);
12281240
statement_list top cxt f r
12291241

1230-
(* and statement_list cxt f b = *)
1231-
(* match b with *)
1232-
(* | [] -> cxt *)
1233-
(* | _ -> P.vgroup f 0 (fun _ -> loop_statement cxt f b) *)
1234-
12351242
and block cxt f b =
12361243
(* This one is for '{' *)
12371244
P.brace_vgroup f 1 (fun _ -> statement_list false cxt f b )
12381245

1239-
(* Node style *)
1240-
let requires cxt f (modules : (Ident.t * string) list ) =
1241-
P.newline f ;
1242-
let rec aux cxt modules =
1243-
match modules with
1244-
| [] -> cxt
1245-
| (id,s) :: rest ->
1246-
let cxt = P.group f 0 @@ fun _ ->
1247-
P.string f L.var;
1248-
P.space f ;
1249-
let cxt = ident cxt f id in
1250-
P.space f;
1251-
P.string f L.eq;
1252-
P.space f;
1253-
P.string f L.require;
1254-
P.paren_group f 0 @@ (fun _ ->
1255-
pp_string f ~utf:true ~quote:(best_string_quote s) s );
1256-
cxt in
1257-
semi f ;
1258-
P.newline f ;
1259-
aux cxt rest
1260-
in aux cxt modules
12611246

12621247
let exports cxt f (idents : Ident.t list) =
1248+
let outer_cxt, reversed_list, margin =
1249+
List.fold_left (fun (cxt, acc, len ) (id : Ident.t) ->
1250+
let s = Ext_ident.convert id.name in
1251+
let str,cxt = str_of_ident cxt id in
1252+
cxt, ( (s,str) :: acc ) , max len (String.length s) )
1253+
(cxt, [], 0) idents in
12631254
P.newline f ;
1264-
List.iter (fun (id : Ident.t) ->
1255+
Ext_list.rev_iter (fun (s,export) ->
12651256
P.group f 0 @@ (fun _ ->
12661257
P.string f L.exports;
12671258
P.string f L.dot;
1268-
P.string f (Ext_ident.convert id.name);
1269-
P.space f ;
1259+
P.string f s;
1260+
P.nspace f (margin - String.length s + 1) ;
12701261
P.string f L.eq;
12711262
P.space f;
1272-
ignore @@ ident cxt f id;
1263+
P.string f export;
12731264
semi f;);
12741265
P.newline f;
1275-
)
1276-
idents
1266+
) reversed_list;
1267+
outer_cxt
12771268

1278-
let node_program
1279-
f
1280-
({modules; block = b ; exports = exp ; side_effect } : J.program)
1281-
=
1269+
1270+
let node_program f ( program : J.program) =
12821271
let cxt = Ext_pp_scope.empty in
1283-
let cxt = requires cxt f modules in
1272+
(* Node style *)
1273+
let requires cxt f (modules : (Ident.t * string) list ) =
1274+
P.newline f ;
1275+
(* the context used to print the following program *)
1276+
let outer_cxt, reversed_list, margin =
1277+
List.fold_left
1278+
(fun (cxt, acc, len) (id,s) ->
1279+
let str, cxt = str_of_ident cxt id in
1280+
cxt, ((str,s) :: acc), (max len (String.length str))
1281+
)
1282+
(cxt, [], 0) modules in
1283+
P.force_newline f ;
1284+
Ext_list.rev_iter (fun (s,file) ->
1285+
P.string f L.var;
1286+
P.space f ;
1287+
P.string f s ;
1288+
P.nspace f (margin - String.length s + 1) ;
1289+
P.string f L.eq;
1290+
P.space f;
1291+
P.string f L.require;
1292+
P.paren_group f 0 @@ (fun _ ->
1293+
pp_string f ~utf:true ~quote:(best_string_quote s) file );
1294+
semi f ;
1295+
P.newline f ;
1296+
) reversed_list;
1297+
outer_cxt
1298+
in
1299+
1300+
let cxt = requires cxt f program.modules in
1301+
12841302
let () = P.force_newline f in
1285-
let cxt = statement_list true cxt f b in
1303+
let cxt = statement_list true cxt f program.block in
12861304
let () = P.force_newline f in
1287-
exports cxt f exp
1305+
exports cxt f program.exports
12881306

12891307

12901308
let amd_program f ({modules; block = b ; exports = exp ; side_effect } : J.program)
@@ -1358,13 +1376,13 @@ let pp_program (program : J.program) (f : Ext_pp.t) =
13581376
let () =
13591377
P.string f "// Generated CODE, PLEASE EDIT WITH CARE";
13601378
P.newline f;
1361-
P.newline f ;
13621379
P.string f {|"use strict";|};
1380+
P.newline f ;
13631381
in
13641382
(match Sys.getenv "OCAML_AMD_MODULE" with
13651383
| exception Not_found ->
1366-
node_program f program
1367-
| _ -> amd_program f program ) ;
1384+
ignore (node_program f program)
1385+
| _ -> amd_program f program ) ;
13681386
P.string f (
13691387
match program.side_effect with
13701388
| None -> "/* No side effect */"

jscomp/runtime/caml_array.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Generated CODE, PLEASE EDIT WITH CARE
22
"use strict";
33

4+
45
function caml_array_sub(x, offset, len) {
56
var result = new Array(len);
67
var j = 0;
@@ -80,8 +81,8 @@ function caml_array_blit(a1, i1, a2, i2, len) {
8081
}
8182
}
8283

83-
exports.caml_array_sub = caml_array_sub;
84+
exports.caml_array_sub = caml_array_sub;
8485
exports.caml_array_concat = caml_array_concat;
85-
exports.caml_make_vect = caml_make_vect;
86-
exports.caml_array_blit = caml_array_blit;
86+
exports.caml_make_vect = caml_make_vect;
87+
exports.caml_array_blit = caml_array_blit;
8788
/* No side effect */

jscomp/runtime/caml_oo.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Generated CODE, PLEASE EDIT WITH CARE
22
"use strict";
3+
34
var Caml_exceptions = require("./caml_exceptions");
4-
var Caml_array = require("./caml_array");
5+
var Caml_array = require("./caml_array");
56

67
var caml_methods_cache = Caml_array.caml_make_vect(1000, 0);
78

jscomp/runtime/caml_string.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Generated CODE, PLEASE EDIT WITH CARE
22
"use strict";
3+
34
var Caml_exceptions = require("./caml_exceptions");
45

56
function add(prim, prim$1) {
@@ -157,16 +158,16 @@ function caml_is_printable(c) {
157158
return +(code > 31 && code < 127);
158159
}
159160

160-
exports.add = add;
161-
exports.bytes_cat = bytes_cat;
162-
exports.bytes_of_string = bytes_of_string;
163-
exports.bytes_to_string = bytes_to_string;
164-
exports.caml_is_printable = caml_is_printable;
161+
exports.add = add;
162+
exports.bytes_cat = bytes_cat;
163+
exports.bytes_of_string = bytes_of_string;
164+
exports.bytes_to_string = bytes_to_string;
165+
exports.caml_is_printable = caml_is_printable;
165166
exports.caml_string_of_char_array = caml_string_of_char_array;
166-
exports.caml_string_get = caml_string_get;
167-
exports.caml_string_compare = caml_string_compare;
168-
exports.caml_create_string = caml_create_string;
169-
exports.caml_fill_string = caml_fill_string;
170-
exports.caml_blit_string = caml_blit_string;
171-
exports.caml_blit_bytes = caml_blit_bytes;
167+
exports.caml_string_get = caml_string_get;
168+
exports.caml_string_compare = caml_string_compare;
169+
exports.caml_create_string = caml_create_string;
170+
exports.caml_fill_string = caml_fill_string;
171+
exports.caml_blit_string = caml_blit_string;
172+
exports.caml_blit_bytes = caml_blit_bytes;
172173
/* No side effect */

0 commit comments

Comments
 (0)