forked from AssemblyScript/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
25 lines (22 loc) · 844 Bytes
/
index.ts
File metadata and controls
25 lines (22 loc) · 844 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { BLOCK_MAXSIZE } from "rt/common";
import { E_INVALIDLENGTH, E_INDEXOUTOFRANGE } from "util/error";
import { Uint8Array } from "typedarray";
export class Buffer extends Uint8Array {
constructor(size: i32) {
super(size);
}
public static alloc(size: i32): Buffer {
return new Buffer(size);
}
@unsafe public static allocUnsafe(size: i32): Buffer {
// Node throws an error if size is less than 0
if (u32(size) > BLOCK_MAXSIZE) throw new RangeError(E_INVALIDLENGTH);
let buffer = __alloc(size, idof<ArrayBuffer>());
// This retains the pointer to the result Buffer.
let result = changetype<Buffer>(__alloc(offsetof<Buffer>(), idof<Buffer>()));
result.data = changetype<ArrayBuffer>(buffer);
result.dataStart = changetype<usize>(buffer);
result.dataLength = size;
return result;
}
}