Skip to content

Commit eee0307

Browse files
MaxGraeydcodeIO
authored andcommitted
Fix Array#__set with negative indexes (#959)
1 parent aa296f6 commit eee0307

18 files changed

+333
-260
lines changed

std/assembly/array.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,12 @@ export class Array<T> extends ArrayBufferView {
105105
}
106106

107107
@operator("[]=") private __set(index: i32, value: T): void {
108-
ensureSize(changetype<usize>(this), index + 1, alignof<T>());
108+
if (<u32>index >= <u32>this.length_) {
109+
if (index < 0) throw new RangeError(E_INDEXOUTOFRANGE);
110+
ensureSize(changetype<usize>(this), index + 1, alignof<T>());
111+
this.length_ = index + 1;
112+
}
109113
this.__unchecked_set(index, value);
110-
if (index >= this.length_) this.length_ = index + 1;
111114
}
112115

113116
@unsafe @operator("{}=") private __unchecked_set(index: i32, value: T): void {

tests/compiler/binary.optimized.wat

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,7 @@
260260
return
261261
end
262262
local.get $2
263-
i64.const 0
264-
i64.eq
263+
i64.eqz
265264
if (result i64)
266265
local.get $1
267266
i64.const 0

tests/compiler/i64-polyfill.optimized.wat

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@
9494
i64.const 32
9595
i64.shl
9696
i64.or
97-
i64.const 0
98-
i64.eq
97+
i64.eqz
9998
global.set $../../lib/i64/assembly/i64/lo
10099
i32.const 0
101100
global.set $../../lib/i64/assembly/i64/hi

tests/compiler/mandelbrot.optimized.wat

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
local.get $2
3333
i64.const 1
3434
i64.shl
35-
i64.const 0
36-
i64.eq
35+
i64.eqz
3736
if
3837
f64.const -1
3938
local.get $0
@@ -75,8 +74,7 @@
7574
local.get $2
7675
i64.const 32
7776
i64.shl
78-
i64.const 0
79-
i64.eq
77+
i64.eqz
8078
i32.const 0
8179
local.get $1
8280
i32.const 1072693248
@@ -203,8 +201,7 @@
203201
local.get $3
204202
i64.const 1
205203
i64.shl
206-
i64.const 0
207-
i64.eq
204+
i64.eqz
208205
if
209206
f64.const -1
210207
local.get $0
@@ -246,8 +243,7 @@
246243
local.get $3
247244
i64.const 32
248245
i64.shl
249-
i64.const 0
250-
i64.eq
246+
i64.eqz
251247
i32.const 0
252248
local.get $2
253249
i32.const 1072693248

tests/compiler/resolve-access.optimized.wat

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,7 @@
485485
(local $2 i32)
486486
(local $3 i32)
487487
local.get $0
488-
i64.const 0
489-
i64.eq
488+
i64.eqz
490489
if
491490
i32.const 152
492491
return

tests/compiler/retain-release-sanity.optimized.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2298,7 +2298,7 @@
22982298
if
22992299
i32.const 424
23002300
i32.const 376
2301-
i32.const 285
2301+
i32.const 288
23022302
i32.const 20
23032303
call $~lib/builtins/abort
23042304
unreachable

tests/compiler/retain-release-sanity.untouched.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3909,7 +3909,7 @@
39093909
if
39103910
i32.const 424
39113911
i32.const 376
3912-
i32.const 285
3912+
i32.const 288
39133913
i32.const 20
39143914
call $~lib/builtins/abort
39153915
unreachable

tests/compiler/std/array.optimized.wat

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2927,7 +2927,7 @@
29272927
if
29282928
i32.const 872
29292929
i32.const 488
2930-
i32.const 285
2930+
i32.const 288
29312931
i32.const 20
29322932
call $~lib/builtins/abort
29332933
unreachable
@@ -3010,7 +3010,7 @@
30103010
call $~lib/rt/pure/__release
30113011
i32.const 24
30123012
i32.const 488
3013-
i32.const 215
3013+
i32.const 218
30143014
i32.const 59
30153015
call $~lib/builtins/abort
30163016
unreachable
@@ -3190,7 +3190,7 @@
31903190
if
31913191
i32.const 872
31923192
i32.const 488
3193-
i32.const 346
3193+
i32.const 349
31943194
i32.const 20
31953195
call $~lib/builtins/abort
31963196
unreachable
@@ -3868,30 +3868,41 @@
38683868
call $~lib/array/Array<std/array/Ref>#__unchecked_get
38693869
)
38703870
(func $~lib/array/Array<i32>#__set (; 68 ;) (type $FUNCSIG$viii) (param $0 i32) (param $1 i32) (param $2 i32)
3871-
local.get $0
3872-
local.get $1
3873-
i32.const 1
3874-
i32.add
3875-
call $~lib/array/ensureSize
3876-
local.get $0
3877-
i32.load offset=4
3878-
local.get $1
3879-
i32.const 2
3880-
i32.shl
3881-
i32.add
3882-
local.get $2
3883-
i32.store
3871+
(local $3 i32)
38843872
local.get $1
38853873
local.get $0
38863874
i32.load offset=12
3887-
i32.ge_s
3875+
i32.ge_u
38883876
if
3877+
local.get $1
3878+
i32.const 0
3879+
i32.lt_s
3880+
if
3881+
i32.const 280
3882+
i32.const 488
3883+
i32.const 109
3884+
i32.const 21
3885+
call $~lib/builtins/abort
3886+
unreachable
3887+
end
38893888
local.get $0
38903889
local.get $1
38913890
i32.const 1
38923891
i32.add
3892+
local.tee $3
3893+
call $~lib/array/ensureSize
3894+
local.get $0
3895+
local.get $3
38933896
i32.store offset=12
38943897
end
3898+
local.get $0
3899+
i32.load offset=4
3900+
local.get $1
3901+
i32.const 2
3902+
i32.shl
3903+
i32.add
3904+
local.get $2
3905+
i32.store
38953906
)
38963907
(func $start:std/array~anonymous|0 (; 69 ;) (type $FUNCSIG$iiii) (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
38973908
local.get $2
@@ -6620,29 +6631,42 @@
66206631
call $~lib/rt/pure/__release
66216632
)
66226633
(func $~lib/array/Array<~lib/array/Array<i32>>#__set (; 138 ;) (type $FUNCSIG$viii) (param $0 i32) (param $1 i32) (param $2 i32)
6634+
(local $3 i32)
66236635
local.get $2
66246636
call $~lib/rt/pure/__retain
66256637
drop
6626-
local.get $0
6627-
local.get $1
6628-
i32.const 1
6629-
i32.add
6630-
call $~lib/array/ensureSize
6631-
local.get $0
6632-
local.get $1
6633-
local.get $2
6634-
call $~lib/array/Array<~lib/array/Array<i32>>#__unchecked_set
66356638
local.get $1
66366639
local.get $0
66376640
i32.load offset=12
6638-
i32.ge_s
6641+
i32.ge_u
66396642
if
6643+
local.get $1
6644+
i32.const 0
6645+
i32.lt_s
6646+
if
6647+
local.get $2
6648+
call $~lib/rt/pure/__release
6649+
i32.const 280
6650+
i32.const 488
6651+
i32.const 109
6652+
i32.const 21
6653+
call $~lib/builtins/abort
6654+
unreachable
6655+
end
66406656
local.get $0
66416657
local.get $1
66426658
i32.const 1
66436659
i32.add
6660+
local.tee $3
6661+
call $~lib/array/ensureSize
6662+
local.get $0
6663+
local.get $3
66446664
i32.store offset=12
66456665
end
6666+
local.get $0
6667+
local.get $1
6668+
local.get $2
6669+
call $~lib/array/Array<~lib/array/Array<i32>>#__unchecked_set
66466670
local.get $2
66476671
call $~lib/rt/pure/__release
66486672
)
@@ -10325,8 +10349,7 @@
1032510349
(local $2 i32)
1032610350
(local $3 i32)
1032710351
local.get $0
10328-
i64.const 0
10329-
i64.eq
10352+
i64.eqz
1033010353
if
1033110354
i32.const 4768
1033210355
call $~lib/rt/pure/__retain
@@ -10374,8 +10397,7 @@
1037410397
i32.add
1037510398
local.set $0
1037610399
local.get $2
10377-
i64.const 0
10378-
i64.eq
10400+
i64.eqz
1037910401
if
1038010402
local.get $0
1038110403
i32.const 48
@@ -10535,8 +10557,7 @@
1053510557
(local $3 i32)
1053610558
(local $4 i32)
1053710559
local.get $0
10538-
i64.const 0
10539-
i64.eq
10560+
i64.eqz
1054010561
if
1054110562
i32.const 4768
1054210563
call $~lib/rt/pure/__retain
@@ -10605,8 +10626,7 @@
1060510626
i32.add
1060610627
local.set $0
1060710628
local.get $2
10608-
i64.const 0
10609-
i64.eq
10629+
i64.eqz
1061010630
if
1061110631
local.get $0
1061210632
i32.const 48

0 commit comments

Comments
 (0)