Skip to content
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

fix: expose dataStart to definitions #1828

Merged
merged 2 commits into from
Apr 27, 2021
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 std/assembly/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Array<T> {
// block is 16 bytes anyway so it's fine to have a couple extra fields in there.

private buffer: ArrayBuffer;
private dataStart: usize;
@unsafe readonly dataStart: usize;
private byteLength: i32;

// Also note that Array<T> with non-nullable T must guard against uninitialized null values
Expand Down Expand Up @@ -395,7 +395,7 @@ export class Array<T> {
var length = this.length_;
start = start < 0 ? max<i32>(length + start, 0) : min<i32>(start, length);
deleteCount = max<i32>(min<i32>(deleteCount, length - start), 0);
var result = changetype<Array<T>>(__newArray(deleteCount, alignof<T>(), idof<Array<T>>()));
var result = changetype<Array<T>>(__newArray(deleteCount, alignof<T>(), idof<Array<T>>()));
var resultStart = result.dataStart;
var thisStart = this.dataStart;
var thisBase = thisStart + (<usize>start << alignof<T>());
Expand Down
6 changes: 6 additions & 0 deletions std/assembly/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,8 @@ interface ArrayBufferView {
readonly byteOffset: i32;
/** The length in bytes from the start of the referenced {@link ArrayBuffer}. */
readonly byteLength: i32;
/** Returns raw pointer to data storage including offset (unsafe). */
readonly dataStart: usize;
}

/* @internal */
Expand All @@ -1474,6 +1476,8 @@ declare abstract class TypedArray<T> implements ArrayBufferView {
readonly byteOffset: i32;
/** The length in bytes from the start of the referenced {@link ArrayBuffer}. */
readonly byteLength: i32;
/** Returns raw pointer to data storage including offset (unsafe). */
readonly dataStart: usize;
/** The length (in elements). */
readonly length: i32;
/** Returns value using relative indexing. Index may be negative */
Expand Down Expand Up @@ -1585,6 +1589,8 @@ declare class Array<T> {
[key: number]: T;
/** Current length of the array. */
length: i32;
/** Returns raw pointer to data storage (unsafe). */
readonly dataStart: usize;
/** Constructs a new array. */
constructor(length?: i32);
at(index: i32): T;
Expand Down
19 changes: 19 additions & 0 deletions tests/compiler/std/array.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@
(export "memory" (memory $0))
(export "__setArgumentsLength" (func $~setArgumentsLength))
(export "_start" (func $~start))
(export "ArrayU32#get:dataStart" (func $export:~lib/array/Array<u32>#get:dataStart))
(export "ArrayU32#constructor" (func $export:std/array/ArrayU32#constructor@varargs))
(export "ArrayU32#get:length" (func $export:~lib/array/Array<u32>#get:length))
(export "ArrayU32#set:length" (func $export:~lib/array/Array<u32>#set:length))
Expand All @@ -616,6 +617,7 @@
(export "ArrayU32#join" (func $export:~lib/array/Array<u32>#join@varargs))
(export "ArrayU32#flat" (func $export:~lib/array/Array<u32>#flat))
(export "ArrayU32#toString" (func $export:~lib/array/Array<u32>#toString))
(export "ArrayU8#get:dataStart" (func $export:~lib/array/Array<u32>#get:dataStart))
(export "ArrayU8#constructor" (func $export:std/array/ArrayU8#constructor@varargs))
(export "ArrayU8#get:length" (func $export:~lib/array/Array<u32>#get:length))
(export "ArrayU8#set:length" (func $export:~lib/array/Array<u8>#set:length))
Expand All @@ -642,6 +644,7 @@
(export "ArrayU8#join" (func $export:~lib/array/Array<u8>#join@varargs))
(export "ArrayU8#flat" (func $export:~lib/array/Array<u32>#flat))
(export "ArrayU8#toString" (func $export:~lib/array/Array<u8>#toString))
(export "ArrayStr#get:dataStart" (func $export:~lib/array/Array<u32>#get:dataStart))
(export "ArrayStr#constructor" (func $export:std/array/ArrayStr#constructor@varargs))
(export "ArrayStr#get:length" (func $export:~lib/array/Array<u32>#get:length))
(export "ArrayStr#set:length" (func $export:~lib/array/Array<u32>#set:length))
Expand Down Expand Up @@ -22236,6 +22239,22 @@
global.set $~lib/memory/__stack_pointer
local.get $4
)
(func $export:~lib/array/Array<u32>#get:dataStart (param $0 i32) (result i32)
global.get $~lib/memory/__stack_pointer
i32.const 4
i32.sub
global.set $~lib/memory/__stack_pointer
call $~stack_check
global.get $~lib/memory/__stack_pointer
local.get $0
i32.store
local.get $0
i32.load offset=4
global.get $~lib/memory/__stack_pointer
i32.const 4
i32.add
global.set $~lib/memory/__stack_pointer
)
(func $export:std/array/ArrayU32#constructor@varargs (param $0 i32) (param $1 i32) (result i32)
global.get $~lib/memory/__stack_pointer
i32.const 4
Expand Down
60 changes: 60 additions & 0 deletions tests/compiler/std/array.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@
(export "memory" (memory $0))
(export "__setArgumentsLength" (func $~setArgumentsLength))
(export "_start" (func $~start))
(export "ArrayU32#get:dataStart" (func $export:~lib/array/Array<u32>#get:dataStart))
(export "ArrayU32#constructor" (func $export:std/array/ArrayU32#constructor@varargs))
(export "ArrayU32#get:length" (func $export:~lib/array/Array<u32>#get:length))
(export "ArrayU32#set:length" (func $export:~lib/array/Array<u32>#set:length))
Expand All @@ -377,6 +378,7 @@
(export "ArrayU32#join" (func $export:~lib/array/Array<u32>#join@varargs))
(export "ArrayU32#flat" (func $export:~lib/array/Array<u32>#flat))
(export "ArrayU32#toString" (func $export:~lib/array/Array<u32>#toString))
(export "ArrayU8#get:dataStart" (func $export:~lib/array/Array<u8>#get:dataStart))
(export "ArrayU8#constructor" (func $export:std/array/ArrayU8#constructor@varargs))
(export "ArrayU8#get:length" (func $export:~lib/array/Array<u8>#get:length))
(export "ArrayU8#set:length" (func $export:~lib/array/Array<u8>#set:length))
Expand All @@ -403,6 +405,7 @@
(export "ArrayU8#join" (func $export:~lib/array/Array<u8>#join@varargs))
(export "ArrayU8#flat" (func $export:~lib/array/Array<u8>#flat))
(export "ArrayU8#toString" (func $export:~lib/array/Array<u8>#toString))
(export "ArrayStr#get:dataStart" (func $export:~lib/array/Array<~lib/string/String>#get:dataStart))
(export "ArrayStr#constructor" (func $export:std/array/ArrayStr#constructor@varargs))
(export "ArrayStr#get:length" (func $export:~lib/array/Array<~lib/string/String>#get:length))
(export "ArrayStr#set:length" (func $export:~lib/array/Array<~lib/string/String>#set:length))
Expand Down Expand Up @@ -35312,6 +35315,25 @@
global.set $~lib/memory/__stack_pointer
local.get $2
)
(func $export:~lib/array/Array<u32>#get:dataStart (param $0 i32) (result i32)
(local $1 i32)
global.get $~lib/memory/__stack_pointer
i32.const 4
i32.sub
global.set $~lib/memory/__stack_pointer
call $~stack_check
global.get $~lib/memory/__stack_pointer
local.get $0
i32.store
local.get $0
call $~lib/array/Array<u32>#get:dataStart
local.set $1
global.get $~lib/memory/__stack_pointer
i32.const 4
i32.add
global.set $~lib/memory/__stack_pointer
local.get $1
)
(func $export:std/array/ArrayU32#constructor@varargs (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
global.get $~lib/memory/__stack_pointer
Expand Down Expand Up @@ -35853,6 +35875,25 @@
global.set $~lib/memory/__stack_pointer
local.get $1
)
(func $export:~lib/array/Array<u8>#get:dataStart (param $0 i32) (result i32)
(local $1 i32)
global.get $~lib/memory/__stack_pointer
i32.const 4
i32.sub
global.set $~lib/memory/__stack_pointer
call $~stack_check
global.get $~lib/memory/__stack_pointer
local.get $0
i32.store
local.get $0
call $~lib/array/Array<u8>#get:dataStart
local.set $1
global.get $~lib/memory/__stack_pointer
i32.const 4
i32.add
global.set $~lib/memory/__stack_pointer
local.get $1
)
(func $export:std/array/ArrayU8#constructor@varargs (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
global.get $~lib/memory/__stack_pointer
Expand Down Expand Up @@ -36394,6 +36435,25 @@
global.set $~lib/memory/__stack_pointer
local.get $1
)
(func $export:~lib/array/Array<~lib/string/String>#get:dataStart (param $0 i32) (result i32)
(local $1 i32)
global.get $~lib/memory/__stack_pointer
i32.const 4
i32.sub
global.set $~lib/memory/__stack_pointer
call $~stack_check
global.get $~lib/memory/__stack_pointer
local.get $0
i32.store
local.get $0
call $~lib/array/Array<~lib/string/String>#get:dataStart
local.set $1
global.get $~lib/memory/__stack_pointer
i32.const 4
i32.add
global.set $~lib/memory/__stack_pointer
local.get $1
)
(func $export:std/array/ArrayStr#constructor@varargs (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
global.get $~lib/memory/__stack_pointer
Expand Down