2020-03-07 19:42:11 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-03-19 17:39:13 +01:00
|
|
|
#include <AK/Function.h>
|
2020-03-12 19:22:13 +08:00
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <AK/StringBuilder.h>
|
2020-03-07 19:42:11 +01:00
|
|
|
#include <LibJS/AST.h>
|
|
|
|
#include <LibJS/Interpreter.h>
|
2020-03-20 20:29:57 +01:00
|
|
|
#include <LibJS/Runtime/Array.h>
|
2020-03-24 14:37:39 +01:00
|
|
|
#include <LibJS/Runtime/Error.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Runtime/PrimitiveString.h>
|
|
|
|
#include <LibJS/Runtime/ScriptFunction.h>
|
|
|
|
#include <LibJS/Runtime/Value.h>
|
2020-03-07 19:42:11 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
Value ScopeNode::execute(Interpreter& interpreter) const
|
|
|
|
{
|
|
|
|
return interpreter.run(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value FunctionDeclaration::execute(Interpreter& interpreter) const
|
|
|
|
{
|
2020-03-13 10:08:52 +01:00
|
|
|
auto* function = interpreter.heap().allocate<ScriptFunction>(body(), parameters());
|
2020-03-19 11:12:08 +01:00
|
|
|
interpreter.set_variable(name(), function);
|
2020-03-26 12:27:15 +01:00
|
|
|
return {};
|
2020-03-07 19:42:11 +01:00
|
|
|
}
|
|
|
|
|
2020-03-19 11:12:08 +01:00
|
|
|
Value FunctionExpression::execute(Interpreter& interpreter) const
|
|
|
|
{
|
|
|
|
return interpreter.heap().allocate<ScriptFunction>(body(), parameters());
|
|
|
|
}
|
|
|
|
|
2020-03-11 19:27:43 +01:00
|
|
|
Value ExpressionStatement::execute(Interpreter& interpreter) const
|
|
|
|
{
|
|
|
|
return m_expression->execute(interpreter);
|
|
|
|
}
|
|
|
|
|
2020-04-01 18:51:27 +02:00
|
|
|
CallExpression::ThisAndCallee CallExpression::compute_this_and_callee(Interpreter& interpreter) const
|
|
|
|
{
|
|
|
|
if (is_new_expression()) {
|
|
|
|
// Computing |this| is irrelevant for "new" expression.
|
|
|
|
return { {}, m_callee->execute(interpreter) };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_callee->is_member_expression()) {
|
|
|
|
auto& member_expression = static_cast<const MemberExpression&>(*m_callee);
|
|
|
|
auto object_value = member_expression.object().execute(interpreter);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
auto* this_value = object_value.to_object(interpreter.heap());
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
auto callee = this_value->get(member_expression.computed_property_name(interpreter)).value_or({});
|
|
|
|
return { this_value, callee };
|
|
|
|
}
|
|
|
|
return { &interpreter.global_object(), m_callee->execute(interpreter) };
|
|
|
|
}
|
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
Value CallExpression::execute(Interpreter& interpreter) const
|
|
|
|
{
|
2020-04-01 18:51:27 +02:00
|
|
|
auto [this_value, callee] = compute_this_and_callee(interpreter);
|
2020-03-24 14:37:39 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
|
2020-03-30 13:41:17 +01:00
|
|
|
if (!callee.is_object() || !callee.as_object()->is_function())
|
|
|
|
return interpreter.throw_exception<Error>("TypeError", String::format("%s is not a function", callee.to_string().characters()));
|
|
|
|
|
2020-03-13 10:08:52 +01:00
|
|
|
auto* function = static_cast<Function*>(callee.as_object());
|
2020-03-12 19:53:31 +01:00
|
|
|
|
2020-03-17 11:00:09 +01:00
|
|
|
auto& call_frame = interpreter.push_call_frame();
|
2020-03-24 14:37:39 +01:00
|
|
|
for (size_t i = 0; i < m_arguments.size(); ++i) {
|
2020-03-27 15:35:35 +01:00
|
|
|
auto value = m_arguments[i].execute(interpreter);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
call_frame.arguments.append(value);
|
2020-03-24 14:37:39 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
}
|
2020-03-12 19:22:13 +08:00
|
|
|
|
2020-03-28 16:33:52 +01:00
|
|
|
Object* new_object = nullptr;
|
|
|
|
if (is_new_expression()) {
|
|
|
|
new_object = interpreter.heap().allocate<Object>();
|
2020-03-28 16:40:36 +01:00
|
|
|
auto prototype = function->get("prototype");
|
|
|
|
if (prototype.has_value() && prototype.value().is_object())
|
|
|
|
new_object->set_prototype(prototype.value().as_object());
|
2020-03-28 16:33:52 +01:00
|
|
|
call_frame.this_value = new_object;
|
|
|
|
} else {
|
2020-04-01 18:51:27 +02:00
|
|
|
call_frame.this_value = this_value;
|
2020-03-24 14:37:39 +01:00
|
|
|
}
|
2020-03-15 15:01:10 +01:00
|
|
|
|
2020-03-28 22:48:35 +01:00
|
|
|
auto result = function->call(interpreter);
|
2020-03-17 11:00:09 +01:00
|
|
|
interpreter.pop_call_frame();
|
2020-03-28 16:33:52 +01:00
|
|
|
|
|
|
|
if (is_new_expression()) {
|
|
|
|
if (result.is_object())
|
|
|
|
return result;
|
|
|
|
return new_object;
|
|
|
|
}
|
2020-03-15 15:01:10 +01:00
|
|
|
return result;
|
2020-03-07 19:42:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Value ReturnStatement::execute(Interpreter& interpreter) const
|
|
|
|
{
|
2020-03-11 19:27:43 +01:00
|
|
|
auto value = argument() ? argument()->execute(interpreter) : js_undefined();
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-03-23 19:19:03 +01:00
|
|
|
interpreter.unwind(ScopeType::Function);
|
2020-03-07 19:42:11 +01:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-03-08 07:58:58 +02:00
|
|
|
Value IfStatement::execute(Interpreter& interpreter) const
|
|
|
|
{
|
|
|
|
auto predicate_result = m_predicate->execute(interpreter);
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-03-08 07:58:58 +02:00
|
|
|
|
2020-03-10 08:46:20 +01:00
|
|
|
if (predicate_result.to_boolean())
|
2020-03-08 07:58:58 +02:00
|
|
|
return interpreter.run(*m_consequent);
|
2020-03-21 18:40:17 +01:00
|
|
|
|
|
|
|
if (m_alternate)
|
2020-03-08 07:58:58 +02:00
|
|
|
return interpreter.run(*m_alternate);
|
2020-03-21 18:40:17 +01:00
|
|
|
|
2020-03-26 12:36:52 +01:00
|
|
|
return {};
|
2020-03-08 07:58:58 +02:00
|
|
|
}
|
|
|
|
|
2020-03-09 03:22:21 +08:00
|
|
|
Value WhileStatement::execute(Interpreter& interpreter) const
|
|
|
|
{
|
|
|
|
Value last_value = js_undefined();
|
2020-03-10 08:46:20 +01:00
|
|
|
while (m_predicate->execute(interpreter).to_boolean()) {
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-03-09 03:22:21 +08:00
|
|
|
last_value = interpreter.run(*m_body);
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-03-09 03:22:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return last_value;
|
|
|
|
}
|
|
|
|
|
2020-03-12 23:12:12 +11:00
|
|
|
Value ForStatement::execute(Interpreter& interpreter) const
|
|
|
|
{
|
2020-03-18 11:23:53 +01:00
|
|
|
RefPtr<BlockStatement> wrapper;
|
2020-03-14 13:56:49 +02:00
|
|
|
|
2020-03-25 16:14:18 +01:00
|
|
|
if (m_init && m_init->is_variable_declaration() && static_cast<const VariableDeclaration*>(m_init.ptr())->declaration_type() != DeclarationType::Var) {
|
2020-03-18 11:23:53 +01:00
|
|
|
wrapper = create_ast_node<BlockStatement>();
|
2020-03-14 13:56:49 +02:00
|
|
|
interpreter.enter_scope(*wrapper, {}, ScopeType::Block);
|
|
|
|
}
|
|
|
|
|
2020-03-12 23:12:12 +11:00
|
|
|
Value last_value = js_undefined();
|
|
|
|
|
2020-03-27 15:35:35 +01:00
|
|
|
if (m_init) {
|
2020-03-12 23:12:12 +11:00
|
|
|
m_init->execute(interpreter);
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
}
|
2020-03-12 23:12:12 +11:00
|
|
|
|
|
|
|
if (m_test) {
|
|
|
|
while (m_test->execute(interpreter).to_boolean()) {
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-03-12 23:12:12 +11:00
|
|
|
last_value = interpreter.run(*m_body);
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
if (m_update) {
|
2020-03-12 23:12:12 +11:00
|
|
|
m_update->execute(interpreter);
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
}
|
2020-03-12 23:12:12 +11:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (true) {
|
|
|
|
last_value = interpreter.run(*m_body);
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
if (m_update) {
|
2020-03-12 23:12:12 +11:00
|
|
|
m_update->execute(interpreter);
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
}
|
2020-03-12 23:12:12 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-14 13:56:49 +02:00
|
|
|
if (wrapper)
|
|
|
|
interpreter.exit_scope(*wrapper);
|
|
|
|
|
2020-03-12 23:12:12 +11:00
|
|
|
return last_value;
|
|
|
|
}
|
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
Value BinaryExpression::execute(Interpreter& interpreter) const
|
|
|
|
{
|
|
|
|
auto lhs_result = m_lhs->execute(interpreter);
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-03-07 19:42:11 +01:00
|
|
|
auto rhs_result = m_rhs->execute(interpreter);
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-03-07 19:42:11 +01:00
|
|
|
|
|
|
|
switch (m_op) {
|
|
|
|
case BinaryOp::Plus:
|
|
|
|
return add(lhs_result, rhs_result);
|
|
|
|
case BinaryOp::Minus:
|
|
|
|
return sub(lhs_result, rhs_result);
|
2020-03-12 23:04:52 +11:00
|
|
|
case BinaryOp::Asterisk:
|
|
|
|
return mul(lhs_result, rhs_result);
|
|
|
|
case BinaryOp::Slash:
|
|
|
|
return div(lhs_result, rhs_result);
|
2020-03-08 07:53:02 +02:00
|
|
|
case BinaryOp::TypedEquals:
|
|
|
|
return typed_eq(lhs_result, rhs_result);
|
2020-03-08 23:27:18 +02:00
|
|
|
case BinaryOp::TypedInequals:
|
2020-03-16 00:23:38 +02:00
|
|
|
return Value(!typed_eq(lhs_result, rhs_result).as_bool());
|
|
|
|
case BinaryOp::AbstractEquals:
|
|
|
|
return eq(lhs_result, rhs_result);
|
|
|
|
case BinaryOp::AbstractInequals:
|
|
|
|
return Value(!eq(lhs_result, rhs_result).as_bool());
|
2020-03-10 11:35:05 +01:00
|
|
|
case BinaryOp::GreaterThan:
|
|
|
|
return greater_than(lhs_result, rhs_result);
|
2020-03-12 23:07:08 +11:00
|
|
|
case BinaryOp::GreaterThanEquals:
|
|
|
|
return greater_than_equals(lhs_result, rhs_result);
|
2020-03-10 11:35:05 +01:00
|
|
|
case BinaryOp::LessThan:
|
|
|
|
return less_than(lhs_result, rhs_result);
|
2020-03-12 23:07:08 +11:00
|
|
|
case BinaryOp::LessThanEquals:
|
|
|
|
return less_than_equals(lhs_result, rhs_result);
|
2020-03-10 11:35:05 +01:00
|
|
|
case BinaryOp::BitwiseAnd:
|
|
|
|
return bitwise_and(lhs_result, rhs_result);
|
|
|
|
case BinaryOp::BitwiseOr:
|
|
|
|
return bitwise_or(lhs_result, rhs_result);
|
|
|
|
case BinaryOp::BitwiseXor:
|
|
|
|
return bitwise_xor(lhs_result, rhs_result);
|
|
|
|
case BinaryOp::LeftShift:
|
|
|
|
return left_shift(lhs_result, rhs_result);
|
|
|
|
case BinaryOp::RightShift:
|
|
|
|
return right_shift(lhs_result, rhs_result);
|
2020-03-28 16:56:54 +01:00
|
|
|
case BinaryOp::InstanceOf:
|
|
|
|
return instance_of(lhs_result, rhs_result);
|
2020-03-08 07:53:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
2020-03-08 07:55:44 +02:00
|
|
|
|
|
|
|
Value LogicalExpression::execute(Interpreter& interpreter) const
|
|
|
|
{
|
2020-03-10 08:46:20 +01:00
|
|
|
auto lhs_result = m_lhs->execute(interpreter).to_boolean();
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-03-10 08:46:20 +01:00
|
|
|
auto rhs_result = m_rhs->execute(interpreter).to_boolean();
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-03-08 07:55:44 +02:00
|
|
|
switch (m_op) {
|
|
|
|
case LogicalOp::And:
|
|
|
|
return Value(lhs_result && rhs_result);
|
|
|
|
case LogicalOp::Or:
|
|
|
|
return Value(lhs_result || rhs_result);
|
2020-03-07 19:42:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2020-03-08 23:27:18 +02:00
|
|
|
Value UnaryExpression::execute(Interpreter& interpreter) const
|
|
|
|
{
|
|
|
|
auto lhs_result = m_lhs->execute(interpreter);
|
|
|
|
switch (m_op) {
|
2020-03-14 20:43:35 +02:00
|
|
|
case UnaryOp::BitwiseNot:
|
2020-03-10 11:35:05 +01:00
|
|
|
return bitwise_not(lhs_result);
|
2020-03-09 19:04:44 +02:00
|
|
|
case UnaryOp::Not:
|
2020-03-10 11:08:37 +01:00
|
|
|
return Value(!lhs_result.to_boolean());
|
2020-03-18 06:33:32 +11:00
|
|
|
case UnaryOp::Typeof:
|
|
|
|
switch (lhs_result.type()) {
|
|
|
|
case Value::Type::Undefined:
|
|
|
|
return js_string(interpreter.heap(), "undefined");
|
|
|
|
case Value::Type::Null:
|
|
|
|
// yes, this is on purpose. yes, this is how javascript works.
|
|
|
|
// yes, it's silly.
|
|
|
|
return js_string(interpreter.heap(), "object");
|
|
|
|
case Value::Type::Number:
|
|
|
|
return js_string(interpreter.heap(), "number");
|
|
|
|
case Value::Type::String:
|
|
|
|
return js_string(interpreter.heap(), "string");
|
|
|
|
case Value::Type::Object:
|
|
|
|
return js_string(interpreter.heap(), "object");
|
|
|
|
case Value::Type::Boolean:
|
|
|
|
return js_string(interpreter.heap(), "boolean");
|
|
|
|
}
|
2020-03-08 23:27:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
static void print_indent(int indent)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < indent * 2; ++i)
|
|
|
|
putchar(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASTNode::dump(int indent) const
|
|
|
|
{
|
|
|
|
print_indent(indent);
|
|
|
|
printf("%s\n", class_name());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScopeNode::dump(int indent) const
|
|
|
|
{
|
|
|
|
ASTNode::dump(indent);
|
|
|
|
for (auto& child : children())
|
|
|
|
child.dump(indent + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BinaryExpression::dump(int indent) const
|
|
|
|
{
|
2020-03-07 23:16:34 +01:00
|
|
|
const char* op_string = nullptr;
|
|
|
|
switch (m_op) {
|
|
|
|
case BinaryOp::Plus:
|
|
|
|
op_string = "+";
|
|
|
|
break;
|
|
|
|
case BinaryOp::Minus:
|
|
|
|
op_string = "-";
|
|
|
|
break;
|
2020-03-12 23:04:52 +11:00
|
|
|
case BinaryOp::Asterisk:
|
|
|
|
op_string = "*";
|
|
|
|
break;
|
|
|
|
case BinaryOp::Slash:
|
|
|
|
op_string = "/";
|
|
|
|
break;
|
2020-03-08 07:53:02 +02:00
|
|
|
case BinaryOp::TypedEquals:
|
|
|
|
op_string = "===";
|
|
|
|
break;
|
2020-03-08 23:27:18 +02:00
|
|
|
case BinaryOp::TypedInequals:
|
|
|
|
op_string = "!==";
|
|
|
|
break;
|
2020-03-16 00:23:38 +02:00
|
|
|
case BinaryOp::AbstractEquals:
|
|
|
|
op_string = "==";
|
|
|
|
break;
|
|
|
|
case BinaryOp::AbstractInequals:
|
|
|
|
op_string = "!=";
|
|
|
|
break;
|
2020-03-10 11:35:05 +01:00
|
|
|
case BinaryOp::GreaterThan:
|
2020-03-08 23:27:18 +02:00
|
|
|
op_string = ">";
|
|
|
|
break;
|
2020-03-12 23:07:08 +11:00
|
|
|
case BinaryOp::GreaterThanEquals:
|
|
|
|
op_string = ">=";
|
|
|
|
break;
|
2020-03-10 11:35:05 +01:00
|
|
|
case BinaryOp::LessThan:
|
2020-03-08 23:27:18 +02:00
|
|
|
op_string = "<";
|
|
|
|
break;
|
2020-03-12 23:07:08 +11:00
|
|
|
case BinaryOp::LessThanEquals:
|
|
|
|
op_string = "<=";
|
|
|
|
break;
|
2020-03-10 11:35:05 +01:00
|
|
|
case BinaryOp::BitwiseAnd:
|
2020-03-08 23:27:18 +02:00
|
|
|
op_string = "&";
|
|
|
|
break;
|
2020-03-10 11:35:05 +01:00
|
|
|
case BinaryOp::BitwiseOr:
|
2020-03-08 23:27:18 +02:00
|
|
|
op_string = "|";
|
|
|
|
break;
|
2020-03-10 11:35:05 +01:00
|
|
|
case BinaryOp::BitwiseXor:
|
2020-03-08 23:27:18 +02:00
|
|
|
op_string = "^";
|
|
|
|
break;
|
2020-03-10 11:35:05 +01:00
|
|
|
case BinaryOp::LeftShift:
|
2020-03-08 23:27:18 +02:00
|
|
|
op_string = "<<";
|
|
|
|
break;
|
2020-03-10 11:35:05 +01:00
|
|
|
case BinaryOp::RightShift:
|
2020-03-08 23:27:18 +02:00
|
|
|
op_string = ">>";
|
|
|
|
break;
|
2020-03-28 16:56:54 +01:00
|
|
|
case BinaryOp::InstanceOf:
|
|
|
|
op_string = "instanceof";
|
|
|
|
break;
|
2020-03-08 07:53:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
print_indent(indent);
|
|
|
|
printf("%s\n", class_name());
|
|
|
|
m_lhs->dump(indent + 1);
|
|
|
|
print_indent(indent + 1);
|
|
|
|
printf("%s\n", op_string);
|
|
|
|
m_rhs->dump(indent + 1);
|
|
|
|
}
|
|
|
|
|
2020-03-08 07:55:44 +02:00
|
|
|
void LogicalExpression::dump(int indent) const
|
|
|
|
{
|
|
|
|
const char* op_string = nullptr;
|
|
|
|
switch (m_op) {
|
|
|
|
case LogicalOp::And:
|
|
|
|
op_string = "&&";
|
|
|
|
break;
|
|
|
|
case LogicalOp::Or:
|
|
|
|
op_string = "||";
|
|
|
|
break;
|
2020-03-07 23:16:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
print_indent(indent);
|
|
|
|
printf("%s\n", class_name());
|
2020-03-07 19:42:11 +01:00
|
|
|
m_lhs->dump(indent + 1);
|
2020-03-07 23:16:34 +01:00
|
|
|
print_indent(indent + 1);
|
|
|
|
printf("%s\n", op_string);
|
2020-03-07 19:42:11 +01:00
|
|
|
m_rhs->dump(indent + 1);
|
|
|
|
}
|
|
|
|
|
2020-03-08 23:27:18 +02:00
|
|
|
void UnaryExpression::dump(int indent) const
|
|
|
|
{
|
|
|
|
const char* op_string = nullptr;
|
|
|
|
switch (m_op) {
|
2020-03-14 20:43:35 +02:00
|
|
|
case UnaryOp::BitwiseNot:
|
2020-03-08 23:27:18 +02:00
|
|
|
op_string = "~";
|
|
|
|
break;
|
2020-03-09 19:04:44 +02:00
|
|
|
case UnaryOp::Not:
|
|
|
|
op_string = "!";
|
|
|
|
break;
|
2020-03-18 06:33:32 +11:00
|
|
|
case UnaryOp::Typeof:
|
|
|
|
op_string = "typeof ";
|
|
|
|
break;
|
2020-03-08 23:27:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
print_indent(indent);
|
|
|
|
printf("%s\n", class_name());
|
|
|
|
print_indent(indent + 1);
|
|
|
|
printf("%s\n", op_string);
|
|
|
|
m_lhs->dump(indent + 1);
|
|
|
|
}
|
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
void CallExpression::dump(int indent) const
|
|
|
|
{
|
2020-03-12 23:02:41 +01:00
|
|
|
ASTNode::dump(indent);
|
|
|
|
m_callee->dump(indent + 1);
|
2020-03-12 19:34:59 +01:00
|
|
|
for (auto& argument : m_arguments)
|
|
|
|
argument.dump(indent + 1);
|
2020-03-07 19:42:11 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 12:19:11 +01:00
|
|
|
void StringLiteral::dump(int indent) const
|
2020-03-07 19:42:11 +01:00
|
|
|
{
|
|
|
|
print_indent(indent);
|
2020-03-12 12:19:11 +01:00
|
|
|
printf("StringLiteral \"%s\"\n", m_value.characters());
|
|
|
|
}
|
|
|
|
|
|
|
|
void NumericLiteral::dump(int indent) const
|
|
|
|
{
|
|
|
|
print_indent(indent);
|
2020-03-12 19:36:24 +01:00
|
|
|
printf("NumericLiteral %g\n", m_value);
|
2020-03-12 12:19:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BooleanLiteral::dump(int indent) const
|
|
|
|
{
|
|
|
|
print_indent(indent);
|
|
|
|
printf("BooleanLiteral %s\n", m_value ? "true" : "false");
|
2020-03-07 19:42:11 +01:00
|
|
|
}
|
|
|
|
|
2020-03-15 23:32:34 +02:00
|
|
|
void UndefinedLiteral::dump(int indent) const
|
|
|
|
{
|
|
|
|
print_indent(indent);
|
|
|
|
printf("undefined\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void NullLiteral::dump(int indent) const
|
|
|
|
{
|
|
|
|
print_indent(indent);
|
|
|
|
printf("null\n");
|
|
|
|
}
|
|
|
|
|
2020-03-19 11:12:08 +01:00
|
|
|
void FunctionNode::dump(int indent, const char* class_name) const
|
2020-03-07 19:42:11 +01:00
|
|
|
{
|
2020-03-12 19:22:13 +08:00
|
|
|
StringBuilder parameters_builder;
|
2020-03-20 14:40:08 +01:00
|
|
|
parameters_builder.join(',', parameters());
|
2020-03-12 19:22:13 +08:00
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
print_indent(indent);
|
2020-03-19 11:12:08 +01:00
|
|
|
printf("%s '%s(%s)'\n", class_name, name().characters(), parameters_builder.build().characters());
|
2020-03-07 19:42:11 +01:00
|
|
|
body().dump(indent + 1);
|
|
|
|
}
|
|
|
|
|
2020-03-19 11:12:08 +01:00
|
|
|
void FunctionDeclaration::dump(int indent) const
|
|
|
|
{
|
|
|
|
FunctionNode::dump(indent, class_name());
|
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionExpression::dump(int indent) const
|
|
|
|
{
|
|
|
|
FunctionNode::dump(indent, class_name());
|
|
|
|
}
|
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
void ReturnStatement::dump(int indent) const
|
|
|
|
{
|
|
|
|
ASTNode::dump(indent);
|
2020-03-11 19:27:43 +01:00
|
|
|
if (argument())
|
|
|
|
argument()->dump(indent + 1);
|
2020-03-07 19:42:11 +01:00
|
|
|
}
|
|
|
|
|
2020-03-08 07:58:58 +02:00
|
|
|
void IfStatement::dump(int indent) const
|
|
|
|
{
|
|
|
|
ASTNode::dump(indent);
|
|
|
|
|
|
|
|
print_indent(indent);
|
|
|
|
printf("If\n");
|
|
|
|
predicate().dump(indent + 1);
|
|
|
|
consequent().dump(indent + 1);
|
2020-03-21 18:40:17 +01:00
|
|
|
if (alternate()) {
|
|
|
|
print_indent(indent);
|
|
|
|
printf("Else\n");
|
|
|
|
alternate()->dump(indent + 1);
|
|
|
|
}
|
2020-03-08 07:58:58 +02:00
|
|
|
}
|
|
|
|
|
2020-03-09 03:22:21 +08:00
|
|
|
void WhileStatement::dump(int indent) const
|
|
|
|
{
|
|
|
|
ASTNode::dump(indent);
|
|
|
|
|
|
|
|
print_indent(indent);
|
|
|
|
printf("While\n");
|
|
|
|
predicate().dump(indent + 1);
|
|
|
|
body().dump(indent + 1);
|
|
|
|
}
|
|
|
|
|
2020-03-12 23:12:12 +11:00
|
|
|
void ForStatement::dump(int indent) const
|
|
|
|
{
|
|
|
|
ASTNode::dump(indent);
|
|
|
|
|
|
|
|
print_indent(indent);
|
|
|
|
printf("For\n");
|
|
|
|
if (init())
|
|
|
|
init()->dump(indent + 1);
|
|
|
|
if (test())
|
|
|
|
test()->dump(indent + 1);
|
|
|
|
if (update())
|
|
|
|
update()->dump(indent + 1);
|
|
|
|
body().dump(indent + 1);
|
|
|
|
}
|
|
|
|
|
2020-03-09 21:13:55 +01:00
|
|
|
Value Identifier::execute(Interpreter& interpreter) const
|
|
|
|
{
|
2020-03-27 12:54:18 +01:00
|
|
|
auto variable = interpreter.get_variable(string());
|
|
|
|
if (!variable.has_value())
|
2020-03-24 22:03:50 +01:00
|
|
|
return interpreter.throw_exception<Error>("ReferenceError", String::format("'%s' not known", string().characters()));
|
2020-03-27 12:54:18 +01:00
|
|
|
return variable.value();
|
2020-03-09 21:13:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Identifier::dump(int indent) const
|
|
|
|
{
|
|
|
|
print_indent(indent);
|
|
|
|
printf("Identifier \"%s\"\n", m_string.characters());
|
|
|
|
}
|
|
|
|
|
|
|
|
Value AssignmentExpression::execute(Interpreter& interpreter) const
|
|
|
|
{
|
2020-03-19 17:39:13 +01:00
|
|
|
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) {
|
2020-03-28 22:48:35 +01:00
|
|
|
if (auto* object = static_cast<const MemberExpression&>(*m_lhs).object().execute(interpreter).to_object(interpreter.heap())) {
|
|
|
|
auto property_name = static_cast<const MemberExpression&>(*m_lhs).computed_property_name(interpreter);
|
|
|
|
object->put(property_name, value);
|
|
|
|
}
|
2020-03-19 17:39:13 +01:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2020-03-09 21:13:55 +01:00
|
|
|
auto rhs_result = m_rhs->execute(interpreter);
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-03-09 21:13:55 +01:00
|
|
|
|
|
|
|
switch (m_op) {
|
2020-03-12 13:54:56 +01:00
|
|
|
case AssignmentOp::Assignment:
|
2020-03-09 21:13:55 +01:00
|
|
|
break;
|
2020-03-12 13:54:56 +01:00
|
|
|
case AssignmentOp::AdditionAssignment:
|
2020-03-12 23:09:15 +11:00
|
|
|
rhs_result = add(m_lhs->execute(interpreter), rhs_result);
|
|
|
|
break;
|
2020-03-12 13:54:56 +01:00
|
|
|
case AssignmentOp::SubtractionAssignment:
|
2020-03-12 23:09:15 +11:00
|
|
|
rhs_result = sub(m_lhs->execute(interpreter), rhs_result);
|
|
|
|
break;
|
2020-03-12 13:54:56 +01:00
|
|
|
case AssignmentOp::MultiplicationAssignment:
|
2020-03-12 23:09:15 +11:00
|
|
|
rhs_result = mul(m_lhs->execute(interpreter), rhs_result);
|
|
|
|
break;
|
2020-03-12 13:54:56 +01:00
|
|
|
case AssignmentOp::DivisionAssignment:
|
2020-03-12 23:09:15 +11:00
|
|
|
rhs_result = div(m_lhs->execute(interpreter), rhs_result);
|
|
|
|
break;
|
2020-03-09 21:13:55 +01:00
|
|
|
}
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-03-19 17:39:13 +01:00
|
|
|
commit(rhs_result);
|
2020-03-09 21:13:55 +01:00
|
|
|
return rhs_result;
|
|
|
|
}
|
|
|
|
|
2020-03-12 13:45:45 +02:00
|
|
|
Value UpdateExpression::execute(Interpreter& interpreter) const
|
|
|
|
{
|
|
|
|
ASSERT(m_argument->is_identifier());
|
|
|
|
auto name = static_cast<const Identifier&>(*m_argument).string();
|
|
|
|
|
2020-03-27 12:54:18 +01:00
|
|
|
auto previous_variable = interpreter.get_variable(name);
|
|
|
|
ASSERT(previous_variable.has_value());
|
|
|
|
auto previous_value = previous_variable.value();
|
2020-03-12 13:45:45 +02:00
|
|
|
ASSERT(previous_value.is_number());
|
|
|
|
|
2020-03-14 20:44:57 +02:00
|
|
|
int op_result = 0;
|
2020-03-12 13:45:45 +02:00
|
|
|
switch (m_op) {
|
|
|
|
case UpdateOp::Increment:
|
2020-03-14 20:44:57 +02:00
|
|
|
op_result = 1;
|
2020-03-12 13:45:45 +02:00
|
|
|
break;
|
|
|
|
case UpdateOp::Decrement:
|
2020-03-14 20:44:57 +02:00
|
|
|
op_result = -1;
|
2020-03-12 14:24:34 +02:00
|
|
|
break;
|
2020-03-12 13:45:45 +02:00
|
|
|
}
|
|
|
|
|
2020-03-14 20:44:57 +02:00
|
|
|
interpreter.set_variable(name, Value(previous_value.as_double() + op_result));
|
|
|
|
|
|
|
|
if (m_prefixed)
|
|
|
|
return JS::Value(previous_value.as_double() + op_result);
|
|
|
|
|
2020-03-12 13:45:45 +02:00
|
|
|
return previous_value;
|
|
|
|
}
|
|
|
|
|
2020-03-09 21:13:55 +01:00
|
|
|
void AssignmentExpression::dump(int indent) const
|
|
|
|
{
|
|
|
|
const char* op_string = nullptr;
|
|
|
|
switch (m_op) {
|
2020-03-12 13:54:56 +01:00
|
|
|
case AssignmentOp::Assignment:
|
2020-03-09 21:13:55 +01:00
|
|
|
op_string = "=";
|
|
|
|
break;
|
2020-03-12 13:54:56 +01:00
|
|
|
case AssignmentOp::AdditionAssignment:
|
2020-03-12 23:09:15 +11:00
|
|
|
op_string = "+=";
|
|
|
|
break;
|
2020-03-12 13:54:56 +01:00
|
|
|
case AssignmentOp::SubtractionAssignment:
|
2020-03-12 23:09:15 +11:00
|
|
|
op_string = "-=";
|
|
|
|
break;
|
2020-03-12 13:54:56 +01:00
|
|
|
case AssignmentOp::MultiplicationAssignment:
|
2020-03-12 23:09:15 +11:00
|
|
|
op_string = "*=";
|
|
|
|
break;
|
2020-03-12 13:54:56 +01:00
|
|
|
case AssignmentOp::DivisionAssignment:
|
2020-03-12 23:09:15 +11:00
|
|
|
op_string = "/=";
|
|
|
|
break;
|
2020-03-09 21:13:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ASTNode::dump(indent);
|
|
|
|
print_indent(indent + 1);
|
|
|
|
printf("%s\n", op_string);
|
|
|
|
m_lhs->dump(indent + 1);
|
|
|
|
m_rhs->dump(indent + 1);
|
|
|
|
}
|
|
|
|
|
2020-03-12 13:45:45 +02:00
|
|
|
void UpdateExpression::dump(int indent) const
|
|
|
|
{
|
|
|
|
const char* op_string = nullptr;
|
|
|
|
switch (m_op) {
|
|
|
|
case UpdateOp::Increment:
|
|
|
|
op_string = "++";
|
|
|
|
break;
|
|
|
|
case UpdateOp::Decrement:
|
|
|
|
op_string = "--";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTNode::dump(indent);
|
|
|
|
print_indent(indent + 1);
|
2020-03-14 20:44:57 +02:00
|
|
|
if (m_prefixed)
|
|
|
|
printf("%s\n", op_string);
|
2020-03-12 13:45:45 +02:00
|
|
|
m_argument->dump(indent + 1);
|
2020-03-14 20:44:57 +02:00
|
|
|
if (!m_prefixed) {
|
|
|
|
print_indent(indent + 1);
|
|
|
|
printf("%s\n", op_string);
|
|
|
|
}
|
2020-03-12 13:45:45 +02:00
|
|
|
}
|
|
|
|
|
2020-03-09 21:13:55 +01:00
|
|
|
Value VariableDeclaration::execute(Interpreter& interpreter) const
|
|
|
|
{
|
2020-03-11 21:09:20 +02:00
|
|
|
interpreter.declare_variable(name().string(), m_declaration_type);
|
2020-03-09 21:13:55 +01:00
|
|
|
if (m_initializer) {
|
|
|
|
auto initalizer_result = m_initializer->execute(interpreter);
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-03-15 23:57:54 +02:00
|
|
|
interpreter.set_variable(name().string(), initalizer_result, true);
|
2020-03-09 21:13:55 +01:00
|
|
|
}
|
2020-03-11 21:09:20 +02:00
|
|
|
|
2020-03-26 12:36:52 +01:00
|
|
|
return {};
|
2020-03-09 21:13:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void VariableDeclaration::dump(int indent) const
|
|
|
|
{
|
2020-03-12 14:24:34 +02:00
|
|
|
const char* declaration_type_string = nullptr;
|
2020-03-11 21:09:20 +02:00
|
|
|
switch (m_declaration_type) {
|
|
|
|
case DeclarationType::Let:
|
2020-03-12 14:24:34 +02:00
|
|
|
declaration_type_string = "Let";
|
2020-03-11 21:09:20 +02:00
|
|
|
break;
|
|
|
|
case DeclarationType::Var:
|
2020-03-12 14:24:34 +02:00
|
|
|
declaration_type_string = "Var";
|
|
|
|
break;
|
|
|
|
case DeclarationType::Const:
|
|
|
|
declaration_type_string = "Const";
|
2020-03-11 21:09:20 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-03-09 21:13:55 +01:00
|
|
|
ASTNode::dump(indent);
|
2020-03-11 21:09:20 +02:00
|
|
|
print_indent(indent + 1);
|
2020-03-12 14:24:34 +02:00
|
|
|
printf("%s\n", declaration_type_string);
|
2020-03-09 21:13:55 +01:00
|
|
|
m_name->dump(indent + 1);
|
|
|
|
if (m_initializer)
|
|
|
|
m_initializer->dump(indent + 1);
|
|
|
|
}
|
|
|
|
|
2020-03-09 21:28:31 +01:00
|
|
|
void ObjectExpression::dump(int indent) const
|
|
|
|
{
|
|
|
|
ASTNode::dump(indent);
|
2020-03-21 13:05:43 +01:00
|
|
|
for (auto it : m_properties) {
|
2020-03-21 02:29:00 +02:00
|
|
|
print_indent(indent + 1);
|
2020-03-21 13:05:43 +01:00
|
|
|
printf("%s: ", it.key.characters());
|
|
|
|
it.value->dump(0);
|
2020-03-21 02:29:00 +02:00
|
|
|
}
|
2020-03-09 21:28:31 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 19:27:43 +01:00
|
|
|
void ExpressionStatement::dump(int indent) const
|
|
|
|
{
|
|
|
|
ASTNode::dump(indent);
|
|
|
|
m_expression->dump(indent + 1);
|
|
|
|
}
|
|
|
|
|
2020-03-09 21:28:31 +01:00
|
|
|
Value ObjectExpression::execute(Interpreter& interpreter) const
|
|
|
|
{
|
2020-03-21 02:29:00 +02:00
|
|
|
auto object = interpreter.heap().allocate<Object>();
|
2020-03-27 15:35:35 +01:00
|
|
|
for (auto it : m_properties) {
|
|
|
|
auto value = it.value->execute(interpreter);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
object->put(it.key, value);
|
|
|
|
}
|
2020-03-21 02:29:00 +02:00
|
|
|
return object;
|
2020-03-09 21:28:31 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 18:58:19 +01:00
|
|
|
void MemberExpression::dump(int indent) const
|
|
|
|
{
|
2020-03-20 20:51:03 +01:00
|
|
|
print_indent(indent);
|
|
|
|
printf("%s (computed=%s)\n", class_name(), is_computed() ? "true" : "false");
|
2020-03-11 18:58:19 +01:00
|
|
|
m_object->dump(indent + 1);
|
|
|
|
m_property->dump(indent + 1);
|
|
|
|
}
|
|
|
|
|
2020-03-22 11:07:55 +01:00
|
|
|
FlyString MemberExpression::computed_property_name(Interpreter& interpreter) const
|
2020-03-20 20:51:03 +01:00
|
|
|
{
|
|
|
|
if (!is_computed()) {
|
|
|
|
ASSERT(m_property->is_identifier());
|
|
|
|
return static_cast<const Identifier&>(*m_property).string();
|
|
|
|
}
|
|
|
|
return m_property->execute(interpreter).to_string();
|
|
|
|
}
|
|
|
|
|
2020-03-11 18:58:19 +01:00
|
|
|
Value MemberExpression::execute(Interpreter& interpreter) const
|
|
|
|
{
|
2020-03-28 22:48:35 +01:00
|
|
|
auto* object_result = m_object->execute(interpreter).to_object(interpreter.heap());
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-03-28 22:48:35 +01:00
|
|
|
auto result = object_result->get(computed_property_name(interpreter));
|
2020-03-27 12:54:18 +01:00
|
|
|
return result.value_or({});
|
2020-03-11 18:58:19 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 12:19:11 +01:00
|
|
|
Value StringLiteral::execute(Interpreter& interpreter) const
|
|
|
|
{
|
2020-03-12 19:57:40 +01:00
|
|
|
return js_string(interpreter.heap(), m_value);
|
2020-03-12 12:19:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Value NumericLiteral::execute(Interpreter&) const
|
|
|
|
{
|
|
|
|
return Value(m_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value BooleanLiteral::execute(Interpreter&) const
|
|
|
|
{
|
|
|
|
return Value(m_value);
|
|
|
|
}
|
|
|
|
|
2020-03-15 23:32:34 +02:00
|
|
|
Value UndefinedLiteral::execute(Interpreter&) const
|
|
|
|
{
|
2020-03-26 12:36:52 +01:00
|
|
|
return {};
|
2020-03-15 23:32:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Value NullLiteral::execute(Interpreter&) const
|
|
|
|
{
|
|
|
|
return js_null();
|
|
|
|
}
|
|
|
|
|
2020-03-20 20:29:57 +01:00
|
|
|
void ArrayExpression::dump(int indent) const
|
|
|
|
{
|
|
|
|
ASTNode::dump(indent);
|
|
|
|
for (auto& element : m_elements) {
|
|
|
|
element.dump(indent + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Value ArrayExpression::execute(Interpreter& interpreter) const
|
|
|
|
{
|
|
|
|
auto* array = interpreter.heap().allocate<Array>();
|
|
|
|
for (auto& element : m_elements) {
|
2020-03-27 15:35:35 +01:00
|
|
|
auto value = element.execute(interpreter);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
array->push(value);
|
2020-03-20 20:29:57 +01:00
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2020-03-24 14:03:55 +01:00
|
|
|
void TryStatement::dump(int indent) const
|
|
|
|
{
|
|
|
|
ASTNode::dump(indent);
|
|
|
|
print_indent(indent);
|
|
|
|
printf("(Block)\n");
|
|
|
|
block().dump(indent + 1);
|
|
|
|
|
|
|
|
if (handler()) {
|
|
|
|
print_indent(indent);
|
|
|
|
printf("(Handler)\n");
|
|
|
|
handler()->dump(indent + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (finalizer()) {
|
|
|
|
print_indent(indent);
|
|
|
|
printf("(Finalizer)\n");
|
|
|
|
finalizer()->dump(indent + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CatchClause::dump(int indent) const
|
|
|
|
{
|
|
|
|
print_indent(indent);
|
|
|
|
printf("CatchClause");
|
|
|
|
if (!m_parameter.is_null())
|
|
|
|
printf(" (%s)", m_parameter.characters());
|
|
|
|
printf("\n");
|
|
|
|
body().dump(indent + 1);
|
|
|
|
}
|
|
|
|
|
2020-03-24 22:03:50 +01:00
|
|
|
void ThrowStatement::dump(int indent) const
|
|
|
|
{
|
|
|
|
ASTNode::dump(indent);
|
|
|
|
argument().dump(indent + 1);
|
|
|
|
}
|
|
|
|
|
2020-03-24 14:37:39 +01:00
|
|
|
Value TryStatement::execute(Interpreter& interpreter) const
|
2020-03-24 14:03:55 +01:00
|
|
|
{
|
2020-03-24 14:37:39 +01:00
|
|
|
interpreter.run(block(), {}, ScopeType::Try);
|
|
|
|
if (auto* exception = interpreter.exception()) {
|
|
|
|
if (m_handler) {
|
|
|
|
interpreter.clear_exception();
|
2020-03-24 22:03:50 +01:00
|
|
|
Vector<Argument> arguments { { m_handler->parameter(), exception->value() } };
|
2020-03-24 14:37:39 +01:00
|
|
|
interpreter.run(m_handler->body(), move(arguments));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_finalizer)
|
|
|
|
m_finalizer->execute(interpreter);
|
|
|
|
|
2020-03-24 14:03:55 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
Value CatchClause::execute(Interpreter&) const
|
|
|
|
{
|
2020-03-24 14:37:39 +01:00
|
|
|
// NOTE: CatchClause execution is handled by TryStatement.
|
|
|
|
ASSERT_NOT_REACHED();
|
2020-03-24 14:03:55 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-03-27 15:35:35 +01:00
|
|
|
Value ThrowStatement::execute(Interpreter& interpreter) const
|
2020-03-24 22:03:50 +01:00
|
|
|
{
|
2020-03-27 15:35:35 +01:00
|
|
|
auto value = m_argument->execute(interpreter);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
return interpreter.throw_exception(value);
|
2020-03-24 22:03:50 +01:00
|
|
|
}
|
|
|
|
|
2020-03-29 13:09:54 +02:00
|
|
|
Value SwitchStatement::execute(Interpreter& interpreter) const
|
|
|
|
{
|
2020-03-29 14:34:25 +02:00
|
|
|
auto discriminant_result = m_discriminant->execute(interpreter);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
bool falling_through = false;
|
|
|
|
|
|
|
|
for (auto& switch_case : m_cases) {
|
|
|
|
if (!falling_through && switch_case.test()) {
|
|
|
|
auto test_result = switch_case.test()->execute(interpreter);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
if (!eq(discriminant_result, test_result).to_boolean())
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
falling_through = true;
|
|
|
|
|
|
|
|
for (auto& statement : switch_case.consequent()) {
|
|
|
|
statement.execute(interpreter);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
if (interpreter.should_unwind())
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 13:09:54 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
Value SwitchCase::execute(Interpreter& interpreter) const
|
|
|
|
{
|
|
|
|
(void)interpreter;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
Value BreakStatement::execute(Interpreter& interpreter) const
|
|
|
|
{
|
2020-03-29 14:34:25 +02:00
|
|
|
interpreter.unwind(ScopeType::Breakable);
|
2020-03-29 13:09:54 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void SwitchStatement::dump(int indent) const
|
|
|
|
{
|
|
|
|
ASTNode::dump(indent);
|
|
|
|
m_discriminant->dump(indent + 1);
|
|
|
|
for (auto& switch_case : m_cases) {
|
|
|
|
switch_case.dump(indent + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SwitchCase::dump(int indent) const
|
|
|
|
{
|
|
|
|
ASTNode::dump(indent);
|
|
|
|
print_indent(indent);
|
|
|
|
if (m_test) {
|
|
|
|
printf("(Test)\n");
|
|
|
|
m_test->dump(indent + 1);
|
|
|
|
} else {
|
|
|
|
printf("(Default)\n");
|
|
|
|
}
|
|
|
|
print_indent(indent);
|
|
|
|
printf("(Consequent)\n");
|
|
|
|
int i = 0;
|
|
|
|
for (auto& statement : m_consequent) {
|
|
|
|
print_indent(indent);
|
|
|
|
printf("[%d]\n", i++);
|
|
|
|
statement.dump(indent + 1);
|
|
|
|
}
|
|
|
|
}
|
2020-03-07 19:42:11 +01:00
|
|
|
}
|