mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-12 14:31:28 +00:00
Initial ArrayBuffer implementation; Conditional allocation within constructors; Explicit constructor return values
This commit is contained in:
12
std/assembly.d.ts
vendored
12
std/assembly.d.ts
vendored
@ -248,7 +248,17 @@ declare function parseI64(str: string, radix?: i32): i64;
|
||||
/** Parses a string to a 64-bit float. */
|
||||
declare function parseFloat(str: string): f64;
|
||||
|
||||
// Standard library (not yet implemented)
|
||||
// Standard library
|
||||
|
||||
/** Class representing a generic, fixed-length raw binary data buffer. */
|
||||
declare class ArrayBuffer {
|
||||
/** The size, in bytes, of the array. */
|
||||
readonly byteLength: i32;
|
||||
/** Constructs a new array buffer of the given length in bytes. */
|
||||
constructor(length: i32);
|
||||
/** Returns a copy of this array buffer's bytes from begin, inclusive, up to end, exclusive. */
|
||||
slice(begin?: i32, end?: i32): ArrayBuffer;
|
||||
}
|
||||
|
||||
/** Class representing a sequence of values of type `T`. */
|
||||
declare class Array<T> {
|
||||
|
29
std/assembly/arraybuffer.ts
Normal file
29
std/assembly/arraybuffer.ts
Normal file
@ -0,0 +1,29 @@
|
||||
const HEADER_SIZE: usize = sizeof<i32>();
|
||||
|
||||
@unmanaged
|
||||
export class ArrayBuffer {
|
||||
|
||||
readonly byteLength: i32;
|
||||
|
||||
constructor(length: i32) {
|
||||
if (<u32>length > 0x7fffffff) {
|
||||
throw new RangeError("Invalid array buffer length");
|
||||
}
|
||||
var buffer = allocate_memory(HEADER_SIZE + <usize>length);
|
||||
store<i32>(buffer, length);
|
||||
return changetype<ArrayBuffer>(buffer);
|
||||
}
|
||||
|
||||
slice(begin: i32 = 0, end: i32 = 0x7fffffff): 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 = allocate_memory(HEADER_SIZE + <usize>newLen);
|
||||
store<i32>(buffer, newLen);
|
||||
move_memory(buffer + HEADER_SIZE, changetype<usize>(this) + HEADER_SIZE + begin, newLen);
|
||||
return changetype<ArrayBuffer>(buffer);
|
||||
}
|
||||
}
|
10
std/portable.d.ts
vendored
10
std/portable.d.ts
vendored
@ -200,6 +200,16 @@ declare function parseFloat(str: string): f64;
|
||||
declare const NaN: f32 | f64;
|
||||
declare const Infinity: f32 | f64;
|
||||
|
||||
/** Class representing a generic, fixed-length raw binary data buffer. */
|
||||
declare class ArrayBuffer {
|
||||
/** The size, in bytes, of the array. */
|
||||
readonly byteLength: i32;
|
||||
/** Constructs a new array buffer of the given length in bytes. */
|
||||
constructor(length: i32);
|
||||
/** Returns a copy of this array buffer's bytes from begin, inclusive, up to end, exclusive. */
|
||||
slice(begin?: i32, end?: i32): ArrayBuffer;
|
||||
}
|
||||
|
||||
declare class Array<T> {
|
||||
[key: number]: T;
|
||||
length: i32;
|
||||
|
Reference in New Issue
Block a user