Skip to content

Commit 02507d1

Browse files
committed
Use a statically allocated buffer
1 parent 48488ca commit 02507d1

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

runtime/wasm/jsstring.wat

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
(global $text_converters_available (mut i32) (i32.const 0))
5151
(global $string_builtins_available (mut i32) (i32.const 0))
5252

53+
(global $utf16_buffer_size i32 (i32.const 32768))
54+
(global $buffer (mut (ref $wstring))
55+
(array.new $wstring (i32.const 0) (i32.const 0)))
56+
5357
(start $init)
5458

5559
(func $init
@@ -74,7 +78,14 @@
7478
(i32.const 0) (i32.const 1))
7579
(call $fromCharCodeArray
7680
(array.new_fixed $wstring 1 (i32.const 1))
77-
(i32.const 0) (i32.const 1))))))
81+
(i32.const 0) (i32.const 1)))))
82+
(if (i32.eqz (global.get $text_converters_available))
83+
(then
84+
(if (global.get $string_builtins_available)
85+
(then
86+
(global.set $buffer
87+
(array.new $wstring (i32.const 0)
88+
(global.get $utf16_buffer_size))))))))
7889

7990
(func (export "jsstring_compare")
8091
(param $s anyref) (param $s' anyref) (result i32)
@@ -87,47 +98,37 @@
8798

8899
(export "jsstring_hash" (func $hash_string))
89100

90-
(func $is_ascii
91-
(param $s (ref $string)) (param $i i32) (param $end i32) (result i32)
92-
(loop $loop
93-
(if (i32.eq (local.get $i) (local.get $end))
94-
(then (return (i32.const 1))))
95-
(if (i32.gt_u (array.get_u $string (local.get $s) (local.get $i))
96-
(i32.const 127))
97-
(then (return (i32.const 0))))
98-
(local.set $i (i32.add (local.get $i) (i32.const 1)))
99-
(br $loop)))
100-
101101
;; Used by package zarith_stubs_js
102102
(func $jsstring_of_substring (export "jsstring_of_substring")
103103
(param $s (ref $string)) (param $pos i32) (param $len i32)
104104
(result anyref)
105-
(local $i i32) (local $ws (ref $wstring))
105+
(local $i i32) (local $c i32)
106106
(if (global.get $text_converters_available)
107107
(then
108108
(return
109109
(any.convert_extern
110110
(call $decodeStringFromUTF8Array (local.get $s)
111111
(local.get $pos)
112112
(i32.add (local.get $pos) (local.get $len)))))))
113-
(if (i32.and (global.get $string_builtins_available)
114-
(call $is_ascii
115-
(local.get $s) (local.get $pos)
116-
(i32.add (local.get $pos) (local.get $len))))
113+
(if $continue
114+
(i32.and (global.get $string_builtins_available)
115+
(i32.le_u (local.get $len) (global.get $utf16_buffer_size)))
117116
(then
118-
(local.set $ws (array.new $wstring (i32.const 0) (local.get $len)))
119117
(loop $loop
120118
(if (i32.lt_u (local.get $i) (local.get $len))
121119
(then
122-
(array.set $wstring (local.get $ws) (local.get $i)
120+
(local.set $c
123121
(array.get $string (local.get $s)
124122
(i32.add (local.get $pos) (local.get $i))))
123+
(br_if $continue
124+
(i32.ge_u (local.get $c) (i32.const 128)))
125+
(array.set $wstring (global.get $buffer) (local.get $i)
126+
(local.get $c))
125127
(local.set $i (i32.add (local.get $i) (i32.const 1)))
126-
127128
(br $loop))))
128129
(return
129130
(any.convert_extern
130-
(call $fromCharCodeArray (local.get $ws)
131+
(call $fromCharCodeArray (global.get $buffer)
131132
(i32.const 0) (local.get $len))))))
132133
(return_call $jsstring_of_substring_fallback
133134
(local.get $s) (local.get $pos) (local.get $len)))

0 commit comments

Comments
 (0)