Skip to content

Commit

Permalink
Merge pull request #19 from grain-lang/u8a
Browse files Browse the repository at this point in the history
Possible Uint8Array fixes
  • Loading branch information
phated committed Jul 25, 2020
2 parents b0e19b0 + a70389e commit ddb40cc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
39 changes: 26 additions & 13 deletions js/module.ml
Expand Up @@ -2,6 +2,28 @@ open Js_of_ocaml
open Js_of_ocaml.Js
open Js_of_ocaml.Js.Unsafe

let make_encoder () = new_obj global ##. TextEncoder [||]

let encode encoder (value : string) =
meth_call encoder "encode" [| inject (string value) |]

let make_decoder () = new_obj global ##. TextDecoder [||]

let decode decoder u8a = to_string (meth_call decoder "decode" [| inject u8a |])

(* Uint8Array/Byte utilities *)
let u8a_to_bytes u8a =
let decoder = make_decoder () in
Bytes.of_string (decode decoder u8a)

let bytes_to_u8a byts =
let encoder = make_encoder () in
encode encoder (Bytes.to_string byts)

let string_to_u8a str =
let encoder = make_encoder () in
encode encoder str

type t

let create () = new_obj global ##. binaryen ##. Module [||]
Expand All @@ -10,9 +32,7 @@ let dispose wasm_mod = ignore (meth_call wasm_mod "dispose" [||])

(* TODO: Check the unit8Array conversion *)
let add_custom_section wasm_mod name contents =
let contents =
meth_call Typed_array.uint8Array "from" [| inject (string contents) |]
in
let contents = string_to_u8a contents in
ignore
(meth_call wasm_mod "addCustomSection"
[| inject (string name); inject contents |])
Expand Down Expand Up @@ -88,32 +108,25 @@ let run_passes wasm_mod passes =

let auto_drop wasm_mod = meth_call wasm_mod "autoDrop" [||]

let uint8array_to_bytes u8a =
Bytes.of_string (Typed_array.String.of_uint8Array u8a)

let bytes_to_uint8array byts =
meth_call Typed_array.uint8Array "from"
[| inject (string (Bytes.to_string byts)) |]

(* TODO: This returns the wrong type, need to convert from `Uint8Array` to `bytes` *)
let write wasm_mod sourcemap_url =
match sourcemap_url with
| Some url ->
let obj = meth_call wasm_mod "emitBinary" [| inject url |] in
let binary = get obj "binary" in
let soucemap = get obj "soureMap" in
(uint8array_to_bytes binary, Some soucemap)
(u8a_to_bytes binary, Some soucemap)
| None ->
let binary = meth_call wasm_mod "emitBinary" [||] in
(uint8array_to_bytes binary, None)
(u8a_to_bytes binary, None)

let write_text wasm_mod =
let text = meth_call wasm_mod "emitText" [||] in
to_string text

(* TODO: This doesn't handle `bytes` correctly *)
let read byts =
let data = bytes_to_uint8array byts in
let data = bytes_to_u8a byts in
meth_call global##.binaryen "readBinary" [| inject data |]

let interpret wasm_mod = meth_call wasm_mod "interpret" [||]
Expand Down
9 changes: 9 additions & 0 deletions test/test.expected
Expand Up @@ -7,3 +7,12 @@
)
)
)
(module
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(func $0 (param $0 i32) (param $1 i32) (result i32)
(i32.add
(local.get $0)
(local.get $1)
)
)
)
10 changes: 10 additions & 0 deletions test/test.ml
Expand Up @@ -19,4 +19,14 @@ let adder = Function.add_function wasm_mod "adder" params results [||] add

let _ = Module.print wasm_mod

let byts, _ = Module.write wasm_mod None

let new_mod = Module.read byts

let _ = Module.validate new_mod

let _ = Module.print new_mod

let _ = Module.dispose wasm_mod

let _ = Module.dispose new_mod

0 comments on commit ddb40cc

Please sign in to comment.