Skip to content

Commit 63b64ba

Browse files
LinusUdcodeIO
authored andcommitted
Respect current byteOffset in Array#subarray (AssemblyScript#329)
1 parent 4f95dce commit 63b64ba

File tree

3 files changed

+272
-1
lines changed

3 files changed

+272
-1
lines changed

std/assembly/internal/typedarray.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export abstract class TypedArray<T,V> {
9090
else end = max(min(end, length), begin);
9191
var slice = memory.allocate(offsetof<this>());
9292
store<usize>(slice, this.buffer, offsetof<this>("buffer"));
93-
store<i32>(slice, begin << alignof<T>(), offsetof<this>("byteOffset"));
93+
store<i32>(slice, this.byteOffset + (begin << alignof<T>()), offsetof<this>("byteOffset"));
9494
store<i32>(slice, (end - begin) << alignof<T>(), offsetof<this>("byteLength"));
9595
return changetype<this>(slice);
9696
}

tests/compiler/std/typedarray.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,29 @@ import { MAX_BLENGTH } from "internal/arraybuffer";
199199
const MAX_F64LENGTH = <u32>MAX_BLENGTH >> alignof<f64>();
200200
new Float64Array(MAX_F64LENGTH); // 1GB
201201
// new Float64Array(MAX_F64 + 1); // throws
202+
203+
var multisubarr = new Int8Array(6);
204+
multisubarr[0] = 1;
205+
multisubarr[1] = 2;
206+
multisubarr[2] = 3;
207+
multisubarr[3] = 4;
208+
multisubarr[4] = 5;
209+
multisubarr[5] = 6;
210+
211+
var multisubarr1 = multisubarr.subarray(1, 6);
212+
assert(multisubarr1[0] === 2);
213+
assert(multisubarr1.length === 5);
214+
assert(multisubarr1.byteOffset === 1);
215+
assert(multisubarr1.byteLength === 5);
216+
217+
var multisubarr2 = multisubarr1.subarray(1, 5);
218+
assert(multisubarr2[0] === 3);
219+
assert(multisubarr2.length === 4);
220+
assert(multisubarr2.byteOffset === 2);
221+
assert(multisubarr2.byteLength === 4);
222+
223+
var multisubarr3 = multisubarr2.subarray(1, 4);
224+
assert(multisubarr3[0] === 4);
225+
assert(multisubarr3.length === 3);
226+
assert(multisubarr3.byteOffset === 3);
227+
assert(multisubarr3.byteLength === 3);

tests/compiler/std/typedarray.untouched.wat

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@
7474
(global $std/typedarray/arr32 (mut i32) (i32.const 0))
7575
(global $std/typedarray/sub32 (mut i32) (i32.const 0))
7676
(global $std/typedarray/MAX_F64LENGTH i32 (i32.const 134217727))
77+
(global $std/typedarray/multisubarr (mut i32) (i32.const 0))
78+
(global $std/typedarray/multisubarr1 (mut i32) (i32.const 0))
79+
(global $std/typedarray/multisubarr2 (mut i32) (i32.const 0))
80+
(global $std/typedarray/multisubarr3 (mut i32) (i32.const 0))
7781
(global $HEAP_BASE i32 (i32.const 624))
7882
(export "memory" (memory $0))
7983
(export "table" (table $0))
@@ -1831,9 +1835,12 @@
18311835
i32.load
18321836
i32.store
18331837
get_local $4
1838+
get_local $0
1839+
i32.load offset=4
18341840
get_local $1
18351841
i32.const 2
18361842
i32.shl
1843+
i32.add
18371844
i32.store offset=4
18381845
get_local $4
18391846
get_local $2
@@ -1962,9 +1969,12 @@
19621969
i32.load
19631970
i32.store
19641971
get_local $4
1972+
get_local $0
1973+
i32.load offset=4
19651974
get_local $1
19661975
i32.const 3
19671976
i32.shl
1977+
i32.add
19681978
i32.store offset=4
19691979
get_local $4
19701980
get_local $2
@@ -3173,9 +3183,12 @@
31733183
i32.load
31743184
i32.store
31753185
get_local $4
3186+
get_local $0
3187+
i32.load offset=4
31763188
get_local $1
31773189
i32.const 0
31783190
i32.shl
3191+
i32.add
31793192
i32.store offset=4
31803193
get_local $4
31813194
get_local $2
@@ -4335,6 +4348,238 @@
43354348
get_global $std/typedarray/MAX_F64LENGTH
43364349
call $~lib/internal/typedarray/TypedArray<f64,f64>#constructor
43374350
drop
4351+
i32.const 0
4352+
i32.const 6
4353+
call $~lib/internal/typedarray/TypedArray<i8,i32>#constructor
4354+
set_global $std/typedarray/multisubarr
4355+
get_global $std/typedarray/multisubarr
4356+
i32.const 0
4357+
i32.const 1
4358+
call $~lib/internal/typedarray/TypedArray<i8,i32>#__set
4359+
get_global $std/typedarray/multisubarr
4360+
i32.const 1
4361+
i32.const 2
4362+
call $~lib/internal/typedarray/TypedArray<i8,i32>#__set
4363+
get_global $std/typedarray/multisubarr
4364+
i32.const 2
4365+
i32.const 3
4366+
call $~lib/internal/typedarray/TypedArray<i8,i32>#__set
4367+
get_global $std/typedarray/multisubarr
4368+
i32.const 3
4369+
i32.const 4
4370+
call $~lib/internal/typedarray/TypedArray<i8,i32>#__set
4371+
get_global $std/typedarray/multisubarr
4372+
i32.const 4
4373+
i32.const 5
4374+
call $~lib/internal/typedarray/TypedArray<i8,i32>#__set
4375+
get_global $std/typedarray/multisubarr
4376+
i32.const 5
4377+
i32.const 6
4378+
call $~lib/internal/typedarray/TypedArray<i8,i32>#__set
4379+
get_global $std/typedarray/multisubarr
4380+
i32.const 1
4381+
i32.const 6
4382+
call $~lib/typedarray/Int8Array#subarray
4383+
set_global $std/typedarray/multisubarr1
4384+
get_global $std/typedarray/multisubarr1
4385+
i32.const 0
4386+
call $~lib/internal/typedarray/TypedArray<i8,i32>#__get
4387+
i32.const 24
4388+
i32.shl
4389+
i32.const 24
4390+
i32.shr_s
4391+
i32.const 2
4392+
i32.eq
4393+
i32.eqz
4394+
if
4395+
i32.const 0
4396+
i32.const 8
4397+
i32.const 212
4398+
i32.const 0
4399+
call $~lib/env/abort
4400+
unreachable
4401+
end
4402+
block $~lib/internal/typedarray/TypedArray<i8,i32>#get:length|inlined.7 (result i32)
4403+
get_global $std/typedarray/multisubarr1
4404+
set_local $0
4405+
get_local $0
4406+
i32.load offset=8
4407+
i32.const 0
4408+
i32.shr_u
4409+
end
4410+
i32.const 5
4411+
i32.eq
4412+
i32.eqz
4413+
if
4414+
i32.const 0
4415+
i32.const 8
4416+
i32.const 213
4417+
i32.const 0
4418+
call $~lib/env/abort
4419+
unreachable
4420+
end
4421+
get_global $std/typedarray/multisubarr1
4422+
i32.load offset=4
4423+
i32.const 1
4424+
i32.eq
4425+
i32.eqz
4426+
if
4427+
i32.const 0
4428+
i32.const 8
4429+
i32.const 214
4430+
i32.const 0
4431+
call $~lib/env/abort
4432+
unreachable
4433+
end
4434+
get_global $std/typedarray/multisubarr1
4435+
i32.load offset=8
4436+
i32.const 5
4437+
i32.eq
4438+
i32.eqz
4439+
if
4440+
i32.const 0
4441+
i32.const 8
4442+
i32.const 215
4443+
i32.const 0
4444+
call $~lib/env/abort
4445+
unreachable
4446+
end
4447+
get_global $std/typedarray/multisubarr1
4448+
i32.const 1
4449+
i32.const 5
4450+
call $~lib/typedarray/Int8Array#subarray
4451+
set_global $std/typedarray/multisubarr2
4452+
get_global $std/typedarray/multisubarr2
4453+
i32.const 0
4454+
call $~lib/internal/typedarray/TypedArray<i8,i32>#__get
4455+
i32.const 24
4456+
i32.shl
4457+
i32.const 24
4458+
i32.shr_s
4459+
i32.const 3
4460+
i32.eq
4461+
i32.eqz
4462+
if
4463+
i32.const 0
4464+
i32.const 8
4465+
i32.const 218
4466+
i32.const 0
4467+
call $~lib/env/abort
4468+
unreachable
4469+
end
4470+
block $~lib/internal/typedarray/TypedArray<i8,i32>#get:length|inlined.8 (result i32)
4471+
get_global $std/typedarray/multisubarr2
4472+
set_local $0
4473+
get_local $0
4474+
i32.load offset=8
4475+
i32.const 0
4476+
i32.shr_u
4477+
end
4478+
i32.const 4
4479+
i32.eq
4480+
i32.eqz
4481+
if
4482+
i32.const 0
4483+
i32.const 8
4484+
i32.const 219
4485+
i32.const 0
4486+
call $~lib/env/abort
4487+
unreachable
4488+
end
4489+
get_global $std/typedarray/multisubarr2
4490+
i32.load offset=4
4491+
i32.const 2
4492+
i32.eq
4493+
i32.eqz
4494+
if
4495+
i32.const 0
4496+
i32.const 8
4497+
i32.const 220
4498+
i32.const 0
4499+
call $~lib/env/abort
4500+
unreachable
4501+
end
4502+
get_global $std/typedarray/multisubarr2
4503+
i32.load offset=8
4504+
i32.const 4
4505+
i32.eq
4506+
i32.eqz
4507+
if
4508+
i32.const 0
4509+
i32.const 8
4510+
i32.const 221
4511+
i32.const 0
4512+
call $~lib/env/abort
4513+
unreachable
4514+
end
4515+
get_global $std/typedarray/multisubarr2
4516+
i32.const 1
4517+
i32.const 4
4518+
call $~lib/typedarray/Int8Array#subarray
4519+
set_global $std/typedarray/multisubarr3
4520+
get_global $std/typedarray/multisubarr3
4521+
i32.const 0
4522+
call $~lib/internal/typedarray/TypedArray<i8,i32>#__get
4523+
i32.const 24
4524+
i32.shl
4525+
i32.const 24
4526+
i32.shr_s
4527+
i32.const 4
4528+
i32.eq
4529+
i32.eqz
4530+
if
4531+
i32.const 0
4532+
i32.const 8
4533+
i32.const 224
4534+
i32.const 0
4535+
call $~lib/env/abort
4536+
unreachable
4537+
end
4538+
block $~lib/internal/typedarray/TypedArray<i8,i32>#get:length|inlined.9 (result i32)
4539+
get_global $std/typedarray/multisubarr3
4540+
set_local $0
4541+
get_local $0
4542+
i32.load offset=8
4543+
i32.const 0
4544+
i32.shr_u
4545+
end
4546+
i32.const 3
4547+
i32.eq
4548+
i32.eqz
4549+
if
4550+
i32.const 0
4551+
i32.const 8
4552+
i32.const 225
4553+
i32.const 0
4554+
call $~lib/env/abort
4555+
unreachable
4556+
end
4557+
get_global $std/typedarray/multisubarr3
4558+
i32.load offset=4
4559+
i32.const 3
4560+
i32.eq
4561+
i32.eqz
4562+
if
4563+
i32.const 0
4564+
i32.const 8
4565+
i32.const 226
4566+
i32.const 0
4567+
call $~lib/env/abort
4568+
unreachable
4569+
end
4570+
get_global $std/typedarray/multisubarr3
4571+
i32.load offset=8
4572+
i32.const 3
4573+
i32.eq
4574+
i32.eqz
4575+
if
4576+
i32.const 0
4577+
i32.const 8
4578+
i32.const 227
4579+
i32.const 0
4580+
call $~lib/env/abort
4581+
unreachable
4582+
end
43384583
)
43394584
(func $null (; 44 ;) (type $v)
43404585
)

0 commit comments

Comments
 (0)