diff options
author | Brian Gianforcaro <b.gianfo@gmail.com> | 2020-04-12 15:50:49 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-13 01:12:31 +0200 |
commit | 0d41e542b70bdcd2eea6eac7dc2748bb5b1f2f79 (patch) | |
tree | d121cb8d327bffd97c39605e590aa3989410953e /Libraries/LibJS | |
parent | 8e87d340c310c1ce4893cc5517d33aaf848eb808 (diff) | |
download | serenity-0d41e542b70bdcd2eea6eac7dc2748bb5b1f2f79.zip |
LibJS: Throw on assignment of an const variable
Was stubbed out as an assert, should be handled with a runtime exception.
Diffstat (limited to 'Libraries/LibJS')
-rw-r--r-- | Libraries/LibJS/Interpreter.cpp | 6 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/variable-declaration.js | 25 |
2 files changed, 29 insertions, 2 deletions
diff --git a/Libraries/LibJS/Interpreter.cpp b/Libraries/LibJS/Interpreter.cpp index c3b74cff9c..756855b499 100644 --- a/Libraries/LibJS/Interpreter.cpp +++ b/Libraries/LibJS/Interpreter.cpp @@ -145,8 +145,10 @@ void Interpreter::set_variable(const FlyString& name, Value value, bool first_as auto possible_match = scope.variables.get(name); if (possible_match.has_value()) { - if (!first_assignment && possible_match.value().declaration_kind == DeclarationKind::Const) - ASSERT_NOT_REACHED(); + if (!first_assignment && possible_match.value().declaration_kind == DeclarationKind::Const) { + throw_exception<TypeError>("Assignment to constant variable"); + return; + } scope.variables.set(move(name), { move(value), possible_match.value().declaration_kind }); return; diff --git a/Libraries/LibJS/Tests/variable-declaration.js b/Libraries/LibJS/Tests/variable-declaration.js new file mode 100644 index 0000000000..ef634867c9 --- /dev/null +++ b/Libraries/LibJS/Tests/variable-declaration.js @@ -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); +} |