mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 15:48:12 +00:00
LibJS: Implement basic object property assignment
This is pretty naive, we just walk up the prototype chain and call any NativeProperty setter that we find. If we don't find one, we put/set the value as an own property of the object itself.
This commit is contained in:
parent
7268499c76
commit
1a10470c1d
3 changed files with 32 additions and 9 deletions
|
@ -24,6 +24,7 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <AK/Function.h>
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibJS/AST.h>
|
||||
|
@ -476,31 +477,42 @@ void Identifier::dump(int indent) const
|
|||
|
||||
Value AssignmentExpression::execute(Interpreter& interpreter) const
|
||||
{
|
||||
ASSERT(m_lhs->is_identifier());
|
||||
auto name = static_cast<const Identifier&>(*m_lhs).string();
|
||||
AK::Function<void(Value)> commit;
|
||||
if (m_lhs->is_identifier()) {
|
||||
commit = [&](Value value) {
|
||||
auto name = static_cast<const Identifier&>(*m_lhs).string();
|
||||
interpreter.set_variable(name, value);
|
||||
};
|
||||
} else if (m_lhs->is_member_expression()) {
|
||||
commit = [&](Value value) {
|
||||
auto object = static_cast<const MemberExpression&>(*m_lhs).object().execute(interpreter).to_object(interpreter.heap());
|
||||
ASSERT(object.is_object());
|
||||
auto property_name = static_cast<const Identifier&>(static_cast<const MemberExpression&>(*m_lhs).property()).string();
|
||||
object.as_object()->put(property_name, value);
|
||||
};
|
||||
} else {
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
auto rhs_result = m_rhs->execute(interpreter);
|
||||
|
||||
switch (m_op) {
|
||||
case AssignmentOp::Assignment:
|
||||
interpreter.set_variable(name, rhs_result);
|
||||
break;
|
||||
case AssignmentOp::AdditionAssignment:
|
||||
rhs_result = add(m_lhs->execute(interpreter), rhs_result);
|
||||
interpreter.set_variable(name, rhs_result);
|
||||
break;
|
||||
case AssignmentOp::SubtractionAssignment:
|
||||
rhs_result = sub(m_lhs->execute(interpreter), rhs_result);
|
||||
interpreter.set_variable(name, rhs_result);
|
||||
break;
|
||||
case AssignmentOp::MultiplicationAssignment:
|
||||
rhs_result = mul(m_lhs->execute(interpreter), rhs_result);
|
||||
interpreter.set_variable(name, rhs_result);
|
||||
break;
|
||||
case AssignmentOp::DivisionAssignment:
|
||||
rhs_result = div(m_lhs->execute(interpreter), rhs_result);
|
||||
interpreter.set_variable(name, rhs_result);
|
||||
break;
|
||||
}
|
||||
commit(rhs_result);
|
||||
return rhs_result;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue