diff --git a/compiler/bin-wasm_of_ocaml/compile.ml b/compiler/bin-wasm_of_ocaml/compile.ml index 99577566..ee45cd0f 100644 --- a/compiler/bin-wasm_of_ocaml/compile.ml +++ b/compiler/bin-wasm_of_ocaml/compile.ml @@ -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 ] ()) diff --git a/compiler/lib-wasm/link.ml b/compiler/lib-wasm/link.ml index d043d083..eb3d0571 100644 --- a/compiler/lib-wasm/link.ml +++ b/compiler/lib-wasm/link.ml @@ -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)) @@ -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; diff --git a/compiler/lib-wasm/link.mli b/compiler/lib-wasm/link.mli index 9ad39a42..8278d5c2 100644 --- a/compiler/lib-wasm/link.mli +++ b/compiler/lib-wasm/link.mli @@ -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 diff --git a/runtime/wasm/runtime.js b/runtime/wasm/runtime.js index 03a88e62..3dcfe221 100644 --- a/runtime/wasm/runtime.js +++ b/runtime/wasm/runtime.js @@ -493,10 +493,13 @@ 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 = {}; @@ -504,9 +507,12 @@ 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,