mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-22 03:01:55 +00:00
Add ArrayBuffer/DataView/Symbol#toString and improve Errors (#332)
This commit is contained in:
@ -30,4 +30,8 @@ export class ArrayBuffer {
|
||||
memory.copy(changetype<usize>(buffer) + HEADER_SIZE, changetype<usize>(this) + HEADER_SIZE + begin, newLen);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return "[object ArrayBuffer]";
|
||||
}
|
||||
}
|
||||
|
@ -177,6 +177,10 @@ export class DataView {
|
||||
HEADER_SIZE
|
||||
);
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return "[object DataView]";
|
||||
}
|
||||
}
|
||||
|
||||
@inline function checkOffset(byteOffset: i32, n: i32, byteLength: i32): void {
|
||||
|
@ -1,12 +1,11 @@
|
||||
export class Error {
|
||||
|
||||
name: string = "Error";
|
||||
message: string;
|
||||
name: string = "Error";
|
||||
stack: string = ""; // TODO
|
||||
|
||||
constructor(message: string = "") {
|
||||
this.message = message;
|
||||
}
|
||||
constructor(
|
||||
public message: string = ""
|
||||
) {}
|
||||
|
||||
toString(): string {
|
||||
var message = this.message;
|
||||
@ -29,3 +28,10 @@ export class TypeError extends Error {
|
||||
this.name = "TypeError";
|
||||
}
|
||||
}
|
||||
|
||||
export class SyntaxError extends Error {
|
||||
constructor(message: string = "") {
|
||||
super(message);
|
||||
this.name = "SyntaxError";
|
||||
}
|
||||
}
|
||||
|
68
std/assembly/index.d.ts
vendored
68
std/assembly/index.d.ts
vendored
@ -447,45 +447,47 @@ declare class DataView {
|
||||
/** Constructs a new `DataView` with the given properties */
|
||||
constructor(buffer: ArrayBuffer, byteOffset?: i32, byteLength?: i32);
|
||||
/** The `getFloat32()` method gets a signed 32-bit float (float) at the specified byte offset from the start of the `DataView`. */
|
||||
getFloat32(byteOffset: i32, littleEndian?: boolean): f32
|
||||
getFloat32(byteOffset: i32, littleEndian?: boolean): f32;
|
||||
/** The `getFloat64()` method gets a signed 64-bit float (double) at the specified byte offset from the start of the `DataView`. */
|
||||
getFloat64(byteOffset: i32, littleEndian?: boolean): f64
|
||||
getFloat64(byteOffset: i32, littleEndian?: boolean): f64;
|
||||
/** The `getInt8()` method gets a signed 8-bit integer (byte) at the specified byte offset from the start of the `DataView`. */
|
||||
getInt8(byteOffset: i32): i8
|
||||
getInt8(byteOffset: i32): i8;
|
||||
/** The `getInt16()` method gets a signed 16-bit integer (short) at the specified byte offset from the start of the `DataView`. */
|
||||
getInt16(byteOffset: i32, littleEndian?: boolean): i16
|
||||
getInt16(byteOffset: i32, littleEndian?: boolean): i16;
|
||||
/** The `getInt32()` method gets a signed 32-bit integer (long) at the specified byte offset from the start of the `DataView`. */
|
||||
getInt32(byteOffset: i32, littleEndian?: boolean): i32
|
||||
getInt32(byteOffset: i32, littleEndian?: boolean): i32;
|
||||
/** The `getInt64()` method gets a signed 64-bit integer (long long) at the specified byte offset from the start of the `DataView`. */
|
||||
getInt64(byteOffset: i32, littleEndian?: boolean): i64
|
||||
getInt64(byteOffset: i32, littleEndian?: boolean): i64;
|
||||
/** The `getUint8()` method gets an unsigned 8-bit integer (unsigned byte) at the specified byte offset from the start of the `DataView`. */
|
||||
getUint8(byteOffset: i32): u8
|
||||
getUint8(byteOffset: i32): u8;
|
||||
/** The `getUint16()` method gets an unsigned 16-bit integer (unsigned short) at the specified byte offset from the start of the `DataView`. */
|
||||
getUint16(byteOffset: i32, littleEndian?: boolean): u16
|
||||
getUint16(byteOffset: i32, littleEndian?: boolean): u16;
|
||||
/** The `getUint32()` method gets an unsigned 32-bit integer (unsigned long) at the specified byte offset from the start of the `DataView`. */
|
||||
getUint32(byteOffset: i32, littleEndian?: boolean): u32
|
||||
getUint32(byteOffset: i32, littleEndian?: boolean): u32;
|
||||
/** The `getUint64()` method gets an unsigned 64-bit integer (unsigned long long) at the specified byte offset from the start of the `DataView`. */
|
||||
getUint64(byteOffset: i32, littleEndian?: boolean): u64
|
||||
getUint64(byteOffset: i32, littleEndian?: boolean): u64;
|
||||
/** The `setFloat32()` method stores a signed 32-bit float (float) value at the specified byte offset from the start of the `DataView`. */
|
||||
setFloat32(byteOffset: i32, value: f32, littleEndian?: boolean): void
|
||||
setFloat32(byteOffset: i32, value: f32, littleEndian?: boolean): void;
|
||||
/** The `setFloat64()` method stores a signed 64-bit float (double) value at the specified byte offset from the start of the `DataView`. */
|
||||
setFloat64(byteOffset: i32, value: f64, littleEndian?: boolean): void
|
||||
setFloat64(byteOffset: i32, value: f64, littleEndian?: boolean): void;
|
||||
/** The `setInt8()` method stores a signed 8-bit integer (byte) value at the specified byte offset from the start of the `DataView`. */
|
||||
setInt8(byteOffset: i32, value: i8): void
|
||||
setInt8(byteOffset: i32, value: i8): void;
|
||||
/** The `setInt16()` method stores a signed 16-bit integer (short) value at the specified byte offset from the start of the `DataView`. */
|
||||
setInt16(byteOffset: i32, value: i16, littleEndian?: boolean): void
|
||||
setInt16(byteOffset: i32, value: i16, littleEndian?: boolean): void;
|
||||
/** The `setInt32()` method stores a signed 32-bit integer (long) value at the specified byte offset from the start of the `DataView`. */
|
||||
setInt32(byteOffset: i32, value: i32, littleEndian?: boolean): void
|
||||
setInt32(byteOffset: i32, value: i32, littleEndian?: boolean): void;
|
||||
/** The `setInt64()` method stores a signed 64-bit integer (long long) value at the specified byte offset from the start of the `DataView`. */
|
||||
setInt64(byteOffset: i32, value: i64, littleEndian?: boolean): void
|
||||
setInt64(byteOffset: i32, value: i64, littleEndian?: boolean): void;
|
||||
/** The `setUint8()` method stores an unsigned 8-bit integer (byte) value at the specified byte offset from the start of the `DataView`. */
|
||||
setUint8(byteOffset: i32, value: u8): void
|
||||
setUint8(byteOffset: i32, value: u8): void;
|
||||
/** The `setUint16()` method stores an unsigned 16-bit integer (unsigned short) value at the specified byte offset from the start of the `DataView`. */
|
||||
setUint16(byteOffset: i32, value: u16, littleEndian?: boolean): void
|
||||
setUint16(byteOffset: i32, value: u16, littleEndian?: boolean): void;
|
||||
/** The `setUint32()` method stores an unsigned 32-bit integer (unsigned long) value at the specified byte offset from the start of the `DataView`. */
|
||||
setUint32(byteOffset: i32, value: u32, littleEndian?: boolean): void
|
||||
setUint32(byteOffset: i32, value: u32, littleEndian?: boolean): void;
|
||||
/** The `setUint64()` method stores an unsigned 64-bit integer (unsigned long long) value at the specified byte offset from the start of the `DataView`. */
|
||||
setUint64(byteOffset: i32, value: u64, littleEndian?: boolean): void
|
||||
setUint64(byteOffset: i32, value: u64, littleEndian?: boolean): void;
|
||||
/** Returns a string representation of DataView. */
|
||||
toString(): string;
|
||||
}
|
||||
|
||||
/** Interface for a typed view on an array buffer. */
|
||||
@ -621,15 +623,24 @@ declare class Error {
|
||||
message: string;
|
||||
|
||||
/** Stack trace. */
|
||||
stack: string;
|
||||
stack?: string;
|
||||
|
||||
/** Constructs a new error, optionally with a message. */
|
||||
constructor(message?: string);
|
||||
|
||||
/** Method returns a string representing the specified Error class. */
|
||||
toString(): string;
|
||||
}
|
||||
|
||||
/** Class for indicating an error when a value is not in the set or range of allowed values. */
|
||||
declare class RangeError extends Error { }
|
||||
|
||||
/** Class for indicating an error when a value is not of the expected type. */
|
||||
declare class TypeError extends Error { }
|
||||
|
||||
/** Class for indicating an error when trying to interpret syntactically invalid code. */
|
||||
declare class SyntaxError extends Error { }
|
||||
|
||||
interface Boolean {}
|
||||
interface Function {}
|
||||
interface IArguments {}
|
||||
@ -644,6 +655,7 @@ declare class Map<K,V> {
|
||||
get(key: K): V;
|
||||
delete(key: K): bool;
|
||||
clear(): void;
|
||||
toString(): string;
|
||||
}
|
||||
|
||||
declare class Set<T> {
|
||||
@ -652,13 +664,27 @@ declare class Set<T> {
|
||||
add(value: T): void;
|
||||
delete(value: T): bool;
|
||||
clear(): void;
|
||||
toString(): string;
|
||||
}
|
||||
|
||||
interface SymbolConstructor {
|
||||
readonly hasInstance: symbol;
|
||||
readonly isConcatSpreadable: symbol;
|
||||
readonly isRegExp: symbol;
|
||||
readonly iterator: symbol;
|
||||
readonly match: symbol;
|
||||
readonly replace: symbol;
|
||||
readonly search: symbol;
|
||||
readonly species: symbol;
|
||||
readonly split: symbol;
|
||||
readonly toPrimitive: symbol;
|
||||
readonly toStringTag: symbol;
|
||||
readonly unscopables: symbol;
|
||||
(description?: string | null): symbol;
|
||||
for(key: string): symbol;
|
||||
keyFor(sym: symbol): string | null;
|
||||
}
|
||||
|
||||
declare const Symbol: SymbolConstructor;
|
||||
|
||||
interface IMath<T> {
|
||||
|
@ -165,6 +165,10 @@ export class Map<K,V> {
|
||||
this.entriesOffset = this.entriesCount;
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return "[object Map]";
|
||||
}
|
||||
|
||||
private __gc(): void {
|
||||
__gc_mark(changetype<usize>(this.buckets)); // tslint:disable-line
|
||||
var entries = this.entries;
|
||||
|
@ -153,6 +153,10 @@ export class Set<K> {
|
||||
this.entriesOffset = this.entriesCount;
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return "[object Set]";
|
||||
}
|
||||
|
||||
private __gc(): void {
|
||||
__gc_mark(changetype<usize>(this.buckets)); // tslint:disable-line
|
||||
var entries = this.entries;
|
||||
|
@ -4,7 +4,30 @@ var stringToId: Map<string, usize>;
|
||||
var idToString: Map<usize, string>;
|
||||
var nextId: usize = 12; // Symbol.unscopables + 1
|
||||
|
||||
@unmanaged export class symbol {}
|
||||
@unmanaged export class symbol {
|
||||
toString(): string {
|
||||
var id = changetype<usize>(this);
|
||||
var str = "";
|
||||
switch (id) {
|
||||
case 1: { str = "hasInstance"; break; }
|
||||
case 2: { str = "isConcatSpreadable"; break; }
|
||||
case 3: { str = "isRegExp"; break; }
|
||||
case 4: { str = "match"; break; }
|
||||
case 5: { str = "replace"; break; }
|
||||
case 6: { str = "search"; break; }
|
||||
case 7: { str = "species"; break; }
|
||||
case 8: { str = "split"; break; }
|
||||
case 9: { str = "toPrimitive"; break; }
|
||||
case 10: { str = "toStringTag"; break; }
|
||||
case 11: { str = "unscopables"; break; }
|
||||
default: {
|
||||
if (idToString !== null && idToString.has(id)) str = idToString.get(id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return "Symbol(" + str + ")";
|
||||
}
|
||||
}
|
||||
|
||||
type Symbol = symbol;
|
||||
|
||||
@ -17,18 +40,18 @@ export function Symbol(description: string | null = null): symbol {
|
||||
export namespace Symbol {
|
||||
|
||||
// well-known symbols
|
||||
export const hasInstance = changetype<symbol>(1);
|
||||
export const concatSpreadable = changetype<symbol>(2);
|
||||
export const isRegExp = changetype<symbol>(3);
|
||||
export const iterator = changetype<symbol>(3);
|
||||
export const match = changetype<symbol>(4);
|
||||
export const replace = changetype<symbol>(5);
|
||||
export const search = changetype<symbol>(6);
|
||||
export const species = changetype<symbol>(7);
|
||||
export const split = changetype<symbol>(8);
|
||||
export const toPrimitive = changetype<symbol>(9);
|
||||
export const toStringTag = changetype<symbol>(10);
|
||||
export const unscopables = changetype<symbol>(11);
|
||||
export const hasInstance = changetype<symbol>(1);
|
||||
export const isConcatSpreadable = changetype<symbol>(2);
|
||||
export const isRegExp = changetype<symbol>(3);
|
||||
export const iterator = changetype<symbol>(3);
|
||||
export const match = changetype<symbol>(4);
|
||||
export const replace = changetype<symbol>(5);
|
||||
export const search = changetype<symbol>(6);
|
||||
export const species = changetype<symbol>(7);
|
||||
export const split = changetype<symbol>(8);
|
||||
export const toPrimitive = changetype<symbol>(9);
|
||||
export const toStringTag = changetype<symbol>(10);
|
||||
export const unscopables = changetype<symbol>(11);
|
||||
|
||||
/* tslint:disable */// not valid TS
|
||||
export function for(key: string): symbol {
|
||||
|
77
std/portable/index.d.ts
vendored
77
std/portable/index.d.ts
vendored
@ -297,6 +297,8 @@ declare class ArrayBuffer {
|
||||
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;
|
||||
/** 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. */
|
||||
@ -310,37 +312,39 @@ declare class DataView {
|
||||
/** Constructs a new `DataView` with the given properties */
|
||||
constructor(buffer: ArrayBuffer, byteOffset?: i32, byteLength?: i32);
|
||||
/** The `getFloat32()` method gets a signed 32-bit float (float) at the specified byte offset from the start of the `DataView`. */
|
||||
getFloat32(byteOffset: i32, littleEndian?: boolean): f32
|
||||
getFloat32(byteOffset: i32, littleEndian?: boolean): f32;
|
||||
/** The `getFloat64()` method gets a signed 64-bit float (double) at the specified byte offset from the start of the `DataView`. */
|
||||
getFloat64(byteOffset: i32, littleEndian?: boolean): f64
|
||||
getFloat64(byteOffset: i32, littleEndian?: boolean): f64;
|
||||
/** The `getInt8()` method gets a signed 8-bit integer (byte) at the specified byte offset from the start of the `DataView`. */
|
||||
getInt8(byteOffset: i32): i8
|
||||
getInt8(byteOffset: i32): i8;
|
||||
/** The `getInt16()` method gets a signed 16-bit integer (short) at the specified byte offset from the start of the `DataView`. */
|
||||
getInt16(byteOffset: i32, littleEndian?: boolean): i16
|
||||
getInt16(byteOffset: i32, littleEndian?: boolean): i16;
|
||||
/** The `getInt32()` method gets a signed 32-bit integer (long) at the specified byte offset from the start of the `DataView`. */
|
||||
getInt32(byteOffset: i32, littleEndian?: boolean): i32
|
||||
getInt32(byteOffset: i32, littleEndian?: boolean): i32;
|
||||
/** The `getUint8()` method gets an unsigned 8-bit integer (unsigned byte) at the specified byte offset from the start of the `DataView`. */
|
||||
getUint8(byteOffset: i32): u8
|
||||
getUint8(byteOffset: i32): u8;
|
||||
/** The `getUint16()` method gets an unsigned 16-bit integer (unsigned short) at the specified byte offset from the start of the `DataView`. */
|
||||
getUint16(byteOffset: i32, littleEndian?: boolean): u16
|
||||
getUint16(byteOffset: i32, littleEndian?: boolean): u16;
|
||||
/** The `getUint32()` method gets an unsigned 32-bit integer (unsigned long) at the specified byte offset from the start of the `DataView`. */
|
||||
getUint32(byteOffset: i32, littleEndian?: boolean): u32
|
||||
getUint32(byteOffset: i32, littleEndian?: boolean): u32;
|
||||
/** The `setFloat32()` method stores a signed 32-bit float (float) value at the specified byte offset from the start of the `DataView`. */
|
||||
setFloat32(byteOffset: i32, value: f32, littleEndian?: boolean): void
|
||||
setFloat32(byteOffset: i32, value: f32, littleEndian?: boolean): void;
|
||||
/** The `setFloat64()` method stores a signed 64-bit float (double) value at the specified byte offset from the start of the `DataView`. */
|
||||
setFloat64(byteOffset: i32, value: f64, littleEndian?: boolean): void
|
||||
setFloat64(byteOffset: i32, value: f64, littleEndian?: boolean): void;
|
||||
/** The `setInt8()` method stores a signed 8-bit integer (byte) value at the specified byte offset from the start of the `DataView`. */
|
||||
setInt8(byteOffset: i32, value: i8): void
|
||||
setInt8(byteOffset: i32, value: i8): void;
|
||||
/** The `setInt16()` method stores a signed 16-bit integer (short) value at the specified byte offset from the start of the `DataView`. */
|
||||
setInt16(byteOffset: i32, value: i16, littleEndian?: boolean): void
|
||||
setInt16(byteOffset: i32, value: i16, littleEndian?: boolean): void;
|
||||
/** The `setInt32()` method stores a signed 32-bit integer (long) value at the specified byte offset from the start of the `DataView`. */
|
||||
setInt32(byteOffset: i32, value: i32, littleEndian?: boolean): void
|
||||
setInt32(byteOffset: i32, value: i32, littleEndian?: boolean): void;
|
||||
/** The `setUint8()` method stores an unsigned 8-bit integer (byte) value at the specified byte offset from the start of the `DataView`. */
|
||||
setUint8(byteOffset: i32, value: u8): void
|
||||
setUint8(byteOffset: i32, value: u8): void;
|
||||
/** The `setUint16()` method stores an unsigned 16-bit integer (unsigned short) value at the specified byte offset from the start of the `DataView`. */
|
||||
setUint16(byteOffset: i32, value: u16, littleEndian?: boolean): void
|
||||
setUint16(byteOffset: i32, value: u16, littleEndian?: boolean): void;
|
||||
/** The `setUint32()` method stores an unsigned 32-bit integer (unsigned long) value at the specified byte offset from the start of the `DataView`. */
|
||||
setUint32(byteOffset: i32, value: u32, littleEndian?: boolean): void
|
||||
setUint32(byteOffset: i32, value: u32, littleEndian?: boolean): void;
|
||||
/** Returns a string representation of DataView. */
|
||||
toString(): string;
|
||||
}
|
||||
|
||||
declare class Array<T> {
|
||||
@ -435,12 +439,34 @@ interface RegExp {}
|
||||
|
||||
interface IArguments {}
|
||||
|
||||
/** Class for representing a runtime error. Base class of all errors. */
|
||||
declare class Error {
|
||||
constructor(message: string);
|
||||
|
||||
/** Error name. */
|
||||
name: string;
|
||||
|
||||
/** Message provided on construction. */
|
||||
message: string;
|
||||
stack: string | null;
|
||||
|
||||
/** Stack trace. */
|
||||
stack?: string;
|
||||
|
||||
/** Constructs a new error, optionally with a message. */
|
||||
constructor(message?: string);
|
||||
|
||||
/** Method returns a string representing the specified Error class. */
|
||||
toString(): string;
|
||||
}
|
||||
|
||||
/** Class for indicating an error when a value is not in the set or range of allowed values. */
|
||||
declare class RangeError extends Error { }
|
||||
|
||||
/** Class for indicating an error when a value is not of the expected type. */
|
||||
declare class TypeError extends Error { }
|
||||
|
||||
/** Class for indicating an error when trying to interpret syntactically invalid code. */
|
||||
declare class SyntaxError extends Error { }
|
||||
|
||||
declare class Set<T> {
|
||||
constructor(entries?: T[]);
|
||||
readonly size: i32;
|
||||
@ -448,6 +474,7 @@ declare class Set<T> {
|
||||
add(value: T): void;
|
||||
delete(value: T): bool;
|
||||
clear(): void;
|
||||
toString(): string;
|
||||
[Symbol.iterator](): Iterator<T>;
|
||||
}
|
||||
|
||||
@ -462,14 +489,26 @@ declare class Map<K,V> {
|
||||
keys(): Iterable<K>;
|
||||
values(): Iterable<V>;
|
||||
delete(key: K): bool;
|
||||
toString(): string;
|
||||
[Symbol.iterator](): Iterator<[K,V]>;
|
||||
}
|
||||
|
||||
interface SymbolConstructor {
|
||||
readonly hasInstance: symbol;
|
||||
readonly isConcatSpreadable: symbol;
|
||||
readonly isRegExp: symbol;
|
||||
readonly iterator: symbol;
|
||||
readonly match: symbol;
|
||||
readonly replace: symbol;
|
||||
readonly search: symbol;
|
||||
readonly species: symbol;
|
||||
readonly split: symbol;
|
||||
readonly toPrimitive: symbol;
|
||||
readonly toStringTag: symbol;
|
||||
readonly unscopables: symbol;
|
||||
(description?: string | null): symbol;
|
||||
for(key: string): symbol;
|
||||
keyFor(sym: symbol): string | null;
|
||||
readonly iterator: symbol;
|
||||
}
|
||||
declare const Symbol: SymbolConstructor;
|
||||
|
||||
|
Reference in New Issue
Block a user