Skip to content

Commit 0cf5f40

Browse files
committed
fix: should detect base exceptions and break instead
1 parent 4440843 commit 0cf5f40

7 files changed

Lines changed: 39 additions & 47 deletions

File tree

assembly/test.ts

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,10 @@
1-
import { JSON } from "json-as";
2-
// 1st Level Abort
3-
// try {
4-
// abort("Failed to execute!", "test.ts");
5-
// console.log("This should not execute");
6-
// } catch (e) {
7-
// console.log("Got an error: " + e.toString());
8-
// } finally {
9-
// console.log("Gracefully shutting down...");
10-
// process.exit(0);
11-
// }
12-
import { __Exception } from "./types/exception";
13-
@json
14-
class Vec3 {
15-
x: i32 = 0;
16-
y: i32 = 0;
17-
z: i32 = 0;
18-
}
191
try {
20-
JSON.parse<Vec3>("\"hello\"")
2+
// Do something
3+
abort("Failed to execute!", "test.ts");
4+
console.log("This should not execute");
215
} catch (e) {
226
console.log("Got an error: " + e.toString());
237
} finally {
248
console.log("Gracefully shutting down...");
259
process.exit(0);
26-
}
27-
28-
// function doSomething(shouldAbort: boolean = false): void {
29-
// if (shouldAbort) {
30-
// abort("Function 'doSomething' failed to execute properly!");
31-
// }
32-
// console.log("'doSomething' executed");
33-
// }
34-
35-
// // 2nd Level Abort
36-
// try {
37-
// doSomething(false);
38-
// console.log("First exception passed");
39-
// doSomething(true);
40-
// console.log("Second exception passed")
41-
// } catch (e) {
42-
// console.log("Got an error: " + e.toString());
43-
// } finally {
44-
// console.log("Gracefully shutting down...");
45-
// process.exit(0);
46-
// }
10+
}

transform/lib/linkers/exception.js

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

transform/lib/linkers/exception.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

transform/lib/transform.js

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

transform/lib/transform.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

transform/src/linkers/exception.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@ export class ExceptionLinker extends Visitor {
4949

5050
public changed: boolean = false;
5151
public fn: FunctionDeclaration | null = null;
52+
public loop: DoStatement | null = null;
5253

5354
public exceptions: ExceptionParent[] = [];
5455

56+
public baseException: boolean = false;
57+
5558
visitCallExpression(
5659
node: CallExpression,
5760
ref: Node | Node[] | null = null,
@@ -235,6 +238,12 @@ export class ExceptionLinker extends Visitor {
235238
node.range,
236239
);
237240

241+
if (this.loop) {
242+
breakStmt = Node.createBreakStatement(null, node.range);
243+
this.loop = null;
244+
return breakStmt;
245+
}
246+
238247
if (parent) {
239248
const returnType = toString(parent.signature.returnType);
240249
if (DEBUG) console.log("Return Type: " + returnType + " derived from " + parent.name.text);
@@ -385,8 +394,12 @@ export class ExceptionLinker extends Visitor {
385394
}
386395
}
387396

388-
static replace(node: Node | Node[]): void {
397+
static replace(node: Node | Node[], baseException: boolean = false): void {
389398
ExceptionLinker.SN.fn = null;
399+
if (baseException && !Array.isArray(node) && node.kind == NodeKind.Do) {
400+
const n = node as DoStatement;
401+
ExceptionLinker.SN.loop = n;
402+
}
390403
ExceptionLinker.SN.visit(node);
391404
}
392405
}

transform/src/transform.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
CallExpression,
33
CommonFlags,
4+
ExpressionStatement,
45
FunctionDeclaration,
56
IdentifierExpression,
67
Node,
@@ -11,7 +12,7 @@ import {
1112
TryStatement,
1213
} from "assemblyscript/dist/assemblyscript.js";
1314
import { Visitor } from "./lib/visitor.js";
14-
import { toString } from "./lib/util.js";
15+
import { SimpleParser, toString } from "./lib/util.js";
1516
import { replaceRef } from "./utils.js";
1617
import { ExceptionLinker } from "./linkers/exception.js";
1718

@@ -46,6 +47,7 @@ export class TryTransform extends Visitor {
4647

4748
const hasBaseException = node.bodyStatements.some((v) => {
4849
if (!v) return false;
50+
if (v.kind == NodeKind.Expression) v = (v as ExpressionStatement).expression;
4951
if (
5052
v.kind == NodeKind.Call
5153
&& (v as CallExpression).expression.kind == NodeKind.Identifier
@@ -80,7 +82,7 @@ export class TryTransform extends Visitor {
8082
)
8183
) : null;
8284

83-
ExceptionLinker.replace(tryLoop || tryBlock);
85+
ExceptionLinker.replace(tryLoop || tryBlock, hasBaseException);
8486

8587
if (DEBUG) console.log("Before Try: " + toString(beforeTry));
8688

0 commit comments

Comments
 (0)