Skip to content

Commit d69df7a

Browse files
committed
WIP
1 parent 889e292 commit d69df7a

File tree

4 files changed

+47
-26
lines changed

4 files changed

+47
-26
lines changed

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ let run
479479
(Link.build_runtime_arguments
480480
~missing_primitives
481481
~wasm_dir:dir
482-
~link_spec:[ wasm_name, None ]
482+
~link_spec:[ (*`Name wasm_name*) `File tmp_wasm_file', None ]
483483
~separate_compilation:false
484484
~generated_js:[ None, generated_js ]
485485
())

compiler/lib-wasm/link.ml

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -540,23 +540,32 @@ let build_runtime_arguments
540540
, EArr
541541
(List.map
542542
~f:(fun (m, deps) ->
543-
Javascript.Element
544-
(EArr
545-
[ Element (EStr (Utf8_string.of_string_exn m))
546-
; Element
547-
(match deps with
548-
| None ->
549-
ENum (Javascript.Num.of_targetint (Targetint.of_int_exn 0))
550-
| Some l ->
551-
EArr
552-
(List.map
553-
~f:(fun i ->
554-
Javascript.Element
555-
(ENum
556-
(Javascript.Num.of_targetint
557-
(Targetint.of_int_exn i))))
558-
l))
559-
]))
543+
let deps =
544+
Javascript.Element
545+
(match deps with
546+
| None -> ENum (Javascript.Num.of_targetint (Targetint.of_int_exn 0))
547+
| Some l ->
548+
EArr
549+
(List.map
550+
~f:(fun i ->
551+
Javascript.Element
552+
(ENum
553+
(Javascript.Num.of_targetint (Targetint.of_int_exn i))))
554+
l))
555+
in
556+
match m with
557+
| `Name m ->
558+
Javascript.Element
559+
(EArr [ Element (EStr (Utf8_string.of_string_exn m)); deps ])
560+
| `File f ->
561+
let contents =
562+
f
563+
|> Fs.read_file
564+
|> Base64.encode_string
565+
|> Utf8_string.of_string_exn
566+
in
567+
Javascript.Element
568+
(EArr [ ElementHole; deps; Javascript.Element (EStr contents) ]))
560569
link_spec) )
561570
; "generated", generated_js
562571
; "src", EStr (Utf8_string.of_string_exn (Filename.basename wasm_dir))
@@ -814,7 +823,10 @@ let link ~output_file ~linkall ~enable_source_maps ~files =
814823
( interfaces
815824
, dir
816825
, let to_link = compute_dependencies ~files_to_link ~files in
817-
List.combine module_names (None :: None :: to_link) @ [ start_module, None ] )
826+
List.combine
827+
(List.map ~f:(fun nm -> `Name nm) module_names)
828+
(None :: None :: to_link)
829+
@ [ `Name start_module, None ] )
818830
in
819831
let missing_primitives = compute_missing_primitives interfaces in
820832
if times () then Format.eprintf " copy wasm files: %a@." Timer.print t;

compiler/lib-wasm/link.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ val add_info :
4545
-> unit
4646

4747
val build_runtime_arguments :
48-
link_spec:(string * int list option) list
48+
link_spec:([ `Name of string | `File of string ] * int list option) list
4949
-> separate_compilation:bool
5050
-> missing_primitives:string list
5151
-> wasm_dir:string

runtime/wasm/runtime.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -493,20 +493,29 @@
493493
return fetch(url);
494494
}
495495
const loadCode = isNode ? loadRelative : fetchRelative;
496-
async function instantiateModule(code) {
497-
return isNode
498-
? WebAssembly.instantiate(await code, imports, options)
499-
: WebAssembly.instantiateStreaming(code, imports, options);
496+
async function decodeCode(code) {
497+
const dataUri = "data:application/octet-stream;base64," + code;
498+
const response = await fetch(dataUri);
499+
const buffer = await response.arrayBuffer();
500+
return new Uint8Array(buffer);
501+
}
502+
async function instantiateModule(code, stream) {
503+
return stream
504+
? WebAssembly.instantiateStreaming(code, imports, options)
505+
: WebAssembly.instantiate(await code, imports, options);
500506
}
501507
async function instantiateFromDir() {
502508
imports.OCaml = {};
503509
const deps = [];
504510
async function loadModule(module, isRuntime) {
505511
const sync = module[1].constructor !== Array;
506512
async function instantiate() {
507-
const code = loadCode(src + "/" + module[0] + ".wasm");
513+
const code = module[0]
514+
? loadCode(src + "/" + module[0] + ".wasm")
515+
: decodeCode(module[2]);
516+
const stream = !isNode && module[0];
508517
await Promise.all(sync ? deps : module[1].map((i) => deps[i]));
509-
const wasmModule = await instantiateModule(code);
518+
const wasmModule = await instantiateModule(code, stream);
510519
Object.assign(
511520
isRuntime ? imports.env : imports.OCaml,
512521
wasmModule.instance.exports,

0 commit comments

Comments
 (0)