Skip to content

Commit b108b85

Browse files
committed
Merge pull request #80 from bloomberg/sort_modules_deps
delay the external module dependency check, so we can decide whehter …
2 parents 2b73512 + 63d67f5 commit b108b85

File tree

10 files changed

+82
-80
lines changed

10 files changed

+82
-80
lines changed

jscomp/j.ml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,15 @@ and block = statement list
341341

342342
and program = {
343343
name : string;
344-
modules : required_modules ;
344+
345345
block : block ;
346346
exports : exports ;
347347
export_set : Ident_set.t ;
348-
side_effect : string option (* None: no, Some reason *)
348+
349349
}
350+
and deps_program =
351+
{
352+
program : program ;
353+
modules : required_modules ;
354+
side_effect : string option (* None: no, Some reason *)
355+
}

jscomp/js_dump.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,7 @@ let exports cxt f (idents : Ident.t list) =
13001300
outer_cxt
13011301

13021302

1303-
let node_program f ( program : J.program) =
1303+
let node_program f ( {program ; modules ; } : J.deps_program) =
13041304
let cxt = Ext_pp_scope.empty in
13051305
(* Node style *)
13061306
let requires cxt f (modules : (Ident.t * string) list ) =
@@ -1330,7 +1330,7 @@ let node_program f ( program : J.program) =
13301330
outer_cxt
13311331
in
13321332

1333-
let cxt = requires cxt f program.modules in
1333+
let cxt = requires cxt f modules in
13341334

13351335
let () = P.force_newline f in
13361336
let cxt = statement_list true cxt f program.block in
@@ -1339,7 +1339,7 @@ let node_program f ( program : J.program) =
13391339

13401340

13411341
let amd_program f
1342-
({modules; block = b ; exports = exp ; side_effect } as program : J.program)
1342+
( {program ; modules ; _} : J.deps_program)
13431343
=
13441344
P.newline f ;
13451345
let cxt = Ext_pp_scope.empty in
@@ -1371,15 +1371,15 @@ let amd_program f
13711371
P.brace_vgroup f 1 @@ (fun _ ->
13721372
let () = P.string f L.strict_directive in
13731373
let () = P.newline f in
1374-
let cxt = statement_list true cxt f b in
1374+
let cxt = statement_list true cxt f program.block in
13751375
(* FIXME AMD : use {[ function xx ]} or {[ var x = function ..]} *)
13761376
P.newline f;
13771377
P.force_newline f;
13781378
ignore (exports cxt f program.exports));
13791379
P.string f ")";
13801380
;;
13811381

1382-
let pp_program (program : J.program) (f : Ext_pp.t) =
1382+
let pp_program ( program : J.deps_program) (f : Ext_pp.t) =
13831383
begin
13841384
P.string f "// Generated CODE, PLEASE EDIT WITH CARE";
13851385
P.newline f;
@@ -1404,6 +1404,6 @@ let pp_program (program : J.program) (f : Ext_pp.t) =
14041404
P.flush f ()
14051405
end
14061406
let dump_program
1407-
(program : J.program)
1407+
(program : J.deps_program)
14081408
(oc : out_channel) =
14091409
pp_program program (P.from_channel oc)

jscomp/js_dump.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@
2525
(** Print JS IR to vanilla Javascript code *)
2626

2727

28-
val pp_program : J.program -> Ext_pp.t -> unit
28+
val pp_program : J.deps_program -> Ext_pp.t -> unit
2929

30-
val dump_program : J.program -> out_channel -> unit
30+
val dump_program : J.deps_program -> out_channel -> unit

jscomp/js_fold.ml

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -301,21 +301,11 @@ class virtual fold =
301301
let o = o#property_name _x in let o = o#expression _x_i1 in o)
302302
method property : property -> 'self_type = o#unknown
303303
method program : program -> 'self_type =
304-
fun
305-
{
306-
name = _x;
307-
modules = _x_i1;
308-
block = _x_i2;
309-
exports = _x_i3;
310-
export_set = _x_i4;
311-
side_effect = _x_i5
312-
} ->
304+
fun { name = _x; block = _x_i1; exports = _x_i2; export_set = _x_i3 }
305+
->
313306
let o = o#string _x in
314-
let o = o#required_modules _x_i1 in
315-
let o = o#block _x_i2 in
316-
let o = o#exports _x_i3 in
317-
let o = o#unknown _x_i4 in
318-
let o = o#option (fun o -> o#string) _x_i5 in o
307+
let o = o#block _x_i1 in
308+
let o = o#exports _x_i2 in let o = o#unknown _x_i3 in o
319309
method number : number -> 'self_type = o#unknown
320310
method mutable_flag : mutable_flag -> 'self_type = o#unknown
321311
method label : label -> 'self_type = o#string
@@ -397,6 +387,11 @@ class virtual fold =
397387
let o = o#option (fun o -> o#string) _x_i1 in o
398388
method exports : exports -> 'self_type = o#unknown
399389
method exception_ident : exception_ident -> 'self_type = o#ident
390+
method deps_program : deps_program -> 'self_type =
391+
fun { program = _x; modules = _x_i1; side_effect = _x_i2 } ->
392+
let o = o#program _x in
393+
let o = o#required_modules _x_i1 in
394+
let o = o#option (fun o -> o#string) _x_i2 in o
400395
method case_clause :
401396
(* since in ocaml, it's expression oriented langauge, [return] in
402397
general has no jumps, it only happens when we do

jscomp/js_map.ml

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -322,30 +322,13 @@ class virtual map =
322322
let _x_i1 = o#expression _x_i1 in (_x, _x_i1))
323323
method property : property -> property = o#unknown
324324
method program : program -> program =
325-
fun
326-
{
327-
name = _x;
328-
modules = _x_i1;
329-
block = _x_i2;
330-
exports = _x_i3;
331-
export_set = _x_i4;
332-
side_effect = _x_i5
333-
} ->
325+
fun { name = _x; block = _x_i1; exports = _x_i2; export_set = _x_i3 }
326+
->
334327
let _x = o#string _x in
335-
let _x_i1 = o#required_modules _x_i1 in
336-
let _x_i2 = o#block _x_i2 in
337-
let _x_i3 = o#exports _x_i3 in
338-
let _x_i4 = o#unknown _x_i4 in
339-
let _x_i5 = o#option (fun o -> o#string) _x_i5
340-
in
341-
{
342-
name = _x;
343-
modules = _x_i1;
344-
block = _x_i2;
345-
exports = _x_i3;
346-
export_set = _x_i4;
347-
side_effect = _x_i5;
348-
}
328+
let _x_i1 = o#block _x_i1 in
329+
let _x_i2 = o#exports _x_i2 in
330+
let _x_i3 = o#unknown _x_i3
331+
in { name = _x; block = _x_i1; exports = _x_i2; export_set = _x_i3; }
349332
method number : number -> number = o#unknown
350333
method mutable_flag : mutable_flag -> mutable_flag = o#unknown
351334
method label : label -> label = o#string
@@ -444,6 +427,12 @@ class virtual map =
444427
in { expression_desc = _x; comment = _x_i1; }
445428
method exports : exports -> exports = o#unknown
446429
method exception_ident : exception_ident -> exception_ident = o#ident
430+
method deps_program : deps_program -> deps_program =
431+
fun { program = _x; modules = _x_i1; side_effect = _x_i2 } ->
432+
let _x = o#program _x in
433+
let _x_i1 = o#required_modules _x_i1 in
434+
let _x_i2 = o#option (fun o -> o#string) _x_i2
435+
in { program = _x; modules = _x_i1; side_effect = _x_i2; }
447436
method case_clause :
448437
(* since in ocaml, it's expression oriented langauge, [return] in
449438
general has no jumps, it only happens when we do

jscomp/js_program_loader.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,17 @@ let string_of_module_id (x : module_id) : string =
9595
FIXME: the module order matters?
9696
*)
9797

98-
let make_program name side_effect export_idents external_module_ids block : J.program =
99-
let modules =
100-
List.map (fun id -> Lam_module_ident.id id, string_of_module_id id )
101-
external_module_ids in
98+
let make_program name export_idents block : J.program =
10299

103100
{
104101
name;
105-
modules;
102+
106103
exports = export_idents ;
107104
export_set = Ident_set.of_list export_idents;
108105
block = block;
109-
side_effect ;
106+
110107
}
111-
108+
let decorate_deps modules side_effect program : J.deps_program =
109+
110+
{ program ; modules ; side_effect }
111+

jscomp/js_program_loader.mli

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,11 @@
3030

3131
val make_program :
3232
string ->
33-
string option ->
34-
Ident.t list -> Lam_module_ident.t list -> J.block -> J.program
33+
Ident.t list -> J.block -> J.program
34+
35+
val decorate_deps :
36+
J.required_modules ->
37+
string option ->
38+
J.program -> J.deps_program
39+
40+
val string_of_module_id : Lam_module_ident.t -> string

jscomp/lam_compile_group.ml

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ let compile_group ({filename = file_name; env;} as meta : Lam_stats.meta)
150150

151151
(** Actually simplify_lets is kind of global optimization since it requires you to know whether
152152
it's used or not
153+
[non_export] is only used in playground
153154
*)
154-
let compile ~filename non_export env _sigs lam : J.program =
155-
155+
let compile ~filename non_export env _sigs lam =
156156
let export_idents =
157157
if non_export then
158158
[]
@@ -305,35 +305,41 @@ let compile ~filename non_export env _sigs lam : J.program =
305305
|> Js_output.concat
306306
|> Js_output.to_block
307307
in
308-
let external_module_ids =
309-
Lam_compile_env.get_requried_modules
310-
meta.env
311-
meta.required_modules
312-
(Js_fold_basic.calculate_hard_dependencies body)
313-
in
314-
(* Exporting ... *)
315-
let v =
316-
Lam_stats_util.export_to_cmj meta maybe_pure external_module_ids
317-
(if non_export then [] else lambda_exports)
318-
in
319-
(if not @@ Ext_string.is_empty filename then
320-
Js_cmj_format.to_file
321-
(Ext_filename.chop_extension ~loc:__LOC__ filename ^ ".cmj") v);
322-
let js =
323-
Js_program_loader.make_program filename v.pure meta.exports
324-
external_module_ids body
325-
in
326308
(* The file is not big at all compared with [cmo] *)
327309
(* Ext_marshal.to_file (Ext_filename.chop_extension filename ^ ".mj") js; *)
328-
310+
let js =
311+
Js_program_loader.make_program filename meta.exports
312+
body
313+
in
329314
js
330315
|> Js_pass_flatten.program
331316
|> Js_inline_and_eliminate.inline_and_shake
332317

333318
|> Js_pass_flatten_and_mark_dead.program
334319
|> (fun js -> ignore @@ Js_pass_scope.program js ; js )
335320
|> Js_shake.shake_program
336-
321+
|> ( fun (js: J.program) ->
322+
let external_module_ids =
323+
Lam_compile_env.get_requried_modules
324+
meta.env
325+
meta.required_modules
326+
(Js_fold_basic.calculate_hard_dependencies js.block)
327+
in
328+
let required_modules =
329+
List.map
330+
(fun id -> Lam_module_ident.id id, Js_program_loader.string_of_module_id id )
331+
external_module_ids in
332+
333+
(* Exporting ... *)
334+
let v =
335+
Lam_stats_util.export_to_cmj meta maybe_pure external_module_ids
336+
(if non_export then [] else lambda_exports)
337+
in
338+
(if not @@ Ext_string.is_empty filename then
339+
Js_cmj_format.to_file
340+
(Ext_filename.chop_extension ~loc:__LOC__ filename ^ ".cmj") v);
341+
Js_program_loader.decorate_deps required_modules v.pure js
342+
)
337343
| _ -> raise Not_a_module
338344
end
339345
| _ -> raise Not_a_module end

jscomp/lam_compile_group.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ val compile :
3434
Env.t ->
3535
Types.signature ->
3636
Lambda.lambda ->
37-
J.program
37+
J.deps_program
3838

3939
val lambda_as_module :
4040
Env.t ->

jscomp/test/string_runtime_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
var Caml_string = require("../runtime/caml_string");
55
var Mt = require("./mt");
66
var $$String = require("../stdlib/string");
7-
var Caml_string = require("../runtime/caml_string");
87
var List = require("../stdlib/list");
8+
var Caml_string = require("../runtime/caml_string");
99

1010
var suites_001 = [
1111
/* tuple */0,

0 commit comments

Comments
 (0)