Skip to content

Add ArrayBuffer/DataView/Symbol/#toString. Improve Errors #332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Nov 18, 2018
4 changes: 4 additions & 0 deletions std/assembly/arraybuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]";
}
}
4 changes: 4 additions & 0 deletions std/assembly/dataview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ export class DataView {
HEADER_SIZE
);
}

toString(): string {
return "[object DataView]";
}
}

@inline function checkOffset(byteOffset: i32, n: i32, byteLength: i32): void {
Expand Down
7 changes: 3 additions & 4 deletions std/assembly/error.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
export class Error {

name: string = "Error";
message: string;
stack: string = ""; // TODO

constructor(message: string = "") {
this.message = message;
}
constructor(
public message: string = ""
) {}

toString(): string {
var message = this.message;
Expand Down
65 changes: 44 additions & 21 deletions std/assembly/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -621,15 +623,21 @@ 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 { }

interface Boolean {}
interface Function {}
interface IArguments {}
Expand All @@ -644,6 +652,7 @@ declare class Map<K,V> {
get(key: K): V;
delete(key: K): bool;
clear(): void;
toString(): string;
}

declare class Set<T> {
Expand All @@ -652,13 +661,27 @@ declare class Set<T> {
add(value: T): void;
delete(value: T): bool;
clear(): void;
toString(): string;
}

interface SymbolConstructor {
hasInstance: symbol;
isConcatSpreadable: symbol;
isRegExp: symbol;
iterator: symbol;
match: symbol;
replace: symbol;
search: symbol;
species: symbol;
split: symbol;
toPrimitive: symbol;
toStringTag: symbol;
unscopables: symbol;
(description?: string | null): symbol;
for(key: string): symbol;
keyFor(sym: symbol): string | null;
}

declare const Symbol: SymbolConstructor;

interface IMath<T> {
Expand Down
4 changes: 4 additions & 0 deletions std/assembly/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions std/assembly/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
46 changes: 33 additions & 13 deletions std/assembly/symbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,27 @@ 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: string | null = null;
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; }
}
if (idToString !== null && idToString.has(id)) str = idToString.get(id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this should be the default case of the switch above

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, make sense

return str ? "Symbol(" + str + ")" : "Symbol()";
}
}

type Symbol = symbol;

Expand All @@ -17,18 +37,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 {
Expand Down
59 changes: 41 additions & 18 deletions std/portable/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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> {
Expand Down Expand Up @@ -436,18 +440,24 @@ interface RegExp {}
interface IArguments {}

declare class Error {
constructor(message: string);
name: string;
stack?: string;
message: string;
stack: string | null;
constructor(message: string);
toString(): string;
}

declare class RangeError extends Error { }
declare class TypeError extends Error { }

declare class Set<T> {
constructor(entries?: T[]);
readonly size: i32;
has(value: T): bool;
add(value: T): void;
delete(value: T): bool;
clear(): void;
toString(): string;
[Symbol.iterator](): Iterator<T>;
}

Expand All @@ -462,10 +472,23 @@ declare class Map<K,V> {
keys(): Iterable<K>;
values(): Iterable<V>;
delete(key: K): bool;
toString(): string;
[Symbol.iterator](): Iterator<[K,V]>;
}

interface SymbolConstructor {
hasInstance: symbol;
isConcatSpreadable: symbol;
isRegExp: symbol;
iterator: symbol;
match: symbol;
replace: symbol;
search: symbol;
species: symbol;
split: symbol;
toPrimitive: symbol;
toStringTag: symbol;
unscopables: symbol;
(description?: string | null): symbol;
for(key: string): symbol;
keyFor(sym: symbol): string | null;
Expand Down
Loading