Skip to content

Commit

Permalink
Embed the Wasm code inside the JavaScript script
Browse files Browse the repository at this point in the history
  • Loading branch information
vouillon committed Dec 5, 2024
1 parent 889e292 commit ce10931
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 26 deletions.
2 changes: 1 addition & 1 deletion compiler/bin-wasm_of_ocaml/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ let run
(Link.build_runtime_arguments
~missing_primitives
~wasm_dir:dir
~link_spec:[ wasm_name, None ]
~link_spec:[ (*`Name wasm_name*) `File tmp_wasm_file', None ]
~separate_compilation:false
~generated_js:[ None, generated_js ]
())
Expand Down
48 changes: 30 additions & 18 deletions compiler/lib-wasm/link.ml
Original file line number Diff line number Diff line change
Expand Up @@ -540,23 +540,32 @@ let build_runtime_arguments
, EArr
(List.map
~f:(fun (m, deps) ->
Javascript.Element
(EArr
[ Element (EStr (Utf8_string.of_string_exn m))
; Element
(match deps with
| None ->
ENum (Javascript.Num.of_targetint (Targetint.of_int_exn 0))
| Some l ->
EArr
(List.map
~f:(fun i ->
Javascript.Element
(ENum
(Javascript.Num.of_targetint
(Targetint.of_int_exn i))))
l))
]))
let deps =
Javascript.Element
(match deps with
| None -> ENum (Javascript.Num.of_targetint (Targetint.of_int_exn 0))
| Some l ->
EArr
(List.map
~f:(fun i ->
Javascript.Element
(ENum
(Javascript.Num.of_targetint (Targetint.of_int_exn i))))
l))
in
match m with
| `Name m ->
Javascript.Element
(EArr [ Element (EStr (Utf8_string.of_string_exn m)); deps ])
| `File f ->
let contents =
f
|> Fs.read_file
|> Base64.encode_string
|> Utf8_string.of_string_exn
in
Javascript.Element
(EArr [ ElementHole; deps; Javascript.Element (EStr contents) ]))
link_spec) )
; "generated", generated_js
; "src", EStr (Utf8_string.of_string_exn (Filename.basename wasm_dir))
Expand Down Expand Up @@ -814,7 +823,10 @@ let link ~output_file ~linkall ~enable_source_maps ~files =
( interfaces
, dir
, let to_link = compute_dependencies ~files_to_link ~files in
List.combine module_names (None :: None :: to_link) @ [ start_module, None ] )
List.combine
(List.map ~f:(fun nm -> `Name nm) module_names)
(None :: None :: to_link)
@ [ `Name start_module, None ] )
in
let missing_primitives = compute_missing_primitives interfaces in
if times () then Format.eprintf " copy wasm files: %a@." Timer.print t;
Expand Down
2 changes: 1 addition & 1 deletion compiler/lib-wasm/link.mli
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ val add_info :
-> unit

val build_runtime_arguments :
link_spec:(string * int list option) list
link_spec:([ `Name of string | `File of string ] * int list option) list
-> separate_compilation:bool
-> missing_primitives:string list
-> wasm_dir:string
Expand Down
18 changes: 12 additions & 6 deletions runtime/wasm/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,20 +493,26 @@
return fetch(url);
}
const loadCode = isNode ? loadRelative : fetchRelative;
async function instantiateModule(code) {
return isNode
? WebAssembly.instantiate(await code, imports, options)
: WebAssembly.instantiateStreaming(code, imports, options);
async function decodeCode(code) {
return fetch("data:application/wasm;base64," + code);
}
async function instantiateModule(code, stream) {
return stream
? WebAssembly.instantiateStreaming(code, imports, options)
: WebAssembly.instantiate(await code, imports, options);
}
async function instantiateFromDir() {
imports.OCaml = {};
const deps = [];
async function loadModule(module, isRuntime) {
const sync = module[1].constructor !== Array;
async function instantiate() {
const code = loadCode(src + "/" + module[0] + ".wasm");
const code = module[0]
? loadCode(src + "/" + module[0] + ".wasm")
: decodeCode(module[2]);
const stream = !(isNode && module[0]);
await Promise.all(sync ? deps : module[1].map((i) => deps[i]));
const wasmModule = await instantiateModule(code);
const wasmModule = await instantiateModule(code, stream);
Object.assign(
isRuntime ? imports.env : imports.OCaml,
wasmModule.instance.exports,
Expand Down

0 comments on commit ce10931

Please sign in to comment.