mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-04-26 07:22:21 +00:00
The `unchecked` builtin should be handled with ultimate care and it would be a lot better if there'd be a mechanism doing this automatically.
31 lines
902 B
TypeScript
31 lines
902 B
TypeScript
import {
|
|
HEADER_SIZE,
|
|
MAX_BLENGTH,
|
|
allocUnsafe
|
|
} from "./internal/arraybuffer";
|
|
|
|
@sealed
|
|
export class ArrayBuffer {
|
|
|
|
readonly byteLength: i32; // capped to [0, MAX_LENGTH]
|
|
|
|
constructor(length: i32) {
|
|
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);
|
|
return buffer;
|
|
}
|
|
|
|
slice(begin: i32 = 0, end: i32 = MAX_BLENGTH): ArrayBuffer {
|
|
var len = this.byteLength;
|
|
if (begin < 0) begin = max(len + begin, 0);
|
|
else begin = min(begin, len);
|
|
if (end < 0) end = max(len + end, 0);
|
|
else end = min(end, len);
|
|
var newLen = max(end - begin, 0);
|
|
var buffer = allocUnsafe(newLen);
|
|
move_memory(changetype<usize>(buffer) + HEADER_SIZE, changetype<usize>(this) + HEADER_SIZE + begin, newLen);
|
|
return buffer;
|
|
}
|
|
}
|