|
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))) |
| 49 | + |
| 50 | + (global $text_converters_available (mut i32) (i32.const 0)) |
| 51 | + (global $string_builtins_available (mut i32) (i32.const 0)) |
44 | 52 |
|
45 | | - (global $builtins_available (mut i32) (i32.const 0)) |
| 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))) |
46 | 56 |
|
47 | 57 | (start $init) |
48 | 58 |
|
49 | 59 | (func $init |
50 | 60 | ;; Our dummy implementation of string conversion always returns |
51 | 61 | ;; the empty string. |
52 | | - (global.set $builtins_available |
| 62 | + (global.set $text_converters_available |
53 | 63 | (i32.ne |
54 | 64 | (i32.const 0) |
55 | 65 | (call $compare_strings |
|
58 | 68 | (i32.const 0) (i32.const 1)) |
59 | 69 | (call $decodeStringFromUTF8Array |
60 | 70 | (array.new_fixed $string 1 (i32.const 1)) |
61 | | - (i32.const 0) (i32.const 1)))))) |
| 71 | + (i32.const 0) (i32.const 1))))) |
| 72 | + (global.set $string_builtins_available |
| 73 | + (i32.ne |
| 74 | + (i32.const 0) |
| 75 | + (call $compare_strings |
| 76 | + (call $fromCharCodeArray |
| 77 | + (array.new_fixed $wstring 1 (i32.const 0)) |
| 78 | + (i32.const 0) (i32.const 1)) |
| 79 | + (call $fromCharCodeArray |
| 80 | + (array.new_fixed $wstring 1 (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)))))))) |
62 | 89 |
|
63 | 90 | (func (export "jsstring_compare") |
64 | 91 | (param $s anyref) (param $s' anyref) (result i32) |
|
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 $c i32) |
| 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 $continue |
| 114 | + (i32.and (global.get $string_builtins_available) |
| 115 | + (i32.le_u (local.get $len) (global.get $utf16_buffer_size))) |
| 116 | + (then |
| 117 | + (loop $loop |
| 118 | + (if (i32.lt_u (local.get $i) (local.get $len)) |
| 119 | + (then |
| 120 | + (local.set $c |
| 121 | + (array.get $string (local.get $s) |
| 122 | + (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)) |
| 127 | + (local.set $i (i32.add (local.get $i) (i32.const 1))) |
| 128 | + (br $loop)))) |
| 129 | + (return |
| 130 | + (any.convert_extern |
| 131 | + (call $fromCharCodeArray (global.get $buffer) |
| 132 | + (i32.const 0) (local.get $len)))))) |
85 | 133 | (return_call $jsstring_of_substring_fallback |
86 | 134 | (local.get $s) (local.get $pos) (local.get $len))) |
87 | 135 |
|
|
90 | 138 | (local.get $s) (i32.const 0) (array.len (local.get $s)))) |
91 | 139 |
|
92 | 140 | (func (export "string_of_jsstring") (param $s anyref) (result (ref $string)) |
93 | | - (if (global.get $builtins_available) |
| 141 | + (if (global.get $text_converters_available) |
94 | 142 | (then |
95 | 143 | (return_call $encodeStringToUTF8Array |
96 | 144 | (extern.convert_any (local.get $s))))) |
|
0 commit comments