2020-03-07 19:42:11 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2020-04-23 15:43:10 +01:00
|
|
|
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
|
2020-03-07 19:42:11 +01:00
|
|
|
* 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>
|
2020-04-05 00:24:32 +02:00
|
|
|
#include <AK/ScopeGuard.h>
|
2020-03-12 19:22:13 +08:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-06-06 01:14:10 +01:00
|
|
|
#include <LibCrypto/BigInt/SignedBigInteger.h>
|
2020-03-07 19:42:11 +01:00
|
|
|
#include <LibJS/AST.h>
|
|
|
|
#include <LibJS/Interpreter.h>
|
2020-05-21 17:28:28 -07:00
|
|
|
#include <LibJS/Runtime/Accessor.h>
|
2020-03-20 20:29:57 +01:00
|
|
|
#include <LibJS/Runtime/Array.h>
|
2020-06-06 01:14:10 +01:00
|
|
|
#include <LibJS/Runtime/BigInt.h>
|
2020-03-24 14:37:39 +01:00
|
|
|
#include <LibJS/Runtime/Error.h>
|
2020-04-08 11:05:38 +02:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-04-19 17:24:56 +02:00
|
|
|
#include <LibJS/Runtime/MarkedValueList.h>
|
2020-04-01 18:31:24 +01:00
|
|
|
#include <LibJS/Runtime/NativeFunction.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Runtime/PrimitiveString.h>
|
2020-04-27 12:10:16 +02:00
|
|
|
#include <LibJS/Runtime/Reference.h>
|
2020-06-03 16:05:49 -07:00
|
|
|
#include <LibJS/Runtime/RegExpObject.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Runtime/ScriptFunction.h>
|
2020-04-27 21:52:47 -07:00
|
|
|
#include <LibJS/Runtime/Shape.h>
|
2020-04-27 14:01:04 +01:00
|
|
|
#include <LibJS/Runtime/StringObject.h>
|
2020-03-07 19:42:11 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2020-05-02 20:28:48 +01:00
|
|
|
static void update_function_name(Value& value, const FlyString& name)
|
|
|
|
{
|
|
|
|
if (!value.is_object())
|
|
|
|
return;
|
|
|
|
auto& object = value.as_object();
|
|
|
|
if (object.is_function()) {
|
2020-06-01 16:58:54 +03:00
|
|
|
auto& function = static_cast<Function&>(object);
|
|
|
|
if (function.is_script_function() && function.name().is_empty())
|
|
|
|
static_cast<ScriptFunction&>(function).set_name(name);
|
2020-05-02 20:28:48 +01:00
|
|
|
} else if (object.is_array()) {
|
|
|
|
auto& array = static_cast<Array&>(object);
|
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
|
|
|
for (auto& entry : array.indexed_properties().values_unordered())
|
|
|
|
update_function_name(entry.value, name);
|
2020-05-02 20:28:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 21:25:16 +02:00
|
|
|
Value ScopeNode::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-03-07 19:42:11 +01:00
|
|
|
{
|
2020-06-08 21:25:16 +02:00
|
|
|
return interpreter.run(global_object, *this);
|
2020-03-07 19:42:11 +01:00
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value FunctionDeclaration::execute(Interpreter&, GlobalObject&) const
|
2020-03-07 19:42:11 +01:00
|
|
|
{
|
2020-04-06 20:24:45 +02:00
|
|
|
return js_undefined();
|
2020-03-07 19:42:11 +01:00
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value FunctionExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-03-19 11:12:08 +01:00
|
|
|
{
|
2020-06-08 20:57:54 +02:00
|
|
|
return ScriptFunction::create(global_object, name(), body(), parameters(), function_length(), interpreter.current_environment(), m_is_arrow_function);
|
2020-03-19 11:12:08 +01:00
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value ExpressionStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-03-11 19:27:43 +01:00
|
|
|
{
|
2020-06-08 20:57:54 +02:00
|
|
|
return m_expression->execute(interpreter, global_object);
|
2020-03-11 19:27:43 +01:00
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
CallExpression::ThisAndCallee CallExpression::compute_this_and_callee(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-04-01 18:51:27 +02:00
|
|
|
{
|
|
|
|
if (is_new_expression()) {
|
|
|
|
// Computing |this| is irrelevant for "new" expression.
|
2020-06-08 20:57:54 +02:00
|
|
|
return { js_undefined(), m_callee->execute(interpreter, global_object) };
|
2020-04-01 18:51:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_callee->is_member_expression()) {
|
|
|
|
auto& member_expression = static_cast<const MemberExpression&>(*m_callee);
|
2020-06-08 20:57:54 +02:00
|
|
|
auto object_value = member_expression.object().execute(interpreter, global_object);
|
2020-04-01 18:51:27 +02:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-05-17 21:38:47 +01:00
|
|
|
auto* this_value = object_value.to_object(interpreter);
|
2020-04-01 18:51:27 +02:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-08 20:57:54 +02:00
|
|
|
auto callee = this_value->get(member_expression.computed_property_name(interpreter, global_object)).value_or(js_undefined());
|
2020-04-01 18:51:27 +02:00
|
|
|
return { this_value, callee };
|
|
|
|
}
|
2020-06-08 20:57:54 +02:00
|
|
|
return { &interpreter.global_object(), m_callee->execute(interpreter, global_object) };
|
2020-04-01 18:51:27 +02:00
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value CallExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-03-07 19:42:11 +01:00
|
|
|
{
|
2020-06-08 20:57:54 +02:00
|
|
|
auto [this_value, callee] = compute_this_and_callee(interpreter, global_object);
|
2020-03-24 14:37:39 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
|
2020-04-06 20:24:45 +02:00
|
|
|
ASSERT(!callee.is_empty());
|
|
|
|
|
2020-05-06 11:52:53 +01:00
|
|
|
if (!callee.is_function()
|
2020-04-19 14:50:27 +02:00
|
|
|
|| (is_new_expression() && (callee.as_object().is_native_function() && !static_cast<NativeFunction&>(callee.as_object()).has_constructor()))) {
|
2020-04-19 01:12:51 +01:00
|
|
|
String error_message;
|
|
|
|
auto call_type = is_new_expression() ? "constructor" : "function";
|
|
|
|
if (m_callee->is_identifier() || m_callee->is_member_expression()) {
|
|
|
|
String expression_string;
|
2020-06-09 22:48:01 -07:00
|
|
|
if (m_callee->is_identifier()) {
|
2020-04-19 01:12:51 +01:00
|
|
|
expression_string = static_cast<const Identifier&>(*m_callee).string();
|
2020-06-09 22:48:01 -07:00
|
|
|
} else {
|
2020-04-19 01:12:51 +01:00
|
|
|
expression_string = static_cast<const MemberExpression&>(*m_callee).to_string_approximation();
|
2020-06-09 22:48:01 -07:00
|
|
|
}
|
|
|
|
return interpreter.throw_exception<TypeError>(ErrorType::IsNotAEvaluatedFrom, callee.to_string_without_side_effects().characters(), call_type, expression_string.characters());
|
2020-04-19 01:12:51 +01:00
|
|
|
} else {
|
2020-06-09 22:48:01 -07:00
|
|
|
return interpreter.throw_exception<TypeError>(ErrorType::IsNotA, callee.to_string_without_side_effects().characters(), call_type);
|
2020-04-19 01:12:51 +01:00
|
|
|
}
|
2020-04-01 18:31:24 +01:00
|
|
|
}
|
|
|
|
|
2020-05-06 11:52:53 +01:00
|
|
|
auto& function = callee.as_function();
|
2020-03-12 19:53:31 +01:00
|
|
|
|
2020-04-19 17:24:56 +02:00
|
|
|
MarkedValueList arguments(interpreter.heap());
|
2020-04-19 15:03:02 -05:00
|
|
|
arguments.values().append(function.bound_arguments());
|
|
|
|
|
2020-03-24 14:37:39 +01:00
|
|
|
for (size_t i = 0; i < m_arguments.size(); ++i) {
|
2020-06-08 20:57:54 +02:00
|
|
|
auto value = m_arguments[i].value->execute(interpreter, global_object);
|
2020-03-24 14:37:39 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-05-05 22:36:24 -07:00
|
|
|
if (m_arguments[i].is_spread) {
|
|
|
|
// FIXME: Support generic iterables
|
|
|
|
if (value.is_string()) {
|
|
|
|
for (auto ch : value.as_string().string())
|
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
|
|
|
arguments.append(Value(js_string(interpreter, String::format("%c", ch))));
|
2020-05-05 22:36:24 -07:00
|
|
|
} else if (value.is_object() && value.as_object().is_array()) {
|
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
|
|
|
auto& array = static_cast<Array&>(value.as_object());
|
|
|
|
for (auto& entry : array.indexed_properties()) {
|
|
|
|
arguments.append(entry.value_and_attributes(&array).value);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
}
|
2020-05-05 22:36:24 -07:00
|
|
|
} else if (value.is_object() && value.as_object().is_string_object()) {
|
|
|
|
for (auto ch : static_cast<const StringObject&>(value.as_object()).primitive_string().string())
|
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
|
|
|
arguments.append(Value(js_string(interpreter, String::format("%c", ch))));
|
2020-05-05 22:36:24 -07:00
|
|
|
} else {
|
2020-06-09 22:48:01 -07:00
|
|
|
interpreter.throw_exception<TypeError>(ErrorType::NotIterable, value.to_string_without_side_effects().characters());
|
2020-05-05 22:36:24 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
arguments.append(value);
|
|
|
|
}
|
2020-03-24 14:37:39 +01:00
|
|
|
}
|
2020-03-12 19:22:13 +08:00
|
|
|
|
2020-04-01 22:28:48 -05:00
|
|
|
auto& call_frame = interpreter.push_call_frame();
|
2020-04-11 12:56:20 +01:00
|
|
|
call_frame.function_name = function.name();
|
2020-04-19 17:24:56 +02:00
|
|
|
call_frame.arguments = arguments.values();
|
2020-04-15 21:58:22 +02:00
|
|
|
call_frame.environment = function.create_environment();
|
2020-04-01 22:28:48 -05:00
|
|
|
|
2020-03-28 16:33:52 +01:00
|
|
|
Object* new_object = nullptr;
|
2020-04-01 18:31:24 +01:00
|
|
|
Value result;
|
2020-03-28 16:33:52 +01:00
|
|
|
if (is_new_expression()) {
|
2020-04-18 10:27:57 +02:00
|
|
|
new_object = Object::create_empty(interpreter, interpreter.global_object());
|
2020-04-01 22:18:47 +02:00
|
|
|
auto prototype = function.get("prototype");
|
2020-06-07 10:53:14 -07:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
if (prototype.is_object()) {
|
2020-04-25 18:43:34 +02:00
|
|
|
new_object->set_prototype(&prototype.as_object());
|
2020-06-07 10:53:14 -07:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
}
|
2020-03-28 16:33:52 +01:00
|
|
|
call_frame.this_value = new_object;
|
2020-04-01 22:18:47 +02:00
|
|
|
result = function.construct(interpreter);
|
2020-03-28 16:33:52 +01:00
|
|
|
} else {
|
2020-04-19 14:51:17 -05:00
|
|
|
call_frame.this_value = function.bound_this().value_or(this_value);
|
2020-04-01 22:18:47 +02:00
|
|
|
result = function.call(interpreter);
|
2020-03-24 14:37:39 +01:00
|
|
|
}
|
2020-03-15 15:01:10 +01:00
|
|
|
|
2020-04-15 21:58:22 +02:00
|
|
|
interpreter.pop_call_frame();
|
|
|
|
|
2020-04-06 20:24:45 +02:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value ReturnStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-03-07 19:42:11 +01:00
|
|
|
{
|
2020-06-08 20:57:54 +02:00
|
|
|
auto value = argument() ? argument()->execute(interpreter, global_object) : 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-06-08 20:57:54 +02:00
|
|
|
Value IfStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-03-08 07:58:58 +02:00
|
|
|
{
|
2020-06-08 20:57:54 +02:00
|
|
|
auto predicate_result = m_predicate->execute(interpreter, global_object);
|
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-06-08 21:25:16 +02:00
|
|
|
return interpreter.run(global_object, *m_consequent);
|
2020-03-21 18:40:17 +01:00
|
|
|
|
|
|
|
if (m_alternate)
|
2020-06-08 21:25:16 +02:00
|
|
|
return interpreter.run(global_object, *m_alternate);
|
2020-03-21 18:40:17 +01:00
|
|
|
|
2020-04-06 20:24:45 +02:00
|
|
|
return js_undefined();
|
2020-03-08 07:58:58 +02:00
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value WhileStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-03-09 03:22:21 +08:00
|
|
|
{
|
|
|
|
Value last_value = js_undefined();
|
2020-06-08 20:57:54 +02:00
|
|
|
while (m_test->execute(interpreter, global_object).to_boolean()) {
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-08 21:25:16 +02:00
|
|
|
last_value = interpreter.run(global_object, *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-06-08 20:57:54 +02:00
|
|
|
Value DoWhileStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-04-04 21:29:23 +02:00
|
|
|
{
|
|
|
|
Value last_value = js_undefined();
|
|
|
|
do {
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-08 21:25:16 +02:00
|
|
|
last_value = interpreter.run(global_object, *m_body);
|
2020-04-04 21:29:23 +02:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-08 20:57:54 +02:00
|
|
|
} while (m_test->execute(interpreter, global_object).to_boolean());
|
2020-04-04 21:29:23 +02:00
|
|
|
|
|
|
|
return last_value;
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value ForStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-03-12 23:12:12 +11:00
|
|
|
{
|
2020-03-18 11:23:53 +01:00
|
|
|
RefPtr<BlockStatement> wrapper;
|
2020-03-14 13:56:49 +02:00
|
|
|
|
2020-04-08 11:59:18 +02:00
|
|
|
if (m_init && m_init->is_variable_declaration() && static_cast<const VariableDeclaration*>(m_init.ptr())->declaration_kind() != DeclarationKind::Var) {
|
2020-03-18 11:23:53 +01:00
|
|
|
wrapper = create_ast_node<BlockStatement>();
|
2020-05-05 01:58:53 +03:00
|
|
|
NonnullRefPtrVector<VariableDeclaration> decls;
|
|
|
|
decls.append(*static_cast<const VariableDeclaration*>(m_init.ptr()));
|
|
|
|
wrapper->add_variables(decls);
|
2020-06-08 21:25:16 +02:00
|
|
|
interpreter.enter_scope(*wrapper, {}, ScopeType::Block, global_object);
|
2020-03-14 13:56:49 +02:00
|
|
|
}
|
|
|
|
|
2020-04-05 00:24:32 +02:00
|
|
|
auto wrapper_cleanup = ScopeGuard([&] {
|
|
|
|
if (wrapper)
|
|
|
|
interpreter.exit_scope(*wrapper);
|
|
|
|
});
|
|
|
|
|
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-06-08 20:57:54 +02:00
|
|
|
m_init->execute(interpreter, global_object);
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
}
|
2020-03-12 23:12:12 +11:00
|
|
|
|
|
|
|
if (m_test) {
|
2020-04-28 20:16:40 +01:00
|
|
|
while (true) {
|
2020-06-08 20:57:54 +02:00
|
|
|
auto test_result = m_test->execute(interpreter, global_object);
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-04-28 20:16:40 +01:00
|
|
|
if (!test_result.to_boolean())
|
|
|
|
break;
|
2020-06-08 21:25:16 +02:00
|
|
|
last_value = interpreter.run(global_object, *m_body);
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-04-05 00:22:42 +02:00
|
|
|
if (interpreter.should_unwind()) {
|
2020-05-28 13:36:59 -07:00
|
|
|
if (interpreter.should_unwind_until(ScopeType::Continuable, m_label)) {
|
2020-04-05 00:22:42 +02:00
|
|
|
interpreter.stop_unwind();
|
2020-05-28 13:36:59 -07:00
|
|
|
} else if (interpreter.should_unwind_until(ScopeType::Breakable, m_label)) {
|
2020-04-05 00:22:42 +02:00
|
|
|
interpreter.stop_unwind();
|
|
|
|
break;
|
|
|
|
} else {
|
2020-04-06 20:24:45 +02:00
|
|
|
return js_undefined();
|
2020-04-05 00:22:42 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-27 15:35:35 +01:00
|
|
|
if (m_update) {
|
2020-06-08 20:57:54 +02:00
|
|
|
m_update->execute(interpreter, global_object);
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
}
|
2020-03-12 23:12:12 +11:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (true) {
|
2020-06-08 21:25:16 +02:00
|
|
|
last_value = interpreter.run(global_object, *m_body);
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-04-05 00:22:42 +02:00
|
|
|
if (interpreter.should_unwind()) {
|
2020-05-28 13:36:59 -07:00
|
|
|
if (interpreter.should_unwind_until(ScopeType::Continuable, m_label)) {
|
2020-04-05 00:22:42 +02:00
|
|
|
interpreter.stop_unwind();
|
2020-05-28 13:36:59 -07:00
|
|
|
} else if (interpreter.should_unwind_until(ScopeType::Breakable, m_label)) {
|
2020-04-05 00:22:42 +02:00
|
|
|
interpreter.stop_unwind();
|
|
|
|
break;
|
|
|
|
} else {
|
2020-04-06 20:24:45 +02:00
|
|
|
return js_undefined();
|
2020-04-05 00:22:42 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-27 15:35:35 +01:00
|
|
|
if (m_update) {
|
2020-06-08 20:57:54 +02:00
|
|
|
m_update->execute(interpreter, global_object);
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
}
|
2020-03-12 23:12:12 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return last_value;
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
static FlyString variable_from_for_declaration(Interpreter& interpreter, GlobalObject& global_object, NonnullRefPtr<ASTNode> node, RefPtr<BlockStatement> wrapper)
|
2020-04-21 19:21:26 +01:00
|
|
|
{
|
|
|
|
FlyString variable_name;
|
|
|
|
if (node->is_variable_declaration()) {
|
|
|
|
auto* variable_declaration = static_cast<const VariableDeclaration*>(node.ptr());
|
|
|
|
ASSERT(!variable_declaration->declarations().is_empty());
|
|
|
|
if (variable_declaration->declaration_kind() != DeclarationKind::Var) {
|
|
|
|
wrapper = create_ast_node<BlockStatement>();
|
2020-06-08 21:25:16 +02:00
|
|
|
interpreter.enter_scope(*wrapper, {}, ScopeType::Block, global_object);
|
2020-04-21 19:21:26 +01:00
|
|
|
}
|
2020-06-08 20:57:54 +02:00
|
|
|
variable_declaration->execute(interpreter, global_object);
|
2020-04-21 19:21:26 +01:00
|
|
|
variable_name = variable_declaration->declarations().first().id().string();
|
|
|
|
} else if (node->is_identifier()) {
|
|
|
|
variable_name = static_cast<const Identifier&>(*node).string();
|
|
|
|
} else {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
return variable_name;
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value ForInStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-04-21 19:21:26 +01:00
|
|
|
{
|
|
|
|
if (!m_lhs->is_variable_declaration() && !m_lhs->is_identifier()) {
|
|
|
|
// FIXME: Implement "for (foo.bar in baz)", "for (foo[0] in bar)"
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
RefPtr<BlockStatement> wrapper;
|
2020-06-08 20:57:54 +02:00
|
|
|
auto variable_name = variable_from_for_declaration(interpreter, global_object, m_lhs, wrapper);
|
2020-04-21 19:21:26 +01:00
|
|
|
auto wrapper_cleanup = ScopeGuard([&] {
|
|
|
|
if (wrapper)
|
|
|
|
interpreter.exit_scope(*wrapper);
|
|
|
|
});
|
|
|
|
auto last_value = js_undefined();
|
2020-06-08 20:57:54 +02:00
|
|
|
auto rhs_result = m_rhs->execute(interpreter, global_object);
|
2020-04-21 19:21:26 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
auto* object = rhs_result.to_object(interpreter);
|
|
|
|
while (object) {
|
2020-06-03 14:34:52 -07:00
|
|
|
auto property_names = object->get_own_properties(*object, Object::GetOwnPropertyMode::Key, true);
|
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
|
|
|
for (auto& property_name : property_names.as_object().indexed_properties()) {
|
2020-06-08 21:25:16 +02:00
|
|
|
interpreter.set_variable(variable_name, property_name.value_and_attributes(object).value, global_object);
|
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-08 21:25:16 +02:00
|
|
|
last_value = interpreter.run(global_object, *m_body);
|
2020-04-21 19:21:26 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
if (interpreter.should_unwind()) {
|
2020-05-28 13:36:59 -07:00
|
|
|
if (interpreter.should_unwind_until(ScopeType::Continuable, m_label)) {
|
2020-04-21 19:21:26 +01:00
|
|
|
interpreter.stop_unwind();
|
2020-05-28 13:36:59 -07:00
|
|
|
} else if (interpreter.should_unwind_until(ScopeType::Breakable, m_label)) {
|
2020-04-21 19:21:26 +01:00
|
|
|
interpreter.stop_unwind();
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
return js_undefined();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
object = object->prototype();
|
2020-06-07 10:53:14 -07:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-04-21 19:21:26 +01:00
|
|
|
}
|
|
|
|
return last_value;
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value ForOfStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-04-21 19:21:26 +01:00
|
|
|
{
|
|
|
|
if (!m_lhs->is_variable_declaration() && !m_lhs->is_identifier()) {
|
|
|
|
// FIXME: Implement "for (foo.bar of baz)", "for (foo[0] of bar)"
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
RefPtr<BlockStatement> wrapper;
|
2020-06-08 20:57:54 +02:00
|
|
|
auto variable_name = variable_from_for_declaration(interpreter, global_object, m_lhs, wrapper);
|
2020-04-21 19:21:26 +01:00
|
|
|
auto wrapper_cleanup = ScopeGuard([&] {
|
|
|
|
if (wrapper)
|
|
|
|
interpreter.exit_scope(*wrapper);
|
|
|
|
});
|
|
|
|
auto last_value = js_undefined();
|
2020-06-08 20:57:54 +02:00
|
|
|
auto rhs_result = m_rhs->execute(interpreter, global_object);
|
2020-04-21 19:21:26 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
// FIXME: We need to properly implement the iterator protocol
|
|
|
|
auto is_iterable = rhs_result.is_array() || rhs_result.is_string() || (rhs_result.is_object() && rhs_result.as_object().is_string_object());
|
|
|
|
if (!is_iterable)
|
2020-06-09 22:48:01 -07:00
|
|
|
return interpreter.throw_exception<TypeError>(ErrorType::ForOfNotIterable);
|
2020-04-21 19:21:26 +01:00
|
|
|
|
|
|
|
size_t index = 0;
|
|
|
|
auto next = [&]() -> Optional<Value> {
|
|
|
|
if (rhs_result.is_array()) {
|
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
|
|
|
auto& array_elements = rhs_result.as_object().indexed_properties();
|
|
|
|
if (index < array_elements.array_like_size()) {
|
|
|
|
auto result = array_elements.get(&rhs_result.as_object(), index);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
return result.value().value;
|
|
|
|
}
|
2020-04-21 19:21:26 +01:00
|
|
|
} else if (rhs_result.is_string()) {
|
|
|
|
auto string = rhs_result.as_string().string();
|
|
|
|
if (index < string.length())
|
|
|
|
return js_string(interpreter, string.substring(index, 1));
|
|
|
|
} else if (rhs_result.is_object() && rhs_result.as_object().is_string_object()) {
|
|
|
|
auto string = static_cast<StringObject*>(&rhs_result.as_object())->primitive_string().string();
|
|
|
|
if (index < string.length())
|
|
|
|
return js_string(interpreter, string.substring(index, 1));
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
};
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
auto next_item = next();
|
|
|
|
if (!next_item.has_value())
|
|
|
|
break;
|
2020-06-08 21:25:16 +02:00
|
|
|
interpreter.set_variable(variable_name, next_item.value(), global_object);
|
|
|
|
last_value = interpreter.run(global_object, *m_body);
|
2020-04-21 19:21:26 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
if (interpreter.should_unwind()) {
|
2020-05-28 13:36:59 -07:00
|
|
|
if (interpreter.should_unwind_until(ScopeType::Continuable, m_label)) {
|
2020-04-21 19:21:26 +01:00
|
|
|
interpreter.stop_unwind();
|
2020-05-28 13:36:59 -07:00
|
|
|
} else if (interpreter.should_unwind_until(ScopeType::Breakable, m_label)) {
|
2020-04-21 19:21:26 +01:00
|
|
|
interpreter.stop_unwind();
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
return js_undefined();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++index;
|
|
|
|
}
|
|
|
|
return last_value;
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value BinaryExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-03-07 19:42:11 +01:00
|
|
|
{
|
2020-06-08 20:57:54 +02:00
|
|
|
auto lhs_result = m_lhs->execute(interpreter, global_object);
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-08 20:57:54 +02:00
|
|
|
auto rhs_result = m_rhs->execute(interpreter, global_object);
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-03-07 19:42:11 +01:00
|
|
|
|
|
|
|
switch (m_op) {
|
2020-04-05 12:56:53 +01:00
|
|
|
case BinaryOp::Addition:
|
2020-04-15 09:28:41 +02:00
|
|
|
return add(interpreter, lhs_result, rhs_result);
|
2020-04-05 12:56:53 +01:00
|
|
|
case BinaryOp::Subtraction:
|
2020-04-15 09:28:41 +02:00
|
|
|
return sub(interpreter, lhs_result, rhs_result);
|
2020-04-05 12:56:53 +01:00
|
|
|
case BinaryOp::Multiplication:
|
2020-04-15 09:28:41 +02:00
|
|
|
return mul(interpreter, lhs_result, rhs_result);
|
2020-04-05 12:56:53 +01:00
|
|
|
case BinaryOp::Division:
|
2020-04-15 09:28:41 +02:00
|
|
|
return div(interpreter, lhs_result, rhs_result);
|
2020-04-04 21:17:34 +02:00
|
|
|
case BinaryOp::Modulo:
|
2020-04-15 09:28:41 +02:00
|
|
|
return mod(interpreter, lhs_result, rhs_result);
|
2020-04-05 13:40:00 +01:00
|
|
|
case BinaryOp::Exponentiation:
|
2020-04-15 09:28:41 +02:00
|
|
|
return exp(interpreter, lhs_result, rhs_result);
|
2020-03-08 07:53:02 +02:00
|
|
|
case BinaryOp::TypedEquals:
|
2020-05-07 17:09:00 -07:00
|
|
|
return Value(strict_eq(interpreter, lhs_result, rhs_result));
|
2020-03-08 23:27:18 +02:00
|
|
|
case BinaryOp::TypedInequals:
|
2020-05-07 17:09:00 -07:00
|
|
|
return Value(!strict_eq(interpreter, lhs_result, rhs_result));
|
2020-03-16 00:23:38 +02:00
|
|
|
case BinaryOp::AbstractEquals:
|
2020-05-07 17:09:00 -07:00
|
|
|
return Value(abstract_eq(interpreter, lhs_result, rhs_result));
|
2020-03-16 00:23:38 +02:00
|
|
|
case BinaryOp::AbstractInequals:
|
2020-05-07 17:09:00 -07:00
|
|
|
return Value(!abstract_eq(interpreter, lhs_result, rhs_result));
|
2020-03-10 11:35:05 +01:00
|
|
|
case BinaryOp::GreaterThan:
|
2020-04-15 09:28:41 +02:00
|
|
|
return greater_than(interpreter, lhs_result, rhs_result);
|
2020-03-12 23:07:08 +11:00
|
|
|
case BinaryOp::GreaterThanEquals:
|
2020-04-15 09:28:41 +02:00
|
|
|
return greater_than_equals(interpreter, lhs_result, rhs_result);
|
2020-03-10 11:35:05 +01:00
|
|
|
case BinaryOp::LessThan:
|
2020-04-15 09:28:41 +02:00
|
|
|
return less_than(interpreter, lhs_result, rhs_result);
|
2020-03-12 23:07:08 +11:00
|
|
|
case BinaryOp::LessThanEquals:
|
2020-04-15 09:28:41 +02:00
|
|
|
return less_than_equals(interpreter, lhs_result, rhs_result);
|
2020-03-10 11:35:05 +01:00
|
|
|
case BinaryOp::BitwiseAnd:
|
2020-04-15 09:28:41 +02:00
|
|
|
return bitwise_and(interpreter, lhs_result, rhs_result);
|
2020-03-10 11:35:05 +01:00
|
|
|
case BinaryOp::BitwiseOr:
|
2020-04-15 09:28:41 +02:00
|
|
|
return bitwise_or(interpreter, lhs_result, rhs_result);
|
2020-03-10 11:35:05 +01:00
|
|
|
case BinaryOp::BitwiseXor:
|
2020-04-15 09:28:41 +02:00
|
|
|
return bitwise_xor(interpreter, lhs_result, rhs_result);
|
2020-03-10 11:35:05 +01:00
|
|
|
case BinaryOp::LeftShift:
|
2020-04-15 09:28:41 +02:00
|
|
|
return left_shift(interpreter, lhs_result, rhs_result);
|
2020-03-10 11:35:05 +01:00
|
|
|
case BinaryOp::RightShift:
|
2020-04-15 09:28:41 +02:00
|
|
|
return right_shift(interpreter, lhs_result, rhs_result);
|
2020-04-23 15:43:10 +01:00
|
|
|
case BinaryOp::UnsignedRightShift:
|
|
|
|
return unsigned_right_shift(interpreter, lhs_result, rhs_result);
|
2020-04-23 16:06:01 +01:00
|
|
|
case BinaryOp::In:
|
|
|
|
return in(interpreter, lhs_result, rhs_result);
|
2020-03-28 16:56:54 +01:00
|
|
|
case BinaryOp::InstanceOf:
|
2020-04-15 09:28:41 +02:00
|
|
|
return instance_of(interpreter, lhs_result, rhs_result);
|
2020-03-08 07:53:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
2020-03-08 07:55:44 +02:00
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value LogicalExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-03-08 07:55:44 +02:00
|
|
|
{
|
2020-06-08 20:57:54 +02:00
|
|
|
auto lhs_result = m_lhs->execute(interpreter, global_object);
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-04-03 19:11:31 +02:00
|
|
|
|
2020-03-08 07:55:44 +02:00
|
|
|
switch (m_op) {
|
|
|
|
case LogicalOp::And:
|
2020-04-03 19:11:31 +02:00
|
|
|
if (lhs_result.to_boolean()) {
|
2020-06-08 20:57:54 +02:00
|
|
|
auto rhs_result = m_rhs->execute(interpreter, global_object);
|
2020-04-03 19:11:31 +02:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-04-18 00:49:11 +01:00
|
|
|
return rhs_result;
|
2020-04-03 19:11:31 +02:00
|
|
|
}
|
2020-04-18 00:49:11 +01:00
|
|
|
return lhs_result;
|
|
|
|
case LogicalOp::Or: {
|
2020-04-03 14:33:28 +01:00
|
|
|
if (lhs_result.to_boolean())
|
2020-04-18 00:49:11 +01:00
|
|
|
return lhs_result;
|
2020-06-08 20:57:54 +02:00
|
|
|
auto rhs_result = m_rhs->execute(interpreter, global_object);
|
2020-04-03 19:11:31 +02:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-04-18 00:49:11 +01:00
|
|
|
return rhs_result;
|
|
|
|
}
|
|
|
|
case LogicalOp::NullishCoalescing:
|
|
|
|
if (lhs_result.is_null() || lhs_result.is_undefined()) {
|
2020-06-08 20:57:54 +02:00
|
|
|
auto rhs_result = m_rhs->execute(interpreter, global_object);
|
2020-04-18 00:49:11 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
return rhs_result;
|
|
|
|
}
|
|
|
|
return lhs_result;
|
2020-03-07 19:42:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Reference Expression::to_reference(Interpreter&, GlobalObject&) const
|
2020-04-27 12:10:16 +02:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Reference Identifier::to_reference(Interpreter& interpreter, GlobalObject&) const
|
2020-04-27 12:37:27 +02:00
|
|
|
{
|
|
|
|
return interpreter.get_reference(string());
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Reference MemberExpression::to_reference(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-04-27 12:10:16 +02:00
|
|
|
{
|
2020-06-08 20:57:54 +02:00
|
|
|
auto object_value = m_object->execute(interpreter, global_object);
|
2020-05-28 17:48:25 +01:00
|
|
|
if (interpreter.exception())
|
2020-04-27 12:10:16 +02:00
|
|
|
return {};
|
2020-06-08 20:57:54 +02:00
|
|
|
auto property_name = computed_property_name(interpreter, global_object);
|
2020-04-27 12:10:16 +02:00
|
|
|
if (!property_name.is_valid())
|
|
|
|
return {};
|
2020-05-28 17:48:25 +01:00
|
|
|
return { object_value, property_name };
|
2020-04-27 12:10:16 +02:00
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value UnaryExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-03-08 23:27:18 +02:00
|
|
|
{
|
2020-04-26 13:53:40 +02:00
|
|
|
if (m_op == UnaryOp::Delete) {
|
2020-06-08 20:57:54 +02:00
|
|
|
auto reference = m_lhs->to_reference(interpreter, global_object);
|
2020-04-26 13:53:40 +02:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-04-27 12:10:16 +02:00
|
|
|
if (reference.is_unresolvable())
|
|
|
|
return Value(true);
|
2020-04-27 12:37:27 +02:00
|
|
|
// FIXME: Support deleting locals
|
|
|
|
ASSERT(!reference.is_local_variable());
|
2020-04-28 14:44:48 +02:00
|
|
|
if (reference.is_global_variable())
|
|
|
|
return interpreter.global_object().delete_property(reference.name());
|
2020-05-17 21:38:47 +01:00
|
|
|
auto* base_object = reference.base().to_object(interpreter);
|
2020-04-27 12:10:16 +02:00
|
|
|
if (!base_object)
|
2020-04-26 13:53:40 +02:00
|
|
|
return {};
|
2020-04-27 12:10:16 +02:00
|
|
|
return base_object->delete_property(reference.name());
|
2020-04-26 13:53:40 +02:00
|
|
|
}
|
|
|
|
|
2020-06-02 23:26:39 +02:00
|
|
|
Value lhs_result;
|
|
|
|
if (m_op == UnaryOp::Typeof && m_lhs->is_identifier()) {
|
2020-06-08 20:57:54 +02:00
|
|
|
auto reference = m_lhs->to_reference(interpreter, global_object);
|
2020-06-02 23:26:39 +02:00
|
|
|
if (interpreter.exception()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
// FIXME: standard recommends checking with is_unresolvable but it ALWAYS return false here
|
|
|
|
if (reference.is_local_variable() || reference.is_global_variable()) {
|
|
|
|
auto name = reference.name();
|
2020-06-08 21:25:16 +02:00
|
|
|
lhs_result = interpreter.get_variable(name.to_string(), global_object).value_or(js_undefined());
|
2020-06-02 23:26:39 +02:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
} else {
|
2020-06-08 20:57:54 +02:00
|
|
|
lhs_result = m_lhs->execute(interpreter, global_object);
|
2020-06-02 23:26:39 +02:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-03-08 23:27:18 +02:00
|
|
|
switch (m_op) {
|
2020-03-14 20:43:35 +02:00
|
|
|
case UnaryOp::BitwiseNot:
|
2020-04-15 09:28:41 +02:00
|
|
|
return bitwise_not(interpreter, 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-04-02 17:58:39 +01:00
|
|
|
case UnaryOp::Plus:
|
2020-04-15 09:28:41 +02:00
|
|
|
return unary_plus(interpreter, lhs_result);
|
2020-04-02 17:58:39 +01:00
|
|
|
case UnaryOp::Minus:
|
2020-04-15 09:28:41 +02:00
|
|
|
return unary_minus(interpreter, lhs_result);
|
2020-03-18 06:33:32 +11:00
|
|
|
case UnaryOp::Typeof:
|
|
|
|
switch (lhs_result.type()) {
|
2020-04-06 20:24:45 +02:00
|
|
|
case Value::Type::Empty:
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return {};
|
2020-03-18 06:33:32 +11:00
|
|
|
case Value::Type::Undefined:
|
2020-04-04 12:57:37 +02:00
|
|
|
return js_string(interpreter, "undefined");
|
2020-03-18 06:33:32 +11:00
|
|
|
case Value::Type::Null:
|
|
|
|
// yes, this is on purpose. yes, this is how javascript works.
|
|
|
|
// yes, it's silly.
|
2020-04-04 12:57:37 +02:00
|
|
|
return js_string(interpreter, "object");
|
2020-03-18 06:33:32 +11:00
|
|
|
case Value::Type::Number:
|
2020-04-04 12:57:37 +02:00
|
|
|
return js_string(interpreter, "number");
|
2020-03-18 06:33:32 +11:00
|
|
|
case Value::Type::String:
|
2020-04-04 12:57:37 +02:00
|
|
|
return js_string(interpreter, "string");
|
2020-03-18 06:33:32 +11:00
|
|
|
case Value::Type::Object:
|
2020-05-06 11:52:53 +01:00
|
|
|
if (lhs_result.is_function())
|
2020-04-04 14:34:31 +01:00
|
|
|
return js_string(interpreter, "function");
|
2020-04-04 12:57:37 +02:00
|
|
|
return js_string(interpreter, "object");
|
2020-03-18 06:33:32 +11:00
|
|
|
case Value::Type::Boolean:
|
2020-04-04 12:57:37 +02:00
|
|
|
return js_string(interpreter, "boolean");
|
2020-04-29 23:25:21 -07:00
|
|
|
case Value::Type::Symbol:
|
|
|
|
return js_string(interpreter, "symbol");
|
2020-06-06 01:14:10 +01:00
|
|
|
case Value::Type::BigInt:
|
|
|
|
return js_string(interpreter, "bigint");
|
2020-04-15 17:55:03 +01:00
|
|
|
default:
|
|
|
|
ASSERT_NOT_REACHED();
|
2020-03-18 06:33:32 +11:00
|
|
|
}
|
2020-04-15 17:55:03 +01:00
|
|
|
case UnaryOp::Void:
|
|
|
|
return js_undefined();
|
2020-04-26 13:53:40 +02:00
|
|
|
case UnaryOp::Delete:
|
|
|
|
ASSERT_NOT_REACHED();
|
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);
|
2020-04-13 16:42:54 +02:00
|
|
|
if (!m_variables.is_empty()) {
|
|
|
|
print_indent(indent + 1);
|
|
|
|
printf("(Variables)\n");
|
|
|
|
for (auto& variable : m_variables)
|
|
|
|
variable.dump(indent + 2);
|
|
|
|
}
|
|
|
|
if (!m_children.is_empty()) {
|
|
|
|
print_indent(indent + 1);
|
|
|
|
printf("(Children)\n");
|
|
|
|
for (auto& child : children())
|
|
|
|
child.dump(indent + 2);
|
|
|
|
}
|
2020-03-07 19:42:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BinaryExpression::dump(int indent) const
|
|
|
|
{
|
2020-03-07 23:16:34 +01:00
|
|
|
const char* op_string = nullptr;
|
|
|
|
switch (m_op) {
|
2020-04-05 12:56:53 +01:00
|
|
|
case BinaryOp::Addition:
|
2020-03-07 23:16:34 +01:00
|
|
|
op_string = "+";
|
|
|
|
break;
|
2020-04-05 12:56:53 +01:00
|
|
|
case BinaryOp::Subtraction:
|
2020-03-07 23:16:34 +01:00
|
|
|
op_string = "-";
|
|
|
|
break;
|
2020-04-05 12:56:53 +01:00
|
|
|
case BinaryOp::Multiplication:
|
2020-03-12 23:04:52 +11:00
|
|
|
op_string = "*";
|
|
|
|
break;
|
2020-04-05 12:56:53 +01:00
|
|
|
case BinaryOp::Division:
|
2020-03-12 23:04:52 +11:00
|
|
|
op_string = "/";
|
|
|
|
break;
|
2020-04-04 21:17:34 +02:00
|
|
|
case BinaryOp::Modulo:
|
|
|
|
op_string = "%";
|
|
|
|
break;
|
2020-04-05 13:40:00 +01:00
|
|
|
case BinaryOp::Exponentiation:
|
|
|
|
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-04-23 15:43:10 +01:00
|
|
|
case BinaryOp::UnsignedRightShift:
|
|
|
|
op_string = ">>>";
|
|
|
|
break;
|
2020-04-23 16:06:01 +01:00
|
|
|
case BinaryOp::In:
|
|
|
|
op_string = "in";
|
|
|
|
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-04-18 00:49:11 +01:00
|
|
|
case LogicalOp::NullishCoalescing:
|
|
|
|
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-04-02 17:58:39 +01:00
|
|
|
case UnaryOp::Plus:
|
|
|
|
op_string = "+";
|
|
|
|
break;
|
|
|
|
case UnaryOp::Minus:
|
|
|
|
op_string = "-";
|
|
|
|
break;
|
2020-03-18 06:33:32 +11:00
|
|
|
case UnaryOp::Typeof:
|
|
|
|
op_string = "typeof ";
|
|
|
|
break;
|
2020-04-15 17:55:03 +01:00
|
|
|
case UnaryOp::Void:
|
|
|
|
op_string = "void ";
|
|
|
|
break;
|
2020-04-26 13:53:40 +02:00
|
|
|
case UnaryOp::Delete:
|
|
|
|
op_string = "delete ";
|
|
|
|
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-04-06 20:24:45 +02:00
|
|
|
print_indent(indent);
|
|
|
|
printf("CallExpression %s\n", is_new_expression() ? "[new]" : "");
|
2020-03-12 23:02:41 +01:00
|
|
|
m_callee->dump(indent + 1);
|
2020-03-12 19:34:59 +01:00
|
|
|
for (auto& argument : m_arguments)
|
2020-05-05 22:36:24 -07:00
|
|
|
argument.value->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
|
|
|
}
|
|
|
|
|
2020-06-06 01:14:10 +01:00
|
|
|
void BigIntLiteral::dump(int indent) const
|
|
|
|
{
|
|
|
|
print_indent(indent);
|
|
|
|
printf("BigIntLiteral %s\n", m_value.characters());
|
|
|
|
}
|
|
|
|
|
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 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
|
|
|
{
|
|
|
|
print_indent(indent);
|
2020-05-02 11:46:39 -07:00
|
|
|
printf("%s '%s'\n", class_name, name().characters());
|
|
|
|
if (!m_parameters.is_empty()) {
|
|
|
|
print_indent(indent + 1);
|
|
|
|
printf("(Parameters)\n");
|
|
|
|
|
|
|
|
for (auto& parameter : m_parameters) {
|
|
|
|
print_indent(indent + 2);
|
2020-05-04 16:05:13 +01:00
|
|
|
if (parameter.is_rest)
|
|
|
|
printf("...");
|
2020-05-02 11:46:39 -07:00
|
|
|
printf("%s\n", parameter.name.characters());
|
2020-05-04 16:05:13 +01:00
|
|
|
if (parameter.default_value)
|
2020-05-02 11:46:39 -07:00
|
|
|
parameter.default_value->dump(indent + 3);
|
|
|
|
}
|
|
|
|
}
|
2020-04-13 16:42:54 +02:00
|
|
|
if (!m_variables.is_empty()) {
|
|
|
|
print_indent(indent + 1);
|
|
|
|
printf("(Variables)\n");
|
2020-05-02 11:46:39 -07:00
|
|
|
|
|
|
|
for (auto& variable : m_variables)
|
|
|
|
variable.dump(indent + 2);
|
2020-04-13 16:42:54 +02:00
|
|
|
}
|
|
|
|
print_indent(indent + 1);
|
|
|
|
printf("(Body)\n");
|
|
|
|
body().dump(indent + 2);
|
2020-03-07 19:42:11 +01:00
|
|
|
}
|
|
|
|
|
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");
|
2020-04-04 21:21:19 +02:00
|
|
|
test().dump(indent + 1);
|
2020-03-09 03:22:21 +08:00
|
|
|
body().dump(indent + 1);
|
|
|
|
}
|
|
|
|
|
2020-04-04 21:29:23 +02:00
|
|
|
void DoWhileStatement::dump(int indent) const
|
|
|
|
{
|
|
|
|
ASTNode::dump(indent);
|
|
|
|
|
|
|
|
print_indent(indent);
|
|
|
|
printf("DoWhile\n");
|
|
|
|
test().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-04-21 19:21:26 +01:00
|
|
|
void ForInStatement::dump(int indent) const
|
|
|
|
{
|
|
|
|
ASTNode::dump(indent);
|
|
|
|
|
|
|
|
print_indent(indent);
|
|
|
|
printf("ForIn\n");
|
|
|
|
lhs().dump(indent + 1);
|
|
|
|
rhs().dump(indent + 1);
|
|
|
|
body().dump(indent + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ForOfStatement::dump(int indent) const
|
|
|
|
{
|
|
|
|
ASTNode::dump(indent);
|
|
|
|
|
|
|
|
print_indent(indent);
|
|
|
|
printf("ForOf\n");
|
|
|
|
lhs().dump(indent + 1);
|
|
|
|
rhs().dump(indent + 1);
|
|
|
|
body().dump(indent + 1);
|
|
|
|
}
|
|
|
|
|
2020-06-08 21:25:16 +02:00
|
|
|
Value Identifier::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-03-09 21:13:55 +01:00
|
|
|
{
|
2020-06-08 21:25:16 +02:00
|
|
|
auto value = interpreter.get_variable(string(), global_object);
|
2020-04-25 18:43:34 +02:00
|
|
|
if (value.is_empty())
|
2020-06-09 22:48:01 -07:00
|
|
|
return interpreter.throw_exception<ReferenceError>(ErrorType::UnknownIdentifier, string().characters());
|
2020-04-25 18:43:34 +02:00
|
|
|
return 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());
|
|
|
|
}
|
|
|
|
|
2020-04-26 23:05:37 -07:00
|
|
|
void SpreadExpression::dump(int indent) const
|
|
|
|
{
|
|
|
|
ASTNode::dump(indent);
|
|
|
|
m_target->dump(indent + 1);
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value SpreadExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-04-26 23:05:37 -07:00
|
|
|
{
|
2020-06-08 20:57:54 +02:00
|
|
|
return m_target->execute(interpreter, global_object);
|
2020-04-26 23:05:37 -07:00
|
|
|
}
|
|
|
|
|
2020-06-08 21:10:22 +02:00
|
|
|
Value ThisExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-04-13 00:42:14 +02:00
|
|
|
{
|
2020-06-08 21:10:22 +02:00
|
|
|
return interpreter.this_value(global_object);
|
2020-04-13 00:42:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThisExpression::dump(int indent) const
|
|
|
|
{
|
|
|
|
ASTNode::dump(indent);
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value AssignmentExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-03-09 21:13:55 +01:00
|
|
|
{
|
2020-06-08 20:57:54 +02:00
|
|
|
auto rhs_result = m_rhs->execute(interpreter, global_object);
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-03-09 21:13:55 +01:00
|
|
|
|
2020-04-06 20:24:45 +02:00
|
|
|
Value lhs_result;
|
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-06-08 20:57:54 +02:00
|
|
|
lhs_result = m_lhs->execute(interpreter, global_object);
|
2020-04-06 20:24:45 +02:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-04-15 09:28:41 +02:00
|
|
|
rhs_result = add(interpreter, lhs_result, rhs_result);
|
2020-03-12 23:09:15 +11:00
|
|
|
break;
|
2020-03-12 13:54:56 +01:00
|
|
|
case AssignmentOp::SubtractionAssignment:
|
2020-06-08 20:57:54 +02:00
|
|
|
lhs_result = m_lhs->execute(interpreter, global_object);
|
2020-04-06 20:24:45 +02:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-04-15 09:28:41 +02:00
|
|
|
rhs_result = sub(interpreter, lhs_result, rhs_result);
|
2020-03-12 23:09:15 +11:00
|
|
|
break;
|
2020-03-12 13:54:56 +01:00
|
|
|
case AssignmentOp::MultiplicationAssignment:
|
2020-06-08 20:57:54 +02:00
|
|
|
lhs_result = m_lhs->execute(interpreter, global_object);
|
2020-04-06 20:24:45 +02:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-04-15 09:28:41 +02:00
|
|
|
rhs_result = mul(interpreter, lhs_result, rhs_result);
|
2020-03-12 23:09:15 +11:00
|
|
|
break;
|
2020-03-12 13:54:56 +01:00
|
|
|
case AssignmentOp::DivisionAssignment:
|
2020-06-08 20:57:54 +02:00
|
|
|
lhs_result = m_lhs->execute(interpreter, global_object);
|
2020-04-06 20:24:45 +02:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-04-15 09:28:41 +02:00
|
|
|
rhs_result = div(interpreter, lhs_result, rhs_result);
|
2020-03-12 23:09:15 +11:00
|
|
|
break;
|
2020-05-04 23:07:05 +01:00
|
|
|
case AssignmentOp::ModuloAssignment:
|
2020-06-08 20:57:54 +02:00
|
|
|
lhs_result = m_lhs->execute(interpreter, global_object);
|
2020-05-04 23:07:05 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
rhs_result = mod(interpreter, lhs_result, rhs_result);
|
|
|
|
break;
|
2020-05-04 23:03:35 +01:00
|
|
|
case AssignmentOp::ExponentiationAssignment:
|
2020-06-08 20:57:54 +02:00
|
|
|
lhs_result = m_lhs->execute(interpreter, global_object);
|
2020-05-04 23:03:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
rhs_result = exp(interpreter, lhs_result, rhs_result);
|
|
|
|
break;
|
2020-05-04 22:34:45 +01:00
|
|
|
case AssignmentOp::BitwiseAndAssignment:
|
2020-06-08 20:57:54 +02:00
|
|
|
lhs_result = m_lhs->execute(interpreter, global_object);
|
2020-05-04 22:34:45 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
rhs_result = bitwise_and(interpreter, lhs_result, rhs_result);
|
|
|
|
break;
|
|
|
|
case AssignmentOp::BitwiseOrAssignment:
|
2020-06-08 20:57:54 +02:00
|
|
|
lhs_result = m_lhs->execute(interpreter, global_object);
|
2020-05-04 22:34:45 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
rhs_result = bitwise_or(interpreter, lhs_result, rhs_result);
|
|
|
|
break;
|
|
|
|
case AssignmentOp::BitwiseXorAssignment:
|
2020-06-08 20:57:54 +02:00
|
|
|
lhs_result = m_lhs->execute(interpreter, global_object);
|
2020-05-04 22:34:45 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
rhs_result = bitwise_xor(interpreter, lhs_result, rhs_result);
|
|
|
|
break;
|
2020-04-23 13:36:14 +01:00
|
|
|
case AssignmentOp::LeftShiftAssignment:
|
2020-06-08 20:57:54 +02:00
|
|
|
lhs_result = m_lhs->execute(interpreter, global_object);
|
2020-04-23 13:36:14 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
rhs_result = left_shift(interpreter, lhs_result, rhs_result);
|
|
|
|
break;
|
2020-04-23 13:45:19 +01:00
|
|
|
case AssignmentOp::RightShiftAssignment:
|
2020-06-08 20:57:54 +02:00
|
|
|
lhs_result = m_lhs->execute(interpreter, global_object);
|
2020-04-23 13:45:19 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
rhs_result = right_shift(interpreter, lhs_result, rhs_result);
|
|
|
|
break;
|
2020-04-23 15:43:10 +01:00
|
|
|
case AssignmentOp::UnsignedRightShiftAssignment:
|
2020-06-08 20:57:54 +02:00
|
|
|
lhs_result = m_lhs->execute(interpreter, global_object);
|
2020-04-23 15:43:10 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
rhs_result = unsigned_right_shift(interpreter, lhs_result, rhs_result);
|
|
|
|
break;
|
2020-03-09 21:13:55 +01:00
|
|
|
}
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-04-05 19:21:36 +02:00
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
auto reference = m_lhs->to_reference(interpreter, global_object);
|
2020-04-27 12:56:09 +02:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
if (reference.is_unresolvable())
|
2020-06-09 22:48:01 -07:00
|
|
|
return interpreter.throw_exception<ReferenceError>(ErrorType::InvalidLeftHandAssignment);
|
2020-04-05 19:21:36 +02:00
|
|
|
|
2020-05-02 20:28:48 +01:00
|
|
|
update_function_name(rhs_result, reference.name().as_string());
|
2020-04-28 14:44:48 +02:00
|
|
|
reference.put(interpreter, rhs_result);
|
2020-04-27 12:56:09 +02:00
|
|
|
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-03-09 21:13:55 +01:00
|
|
|
return rhs_result;
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value UpdateExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-03-12 13:45:45 +02:00
|
|
|
{
|
2020-06-08 20:57:54 +02:00
|
|
|
auto reference = m_argument->to_reference(interpreter, global_object);
|
2020-04-28 14:44:48 +02:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
auto old_value = reference.get(interpreter);
|
2020-04-21 23:27:11 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 01:14:10 +01:00
|
|
|
old_value = old_value.to_numeric(interpreter);
|
2020-05-18 00:28:00 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-03-12 13:45:45 +02:00
|
|
|
|
2020-06-06 01:14:10 +01:00
|
|
|
Value new_value;
|
2020-03-12 13:45:45 +02:00
|
|
|
switch (m_op) {
|
|
|
|
case UpdateOp::Increment:
|
2020-06-06 01:14:10 +01:00
|
|
|
if (old_value.is_number())
|
|
|
|
new_value = Value(old_value.as_double() + 1);
|
|
|
|
else
|
|
|
|
new_value = js_bigint(interpreter, old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
|
2020-03-12 13:45:45 +02:00
|
|
|
break;
|
|
|
|
case UpdateOp::Decrement:
|
2020-06-06 01:14:10 +01:00
|
|
|
if (old_value.is_number())
|
|
|
|
new_value = Value(old_value.as_double() - 1);
|
|
|
|
else
|
|
|
|
new_value = js_bigint(interpreter, old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
|
2020-03-12 14:24:34 +02:00
|
|
|
break;
|
2020-04-21 23:27:11 +01:00
|
|
|
default:
|
|
|
|
ASSERT_NOT_REACHED();
|
2020-03-12 13:45:45 +02:00
|
|
|
}
|
|
|
|
|
2020-04-28 14:44:48 +02:00
|
|
|
reference.put(interpreter, new_value);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-04-21 23:27:11 +01:00
|
|
|
return m_prefixed ? new_value : old_value;
|
2020-03-12 13:45:45 +02:00
|
|
|
}
|
|
|
|
|
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-05-04 23:07:05 +01:00
|
|
|
case AssignmentOp::ModuloAssignment:
|
|
|
|
op_string = "%=";
|
|
|
|
break;
|
2020-05-04 23:03:35 +01:00
|
|
|
case AssignmentOp::ExponentiationAssignment:
|
|
|
|
op_string = "**=";
|
|
|
|
break;
|
2020-05-04 22:34:45 +01:00
|
|
|
case AssignmentOp::BitwiseAndAssignment:
|
|
|
|
op_string = "&=";
|
|
|
|
break;
|
|
|
|
case AssignmentOp::BitwiseOrAssignment:
|
|
|
|
op_string = "|=";
|
|
|
|
break;
|
|
|
|
case AssignmentOp::BitwiseXorAssignment:
|
|
|
|
op_string = "^=";
|
|
|
|
break;
|
2020-04-23 13:36:14 +01:00
|
|
|
case AssignmentOp::LeftShiftAssignment:
|
|
|
|
op_string = "<<=";
|
|
|
|
break;
|
2020-04-23 13:45:19 +01:00
|
|
|
case AssignmentOp::RightShiftAssignment:
|
|
|
|
op_string = ">>=";
|
|
|
|
break;
|
2020-04-23 15:43:10 +01:00
|
|
|
case AssignmentOp::UnsignedRightShiftAssignment:
|
|
|
|
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-06-08 20:57:54 +02:00
|
|
|
Value VariableDeclaration::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-03-09 21:13:55 +01:00
|
|
|
{
|
2020-04-04 21:46:25 +02:00
|
|
|
for (auto& declarator : m_declarations) {
|
|
|
|
if (auto* init = declarator.init()) {
|
2020-06-08 20:57:54 +02:00
|
|
|
auto initalizer_result = init->execute(interpreter, global_object);
|
2020-04-04 21:46:25 +02:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-05-02 20:28:48 +01:00
|
|
|
auto variable_name = declarator.id().string();
|
|
|
|
update_function_name(initalizer_result, variable_name);
|
2020-06-08 21:25:16 +02:00
|
|
|
interpreter.set_variable(variable_name, initalizer_result, global_object, true);
|
2020-04-04 21:46:25 +02:00
|
|
|
}
|
2020-03-09 21:13:55 +01:00
|
|
|
}
|
2020-04-06 20:24:45 +02:00
|
|
|
return js_undefined();
|
2020-03-09 21:13:55 +01:00
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value VariableDeclarator::execute(Interpreter&, GlobalObject&) const
|
2020-04-04 21:46:25 +02:00
|
|
|
{
|
|
|
|
// NOTE: This node is handled by VariableDeclaration.
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2020-03-09 21:13:55 +01:00
|
|
|
void VariableDeclaration::dump(int indent) const
|
|
|
|
{
|
2020-04-08 11:59:18 +02:00
|
|
|
const char* declaration_kind_string = nullptr;
|
|
|
|
switch (m_declaration_kind) {
|
|
|
|
case DeclarationKind::Let:
|
|
|
|
declaration_kind_string = "Let";
|
2020-03-11 21:09:20 +02:00
|
|
|
break;
|
2020-04-08 11:59:18 +02:00
|
|
|
case DeclarationKind::Var:
|
|
|
|
declaration_kind_string = "Var";
|
2020-03-12 14:24:34 +02:00
|
|
|
break;
|
2020-04-08 11:59:18 +02:00
|
|
|
case DeclarationKind::Const:
|
|
|
|
declaration_kind_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-04-08 11:59:18 +02:00
|
|
|
printf("%s\n", declaration_kind_string);
|
2020-04-04 21:46:25 +02:00
|
|
|
|
|
|
|
for (auto& declarator : m_declarations)
|
|
|
|
declarator.dump(indent + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VariableDeclarator::dump(int indent) const
|
|
|
|
{
|
|
|
|
ASTNode::dump(indent);
|
|
|
|
m_id->dump(indent + 1);
|
|
|
|
if (m_init)
|
|
|
|
m_init->dump(indent + 1);
|
2020-03-09 21:13:55 +01:00
|
|
|
}
|
|
|
|
|
2020-04-23 19:37:53 +01:00
|
|
|
void ObjectProperty::dump(int indent) const
|
|
|
|
{
|
|
|
|
ASTNode::dump(indent);
|
|
|
|
m_key->dump(indent + 1);
|
|
|
|
m_value->dump(indent + 1);
|
|
|
|
}
|
|
|
|
|
2020-03-09 21:28:31 +01:00
|
|
|
void ObjectExpression::dump(int indent) const
|
|
|
|
{
|
|
|
|
ASTNode::dump(indent);
|
2020-04-23 19:37:53 +01:00
|
|
|
for (auto& property : m_properties) {
|
|
|
|
property.dump(indent + 1);
|
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-06-08 20:57:54 +02:00
|
|
|
Value ObjectProperty::execute(Interpreter&, GlobalObject&) const
|
2020-04-23 19:37:53 +01:00
|
|
|
{
|
|
|
|
// NOTE: ObjectProperty execution is handled by ObjectExpression.
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value ObjectExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-03-09 21:28:31 +01:00
|
|
|
{
|
2020-06-08 20:57:54 +02:00
|
|
|
auto* object = Object::create_empty(interpreter, global_object);
|
2020-04-23 19:37:53 +01:00
|
|
|
for (auto& property : m_properties) {
|
2020-06-08 20:57:54 +02:00
|
|
|
auto key_result = property.key().execute(interpreter, global_object);
|
2020-04-23 19:37:53 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-04-27 21:52:47 -07:00
|
|
|
|
2020-05-21 17:28:28 -07:00
|
|
|
if (property.type() == ObjectProperty::Type::Spread) {
|
2020-04-27 21:52:47 -07:00
|
|
|
if (key_result.is_array()) {
|
|
|
|
auto& array_to_spread = static_cast<Array&>(key_result.as_object());
|
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
|
|
|
for (auto& entry : array_to_spread.indexed_properties()) {
|
|
|
|
object->indexed_properties().append(entry.value_and_attributes(&array_to_spread).value);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-04-27 21:52:47 -07:00
|
|
|
}
|
|
|
|
} else if (key_result.is_object()) {
|
|
|
|
auto& obj_to_spread = key_result.as_object();
|
|
|
|
|
2020-04-28 19:19:31 -07:00
|
|
|
for (auto& it : obj_to_spread.shape().property_table_ordered()) {
|
2020-06-07 10:53:14 -07:00
|
|
|
if (it.value.attributes.is_enumerable()) {
|
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
|
|
|
object->define_property(it.key, obj_to_spread.get(it.key));
|
2020-06-07 10:53:14 -07:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
}
|
2020-04-27 21:52:47 -07:00
|
|
|
}
|
|
|
|
} else if (key_result.is_string()) {
|
2020-04-29 12:35:39 +02:00
|
|
|
auto& str_to_spread = key_result.as_string().string();
|
2020-04-27 21:52:47 -07:00
|
|
|
|
|
|
|
for (size_t i = 0; i < str_to_spread.length(); i++) {
|
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
|
|
|
object->define_property(i, js_string(interpreter, str_to_spread.substring(i, 1)));
|
2020-06-07 10:53:14 -07:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-04-27 21:52:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-05-15 13:39:24 +02:00
|
|
|
auto key = key_result.to_string(interpreter);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-08 20:57:54 +02:00
|
|
|
auto value = property.value().execute(interpreter, global_object);
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-05-21 17:28:28 -07:00
|
|
|
|
|
|
|
String name = key;
|
|
|
|
if (property.type() == ObjectProperty::Type::Getter) {
|
|
|
|
name = String::format("get %s", key.characters());
|
|
|
|
} else if (property.type() == ObjectProperty::Type::Setter) {
|
|
|
|
name = String::format("set %s", key.characters());
|
|
|
|
}
|
|
|
|
|
|
|
|
update_function_name(value, name);
|
|
|
|
|
|
|
|
if (property.type() == ObjectProperty::Type::Getter || property.type() == ObjectProperty::Type::Setter) {
|
2020-05-23 23:27:10 +01:00
|
|
|
ASSERT(value.is_function());
|
|
|
|
Accessor* accessor { nullptr };
|
|
|
|
auto property_metadata = object->shape().lookup(key);
|
|
|
|
if (property_metadata.has_value()) {
|
|
|
|
auto existing_property = object->get_direct(property_metadata.value().offset);
|
|
|
|
if (existing_property.is_accessor())
|
|
|
|
accessor = &existing_property.as_accessor();
|
|
|
|
}
|
|
|
|
if (!accessor) {
|
|
|
|
accessor = Accessor::create(interpreter, nullptr, nullptr);
|
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
|
|
|
object->define_property(key, accessor, Attribute::Configurable | Attribute::Enumerable);
|
2020-06-07 10:53:14 -07:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-05-21 17:28:28 -07:00
|
|
|
}
|
2020-05-23 23:27:10 +01:00
|
|
|
if (property.type() == ObjectProperty::Type::Getter)
|
|
|
|
accessor->set_getter(&value.as_function());
|
|
|
|
else
|
|
|
|
accessor->set_setter(&value.as_function());
|
2020-05-21 17:28:28 -07:00
|
|
|
} else {
|
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
|
|
|
object->define_property(key, value);
|
2020-06-07 10:53:14 -07:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-05-21 17:28:28 -07:00
|
|
|
}
|
2020-03-27 15:35:35 +01:00
|
|
|
}
|
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-06-08 20:57:54 +02:00
|
|
|
PropertyName MemberExpression::computed_property_name(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-03-20 20:51:03 +01:00
|
|
|
{
|
|
|
|
if (!is_computed()) {
|
|
|
|
ASSERT(m_property->is_identifier());
|
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
|
|
|
return static_cast<const Identifier&>(*m_property).string();
|
2020-03-20 20:51:03 +01:00
|
|
|
}
|
2020-06-08 20:57:54 +02:00
|
|
|
auto index = m_property->execute(interpreter, global_object);
|
2020-04-06 20:24:45 +02:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-05-01 11:06:27 +01:00
|
|
|
|
2020-04-06 20:24:45 +02:00
|
|
|
ASSERT(!index.is_empty());
|
2020-05-01 11:06:27 +01:00
|
|
|
|
2020-05-18 08:59:35 +01:00
|
|
|
if (index.is_integer() && index.as_i32() >= 0)
|
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
|
|
|
return index.as_i32();
|
2020-05-01 11:06:27 +01:00
|
|
|
|
2020-05-15 13:39:24 +02:00
|
|
|
auto index_string = index.to_string(interpreter);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
|
|
|
return index_string;
|
2020-03-20 20:51:03 +01:00
|
|
|
}
|
|
|
|
|
2020-04-19 01:12:51 +01:00
|
|
|
String MemberExpression::to_string_approximation() const
|
|
|
|
{
|
|
|
|
String object_string = "<object>";
|
|
|
|
if (m_object->is_identifier())
|
|
|
|
object_string = static_cast<const Identifier&>(*m_object).string();
|
|
|
|
if (is_computed())
|
|
|
|
return String::format("%s[<computed>]", object_string.characters());
|
|
|
|
ASSERT(m_property->is_identifier());
|
|
|
|
return String::format("%s.%s", object_string.characters(), static_cast<const Identifier&>(*m_property).string().characters());
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value MemberExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-03-11 18:58:19 +01:00
|
|
|
{
|
2020-06-08 20:57:54 +02:00
|
|
|
auto object_value = m_object->execute(interpreter, global_object);
|
2020-04-06 20:24:45 +02:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-05-17 21:38:47 +01:00
|
|
|
auto* object_result = object_value.to_object(interpreter);
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-08 20:57:54 +02:00
|
|
|
return object_result->get(computed_property_name(interpreter, global_object)).value_or(js_undefined());
|
2020-03-11 18:58:19 +01:00
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value StringLiteral::execute(Interpreter& interpreter, GlobalObject&) const
|
2020-03-12 12:19:11 +01:00
|
|
|
{
|
2020-04-04 12:57:37 +02:00
|
|
|
return js_string(interpreter, m_value);
|
2020-03-12 12:19:11 +01:00
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value NumericLiteral::execute(Interpreter&, GlobalObject&) const
|
2020-03-12 12:19:11 +01:00
|
|
|
{
|
|
|
|
return Value(m_value);
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value BigIntLiteral::execute(Interpreter& interpreter, GlobalObject&) const
|
2020-06-06 01:14:10 +01:00
|
|
|
{
|
|
|
|
return js_bigint(interpreter, Crypto::SignedBigInteger::from_base10(m_value.substring(0, m_value.length() - 1)));
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value BooleanLiteral::execute(Interpreter&, GlobalObject&) const
|
2020-03-12 12:19:11 +01:00
|
|
|
{
|
|
|
|
return Value(m_value);
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value NullLiteral::execute(Interpreter&, GlobalObject&) const
|
2020-03-15 23:32:34 +02:00
|
|
|
{
|
|
|
|
return js_null();
|
|
|
|
}
|
|
|
|
|
2020-06-03 16:05:49 -07:00
|
|
|
void RegExpLiteral::dump(int indent) const
|
|
|
|
{
|
|
|
|
print_indent(indent);
|
|
|
|
printf("%s (/%s/%s)\n", class_name(), content().characters(), flags().characters());
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value RegExpLiteral::execute(Interpreter&, GlobalObject& global_object) const
|
2020-06-03 16:05:49 -07:00
|
|
|
{
|
2020-06-08 20:57:54 +02:00
|
|
|
return RegExpObject::create(global_object, content(), flags());
|
2020-06-03 16:05:49 -07:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:29:57 +01:00
|
|
|
void ArrayExpression::dump(int indent) const
|
|
|
|
{
|
|
|
|
ASTNode::dump(indent);
|
|
|
|
for (auto& element : m_elements) {
|
2020-04-15 20:09:06 +01:00
|
|
|
if (element) {
|
|
|
|
element->dump(indent + 1);
|
|
|
|
} else {
|
|
|
|
print_indent(indent + 1);
|
|
|
|
printf("<empty>\n");
|
|
|
|
}
|
2020-03-20 20:29:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value ArrayExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-03-20 20:29:57 +01:00
|
|
|
{
|
2020-04-17 18:24:01 +02:00
|
|
|
auto* array = Array::create(interpreter.global_object());
|
2020-03-20 20:29:57 +01:00
|
|
|
for (auto& element : m_elements) {
|
2020-04-15 20:09:06 +01:00
|
|
|
auto value = Value();
|
|
|
|
if (element) {
|
2020-06-08 20:57:54 +02:00
|
|
|
value = element->execute(interpreter, global_object);
|
2020-04-26 23:05:37 -07:00
|
|
|
|
2020-04-15 20:09:06 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-04-26 23:05:37 -07:00
|
|
|
|
|
|
|
if (element->is_spread_expression()) {
|
2020-04-27 14:01:04 +01:00
|
|
|
// FIXME: Support arbitrary iterables
|
|
|
|
if (value.is_array()) {
|
|
|
|
auto& array_to_spread = static_cast<Array&>(value.as_object());
|
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
|
|
|
for (auto& entry : array_to_spread.indexed_properties()) {
|
|
|
|
array->indexed_properties().append(entry.value_and_attributes(&array_to_spread).value);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-04-26 23:05:37 -07:00
|
|
|
}
|
2020-04-27 14:01:04 +01:00
|
|
|
continue;
|
2020-04-26 23:05:37 -07:00
|
|
|
}
|
2020-04-27 14:01:04 +01:00
|
|
|
if (value.is_string() || (value.is_object() && value.as_object().is_string_object())) {
|
|
|
|
String string_to_spread;
|
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
|
|
|
if (value.is_string()) {
|
2020-04-29 12:35:39 +02:00
|
|
|
string_to_spread = value.as_string().string();
|
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
|
|
|
} else {
|
2020-04-29 12:33:28 +02:00
|
|
|
string_to_spread = static_cast<const StringObject&>(value.as_object()).primitive_string().string();
|
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
|
|
|
}
|
2020-04-27 14:01:04 +01:00
|
|
|
for (size_t i = 0; i < string_to_spread.length(); ++i)
|
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
|
|
|
array->indexed_properties().append(js_string(interpreter, string_to_spread.substring(i, 1)));
|
2020-04-27 14:01:04 +01:00
|
|
|
continue;
|
|
|
|
}
|
2020-06-09 22:48:01 -07:00
|
|
|
interpreter.throw_exception<TypeError>(ErrorType::NotIterable, value.to_string_without_side_effects().characters());
|
2020-04-27 14:01:04 +01:00
|
|
|
return {};
|
2020-04-26 23:05:37 -07:00
|
|
|
}
|
2020-04-15 20:09:06 +01:00
|
|
|
}
|
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
|
|
|
array->indexed_properties().append(value);
|
2020-03-20 20:29:57 +01:00
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
LibJS: Add template literals
Adds fully functioning template literals. Because template literals
contain expressions, most of the work has to be done in the Lexer rather
than the Parser. And because of the complexity of template literals
(expressions, nesting, escapes, etc), the Lexer needs to have some
template-related state.
When entering a new template literal, a TemplateLiteralStart token is
emitted. When inside a literal, all text will be parsed up until a '${'
or '`' (or EOF, but that's a syntax error) is seen, and then a
TemplateLiteralExprStart token is emitted. At this point, the Lexer
proceeds as normal, however it keeps track of the number of opening
and closing curly braces it has seen in order to determine the close
of the expression. Once it finds a matching curly brace for the '${',
a TemplateLiteralExprEnd token is emitted and the state is updated
accordingly.
When the Lexer is inside of a template literal, but not an expression,
and sees a '`', this must be the closing grave: a TemplateLiteralEnd
token is emitted.
The state required to correctly parse template strings consists of a
vector (for nesting) of two pieces of information: whether or not we
are in a template expression (as opposed to a template string); and
the count of the number of unmatched open curly braces we have seen
(only applicable if the Lexer is currently in a template expression).
TODO: Add support for template literal newlines in the JS REPL (this will
cause a syntax error currently):
> `foo
> bar`
'foo
bar'
2020-05-03 15:41:14 -07:00
|
|
|
void TemplateLiteral::dump(int indent) const
|
|
|
|
{
|
|
|
|
ASTNode::dump(indent);
|
2020-05-06 10:17:35 +01:00
|
|
|
for (auto& expression : m_expressions)
|
LibJS: Add template literals
Adds fully functioning template literals. Because template literals
contain expressions, most of the work has to be done in the Lexer rather
than the Parser. And because of the complexity of template literals
(expressions, nesting, escapes, etc), the Lexer needs to have some
template-related state.
When entering a new template literal, a TemplateLiteralStart token is
emitted. When inside a literal, all text will be parsed up until a '${'
or '`' (or EOF, but that's a syntax error) is seen, and then a
TemplateLiteralExprStart token is emitted. At this point, the Lexer
proceeds as normal, however it keeps track of the number of opening
and closing curly braces it has seen in order to determine the close
of the expression. Once it finds a matching curly brace for the '${',
a TemplateLiteralExprEnd token is emitted and the state is updated
accordingly.
When the Lexer is inside of a template literal, but not an expression,
and sees a '`', this must be the closing grave: a TemplateLiteralEnd
token is emitted.
The state required to correctly parse template strings consists of a
vector (for nesting) of two pieces of information: whether or not we
are in a template expression (as opposed to a template string); and
the count of the number of unmatched open curly braces we have seen
(only applicable if the Lexer is currently in a template expression).
TODO: Add support for template literal newlines in the JS REPL (this will
cause a syntax error currently):
> `foo
> bar`
'foo
bar'
2020-05-03 15:41:14 -07:00
|
|
|
expression.dump(indent + 1);
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value TemplateLiteral::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
LibJS: Add template literals
Adds fully functioning template literals. Because template literals
contain expressions, most of the work has to be done in the Lexer rather
than the Parser. And because of the complexity of template literals
(expressions, nesting, escapes, etc), the Lexer needs to have some
template-related state.
When entering a new template literal, a TemplateLiteralStart token is
emitted. When inside a literal, all text will be parsed up until a '${'
or '`' (or EOF, but that's a syntax error) is seen, and then a
TemplateLiteralExprStart token is emitted. At this point, the Lexer
proceeds as normal, however it keeps track of the number of opening
and closing curly braces it has seen in order to determine the close
of the expression. Once it finds a matching curly brace for the '${',
a TemplateLiteralExprEnd token is emitted and the state is updated
accordingly.
When the Lexer is inside of a template literal, but not an expression,
and sees a '`', this must be the closing grave: a TemplateLiteralEnd
token is emitted.
The state required to correctly parse template strings consists of a
vector (for nesting) of two pieces of information: whether or not we
are in a template expression (as opposed to a template string); and
the count of the number of unmatched open curly braces we have seen
(only applicable if the Lexer is currently in a template expression).
TODO: Add support for template literal newlines in the JS REPL (this will
cause a syntax error currently):
> `foo
> bar`
'foo
bar'
2020-05-03 15:41:14 -07:00
|
|
|
{
|
|
|
|
StringBuilder string_builder;
|
|
|
|
|
2020-05-06 10:17:35 +01:00
|
|
|
for (auto& expression : m_expressions) {
|
2020-06-08 20:57:54 +02:00
|
|
|
auto expr = expression.execute(interpreter, global_object);
|
LibJS: Add template literals
Adds fully functioning template literals. Because template literals
contain expressions, most of the work has to be done in the Lexer rather
than the Parser. And because of the complexity of template literals
(expressions, nesting, escapes, etc), the Lexer needs to have some
template-related state.
When entering a new template literal, a TemplateLiteralStart token is
emitted. When inside a literal, all text will be parsed up until a '${'
or '`' (or EOF, but that's a syntax error) is seen, and then a
TemplateLiteralExprStart token is emitted. At this point, the Lexer
proceeds as normal, however it keeps track of the number of opening
and closing curly braces it has seen in order to determine the close
of the expression. Once it finds a matching curly brace for the '${',
a TemplateLiteralExprEnd token is emitted and the state is updated
accordingly.
When the Lexer is inside of a template literal, but not an expression,
and sees a '`', this must be the closing grave: a TemplateLiteralEnd
token is emitted.
The state required to correctly parse template strings consists of a
vector (for nesting) of two pieces of information: whether or not we
are in a template expression (as opposed to a template string); and
the count of the number of unmatched open curly braces we have seen
(only applicable if the Lexer is currently in a template expression).
TODO: Add support for template literal newlines in the JS REPL (this will
cause a syntax error currently):
> `foo
> bar`
'foo
bar'
2020-05-03 15:41:14 -07:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-05-15 13:39:24 +02:00
|
|
|
auto string = expr.to_string(interpreter);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
string_builder.append(string);
|
LibJS: Add template literals
Adds fully functioning template literals. Because template literals
contain expressions, most of the work has to be done in the Lexer rather
than the Parser. And because of the complexity of template literals
(expressions, nesting, escapes, etc), the Lexer needs to have some
template-related state.
When entering a new template literal, a TemplateLiteralStart token is
emitted. When inside a literal, all text will be parsed up until a '${'
or '`' (or EOF, but that's a syntax error) is seen, and then a
TemplateLiteralExprStart token is emitted. At this point, the Lexer
proceeds as normal, however it keeps track of the number of opening
and closing curly braces it has seen in order to determine the close
of the expression. Once it finds a matching curly brace for the '${',
a TemplateLiteralExprEnd token is emitted and the state is updated
accordingly.
When the Lexer is inside of a template literal, but not an expression,
and sees a '`', this must be the closing grave: a TemplateLiteralEnd
token is emitted.
The state required to correctly parse template strings consists of a
vector (for nesting) of two pieces of information: whether or not we
are in a template expression (as opposed to a template string); and
the count of the number of unmatched open curly braces we have seen
(only applicable if the Lexer is currently in a template expression).
TODO: Add support for template literal newlines in the JS REPL (this will
cause a syntax error currently):
> `foo
> bar`
'foo
bar'
2020-05-03 15:41:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return js_string(interpreter, string_builder.build());
|
|
|
|
}
|
|
|
|
|
2020-05-06 10:17:35 +01:00
|
|
|
void TaggedTemplateLiteral::dump(int indent) const
|
|
|
|
{
|
|
|
|
ASTNode::dump(indent);
|
|
|
|
print_indent(indent + 1);
|
|
|
|
printf("(Tag)\n");
|
|
|
|
m_tag->dump(indent + 2);
|
|
|
|
print_indent(indent + 1);
|
|
|
|
printf("(Template Literal)\n");
|
|
|
|
m_template_literal->dump(indent + 2);
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value TaggedTemplateLiteral::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-05-06 10:17:35 +01:00
|
|
|
{
|
2020-06-08 20:57:54 +02:00
|
|
|
auto tag = m_tag->execute(interpreter, global_object);
|
2020-05-06 10:17:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
if (!tag.is_function()) {
|
2020-06-09 22:48:01 -07:00
|
|
|
interpreter.throw_exception<TypeError>(ErrorType::NotAFunction, tag.to_string_without_side_effects().characters());
|
2020-05-06 10:17:35 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
auto& tag_function = tag.as_function();
|
|
|
|
auto& expressions = m_template_literal->expressions();
|
|
|
|
auto* strings = Array::create(interpreter.global_object());
|
|
|
|
MarkedValueList arguments(interpreter.heap());
|
|
|
|
arguments.append(strings);
|
|
|
|
for (size_t i = 0; i < expressions.size(); ++i) {
|
2020-06-08 20:57:54 +02:00
|
|
|
auto value = expressions[i].execute(interpreter, global_object);
|
2020-05-06 10:17:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
// tag`${foo}` -> "", foo, "" -> tag(["", ""], foo)
|
|
|
|
// tag`foo${bar}baz${qux}` -> "foo", bar, "baz", qux, "" -> tag(["foo", "baz", ""], bar, qux)
|
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
|
|
|
if (i % 2 == 0) {
|
|
|
|
strings->indexed_properties().append(value);
|
|
|
|
} else {
|
2020-05-06 10:17:35 +01:00
|
|
|
arguments.append(value);
|
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
|
|
|
}
|
2020-05-06 10:17:35 +01:00
|
|
|
}
|
2020-05-06 16:34:14 -07:00
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
auto* raw_strings = Array::create(global_object);
|
2020-05-06 16:34:14 -07:00
|
|
|
for (auto& raw_string : m_template_literal->raw_strings()) {
|
2020-06-08 20:57:54 +02:00
|
|
|
auto value = raw_string.execute(interpreter, global_object);
|
2020-05-06 16:34:14 -07:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
|
|
|
raw_strings->indexed_properties().append(value);
|
2020-05-06 16:34:14 -07:00
|
|
|
}
|
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
|
|
|
strings->define_property("raw", raw_strings, 0);
|
2020-06-07 10:53:14 -07:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-05-06 16:34:14 -07:00
|
|
|
|
2020-05-06 10:17:35 +01:00
|
|
|
return interpreter.call(tag_function, js_undefined(), move(arguments));
|
|
|
|
}
|
|
|
|
|
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-06-08 20:57:54 +02:00
|
|
|
Value TryStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-03-24 14:03:55 +01:00
|
|
|
{
|
2020-06-08 21:25:16 +02:00
|
|
|
interpreter.run(global_object, block(), {}, ScopeType::Try);
|
2020-03-24 14:37:39 +01:00
|
|
|
if (auto* exception = interpreter.exception()) {
|
|
|
|
if (m_handler) {
|
|
|
|
interpreter.clear_exception();
|
2020-04-06 19:22:12 +02:00
|
|
|
ArgumentVector arguments { { m_handler->parameter(), exception->value() } };
|
2020-06-08 21:25:16 +02:00
|
|
|
interpreter.run(global_object, m_handler->body(), move(arguments));
|
2020-03-24 14:37:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_finalizer)
|
2020-06-08 20:57:54 +02:00
|
|
|
m_finalizer->execute(interpreter, global_object);
|
2020-03-24 14:37:39 +01:00
|
|
|
|
2020-04-06 20:24:45 +02:00
|
|
|
return js_undefined();
|
2020-03-24 14:03:55 +01:00
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value CatchClause::execute(Interpreter&, GlobalObject&) const
|
2020-03-24 14:03:55 +01:00
|
|
|
{
|
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-06-08 20:57:54 +02:00
|
|
|
Value ThrowStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-03-24 22:03:50 +01:00
|
|
|
{
|
2020-06-08 20:57:54 +02:00
|
|
|
auto value = m_argument->execute(interpreter, global_object);
|
2020-03-27 15:35:35 +01:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
return interpreter.throw_exception(value);
|
2020-03-24 22:03:50 +01:00
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value SwitchStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-03-29 13:09:54 +02:00
|
|
|
{
|
2020-06-08 20:57:54 +02:00
|
|
|
auto discriminant_result = m_discriminant->execute(interpreter, global_object);
|
2020-03-29 14:34:25 +02:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
bool falling_through = false;
|
|
|
|
|
|
|
|
for (auto& switch_case : m_cases) {
|
|
|
|
if (!falling_through && switch_case.test()) {
|
2020-06-08 20:57:54 +02:00
|
|
|
auto test_result = switch_case.test()->execute(interpreter, global_object);
|
2020-03-29 14:34:25 +02:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-05-07 17:09:00 -07:00
|
|
|
if (!strict_eq(interpreter, discriminant_result, test_result))
|
2020-03-29 14:34:25 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
falling_through = true;
|
|
|
|
|
|
|
|
for (auto& statement : switch_case.consequent()) {
|
2020-06-08 20:57:54 +02:00
|
|
|
statement.execute(interpreter, global_object);
|
2020-03-29 14:34:25 +02:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-04-05 00:09:48 +02:00
|
|
|
if (interpreter.should_unwind()) {
|
2020-05-28 13:36:59 -07:00
|
|
|
if (interpreter.should_unwind_until(ScopeType::Breakable, m_label)) {
|
2020-04-05 00:09:48 +02:00
|
|
|
interpreter.stop_unwind();
|
|
|
|
return {};
|
|
|
|
}
|
2020-03-29 14:34:25 +02:00
|
|
|
return {};
|
2020-04-05 00:09:48 +02:00
|
|
|
}
|
2020-03-29 14:34:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 20:24:45 +02:00
|
|
|
return js_undefined();
|
2020-03-29 13:09:54 +02:00
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value SwitchCase::execute(Interpreter&, GlobalObject&) const
|
2020-03-29 13:09:54 +02:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value BreakStatement::execute(Interpreter& interpreter, GlobalObject&) const
|
2020-03-29 13:09:54 +02:00
|
|
|
{
|
2020-05-28 13:36:59 -07:00
|
|
|
interpreter.unwind(ScopeType::Breakable, m_target_label);
|
2020-04-06 20:24:45 +02:00
|
|
|
return js_undefined();
|
2020-03-29 13:09:54 +02:00
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value ContinueStatement::execute(Interpreter& interpreter, GlobalObject&) const
|
2020-04-05 00:22:42 +02:00
|
|
|
{
|
2020-05-28 13:36:59 -07:00
|
|
|
interpreter.unwind(ScopeType::Continuable, m_target_label);
|
2020-04-06 20:24:45 +02:00
|
|
|
return js_undefined();
|
2020-04-05 00:22:42 +02:00
|
|
|
}
|
|
|
|
|
2020-03-29 13:09:54 +02:00
|
|
|
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);
|
2020-05-04 19:01:24 +01:00
|
|
|
print_indent(indent + 1);
|
2020-03-29 13:09:54 +02:00
|
|
|
if (m_test) {
|
|
|
|
printf("(Test)\n");
|
2020-05-04 19:01:24 +01:00
|
|
|
m_test->dump(indent + 2);
|
2020-03-29 13:09:54 +02:00
|
|
|
} else {
|
|
|
|
printf("(Default)\n");
|
|
|
|
}
|
2020-05-04 19:01:24 +01:00
|
|
|
print_indent(indent + 1);
|
2020-03-29 13:09:54 +02:00
|
|
|
printf("(Consequent)\n");
|
2020-05-04 19:01:24 +01:00
|
|
|
for (auto& statement : m_consequent)
|
|
|
|
statement.dump(indent + 2);
|
2020-03-29 13:09:54 +02:00
|
|
|
}
|
2020-04-01 18:31:24 +01:00
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value ConditionalExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-04-03 12:14:28 +02:00
|
|
|
{
|
2020-06-08 20:57:54 +02:00
|
|
|
auto test_result = m_test->execute(interpreter, global_object);
|
2020-04-03 12:14:28 +02:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
Value result;
|
|
|
|
if (test_result.to_boolean()) {
|
2020-06-08 20:57:54 +02:00
|
|
|
result = m_consequent->execute(interpreter, global_object);
|
2020-04-03 12:14:28 +02:00
|
|
|
} else {
|
2020-06-08 20:57:54 +02:00
|
|
|
result = m_alternate->execute(interpreter, global_object);
|
2020-04-03 12:14:28 +02:00
|
|
|
}
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConditionalExpression::dump(int indent) const
|
|
|
|
{
|
|
|
|
ASTNode::dump(indent);
|
LibJS: Add template literals
Adds fully functioning template literals. Because template literals
contain expressions, most of the work has to be done in the Lexer rather
than the Parser. And because of the complexity of template literals
(expressions, nesting, escapes, etc), the Lexer needs to have some
template-related state.
When entering a new template literal, a TemplateLiteralStart token is
emitted. When inside a literal, all text will be parsed up until a '${'
or '`' (or EOF, but that's a syntax error) is seen, and then a
TemplateLiteralExprStart token is emitted. At this point, the Lexer
proceeds as normal, however it keeps track of the number of opening
and closing curly braces it has seen in order to determine the close
of the expression. Once it finds a matching curly brace for the '${',
a TemplateLiteralExprEnd token is emitted and the state is updated
accordingly.
When the Lexer is inside of a template literal, but not an expression,
and sees a '`', this must be the closing grave: a TemplateLiteralEnd
token is emitted.
The state required to correctly parse template strings consists of a
vector (for nesting) of two pieces of information: whether or not we
are in a template expression (as opposed to a template string); and
the count of the number of unmatched open curly braces we have seen
(only applicable if the Lexer is currently in a template expression).
TODO: Add support for template literal newlines in the JS REPL (this will
cause a syntax error currently):
> `foo
> bar`
'foo
bar'
2020-05-03 15:41:14 -07:00
|
|
|
print_indent(indent + 1);
|
2020-04-03 12:14:28 +02:00
|
|
|
printf("(Test)\n");
|
LibJS: Add template literals
Adds fully functioning template literals. Because template literals
contain expressions, most of the work has to be done in the Lexer rather
than the Parser. And because of the complexity of template literals
(expressions, nesting, escapes, etc), the Lexer needs to have some
template-related state.
When entering a new template literal, a TemplateLiteralStart token is
emitted. When inside a literal, all text will be parsed up until a '${'
or '`' (or EOF, but that's a syntax error) is seen, and then a
TemplateLiteralExprStart token is emitted. At this point, the Lexer
proceeds as normal, however it keeps track of the number of opening
and closing curly braces it has seen in order to determine the close
of the expression. Once it finds a matching curly brace for the '${',
a TemplateLiteralExprEnd token is emitted and the state is updated
accordingly.
When the Lexer is inside of a template literal, but not an expression,
and sees a '`', this must be the closing grave: a TemplateLiteralEnd
token is emitted.
The state required to correctly parse template strings consists of a
vector (for nesting) of two pieces of information: whether or not we
are in a template expression (as opposed to a template string); and
the count of the number of unmatched open curly braces we have seen
(only applicable if the Lexer is currently in a template expression).
TODO: Add support for template literal newlines in the JS REPL (this will
cause a syntax error currently):
> `foo
> bar`
'foo
bar'
2020-05-03 15:41:14 -07:00
|
|
|
m_test->dump(indent + 2);
|
|
|
|
print_indent(indent + 1);
|
2020-04-03 12:14:28 +02:00
|
|
|
printf("(Consequent)\n");
|
LibJS: Add template literals
Adds fully functioning template literals. Because template literals
contain expressions, most of the work has to be done in the Lexer rather
than the Parser. And because of the complexity of template literals
(expressions, nesting, escapes, etc), the Lexer needs to have some
template-related state.
When entering a new template literal, a TemplateLiteralStart token is
emitted. When inside a literal, all text will be parsed up until a '${'
or '`' (or EOF, but that's a syntax error) is seen, and then a
TemplateLiteralExprStart token is emitted. At this point, the Lexer
proceeds as normal, however it keeps track of the number of opening
and closing curly braces it has seen in order to determine the close
of the expression. Once it finds a matching curly brace for the '${',
a TemplateLiteralExprEnd token is emitted and the state is updated
accordingly.
When the Lexer is inside of a template literal, but not an expression,
and sees a '`', this must be the closing grave: a TemplateLiteralEnd
token is emitted.
The state required to correctly parse template strings consists of a
vector (for nesting) of two pieces of information: whether or not we
are in a template expression (as opposed to a template string); and
the count of the number of unmatched open curly braces we have seen
(only applicable if the Lexer is currently in a template expression).
TODO: Add support for template literal newlines in the JS REPL (this will
cause a syntax error currently):
> `foo
> bar`
'foo
bar'
2020-05-03 15:41:14 -07:00
|
|
|
m_consequent->dump(indent + 2);
|
|
|
|
print_indent(indent + 1);
|
2020-04-03 12:14:28 +02:00
|
|
|
printf("(Alternate)\n");
|
LibJS: Add template literals
Adds fully functioning template literals. Because template literals
contain expressions, most of the work has to be done in the Lexer rather
than the Parser. And because of the complexity of template literals
(expressions, nesting, escapes, etc), the Lexer needs to have some
template-related state.
When entering a new template literal, a TemplateLiteralStart token is
emitted. When inside a literal, all text will be parsed up until a '${'
or '`' (or EOF, but that's a syntax error) is seen, and then a
TemplateLiteralExprStart token is emitted. At this point, the Lexer
proceeds as normal, however it keeps track of the number of opening
and closing curly braces it has seen in order to determine the close
of the expression. Once it finds a matching curly brace for the '${',
a TemplateLiteralExprEnd token is emitted and the state is updated
accordingly.
When the Lexer is inside of a template literal, but not an expression,
and sees a '`', this must be the closing grave: a TemplateLiteralEnd
token is emitted.
The state required to correctly parse template strings consists of a
vector (for nesting) of two pieces of information: whether or not we
are in a template expression (as opposed to a template string); and
the count of the number of unmatched open curly braces we have seen
(only applicable if the Lexer is currently in a template expression).
TODO: Add support for template literal newlines in the JS REPL (this will
cause a syntax error currently):
> `foo
> bar`
'foo
bar'
2020-05-03 15:41:14 -07:00
|
|
|
m_alternate->dump(indent + 2);
|
2020-04-03 12:14:28 +02:00
|
|
|
}
|
|
|
|
|
2020-04-07 15:11:05 +02:00
|
|
|
void SequenceExpression::dump(int indent) const
|
|
|
|
{
|
|
|
|
ASTNode::dump(indent);
|
|
|
|
for (auto& expression : m_expressions)
|
|
|
|
expression.dump(indent + 1);
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value SequenceExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
2020-04-07 15:11:05 +02:00
|
|
|
{
|
|
|
|
Value last_value;
|
|
|
|
for (auto& expression : m_expressions) {
|
2020-06-08 20:57:54 +02:00
|
|
|
last_value = expression.execute(interpreter, global_object);
|
2020-04-07 15:11:05 +02:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return last_value;
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:57:54 +02:00
|
|
|
Value DebuggerStatement::execute(Interpreter&, GlobalObject&) const
|
2020-04-30 17:26:27 +01:00
|
|
|
{
|
|
|
|
dbg() << "Sorry, no JavaScript debugger available (yet)!";
|
|
|
|
return js_undefined();
|
|
|
|
}
|
|
|
|
|
2020-04-13 16:42:54 +02:00
|
|
|
void ScopeNode::add_variables(NonnullRefPtrVector<VariableDeclaration> variables)
|
|
|
|
{
|
|
|
|
m_variables.append(move(variables));
|
|
|
|
}
|
|
|
|
|
2020-06-04 14:48:36 +02:00
|
|
|
void ScopeNode::add_functions(NonnullRefPtrVector<FunctionDeclaration> functions)
|
|
|
|
{
|
|
|
|
m_functions.append(move(functions));
|
|
|
|
}
|
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
}
|