Skip to content

Commit 6ac5d58

Browse files
committed
in memory loading cmi/j for browser -- much easier playground
1 parent 30af6f4 commit 6ac5d58

25 files changed

+369220
-88198
lines changed

jscomp/core/bs_cmi_load.ml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
3+
(* TODO: provide native load*)
4+
let browse_load ~unit_name : Env.Persistent_signature.t option=
5+
match Ext_string_array.find_sorted Builtin_cmi_datasets.module_sets unit_name with
6+
| Some index ->
7+
(* Format.fprintf Format.err_formatter "reading %s@." unit_name; *)
8+
Some {filename = Sys.executable_name ;
9+
cmi =
10+
Lazy.force Builtin_cmi_datasets.module_sets_cmi.(index)}
11+
| None -> assert false

jscomp/core/bs_conditional_initial.ml

+5-13
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,12 @@
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+
(* Clflags.keep_docs := false; *)
26+
(* default to false -check later*)
27+
(* Clflags.keep_locs := false; *)
2628
let setup_env () =
27-
#if 0 then
28-
let old_loader = !Env.Persistent_signature.load in
29-
Env.Persistent_signature.load := (fun ~unit_name ->
30-
match Ext_string_array.find_sorted Js_cmi_datasets.module_sets unit_name with
31-
| Some index ->
32-
Format.fprintf Format.err_formatter "reading %s@." unit_name;
33-
Some {filename = Sys.executable_name ; cmi =
34-
Lazy.force Js_cmi_datasets.module_sets_cmi.(index)}
35-
| None -> old_loader ~unit_name
36-
);
37-
Clflags.keep_locs := false;
38-
Clflags.keep_docs := false;
29+
#if BS_BROWSER then
30+
Env.Persistent_signature.load := Bs_cmi_load.browse_load;
3931
#end
4032
Warnings.parse_options false Bsc_warnings.defaults_w;
4133
Warnings.parse_options true Bsc_warnings.defaults_warn_error;

jscomp/core/builtin_cmi_datasets.ml

+369
Large diffs are not rendered by default.

jscomp/core/builtin_cmj_datasets.ml

+375
Large diffs are not rendered by default.

jscomp/core/js_cmj_datasets.ml

-185
This file was deleted.

jscomp/core/js_cmj_datasets.mli

-1
This file was deleted.

jscomp/core/js_cmj_format.ml

+7-1
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,10 @@ let pp_cmj
243243
k i
244244
(Format.asprintf "%a" Lam_arity.print arity ))
245245
xs
246-
)
246+
)
247+
248+
type path = string
249+
type cmj_load_info = {
250+
cmj_table : t ;
251+
cmj_path : path ;
252+
}

jscomp/core/js_cmj_format.mli

+8-1
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,11 @@ val from_string : string -> t
108108
val to_file :
109109
string -> check_exists:bool -> t -> unit
110110

111-
val pp_cmj: t -> unit
111+
val pp_cmj: t -> unit
112+
113+
114+
type path = string
115+
type cmj_load_info = {
116+
cmj_table : t ;
117+
cmj_path : path ;
118+
}

jscomp/core/js_cmj_load.ml

+8-20
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,20 @@
2828
*)
2929

3030

31-
type path = string
32-
type cmj_load_info = {
33-
cmj_table : Js_cmj_format.t ;
34-
cmj_path : path ;
35-
}
3631

37-
#if BS_BROWSER then
38-
let find_cmj_exn file : cmj_load_info =
32+
33+
#if BS_BROWSER then
34+
let find_cmj_exn file : Js_cmj_format.cmj_load_info =
3935
let target = Ext_string.uncapitalize_ascii (Filename.basename file) in
40-
match Map_string.find_exn !Js_cmj_datasets.data_sets target with
41-
| v
36+
match Builtin_cmj_datasets.query_by_name target with
37+
| Some v
4238
->
43-
begin match Lazy.force v with
44-
| exception _
45-
->
46-
Ext_log.warn __LOC__
47-
"@[%s corrupted in database, when looking %s while compiling %s please update @]" file target !Location.input_name ;
48-
Bs_exception.error (Cmj_not_found file)
49-
| v -> {cmj_path = "BROWSER"; cmj_table = v}
50-
(* see {!Js_packages_info.string_of_module_id} *)
51-
end
52-
| exception Not_found
39+
{cmj_path = "BROWSER"; cmj_table = v}
40+
| None
5341
->
5442
Bs_exception.error (Cmj_not_found file)
5543
#else
56-
let find_cmj_exn file : cmj_load_info =
44+
let find_cmj_exn file : Js_cmj_format.cmj_load_info =
5745
match Config_util.find_opt file with
5846
| Some f
5947
->

jscomp/core/js_cmj_load.mli

+2-6
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,9 @@
2727
it depends on {!Js_cmj_datasets}, for non-browser environment, it fails
2828
*)
2929

30-
type path = string
31-
type cmj_load_info = {
32-
cmj_table : Js_cmj_format.t ;
33-
cmj_path : path ;
34-
}
30+
3531

3632
(** return path and meta data *)
3733
val find_cmj_exn :
3834
string ->
39-
cmj_load_info
35+
Js_cmj_format.cmj_load_info

jscomp/core/lam_compile_env.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module S = Js_stmt_make
3434
*)
3535

3636
type env_value =
37-
| Ml of Js_cmj_load.cmj_load_info
37+
| Ml of Js_cmj_format.cmj_load_info
3838
(* | Runtime of Js_cmj_load.cmj_load_info *)
3939
(**
4040
[Runtime (pure, path, cmj_format)]
@@ -166,7 +166,7 @@ let get_package_path_from_cmj
166166
| Runtime
167167
| External _ -> assert false
168168
| Ml ->
169-
let ({Js_cmj_load.cmj_table} as cmj_load_info) =
169+
let ({Js_cmj_format.cmj_table} as cmj_load_info) =
170170
Js_cmj_load.find_cmj_exn (Lam_module_ident.name id ^ Literals.suffix_cmj) in
171171
id +> Ml cmj_load_info;
172172
(cmj_load_info.cmj_path,

jscomp/core/ocaml_options.ml

+5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ let mk_keep_docs f =
6464
let mk_keep_locs f =
6565
"-keep-locs", Arg.Unit f, " Keep locations in .cmi files"
6666
;;
67+
let mk_no_keep_locs f =
68+
"-no-keep-locs", Arg.Unit f, " Do not keep locations in .cmi files"
69+
;;
6770

6871
let mk_labels f =
6972
"-labels", Arg.Unit f, " Use commuting label mode"
@@ -290,6 +293,7 @@ let ocaml_options =
290293
let _intf_suffix s = Config.interface_suffix := s in
291294
let _keep_docs = set Clflags.keep_docs in
292295
let _keep_locs = set Clflags.keep_locs in
296+
let _no_keep_locs = unset Clflags.keep_locs in
293297
let _labels = unset Clflags.classic in
294298
let _no_alias_deps = set Clflags.transparent_modules in
295299
let _no_app_funct = unset Clflags.applicative_functors in
@@ -339,6 +343,7 @@ let ocaml_options =
339343
mk_intf_suffix _intf_suffix;
340344
mk_keep_docs _keep_docs;
341345
mk_keep_locs _keep_locs;
346+
mk_no_keep_locs _no_keep_locs;
342347
mk_labels _labels;
343348
mk_no_alias_deps _no_alias_deps;
344349
mk_no_app_funct _no_app_funct;

jscomp/main/cmij_main.ml

+48-52
Original file line numberDiff line numberDiff line change
@@ -38,74 +38,70 @@ let get_files ext dir =
3838
Array.sort (fun (x : string) y -> Pervasives.compare x y ) arr;
3939
Array.to_list arr
4040

41+
(** the cache should be readable and also update *)
42+
4143
let from_cmj (files : string list) (output_file : string) =
42-
let raw_to_str f str =
43-
Ext_pp.string f "\"" ;
44-
Ext_pp.string f (String.escaped str);
45-
Ext_pp.string f "\""
46-
in
44+
let files = List.sort (fun filea fileb ->
45+
Ext_string_array.cmp (Filename.basename filea) (Filename.basename fileb)) files in
46+
let keys = Ext_list.map files (fun x -> "\"" ^Filename.basename x ^ "\"") in
4747
let v = open_out_bin output_file in
48-
Ext_pervasives.finally v ~clean:close_out (fun v ->
49-
let f = Ext_pp.from_channel v in
50-
let aux file =
51-
let str = Ext_io.load_file file in
52-
begin
53-
#if 0 then
54-
prerr_endline (* can not embed corrupted data *)
55-
(Printf.sprintf "Begin Verifying %s" file);
56-
let _ = Js_cmj_format.from_string str in
57-
prerr_endline "End";
58-
#end
59-
Ext_pp.paren_group f 1 (fun _ ->
60-
raw_to_str f (Filename.basename file) ;
61-
Ext_pp.string f ",";
62-
Ext_pp.string f "lazy";
63-
Ext_pp.space f ;
64-
Ext_pp.paren_group f 1 (fun _ ->
65-
Ext_pp.string f "Js_cmj_format.from_string " ;
66-
raw_to_str f str));
67-
Ext_pp.string f ";";
68-
Ext_pp.newline f
69-
end
70-
in
48+
Ext_pervasives.finally v ~clean:close_out (fun f ->
49+
output_string f
50+
(Printf.sprintf {|let module_sets = [|
51+
%s
52+
|]|}
53+
(String.concat ";\n" keys)
54+
) ;
55+
output_string f "\n";
56+
output_string f
57+
(Printf.sprintf {|let module_sets_cmj : Js_cmj_format.t Lazy.t array = [|
58+
%s
59+
|]
60+
|} (String.concat ";\n" (Ext_list.map files (fun file ->
61+
Printf.sprintf "lazy (Js_cmj_format.from_string %S )"
62+
(Ext_io.load_file file)))));
63+
output_string f "\n";
64+
output_string f {|
65+
let query_by_name s =
66+
match Ext_string_array.find_sorted
67+
module_sets s with
68+
| None -> None
69+
| Some i ->
70+
Some (Lazy.force module_sets_cmj.(i))
71+
|}
72+
73+
)
7174

72-
Ext_pp.newline f ;
73-
Ext_pp.string f "let data_sets = let map = Map_string.of_list " ;
74-
Ext_pp.bracket_vgroup f 1 (fun _ -> List.iter aux files);
75-
Ext_pp.string f " in ref map")
7675

7776

78-
(** the cache should be readable and also update *)
79-
let _raw_to_str f str =
80-
Ext_pp.string f "\"" ;
81-
Ext_pp.string f (String.escaped str);
82-
Ext_pp.string f "\""
8377

8478
let from_cmi (files : string list) (output_file : string) =
8579
let files = List.sort (fun filea fileb ->
86-
Ext_string_array.cmp (Ext_filename.module_name filea) (Ext_filename.module_name fileb)) files in
80+
Ext_string_array.cmp (Ext_filename.module_name filea) (Ext_filename.module_name fileb)) files in
81+
let keys = Ext_list.map files (fun x -> "\"" ^ Ext_filename.module_name x ^ "\"") in
8782
let v = open_out_bin output_file in
8883
Ext_pervasives.finally v ~clean:close_out (fun f ->
8984
output_string f
9085
(Printf.sprintf {|let module_sets = [|
9186
%s
9287
|]|}
93-
(String.concat ";\n" (Ext_list.map files (fun x -> "\"" ^ Ext_filename.module_name x ^ "\"")))
94-
) ;
88+
(String.concat ";\n" keys)
89+
) ;
9590
output_string f "\n";
9691
output_string f
97-
(Printf.sprintf {|let module_sets_cmi : Cmi_format.cmi_infos Lazy.t array = [|
92+
(Printf.sprintf {|let module_sets_cmi : Cmi_format.cmi_infos Lazy.t array = [|
9893
%s
9994
|]
10095
|} (String.concat ";\n" (Ext_list.map files (fun file ->
101-
Printf.sprintf "lazy (Marshal.from_string %S 0)"
102-
(let content = (Cmi_format.read_cmi file) in
103-
Marshal.to_string content []
104-
(* let header_len = (String.length Config.cmi_magic_number) in
105-
String.sub content header_len (String.length content - header_len) *)
106-
)))
107-
)
108-
))
96+
Printf.sprintf "lazy (Marshal.from_string %S 0)"
97+
(let content = (Cmi_format.read_cmi file) in
98+
Marshal.to_string content []
99+
(* let header_len = (String.length Config.cmi_magic_number) in
100+
String.sub content header_len (String.length content - header_len) *)
101+
)))
102+
)
103+
)
104+
)
109105
;;
110106

111107
let stdlib = "stdlib-406"
@@ -114,9 +110,9 @@ let () =
114110
from_cmj ( Ext_list.append (get_files Literals.suffix_cmj stdlib)
115111
(Ext_list.append (get_files Literals.suffix_cmj "runtime")
116112
(get_files Literals.suffix_cmj "others")))
117-
(Filename.concat "core" "js_cmj_datasets.ml")
118-
(* from_cmi ( Ext_list.append (get_files Literals.suffix_cmi stdlib)
113+
(Filename.concat "core" "builtin_cmj_datasets.ml");
114+
from_cmi ( Ext_list.append (get_files Literals.suffix_cmi stdlib)
119115
(Ext_list.append (get_files Literals.suffix_cmi "runtime")
120116
(get_files Literals.suffix_cmi "others")))
121-
(Filename.concat "core" "js_cmi_datasets.ml") *)
117+
(Filename.concat "core" "builtin_cmi_datasets.ml")
122118

jscomp/main/jsoo_main.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ let shake_compile impl ~use_super_errors ?react_ppx_version =
113113

114114

115115

116-
let load_module cmi_path cmi_content cmj_name cmj_content =
116+
(* let load_module cmi_path cmi_content cmj_name cmj_content =
117117
Js.create_file cmi_path cmi_content;
118118
Js_cmj_datasets.data_sets :=
119119
Map_string.add !Js_cmj_datasets.data_sets
120120
cmj_name (lazy (Js_cmj_format.from_string cmj_content))
121-
121+
*)
122122

123123

124124
let export (field : string) v =
@@ -167,15 +167,15 @@ let make_compiler name impl =
167167
Js.wrap_meth_callback
168168
(fun _ code -> (shake_compile impl ~use_super_errors:true (Js.to_string code)));
169169
"version", Js.Unsafe.inject (Js.string (Bs_version.version));
170-
"load_module",
170+
(* "load_module",
171171
inject @@
172172
Js.wrap_meth_callback
173173
(fun _ cmi_path cmi_content cmj_name cmj_content ->
174174
let cmj_bytestring = Js.to_bytestring cmj_content in
175175
(* HACK: force string tag to ASCII (9) to avoid
176176
* UTF-8 encoding *)
177177
Js.Unsafe.set cmj_bytestring "t" 9;
178-
load_module cmi_path cmi_content (Js.to_string cmj_name) cmj_bytestring);
178+
load_module cmi_path cmi_content (Js.to_string cmj_name) cmj_bytestring); *)
179179
|]))
180180
let () = make_compiler "ocaml" Parse.implementation
181181

jscomp/others/release.ninja

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
bsc = ../lib/bsc.exe
3-
bsc_flags = -absname -no-alias-deps -bs-no-version-header -bs-diagnose -bs-no-check-div-by-zero -bs-cross-module-opt -bs-package-name bs-platform -bs-package-output commonjs:lib/js -bs-package-output es6:lib/es6 -nostdlib -nopervasives -unsafe -w +50 -warn-error A -bin-annot -bs-noassertfalse -open Bs_stdlib_mini -I ./runtime
3+
bsc_flags = -no-keep-locs -absname -no-alias-deps -bs-no-version-header -bs-diagnose -bs-no-check-div-by-zero -nostdlib -bs-cross-module-opt -bs-package-name bs-platform -bs-package-output commonjs:lib/js -bs-package-output es6:lib/es6 -nopervasives -unsafe -w +50 -warn-error A -bs-noassertfalse -open Bs_stdlib_mini -I ./runtime
44

55
rule cc
66
command = $bsc -bs-cmi -bs-cmj $bsc_flags -I others $in

jscomp/runtime/release.ninja

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
bsc = ../lib/bsc.exe
3-
bsc_no_open_flags = -absname -no-alias-deps -bs-no-version-header -bs-diagnose -bs-no-check-div-by-zero -bs-cross-module-opt -bs-package-name bs-platform -bs-package-output commonjs:lib/js -bs-package-output es6:lib/es6 -nostdlib -nopervasives -unsafe -w +50 -warn-error A -bin-annot
3+
bsc_no_open_flags = -no-keep-locs -absname -no-alias-deps -bs-no-version-header -bs-diagnose -bs-no-check-div-by-zero -nostdlib -bs-cross-module-opt -bs-package-name bs-platform -bs-package-output commonjs:lib/js -bs-package-output es6:lib/es6 -nopervasives -unsafe -w +50 -warn-error A
44
bsc_flags = $bsc_no_open_flags -open Bs_stdlib_mini
55

66
rule cc

jscomp/snapshot.ninja

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ build $SNAP/unstable/bspack.ml: bspack | ./bin/bspack.exe
6161
main = Bspack_main
6262

6363
build $SNAP/unstable/js_compiler.ml: bspack | ./bin/bspack.exe
64-
flags = -D BS_BROWSER=true -D BS_RELEASE_BUILD=true -bs-MD -module-alias Config=Config_whole_compiler -bs-exclude-I config -I $OCAML_SRC_UTILS -I $OCAML_SRC_PARSING -I $OCAML_SRC_TYPING -I $OCAML_SRC_BYTECOMP -I $OCAML_SRC_DRIVER -I stubs -I ext -I syntax -I depends -I common -I core -I super_errors -I bsb -I outcome_printer -I main
64+
flags = -D BS_BROWSER=true -D BS_RELEASE_BUILD=true -bs-MD -module-alias Config=Config_whole_compiler -bs-exclude-I config -I $OCAML_SRC_UTILS -I $OCAML_SRC_PARSING -I $OCAML_SRC_TYPING -I $OCAML_SRC_BYTECOMP -I $OCAML_SRC_DRIVER -I stubs -I ext -I syntax -I depends -I common -I core -I super_errors -I bsb -I outcome_printer -I js_parser -I main
6565
main = Jsoo_main
6666

6767
build $SNAP/unstable/js_refmt_compiler.ml: bspack | ./bin/bspack.exe

jscomp/stdlib-406/release.ninja

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
bsc = ../lib/bsc.exe
3-
bsc_flags = -absname -no-alias-deps -bs-no-version-header -bs-diagnose -bs-no-check-div-by-zero -bs-cross-module-opt -bs-package-name bs-platform -bs-package-output commonjs:lib/js -bs-package-output es6:lib/es6 -nostdlib -w -9-3-106 -warn-error A -I runtime -I others
3+
bsc_flags = -no-keep-locs -absname -no-alias-deps -bs-no-version-header -bs-diagnose -bs-no-check-div-by-zero -nostdlib -bs-cross-module-opt -bs-package-name bs-platform -bs-package-output commonjs:lib/js -bs-package-output es6:lib/es6 -w -9-3-106 -warn-error A -I runtime -I others
44

55
rule cc
66
command = $bsc -bs-cmi -bs-cmj $bsc_flags -I stdlib-406 $in

lib/4.06.1/bsdep.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -28211,7 +28211,9 @@ end = struct
2821128211
* along with this program; if not, write to the Free Software
2821228212
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2821328213

28214-
28214+
(* Clflags.keep_docs := false; *)
28215+
(* default to false -check later*)
28216+
(* Clflags.keep_locs := false; *)
2821528217
let setup_env () =
2821628218

2821728219
Warnings.parse_options false Bsc_warnings.defaults_w;

0 commit comments

Comments
 (0)