Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embed the Wasm code inside the JavaScript script #1758

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading