import { BLOCK_MAXSIZE } from "./rt/common"; import { ArrayBuffer } from "./arraybuffer"; import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH } from "./util/error"; // TODO: there is probably a smarter way to check byteOffset for accesses larger than 1 byte export class DataView { readonly buffer: ArrayBuffer; @unsafe readonly dataStart: usize; readonly byteLength: i32; get byteOffset(): i32 { return (this.dataStart - changetype(this.buffer)); } constructor( buffer: ArrayBuffer, byteOffset: i32 = 0, byteLength: i32 = buffer.byteLength ) { if ( i32(byteLength > BLOCK_MAXSIZE) | i32(byteOffset + byteLength > buffer.byteLength) ) throw new RangeError(E_INVALIDLENGTH); this.buffer = buffer; // links let dataStart = changetype(buffer) + byteOffset; this.dataStart = dataStart; this.byteLength = byteLength; } getFloat32(byteOffset: i32, littleEndian: bool = false): f32 { if ( (byteOffset >>> 31) | i32(byteOffset + 4 > this.byteLength) ) throw new RangeError(E_INDEXOUTOFRANGE); return littleEndian ? load(this.dataStart + byteOffset) : reinterpret(bswap(load(this.dataStart + byteOffset))); } getFloat64(byteOffset: i32, littleEndian: bool = false): f64 { if ( (byteOffset >>> 31) | i32(byteOffset + 8 > this.byteLength) ) throw new RangeError(E_INDEXOUTOFRANGE); return littleEndian ? load(this.dataStart + byteOffset) : reinterpret(bswap(load(this.dataStart + byteOffset))); } getInt8(byteOffset: i32): i8 { if (byteOffset >= this.byteLength) throw new RangeError(E_INDEXOUTOFRANGE); return load(this.dataStart + byteOffset); } getInt16(byteOffset: i32, littleEndian: bool = false): i16 { if ( (byteOffset >>> 31) | i32(byteOffset + 2 > this.byteLength) ) throw new RangeError(E_INDEXOUTOFRANGE); let result: i16 = load(this.dataStart + byteOffset); return littleEndian ? result : bswap(result); } getInt32(byteOffset: i32, littleEndian: bool = false): i32 { if ( (byteOffset >>> 31) | i32(byteOffset + 4 > this.byteLength) ) throw new RangeError(E_INDEXOUTOFRANGE); let result: i32 = load(this.dataStart + byteOffset); return littleEndian ? result : bswap(result); } getUint8(byteOffset: i32): u8 { if (byteOffset >= this.byteLength) throw new RangeError(E_INDEXOUTOFRANGE); return load(this.dataStart + byteOffset); } getUint16(byteOffset: i32, littleEndian: bool = false): u16 { if ( (byteOffset >>> 31) | i32(byteOffset + 2 > this.byteLength) ) throw new RangeError(E_INDEXOUTOFRANGE); let result: u16 = load(this.dataStart + byteOffset); return littleEndian ? result : bswap(result); } getUint32(byteOffset: i32, littleEndian: bool = false): u32 { if ( (byteOffset >>> 31) | i32(byteOffset + 4 > this.byteLength) ) throw new RangeError(E_INDEXOUTOFRANGE); let result: u32 = load(this.dataStart + byteOffset); return littleEndian ? result : bswap(result); } setFloat32(byteOffset: i32, value: f32, littleEndian: bool = false): void { if ( (byteOffset >>> 31) | i32(byteOffset + 4 > this.byteLength) ) throw new RangeError(E_INDEXOUTOFRANGE); if (littleEndian) store(this.dataStart + byteOffset, value); else store(this.dataStart + byteOffset, bswap(reinterpret(value))); } setFloat64(byteOffset: i32, value: f64, littleEndian: bool = false): void { if ( (byteOffset >>> 31) | i32(byteOffset + 8 > this.byteLength) ) throw new RangeError(E_INDEXOUTOFRANGE); if (littleEndian) store(this.dataStart + byteOffset, value); else store(this.dataStart + byteOffset, bswap(reinterpret(value))); } setInt8(byteOffset: i32, value: i8): void { if (byteOffset >= this.byteLength) throw new RangeError(E_INDEXOUTOFRANGE); store(this.dataStart + byteOffset, value); } setInt16(byteOffset: i32, value: i16, littleEndian: bool = false): void { if ( (byteOffset >>> 31) | i32(byteOffset + 2 > this.byteLength) ) throw new RangeError(E_INDEXOUTOFRANGE); store(this.dataStart + byteOffset, littleEndian ? value : bswap(value)); } setInt32(byteOffset: i32, value: i32, littleEndian: bool = false): void { if ( (byteOffset >>> 31) | i32(byteOffset + 4 > this.byteLength) ) throw new RangeError(E_INDEXOUTOFRANGE); store(this.dataStart + byteOffset, littleEndian ? value : bswap(value)); } setUint8(byteOffset: i32, value: u8): void { if (byteOffset >= this.byteLength) throw new RangeError(E_INDEXOUTOFRANGE); store(this.dataStart + byteOffset, value); } setUint16(byteOffset: i32, value: u16, littleEndian: bool = false): void { if ( (byteOffset >>> 31) | i32(byteOffset + 2 > this.byteLength) ) throw new RangeError(E_INDEXOUTOFRANGE); store(this.dataStart + byteOffset, littleEndian ? value : bswap(value)); } setUint32(byteOffset: i32, value: u32, littleEndian: bool = false): void { if ( (byteOffset >>> 31) | i32(byteOffset + 4 > this.byteLength) ) throw new RangeError(E_INDEXOUTOFRANGE); store(this.dataStart + byteOffset, littleEndian ? value : bswap(value)); } // Non-standard additions that make sense in WebAssembly, but won't work in JS: getInt64(byteOffset: i32, littleEndian: bool = false): i64 { if ( (byteOffset >>> 31) | i32(byteOffset + 8 > this.byteLength) ) throw new RangeError(E_INDEXOUTOFRANGE); let result: i64 = load(this.dataStart + byteOffset); return littleEndian ? result : bswap(result); } getUint64(byteOffset: i32, littleEndian: bool = false): u64 { if ( (byteOffset >>> 31) | i32(byteOffset + 8 > this.byteLength) ) throw new RangeError(E_INDEXOUTOFRANGE); let result = load(this.dataStart + byteOffset); return littleEndian ? result : bswap(result); } setInt64(byteOffset: i32, value: i64, littleEndian: bool = false): void { if ( (byteOffset >>> 31) | i32(byteOffset + 8 > this.byteLength) ) throw new RangeError(E_INDEXOUTOFRANGE); store(this.dataStart + byteOffset, littleEndian ? value : bswap(value)); } setUint64(byteOffset: i32, value: u64, littleEndian: bool = false): void { if ( (byteOffset >>> 31) | i32(byteOffset + 8 > this.byteLength) ) throw new RangeError(E_INDEXOUTOFRANGE); store(this.dataStart + byteOffset, littleEndian ? value : bswap(value)); } toString(): string { return "[object DataView]"; } }