@@ -103,22 +103,24 @@ let best_string_quote s =
103
103
then '\' '
104
104
else '"'
105
105
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 ) =
107
111
if Ext_ident. is_js id then (* reserved by compiler *)
108
- begin P. string f id.name ; cxt end
112
+ ( id.name , cxt)
109
113
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
+ *)
117
118
let name = Ext_ident. convert id.name in
119
+ let i,new_cxt = Ext_pp_scope. add_ident id cxt in
118
120
(* 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] )
122
124
123
125
However, this means we loose the ability of dynamic loading, is it a big
124
126
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 =
127
129
check [test/test_global_print.ml] for regression
128
130
129
131
*)
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
138
142
139
143
let pp_string f ?(quote ='"' ) ?(utf =false ) s =
140
144
let array_str1 =
@@ -200,7 +204,9 @@ f/122 -->
200
204
else check last bumped id, increase it and register
201
205
*)
202
206
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 ) =
204
210
let ipp_ident cxt f id un_used =
205
211
if un_used then
206
212
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)
241
247
in
242
248
(* the context will be continued after this function *)
243
249
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
+ *)
245
257
let inner_cxt = Ext_pp_scope. sub_scope outer_cxt set_env in
246
258
247
259
(
@@ -1227,64 +1239,70 @@ and statement_list top cxt f b =
1227
1239
(if top then P. force_newline f);
1228
1240
statement_list top cxt f r
1229
1241
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
-
1235
1242
and block cxt f b =
1236
1243
(* This one is for '{' *)
1237
1244
P. brace_vgroup f 1 (fun _ -> statement_list false cxt f b )
1238
1245
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
1261
1246
1262
1247
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
1263
1254
P. newline f ;
1264
- List. iter (fun (id : Ident.t ) ->
1255
+ Ext_list. rev_iter (fun (s , export ) ->
1265
1256
P. group f 0 @@ (fun _ ->
1266
1257
P. string f L. exports;
1267
1258
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 ) ;
1270
1261
P. string f L. eq;
1271
1262
P. space f;
1272
- ignore @@ ident cxt f id;
1263
+ P. string f export;
1273
1264
semi f;);
1274
1265
P. newline f;
1275
- )
1276
- idents
1266
+ ) reversed_list;
1267
+ outer_cxt
1277
1268
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 ) =
1282
1271
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
+
1284
1302
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
1286
1304
let () = P. force_newline f in
1287
- exports cxt f exp
1305
+ exports cxt f program.exports
1288
1306
1289
1307
1290
1308
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) =
1358
1376
let () =
1359
1377
P. string f " // Generated CODE, PLEASE EDIT WITH CARE" ;
1360
1378
P. newline f;
1361
- P. newline f ;
1362
1379
P. string f {| " use strict" ;| };
1380
+ P. newline f ;
1363
1381
in
1364
1382
(match Sys. getenv " OCAML_AMD_MODULE" with
1365
1383
| exception Not_found ->
1366
- node_program f program
1367
- | _ -> amd_program f program ) ;
1384
+ ignore ( node_program f program)
1385
+ | _ -> amd_program f program ) ;
1368
1386
P. string f (
1369
1387
match program.side_effect with
1370
1388
| None -> " /* No side effect */"
0 commit comments