Skip to content

Truly independent web browser

LibJS: Keep the old value as the completion of a postfix update

Shannon Booth5c8f8d2be496
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
index de98f178b7f..b166bd6aa71 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
@@ -3264,6 +3264,12 @@ impl Generator<'_> {
// --- Statements ---
+ /// Whether the value of an expression statement is what the code completes with, should it be the last one.
+ #[inline(always)]
+ pub(super) fn tracks_completion(&self) -> bool {
+ self.context.stream.completion.is_some()
+ }
+
/// What the statement evaluated is of no use.
#[inline(always)]
pub(super) fn end_of_expression_statement(&mut self) {
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/sweep.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/sweep.rs
index e81dfc1086b..650d4e9143c 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/sweep.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/sweep.rs
@@ -314,7 +314,8 @@ impl Generator<'_> {
row += 1;
continue;
}
- let value_is_used = next_tag(tags, row) != tag::EXPR_STMT && !self.is_unused_value_row(row);
+ let value_is_used = (next_tag(tags, row) != tag::EXPR_STMT || self.tracks_completion())
+ && !self.is_unused_value_row(row);
self.update(tree.update_operator(node), row_tag == tag::PREFIX_UPDATE, value_is_used);
}
tag::CONDITIONAL => self.conditional_end(),
diff --git a/Tests/LibJS/Runtime/completion-values.js b/Tests/LibJS/Runtime/completion-values.js
index ba9004cdf3e..b9e7817da2a 100644
--- a/Tests/LibJS/Runtime/completion-values.js
+++ b/Tests/LibJS/Runtime/completion-values.js
@@ -24,6 +24,14 @@ const cases = [
["1; { let a = 1 }", 1],
["1; class C {}", 1],
["{ let a = 1; a; let b = (a = 9); }", 1],
+ ["{ let a = 1; a++; }", 1],
+ ["{ let a = 5; a--; }", 5],
+ ["{ let a = '5'; a++ }", 5],
+ ["{ let a = 1n; a-- }", 1n],
+ ["{ let i = 0; while (i < 3) i++; }", 2],
+ ["for (let i = 0; i < 2; i++) { i++; }", 0],
+ ["try { throw 1 } catch (e) { e++ }", 1],
+ ["{ let a = 1; ++a; }", 2],
];
test("the value of statements in eval code", () => {