|
22 | 22 | (func $is_string (param externref) (result i32))) |
23 | 23 | (import "wasm:js-string" "hash" |
24 | 24 | (func $hash_string (param i32) (param anyref) (result i32))) |
| 25 | + (import "wasm:js-string" "fromCharCodeArray" |
| 26 | + (func $fromCharCodeArray |
| 27 | + (param (ref null $wstring)) (param i32) (param i32) |
| 28 | + (result (ref extern)))) |
25 | 29 |
|
26 | 30 | (import "wasm:text-decoder" "decodeStringFromUTF8Array" |
27 | 31 | (func $decodeStringFromUTF8Array |
|
41 | 45 | (func $append_string (param anyref) (param anyref) (result anyref))) |
42 | 46 |
|
43 | 47 | (type $string (array (mut i8))) |
| 48 | + (type $wstring (array (mut i16))) |
44 | 49 |
|
45 | | - (global $builtins_available (mut i32) (i32.const 0)) |
| 50 | + (global $text_converters_available (mut i32) (i32.const 0)) |
| 51 | + (global $string_builtins_available (mut i32) (i32.const 0)) |
46 | 52 |
|
47 | 53 | (start $init) |
48 | 54 |
|
49 | 55 | (func $init |
50 | 56 | ;; Our dummy implementation of string conversion always returns |
51 | 57 | ;; the empty string. |
52 | | - (global.set $builtins_available |
| 58 | + (global.set $text_converters_available |
53 | 59 | (i32.ne |
54 | 60 | (i32.const 0) |
55 | 61 | (call $compare_strings |
|
58 | 64 | (i32.const 0) (i32.const 1)) |
59 | 65 | (call $decodeStringFromUTF8Array |
60 | 66 | (array.new_fixed $string 1 (i32.const 1)) |
| 67 | + (i32.const 0) (i32.const 1))))) |
| 68 | + (global.set $string_builtins_available |
| 69 | + (i32.ne |
| 70 | + (i32.const 0) |
| 71 | + (call $compare_strings |
| 72 | + (call $fromCharCodeArray |
| 73 | + (array.new_fixed $wstring 1 (i32.const 0)) |
| 74 | + (i32.const 0) (i32.const 1)) |
| 75 | + (call $fromCharCodeArray |
| 76 | + (array.new_fixed $wstring 1 (i32.const 1)) |
61 | 77 | (i32.const 0) (i32.const 1)))))) |
62 | 78 |
|
63 | 79 | (func (export "jsstring_compare") |
|
71 | 87 |
|
72 | 88 | (export "jsstring_hash" (func $hash_string)) |
73 | 89 |
|
| 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 | + |
74 | 101 | ;; Used by package zarith_stubs_js |
75 | 102 | (func $jsstring_of_substring (export "jsstring_of_substring") |
76 | 103 | (param $s (ref $string)) (param $pos i32) (param $len i32) |
77 | 104 | (result anyref) |
78 | | - (if (global.get $builtins_available) |
| 105 | + (local $i i32) (local $ws (ref $wstring)) |
| 106 | + (if (global.get $text_converters_available) |
79 | 107 | (then |
80 | 108 | (return |
81 | 109 | (any.convert_extern |
82 | 110 | (call $decodeStringFromUTF8Array (local.get $s) |
83 | 111 | (local.get $pos) |
84 | 112 | (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)))) |
| 117 | + (then |
| 118 | + (local.set $ws (array.new $wstring (i32.const 0) (local.get $len))) |
| 119 | + (loop $loop |
| 120 | + (if (i32.lt_u (local.get $i) (local.get $len)) |
| 121 | + (then |
| 122 | + (array.set $wstring (local.get $ws) (local.get $i) |
| 123 | + (array.get $string (local.get $s) |
| 124 | + (i32.add (local.get $pos) (local.get $i)))) |
| 125 | + (local.set $i (i32.add (local.get $i) (i32.const 1))) |
| 126 | + |
| 127 | + (br $loop)))) |
| 128 | + (return |
| 129 | + (any.convert_extern |
| 130 | + (call $fromCharCodeArray (local.get $ws) |
| 131 | + (i32.const 0) (local.get $len)))))) |
85 | 132 | (return_call $jsstring_of_substring_fallback |
86 | 133 | (local.get $s) (local.get $pos) (local.get $len))) |
87 | 134 |
|
|
90 | 137 | (local.get $s) (i32.const 0) (array.len (local.get $s)))) |
91 | 138 |
|
92 | 139 | (func (export "string_of_jsstring") (param $s anyref) (result (ref $string)) |
93 | | - (if (global.get $builtins_available) |
| 140 | + (if (global.get $text_converters_available) |
94 | 141 | (then |
95 | 142 | (return_call $encodeStringToUTF8Array |
96 | 143 | (extern.convert_any (local.get $s))))) |
|
0 commit comments