mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:38:10 +00:00
LibJS: Throw on assignment of an const variable
Was stubbed out as an assert, should be handled with a runtime exception.
This commit is contained in:
parent
8e87d340c3
commit
0d41e542b7
2 changed files with 29 additions and 2 deletions
|
@ -145,8 +145,10 @@ void Interpreter::set_variable(const FlyString& name, Value value, bool first_as
|
||||||
|
|
||||||
auto possible_match = scope.variables.get(name);
|
auto possible_match = scope.variables.get(name);
|
||||||
if (possible_match.has_value()) {
|
if (possible_match.has_value()) {
|
||||||
if (!first_assignment && possible_match.value().declaration_kind == DeclarationKind::Const)
|
if (!first_assignment && possible_match.value().declaration_kind == DeclarationKind::Const) {
|
||||||
ASSERT_NOT_REACHED();
|
throw_exception<TypeError>("Assignment to constant variable");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
scope.variables.set(move(name), { move(value), possible_match.value().declaration_kind });
|
scope.variables.set(move(name), { move(value), possible_match.value().declaration_kind });
|
||||||
return;
|
return;
|
||||||
|
|
25
Libraries/LibJS/Tests/variable-declaration.js
Normal file
25
Libraries/LibJS/Tests/variable-declaration.js
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
try {
|
||||||
|
|
||||||
|
const ConstantValue = 1;
|
||||||
|
try {
|
||||||
|
ConstantValue = 2;
|
||||||
|
} catch (e) {
|
||||||
|
assert(e.name === "TypeError");
|
||||||
|
assert(e.message === "Assignment to constant variable");
|
||||||
|
assert(ConstantValue === 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure we can define new constants in inner scopes.
|
||||||
|
//
|
||||||
|
const ConstantValue2 = 1;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
const ConstantValue2 = 2;
|
||||||
|
}
|
||||||
|
while (false)
|
||||||
|
|
||||||
|
console.log("PASS");
|
||||||
|
} catch (e) {
|
||||||
|
console.log("FAIL: " + e);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue