Skip to content

Commit 1928404

Browse files
MaxGraeydcodeIO
authored andcommitted
Add ArrayBuffer/DataView/Symbol#toString and improve Errors (#332)
1 parent a79db87 commit 1928404

13 files changed

+3480
-119
lines changed

std/assembly/arraybuffer.ts

Lines changed: 4 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 11 additions & 5 deletions
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

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -447,45 +447,47 @@ declare class DataView {
447447
/** Constructs a new `DataView` with the given properties */
448448
constructor(buffer: ArrayBuffer, byteOffset?: i32, byteLength?: i32);
449449
/** The `getFloat32()` method gets a signed 32-bit float (float) at the specified byte offset from the start of the `DataView`. */
450-
getFloat32(byteOffset: i32, littleEndian?: boolean): f32
450+
getFloat32(byteOffset: i32, littleEndian?: boolean): f32;
451451
/** The `getFloat64()` method gets a signed 64-bit float (double) at the specified byte offset from the start of the `DataView`. */
452-
getFloat64(byteOffset: i32, littleEndian?: boolean): f64
452+
getFloat64(byteOffset: i32, littleEndian?: boolean): f64;
453453
/** The `getInt8()` method gets a signed 8-bit integer (byte) at the specified byte offset from the start of the `DataView`. */
454-
getInt8(byteOffset: i32): i8
454+
getInt8(byteOffset: i32): i8;
455455
/** The `getInt16()` method gets a signed 16-bit integer (short) at the specified byte offset from the start of the `DataView`. */
456-
getInt16(byteOffset: i32, littleEndian?: boolean): i16
456+
getInt16(byteOffset: i32, littleEndian?: boolean): i16;
457457
/** The `getInt32()` method gets a signed 32-bit integer (long) at the specified byte offset from the start of the `DataView`. */
458-
getInt32(byteOffset: i32, littleEndian?: boolean): i32
458+
getInt32(byteOffset: i32, littleEndian?: boolean): i32;
459459
/** The `getInt64()` method gets a signed 64-bit integer (long long) at the specified byte offset from the start of the `DataView`. */
460-
getInt64(byteOffset: i32, littleEndian?: boolean): i64
460+
getInt64(byteOffset: i32, littleEndian?: boolean): i64;
461461
/** The `getUint8()` method gets an unsigned 8-bit integer (unsigned byte) at the specified byte offset from the start of the `DataView`. */
462-
getUint8(byteOffset: i32): u8
462+
getUint8(byteOffset: i32): u8;
463463
/** The `getUint16()` method gets an unsigned 16-bit integer (unsigned short) at the specified byte offset from the start of the `DataView`. */
464-
getUint16(byteOffset: i32, littleEndian?: boolean): u16
464+
getUint16(byteOffset: i32, littleEndian?: boolean): u16;
465465
/** The `getUint32()` method gets an unsigned 32-bit integer (unsigned long) at the specified byte offset from the start of the `DataView`. */
466-
getUint32(byteOffset: i32, littleEndian?: boolean): u32
466+
getUint32(byteOffset: i32, littleEndian?: boolean): u32;
467467
/** The `getUint64()` method gets an unsigned 64-bit integer (unsigned long long) at the specified byte offset from the start of the `DataView`. */
468-
getUint64(byteOffset: i32, littleEndian?: boolean): u64
468+
getUint64(byteOffset: i32, littleEndian?: boolean): u64;
469469
/** The `setFloat32()` method stores a signed 32-bit float (float) value at the specified byte offset from the start of the `DataView`. */
470-
setFloat32(byteOffset: i32, value: f32, littleEndian?: boolean): void
470+
setFloat32(byteOffset: i32, value: f32, littleEndian?: boolean): void;
471471
/** The `setFloat64()` method stores a signed 64-bit float (double) value at the specified byte offset from the start of the `DataView`. */
472-
setFloat64(byteOffset: i32, value: f64, littleEndian?: boolean): void
472+
setFloat64(byteOffset: i32, value: f64, littleEndian?: boolean): void;
473473
/** The `setInt8()` method stores a signed 8-bit integer (byte) value at the specified byte offset from the start of the `DataView`. */
474-
setInt8(byteOffset: i32, value: i8): void
474+
setInt8(byteOffset: i32, value: i8): void;
475475
/** The `setInt16()` method stores a signed 16-bit integer (short) value at the specified byte offset from the start of the `DataView`. */
476-
setInt16(byteOffset: i32, value: i16, littleEndian?: boolean): void
476+
setInt16(byteOffset: i32, value: i16, littleEndian?: boolean): void;
477477
/** The `setInt32()` method stores a signed 32-bit integer (long) value at the specified byte offset from the start of the `DataView`. */
478-
setInt32(byteOffset: i32, value: i32, littleEndian?: boolean): void
478+
setInt32(byteOffset: i32, value: i32, littleEndian?: boolean): void;
479479
/** The `setInt64()` method stores a signed 64-bit integer (long long) value at the specified byte offset from the start of the `DataView`. */
480-
setInt64(byteOffset: i32, value: i64, littleEndian?: boolean): void
480+
setInt64(byteOffset: i32, value: i64, littleEndian?: boolean): void;
481481
/** The `setUint8()` method stores an unsigned 8-bit integer (byte) value at the specified byte offset from the start of the `DataView`. */
482-
setUint8(byteOffset: i32, value: u8): void
482+
setUint8(byteOffset: i32, value: u8): void;
483483
/** The `setUint16()` method stores an unsigned 16-bit integer (unsigned short) value at the specified byte offset from the start of the `DataView`. */
484-
setUint16(byteOffset: i32, value: u16, littleEndian?: boolean): void
484+
setUint16(byteOffset: i32, value: u16, littleEndian?: boolean): void;
485485
/** The `setUint32()` method stores an unsigned 32-bit integer (unsigned long) value at the specified byte offset from the start of the `DataView`. */
486-
setUint32(byteOffset: i32, value: u32, littleEndian?: boolean): void
486+
setUint32(byteOffset: i32, value: u32, littleEndian?: boolean): void;
487487
/** The `setUint64()` method stores an unsigned 64-bit integer (unsigned long long) value at the specified byte offset from the start of the `DataView`. */
488-
setUint64(byteOffset: i32, value: u64, littleEndian?: boolean): void
488+
setUint64(byteOffset: i32, value: u64, littleEndian?: boolean): void;
489+
/** Returns a string representation of DataView. */
490+
toString(): string;
489491
}
490492

491493
/** Interface for a typed view on an array buffer. */
@@ -621,15 +623,24 @@ declare class Error {
621623
message: string;
622624

623625
/** Stack trace. */
624-
stack: string;
626+
stack?: string;
625627

626628
/** Constructs a new error, optionally with a message. */
627629
constructor(message?: string);
630+
631+
/** Method returns a string representing the specified Error class. */
632+
toString(): string;
628633
}
629634

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

638+
/** Class for indicating an error when a value is not of the expected type. */
639+
declare class TypeError extends Error { }
640+
641+
/** Class for indicating an error when trying to interpret syntactically invalid code. */
642+
declare class SyntaxError extends Error { }
643+
633644
interface Boolean {}
634645
interface Function {}
635646
interface IArguments {}
@@ -644,6 +655,7 @@ declare class Map<K,V> {
644655
get(key: K): V;
645656
delete(key: K): bool;
646657
clear(): void;
658+
toString(): string;
647659
}
648660

649661
declare class Set<T> {
@@ -652,13 +664,27 @@ declare class Set<T> {
652664
add(value: T): void;
653665
delete(value: T): bool;
654666
clear(): void;
667+
toString(): string;
655668
}
656669

657670
interface SymbolConstructor {
671+
readonly hasInstance: symbol;
672+
readonly isConcatSpreadable: symbol;
673+
readonly isRegExp: symbol;
674+
readonly iterator: symbol;
675+
readonly match: symbol;
676+
readonly replace: symbol;
677+
readonly search: symbol;
678+
readonly species: symbol;
679+
readonly split: symbol;
680+
readonly toPrimitive: symbol;
681+
readonly toStringTag: symbol;
682+
readonly unscopables: symbol;
658683
(description?: string | null): symbol;
659684
for(key: string): symbol;
660685
keyFor(sym: symbol): string | null;
661686
}
687+
662688
declare const Symbol: SymbolConstructor;
663689

664690
interface IMath<T> {

std/assembly/map.ts

Lines changed: 4 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 36 additions & 13 deletions
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)