From ee0bf55127dc2add3b271d11c22395f7e29b1860 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 27 Apr 2020 12:56:09 +0200 Subject: [PATCH] LibJS: Make AssignmentExpression assign through a Reference Reference now has assign(Interpreter&, Value) which is used to write transparently through a Reference into whatever location it refers to. --- Libraries/LibJS/AST.cpp | 24 ++++++------- Libraries/LibJS/AST.h | 4 +-- Libraries/LibJS/Makefile | 1 + Libraries/LibJS/Runtime/Reference.cpp | 51 +++++++++++++++++++++++++++ Libraries/LibJS/Runtime/Reference.h | 2 ++ 5 files changed, 66 insertions(+), 16 deletions(-) create mode 100644 Libraries/LibJS/Runtime/Reference.cpp diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp index ea0741363f..d5399266dc 100644 --- a/Libraries/LibJS/AST.cpp +++ b/Libraries/LibJS/AST.cpp @@ -837,21 +837,17 @@ Value AssignmentExpression::execute(Interpreter& interpreter) const if (interpreter.exception()) return {}; - if (m_lhs->is_identifier()) { - auto name = static_cast(*m_lhs).string(); - interpreter.set_variable(name, rhs_result); - } else if (m_lhs->is_member_expression()) { - auto object_value = static_cast(*m_lhs).object().execute(interpreter); - if (interpreter.exception()) - return {}; - if (auto* object = object_value.to_object(interpreter.heap())) { - auto property_name = static_cast(*m_lhs).computed_property_name(interpreter); - object->put(property_name, rhs_result); - } - } else { - return interpreter.throw_exception("Invalid left-hand side in assignment"); - } + auto reference = m_lhs->to_reference(interpreter); + if (interpreter.exception()) + return {}; + if (reference.is_unresolvable()) + return interpreter.throw_exception("Invalid left-hand side in assignment"); + + reference.assign(interpreter, rhs_result); + + if (interpreter.exception()) + return {}; return rhs_result; } diff --git a/Libraries/LibJS/AST.h b/Libraries/LibJS/AST.h index e0143f3015..395367afec 100644 --- a/Libraries/LibJS/AST.h +++ b/Libraries/LibJS/AST.h @@ -599,7 +599,7 @@ enum class AssignmentOp { class AssignmentExpression : public Expression { public: - AssignmentExpression(AssignmentOp op, NonnullRefPtr lhs, NonnullRefPtr rhs) + AssignmentExpression(AssignmentOp op, NonnullRefPtr lhs, NonnullRefPtr rhs) : m_op(op) , m_lhs(move(lhs)) , m_rhs(move(rhs)) @@ -613,7 +613,7 @@ private: virtual const char* class_name() const override { return "AssignmentExpression"; } AssignmentOp m_op; - NonnullRefPtr m_lhs; + NonnullRefPtr m_lhs; NonnullRefPtr m_rhs; }; diff --git a/Libraries/LibJS/Makefile b/Libraries/LibJS/Makefile index 7b84c3c845..e7d9442056 100644 --- a/Libraries/LibJS/Makefile +++ b/Libraries/LibJS/Makefile @@ -38,6 +38,7 @@ OBJS = \ Runtime/ObjectConstructor.o \ Runtime/ObjectPrototype.o \ Runtime/PrimitiveString.o \ + Runtime/Reference.o \ Runtime/ScriptFunction.o \ Runtime/Shape.o \ Runtime/StringConstructor.o \ diff --git a/Libraries/LibJS/Runtime/Reference.cpp b/Libraries/LibJS/Runtime/Reference.cpp new file mode 100644 index 0000000000..13402cdc9d --- /dev/null +++ b/Libraries/LibJS/Runtime/Reference.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2020, Andreas Kling + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include + +namespace JS { + +void Reference::assign(Interpreter& interpreter, Value value) +{ + // NOTE: The caller is responsible for doing an exception check after assign(). + + ASSERT(!is_unresolvable()); + + if (is_local_variable()) { + interpreter.set_variable(m_name.to_string(), value); + return; + } + + auto* object = base().to_object(interpreter.heap()); + if (!object) + return; + + object->put(m_name, value); +} + +} diff --git a/Libraries/LibJS/Runtime/Reference.h b/Libraries/LibJS/Runtime/Reference.h index 2cad110497..81b1701496 100644 --- a/Libraries/LibJS/Runtime/Reference.h +++ b/Libraries/LibJS/Runtime/Reference.h @@ -71,6 +71,8 @@ public: return m_local_variable; } + void assign(Interpreter&, Value); + private: Value m_base { js_undefined() }; PropertyName m_name;