2019-04-02 10:12:57 +02:00
|
|
|
import { HEADER, HEADER_SIZE, MAX_BYTELENGTH } from "./util/runtime";
|
|
|
|
import { runtime, __runtime_id } from "./runtime";
|
2019-03-22 15:43:07 +01:00
|
|
|
import { E_INVALIDLENGTH } from "./util/error";
|
2019-02-03 11:41:04 +02:00
|
|
|
|
2019-04-01 22:23:11 +02:00
|
|
|
export abstract class ArrayBufferView {
|
|
|
|
|
|
|
|
@unsafe data: ArrayBuffer;
|
|
|
|
@unsafe dataStart: usize;
|
|
|
|
@unsafe dataLength: u32;
|
|
|
|
|
|
|
|
protected constructor(length: i32, alignLog2: i32) {
|
2019-04-02 10:12:57 +02:00
|
|
|
if (<u32>length > <u32>MAX_BYTELENGTH >>> alignLog2) throw new RangeError(E_INVALIDLENGTH);
|
2019-04-01 22:23:11 +02:00
|
|
|
var buffer = new ArrayBuffer(length = length << alignLog2);
|
|
|
|
this.data = buffer;
|
|
|
|
this.dataStart = changetype<usize>(buffer);
|
|
|
|
this.dataLength = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
get byteOffset(): i32 {
|
|
|
|
return <i32>(this.dataStart - changetype<usize>(this.data));
|
|
|
|
}
|
|
|
|
|
|
|
|
get byteLength(): i32 {
|
|
|
|
return this.dataLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
get length(): i32 {
|
|
|
|
ERROR("missing implementation: subclasses must implement ArrayBufferView#length");
|
|
|
|
return unreachable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 22:35:47 +01:00
|
|
|
@sealed export class ArrayBuffer {
|
2018-03-23 01:47:01 +01:00
|
|
|
|
2019-03-19 08:20:10 +01:00
|
|
|
static isView<T>(value: T): bool {
|
2019-03-12 02:14:30 +01:00
|
|
|
if (value) {
|
|
|
|
if (value instanceof Int8Array) return true;
|
|
|
|
if (value instanceof Uint8Array) return true;
|
|
|
|
if (value instanceof Uint8ClampedArray) return true;
|
|
|
|
if (value instanceof Int16Array) return true;
|
|
|
|
if (value instanceof Uint16Array) return true;
|
|
|
|
if (value instanceof Int32Array) return true;
|
|
|
|
if (value instanceof Uint32Array) return true;
|
|
|
|
if (value instanceof Int64Array) return true;
|
|
|
|
if (value instanceof Uint64Array) return true;
|
|
|
|
if (value instanceof Float32Array) return true;
|
|
|
|
if (value instanceof Float64Array) return true;
|
|
|
|
if (value instanceof DataView) return true;
|
|
|
|
}
|
2019-02-03 11:41:04 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-13 22:35:47 +01:00
|
|
|
constructor(length: i32) {
|
2019-04-02 10:12:57 +02:00
|
|
|
if (<u32>length > <u32>MAX_BYTELENGTH) throw new RangeError(E_INVALIDLENGTH);
|
2019-04-01 22:23:11 +02:00
|
|
|
var buffer = runtime.allocate(<usize>length);
|
2019-03-14 12:46:36 +01:00
|
|
|
memory.fill(changetype<usize>(buffer), 0, <usize>length);
|
2019-04-02 10:12:57 +02:00
|
|
|
return changetype<ArrayBuffer>(runtime.register(buffer, __runtime_id<ArrayBuffer>()));
|
2019-03-13 22:35:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get byteLength(): i32 {
|
2019-03-15 09:26:31 +01:00
|
|
|
return changetype<HEADER>(changetype<usize>(this) - HEADER_SIZE).payloadSize;
|
2019-03-13 22:35:47 +01:00
|
|
|
}
|
|
|
|
|
2019-04-02 10:12:57 +02:00
|
|
|
slice(begin: i32 = 0, end: i32 = MAX_BYTELENGTH): ArrayBuffer {
|
2019-03-12 02:14:30 +01:00
|
|
|
var length = this.byteLength;
|
|
|
|
begin = begin < 0 ? max(length + begin, 0) : min(begin, length);
|
|
|
|
end = end < 0 ? max(length + end , 0) : min(end , length);
|
|
|
|
var outSize = <usize>max(end - begin, 0);
|
2019-04-01 22:23:11 +02:00
|
|
|
var out = runtime.allocate(outSize);
|
2019-03-11 07:45:47 +01:00
|
|
|
memory.copy(out, changetype<usize>(this) + <usize>begin, outSize);
|
2019-04-02 10:12:57 +02:00
|
|
|
return changetype<ArrayBuffer>(runtime.register(out, __runtime_id<ArrayBuffer>()));
|
2018-03-23 01:47:01 +01:00
|
|
|
}
|
2018-11-18 12:43:44 +02:00
|
|
|
|
|
|
|
toString(): string {
|
|
|
|
return "[object ArrayBuffer]";
|
|
|
|
}
|
2018-03-23 01:47:01 +01:00
|
|
|
}
|