Fix some map issues; Simplify internal ArrayBuffer API a bit

This commit is contained in:
dcodeIO
2018-06-20 15:51:47 +02:00
parent 48e96cbcf5
commit dd4be7b693
14 changed files with 17159 additions and 1132 deletions

2
std/assembly.d.ts vendored
View File

@ -307,7 +307,7 @@ declare class ArrayBuffer {
/** The size, in bytes, of the array. */
readonly byteLength: i32;
/** Constructs a new array buffer of the given length in bytes. */
constructor(length: i32);
constructor(length: i32, unsafe?: bool);
/** Returns a copy of this array buffer's bytes from begin, inclusive, up to end, exclusive. */
slice(begin?: i32, end?: i32): ArrayBuffer;
}

View File

@ -9,10 +9,10 @@ export class ArrayBuffer {
readonly byteLength: i32; // capped to [0, MAX_LENGTH]
constructor(length: i32) {
constructor(length: i32, unsafe: bool = false) {
if (<u32>length > <u32>MAX_BLENGTH) throw new RangeError("Invalid array buffer length");
var buffer = allocUnsafe(length);
set_memory(changetype<usize>(buffer) + HEADER_SIZE, 0, <usize>length);
if (!unsafe) set_memory(changetype<usize>(buffer) + HEADER_SIZE, 0, <usize>length);
return buffer;
}
@ -27,4 +27,16 @@ export class ArrayBuffer {
move_memory(changetype<usize>(buffer) + HEADER_SIZE, changetype<usize>(this) + HEADER_SIZE + begin, newLen);
return buffer;
}
// internals
static readonly HEADER_SIZE: usize = HEADER_SIZE;
@inline load<T>(index: i32): T {
return load<T>(changetype<usize>(this) + index * sizeof<T>(), HEADER_SIZE);
}
@inline store<T>(index: i32, value: T): void {
store<T>(changetype<usize>(this) + index * sizeof<T>(), value, HEADER_SIZE);
}
}