Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 3 additions & 6 deletions interpreter/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,9 @@ _build/$(LIB).cmxa: $(FILES) $(LIB).mllib _tags Makefile
# Building JavaScript library

.PHONY: $(JSLIB)
$(JSLIB): $(UNOPT)
mkdir -p _build/jslib/src
cp meta/jslib/* _build/jslib
cp $(DIRS:%=_build/%/*.ml*) meta/jslib/*.ml _build/jslib/src
rm _build/jslib/src/*.ml[^i]
(cd _build/jslib; ./build.sh ../../$@)
$(JSLIB):
dune build wast.bc.js
cp ../_build/default/interpreter/wast.bc.js ./wast.js
Comment thread
takikawa marked this conversation as resolved.
Outdated


# Building Windows build file
Expand Down
12 changes: 11 additions & 1 deletion interpreter/dune
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
; We also need to exclude the 'wasm' module as it overlaps with the library
; name.
; 'smallint' is a separate test module.
(modules :standard \ main wasm smallint))
(modules :standard \ main wasm smallint wast))

(executable
(name main)
Expand All @@ -23,6 +23,16 @@
(flags
(-open Wasm)))

(executable
(name wast)
(modules wast)
(libraries wasm js_of_ocaml)
(flags
(-open Wasm))
(optional)
(modes js)
(preprocess (pps js_of_ocaml-ppx)))

(subdir
text
(rule
Expand Down
25 changes: 25 additions & 0 deletions interpreter/meta/jslib/wast.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
open Js_of_ocaml
Comment thread
takikawa marked this conversation as resolved.

let decode (buf : Typed_array.arrayBuffer Js.t) width : (Js.js_string Js.t) =
let s = Typed_array.String.of_uint8Array (new%js Typed_array.uint8Array_fromBuffer buf) in
let m = Decode.decode "(decode)" s in
Js.string (Sexpr.to_string width (Arrange.module_ m))

let _ =
Js.export "WebAssemblyText"
(object%js (_self)

method encode (s : Js.js_string Js.t) : (Typed_array.arrayBuffer Js.t) =
let def = Parse.string_to_module (Js.to_string s) in
let bs =
match def.Source.it with
| Script.Textual m -> (Encode.encode m)
| Script.Encoded (_, bs) -> bs in
let buf = new%js Typed_array.arrayBuffer (String.length bs) in
let u8arr = new%js Typed_array.uint8Array_fromBuffer buf in
String.iteri (fun i c -> Typed_array.set u8arr i (int_of_char c)) bs; buf

method decode buf = decode buf 80
method decode2 buf width = decode buf width
Comment thread
takikawa marked this conversation as resolved.
Outdated

end)