Skip to content

Truly independent web browser

LibJS: Release the caught value once the catch parameter is bound

Shannon Bootha516cd429f2b
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
index bfdc4a71ecc..0ace6ef1929 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
@@ -1008,6 +1008,15 @@ impl Generator<'_> {
self.bind_target(parameter, first_row, value, PatternMode::Declares(DeclarationKind::Let))?;
row = parameter.index() + 1;
}
+ // NB: Nothing reads the caught value once it is bound. The environment of the parameter comes after it in the
+ // registers, though, so that it stays held until the end if there is one.
+ let held = match environment {
+ Some(_) => Some(held),
+ None => {
+ self.release_held_registers(held);
+ None
+ }
+ };
let body_environment = self.begin_scope(tree.node_pos(body), false);
self.push_control(Control::Block {
@@ -1026,7 +1035,9 @@ impl Generator<'_> {
self.pop_control();
self.end_scope(environment);
- self.release_held_registers(held);
+ if let Some(held) = held {
+ self.release_held_registers(held);
+ }
Ok(())
}
diff --git a/Tests/LibJS/Bytecode/expected/catch-scope-boundary.txt b/Tests/LibJS/Bytecode/expected/catch-scope-boundary.txt
index aaabacabf05..7324c886632 100644
--- a/Tests/LibJS/Bytecode/expected/catch-scope-boundary.txt
+++ b/Tests/LibJS/Bytecode/expected/catch-scope-boundary.txt
@@ -21,7 +21,7 @@ block0:
throwInCatch$1fd4a2a1 catch-scope-boundary.js:2:9
- Registers: 7
+ Registers: 6
Blocks: 7
Locals: result~0, e~1, e2~2
Constants:
@@ -47,9 +47,9 @@ block3:
[ 10] Throw src:Int32(2)
block4:
- [ 12] Catch value_feedback:1, dst:reg6
+ [ 12] Catch value_feedback:1, dst:reg5
[ 16] Mov dst:e2~2, src:<Empty>
- [ 19] Mov dst:e2~2, src:reg6
+ [ 19] Mov dst:e2~2, src:reg5
[ 1c] Mov dst:result~0, src:String("good")
[ 1f] Jump target:block5
@@ -65,7 +65,7 @@ Exception handlers:
throwInCatchWithLocals$11740009 catch-scope-boundary.js:17:9
- Registers: 8
+ Registers: 6
Blocks: 5
Locals: outerVar~0, e~1, catchLocal~2, inner~3
Constants:
@@ -93,14 +93,14 @@ block3:
[ 16] Throw src:String("second")
block4:
- [ 18] Catch value_feedback:1, dst:reg6
+ [ 18] Catch value_feedback:1, dst:reg5
[ 1c] Mov dst:inner~3, src:<Empty>
- [ 1f] Mov dst:inner~3, src:reg6
- [ 22] Add arith_feedback:1, dst:reg7, lhs:outerVar~0, rhs:String(",")
- [ 28] Add arith_feedback:2, dst:reg7, lhs:reg7, rhs:catchLocal~2
- [ 2e] Add arith_feedback:3, dst:reg7, lhs:reg7, rhs:String(",")
- [ 34] Add arith_feedback:4, dst:reg7, lhs:reg7, rhs:inner~3
- [ 3a] Return value:reg7
+ [ 1f] Mov dst:inner~3, src:reg5
+ [ 22] Add arith_feedback:1, dst:reg5, lhs:outerVar~0, rhs:String(",")
+ [ 28] Add arith_feedback:2, dst:reg5, lhs:reg5, rhs:catchLocal~2
+ [ 2e] Add arith_feedback:3, dst:reg5, lhs:reg5, rhs:String(",")
+ [ 34] Add arith_feedback:4, dst:reg5, lhs:reg5, rhs:inner~3
+ [ 3a] Return value:reg5
Exception handlers:
[ 4 .. 6] => handler block2
diff --git a/Tests/LibJS/Bytecode/expected/indexed-append-guards.txt b/Tests/LibJS/Bytecode/expected/indexed-append-guards.txt
index baea86fb29a..f84cb54fbc2 100644
--- a/Tests/LibJS/Bytecode/expected/indexed-append-guards.txt
+++ b/Tests/LibJS/Bytecode/expected/indexed-append-guards.txt
@@ -78,7 +78,7 @@ block6:
expect_type_error$f7cfafaa indexed-append-guards.js:4:9
- Registers: 7
+ Registers: 6
Blocks: 8
Locals: did_throw~0, error~1
Constants:
@@ -100,10 +100,10 @@ block2:
[ 10] Catch value_feedback:0, dst:reg5
[ 14] Mov dst:error~1, src:<Empty>
[ 17] Mov dst:error~1, src:reg5
- [ 1a] GetGlobal value_feedback:1, dst:reg6, `TypeError`
- [ 22] InstanceOf dst:reg6, lhs:error~1, rhs:reg6
- [ 26] Not dst:reg6, src:reg6
- [ 29] JumpFalse condition:reg6, target:block4
+ [ 1a] GetGlobal value_feedback:1, dst:reg5, `TypeError`
+ [ 22] InstanceOf dst:reg5, lhs:error~1, rhs:reg5
+ [ 26] Not dst:reg5, src:reg5
+ [ 29] JumpFalse condition:reg5, target:block4
block3:
[ 2f] Throw src:error~1
diff --git a/Tests/LibJS/Bytecode/expected/nested-catch-registers.txt b/Tests/LibJS/Bytecode/expected/nested-catch-registers.txt
new file mode 100644
index 00000000000..e2209f0ec7a
--- /dev/null
+++ b/Tests/LibJS/Bytecode/expected/nested-catch-registers.txt
@@ -0,0 +1,158 @@
+$7fc3e49c nested-catch-registers.js:44:1
+ Registers: 7
+ Blocks: 1
+ Constants:
+ [0] = Undefined
+
+block0:
+ [ 0] Enter
+ [ 1] Mov dst:reg5, src:Undefined
+ [ 4] GetGlobal value_feedback:0, dst:reg6, `nested`
+ [ c] Call call_feedback:0, dst:reg5, callee:reg6, this_value:Undefined
+ [ 13] GetGlobal value_feedback:1, dst:reg6, `bound`
+ [ 1b] Call call_feedback:1, dst:reg5, callee:reg6, this_value:Undefined
+ [ 22] End value:reg5
+
+
+nested$748dd550 nested-catch-registers.js:5:5
+ Registers: 6
+ Blocks: 19
+ Constants:
+ [0] = Undefined
+
+block0:
+ [ 0] Enter
+
+block1:
+ [ 1] GetGlobal value_feedback:0, dst:reg5, `work`
+ [ 9] Call call_feedback:0, dst:reg5, callee:reg5, this_value:Undefined
+ [ 10] Jump target:block18
+
+block2:
+ [ 15] Catch value_feedback:1, dst:reg5
+
+block3:
+ [ 19] GetGlobal value_feedback:2, dst:reg5, `work`
+ [ 21] Call call_feedback:1, dst:reg5, callee:reg5, this_value:Undefined
+ [ 28] Jump target:block17
+
+block4:
+ [ 2d] Catch value_feedback:3, dst:reg5
+
+block5:
+ [ 31] GetGlobal value_feedback:4, dst:reg5, `work`
+ [ 39] Call call_feedback:2, dst:reg5, callee:reg5, this_value:Undefined
+ [ 40] Jump target:block16
+
+block6:
+ [ 45] Catch value_feedback:5, dst:reg5
+
+block7:
+ [ 49] GetGlobal value_feedback:6, dst:reg5, `work`
+ [ 51] Call call_feedback:3, dst:reg5, callee:reg5, this_value:Undefined
+ [ 58] Jump target:block15
+
+block8:
+ [ 5d] Catch value_feedback:7, dst:reg5
+
+block9:
+ [ 61] GetGlobal value_feedback:8, dst:reg5, `work`
+ [ 69] Call call_feedback:4, dst:reg5, callee:reg5, this_value:Undefined
+ [ 70] Jump target:block14
+
+block10:
+ [ 75] Catch value_feedback:9, dst:reg5
+
+block11:
+ [ 79] GetGlobal value_feedback:10, dst:reg5, `work`
+ [ 81] Call call_feedback:5, dst:reg5, callee:reg5, this_value:Undefined
+ [ 88] Jump target:block13
+
+block12:
+ [ 8d] Catch value_feedback:11, dst:reg5
+ [ 91] GetGlobal value_feedback:12, dst:reg5, `recover`
+ [ 99] Call call_feedback:6, dst:reg5, callee:reg5, this_value:Undefined
+ [ a0] Jump target:block13
+
+block13:
+ [ a5] Jump target:block14
+
+block14:
+ [ aa] Jump target:block15
+
+block15:
+ [ af] Jump target:block16
+
+block16:
+ [ b4] Jump target:block17
+
+block17:
+ [ b9] Jump target:block18
+
+block18:
+ [ be] End value:Undefined
+
+Exception handlers:
+ [ 1 .. 15] => handler block2
+ [ 19 .. 2d] => handler block4
+ [ 31 .. 45] => handler block6
+ [ 49 .. 5d] => handler block8
+ [ 61 .. 75] => handler block10
+ [ 79 .. 8d] => handler block12
+
+
+work$79757587
+ Registers: 5
+ Blocks: 1
+ Constants:
+ [0] = Undefined
+
+block0:
+ [ 0] Enter
+ [ 1] End value:Undefined
+
+
+bound$75b76cb3 nested-catch-registers.js:33:5
+ Registers: 7
+ Blocks: 7
+ Locals: error~0, message~1
+ Constants:
+ [0] = Undefined
+ [1] = <Empty>
+
+block0:
+ [ 0] Enter
+
+block1:
+ [ 1] GetGlobal value_feedback:0, dst:reg5, `work`
+ [ 9] Call call_feedback:0, dst:reg5, callee:reg5, this_value:Undefined
+ [ 10] Jump target:block6
+
+block2:
+ [ 15] Catch value_feedback:1, dst:reg5
+ [ 19] Mov dst:error~0, src:<Empty>
+ [ 1c] Mov dst:error~0, src:reg5
+
+block3:
+ [ 1f] GetGlobal value_feedback:2, dst:reg5, `work`
+ [ 27] Call call_feedback:1, dst:reg5, callee:reg5, this_value:Undefined
+ [ 2e] Jump target:block5
+
+block4:
+ [ 33] Catch value_feedback:3, dst:reg5
+ [ 37] Mov dst:message~1, src:<Empty>
+ [ 3a] ThrowIfNullish src:reg5
+ [ 3c] GetById dst:message~1, base:reg5, `message`
+ [ 43] GetGlobal value_feedback:4, dst:reg5, `recover`
+ [ 4b] Call call_feedback:2, dst:reg5, callee:reg5, this_value:Undefined, arguments:[error~0, message~1]
+ [ 54] Jump target:block5
+
+block5:
+ [ 59] Jump target:block6
+
+block6:
+ [ 5e] End value:Undefined
+
+Exception handlers:
+ [ 1 .. 15] => handler block2
+ [ 1f .. 33] => handler block4
diff --git a/Tests/LibJS/Bytecode/expected/try-catch-scoping.txt b/Tests/LibJS/Bytecode/expected/try-catch-scoping.txt
index b4e2e16d849..395593170eb 100644
--- a/Tests/LibJS/Bytecode/expected/try-catch-scoping.txt
+++ b/Tests/LibJS/Bytecode/expected/try-catch-scoping.txt
@@ -15,7 +15,7 @@ block0:
tryCatchWithBlocks$e1a4e7e4 try-catch-scoping.js:2:9
- Registers: 9
+ Registers: 8
Blocks: 4
Locals: x~0, y~1, e~2, z~3
Constants:
@@ -38,11 +38,11 @@ block2:
[ d] Mov dst:e~2, src:<Empty>
[ 10] Mov dst:e~2, src:reg5
[ 13] Mov dst:z~3, src:Int32(3)
- [ 16] GetGlobal value_feedback:1, dst:reg6, `console`
- [ 1e] GetById dst:reg7, base:reg6, `log`
- [ 25] Add arith_feedback:0, dst:reg8, lhs:x~0, rhs:e~2
- [ 2b] Add arith_feedback:1, dst:reg8, lhs:reg8, rhs:z~3
- [ 31] Call call_feedback:0, dst:reg6, callee:reg7, this_value:reg6, arguments:[reg8]
+ [ 16] GetGlobal value_feedback:1, dst:reg5, `console`
+ [ 1e] GetById dst:reg6, base:reg5, `log`
+ [ 25] Add arith_feedback:0, dst:reg7, lhs:x~0, rhs:e~2
+ [ 2b] Add arith_feedback:1, dst:reg7, lhs:reg7, rhs:z~3
+ [ 31] Call call_feedback:0, dst:reg5, callee:reg6, this_value:reg5, arguments:[reg7]
[ 39] Jump target:block3
block3:
@@ -53,7 +53,7 @@ Exception handlers:
tryCatchFinallyWithBlocks$7da25c96 try-catch-scoping.js:14:9
- Registers: 10
+ Registers: 9
Blocks: 7
Locals: x~0, y~1, e~2, z~3
Constants:
@@ -76,9 +76,9 @@ block2:
[ 9] Catch value_feedback:0, dst:reg7
[ d] Mov dst:e~2, src:<Empty>
[ 10] Mov dst:e~2, src:reg7
- [ 13] GetGlobal value_feedback:1, dst:reg8, `console`
- [ 1b] GetById dst:reg9, base:reg8, `log`
- [ 22] Call call_feedback:0, dst:reg8, callee:reg9, this_value:reg8, arguments:[e~2]
+ [ 13] GetGlobal value_feedback:1, dst:reg7, `console`
+ [ 1b] GetById dst:reg8, base:reg7, `log`
+ [ 22] Call call_feedback:0, dst:reg7, callee:reg8, this_value:reg7, arguments:[e~2]
[ 2a] Mov dst:reg5, src:Int32(0)
[ 2d] Jump target:block4
diff --git a/Tests/LibJS/Bytecode/input/nested-catch-registers.js b/Tests/LibJS/Bytecode/input/nested-catch-registers.js
new file mode 100644
index 00000000000..30a03e79d3b
--- /dev/null
+++ b/Tests/LibJS/Bytecode/input/nested-catch-registers.js
@@ -0,0 +1,45 @@
+function work() {}
+function recover() {}
+
+function nested() {
+ try {
+ work();
+ } catch {
+ try {
+ work();
+ } catch {
+ try {
+ work();
+ } catch {
+ try {
+ work();
+ } catch {
+ try {
+ work();
+ } catch {
+ try {
+ work();
+ } catch {
+ recover();
+ }
+ }
+ }
+ }
+ }
+ }
+}
+
+function bound() {
+ try {
+ work();
+ } catch (error) {
+ try {
+ work();
+ } catch ({ message }) {
+ recover(error, message);
+ }
+ }
+}
+
+nested();
+bound();