mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-22 19:21:47 +00:00
Add ArrayBuffer.isView and rework Array.isArray (#431)
This commit is contained in:
@ -4,13 +4,44 @@ import {
|
||||
allocateUnsafe
|
||||
} from "./internal/arraybuffer";
|
||||
|
||||
import {
|
||||
Uint8ClampedArray,
|
||||
Uint8Array,
|
||||
Int8Array,
|
||||
Uint16Array,
|
||||
Int16Array,
|
||||
Uint32Array,
|
||||
Int32Array,
|
||||
Uint64Array,
|
||||
Int64Array
|
||||
} from "./typedarray";
|
||||
|
||||
import {
|
||||
DataView
|
||||
} from "./dataview";
|
||||
|
||||
@sealed
|
||||
export class ArrayBuffer {
|
||||
|
||||
readonly byteLength: i32; // capped to [0, MAX_LENGTH]
|
||||
|
||||
@inline static isView<T>(value: T): bool {
|
||||
if (value === null) return false;
|
||||
if (value instanceof Uint8ClampedArray) return true;
|
||||
if (value instanceof Uint8Array) return true;
|
||||
if (value instanceof Int8Array) return true;
|
||||
if (value instanceof Uint16Array) return true;
|
||||
if (value instanceof Int16Array) return true;
|
||||
if (value instanceof Uint32Array) return true;
|
||||
if (value instanceof Int32Array) return true;
|
||||
if (value instanceof Uint64Array) return true;
|
||||
if (value instanceof Int64Array) return true;
|
||||
if (value instanceof DataView) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// @unsafe
|
||||
get data(): usize { return changetype<usize>(this) + HEADER_SIZE; }
|
||||
@inline get data(): usize { return changetype<usize>(this) + HEADER_SIZE; }
|
||||
|
||||
constructor(length: i32, unsafe: bool = false) {
|
||||
if (<u32>length > <u32>MAX_BLENGTH) throw new RangeError("Invalid array buffer length");
|
||||
|
4
std/assembly/index.d.ts
vendored
4
std/assembly/index.d.ts
vendored
@ -493,10 +493,14 @@ declare class ArrayBuffer {
|
||||
readonly byteLength: i32;
|
||||
/** Unsafe pointer to the start of the data in memory. */
|
||||
readonly data: usize;
|
||||
/** Returns true if value is one of the ArrayBuffer views, such as typed array or a DataView **/
|
||||
static isView<T>(value: T): bool;
|
||||
/** Constructs a new array buffer of the given length in bytes. */
|
||||
constructor(length: i32, unsafe?: bool);
|
||||
/** Returns a copy of this array buffer's bytes from begin, inclusive, up to end, exclusive. */
|
||||
slice(begin?: i32, end?: i32): ArrayBuffer;
|
||||
/** Returns a string representation of ArrayBuffer. */
|
||||
toString(): string;
|
||||
}
|
||||
|
||||
/** 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. */
|
||||
|
29
std/portable/index.d.ts
vendored
29
std/portable/index.d.ts
vendored
@ -16,6 +16,7 @@
|
||||
|
||||
// Types
|
||||
|
||||
declare type bool = boolean;
|
||||
declare type i8 = number;
|
||||
declare type i16 = number;
|
||||
declare type i32 = number;
|
||||
@ -23,7 +24,6 @@ declare type isize = number;
|
||||
declare type u8 = number;
|
||||
declare type u16 = number;
|
||||
declare type u32 = number;
|
||||
declare type bool = boolean;
|
||||
declare type usize = number;
|
||||
declare type f32 = number;
|
||||
declare type f64 = number;
|
||||
@ -32,6 +32,20 @@ declare type f64 = number;
|
||||
|
||||
/** Compiler target. 0 = JS, 1 = WASM32, 2 = WASM64. */
|
||||
declare const ASC_TARGET: i32;
|
||||
/** Provided noTreeshaking option. */
|
||||
declare const ASC_NO_TREESHAKING: bool;
|
||||
/** Provided noAssert option. */
|
||||
declare const ASC_NO_ASSERT: bool;
|
||||
/** Provided memoryBase option. */
|
||||
declare const ASC_MEMORY_BASE: i32;
|
||||
/** Provided optimizeLevel option. */
|
||||
declare const ASC_OPTIMIZE_LEVEL: i32;
|
||||
/** Provided shrinkLevel option. */
|
||||
declare const ASC_SHRINK_LEVEL: i32;
|
||||
/** Whether the mutable global feature is enabled. */
|
||||
declare const ASC_FEATURE_MUTABLE_GLOBAL: bool;
|
||||
/** Whether the sign extension feature is enabled. */
|
||||
declare const ASC_FEATURE_SIGN_EXTENSION: bool;
|
||||
|
||||
// Builtins
|
||||
|
||||
@ -299,6 +313,8 @@ declare namespace memory {
|
||||
declare class ArrayBuffer {
|
||||
/** The size, in bytes, of the array. */
|
||||
readonly byteLength: i32;
|
||||
/** Returns true if value is one of the ArrayBuffer views, such as typed array or a DataView **/
|
||||
static isView<T>(value: T): bool;
|
||||
/** 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. */
|
||||
@ -397,6 +413,17 @@ declare class Int32Array extends Array<i32> {}
|
||||
declare class Float32Array extends Array<f32> {}
|
||||
declare class Float64Array extends Array<f64> {}
|
||||
|
||||
/** Interface for a typed view on an array buffer. */
|
||||
interface ArrayBufferView<T> {
|
||||
[key: number]: T;
|
||||
/** The {@link ArrayBuffer} referenced by this view. */
|
||||
readonly buffer: ArrayBuffer;
|
||||
/** The offset in bytes from the start of the referenced {@link ArrayBuffer}. */
|
||||
readonly byteOffset: i32;
|
||||
/** The length in bytes from the start of the referenced {@link ArrayBuffer}. */
|
||||
readonly byteLength: i32;
|
||||
}
|
||||
|
||||
declare class String {
|
||||
|
||||
static fromCharCode(ls: i32, hs?: i32): string;
|
||||
|
@ -2,7 +2,14 @@
|
||||
|
||||
var globalScope = typeof window !== "undefined" && window || typeof global !== "undefined" && global || self;
|
||||
|
||||
globalScope.ASC_TARGET = 0;
|
||||
globalScope.ASC_TARGET = 0; // JS
|
||||
globalScope.ASC_NO_TREESHAKING = false;
|
||||
globalScope.ASC_NO_ASSERT = false;
|
||||
globalScope.ASC_MEMORY_BASE = 0;
|
||||
globalScope.ASC_OPTIMIZE_LEVEL = 3;
|
||||
globalScope.ASC_SHRINK_LEVEL = 0;
|
||||
globalScope.ASC_FEATURE_MUTABLE_GLOBAL = false;
|
||||
globalScope.ASC_FEATURE_SIGN_EXTENSION = false;
|
||||
|
||||
var F64 = new Float64Array(1);
|
||||
var U64 = new Uint32Array(F64.buffer);
|
||||
@ -197,7 +204,7 @@ globalScope["isFloat"] = function isFloat(arg) {
|
||||
return typeof arg === "number";
|
||||
};
|
||||
|
||||
globalScope["isReference"] = function isClass(arg) {
|
||||
globalScope["isReference"] = function isReference(arg) {
|
||||
return typeof arg === "object" || typeof arg === "string";
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user