Skip to content

improve Array's __set method #959

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 4 commits into from
Nov 16, 2019
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
7 changes: 5 additions & 2 deletions std/assembly/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,12 @@ export class Array<T> extends ArrayBufferView {
}

@operator("[]=") private __set(index: i32, value: T): void {
ensureSize(changetype<usize>(this), index + 1, alignof<T>());
if (<u32>index >= <u32>this.length_) {
if (index < 0) throw new RangeError(E_INDEXOUTOFRANGE);
ensureSize(changetype<usize>(this), index + 1, alignof<T>());
this.length_ = index + 1;
}
this.__unchecked_set(index, value);
if (index >= this.length_) this.length_ = index + 1;
}

@unsafe @operator("{}=") private __unchecked_set(index: i32, value: T): void {
Expand Down
3 changes: 1 addition & 2 deletions tests/compiler/binary.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,7 @@
return
end
local.get $2
i64.const 0
i64.eq
i64.eqz
if (result i64)
local.get $1
i64.const 0
Expand Down
3 changes: 1 addition & 2 deletions tests/compiler/i64-polyfill.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@
i64.const 32
i64.shl
i64.or
i64.const 0
i64.eq
i64.eqz
global.set $../../lib/i64/assembly/i64/lo
i32.const 0
global.set $../../lib/i64/assembly/i64/hi
Expand Down
12 changes: 4 additions & 8 deletions tests/compiler/mandelbrot.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
local.get $2
i64.const 1
i64.shl
i64.const 0
i64.eq
i64.eqz
if
f64.const -1
local.get $0
Expand Down Expand Up @@ -75,8 +74,7 @@
local.get $2
i64.const 32
i64.shl
i64.const 0
i64.eq
i64.eqz
i32.const 0
local.get $1
i32.const 1072693248
Expand Down Expand Up @@ -203,8 +201,7 @@
local.get $3
i64.const 1
i64.shl
i64.const 0
i64.eq
i64.eqz
if
f64.const -1
local.get $0
Expand Down Expand Up @@ -246,8 +243,7 @@
local.get $3
i64.const 32
i64.shl
i64.const 0
i64.eq
i64.eqz
i32.const 0
local.get $2
i32.const 1072693248
Expand Down
3 changes: 1 addition & 2 deletions tests/compiler/resolve-access.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,7 @@
(local $2 i32)
(local $3 i32)
local.get $0
i64.const 0
i64.eq
i64.eqz
if
i32.const 152
return
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/retain-release-sanity.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2298,7 +2298,7 @@
if
i32.const 424
i32.const 376
i32.const 285
i32.const 288
i32.const 20
call $~lib/builtins/abort
unreachable
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/retain-release-sanity.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3909,7 +3909,7 @@
if
i32.const 424
i32.const 376
i32.const 285
i32.const 288
i32.const 20
call $~lib/builtins/abort
unreachable
Expand Down
90 changes: 55 additions & 35 deletions tests/compiler/std/array.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2927,7 +2927,7 @@
if
i32.const 872
i32.const 488
i32.const 285
i32.const 288
i32.const 20
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -3010,7 +3010,7 @@
call $~lib/rt/pure/__release
i32.const 24
i32.const 488
i32.const 215
i32.const 218
i32.const 59
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -3190,7 +3190,7 @@
if
i32.const 872
i32.const 488
i32.const 346
i32.const 349
i32.const 20
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -3868,30 +3868,41 @@
call $~lib/array/Array<std/array/Ref>#__unchecked_get
)
(func $~lib/array/Array<i32>#__set (; 68 ;) (type $FUNCSIG$viii) (param $0 i32) (param $1 i32) (param $2 i32)
local.get $0
local.get $1
i32.const 1
i32.add
call $~lib/array/ensureSize
local.get $0
i32.load offset=4
local.get $1
i32.const 2
i32.shl
i32.add
local.get $2
i32.store
(local $3 i32)
local.get $1
local.get $0
i32.load offset=12
i32.ge_s
i32.ge_u
if
local.get $1
i32.const 0
i32.lt_s
if
i32.const 280
i32.const 488
i32.const 109
i32.const 21
call $~lib/builtins/abort
unreachable
end
local.get $0
local.get $1
i32.const 1
i32.add
local.tee $3
call $~lib/array/ensureSize
local.get $0
local.get $3
i32.store offset=12
end
local.get $0
i32.load offset=4
local.get $1
i32.const 2
i32.shl
i32.add
local.get $2
i32.store
)
(func $start:std/array~anonymous|0 (; 69 ;) (type $FUNCSIG$iiii) (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
local.get $2
Expand Down Expand Up @@ -6620,29 +6631,42 @@
call $~lib/rt/pure/__release
)
(func $~lib/array/Array<~lib/array/Array<i32>>#__set (; 138 ;) (type $FUNCSIG$viii) (param $0 i32) (param $1 i32) (param $2 i32)
(local $3 i32)
local.get $2
call $~lib/rt/pure/__retain
drop
local.get $0
local.get $1
i32.const 1
i32.add
call $~lib/array/ensureSize
local.get $0
local.get $1
local.get $2
call $~lib/array/Array<~lib/array/Array<i32>>#__unchecked_set
local.get $1
local.get $0
i32.load offset=12
i32.ge_s
i32.ge_u
if
local.get $1
i32.const 0
i32.lt_s
if
local.get $2
call $~lib/rt/pure/__release
i32.const 280
i32.const 488
i32.const 109
i32.const 21
call $~lib/builtins/abort
unreachable
end
local.get $0
local.get $1
i32.const 1
i32.add
local.tee $3
call $~lib/array/ensureSize
local.get $0
local.get $3
i32.store offset=12
end
local.get $0
local.get $1
local.get $2
call $~lib/array/Array<~lib/array/Array<i32>>#__unchecked_set
local.get $2
call $~lib/rt/pure/__release
)
Expand Down Expand Up @@ -10325,8 +10349,7 @@
(local $2 i32)
(local $3 i32)
local.get $0
i64.const 0
i64.eq
i64.eqz
if
i32.const 4768
call $~lib/rt/pure/__retain
Expand Down Expand Up @@ -10374,8 +10397,7 @@
i32.add
local.set $0
local.get $2
i64.const 0
i64.eq
i64.eqz
if
local.get $0
i32.const 48
Expand Down Expand Up @@ -10535,8 +10557,7 @@
(local $3 i32)
(local $4 i32)
local.get $0
i64.const 0
i64.eq
i64.eqz
if
i32.const 4768
call $~lib/rt/pure/__retain
Expand Down Expand Up @@ -10605,8 +10626,7 @@
i32.add
local.set $0
local.get $2
i64.const 0
i64.eq
i64.eqz
if
local.get $0
i32.const 48
Expand Down
Loading