Files
assemblyscript/std/assembly/arraybuffer.ts
Daniel Wirtz 39b489bee2 Rename memory instructions; Rework constant handling (#177)
* Rename memory instructions as proposed by the bulk-memory-operations spec.
* Rename memory manager functions to memory.* as well
* Remove automatic inlining of constant globals (Binaryen does this now)
* Improve 'const' enum compatibility
* Improve module-level export generation
* Enable the inline decorator for constant variables
* Add ERROR, WARNING and INFO macros that emit a user-defined diagnostic
* Reintroduce builtin decorator so these can appear anywhere in stdlib again
* Inline isNaN and isFinite by default
* Make an interface around gc.* similar to memory.*
* Emit an error when trying to inline a mutable variable
* Slim down CI stages
* Add a more convenient tracing utility for debugging
* Implement some prequesites for an eventual bundled GC
2018-07-20 22:53:33 +02:00

31 lines
938 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, unsafe: bool = false) {
if (<u32>length > <u32>MAX_BLENGTH) throw new RangeError("Invalid array buffer length");
var buffer = allocUnsafe(length);
if (!unsafe) memory.fill(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);
memory.copy(changetype<usize>(buffer) + HEADER_SIZE, changetype<usize>(this) + HEADER_SIZE + begin, newLen);
return buffer;
}
}