mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-13 23:11:41 +00:00
Add DataView to standard library (#316)
This commit is contained in:
committed by
Daniel Wirtz
parent
6f3209e6c6
commit
3f9758f35a
129
std/assembly/dataview.ts
Normal file
129
std/assembly/dataview.ts
Normal file
@ -0,0 +1,129 @@
|
||||
import { HEADER_SIZE } from "./internal/arraybuffer";
|
||||
|
||||
export class DataView {
|
||||
constructor(
|
||||
readonly buffer: ArrayBuffer,
|
||||
readonly byteOffset: i32 = 0,
|
||||
readonly byteLength: i32 = i32.MIN_VALUE,
|
||||
) {
|
||||
if (byteLength === i32.MIN_VALUE) byteLength = buffer.byteLength - byteOffset;
|
||||
|
||||
if (byteOffset < 0) throw new RangeError("byteOffset cannot be negative");
|
||||
if (byteLength < 0) throw new RangeError("byteLength cannot be negative");
|
||||
if (byteOffset + byteLength > buffer.byteLength) throw new RangeError("Length out of range of buffer");
|
||||
}
|
||||
|
||||
@inline
|
||||
getFloat32(byteOffset: i32, littleEndian: boolean = false): f32 {
|
||||
var result: u32 = load<u32>(changetype<usize>(this.buffer) + this.byteOffset + byteOffset, HEADER_SIZE);
|
||||
return reinterpret<f32>(littleEndian ? result : bswap<u32>(result));
|
||||
}
|
||||
|
||||
@inline
|
||||
getFloat64(byteOffset: i32, littleEndian: boolean = false): f64 {
|
||||
var result: u64 = load<u64>(changetype<usize>(this.buffer) + this.byteOffset + byteOffset, HEADER_SIZE);
|
||||
return reinterpret<f64>(littleEndian ? result : bswap<u64>(result));
|
||||
}
|
||||
|
||||
@inline
|
||||
getInt8(byteOffset: i32): i8 {
|
||||
return load<i8>(changetype<usize>(this.buffer) + this.byteOffset + byteOffset, HEADER_SIZE);
|
||||
}
|
||||
|
||||
@inline
|
||||
getInt16(byteOffset: i32, littleEndian: boolean = false): i16 {
|
||||
var result: i16 = load<i16>(changetype<usize>(this.buffer) + this.byteOffset + byteOffset, HEADER_SIZE);
|
||||
return littleEndian ? result : bswap<i16>(result);
|
||||
}
|
||||
|
||||
@inline
|
||||
getInt32(byteOffset: i32, littleEndian: boolean = false): i32 {
|
||||
var result: i32 = load<i32>(changetype<usize>(this.buffer) + this.byteOffset + byteOffset, HEADER_SIZE);
|
||||
return littleEndian ? result : bswap<i32>(result);
|
||||
}
|
||||
|
||||
@inline
|
||||
getUint8(byteOffset: i32): u8 {
|
||||
return load<u8>(changetype<usize>(this.buffer) + this.byteOffset + byteOffset, HEADER_SIZE);
|
||||
}
|
||||
|
||||
@inline
|
||||
getUint16(byteOffset: i32, littleEndian: boolean = false): u16 {
|
||||
var result: u16 = load<u16>(changetype<usize>(this.buffer) + this.byteOffset + byteOffset, HEADER_SIZE);
|
||||
return littleEndian ? result : bswap<u16>(result);
|
||||
}
|
||||
|
||||
@inline
|
||||
getUint32(byteOffset: i32, littleEndian: boolean = false): u32 {
|
||||
var result: u32 = load<u32>(changetype<usize>(this.buffer) + this.byteOffset + byteOffset, HEADER_SIZE);
|
||||
return littleEndian ? result : bswap<u32>(result);
|
||||
}
|
||||
|
||||
@inline
|
||||
setFloat32(byteOffset: i32, value: f32, littleEndian: boolean = false): void {
|
||||
var input: f32 = littleEndian ? value : reinterpret<f32>(bswap<u32>(reinterpret<u32>(value)));
|
||||
store<f32>(changetype<usize>(this.buffer) + this.byteOffset + byteOffset, input, HEADER_SIZE);
|
||||
}
|
||||
|
||||
@inline
|
||||
setFloat64(byteOffset: i32, value: f64, littleEndian: boolean = false): void {
|
||||
var input: f64 = littleEndian ? value : reinterpret<f64>(bswap<u64>(reinterpret<u64>(value)));
|
||||
store<f64>(changetype<usize>(this.buffer) + this.byteOffset + byteOffset, input, HEADER_SIZE);
|
||||
}
|
||||
|
||||
@inline
|
||||
setInt8(byteOffset: i32, value: i8): void {
|
||||
store<i8>(changetype<usize>(this.buffer) + this.byteOffset + byteOffset, value, HEADER_SIZE);
|
||||
}
|
||||
|
||||
@inline
|
||||
setInt16(byteOffset: i32, value: i16, littleEndian: boolean = false): void {
|
||||
store<i16>(changetype<usize>(this.buffer) + this.byteOffset + byteOffset, littleEndian ? value : bswap<i16>(value), HEADER_SIZE);
|
||||
}
|
||||
|
||||
@inline
|
||||
setInt32(byteOffset: i32, value: i32, littleEndian: boolean = false): void {
|
||||
store<i32>(changetype<usize>(this.buffer) + this.byteOffset + byteOffset, littleEndian ? value : bswap<i32>(value), HEADER_SIZE);
|
||||
}
|
||||
|
||||
@inline
|
||||
setUint8(byteOffset: i32, value: u8): void {
|
||||
store<u8>(changetype<usize>(this.buffer) + this.byteOffset + byteOffset, value, HEADER_SIZE);
|
||||
}
|
||||
|
||||
@inline
|
||||
setUint16(byteOffset: i32, value: u16, littleEndian: boolean = false): void {
|
||||
store<u16>(changetype<usize>(this.buffer) + this.byteOffset + byteOffset, littleEndian ? value : bswap<u16>(value), HEADER_SIZE);
|
||||
}
|
||||
|
||||
@inline
|
||||
setUint32(byteOffset: i32, value: u32, littleEndian: boolean = false): void {
|
||||
store<u32>(changetype<usize>(this.buffer) + this.byteOffset + byteOffset, littleEndian ? value : bswap<u32>(value), HEADER_SIZE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Non-standard additions that makes sense in WebAssembly, but won't work in JS
|
||||
*/
|
||||
|
||||
@inline
|
||||
getInt64(byteOffset: i32, littleEndian: boolean = false): i64 {
|
||||
var result: i64 = load<i64>(changetype<usize>(this.buffer) + this.byteOffset + byteOffset, HEADER_SIZE);
|
||||
return littleEndian ? result : bswap<i64>(result);
|
||||
}
|
||||
|
||||
@inline
|
||||
getUint64(byteOffset: i32, littleEndian: boolean = false): u64 {
|
||||
var result: u64 = load<u64>(changetype<usize>(this.buffer) + this.byteOffset + byteOffset, HEADER_SIZE);
|
||||
return littleEndian ? result : bswap<u64>(result);
|
||||
}
|
||||
|
||||
@inline
|
||||
setInt64(byteOffset: i32, value: i64, littleEndian: boolean = false): void {
|
||||
store<i64>(changetype<usize>(this.buffer) + this.byteOffset + byteOffset, littleEndian ? value : bswap<i64>(value), HEADER_SIZE);
|
||||
}
|
||||
|
||||
@inline
|
||||
setUint64(byteOffset: i32, value: u64, littleEndian: boolean = false): void {
|
||||
store<u64>(changetype<usize>(this.buffer) + this.byteOffset + byteOffset, littleEndian ? value : bswap<u64>(value), HEADER_SIZE);
|
||||
}
|
||||
}
|
52
std/assembly/index.d.ts
vendored
52
std/assembly/index.d.ts
vendored
@ -380,6 +380,58 @@ declare class ArrayBuffer {
|
||||
slice(begin?: i32, end?: i32): ArrayBuffer;
|
||||
}
|
||||
|
||||
/** The `DataView` view provides a low-level interface for reading and writing multiple number types in a binary `ArrayBuffer`, without having to care about the platform's endianness. */
|
||||
declare class DataView {
|
||||
/** The `buffer` accessor property represents the `ArrayBuffer` or `SharedArrayBuffer` referenced by the `DataView` at construction time. */
|
||||
readonly buffer: ArrayBuffer;
|
||||
/** The `byteLength` accessor property represents the length (in bytes) of this view from the start of its `ArrayBuffer` or `SharedArrayBuffer`. */
|
||||
readonly byteLength: i32;
|
||||
/** The `byteOffset` accessor property represents the offset (in bytes) of this view from the start of its `ArrayBuffer` or `SharedArrayBuffer`. */
|
||||
readonly byteOffset: i32;
|
||||
/** Constructs a new `DataView` with the given properties */
|
||||
constructor(buffer: ArrayBuffer, byteOffset?: i32, byteLength?: i32);
|
||||
/** The `getFloat32()` method gets a signed 32-bit float (float) at the specified byte offset from the start of the `DataView`. */
|
||||
getFloat32(byteOffset: i32, littleEndian?: boolean): f32
|
||||
/** The `getFloat64()` method gets a signed 64-bit float (double) at the specified byte offset from the start of the `DataView`. */
|
||||
getFloat64(byteOffset: i32, littleEndian?: boolean): f64
|
||||
/** The `getInt8()` method gets a signed 8-bit integer (byte) at the specified byte offset from the start of the `DataView`. */
|
||||
getInt8(byteOffset: i32): i8
|
||||
/** The `getInt16()` method gets a signed 16-bit integer (short) at the specified byte offset from the start of the `DataView`. */
|
||||
getInt16(byteOffset: i32, littleEndian?: boolean): i16
|
||||
/** The `getInt32()` method gets a signed 32-bit integer (long) at the specified byte offset from the start of the `DataView`. */
|
||||
getInt32(byteOffset: i32, littleEndian?: boolean): i32
|
||||
/** The `getInt64()` method gets a signed 64-bit integer (long long) at the specified byte offset from the start of the `DataView`. */
|
||||
getInt64(byteOffset: i32, littleEndian?: boolean): i64
|
||||
/** The `getUint8()` method gets an unsigned 8-bit integer (unsigned byte) at the specified byte offset from the start of the `DataView`. */
|
||||
getUint8(byteOffset: i32): u8
|
||||
/** The `getUint16()` method gets an unsigned 16-bit integer (unsigned short) at the specified byte offset from the start of the `DataView`. */
|
||||
getUint16(byteOffset: i32, littleEndian?: boolean): u16
|
||||
/** The `getUint32()` method gets an unsigned 32-bit integer (unsigned long) at the specified byte offset from the start of the `DataView`. */
|
||||
getUint32(byteOffset: i32, littleEndian?: boolean): u32
|
||||
/** The `getUint64()` method gets an unsigned 64-bit integer (unsigned long long) at the specified byte offset from the start of the `DataView`. */
|
||||
getUint64(byteOffset: i32, littleEndian?: boolean): u64
|
||||
/** The `setFloat32()` method stores a signed 32-bit float (float) value at the specified byte offset from the start of the `DataView`. */
|
||||
setFloat32(byteOffset: i32, value: f32, littleEndian?: boolean): void
|
||||
/** The `setFloat64()` method stores a signed 64-bit float (double) value at the specified byte offset from the start of the `DataView`. */
|
||||
setFloat64(byteOffset: i32, value: f64, littleEndian?: boolean): void
|
||||
/** The `setInt8()` method stores a signed 8-bit integer (byte) value at the specified byte offset from the start of the `DataView`. */
|
||||
setInt8(byteOffset: i32, value: i8): void
|
||||
/** The `setInt16()` method stores a signed 16-bit integer (short) value at the specified byte offset from the start of the `DataView`. */
|
||||
setInt16(byteOffset: i32, value: i16, littleEndian?: boolean): void
|
||||
/** The `setInt32()` method stores a signed 32-bit integer (long) value at the specified byte offset from the start of the `DataView`. */
|
||||
setInt32(byteOffset: i32, value: i32, littleEndian?: boolean): void
|
||||
/** The `setInt64()` method stores a signed 64-bit integer (long long) value at the specified byte offset from the start of the `DataView`. */
|
||||
setInt64(byteOffset: i32, value: i64, littleEndian?: boolean): void
|
||||
/** The `setUint8()` method stores an unsigned 8-bit integer (byte) value at the specified byte offset from the start of the `DataView`. */
|
||||
setUint8(byteOffset: i32, value: u8): void
|
||||
/** The `setUint16()` method stores an unsigned 16-bit integer (unsigned short) value at the specified byte offset from the start of the `DataView`. */
|
||||
setUint16(byteOffset: i32, value: u16, littleEndian?: boolean): void
|
||||
/** The `setUint32()` method stores an unsigned 32-bit integer (unsigned long) value at the specified byte offset from the start of the `DataView`. */
|
||||
setUint32(byteOffset: i32, value: u32, littleEndian?: boolean): void
|
||||
/** The `setUint64()` method stores an unsigned 64-bit integer (unsigned long long) value at the specified byte offset from the start of the `DataView`. */
|
||||
setUint64(byteOffset: i32, value: u64, littleEndian?: boolean): void
|
||||
}
|
||||
|
||||
/** Interface for a typed view on an array buffer. */
|
||||
interface ArrayBufferView<T> {
|
||||
[key: number]: T;
|
||||
|
Reference in New Issue
Block a user