Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
vouillon committed Dec 18, 2024
1 parent 0e6bb21 commit cb54d52
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 16 deletions.
8 changes: 7 additions & 1 deletion runtime/wasm/bigarray.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2057,6 +2057,8 @@
(local.get $len))
(local.get $s))

(#if use-js-string
(#then
(func (export "caml_uint8_array_of_string")
(param (ref eq)) (result (ref eq))
;; Convert a string to a typed array
Expand All @@ -2073,8 +2075,12 @@
(local.get $s) (i32.const 0) (local.get $ta) (i32.const 0)
(local.get $len))
(call $wrap (any.convert_extern (local.get $ta))))
)
(#else
(export "caml_uint8_array_of_string" (func $caml_uint8_array_of_bytes))
))

(func (export "caml_uint8_array_of_bytes")
(func $caml_uint8_array_of_bytes (export "caml_uint8_array_of_bytes")
(param (ref eq)) (result (ref eq))
;; Convert bytes to a typed array
(local $ta (ref extern)) (local $len i32)
Expand Down
38 changes: 32 additions & 6 deletions runtime/wasm/bigstring.wat
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,25 @@
(param i32)))
(import "hash" "caml_hash_mix_int"
(func $caml_hash_mix_int (param i32) (param i32) (result i32)))
(import "wasm:js-string" "charCodeAt"
(func $string_get (param externref i32) (result i32)))

(type $bytes (array (mut i8)))
(type $string (struct (field anyref)))

(#if use-js-string
(#then
(import "wasm:js-string" "charCodeAt"
(func $string_get (param externref i32) (result i32)))
(func $string_val (param $s (ref eq)) (result externref)
(extern.convert_any
(struct.get $string 0 (ref.cast (ref $string) (local.get $s)))))
)
(#else
(func $string_get (param $s (ref bytes)) (param $i i32)) (result i32)
(array.get $bytes (local.get $s) (local.get $i))
(func $string_val (param $s (ref eq)) (result $bytes)
(ref.cast (ref $bytes) (local.get $s)))
))

(func (export "caml_hash_mix_bigstring")
(param $h i32) (param $b (ref eq)) (result i32)
(local $data (ref extern))
Expand Down Expand Up @@ -167,12 +180,18 @@
(local $i i32) (local $pos1 i32) (local $pos2 i32) (local $len i32)
(local $c1 i32) (local $c2 i32)
(local $d1 (ref extern))
(local $s2 externref)
(local $s2
(#if use-js-string
(#then
externref
)
(#else
(ref $bytes)
))
)
(local.set $d1 (call $caml_ba_get_data (local.get $s1)))
(local.set $pos1 (i31.get_s (ref.cast (ref i31) (local.get $vpos1))))
(local.set $s2
(extern.convert_any
(struct.get $string 0 (ref.cast (ref $string) (local.get $vs2)))))
(local.set $s2 (call $string_val (local.get $vs2)))
(local.set $pos2 (i31.get_s (ref.cast (ref i31) (local.get $vpos2))))
(local.set $len (i31.get_s (ref.cast (ref i31) (local.get $vlen))))
(loop $loop
Expand Down Expand Up @@ -212,6 +231,8 @@
(br $loop))))
(ref.i31 (i32.const -1)))

(#if use-js-string
(#then
(func (export "caml_bigstring_blit_string_to_ba")
(param $str1 (ref eq)) (param $vpos1 (ref eq))
(param $ba2 (ref eq)) (param $vpos2 (ref eq))
Expand All @@ -230,6 +251,11 @@
(local.get $d2) (local.get $pos2)
(local.get $len))
(ref.i31 (i32.const 0)))
)
(#else
(export "caml_bigstring_blit_string_to_ba"
(func $caml_bigstring_blit_bytes_to_ba))
))

(func $caml_bigstring_blit_bytes_to_ba
(export "caml_bigstring_blit_bytes_to_ba")
Expand Down
6 changes: 6 additions & 0 deletions runtime/wasm/io.wat
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,8 @@
(local.set $len (i32.sub (local.get $len) (local.get $written)))
(br $loop)))))

(#if use-js-string
(#then
(func $caml_ml_output (export "caml_ml_output")
(param $ch (ref eq)) (param $s (ref eq)) (param $vpos (ref eq))
(param $vlen (ref eq)) (result (ref eq))
Expand All @@ -776,6 +778,10 @@
(br $loop))))
(call $caml_flush_if_unbuffered (local.get $ch))
(ref.i31 (i32.const 0)))
)
(#else
(export "caml_ml_output" (func $caml_ml_output_bytes))
))

(func $caml_ml_output_bytes (export "caml_ml_output_bytes")
(param $ch (ref eq)) (param $s (ref eq)) (param $vpos (ref eq))
Expand Down
21 changes: 18 additions & 3 deletions runtime/wasm/jslib.wat
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@
(array.get $block (local.get $a) (local.get $i))))
(call $set (local.get $o)
(call $unwrap
(call $jsstring_of_string
(call $caml_jsstring_of_string
(array.get $block (local.get $p) (i32.const 1))))
(call $unwrap
(array.get $block (local.get $p) (i32.const 2))))
Expand Down Expand Up @@ -368,7 +368,7 @@
(then
(array.set $block (local.get $a')
(i32.add (local.get $i) (i32.const 1))
(call $string_of_jsstring
(call $caml_string_of_jsstring
(call $wrap
(call $array_get (local.get $a) (local.get $i)))))
(local.set $i (i32.add (local.get $i) (i32.const 1)))
Expand Down Expand Up @@ -459,8 +459,21 @@
(return_call $unwrap (local.get $acc)))

(export "caml_js_from_string" (func $jsstring_of_string))
(export "caml_jsstring_of_string" (func $jsstring_of_string))

(#if use-js-string
(#then
(export "caml_jsstring_of_string" (func $jsstring_of_string))
(func $caml_jsstring_of_string (param (ref eq)) (result (ref eq))
(return_call $jsstring_of_string (local.get 0)))
)
(#else
(func $caml_jsstring_of_string (export "caml_jsstring_of_string")
(param (ref eq)) (result (ref eq))
(local $s (ref $string))
(local.set $s (ref.cast (ref $string) (local.get 0)))
(return (struct.new $js (call $jsstring_of_bytes (local.get $s)))))
))
;;ZZZZZZZZZZZZZZZZZZZ
(func $caml_jsbytes_of_string (export "caml_jsbytes_of_string")
(param (ref eq)) (result (ref eq))
(local.get 0))
Expand All @@ -473,6 +486,8 @@

(export "caml_js_to_string" (func $string_of_jsstring))
(export "caml_string_of_jsstring" (func $string_of_jsstring))
(func $caml_string_of_jsstring (param (ref eq)) (result (ref eq))
(return_call $string_of_jsstring (local.get 0)))

(func (export "caml_bytes_of_jsstring")
(param $s (ref eq)) (result (ref eq))
Expand Down
18 changes: 12 additions & 6 deletions runtime/wasm/md5.wat
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@
(field (ref $int_array)) ;; buffer
(field (ref $bytes)))) ;; intermediate buffer

(#if use-js-string
(#then
(func (export "caml_md5_string")
(param (ref eq)) (param (ref eq)) (param (ref eq)) (result (ref eq))
(return_call $caml_md5_bytes
(call $caml_bytes_of_string (local.get 0))
(local.get 1) (local.get 2)))
)
(#else
(export "caml_md5_string" (func $caml_md5_bytes))
))

(func $caml_md5_bytes (export "caml_md5_bytes")
(param (ref eq)) (param (ref eq)) (param (ref eq)) (result (ref eq))
(local $ctx (ref $context))
Expand All @@ -45,12 +57,6 @@
(i31.get_u (ref.cast (ref i31) (local.get 2))))
(return_call $MD5Final (local.get $ctx)))

(func (export "caml_md5_string")
(param (ref eq)) (param (ref eq)) (param (ref eq)) (result (ref eq))
(return_call $caml_md5_bytes
(call $caml_bytes_of_string (local.get 0))
(local.get 1) (local.get 2)))

(func (export "caml_md5_chan")
(param $ch (ref eq)) (param $vlen (ref eq)) (result (ref eq))
(local $len i32) (local $read i32)
Expand Down

0 comments on commit cb54d52

Please sign in to comment.