arraybufferview

This commit is contained in:
dcode
2019-03-11 07:45:47 +01:00
parent 1198e71236
commit d9a5681489
10 changed files with 699 additions and 264 deletions

View File

@ -1,8 +1,8 @@
import {
HEADER_SIZE,
MAX_BLENGTH,
allocateUnsafe
} from "./internal/arraybuffer";
ALLOC_RAW,
REGISTER,
ArrayBufferBase
} from "./runtime";
import {
Uint8ClampedArray,
@ -21,9 +21,7 @@ import {
} from "./dataview";
@sealed
export class ArrayBuffer {
readonly byteLength: i32; // capped to [0, MAX_LENGTH]
export class ArrayBuffer extends ArrayBufferBase {
@inline static isView<T>(value: T): bool {
if (value === null) return false;
@ -40,28 +38,15 @@ export class ArrayBuffer {
return false;
}
// @unsafe
@inline get data(): usize { return changetype<usize>(this) + HEADER_SIZE; }
constructor(length: i32, unsafe: bool = false) {
if (<u32>length > <u32>MAX_BLENGTH) throw new RangeError("Invalid array buffer length");
var buffer = allocateUnsafe(length);
if (!unsafe) memory.fill(changetype<usize>(buffer) + HEADER_SIZE, 0, <usize>length);
return buffer;
}
slice(begin: i32 = 0, end: i32 = MAX_BLENGTH): ArrayBuffer {
slice(begin: i32 = 0, end: i32 = ArrayBuffer.MAX_BYTELENGTH): ArrayBuffer {
var len = this.byteLength;
begin = begin < 0 ? max(len + begin, 0) : min(begin, len);
end = end < 0 ? max(len + end, 0) : min(end, len);
len = max(end - begin, 0);
var buffer = allocateUnsafe(len);
memory.copy(
changetype<usize>(buffer) + HEADER_SIZE,
changetype<usize>(this) + HEADER_SIZE + begin,
len
);
return buffer;
var outSize = <usize>len;
var out = ALLOC_RAW(outSize);
memory.copy(out, changetype<usize>(this) + <usize>begin, outSize);
return REGISTER<ArrayBuffer>(out);
}
toString(): string {