Skip to content

Commit 18904e0

Browse files
committed
feat: real example with JSON and bad data
1 parent eadb6e2 commit 18904e0

26 files changed

Lines changed: 400 additions & 271 deletions

assembly/__tests__/abort.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { JSON } from "json-as";
2-
import { AbortState } from "../types/abort";
3-
import { ExceptionState, Exception } from "../types/exception";
2+
import { __AbortState } from "../types/abort";
3+
import { __ExceptionState, __Exception } from "../types/exception";
44
import { describe, expect } from "./lib";
55

66
@json
@@ -10,11 +10,11 @@ class Vec3 {
1010
z: i32 = 0;
1111
}
1212
// describe("Should handle immediate abort call", () => {
13-
try {
14-
JSON.parse<Vec3>("\"hello\"")
15-
} catch (e) {
16-
expect(e.toString()).toBe("abort: This should abort");
17-
}
13+
try {
14+
JSON.parse<Vec3>("\"hello\"")
15+
} catch (e) {
16+
expect(e.toString()).toBe("abort: This should abort");
17+
}
1818
// });
1919

2020
// describe("Should execute finally block", () => {

assembly/__tests__/lib/unreachable.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { UnreachableState } from "../types/unreachable";
2-
import { ExceptionState, Exception } from "../types/exception";
1+
import { __UnreachableState } from "../types/unreachable";
2+
import { __ExceptionState, __Exception } from "../types/exception";
33
import { describe, expect } from "./lib";
44

55
describe("Should handle immediate unreachable call", () => {

assembly/globals.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

assembly/test.transformed.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { AbortState } from "./types/abort";
1+
import { __AbortState } from "./types/abort";
22
import { ExceptionState, Exception } from "./types/exception";
33

44
// 1st Level Abort
55
// ExceptionState.Failed = false;
66
// {
7-
// AbortState.abort("Failed to execute!", "test.ts");
7+
// __AbortState.abort("Failed to execute!", "test.ts");
88
// if (!ExceptionState.Failed) {
99
// console.log("This should not execute");
1010
// }
@@ -36,7 +36,7 @@ function doSomething(shouldAbort: boolean = false): string {
3636

3737
function doSomethingElse(shouldAbort: boolean = false): void {
3838
if (shouldAbort) {
39-
AbortState.abort("Function 'doSomething' failed to execute properly!");
39+
__AbortState.abort("Function 'doSomething' failed to execute properly!");
4040
// Since it's void, I can return here or jump to the branch
4141
return;
4242
}

assembly/test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { JSON } from "json-as";
2-
import { AbortState } from "./types/abort";
3-
import { ExceptionState, Exception } from "./types/exception";
4-
import { ErrorState } from "./types/error";
2+
import { __AbortState } from "./types/abort";
3+
import { __ExceptionState, __Exception } from "./types/exception";
4+
import { __ErrorState } from "./types/error";
55

66
// 1st Level Abort
77
// try {

assembly/types/abort.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
import { ExceptionState, ExceptionType } from "./exception";
1+
import { __ExceptionState, __ExceptionType } from "./exception";
22

3-
export namespace AbortState {
3+
export namespace __AbortState {
44
export let msg: string | null = null;
55
export let fileName: string | null = null;
66
export let lineNumber: i32 = -1;
77
export let columnNumber: i32 = -1;
88
// @ts-ignore: inline
99
@inline export function reset(): void {
10-
ExceptionState.Failed = false;
11-
AbortState.msg = null;
12-
AbortState.fileName = null;
13-
AbortState.lineNumber = -1;
14-
AbortState.columnNumber = -1;
10+
__ExceptionState.Failed = false;
11+
__AbortState.msg = null;
12+
__AbortState.fileName = null;
13+
__AbortState.lineNumber = -1;
14+
__AbortState.columnNumber = -1;
1515
}
1616
// @ts-ignore: inline
1717
export function abort(msg: string | null = null, fileName: string | null = null, lineNumber: i32 = 0, columnNumber: i32 = 0): void {
18-
ExceptionState.Failed = true;
19-
ExceptionState.Type = ExceptionType.Abort;
20-
21-
AbortState.msg = msg;
22-
AbortState.fileName = fileName;
23-
AbortState.lineNumber = lineNumber;
24-
AbortState.columnNumber = columnNumber;
18+
__ExceptionState.Failed = true;
19+
__ExceptionState.Type = __ExceptionType.Abort;
20+
21+
__AbortState.msg = msg;
22+
__AbortState.fileName = fileName;
23+
__AbortState.lineNumber = lineNumber;
24+
__AbortState.columnNumber = columnNumber;
2525
}
2626
}

assembly/types/error.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1-
import { ExceptionState, ExceptionType } from "./exception";
1+
import { __ExceptionState, __ExceptionType } from "./exception";
22

3-
export namespace ErrorState {
3+
export namespace __ErrorState {
44
export let message: string = "";
55
export let name: string = "";
66
export let stack: string | null = null;
77
// @ts-ignore: inline
88
@inline export function reset(): void {
9-
ExceptionState.Failed = false;
10-
ErrorState.message = "";
11-
ErrorState.name = "";
12-
ErrorState.stack = null;
9+
__ExceptionState.Failed = false;
10+
__ErrorState.message = "";
11+
__ErrorState.name = "";
12+
__ErrorState.stack = null;
1313
}
1414
// @ts-ignore: inline
1515
export function error(message: string = ""): void {
16-
ExceptionState.Failed = true;
17-
ExceptionState.Type = ExceptionType.Error;
16+
__ExceptionState.Failed = true;
17+
__ExceptionState.Type = __ExceptionType.Error;
1818

19-
ErrorState.message = message;
19+
__ErrorState.message = message;
2020
}
21-
}
22-
23-
new Error
21+
}

assembly/types/exception.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
1-
import { AbortState } from "./abort";
2-
import { ErrorState } from "./error";
1+
import { __AbortState } from "./abort";
2+
import { __ErrorState } from "./error";
33

4-
export enum ExceptionType {
4+
export enum __ExceptionType {
55
None,
66
Abort,
77
Error,
88
Unreachable,
99
}
1010

11-
export namespace ExceptionState {
11+
export namespace __ExceptionState {
1212
export let Failed: boolean = false;
13-
export let Type: ExceptionType = ExceptionType.None;
13+
export let Type: __ExceptionType = __ExceptionType.None;
1414
}
1515

16-
export class Exception {
17-
public type: ExceptionType;
16+
export class __Exception {
17+
public type: __ExceptionType;
1818
// Abort
19-
public msg: string | null = AbortState.msg;
20-
public fileName: string | null = AbortState.fileName;
21-
public lineNumber: i32 = AbortState.lineNumber;
22-
public columnNumber: i32 = AbortState.columnNumber;
19+
public msg: string | null = __AbortState.msg;
20+
public fileName: string | null = __AbortState.fileName;
21+
public lineNumber: i32 = __AbortState.lineNumber;
22+
public columnNumber: i32 = __AbortState.columnNumber;
2323

2424
// Error
25-
public message: string = ErrorState.message;
26-
public name: string = ErrorState.name;
27-
public stack: string | null = ErrorState.stack;
25+
public message: string = __ErrorState.message;
26+
public name: string = __ErrorState.name;
27+
public stack: string | null = __ErrorState.stack;
2828

29-
constructor(type: ExceptionType) {
29+
constructor(type: __ExceptionType) {
3030
this.type = type;
3131
}
3232
toString(): string {
3333
let out = "";
34-
if (this.type == ExceptionType.Abort) {
34+
if (this.type == __ExceptionType.Abort) {
3535
out = "abort";
36-
if (AbortState.msg) out += ": " + AbortState.msg!;
37-
if (AbortState.fileName) out += " in " + AbortState.fileName!;
38-
if (AbortState.lineNumber)
39-
out += ` in (${AbortState.lineNumber}:${AbortState.columnNumber})`;
40-
} else if (this.type == ExceptionType.Unreachable) {
36+
if (__AbortState.msg) out += ": " + __AbortState.msg!;
37+
if (__AbortState.fileName) out += " in " + __AbortState.fileName!;
38+
if (__AbortState.lineNumber)
39+
out += ` in (${__AbortState.lineNumber}:${__AbortState.columnNumber})`;
40+
} else if (this.type == __ExceptionType.Unreachable) {
4141
out = "unreachable";
42-
} else if (this.type == ExceptionType.Error) {
43-
out = "Error: " + ErrorState.message;
42+
} else if (this.type == __ExceptionType.Error) {
43+
out = "Error: " + __ErrorState.message;
4444
}
4545
return out;
4646
}

assembly/types/unreachable.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { ExceptionState, ExceptionType } from "./exception";
1+
import { __ExceptionState, __ExceptionType } from "./exception";
22

3-
export namespace UnreachableState {
3+
export namespace __UnreachableState {
44
// @ts-ignore: inline
55
@inline export function reset(): void {
6-
ExceptionState.Failed = false;
6+
__ExceptionState.Failed = false;
77
}
88
// @ts-ignore: inline
99
export function unreachable(): void {
10-
ExceptionState.Failed = true;
11-
ExceptionState.Type = ExceptionType.Unreachable;
10+
__ExceptionState.Failed = true;
11+
__ExceptionState.Type = __ExceptionType.Unreachable;
1212
}
1313
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
},
5050
"type": "module",
5151
"dependencies": {
52+
"@types/clone-deep": "^4.0.4",
53+
"clone-deep": "^4.0.1",
5254
"json-as": "^1.0.4"
5355
}
54-
}
56+
}

0 commit comments

Comments
 (0)