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-06-04 12:48:36 +00:00
|
|
|
#include <LibJS/Runtime/ScriptFunction.h>
|
2020-04-02 17:32:21 +00:00
|
|
|
#include <LibJS/Runtime/Shape.h>
|
2020-04-30 06:25:21 +00:00
|
|
|
#include <LibJS/Runtime/SymbolObject.h>
|
2020-03-16 13:20:30 +00:00
|
|
|
#include <LibJS/Runtime/Value.h>
|
2020-03-07 18:42:11 +00:00
|
|
|
|
2020-07-03 01:01:09 +00:00
|
|
|
//#define INTERPRETER_DEBUG
|
|
|
|
|
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
|
|
|
{
|
2020-07-09 22:34:20 +00:00
|
|
|
#define __JS_ENUMERATE(SymbolName, snake_name) \
|
|
|
|
m_well_known_symbol_##snake_name = js_symbol(*this, "Symbol." #SymbolName, false);
|
|
|
|
JS_ENUMERATE_WELL_KNOWN_SYMBOLS
|
|
|
|
#undef __JS_ENUMERATE
|
2020-03-07 18:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Interpreter::~Interpreter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-06-08 19:25:16 +00:00
|
|
|
Value Interpreter::run(GlobalObject& global_object, const Statement& statement, ArgumentVector arguments, ScopeType scope_type)
|
2020-03-07 18:42:11 +00:00
|
|
|
{
|
2020-08-11 15:45:57 +00:00
|
|
|
ASSERT(!exception());
|
|
|
|
|
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;
|
2020-06-08 19:25:16 +00:00
|
|
|
global_call_frame.this_value = &global_object;
|
2020-04-20 22:53:28 +00:00
|
|
|
global_call_frame.function_name = "(global execution context)";
|
2020-06-08 18:31:21 +00:00
|
|
|
global_call_frame.environment = heap().allocate<LexicalEnvironment>(global_object, LexicalEnvironment::EnvironmentRecordType::Global);
|
|
|
|
global_call_frame.environment->bind_this_value(&global_object);
|
|
|
|
if (exception())
|
|
|
|
return {};
|
2020-04-20 22:53:28 +00:00
|
|
|
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())
|
2020-06-08 19:25:16 +00:00
|
|
|
return statement.execute(*this, global_object);
|
2020-03-23 15:46:41 +00:00
|
|
|
|
2020-04-02 07:53:27 +00:00
|
|
|
auto& block = static_cast<const ScopeNode&>(statement);
|
2020-06-08 19:25:16 +00:00
|
|
|
enter_scope(block, move(arguments), scope_type, global_object);
|
2020-03-07 18:42:11 +00:00
|
|
|
|
2020-06-08 11:08:35 +00:00
|
|
|
if (block.children().is_empty())
|
|
|
|
m_last_value = js_undefined();
|
|
|
|
|
2020-03-23 15:46:41 +00:00
|
|
|
for (auto& node : block.children()) {
|
2020-06-08 19:25:16 +00:00
|
|
|
m_last_value = node.execute(*this, global_object);
|
2020-05-28 20:36:59 +00:00
|
|
|
if (should_unwind()) {
|
2020-08-28 16:52:22 +00:00
|
|
|
if (!block.label().is_null() && should_unwind_until(ScopeType::Breakable, block.label()))
|
2020-05-28 20:36:59 +00:00
|
|
|
stop_unwind();
|
2020-03-23 18:19:03 +00:00
|
|
|
break;
|
2020-05-28 20:36:59 +00:00
|
|
|
}
|
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-06-08 19:25:16 +00:00
|
|
|
void Interpreter::enter_scope(const ScopeNode& scope_node, ArgumentVector arguments, ScopeType scope_type, GlobalObject& global_object)
|
2020-03-07 18:42:11 +00:00
|
|
|
{
|
2020-06-04 12:48:36 +00:00
|
|
|
for (auto& declaration : scope_node.functions()) {
|
2020-06-08 19:25:16 +00:00
|
|
|
auto* function = ScriptFunction::create(global_object, declaration.name(), declaration.body(), declaration.parameters(), declaration.function_length(), current_environment());
|
|
|
|
set_variable(declaration.name(), function, global_object);
|
2020-06-04 12:48:36 +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()) {
|
2020-06-07 17:53:14 +00:00
|
|
|
if (scope_node.is_program()) {
|
2020-06-08 19:25:16 +00:00
|
|
|
global_object.put(declarator.id().string(), js_undefined());
|
2020-06-07 17:53:14 +00:00
|
|
|
if (exception())
|
|
|
|
return;
|
|
|
|
} else {
|
2020-04-13 14:42:54 +00:00
|
|
|
scope_variables_with_declaration_kind.set(declarator.id().string(), { js_undefined(), declaration.declaration_kind() });
|
2020-06-07 17:53:14 +00:00
|
|
|
}
|
2020-04-13 14:42:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()) {
|
2020-06-20 13:40:48 +00:00
|
|
|
auto* block_lexical_environment = heap().allocate<LexicalEnvironment>(global_object, move(scope_variables_with_declaration_kind), current_environment());
|
2020-04-16 08:24:45 +00:00
|
|
|
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-06-08 19:25:16 +00:00
|
|
|
void Interpreter::set_variable(const FlyString& name, Value value, GlobalObject& global_object, 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) {
|
2020-06-10 05:48:01 +00:00
|
|
|
throw_exception<TypeError>(ErrorType::InvalidAssignToConst);
|
2020-04-19 14:12:41 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 19:25:16 +00:00
|
|
|
global_object.put(move(name), move(value));
|
2020-03-09 20:13:55 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 19:25:16 +00:00
|
|
|
Value Interpreter::get_variable(const FlyString& name, GlobalObject& global_object)
|
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
|
|
|
}
|
2020-06-08 19:25:16 +00:00
|
|
|
auto value = global_object.get(name);
|
2020-06-08 11:08:35 +00:00
|
|
|
if (m_underscore_is_last_value && name == "_" && value.is_empty())
|
|
|
|
return m_last_value;
|
|
|
|
return value;
|
2020-03-09 20:13:55 +00:00
|
|
|
}
|
|
|
|
|
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-07-06 23:57:22 +00:00
|
|
|
Symbol* Interpreter::get_global_symbol(const String& description)
|
|
|
|
{
|
|
|
|
auto result = m_global_symbol_map.get(description);
|
|
|
|
if (result.has_value())
|
|
|
|
return result.value();
|
|
|
|
|
|
|
|
auto new_global_symbol = js_symbol(*this, description, true);
|
|
|
|
m_global_symbol_map.set(description, new_global_symbol);
|
|
|
|
return new_global_symbol;
|
|
|
|
}
|
|
|
|
|
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-04-30 06:25:21 +00:00
|
|
|
|
2020-07-09 22:34:20 +00:00
|
|
|
#define __JS_ENUMERATE(SymbolName, snake_name) \
|
|
|
|
roots.set(well_known_symbol_##snake_name());
|
|
|
|
JS_ENUMERATE_WELL_KNOWN_SYMBOLS
|
|
|
|
#undef __JS_ENUMERATE
|
2020-07-06 23:57:22 +00:00
|
|
|
|
|
|
|
for (auto& symbol : m_global_symbol_map)
|
|
|
|
roots.set(symbol.value);
|
2020-03-09 20:29:22 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 17:48:32 +00:00
|
|
|
Value Interpreter::call_internal(Function& function, Value this_value, Optional<MarkedValueList> arguments)
|
2020-03-18 14:20:49 +00:00
|
|
|
{
|
2020-08-14 15:30:34 +00:00
|
|
|
ASSERT(!exception());
|
|
|
|
|
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-05-30 06:07:02 +00:00
|
|
|
call_frame.this_value = function.bound_this().value_or(this_value);
|
|
|
|
call_frame.arguments = function.bound_arguments();
|
2020-04-19 15:24:56 +00:00
|
|
|
if (arguments.has_value())
|
2020-05-30 06:07:02 +00:00
|
|
|
call_frame.arguments.append(arguments.value().values());
|
2020-04-29 11:43:57 +00:00
|
|
|
call_frame.environment = function.create_environment();
|
2020-06-08 18:31:21 +00:00
|
|
|
|
|
|
|
ASSERT(call_frame.environment->this_binding_status() == LexicalEnvironment::ThisBindingStatus::Uninitialized);
|
|
|
|
call_frame.environment->bind_this_value(call_frame.this_value);
|
|
|
|
|
2020-04-29 11:43:57 +00:00
|
|
|
auto result = function.call(*this);
|
2020-03-18 14:20:49 +00:00
|
|
|
pop_call_frame();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-06-20 15:53:33 +00:00
|
|
|
Value Interpreter::construct(Function& function, Function& new_target, Optional<MarkedValueList> arguments, GlobalObject& global_object)
|
2020-05-01 10:06:27 +00:00
|
|
|
{
|
|
|
|
auto& call_frame = push_call_frame();
|
|
|
|
call_frame.function_name = function.name();
|
2020-06-08 18:31:21 +00:00
|
|
|
call_frame.arguments = function.bound_arguments();
|
2020-05-01 10:06:27 +00:00
|
|
|
if (arguments.has_value())
|
2020-06-08 18:31:21 +00:00
|
|
|
call_frame.arguments.append(arguments.value().values());
|
2020-05-01 10:06:27 +00:00
|
|
|
call_frame.environment = function.create_environment();
|
|
|
|
|
2020-06-08 18:31:21 +00:00
|
|
|
current_environment()->set_new_target(&new_target);
|
|
|
|
|
|
|
|
Object* new_object = nullptr;
|
|
|
|
if (function.constructor_kind() == Function::ConstructorKind::Base) {
|
2020-07-22 15:50:18 +00:00
|
|
|
new_object = Object::create_empty(global_object);
|
2020-06-08 18:31:21 +00:00
|
|
|
current_environment()->bind_this_value(new_object);
|
2020-06-07 17:53:14 +00:00
|
|
|
if (exception())
|
|
|
|
return {};
|
2020-06-08 18:31:21 +00:00
|
|
|
auto prototype = new_target.get("prototype");
|
|
|
|
if (exception())
|
|
|
|
return {};
|
|
|
|
if (prototype.is_object()) {
|
|
|
|
new_object->set_prototype(&prototype.as_object());
|
|
|
|
if (exception())
|
|
|
|
return {};
|
|
|
|
}
|
2020-06-07 17:53:14 +00:00
|
|
|
}
|
2020-06-08 18:31:21 +00:00
|
|
|
|
|
|
|
// If we are a Derived constructor, |this| has not been constructed before super is called.
|
|
|
|
Value this_value = function.constructor_kind() == Function::ConstructorKind::Base ? new_object : Value {};
|
|
|
|
call_frame.this_value = this_value;
|
2020-06-25 22:30:58 +00:00
|
|
|
auto result = function.construct(*this, new_target);
|
2020-05-01 10:06:27 +00:00
|
|
|
|
2020-06-08 18:31:21 +00:00
|
|
|
this_value = current_environment()->get_this_binding();
|
2020-05-01 10:06:27 +00:00
|
|
|
pop_call_frame();
|
|
|
|
|
2020-06-08 18:31:21 +00:00
|
|
|
// If we are constructing an instance of a derived class,
|
|
|
|
// set the prototype on objects created by constructors that return an object (i.e. NativeFunction subclasses).
|
|
|
|
if (function.constructor_kind() == Function::ConstructorKind::Base && new_target.constructor_kind() == Function::ConstructorKind::Derived && result.is_object()) {
|
|
|
|
current_environment()->replace_this_binding(result);
|
|
|
|
auto prototype = new_target.get("prototype");
|
|
|
|
if (exception())
|
|
|
|
return {};
|
|
|
|
if (prototype.is_object()) {
|
|
|
|
result.as_object().set_prototype(&prototype.as_object());
|
|
|
|
if (exception())
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-05-01 10:06:27 +00:00
|
|
|
if (exception())
|
|
|
|
return {};
|
2020-06-08 18:31:21 +00:00
|
|
|
|
2020-05-01 10:06:27 +00:00
|
|
|
if (result.is_object())
|
|
|
|
return result;
|
2020-06-08 18:31:21 +00:00
|
|
|
|
|
|
|
return this_value;
|
2020-05-01 10:06:27 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 10:52:32 +00:00
|
|
|
void Interpreter::throw_exception(Exception* exception)
|
2020-03-24 13:37:39 +00:00
|
|
|
{
|
2020-07-03 01:01:09 +00:00
|
|
|
#ifdef INTERPRETER_DEBUG
|
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-05-26 11:52:20 +00:00
|
|
|
#endif
|
2020-03-24 13:37:39 +00:00
|
|
|
m_exception = exception;
|
|
|
|
unwind(ScopeType::Try);
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-05-15 11:39:24 +00:00
|
|
|
joined_arguments.append(argument(i).to_string_without_side_effects().characters());
|
2020-05-04 10:34:49 +00:00
|
|
|
if (i != argument_count() - 1)
|
|
|
|
joined_arguments.append(' ');
|
|
|
|
}
|
|
|
|
return joined_arguments.build();
|
|
|
|
}
|
|
|
|
|
2020-06-08 18:31:21 +00:00
|
|
|
Value Interpreter::resolve_this_binding() const
|
|
|
|
{
|
|
|
|
return get_this_environment()->get_this_binding();
|
|
|
|
}
|
|
|
|
|
|
|
|
const LexicalEnvironment* Interpreter::get_this_environment() const
|
|
|
|
{
|
|
|
|
// We will always return because the Global environment will always be reached, which has a |this| binding.
|
|
|
|
for (const LexicalEnvironment* environment = current_environment(); environment; environment = environment->parent()) {
|
|
|
|
if (environment->has_this_binding())
|
|
|
|
return environment;
|
|
|
|
}
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
Value Interpreter::get_new_target() const
|
|
|
|
{
|
|
|
|
return get_this_environment()->new_target();
|
|
|
|
}
|
|
|
|
|
2020-03-07 18:42:11 +00:00
|
|
|
}
|