mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:17:44 +00:00
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.
This commit is contained in:
parent
3c4a9e421f
commit
ee0bf55127
5 changed files with 66 additions and 16 deletions
|
@ -837,21 +837,17 @@ Value AssignmentExpression::execute(Interpreter& interpreter) const
|
||||||
if (interpreter.exception())
|
if (interpreter.exception())
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
if (m_lhs->is_identifier()) {
|
auto reference = m_lhs->to_reference(interpreter);
|
||||||
auto name = static_cast<const Identifier&>(*m_lhs).string();
|
if (interpreter.exception())
|
||||||
interpreter.set_variable(name, rhs_result);
|
return {};
|
||||||
} 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 {
|
|
||||||
return interpreter.throw_exception<ReferenceError>("Invalid left-hand side in assignment");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
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;
|
return rhs_result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -599,7 +599,7 @@ enum class AssignmentOp {
|
||||||
|
|
||||||
class AssignmentExpression : public Expression {
|
class AssignmentExpression : public Expression {
|
||||||
public:
|
public:
|
||||||
AssignmentExpression(AssignmentOp op, NonnullRefPtr<ASTNode> lhs, NonnullRefPtr<Expression> rhs)
|
AssignmentExpression(AssignmentOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs)
|
||||||
: m_op(op)
|
: m_op(op)
|
||||||
, m_lhs(move(lhs))
|
, m_lhs(move(lhs))
|
||||||
, m_rhs(move(rhs))
|
, m_rhs(move(rhs))
|
||||||
|
@ -613,7 +613,7 @@ private:
|
||||||
virtual const char* class_name() const override { return "AssignmentExpression"; }
|
virtual const char* class_name() const override { return "AssignmentExpression"; }
|
||||||
|
|
||||||
AssignmentOp m_op;
|
AssignmentOp m_op;
|
||||||
NonnullRefPtr<ASTNode> m_lhs;
|
NonnullRefPtr<Expression> m_lhs;
|
||||||
NonnullRefPtr<Expression> m_rhs;
|
NonnullRefPtr<Expression> m_rhs;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -38,6 +38,7 @@ OBJS = \
|
||||||
Runtime/ObjectConstructor.o \
|
Runtime/ObjectConstructor.o \
|
||||||
Runtime/ObjectPrototype.o \
|
Runtime/ObjectPrototype.o \
|
||||||
Runtime/PrimitiveString.o \
|
Runtime/PrimitiveString.o \
|
||||||
|
Runtime/Reference.o \
|
||||||
Runtime/ScriptFunction.o \
|
Runtime/ScriptFunction.o \
|
||||||
Runtime/Shape.o \
|
Runtime/Shape.o \
|
||||||
Runtime/StringConstructor.o \
|
Runtime/StringConstructor.o \
|
||||||
|
|
51
Libraries/LibJS/Runtime/Reference.cpp
Normal file
51
Libraries/LibJS/Runtime/Reference.cpp
Normal file
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -71,6 +71,8 @@ public:
|
||||||
return m_local_variable;
|
return m_local_variable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void assign(Interpreter&, Value);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Value m_base { js_undefined() };
|
Value m_base { js_undefined() };
|
||||||
PropertyName m_name;
|
PropertyName m_name;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue