Skip to content

Commit 3819b5d

Browse files
authored
fix: Fix localeCompare (#2304)
1 parent 324d41c commit 3819b5d

16 files changed

+4446
-4246
lines changed

Diff for: src/bindings/js.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -871,8 +871,8 @@ export class JSBuilder extends ExportsWalker {
871871
sb.push(`
872872
} = await (async url => instantiate(
873873
await (async () => {
874-
try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)) }
875-
catch { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)) }
874+
try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); }
875+
catch { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); }
876876
})(), {
877877
`);
878878
let needsMaybeDefault = false;

Diff for: std/assembly/string.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,13 @@ import { Array } from "./array";
188188
// TODO: implement full locale comparison with locales and Collator options
189189
localeCompare(other: String): i32 {
190190
if (changetype<usize>(other) == changetype<usize>(this)) return 0;
191-
var len: isize = this.length;
192-
var otherLen: isize = other.length;
193-
if (otherLen != len) return select(1, -1, len > otherLen);
194-
if (!otherLen) return 0; // "" == ""
191+
var alen = this.length;
192+
var blen = other.length;
195193
// @ts-ignore: string <-> String
196-
return compareImpl(this, 0, other, 0, otherLen);
194+
var res = compareImpl(this, 0, other, 0, <usize>min(alen, blen));
195+
res = res ? res : alen - blen;
196+
// normalize to [-1, 1] range
197+
return i32(res > 0) - i32(res < 0);
197198
}
198199

199200
startsWith(search: String, start: i32 = 0): bool {

Diff for: std/assembly/util/sort.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ export function COMPARATOR<T>(): Comparator<T> {
4444
}
4545
} else if (isString<T>()) {
4646
return (a, b) => {
47-
if (changetype<usize>(a) == changetype<usize>(b) || changetype<usize>(a) == 0 || changetype<usize>(b) == 0) return 0;
47+
if (
48+
changetype<usize>(a) == changetype<usize>(b) ||
49+
changetype<usize>(a) == 0 ||
50+
changetype<usize>(b) == 0
51+
) return 0;
4852
var alen = changetype<string>(a).length;
4953
var blen = changetype<string>(b).length;
5054
if (!(alen | blen)) return 0;

Diff for: tests/compiler/bindings/esm.debug.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,9 @@ export const {
370370
newInternref,
371371
internrefFunction
372372
} = await (async url => instantiate(
373-
await (
374-
globalThis.fetch && globalThis.WebAssembly.compileStreaming
375-
? globalThis.WebAssembly.compileStreaming(globalThis.fetch(url))
376-
: globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url))
377-
), {
373+
await (async () => {
374+
try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); }
375+
catch { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); }
376+
})(), {
378377
}
379378
))(new URL("esm.debug.wasm", import.meta.url));

Diff for: tests/compiler/bindings/esm.release.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,9 @@ export const {
370370
newInternref,
371371
internrefFunction
372372
} = await (async url => instantiate(
373-
await (
374-
globalThis.fetch && globalThis.WebAssembly.compileStreaming
375-
? globalThis.WebAssembly.compileStreaming(globalThis.fetch(url))
376-
: globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url))
377-
), {
373+
await (async () => {
374+
try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); }
375+
catch { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); }
376+
})(), {
378377
}
379378
))(new URL("esm.release.wasm", import.meta.url));

Diff for: tests/compiler/std-wasi/console.debug.wat

+1-1
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@
530530
if
531531
i32.const 160
532532
i32.const 224
533-
i32.const 741
533+
i32.const 742
534534
i32.const 49
535535
call $~lib/wasi/index/abort
536536
unreachable

Diff for: tests/compiler/std-wasi/crypto.debug.wat

+1-1
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@
504504
if
505505
i32.const 32
506506
i32.const 96
507-
i32.const 741
507+
i32.const 742
508508
i32.const 49
509509
call $~lib/wasi/index/abort
510510
unreachable

Diff for: tests/compiler/std-wasi/process.debug.wat

+2-2
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@
528528
if
529529
i32.const 112
530530
i32.const 176
531-
i32.const 741
531+
i32.const 742
532532
i32.const 49
533533
call $~lib/wasi/index/abort
534534
unreachable
@@ -6370,7 +6370,7 @@
63706370
if
63716371
i32.const 0
63726372
i32.const 176
6373-
i32.const 769
6373+
i32.const 770
63746374
i32.const 7
63756375
call $~lib/wasi/index/abort
63766376
unreachable

Diff for: tests/compiler/std-wasi/process.release.wat

+1-1
Original file line numberDiff line numberDiff line change
@@ -4856,7 +4856,7 @@
48564856
if
48574857
i32.const 0
48584858
i32.const 1200
4859-
i32.const 769
4859+
i32.const 770
48604860
i32.const 7
48614861
call $~lib/wasi/index/abort
48624862
unreachable

Diff for: tests/compiler/std/string-encoding.debug.wat

+2-2
Original file line numberDiff line numberDiff line change
@@ -2606,7 +2606,7 @@
26062606
if
26072607
i32.const 688
26082608
i32.const 752
2609-
i32.const 741
2609+
i32.const 742
26102610
i32.const 49
26112611
call $~lib/builtins/abort
26122612
unreachable
@@ -4748,7 +4748,7 @@
47484748
if
47494749
i32.const 0
47504750
i32.const 752
4751-
i32.const 769
4751+
i32.const 770
47524752
i32.const 7
47534753
call $~lib/builtins/abort
47544754
unreachable

Diff for: tests/compiler/std/string-encoding.release.wat

+2-2
Original file line numberDiff line numberDiff line change
@@ -3681,7 +3681,7 @@
36813681
if
36823682
i32.const 1712
36833683
i32.const 1776
3684-
i32.const 741
3684+
i32.const 742
36853685
i32.const 49
36863686
call $~lib/builtins/abort
36873687
unreachable
@@ -3777,7 +3777,7 @@
37773777
if
37783778
i32.const 0
37793779
i32.const 1776
3780-
i32.const 769
3780+
i32.const 770
37813781
i32.const 7
37823782
call $~lib/builtins/abort
37833783
unreachable

0 commit comments

Comments
 (0)