Skip to content

Commit 8e1c660

Browse files
committed
Refactor exception handling tests and assertions
Simplified assertions in exception tests by removing redundant comparisons and using direct boolean checks. Updated type assertions in catch blocks for better type safety. Added eslint-disable comments for returns in finally blocks. Adjusted expected line numbers in .wat test outputs to match code changes.
1 parent bafc198 commit 8e1c660

File tree

4 files changed

+17
-25
lines changed

4 files changed

+17
-25
lines changed

src/compiler.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3022,9 +3022,8 @@ export class Compiler extends DiagnosticEmitter {
30223022

30233023
// If exception handling feature is enabled, use actual throw instruction
30243024
if (this.options.hasFeature(Feature.ExceptionHandling)) {
3025-
let value = statement.value;
30263025
// Compile the thrown value - should be Error or subclass
3027-
let valueExpr = this.compileExpression(value, Type.auto);
3026+
let valueExpr = this.compileExpression(statement.value, Type.auto);
30283027

30293028
// Ensure exception tag exists
30303029
let tagName = this.ensureExceptionTag();

tests/compiler/exceptions.debug.wat

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5331,8 +5331,6 @@
53315331
end
53325332
call $exceptions/testFinally
53335333
global.get $exceptions/finallyRan
5334-
i32.const 1
5335-
i32.eq
53365334
i32.eqz
53375335
if
53385336
i32.const 0
@@ -5367,8 +5365,6 @@
53675365
unreachable
53685366
end
53695367
global.get $exceptions/returnInCatchFinallyRan
5370-
i32.const 1
5371-
i32.eq
53725368
i32.eqz
53735369
if
53745370
i32.const 0
@@ -5392,8 +5388,6 @@
53925388
unreachable
53935389
end
53945390
global.get $exceptions/tryCatchFinallyRan
5395-
i32.const 1
5396-
i32.eq
53975391
i32.eqz
53985392
if
53995393
i32.const 0
@@ -5994,7 +5988,7 @@
59945988
if
59955989
i32.const 0
59965990
i32.const 480
5997-
i32.const 473
5991+
i32.const 474
59985992
i32.const 1
59995993
call $~lib/builtins/abort
60005994
unreachable
@@ -6006,7 +6000,7 @@
60066000
if
60076001
i32.const 0
60086002
i32.const 480
6009-
i32.const 485
6003+
i32.const 487
60106004
i32.const 1
60116005
call $~lib/builtins/abort
60126006
unreachable
@@ -6018,19 +6012,17 @@
60186012
if
60196013
i32.const 0
60206014
i32.const 480
6021-
i32.const 497
6015+
i32.const 500
60226016
i32.const 1
60236017
call $~lib/builtins/abort
60246018
unreachable
60256019
end
60266020
global.get $exceptions/finallyReturnSuppressedExceptionRan
6027-
i32.const 1
6028-
i32.eq
60296021
i32.eqz
60306022
if
60316023
i32.const 0
60326024
i32.const 480
6033-
i32.const 498
6025+
i32.const 501
60346026
i32.const 1
60356027
call $~lib/builtins/abort
60366028
unreachable

tests/compiler/exceptions.release.wat

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3153,8 +3153,7 @@
31533153
unreachable
31543154
end
31553155
global.get $exceptions/tryCatchFinallyRan
3156-
i32.const 1
3157-
i32.ne
3156+
i32.eqz
31583157
if
31593158
i32.const 0
31603159
i32.const 1504
@@ -4334,12 +4333,11 @@
43344333
unreachable
43354334
end
43364335
global.get $exceptions/finallyReturnSuppressedExceptionRan
4337-
i32.const 1
4338-
i32.ne
4336+
i32.eqz
43394337
if
43404338
i32.const 0
43414339
i32.const 1504
4342-
i32.const 498
4340+
i32.const 501
43434341
i32.const 1
43444342
call $~lib/builtins/abort
43454343
unreachable

tests/compiler/exceptions.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function testCatchVar(): string {
1919
try {
2020
throw new Error("msg");
2121
} catch (e) {
22-
return e.message;
22+
return (e as Error).message;
2323
}
2424
return "";
2525
}
@@ -45,7 +45,7 @@ function testFinally(): void {
4545
}
4646
}
4747
testFinally();
48-
assert(finallyRan == true);
48+
assert(finallyRan);
4949

5050
// Nested try-catch
5151
function testNested(): i32 {
@@ -74,7 +74,7 @@ function testReturnInCatchFinally(): i32 {
7474
}
7575
}
7676
assert(testReturnInCatchFinally() == 10);
77-
assert(returnInCatchFinallyRan == true);
77+
assert(returnInCatchFinallyRan);
7878

7979
// Try-catch-finally (without return in catch)
8080
let tryCatchFinallyRan = false;
@@ -90,7 +90,7 @@ function testTryCatchFinally(): void {
9090
}
9191
testTryCatchFinally();
9292
assert(tryCatchFinallyResult == 10);
93-
assert(tryCatchFinallyRan == true);
93+
assert(tryCatchFinallyRan);
9494

9595
// Finally with exception propagation
9696
let finallyWithExceptionRan = false;
@@ -302,7 +302,7 @@ function innerRethrow(): void {
302302
throw new Error("original");
303303
} catch (e) {
304304
rethrowFinallyRan = false;
305-
throw new Error("rethrown: " + e.message);
305+
throw new Error("rethrown: " + (e as Error).message);
306306
} finally {
307307
rethrowFinallyRan = true;
308308
}
@@ -467,6 +467,7 @@ function testReturnInFinally(): i32 {
467467
try {
468468
return 1; // This return is overridden
469469
} finally {
470+
// eslint-disable-next-line no-unsafe-finally
470471
return 2; // This return wins
471472
}
472473
}
@@ -479,6 +480,7 @@ function testReturnInFinallyOverridesCatch(): i32 {
479480
} catch (e) {
480481
return 1; // This return is overridden
481482
} finally {
483+
// eslint-disable-next-line no-unsafe-finally
482484
return 2; // This return wins
483485
}
484486
}
@@ -491,8 +493,9 @@ function testReturnInFinallySuppressesException(): i32 {
491493
throw new Error("should be suppressed");
492494
} finally {
493495
finallyReturnSuppressedExceptionRan = true;
496+
// eslint-disable-next-line no-unsafe-finally
494497
return 42; // This suppresses the exception
495498
}
496499
}
497500
assert(testReturnInFinallySuppressesException() == 42);
498-
assert(finallyReturnSuppressedExceptionRan == true);
501+
assert(finallyReturnSuppressedExceptionRan);

0 commit comments

Comments
 (0)