2020-03-07 18:42:11 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-03-09 20:29:22 +00:00
|
|
|
#include <AK/Badge.h>
|
2020-05-04 10:34:49 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-03-07 18:42:11 +00:00
|
|
|
#include <LibJS/AST.h>
|
|
|
|
#include <LibJS/Interpreter.h>
|
2020-03-24 13:37:39 +00:00
|
|
|
#include <LibJS/Runtime/Error.h>
|
2020-03-16 13:20:30 +00:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-04-15 19:58:22 +00:00
|
|
|
#include <LibJS/Runtime/LexicalEnvironment.h>
|
2020-04-19 15:24:56 +00:00
|
|
|
#include <LibJS/Runtime/MarkedValueList.h>
|
2020-03-16 13:20:30 +00:00
|
|
|
#include <LibJS/Runtime/NativeFunction.h>
|
|
|
|
#include <LibJS/Runtime/Object.h>
|
2020-04-27 10:37:27 +00:00
|
|
|
#include <LibJS/Runtime/Reference.h>
|
2020-04-02 17:32:21 +00:00
|
|
|
#include <LibJS/Runtime/Shape.h>
|
2020-03-16 13:20:30 +00:00
|
|
|
#include <LibJS/Runtime/Value.h>
|
2020-03-07 18:42:11 +00:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
Interpreter::Interpreter()
|
2020-03-08 18:23:58 +00:00
|
|
|
: m_heap(*this)
|
2020-05-01 12:40:43 +00:00
|
|
|
, m_console(*this)
|
2020-03-07 18:42:11 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Interpreter::~Interpreter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-04-06 17:22:12 +00:00
|
|
|
Value Interpreter::run(const Statement& statement, ArgumentVector arguments, ScopeType scope_type)
|
2020-03-07 18:42:11 +00:00
|
|
|
{
|
2020-04-15 19:58:22 +00:00
|
|
|
if (statement.is_program()) {
|
|
|
|
if (m_call_stack.is_empty()) {
|
2020-04-20 22:53:28 +00:00
|
|
|
CallFrame global_call_frame;
|
|
|
|
global_call_frame.this_value = m_global_object;
|
|
|
|
global_call_frame.function_name = "(global execution context)";
|
|
|
|
global_call_frame.environment = heap().allocate<LexicalEnvironment>();
|
|
|
|
m_call_stack.append(move(global_call_frame));
|
2020-04-15 19:58:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-23 15:46:41 +00:00
|
|
|
if (!statement.is_scope_node())
|
|
|
|
return statement.execute(*this);
|
|
|
|
|
2020-04-02 07:53:27 +00:00
|
|
|
auto& block = static_cast<const ScopeNode&>(statement);
|
2020-03-23 15:46:41 +00:00
|
|
|
enter_scope(block, move(arguments), scope_type);
|
2020-03-07 18:42:11 +00:00
|
|
|
|
2020-04-06 18:24:45 +00:00
|
|
|
m_last_value = js_undefined();
|
2020-03-23 15:46:41 +00:00
|
|
|
for (auto& node : block.children()) {
|
2020-04-04 21:45:13 +00:00
|
|
|
m_last_value = node.execute(*this);
|
2020-03-23 18:19:03 +00:00
|
|
|
if (m_unwind_until != ScopeType::None)
|
|
|
|
break;
|
2020-03-07 18:42:11 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 21:45:13 +00:00
|
|
|
bool did_return = m_unwind_until == ScopeType::Function;
|
|
|
|
|
2020-03-23 18:19:03 +00:00
|
|
|
if (m_unwind_until == scope_type)
|
|
|
|
m_unwind_until = ScopeType::None;
|
|
|
|
|
2020-03-23 15:46:41 +00:00
|
|
|
exit_scope(block);
|
2020-04-04 21:45:13 +00:00
|
|
|
|
|
|
|
return did_return ? m_last_value : js_undefined();
|
2020-03-07 18:42:11 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 17:22:12 +00:00
|
|
|
void Interpreter::enter_scope(const ScopeNode& scope_node, ArgumentVector arguments, ScopeType scope_type)
|
2020-03-07 18:42:11 +00:00
|
|
|
{
|
2020-04-15 19:58:22 +00:00
|
|
|
if (scope_type == ScopeType::Function) {
|
|
|
|
m_scope_stack.append({ scope_type, scope_node, false });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-08 09:59:18 +00:00
|
|
|
HashMap<FlyString, Variable> scope_variables_with_declaration_kind;
|
2020-04-13 15:23:42 +00:00
|
|
|
scope_variables_with_declaration_kind.ensure_capacity(16);
|
2020-04-13 14:42:54 +00:00
|
|
|
|
|
|
|
for (auto& declaration : scope_node.variables()) {
|
|
|
|
for (auto& declarator : declaration.declarations()) {
|
|
|
|
if (scope_node.is_program())
|
|
|
|
global_object().put(declarator.id().string(), js_undefined());
|
|
|
|
else
|
|
|
|
scope_variables_with_declaration_kind.set(declarator.id().string(), { js_undefined(), declaration.declaration_kind() });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 18:53:31 +00:00
|
|
|
for (auto& argument : arguments) {
|
2020-04-08 09:59:18 +00:00
|
|
|
scope_variables_with_declaration_kind.set(argument.name, { argument.value, DeclarationKind::Var });
|
2020-03-12 12:24:34 +00:00
|
|
|
}
|
2020-04-13 14:42:54 +00:00
|
|
|
|
2020-04-15 19:58:22 +00:00
|
|
|
bool pushed_lexical_environment = false;
|
|
|
|
|
2020-04-16 08:24:45 +00:00
|
|
|
if (!scope_variables_with_declaration_kind.is_empty()) {
|
|
|
|
auto* block_lexical_environment = heap().allocate<LexicalEnvironment>(move(scope_variables_with_declaration_kind), current_environment());
|
|
|
|
m_call_stack.last().environment = block_lexical_environment;
|
|
|
|
pushed_lexical_environment = true;
|
2020-04-15 19:58:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_scope_stack.append({ scope_type, scope_node, pushed_lexical_environment });
|
2020-03-07 18:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Interpreter::exit_scope(const ScopeNode& scope_node)
|
|
|
|
{
|
2020-03-27 10:34:58 +00:00
|
|
|
while (!m_scope_stack.is_empty()) {
|
|
|
|
auto popped_scope = m_scope_stack.take_last();
|
2020-04-15 19:58:22 +00:00
|
|
|
if (popped_scope.pushed_environment)
|
|
|
|
m_call_stack.last().environment = m_call_stack.last().environment->parent();
|
2020-03-27 10:34:58 +00:00
|
|
|
if (popped_scope.scope_node.ptr() == &scope_node)
|
|
|
|
break;
|
|
|
|
}
|
2020-03-07 18:42:11 +00:00
|
|
|
|
2020-03-23 18:19:03 +00:00
|
|
|
// If we unwind all the way, just reset m_unwind_until so that future "return" doesn't break.
|
|
|
|
if (m_scope_stack.is_empty())
|
|
|
|
m_unwind_until = ScopeType::None;
|
2020-03-07 18:42:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 10:07:55 +00:00
|
|
|
void Interpreter::set_variable(const FlyString& name, Value value, bool first_assignment)
|
2020-03-09 20:13:55 +00:00
|
|
|
{
|
2020-04-19 14:12:41 +00:00
|
|
|
if (m_call_stack.size()) {
|
|
|
|
for (auto* environment = current_environment(); environment; environment = environment->parent()) {
|
|
|
|
auto possible_match = environment->get(name);
|
|
|
|
if (possible_match.has_value()) {
|
|
|
|
if (!first_assignment && possible_match.value().declaration_kind == DeclarationKind::Const) {
|
|
|
|
throw_exception<TypeError>("Assignment to constant variable");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
environment->set(name, { value, possible_match.value().declaration_kind });
|
2020-04-12 22:50:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-03-09 20:13:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
global_object().put(move(name), move(value));
|
|
|
|
}
|
|
|
|
|
2020-04-25 16:43:34 +00:00
|
|
|
Value Interpreter::get_variable(const FlyString& name)
|
2020-03-09 20:13:55 +00:00
|
|
|
{
|
2020-04-19 14:12:41 +00:00
|
|
|
if (m_call_stack.size()) {
|
|
|
|
for (auto* environment = current_environment(); environment; environment = environment->parent()) {
|
|
|
|
auto possible_match = environment->get(name);
|
|
|
|
if (possible_match.has_value())
|
|
|
|
return possible_match.value().value;
|
|
|
|
}
|
2020-03-09 20:13:55 +00:00
|
|
|
}
|
|
|
|
return global_object().get(name);
|
|
|
|
}
|
|
|
|
|
2020-04-27 10:37:27 +00:00
|
|
|
Reference Interpreter::get_reference(const FlyString& name)
|
|
|
|
{
|
|
|
|
if (m_call_stack.size()) {
|
|
|
|
for (auto* environment = current_environment(); environment; environment = environment->parent()) {
|
|
|
|
auto possible_match = environment->get(name);
|
|
|
|
if (possible_match.has_value())
|
|
|
|
return { Reference::LocalVariable, name };
|
|
|
|
}
|
|
|
|
}
|
2020-04-28 12:44:48 +00:00
|
|
|
return { Reference::GlobalVariable, name };
|
2020-04-27 10:37:27 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 14:12:34 +00:00
|
|
|
void Interpreter::gather_roots(Badge<Heap>, HashTable<Cell*>& roots)
|
2020-03-09 20:29:22 +00:00
|
|
|
{
|
|
|
|
roots.set(m_global_object);
|
2020-03-24 13:37:39 +00:00
|
|
|
roots.set(m_exception);
|
2020-03-09 20:29:22 +00:00
|
|
|
|
2020-04-04 21:45:13 +00:00
|
|
|
if (m_last_value.is_cell())
|
|
|
|
roots.set(m_last_value.as_cell());
|
|
|
|
|
2020-03-17 10:00:09 +00:00
|
|
|
for (auto& call_frame : m_call_stack) {
|
|
|
|
if (call_frame.this_value.is_cell())
|
|
|
|
roots.set(call_frame.this_value.as_cell());
|
|
|
|
for (auto& argument : call_frame.arguments) {
|
|
|
|
if (argument.is_cell())
|
|
|
|
roots.set(argument.as_cell());
|
|
|
|
}
|
2020-04-15 19:58:22 +00:00
|
|
|
roots.set(call_frame.environment);
|
2020-03-15 14:07:49 +00:00
|
|
|
}
|
2020-03-09 20:29:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 11:43:57 +00:00
|
|
|
Value Interpreter::call(Function& function, Value this_value, Optional<MarkedValueList> arguments)
|
2020-03-18 14:20:49 +00:00
|
|
|
{
|
|
|
|
auto& call_frame = push_call_frame();
|
2020-04-29 11:43:57 +00:00
|
|
|
call_frame.function_name = function.name();
|
2020-03-18 14:20:49 +00:00
|
|
|
call_frame.this_value = this_value;
|
2020-04-19 15:24:56 +00:00
|
|
|
if (arguments.has_value())
|
|
|
|
call_frame.arguments = arguments.value().values();
|
2020-04-29 11:43:57 +00:00
|
|
|
call_frame.environment = function.create_environment();
|
|
|
|
auto result = function.call(*this);
|
2020-03-18 14:20:49 +00:00
|
|
|
pop_call_frame();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-05-01 10:06:27 +00:00
|
|
|
Value Interpreter::construct(Function& function, Function& new_target, Optional<MarkedValueList> arguments)
|
|
|
|
{
|
|
|
|
auto& call_frame = push_call_frame();
|
|
|
|
call_frame.function_name = function.name();
|
|
|
|
if (arguments.has_value())
|
|
|
|
call_frame.arguments = arguments.value().values();
|
|
|
|
call_frame.environment = function.create_environment();
|
|
|
|
|
|
|
|
auto* new_object = Object::create_empty(*this, global_object());
|
|
|
|
auto prototype = new_target.get("prototype");
|
|
|
|
if (prototype.is_object())
|
|
|
|
new_object->set_prototype(&prototype.as_object());
|
|
|
|
call_frame.this_value = new_object;
|
|
|
|
auto result = function.construct(*this);
|
|
|
|
|
|
|
|
pop_call_frame();
|
|
|
|
|
|
|
|
if (exception())
|
|
|
|
return {};
|
|
|
|
if (result.is_object())
|
|
|
|
return result;
|
|
|
|
return new_object;
|
|
|
|
}
|
|
|
|
|
2020-03-24 21:03:50 +00:00
|
|
|
Value Interpreter::throw_exception(Exception* exception)
|
2020-03-24 13:37:39 +00:00
|
|
|
{
|
2020-04-04 20:14:03 +00:00
|
|
|
if (exception->value().is_object() && exception->value().as_object().is_error()) {
|
|
|
|
auto& error = static_cast<Error&>(exception->value().as_object());
|
|
|
|
dbg() << "Throwing JavaScript Error: " << error.name() << ", " << error.message();
|
2020-04-16 18:23:03 +00:00
|
|
|
|
|
|
|
for (ssize_t i = m_call_stack.size() - 1; i >= 0; --i) {
|
|
|
|
auto function_name = m_call_stack[i].function_name;
|
|
|
|
if (function_name.is_empty())
|
|
|
|
function_name = "<anonymous>";
|
|
|
|
dbg() << " " << function_name;
|
|
|
|
}
|
2020-04-04 20:14:03 +00:00
|
|
|
}
|
2020-03-24 13:37:39 +00:00
|
|
|
m_exception = exception;
|
|
|
|
unwind(ScopeType::Try);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-04-08 09:05:38 +00:00
|
|
|
GlobalObject& Interpreter::global_object()
|
|
|
|
{
|
|
|
|
return static_cast<GlobalObject&>(*m_global_object);
|
|
|
|
}
|
|
|
|
|
|
|
|
const GlobalObject& Interpreter::global_object() const
|
|
|
|
{
|
|
|
|
return static_cast<const GlobalObject&>(*m_global_object);
|
|
|
|
}
|
|
|
|
|
2020-05-04 12:18:15 +00:00
|
|
|
String Interpreter::join_arguments() const
|
2020-05-04 10:34:49 +00:00
|
|
|
{
|
|
|
|
StringBuilder joined_arguments;
|
|
|
|
for (size_t i = 0; i < argument_count(); ++i) {
|
|
|
|
joined_arguments.append(argument(i).to_string().characters());
|
|
|
|
if (i != argument_count() - 1)
|
|
|
|
joined_arguments.append(' ');
|
|
|
|
}
|
|
|
|
return joined_arguments.build();
|
|
|
|
}
|
|
|
|
|
2020-05-04 12:18:15 +00:00
|
|
|
Vector<String> Interpreter::get_trace() const
|
|
|
|
{
|
|
|
|
Vector<String> trace;
|
|
|
|
// -2 to skip the console.trace() call frame
|
|
|
|
for (ssize_t i = m_call_stack.size() - 2; i >= 0; --i)
|
|
|
|
trace.append(m_call_stack[i].function_name);
|
|
|
|
return trace;
|
|
|
|
}
|
|
|
|
|
2020-03-07 18:42:11 +00:00
|
|
|
}
|