Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
vouillon committed Dec 20, 2024
1 parent d1b47a4 commit 3c28717
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion runtime/wasm/ints.wat
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@
(then (local.set $res (i32.sub (i32.const 0) (local.get $res)))))
(local.get $res))

(#string $INT_ERRMSG "int.of_string")
(#string $INT_ERRMSG "int_of_string")

(func (export "caml_int_of_string")
(param $v (ref eq)) (result (ref eq))
Expand Down
22 changes: 22 additions & 0 deletions runtime/wasm/jslib.wat
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@
(func $jsbytes_of_bytes (param (ref $bytes)) (result anyref)))
(import "jsstring" "bytes_of_jsbytes"
(func $bytes_of_jsbytes (param anyref) (result (ref $bytes))))
(import "jsstring" "jsstring_of_subbytes"
(func $jsstring_of_subbytes (param (ref $bytes) i32 i32) (result anyref)))
(import "jsstring" "jsstring_of_substring"
(func $jsstring_of_substring (param (ref eq) i32 i32) (result (ref eq))))
(import "int32" "caml_copy_int32"
(func $caml_copy_int32 (param i32) (result (ref eq))))
(import "int32" "Int32_val"
Expand Down Expand Up @@ -476,6 +480,24 @@
(return (struct.new $js (call $jsstring_of_bytes (local.get $s)))))
))

(#if use-js-string
(#then
(func (export "caml_jsstring_of_substring")
(param $s (ref eq)) (param $i i32) (param $l i32) (result (ref eq))
(return_call $jsstring_of_substring
(local.get $s) (local.get $i) (local.get $l)))
)
(#else
(func (export "caml_jsstring_of_substring")
(param $vs (ref eq)) (param $i i32) (param $l i32) (result (ref eq))
(local $s (ref $bytes))
(local.set $s (ref.cast (ref $bytes) (local.get $vs)))
(return
(struct.new $js
(call $jsstring_of_subbytes
(local.get $s) (local.get $i) (local.get $l)))))
))

(#if use-js-string
(#then
(func (export "caml_jsbytes_of_string")
Expand Down
16 changes: 15 additions & 1 deletion runtime/wasm/jsstring.wat
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
(func $hash_string (param i32) (param anyref) (result i32)))
(import "wasm:js-string" "length"
(func $string_length (param externref) (result i32)))
(import "wasm:js-string" "substring"
(func $string_sub (param externref i32 i32) (result (ref extern))))
(import "wasm:js-string" "fromCharCodeArray"
(func $fromCharCodeArray
(param (ref null $wstring)) (param i32) (param i32)
Expand Down Expand Up @@ -172,7 +174,7 @@
(br $loop))))
(i32.const 1))

(func (export "jsstring_of_string")
(func $jsstring_of_string (export "jsstring_of_string")
(param $s (ref eq)) (result (ref eq))
(if (result (ref eq)) (call $string_is_ascii (local.get $s))
(then
Expand All @@ -184,6 +186,18 @@
(struct.get $string 0
(ref.cast (ref $string) (local.get $s)))))))))

(func (export "jsstring_of_substring")
(param $s (ref eq)) (param $i i32) (param $l i32) (result (ref eq))
(return_call $jsstring_of_string
(struct.new $string
(any.convert_extern
(call $string_sub
(extern.convert_any
(struct.get $string 0
(ref.cast (ref $string) (local.get $s))))
(local.get $i)
(i32.add (local.get $i) (local.get $l)))))))

(func (export "string_of_jsstring")
(param $s (ref eq)) (result (ref eq))
(if (result (ref eq)) (call $string_is_ascii (local.get $s))
Expand Down

0 comments on commit 3c28717

Please sign in to comment.