2021-06-03 11:46:30 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2021-06-07 16:17:37 +03:00
|
|
|
#include <AK/Debug.h>
|
2021-06-03 11:46:30 +03:00
|
|
|
#include <LibJS/Bytecode/Block.h>
|
|
|
|
#include <LibJS/Bytecode/Instruction.h>
|
|
|
|
#include <LibJS/Bytecode/Interpreter.h>
|
2021-06-03 19:26:13 +03:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2021-06-03 11:46:30 +03:00
|
|
|
|
|
|
|
namespace JS::Bytecode {
|
|
|
|
|
2021-06-05 16:53:36 +03:00
|
|
|
static Interpreter* s_current;
|
|
|
|
|
|
|
|
Interpreter* Interpreter::current()
|
|
|
|
{
|
|
|
|
return s_current;
|
|
|
|
}
|
|
|
|
|
2021-06-03 11:46:30 +03:00
|
|
|
Interpreter::Interpreter(GlobalObject& global_object)
|
2021-06-03 19:26:13 +03:00
|
|
|
: m_vm(global_object.vm())
|
|
|
|
, m_global_object(global_object)
|
2021-06-03 11:46:30 +03:00
|
|
|
{
|
2021-06-05 16:53:36 +03:00
|
|
|
VERIFY(!s_current);
|
|
|
|
s_current = this;
|
2021-06-03 11:46:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Interpreter::~Interpreter()
|
|
|
|
{
|
2021-06-05 16:53:36 +03:00
|
|
|
VERIFY(s_current == this);
|
|
|
|
s_current = nullptr;
|
2021-06-03 11:46:30 +03:00
|
|
|
}
|
|
|
|
|
2021-06-05 16:53:36 +03:00
|
|
|
Value Interpreter::run(Bytecode::Block const& block)
|
2021-06-03 11:46:30 +03:00
|
|
|
{
|
2021-06-07 16:17:37 +03:00
|
|
|
dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter will run block {:p}", &block);
|
2021-06-03 11:46:30 +03:00
|
|
|
|
2021-06-07 14:54:29 +03:00
|
|
|
CallFrame global_call_frame;
|
2021-06-05 16:53:36 +03:00
|
|
|
if (vm().call_stack().is_empty()) {
|
|
|
|
global_call_frame.this_value = &global_object();
|
|
|
|
static FlyString global_execution_context_name = "(*BC* global execution context)";
|
|
|
|
global_call_frame.function_name = global_execution_context_name;
|
|
|
|
global_call_frame.scope = &global_object();
|
|
|
|
VERIFY(!vm().exception());
|
|
|
|
// FIXME: How do we know if we're in strict mode? Maybe the Bytecode::Block should know this?
|
|
|
|
// global_call_frame.is_strict_mode = ???;
|
|
|
|
vm().push_call_frame(global_call_frame, global_object());
|
|
|
|
VERIFY(!vm().exception());
|
|
|
|
}
|
2021-06-05 16:11:00 +03:00
|
|
|
|
2021-06-05 16:53:36 +03:00
|
|
|
m_register_windows.append(make<RegisterWindow>());
|
|
|
|
registers().resize(block.register_count());
|
2021-06-03 11:46:30 +03:00
|
|
|
|
2021-06-07 16:12:43 +03:00
|
|
|
Bytecode::InstructionStreamIterator pc(block.instruction_stream());
|
|
|
|
while (!pc.at_end()) {
|
|
|
|
auto& instruction = *pc;
|
2021-06-03 11:46:30 +03:00
|
|
|
instruction.execute(*this);
|
2021-06-04 13:07:38 +03:00
|
|
|
if (m_pending_jump.has_value()) {
|
2021-06-07 16:12:43 +03:00
|
|
|
pc.jump(m_pending_jump.release_value());
|
2021-06-04 13:07:38 +03:00
|
|
|
continue;
|
|
|
|
}
|
2021-06-05 16:53:36 +03:00
|
|
|
if (!m_return_value.is_empty())
|
|
|
|
break;
|
2021-06-04 13:07:38 +03:00
|
|
|
++pc;
|
|
|
|
}
|
2021-06-03 11:46:30 +03:00
|
|
|
|
2021-06-07 16:17:37 +03:00
|
|
|
dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter did run block {:p}", &block);
|
|
|
|
|
|
|
|
if constexpr (JS_BYTECODE_DEBUG) {
|
|
|
|
for (size_t i = 0; i < registers().size(); ++i) {
|
|
|
|
String value_string;
|
|
|
|
if (registers()[i].is_empty())
|
|
|
|
value_string = "(empty)";
|
|
|
|
else
|
|
|
|
value_string = registers()[i].to_string_without_side_effects();
|
|
|
|
dbgln("[{:3}] {}", i, value_string);
|
|
|
|
}
|
2021-06-03 11:46:30 +03:00
|
|
|
}
|
2021-06-05 16:53:36 +03:00
|
|
|
|
|
|
|
m_register_windows.take_last();
|
|
|
|
|
2021-06-07 14:57:02 +03:00
|
|
|
auto return_value = m_return_value.value_or(js_undefined());
|
|
|
|
m_return_value = {};
|
2021-06-05 16:53:36 +03:00
|
|
|
|
|
|
|
// NOTE: The return value from a called function is put into $0 in the caller context.
|
|
|
|
if (!m_register_windows.is_empty())
|
2021-06-07 14:57:02 +03:00
|
|
|
m_register_windows.last()[0] = return_value;
|
2021-06-05 16:53:36 +03:00
|
|
|
|
|
|
|
if (vm().call_stack().size() == 1)
|
|
|
|
vm().pop_call_frame();
|
|
|
|
|
2021-06-07 14:57:02 +03:00
|
|
|
return return_value;
|
2021-06-03 11:46:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|