| 1 |
// |jit-test| skip-if: !wasmJSStringBuiltinsEnabled(); |
| 2 |
|
| 3 |
let testModule = `(module |
| 4 |
(type $arrayMutI16 (array (mut i16))) |
| 5 |
|
| 6 |
(func $testImp |
| 7 |
(import "wasm:js-string" "test") |
| 8 |
(param externref) |
| 9 |
(result i32) |
| 10 |
) |
| 11 |
(func $castImp |
| 12 |
(import "wasm:js-string" "cast") |
| 13 |
(param externref) |
| 14 |
(result (ref extern)) |
| 15 |
) |
| 16 |
(func $fromCharCodeArrayImp |
| 17 |
(import "wasm:js-string" "fromCharCodeArray") |
| 18 |
(param (ref null $arrayMutI16) i32 i32) |
| 19 |
(result (ref extern)) |
| 20 |
) |
| 21 |
(func $intoCharCodeArrayImp |
| 22 |
(import "wasm:js-string" "intoCharCodeArray") |
| 23 |
(param externref (ref null $arrayMutI16) i32) |
| 24 |
(result i32) |
| 25 |
) |
| 26 |
(func $fromCharCodeImp |
| 27 |
(import "wasm:js-string" "fromCharCode") |
| 28 |
(param i32) |
| 29 |
(result (ref extern)) |
| 30 |
) |
| 31 |
(func $fromCodePointImp |
| 32 |
(import "wasm:js-string" "fromCodePoint") |
| 33 |
(param i32) |
| 34 |
(result (ref extern)) |
| 35 |
) |
| 36 |
(func $charCodeAtImp |
| 37 |
(import "wasm:js-string" "charCodeAt") |
| 38 |
(param externref i32) |
| 39 |
(result i32) |
| 40 |
) |
| 41 |
(func $codePointAtImp |
| 42 |
(import "wasm:js-string" "codePointAt") |
| 43 |
(param externref i32) |
| 44 |
(result i32) |
| 45 |
) |
| 46 |
(func $lengthImp |
| 47 |
(import "wasm:js-string" "length") |
| 48 |
(param externref) |
| 49 |
(result i32) |
| 50 |
) |
| 51 |
(func $concatImp |
| 52 |
(import "wasm:js-string" "concat") |
| 53 |
(param externref externref) |
| 54 |
(result (ref extern)) |
| 55 |
) |
| 56 |
(func $substringImp |
| 57 |
(import "wasm:js-string" "substring") |
| 58 |
(param externref i32 i32) |
| 59 |
(result (ref extern)) |
| 60 |
) |
| 61 |
(func $equalsImp |
| 62 |
(import "wasm:js-string" "equals") |
| 63 |
(param externref externref) |
| 64 |
(result i32) |
| 65 |
) |
| 66 |
(func $compareImp |
| 67 |
(import "wasm:js-string" "compare") |
| 68 |
(param externref externref) |
| 69 |
(result i32) |
| 70 |
) |
| 71 |
|
| 72 |
(func $test (export "test") |
| 73 |
(param externref) |
| 74 |
(result i32) |
| 75 |
local.get 0 |
| 76 |
call $testImp |
| 77 |
) |
| 78 |
(func $cast (export "cast") |
| 79 |
(param externref) |
| 80 |
(result (ref extern)) |
| 81 |
local.get 0 |
| 82 |
call $castImp |
| 83 |
) |
| 84 |
(func $fromCharCodeArray (export "fromCharCodeArray") |
| 85 |
(param (ref null $arrayMutI16) i32 i32) |
| 86 |
(result (ref extern)) |
| 87 |
local.get 0 |
| 88 |
local.get 1 |
| 89 |
local.get 2 |
| 90 |
call $fromCharCodeArrayImp |
| 91 |
) |
| 92 |
(func $intoCharCodeArray (export "intoCharCodeArray") |
| 93 |
(param externref (ref null $arrayMutI16) i32) |
| 94 |
(result i32) |
| 95 |
local.get 0 |
| 96 |
local.get 1 |
| 97 |
local.get 2 |
| 98 |
call $intoCharCodeArrayImp |
| 99 |
) |
| 100 |
(func $fromCharCode (export "fromCharCode") |
| 101 |
(param i32) |
| 102 |
(result externref) |
| 103 |
local.get 0 |
| 104 |
call $fromCharCodeImp |
| 105 |
) |
| 106 |
(func $fromCodePoint (export "fromCodePoint") |
| 107 |
(param i32) |
| 108 |
(result externref) |
| 109 |
local.get 0 |
| 110 |
call $fromCodePointImp |
| 111 |
) |
| 112 |
(func $charCodeAt (export "charCodeAt") |
| 113 |
(param externref i32) |
| 114 |
(result i32) |
| 115 |
local.get 0 |
| 116 |
local.get 1 |
| 117 |
call $charCodeAtImp |
| 118 |
) |
| 119 |
(func $codePointAt (export "codePointAt") |
| 120 |
(param externref i32) |
| 121 |
(result i32) |
| 122 |
local.get 0 |
| 123 |
local.get 1 |
| 124 |
call $codePointAtImp |
| 125 |
) |
| 126 |
(func $length (export "length") |
| 127 |
(param externref) |
| 128 |
(result i32) |
| 129 |
local.get 0 |
| 130 |
call $lengthImp |
| 131 |
) |
| 132 |
(func $concat (export "concat") |
| 133 |
(param externref externref) |
| 134 |
(result externref) |
| 135 |
local.get 0 |
| 136 |
local.get 1 |
| 137 |
call $concatImp |
| 138 |
) |
| 139 |
(func $substring (export "substring") |
| 140 |
(param externref i32 i32) |
| 141 |
(result externref) |
| 142 |
local.get 0 |
| 143 |
local.get 1 |
| 144 |
local.get 2 |
| 145 |
call $substringImp |
| 146 |
) |
| 147 |
(func $equals (export "equals") |
| 148 |
(param externref externref) |
| 149 |
(result i32) |
| 150 |
local.get 0 |
| 151 |
local.get 1 |
| 152 |
call $equalsImp |
| 153 |
) |
| 154 |
(func $compare (export "compare") |
| 155 |
(param externref externref) |
| 156 |
(result i32) |
| 157 |
local.get 0 |
| 158 |
local.get 1 |
| 159 |
call $compareImp |
| 160 |
) |
| 161 |
)`; |
| 162 |
|
| 163 |
let { |
| 164 |
createArrayMutI16, |
| 165 |
arrayLength, |
| 166 |
arraySet, |
| 167 |
arrayGet |
| 168 |
} = wasmEvalText(`(module |
| 169 |
(type $arrayMutI16 (array (mut i16))) |
| 170 |
(func (export "createArrayMutI16") (param i32) (result anyref) |
| 171 |
i32.const 0 |
| 172 |
local.get 0 |
| 173 |
array.new $arrayMutI16 |
| 174 |
) |
| 175 |
(func (export "arrayLength") (param arrayref) (result i32) |
| 176 |
local.get 0 |
| 177 |
array.len |
| 178 |
) |
| 179 |
(func (export "arraySet") (param (ref $arrayMutI16) i32 i32) |
| 180 |
local.get 0 |
| 181 |
local.get 1 |
| 182 |
local.get 2 |
| 183 |
array.set $arrayMutI16 |
| 184 |
) |
| 185 |
(func (export "arrayGet") (param (ref $arrayMutI16) i32) (result i32) |
| 186 |
local.get 0 |
| 187 |
local.get 1 |
| 188 |
array.get_u $arrayMutI16 |
| 189 |
) |
| 190 |
)`).exports; |
| 191 |
|
| 192 |
function throwIfNotString(a) { |
| 193 |
if (typeof a !== "string") { |
| 194 |
throw new WebAssembly.RuntimeError(); |
| 195 |
} |
| 196 |
} |
| 197 |
function throwIfNotStringOrNull(a) { |
| 198 |
if (a !== null && typeof a !== "string") { |
| 199 |
throw new WebAssembly.RuntimeError(); |
| 200 |
} |
| 201 |
} |
| 202 |
let polyFillImports = { |
| 203 |
test: (string) => { |
| 204 |
if (string === null || |
| 205 |
typeof string !== "string") { |
| 206 |
return 0; |
| 207 |
} |
| 208 |
return 1; |
| 209 |
}, |
| 210 |
cast: (string) => { |
| 211 |
if (string === null || |
| 212 |
typeof string !== "string") { |
| 213 |
throw new WebAssembly.RuntimeError(); |
| 214 |
} |
| 215 |
return string; |
| 216 |
}, |
| 217 |
fromCharCodeArray: (array, arrayStart, arrayEnd) => { |
| 218 |
arrayStart >>>= 0; |
| 219 |
arrayEnd >>>= 0; |
| 220 |
if (array == null || |
| 221 |
arrayStart > arrayEnd || |
| 222 |
arrayEnd > arrayLength(array)) { |
| 223 |
throw new WebAssembly.RuntimeError(); |
| 224 |
} |
| 225 |
let result = ''; |
| 226 |
for (let i = arrayStart; i < arrayEnd; i++) { |
| 227 |
result += String.fromCharCode(arrayGet(array, i)); |
| 228 |
} |
| 229 |
return result; |
| 230 |
}, |
| 231 |
intoCharCodeArray: (string, arr, arrayStart) => { |
| 232 |
arrayStart >>>= 0; |
| 233 |
throwIfNotString(string); |
| 234 |
if (arr == null) { |
| 235 |
throw new WebAssembly.RuntimeError(); |
| 236 |
} |
| 237 |
let arrLength = arrayLength(arr); |
| 238 |
let stringLength = string.length; |
| 239 |
if (BigInt(arrayStart) + BigInt(stringLength) > BigInt(arrLength)) { |
| 240 |
throw new WebAssembly.RuntimeError(); |
| 241 |
} |
| 242 |
for (let i = 0; i < stringLength; i++) { |
| 243 |
arraySet(arr, arrayStart + i, string[i].charCodeAt(0)); |
| 244 |
} |
| 245 |
return stringLength; |
| 246 |
}, |
| 247 |
fromCharCode: (charCode) => { |
| 248 |
charCode >>>= 0; |
| 249 |
return String.fromCharCode(charCode); |
| 250 |
}, |
| 251 |
fromCodePoint: (codePoint) => { |
| 252 |
codePoint >>>= 0; |
| 253 |
return String.fromCodePoint(codePoint); |
| 254 |
}, |
| 255 |
charCodeAt: (string, stringIndex) => { |
| 256 |
stringIndex >>>= 0; |
| 257 |
throwIfNotString(string); |
| 258 |
if (stringIndex >= string.length) |
| 259 |
throw new WebAssembly.RuntimeError(); |
| 260 |
return string.charCodeAt(stringIndex); |
| 261 |
}, |
| 262 |
codePointAt: (string, stringIndex) => { |
| 263 |
stringIndex >>>= 0; |
| 264 |
throwIfNotString(string); |
| 265 |
if (stringIndex >= string.length) |
| 266 |
throw new WebAssembly.RuntimeError(); |
| 267 |
return string.codePointAt(stringIndex); |
| 268 |
}, |
| 269 |
length: (string) => { |
| 270 |
throwIfNotString(string); |
| 271 |
return string.length; |
| 272 |
}, |
| 273 |
concat: (stringA, stringB) => { |
| 274 |
throwIfNotString(stringA); |
| 275 |
throwIfNotString(stringB); |
| 276 |
return stringA + stringB; |
| 277 |
}, |
| 278 |
substring: (string, startIndex, endIndex) => { |
| 279 |
startIndex >>>= 0; |
| 280 |
endIndex >>>= 0; |
| 281 |
throwIfNotString(string); |
| 282 |
if (startIndex > string.length || |
| 283 |
endIndex < startIndex) { |
| 284 |
return ""; |
| 285 |
} |
| 286 |
if (endIndex > string.length) { |
| 287 |
endIndex = string.length; |
| 288 |
} |
| 289 |
return string.substring(startIndex, endIndex); |
| 290 |
}, |
| 291 |
equals: (stringA, stringB) => { |
| 292 |
throwIfNotStringOrNull(stringA); |
| 293 |
throwIfNotStringOrNull(stringB); |
| 294 |
return stringA === stringB; |
| 295 |
}, |
| 296 |
compare: (stringA, stringB) => { |
| 297 |
throwIfNotString(stringA); |
| 298 |
throwIfNotString(stringB); |
| 299 |
if (stringA < stringB) { |
| 300 |
return -1; |
| 301 |
} |
| 302 |
return stringA === stringB ? 0 : 1; |
| 303 |
}, |
| 304 |
}; |
| 305 |
|
| 306 |
function assertSameBehavior(funcA, funcB, ...params) { |
| 307 |
let resultA; |
| 308 |
let errA = null; |
| 309 |
try { |
| 310 |
resultA = funcA(...params); |
| 311 |
} catch (err) { |
| 312 |
errA = err; |
| 313 |
} |
| 314 |
|
| 315 |
let resultB; |
| 316 |
let errB = null; |
| 317 |
try { |
| 318 |
resultB = funcB(...params); |
| 319 |
} catch (err) { |
| 320 |
errB = err; |
| 321 |
} |
| 322 |
|
| 323 |
if (errA || errB) { |
| 324 |
assertEq(errA === null, errB === null, errA ? errA.message : errB.message); |
| 325 |
assertEq(Object.getPrototypeOf(errA), Object.getPrototypeOf(errB)); |
| 326 |
} |
| 327 |
assertEq(resultA, resultB); |
| 328 |
|
| 329 |
if (errA) { |
| 330 |
throw errA; |
| 331 |
} |
| 332 |
return resultA; |
| 333 |
} |
| 334 |
|
| 335 |
let builtinExports = wasmEvalText(testModule, {}, {builtins: ["js-string"]}).exports; |
| 336 |
let polyfillExports = wasmEvalText(testModule, { 'wasm:js-string': polyFillImports }).exports; |
| 337 |
|
| 338 |
let testStrings = ["", "a", "1", "ab", "hello, world", "\n", "☺", "☺smiley", String.fromCodePoint(0x10000, 0x10001)]; |
| 339 |
let testStringsAndNull = [...testStrings, null]; |
| 340 |
let testCharCodes = [1, 2, 3, 10, 0x7f, 0xff, 0xfffe, 0xffff]; |
| 341 |
let testCodePoints = [1, 2, 3, 10, 0x7f, 0xff, 0xfffe, 0xffff, 0x10000, 0x10001]; |
| 342 |
|
| 343 |
for (let a of WasmExternrefValues) { |
| 344 |
assertSameBehavior( |
| 345 |
builtinExports['test'], |
| 346 |
polyfillExports['test'], |
| 347 |
a |
| 348 |
); |
| 349 |
try { |
| 350 |
assertSameBehavior( |
| 351 |
builtinExports['cast'], |
| 352 |
polyfillExports['cast'], |
| 353 |
a |
| 354 |
); |
| 355 |
} catch (err) { |
| 356 |
assertEq(err instanceof WebAssembly.RuntimeError, true); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
for (let a of testCharCodes) { |
| 361 |
assertSameBehavior( |
| 362 |
builtinExports['fromCharCode'], |
| 363 |
polyfillExports['fromCharCode'], |
| 364 |
a |
| 365 |
); |
| 366 |
} |
| 367 |
|
| 368 |
for (let a of testCodePoints) { |
| 369 |
assertSameBehavior( |
| 370 |
builtinExports['fromCodePoint'], |
| 371 |
polyfillExports['fromCodePoint'], |
| 372 |
a |
| 373 |
); |
| 374 |
} |
| 375 |
|
| 376 |
for (let a of testStrings) { |
| 377 |
let length = assertSameBehavior( |
| 378 |
builtinExports['length'], |
| 379 |
polyfillExports['length'], |
| 380 |
a |
| 381 |
); |
| 382 |
|
| 383 |
for (let i = 0; i < length; i++) { |
| 384 |
let charCode = assertSameBehavior( |
| 385 |
builtinExports['charCodeAt'], |
| 386 |
polyfillExports['charCodeAt'], |
| 387 |
a, i |
| 388 |
); |
| 389 |
} |
| 390 |
for (let i = 0; i < length; i++) { |
| 391 |
let charCode = assertSameBehavior( |
| 392 |
builtinExports['codePointAt'], |
| 393 |
polyfillExports['codePointAt'], |
| 394 |
a, i |
| 395 |
); |
| 396 |
} |
| 397 |
|
| 398 |
let arrayMutI16 = createArrayMutI16(length); |
| 399 |
assertSameBehavior( |
| 400 |
builtinExports['intoCharCodeArray'], |
| 401 |
polyfillExports['intoCharCodeArray'], |
| 402 |
a, arrayMutI16, 0 |
| 403 |
); |
| 404 |
assertSameBehavior( |
| 405 |
builtinExports['fromCharCodeArray'], |
| 406 |
polyfillExports['fromCharCodeArray'], |
| 407 |
arrayMutI16, 0, length |
| 408 |
); |
| 409 |
|
| 410 |
for (let i = 0; i < length; i++) { |
| 411 |
// The end parameter is interpreted as unsigned and is always clamped to |
| 412 |
// the string length. This means that -1, and string.length + 1 are valid |
| 413 |
// end indices. |
| 414 |
for (let j = -1; j <= length + 1; j++) { |
| 415 |
assertSameBehavior( |
| 416 |
builtinExports['substring'], |
| 417 |
polyfillExports['substring'], |
| 418 |
a, i, j |
| 419 |
); |
| 420 |
} |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
for (let a of testStrings) { |
| 425 |
for (let b of testStrings) { |
| 426 |
assertSameBehavior( |
| 427 |
builtinExports['concat'], |
| 428 |
polyfillExports['concat'], |
| 429 |
a, b |
| 430 |
); |
| 431 |
assertSameBehavior( |
| 432 |
builtinExports['compare'], |
| 433 |
polyfillExports['compare'], |
| 434 |
a, b |
| 435 |
); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
for (let a of testStringsAndNull) { |
| 440 |
for (let b of testStringsAndNull) { |
| 441 |
assertSameBehavior( |
| 442 |
builtinExports['equals'], |
| 443 |
polyfillExports['equals'], |
| 444 |
a, b |
| 445 |
); |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
// fromCharCodeArray endIndex is an unsigned integer |
| 450 |
{ |
| 451 |
let arrayMutI16 = createArrayMutI16(1); |
| 452 |
assertErrorMessage(() => assertSameBehavior( |
| 453 |
builtinExports['fromCharCodeArray'], |
| 454 |
polyfillExports['fromCharCodeArray'], |
| 455 |
arrayMutI16, 1, -1 |
| 456 |
), WebAssembly.RuntimeError, /./); |
| 457 |
} |
| 458 |
|
| 459 |
// fromCharCodeArray is startIndex and endIndex, not a count |
| 460 |
{ |
| 461 |
let arrayMutI16 = createArrayMutI16(1); |
| 462 |
// Ask for [1, 1) to get an empty string. If misinterpreted as a count, this |
| 463 |
// will result in a trap. |
| 464 |
assertEq(assertSameBehavior( |
| 465 |
builtinExports['fromCharCodeArray'], |
| 466 |
polyfillExports['fromCharCodeArray'], |
| 467 |
arrayMutI16, 1, 1 |
| 468 |
), ""); |
| 469 |
} |
| 470 |
|
| 471 |
// fromCharCodeArray array is null |
| 472 |
{ |
| 473 |
assertErrorMessage(() => assertSameBehavior( |
| 474 |
builtinExports['fromCharCodeArray'], |
| 475 |
polyfillExports['fromCharCodeArray'], |
| 476 |
null, 0, 0 |
| 477 |
), WebAssembly.RuntimeError, /./); |
| 478 |
} |
| 479 |
|
| 480 |
// intoCharCodeArray array is null |
| 481 |
{ |
| 482 |
assertErrorMessage(() => assertSameBehavior( |
| 483 |
builtinExports['intoCharCodeArray'], |
| 484 |
polyfillExports['intoCharCodeArray'], |
| 485 |
"test", null, 0, |
| 486 |
), WebAssembly.RuntimeError, /./); |
| 487 |
} |