Skip to content

Commit c0706e3

Browse files
MaxGraeyWillem Wyndham
authored and
Willem Wyndham
committed
Add ArrayBuffer/DataView/Symbol#toString and improve Errors (AssemblyScript#332)
1 parent feebe09 commit c0706e3

13 files changed

+3480
-119
lines changed

std/assembly/arraybuffer.ts

+4
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@ export class ArrayBuffer {
3030
memory.copy(changetype<usize>(buffer) + HEADER_SIZE, changetype<usize>(this) + HEADER_SIZE + begin, newLen);
3131
return buffer;
3232
}
33+
34+
toString(): string {
35+
return "[object ArrayBuffer]";
36+
}
3337
}

std/assembly/dataview.ts

+4
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ export class DataView {
177177
HEADER_SIZE
178178
);
179179
}
180+
181+
toString(): string {
182+
return "[object DataView]";
183+
}
180184
}
181185

182186
@inline function checkOffset(byteOffset: i32, n: i32, byteLength: i32): void {

std/assembly/error.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
export class Error {
22

3-
name: string = "Error";
4-
message: string;
3+
name: string = "Error";
54
stack: string = ""; // TODO
65

7-
constructor(message: string = "") {
8-
this.message = message;
9-
}
6+
constructor(
7+
public message: string = ""
8+
) {}
109

1110
toString(): string {
1211
var message = this.message;
@@ -29,3 +28,10 @@ export class TypeError extends Error {
2928
this.name = "TypeError";
3029
}
3130
}
31+
32+
export class SyntaxError extends Error {
33+
constructor(message: string = "") {
34+
super(message);
35+
this.name = "SyntaxError";
36+
}
37+
}

std/assembly/index.d.ts

+47-21
Original file line numberDiff line numberDiff line change
@@ -592,45 +592,47 @@ declare class DataView {
592592
/** Constructs a new `DataView` with the given properties */
593593
constructor(buffer: ArrayBuffer, byteOffset?: i32, byteLength?: i32);
594594
/** The `getFloat32()` method gets a signed 32-bit float (float) at the specified byte offset from the start of the `DataView`. */
595-
getFloat32(byteOffset: i32, littleEndian?: boolean): f32
595+
getFloat32(byteOffset: i32, littleEndian?: boolean): f32;
596596
/** The `getFloat64()` method gets a signed 64-bit float (double) at the specified byte offset from the start of the `DataView`. */
597-
getFloat64(byteOffset: i32, littleEndian?: boolean): f64
597+
getFloat64(byteOffset: i32, littleEndian?: boolean): f64;
598598
/** The `getInt8()` method gets a signed 8-bit integer (byte) at the specified byte offset from the start of the `DataView`. */
599-
getInt8(byteOffset: i32): i8
599+
getInt8(byteOffset: i32): i8;
600600
/** The `getInt16()` method gets a signed 16-bit integer (short) at the specified byte offset from the start of the `DataView`. */
601-
getInt16(byteOffset: i32, littleEndian?: boolean): i16
601+
getInt16(byteOffset: i32, littleEndian?: boolean): i16;
602602
/** The `getInt32()` method gets a signed 32-bit integer (long) at the specified byte offset from the start of the `DataView`. */
603-
getInt32(byteOffset: i32, littleEndian?: boolean): i32
603+
getInt32(byteOffset: i32, littleEndian?: boolean): i32;
604604
/** The `getInt64()` method gets a signed 64-bit integer (long long) at the specified byte offset from the start of the `DataView`. */
605-
getInt64(byteOffset: i32, littleEndian?: boolean): i64
605+
getInt64(byteOffset: i32, littleEndian?: boolean): i64;
606606
/** The `getUint8()` method gets an unsigned 8-bit integer (unsigned byte) at the specified byte offset from the start of the `DataView`. */
607-
getUint8(byteOffset: i32): u8
607+
getUint8(byteOffset: i32): u8;
608608
/** The `getUint16()` method gets an unsigned 16-bit integer (unsigned short) at the specified byte offset from the start of the `DataView`. */
609-
getUint16(byteOffset: i32, littleEndian?: boolean): u16
609+
getUint16(byteOffset: i32, littleEndian?: boolean): u16;
610610
/** The `getUint32()` method gets an unsigned 32-bit integer (unsigned long) at the specified byte offset from the start of the `DataView`. */
611-
getUint32(byteOffset: i32, littleEndian?: boolean): u32
611+
getUint32(byteOffset: i32, littleEndian?: boolean): u32;
612612
/** The `getUint64()` method gets an unsigned 64-bit integer (unsigned long long) at the specified byte offset from the start of the `DataView`. */
613-
getUint64(byteOffset: i32, littleEndian?: boolean): u64
613+
getUint64(byteOffset: i32, littleEndian?: boolean): u64;
614614
/** The `setFloat32()` method stores a signed 32-bit float (float) value at the specified byte offset from the start of the `DataView`. */
615-
setFloat32(byteOffset: i32, value: f32, littleEndian?: boolean): void
615+
setFloat32(byteOffset: i32, value: f32, littleEndian?: boolean): void;
616616
/** The `setFloat64()` method stores a signed 64-bit float (double) value at the specified byte offset from the start of the `DataView`. */
617-
setFloat64(byteOffset: i32, value: f64, littleEndian?: boolean): void
617+
setFloat64(byteOffset: i32, value: f64, littleEndian?: boolean): void;
618618
/** The `setInt8()` method stores a signed 8-bit integer (byte) value at the specified byte offset from the start of the `DataView`. */
619-
setInt8(byteOffset: i32, value: i8): void
619+
setInt8(byteOffset: i32, value: i8): void;
620620
/** The `setInt16()` method stores a signed 16-bit integer (short) value at the specified byte offset from the start of the `DataView`. */
621-
setInt16(byteOffset: i32, value: i16, littleEndian?: boolean): void
621+
setInt16(byteOffset: i32, value: i16, littleEndian?: boolean): void;
622622
/** The `setInt32()` method stores a signed 32-bit integer (long) value at the specified byte offset from the start of the `DataView`. */
623-
setInt32(byteOffset: i32, value: i32, littleEndian?: boolean): void
623+
setInt32(byteOffset: i32, value: i32, littleEndian?: boolean): void;
624624
/** The `setInt64()` method stores a signed 64-bit integer (long long) value at the specified byte offset from the start of the `DataView`. */
625-
setInt64(byteOffset: i32, value: i64, littleEndian?: boolean): void
625+
setInt64(byteOffset: i32, value: i64, littleEndian?: boolean): void;
626626
/** The `setUint8()` method stores an unsigned 8-bit integer (byte) value at the specified byte offset from the start of the `DataView`. */
627-
setUint8(byteOffset: i32, value: u8): void
627+
setUint8(byteOffset: i32, value: u8): void;
628628
/** The `setUint16()` method stores an unsigned 16-bit integer (unsigned short) value at the specified byte offset from the start of the `DataView`. */
629-
setUint16(byteOffset: i32, value: u16, littleEndian?: boolean): void
629+
setUint16(byteOffset: i32, value: u16, littleEndian?: boolean): void;
630630
/** The `setUint32()` method stores an unsigned 32-bit integer (unsigned long) value at the specified byte offset from the start of the `DataView`. */
631-
setUint32(byteOffset: i32, value: u32, littleEndian?: boolean): void
631+
setUint32(byteOffset: i32, value: u32, littleEndian?: boolean): void;
632632
/** The `setUint64()` method stores an unsigned 64-bit integer (unsigned long long) value at the specified byte offset from the start of the `DataView`. */
633-
setUint64(byteOffset: i32, value: u64, littleEndian?: boolean): void
633+
setUint64(byteOffset: i32, value: u64, littleEndian?: boolean): void;
634+
/** Returns a string representation of DataView. */
635+
toString(): string;
634636
}
635637

636638
/** Interface for a typed view on an array buffer. */
@@ -767,15 +769,24 @@ declare class Error {
767769
message: string;
768770

769771
/** Stack trace. */
770-
stack: string;
772+
stack?: string;
771773

772774
/** Constructs a new error, optionally with a message. */
773775
constructor(message?: string);
776+
777+
/** Method returns a string representing the specified Error class. */
778+
toString(): string;
774779
}
775780

776781
/** Class for indicating an error when a value is not in the set or range of allowed values. */
777782
declare class RangeError extends Error { }
778783

784+
/** Class for indicating an error when a value is not of the expected type. */
785+
declare class TypeError extends Error { }
786+
787+
/** Class for indicating an error when trying to interpret syntactically invalid code. */
788+
declare class SyntaxError extends Error { }
789+
779790
interface Boolean {}
780791
interface Function {}
781792
interface IArguments {}
@@ -790,6 +801,7 @@ declare class Map<K,V> {
790801
get(key: K): V;
791802
delete(key: K): bool;
792803
clear(): void;
804+
toString(): string;
793805
}
794806

795807
declare class Set<T> {
@@ -798,13 +810,27 @@ declare class Set<T> {
798810
add(value: T): void;
799811
delete(value: T): bool;
800812
clear(): void;
813+
toString(): string;
801814
}
802815

803816
interface SymbolConstructor {
817+
readonly hasInstance: symbol;
818+
readonly isConcatSpreadable: symbol;
819+
readonly isRegExp: symbol;
820+
readonly iterator: symbol;
821+
readonly match: symbol;
822+
readonly replace: symbol;
823+
readonly search: symbol;
824+
readonly species: symbol;
825+
readonly split: symbol;
826+
readonly toPrimitive: symbol;
827+
readonly toStringTag: symbol;
828+
readonly unscopables: symbol;
804829
(description?: string | null): symbol;
805830
for(key: string): symbol;
806831
keyFor(sym: symbol): string | null;
807832
}
833+
808834
declare const Symbol: SymbolConstructor;
809835

810836
interface IMath<T> {

std/assembly/map.ts

+4
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ export class Map<K,V> {
165165
this.entriesOffset = this.entriesCount;
166166
}
167167

168+
toString(): string {
169+
return "[object Map]";
170+
}
171+
168172
private __gc(): void {
169173
__gc_mark(changetype<usize>(this.buckets)); // tslint:disable-line
170174
var entries = this.entries;

std/assembly/set.ts

+4
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ export class Set<K> {
153153
this.entriesOffset = this.entriesCount;
154154
}
155155

156+
toString(): string {
157+
return "[object Set]";
158+
}
159+
156160
private __gc(): void {
157161
__gc_mark(changetype<usize>(this.buckets)); // tslint:disable-line
158162
var entries = this.entries;

std/assembly/symbol.ts

+36-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,30 @@ var stringToId: Map<string, usize>;
44
var idToString: Map<usize, string>;
55
var nextId: usize = 12; // Symbol.unscopables + 1
66

7-
@unmanaged export class symbol {}
7+
@unmanaged export class symbol {
8+
toString(): string {
9+
var id = changetype<usize>(this);
10+
var str = "";
11+
switch (id) {
12+
case 1: { str = "hasInstance"; break; }
13+
case 2: { str = "isConcatSpreadable"; break; }
14+
case 3: { str = "isRegExp"; break; }
15+
case 4: { str = "match"; break; }
16+
case 5: { str = "replace"; break; }
17+
case 6: { str = "search"; break; }
18+
case 7: { str = "species"; break; }
19+
case 8: { str = "split"; break; }
20+
case 9: { str = "toPrimitive"; break; }
21+
case 10: { str = "toStringTag"; break; }
22+
case 11: { str = "unscopables"; break; }
23+
default: {
24+
if (idToString !== null && idToString.has(id)) str = idToString.get(id);
25+
break;
26+
}
27+
}
28+
return "Symbol(" + str + ")";
29+
}
30+
}
831

932
type Symbol = symbol;
1033

@@ -17,18 +40,18 @@ export function Symbol(description: string | null = null): symbol {
1740
export namespace Symbol {
1841

1942
// well-known symbols
20-
export const hasInstance = changetype<symbol>(1);
21-
export const concatSpreadable = changetype<symbol>(2);
22-
export const isRegExp = changetype<symbol>(3);
23-
export const iterator = changetype<symbol>(3);
24-
export const match = changetype<symbol>(4);
25-
export const replace = changetype<symbol>(5);
26-
export const search = changetype<symbol>(6);
27-
export const species = changetype<symbol>(7);
28-
export const split = changetype<symbol>(8);
29-
export const toPrimitive = changetype<symbol>(9);
30-
export const toStringTag = changetype<symbol>(10);
31-
export const unscopables = changetype<symbol>(11);
43+
export const hasInstance = changetype<symbol>(1);
44+
export const isConcatSpreadable = changetype<symbol>(2);
45+
export const isRegExp = changetype<symbol>(3);
46+
export const iterator = changetype<symbol>(3);
47+
export const match = changetype<symbol>(4);
48+
export const replace = changetype<symbol>(5);
49+
export const search = changetype<symbol>(6);
50+
export const species = changetype<symbol>(7);
51+
export const split = changetype<symbol>(8);
52+
export const toPrimitive = changetype<symbol>(9);
53+
export const toStringTag = changetype<symbol>(10);
54+
export const unscopables = changetype<symbol>(11);
3255

3356
/* tslint:disable */// not valid TS
3457
export function for(key: string): symbol {

0 commit comments

Comments
 (0)