Skip to content

fix: Fix localeCompare result's range #2304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/bindings/js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -871,8 +871,8 @@ export class JSBuilder extends ExportsWalker {
sb.push(`
} = await (async url => instantiate(
await (async () => {
try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)) }
catch { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)) }
try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); }
catch { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); }
})(), {
`);
let needsMaybeDefault = false;
Expand Down
11 changes: 6 additions & 5 deletions std/assembly/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,13 @@ import { Array } from "./array";
// TODO: implement full locale comparison with locales and Collator options
localeCompare(other: String): i32 {
if (changetype<usize>(other) == changetype<usize>(this)) return 0;
var len: isize = this.length;
var otherLen: isize = other.length;
if (otherLen != len) return select(1, -1, len > otherLen);
if (!otherLen) return 0; // "" == ""
var alen = this.length;
var blen = other.length;
// @ts-ignore: string <-> String
return compareImpl(this, 0, other, 0, otherLen);
var res = compareImpl(this, 0, other, 0, <usize>min(alen, blen));
res = res ? res : alen - blen;
// normalize to [-1, 1] range
return i32(res > 0) - i32(res < 0);
}

startsWith(search: String, start: i32 = 0): bool {
Expand Down
6 changes: 5 additions & 1 deletion std/assembly/util/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ export function COMPARATOR<T>(): Comparator<T> {
}
} else if (isString<T>()) {
return (a, b) => {
if (changetype<usize>(a) == changetype<usize>(b) || changetype<usize>(a) == 0 || changetype<usize>(b) == 0) return 0;
if (
changetype<usize>(a) == changetype<usize>(b) ||
changetype<usize>(a) == 0 ||
changetype<usize>(b) == 0
) return 0;
var alen = changetype<string>(a).length;
var blen = changetype<string>(b).length;
if (!(alen | blen)) return 0;
Expand Down
9 changes: 4 additions & 5 deletions tests/compiler/bindings/esm.debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,9 @@ export const {
newInternref,
internrefFunction
} = await (async url => instantiate(
await (
globalThis.fetch && globalThis.WebAssembly.compileStreaming
? globalThis.WebAssembly.compileStreaming(globalThis.fetch(url))
: globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url))
), {
await (async () => {
try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); }
catch { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); }
})(), {
}
))(new URL("esm.debug.wasm", import.meta.url));
9 changes: 4 additions & 5 deletions tests/compiler/bindings/esm.release.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,9 @@ export const {
newInternref,
internrefFunction
} = await (async url => instantiate(
await (
globalThis.fetch && globalThis.WebAssembly.compileStreaming
? globalThis.WebAssembly.compileStreaming(globalThis.fetch(url))
: globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url))
), {
await (async () => {
try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); }
catch { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); }
})(), {
}
))(new URL("esm.release.wasm", import.meta.url));
2 changes: 1 addition & 1 deletion tests/compiler/std-wasi/console.debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@
if
i32.const 160
i32.const 224
i32.const 741
i32.const 742
i32.const 49
call $~lib/wasi/index/abort
unreachable
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/std-wasi/crypto.debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@
if
i32.const 32
i32.const 96
i32.const 741
i32.const 742
i32.const 49
call $~lib/wasi/index/abort
unreachable
Expand Down
4 changes: 2 additions & 2 deletions tests/compiler/std-wasi/process.debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@
if
i32.const 112
i32.const 176
i32.const 741
i32.const 742
i32.const 49
call $~lib/wasi/index/abort
unreachable
Expand Down Expand Up @@ -6370,7 +6370,7 @@
if
i32.const 0
i32.const 176
i32.const 769
i32.const 770
i32.const 7
call $~lib/wasi/index/abort
unreachable
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/std-wasi/process.release.wat
Original file line number Diff line number Diff line change
Expand Up @@ -4856,7 +4856,7 @@
if
i32.const 0
i32.const 1200
i32.const 769
i32.const 770
i32.const 7
call $~lib/wasi/index/abort
unreachable
Expand Down
4 changes: 2 additions & 2 deletions tests/compiler/std/string-encoding.debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2606,7 +2606,7 @@
if
i32.const 688
i32.const 752
i32.const 741
i32.const 742
i32.const 49
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -4748,7 +4748,7 @@
if
i32.const 0
i32.const 752
i32.const 769
i32.const 770
i32.const 7
call $~lib/builtins/abort
unreachable
Expand Down
4 changes: 2 additions & 2 deletions tests/compiler/std/string-encoding.release.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3681,7 +3681,7 @@
if
i32.const 1712
i32.const 1776
i32.const 741
i32.const 742
i32.const 49
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -3777,7 +3777,7 @@
if
i32.const 0
i32.const 1776
i32.const 769
i32.const 770
i32.const 7
call $~lib/builtins/abort
unreachable
Expand Down
Loading