Prechádzať zdrojové kódy

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.
Andreas Kling 5 rokov pred
rodič
commit
ee0bf55127

+ 9 - 13
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<const Identifier&>(*m_lhs).string();
-        interpreter.set_variable(name, rhs_result);
-    } else if (m_lhs->is_member_expression()) {
-        auto object_value = static_cast<const MemberExpression&>(*m_lhs).object().execute(interpreter);
-        if (interpreter.exception())
-            return {};
-        if (auto* object = object_value.to_object(interpreter.heap())) {
-            auto property_name = static_cast<const MemberExpression&>(*m_lhs).computed_property_name(interpreter);
-            object->put(property_name, rhs_result);
-        }
-    } else {
+    auto reference = m_lhs->to_reference(interpreter);
+    if (interpreter.exception())
+        return {};
+
+    if (reference.is_unresolvable())
         return interpreter.throw_exception<ReferenceError>("Invalid left-hand side in assignment");
-    }
 
+    reference.assign(interpreter, rhs_result);
+
+    if (interpreter.exception())
+        return {};
     return rhs_result;
 }
 

+ 2 - 2
Libraries/LibJS/AST.h

@@ -599,7 +599,7 @@ enum class AssignmentOp {
 
 class AssignmentExpression : public Expression {
 public:
-    AssignmentExpression(AssignmentOp op, NonnullRefPtr<ASTNode> lhs, NonnullRefPtr<Expression> rhs)
+    AssignmentExpression(AssignmentOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> 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<ASTNode> m_lhs;
+    NonnullRefPtr<Expression> m_lhs;
     NonnullRefPtr<Expression> m_rhs;
 };
 

+ 1 - 0
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 \

+ 51 - 0
Libraries/LibJS/Runtime/Reference.cpp

@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
+ * 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 <LibJS/Interpreter.h>
+#include <LibJS/Runtime/Reference.h>
+#include <LibJS/Runtime/Object.h>
+
+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);
+}
+
+}

+ 2 - 0
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;