Skip to content

Fix ArrayBufferView interface #1112

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

Closed
wants to merge 1 commit into from
Closed
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 src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1668,9 +1668,9 @@ export class Compiler extends DiagnosticEmitter {

var bufferAddress32 = i64_low(bufferSegment.offset) + runtimeHeaderSize;
assert(!program.options.isWasm64); // TODO
assert(arrayInstance.writeField("buffer", bufferAddress32, buf, runtimeHeaderSize));
assert(arrayInstance.writeField("data", bufferAddress32, buf, runtimeHeaderSize));
assert(arrayInstance.writeField("dataStart", bufferAddress32, buf, runtimeHeaderSize));
assert(arrayInstance.writeField("byteLength", bufferLength, buf, runtimeHeaderSize));
assert(arrayInstance.writeField("dataLength", bufferLength, buf, runtimeHeaderSize));
assert(arrayInstance.writeField("length_", arrayLength, buf, runtimeHeaderSize));

return this.addMemorySegment(buf);
Expand Down
17 changes: 11 additions & 6 deletions std/assembly/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH, E_EMPTYARRAY, E_HOLEYARRAY } from "

/** Ensures that the given array has _at least_ the specified backing size. */
function ensureSize(array: usize, minSize: usize, alignLog2: u32): void {
var oldCapacity = changetype<ArrayBufferView>(array).byteLength;
var oldCapacity = changetype<ArrayBufferView>(array).dataLength;
if (minSize > <usize>oldCapacity >>> alignLog2) {
if (minSize > BLOCK_MAXSIZE >>> alignLog2) throw new RangeError(E_INVALIDLENGTH);
let oldData = changetype<usize>(changetype<ArrayBufferView>(array).buffer);
let oldData = changetype<usize>(changetype<ArrayBufferView>(array).data);
let newCapacity = minSize << alignLog2;
let newData = __realloc(oldData, newCapacity); // keeps RC
memory.fill(newData + oldCapacity, 0, newCapacity - oldCapacity);
if (newData !== oldData) { // oldData has been free'd
store<usize>(array, newData, offsetof<ArrayBufferView>("buffer"));
store<usize>(array, newData, offsetof<ArrayBufferView>("data"));
store<usize>(array, newData, offsetof<ArrayBufferView>("dataStart"));
}
store<u32>(array, newCapacity, offsetof<ArrayBufferView>("byteLength"));
store<i32>(array, newCapacity, offsetof<ArrayBufferView>("dataLength"));
}
}

Expand Down Expand Up @@ -49,10 +49,15 @@ export class Array<T> extends ArrayBufferView {
}

constructor(length: i32 = 0) {
super(length, alignof<T>());
super();
super.alloc(length, alignof<T>());
this.length_ = length;
}

get buffer(): ArrayBuffer {
return this.data;
}

get length(): i32 {
return this.length_;
}
Expand Down Expand Up @@ -498,6 +503,6 @@ export class Array<T> extends ArrayBufferView {
cur += sizeof<usize>();
}
}
// automatically visits ArrayBufferView (.buffer) next
// automatically visits ArrayBufferView (.data) next
}
}
27 changes: 9 additions & 18 deletions std/assembly/arraybuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,17 @@ import { E_INVALIDLENGTH } from "./util/error";

export abstract class ArrayBufferView {

readonly buffer: ArrayBuffer;
@unsafe readonly dataStart: usize;
readonly byteLength: i32;
@unsafe data: ArrayBuffer;
@unsafe dataStart: usize;
@unsafe dataLength: i32;

get byteOffset(): i32 {
return <i32>(this.dataStart - changetype<usize>(this.buffer));
}

get length(): i32 {
ERROR("missing implementation: subclasses must implement ArrayBufferView#length");
return unreachable();
}

protected constructor(length: i32, alignLog2: i32) {
@unsafe protected alloc(length: i32, alignLog2: i32): void {
if (<u32>length > <u32>BLOCK_MAXSIZE >>> alignLog2) throw new RangeError(E_INVALIDLENGTH);
var buffer = __alloc(length = length << alignLog2, idof<ArrayBuffer>());
memory.fill(buffer, 0, <usize>length);
this.buffer = changetype<ArrayBuffer>(buffer); // retains
this.dataStart = buffer;
this.byteLength = length;
var data = __alloc(length = length << alignLog2, idof<ArrayBuffer>());
memory.fill(data, 0, <usize>length);
this.data = changetype<ArrayBuffer>(data); // retains
this.dataStart = data;
this.dataLength = length;
}
}

Expand Down
22 changes: 11 additions & 11 deletions std/assembly/atomics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export namespace Atomics {
export function load<T extends ArrayBufferView>(array: T, index: i32): valueof<T> {
if (index < 0 || (index << alignof<valueof<T>>()) >= array.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
return atomic.load<valueof<T>>(
changetype<usize>(array.buffer) + (index << alignof<valueof<T>>()) + array.byteOffset
changetype<usize>(array.data) + (index << alignof<valueof<T>>()) + array.byteOffset
);
}

Expand All @@ -17,7 +17,7 @@ export namespace Atomics {
export function store<T extends ArrayBufferView>(array: T, index: i32, value: valueof<T>): void {
if (index < 0 || (index << alignof<valueof<T>>()) >= array.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
atomic.store<valueof<T>>(
changetype<usize>(array.buffer) + (index << alignof<valueof<T>>()) + array.byteOffset,
changetype<usize>(array.data) + (index << alignof<valueof<T>>()) + array.byteOffset,
value
);
}
Expand All @@ -27,7 +27,7 @@ export namespace Atomics {
export function add<T extends ArrayBufferView>(array: T, index: i32, value: valueof<T>): valueof<T> {
if (index < 0 || (index << alignof<valueof<T>>()) >= array.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
return atomic.add<valueof<T>>(
changetype<usize>(array.buffer) + (index << alignof<valueof<T>>()) + array.byteOffset,
changetype<usize>(array.data) + (index << alignof<valueof<T>>()) + array.byteOffset,
value
);
}
Expand All @@ -37,7 +37,7 @@ export namespace Atomics {
export function sub<T extends ArrayBufferView>(array: T, index: i32, value: valueof<T>): valueof<T> {
if (index < 0 || (index << alignof<valueof<T>>()) >= array.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
return atomic.sub<valueof<T>>(
changetype<usize>(array.buffer) + (index << alignof<valueof<T>>()) + array.byteOffset,
changetype<usize>(array.data) + (index << alignof<valueof<T>>()) + array.byteOffset,
value
);
}
Expand All @@ -47,7 +47,7 @@ export namespace Atomics {
export function and<T extends ArrayBufferView>(array: T, index: i32, value: valueof<T>): valueof<T> {
if (index < 0 || (index << alignof<valueof<T>>()) >= array.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
return atomic.and<valueof<T>>(
changetype<usize>(array.buffer) + (index << alignof<valueof<T>>()) + array.byteOffset,
changetype<usize>(array.data) + (index << alignof<valueof<T>>()) + array.byteOffset,
value
);
}
Expand All @@ -57,7 +57,7 @@ export namespace Atomics {
export function or<T extends ArrayBufferView>(array: T, index: i32, value: valueof<T>): valueof<T> {
if (index < 0 || (index << alignof<valueof<T>>()) >= array.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
return atomic.or<valueof<T>>(
changetype<usize>(array.buffer) + (index << alignof<valueof<T>>()) + array.byteOffset,
changetype<usize>(array.data) + (index << alignof<valueof<T>>()) + array.byteOffset,
value
);
}
Expand All @@ -67,7 +67,7 @@ export namespace Atomics {
export function xor<T extends ArrayBufferView>(array: T, index: i32, value: valueof<T>): valueof<T> {
if (index < 0 || (index << alignof<valueof<T>>()) >= array.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
return atomic.xor<valueof<T>>(
changetype<usize>(array.buffer) + (index << alignof<valueof<T>>()) + array.byteOffset,
changetype<usize>(array.data) + (index << alignof<valueof<T>>()) + array.byteOffset,
value
);
}
Expand All @@ -77,7 +77,7 @@ export namespace Atomics {
export function exchange<T extends ArrayBufferView>(array: T, index: i32, value: valueof<T>): valueof<T> {
if (index < 0 || (index << alignof<valueof<T>>()) >= array.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
return atomic.xchg<valueof<T>>(
changetype<usize>(array.buffer) + (index << alignof<valueof<T>>()) + array.byteOffset,
changetype<usize>(array.data) + (index << alignof<valueof<T>>()) + array.byteOffset,
value
);
}
Expand All @@ -92,7 +92,7 @@ export namespace Atomics {
): valueof<T> {
if (index < 0 || (index << alignof<valueof<T>>()) >= array.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
return atomic.cmpxchg<valueof<T>>(
changetype<usize>(array.buffer) + (index << alignof<valueof<T>>()) + array.byteOffset,
changetype<usize>(array.data) + (index << alignof<valueof<T>>()) + array.byteOffset,
expectedValue,
replacementValue
);
Expand All @@ -101,14 +101,14 @@ export namespace Atomics {
// @ts-ignore: decorator
@inline
export function wait<T extends ArrayBufferView>(array: T, value: valueof<T>, timeout: i64 = -1): AtomicWaitResult {
return atomic.wait<valueof<T>>(changetype<usize>(array.buffer) + array.byteOffset, value, timeout);
return atomic.wait<valueof<T>>(changetype<usize>(array.data) + array.byteOffset, value, timeout);
}

// @ts-ignore: decorator
@inline
export function notify<T extends ArrayBufferView>(array: T, index: i32, count: i32 = -1): i32 {
if (index < 0 || (index << alignof<valueof<T>>()) >= array.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
return atomic.notify(changetype<usize>(array.buffer) + (index << alignof<valueof<T>>()) + array.byteOffset, count);
return atomic.notify(changetype<usize>(array.data) + (index << alignof<valueof<T>>()) + array.byteOffset, count);
}

export function isLockFree(size: usize): bool {
Expand Down
4 changes: 2 additions & 2 deletions std/assembly/rt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ export function __allocArray(length: i32, alignLog2: usize, id: u32, data: usize
var array = __alloc(offsetof<i32[]>(), id);
var bufferSize = <usize>length << alignLog2;
var buffer = __alloc(bufferSize, idof<ArrayBuffer>());
store<usize>(array, __retain(buffer), offsetof<ArrayBufferView>("buffer"));
store<usize>(array, __retain(buffer), offsetof<ArrayBufferView>("data"));
store<usize>(array, buffer, offsetof<ArrayBufferView>("dataStart"));
store<u32>(array, bufferSize, offsetof<ArrayBufferView>("byteLength"));
store<i32>(array, bufferSize, offsetof<ArrayBufferView>("dataLength"));
store<i32>(array, length, offsetof<i32[]>("length_"));
if (data) memory.copy(buffer, data, bufferSize);
return array;
Expand Down
Loading