Skip to content

Commit 1533680

Browse files
committed
Wasm_of_ocaml: support JavaScript strings in prelude code
1 parent 7535c06 commit 1533680

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,33 +162,33 @@ let generate_prelude ~out_file =
162162
in
163163
let context = Generate.start () in
164164
let debug = Parse_bytecode.Debug.create ~include_cmis:false false in
165-
let _ =
165+
let _, generated_js =
166166
Generate.f
167167
~context
168-
~unit_name:(Some "prelude")
168+
~unit_name:(Some "wasmoo_prelude")
169169
~live_vars:variable_uses
170170
~in_cps
171171
~deadcode_sentinal
172172
~debug
173173
program
174174
in
175175
Generate.output ch ~context;
176-
uinfo.provides
176+
uinfo.provides, generated_js
177177

178178
let build_prelude z =
179179
Fs.with_intermediate_file (Filename.temp_file "prelude" ".wasm")
180180
@@ fun prelude_file ->
181181
Fs.with_intermediate_file (Filename.temp_file "prelude_file" ".wasm")
182182
@@ fun tmp_prelude_file ->
183-
let predefined_exceptions = generate_prelude ~out_file:prelude_file in
183+
let info = generate_prelude ~out_file:prelude_file in
184184
Binaryen.optimize
185185
~profile:(Driver.profile 1)
186186
~input_file:prelude_file
187187
~output_file:tmp_prelude_file
188188
~opt_input_sourcemap:None
189189
~opt_output_sourcemap:None;
190190
Zip.add_file z ~name:"prelude.wasm" ~file:tmp_prelude_file;
191-
predefined_exceptions
191+
info
192192

193193
let build_js_runtime ~primitives ?runtime_arguments () =
194194
let always_required_js, primitives =
@@ -366,12 +366,18 @@ let run
366366
let z = Zip.open_out tmp_output_file in
367367
Zip.add_file z ~name:"runtime.wasm" ~file:tmp_wasm_file;
368368
Zip.add_entry z ~name:"runtime.js" ~contents:js_runtime;
369-
let predefined_exceptions = build_prelude z in
369+
let predefined_exceptions, (strings, fragments) = build_prelude z in
370370
Link.add_info
371371
z
372372
~predefined_exceptions
373373
~build_info:(Build_info.create `Runtime)
374-
~unit_data:[]
374+
~unit_data:
375+
[ { Link.unit_name = "wasmoo_prelude"
376+
; unit_info = Unit_info.empty
377+
; strings
378+
; fragments
379+
}
380+
]
375381
();
376382
Zip.close_out z)
377383
else

compiler/lib-wasm/link.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ let generate_start_function ~to_link ~out_file =
388388
Filename.gen_file out_file
389389
@@ fun ch ->
390390
let context = Generate.start () in
391-
Generate.add_init_function ~context ~to_link:("prelude" :: to_link);
391+
Generate.add_init_function ~context ~to_link:("wasmoo_prelude" :: to_link);
392392
Generate.wasm_output ch ~context;
393393
if times () then Format.eprintf " generate start: %a@." Timer.print t1
394394

@@ -669,11 +669,11 @@ let load_information files =
669669
match files with
670670
| [] -> assert false
671671
| runtime :: other_files ->
672-
let build_info, predefined_exceptions, _unit_data =
672+
let build_info, predefined_exceptions, unit_data =
673673
Zip.with_open_in runtime read_info
674674
in
675675
( predefined_exceptions
676-
, (runtime, (build_info, []))
676+
, (runtime, (build_info, unit_data))
677677
:: List.map other_files ~f:(fun file ->
678678
let build_info, _predefined_exceptions, unit_data =
679679
Zip.with_open_in file read_info
@@ -770,7 +770,8 @@ let link ~output_file ~linkall ~enable_source_maps ~files =
770770
|| cmo_file
771771
|| linkall
772772
|| unit_info.force_link
773-
|| not (StringSet.is_empty (StringSet.inter requires unit_info.provides))
773+
|| (not (StringSet.is_empty (StringSet.inter requires unit_info.provides)))
774+
|| String.equal unit_name "wasmoo_prelude"
774775
then
775776
( StringSet.diff
776777
(StringSet.union unit_info.requires requires)

0 commit comments

Comments
 (0)