mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 11:48:13 +00:00
LibJS: Improve UpdateExpression::execute()
- Let undefined variables throw a ReferenceError by using Identifier::execute() rather than doing variable lookup manually and ASSERT()ing - Coerce value to number rather than ASSERT()ing - Make code DRY - Add tests
This commit is contained in:
parent
b2305cb67d
commit
a1b820b11c
2 changed files with 63 additions and 11 deletions
|
@ -775,11 +775,10 @@ Value UpdateExpression::execute(Interpreter& interpreter) const
|
||||||
{
|
{
|
||||||
ASSERT(m_argument->is_identifier());
|
ASSERT(m_argument->is_identifier());
|
||||||
auto name = static_cast<const Identifier&>(*m_argument).string();
|
auto name = static_cast<const Identifier&>(*m_argument).string();
|
||||||
|
auto old_value = m_argument->execute(interpreter);
|
||||||
auto previous_variable = interpreter.get_variable(name);
|
if (interpreter.exception())
|
||||||
ASSERT(previous_variable.has_value());
|
return {};
|
||||||
auto previous_value = previous_variable.value();
|
old_value = old_value.to_number();
|
||||||
ASSERT(previous_value.is_number());
|
|
||||||
|
|
||||||
int op_result = 0;
|
int op_result = 0;
|
||||||
switch (m_op) {
|
switch (m_op) {
|
||||||
|
@ -789,14 +788,13 @@ Value UpdateExpression::execute(Interpreter& interpreter) const
|
||||||
case UpdateOp::Decrement:
|
case UpdateOp::Decrement:
|
||||||
op_result = -1;
|
op_result = -1;
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
interpreter.set_variable(name, Value(previous_value.as_double() + op_result));
|
auto new_value = Value(old_value.as_double() + op_result);
|
||||||
|
interpreter.set_variable(name, new_value);
|
||||||
if (m_prefixed)
|
return m_prefixed ? new_value : old_value;
|
||||||
return JS::Value(previous_value.as_double() + op_result);
|
|
||||||
|
|
||||||
return previous_value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AssignmentExpression::dump(int indent) const
|
void AssignmentExpression::dump(int indent) const
|
||||||
|
|
54
Libraries/LibJS/Tests/update-expressions-basic.js
Normal file
54
Libraries/LibJS/Tests/update-expressions-basic.js
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
load("test-common.js");
|
||||||
|
|
||||||
|
try {
|
||||||
|
assertThrowsError(() => {
|
||||||
|
++x;
|
||||||
|
}, {
|
||||||
|
error: ReferenceError,
|
||||||
|
message: "'x' not known"
|
||||||
|
});
|
||||||
|
|
||||||
|
var n = 0;
|
||||||
|
assert(++n === 1);
|
||||||
|
assert(n === 1);
|
||||||
|
|
||||||
|
var n = 0;
|
||||||
|
assert(n++ === 0);
|
||||||
|
assert(n === 1);
|
||||||
|
|
||||||
|
var n = 0;
|
||||||
|
assert(--n === -1);
|
||||||
|
assert(n === -1);
|
||||||
|
|
||||||
|
var n = 0;
|
||||||
|
assert(n-- === 0);
|
||||||
|
assert(n === -1);
|
||||||
|
|
||||||
|
var a = [];
|
||||||
|
assert(a++ === 0);
|
||||||
|
assert(a === 1);
|
||||||
|
|
||||||
|
var b = true;
|
||||||
|
assert(b-- === 1);
|
||||||
|
assert(b === 0);
|
||||||
|
|
||||||
|
var s = "foo";
|
||||||
|
assert(isNaN(++s));
|
||||||
|
assert(isNaN(s));
|
||||||
|
|
||||||
|
var s = "foo";
|
||||||
|
assert(isNaN(s++));
|
||||||
|
assert(isNaN(s));
|
||||||
|
|
||||||
|
var s = "foo";
|
||||||
|
assert(isNaN(--s));
|
||||||
|
assert(isNaN(s));
|
||||||
|
|
||||||
|
var s = "foo";
|
||||||
|
assert(isNaN(s--));
|
||||||
|
assert(isNaN(s));
|
||||||
|
|
||||||
|
console.log("PASS");
|
||||||
|
} catch (e) {
|
||||||
|
console.log("FAIL: " + e);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue