1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 19:57:44 +00:00

LibJS: Rip out the AST interpreter :^)

This has been superseded by the bytecode VM, which is both faster
and more capable.
This commit is contained in:
Andreas Kling 2023-08-07 19:59:00 +02:00
parent fcc72a787b
commit 2eaa528a0e
41 changed files with 147 additions and 3734 deletions

View file

@ -1,6 +1,6 @@
/*
* Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021-2023, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -11,7 +11,6 @@
#include <AK/Optional.h>
#include <AK/Utf16View.h>
#include <LibJS/Bytecode/Interpreter.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Parser.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Accessor.h>
@ -24,6 +23,7 @@
#include <LibJS/Runtime/ErrorTypes.h>
#include <LibJS/Runtime/FunctionEnvironment.h>
#include <LibJS/Runtime/FunctionObject.h>
#include <LibJS/Runtime/GlobalEnvironment.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/ObjectEnvironment.h>
@ -700,26 +700,21 @@ ThrowCompletionOr<Value> perform_eval(VM& vm, Value x, CallerMode strict_caller,
// 29. If result.[[Type]] is normal, then
// a. Set result to the result of evaluating body.
if (auto* bytecode_interpreter = vm.bytecode_interpreter_if_exists()) {
auto executable_result = Bytecode::Generator::generate(program);
if (executable_result.is_error())
return vm.throw_completion<InternalError>(ErrorType::NotImplemented, TRY_OR_THROW_OOM(vm, executable_result.error().to_string()));
auto executable_result = Bytecode::Generator::generate(program);
if (executable_result.is_error())
return vm.throw_completion<InternalError>(ErrorType::NotImplemented, TRY_OR_THROW_OOM(vm, executable_result.error().to_string()));
auto executable = executable_result.release_value();
executable->name = "eval"sv;
if (Bytecode::g_dump_bytecode)
executable->dump();
auto result_or_error = bytecode_interpreter->run_and_return_frame(eval_realm, *executable, nullptr);
if (result_or_error.value.is_error())
return result_or_error.value.release_error();
auto executable = executable_result.release_value();
executable->name = "eval"sv;
if (Bytecode::g_dump_bytecode)
executable->dump();
auto result_or_error = vm.bytecode_interpreter().run_and_return_frame(eval_realm, *executable, nullptr);
if (result_or_error.value.is_error())
return result_or_error.value.release_error();
auto& result = result_or_error.frame->registers[0];
if (!result.is_empty())
eval_result = result;
} else {
auto& ast_interpreter = vm.interpreter();
eval_result = TRY(program->execute(ast_interpreter));
}
auto& result = result_or_error.frame->registers[0];
if (!result.is_empty())
eval_result = result;
// 30. If result.[[Type]] is normal and result.[[Value]] is empty, then
// a. Set result to NormalCompletion(undefined).

View file

@ -4,7 +4,6 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/DeclarativeEnvironment.h>
#include <LibJS/Runtime/Error.h>

View file

@ -13,7 +13,6 @@
#include <LibJS/Bytecode/BasicBlock.h>
#include <LibJS/Bytecode/Generator.h>
#include <LibJS/Bytecode/Interpreter.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/AsyncFunctionDriverWrapper.h>
@ -23,6 +22,7 @@
#include <LibJS/Runtime/ExecutionContext.h>
#include <LibJS/Runtime/FunctionEnvironment.h>
#include <LibJS/Runtime/GeneratorObject.h>
#include <LibJS/Runtime/GlobalEnvironment.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/PromiseCapability.h>
@ -152,8 +152,6 @@ ThrowCompletionOr<Value> ECMAScriptFunctionObject::internal_call(Value this_argu
// Non-standard
callee_context.arguments.extend(move(arguments_list));
if (auto* interpreter = vm.interpreter_if_exists(); interpreter && interpreter->current_node())
callee_context.source_range = interpreter->current_node()->unrealized_source_range();
// 2. Let calleeContext be PrepareForOrdinaryCall(F, undefined).
// NOTE: We throw if the end of the native stack is reached, so unlike in the spec this _does_ need an exception check.
@ -223,8 +221,6 @@ ThrowCompletionOr<NonnullGCPtr<Object>> ECMAScriptFunctionObject::internal_const
// Non-standard
callee_context.arguments.extend(move(arguments_list));
if (auto* interpreter = vm.interpreter_if_exists(); interpreter && interpreter->current_node())
callee_context.source_range = interpreter->current_node()->unrealized_source_range();
// 4. Let calleeContext be PrepareForOrdinaryCall(F, newTarget).
// NOTE: We throw if the end of the native stack is reached, so unlike in the spec this _does_ need an exception check.
@ -329,7 +325,7 @@ void ECMAScriptFunctionObject::make_method(Object& home_object)
}
// 10.2.11 FunctionDeclarationInstantiation ( func, argumentsList ), https://tc39.es/ecma262/#sec-functiondeclarationinstantiation
ThrowCompletionOr<void> ECMAScriptFunctionObject::function_declaration_instantiation(Interpreter* interpreter)
ThrowCompletionOr<void> ECMAScriptFunctionObject::function_declaration_instantiation()
{
auto& vm = this->vm();
auto& realm = *vm.current_realm();
@ -560,18 +556,11 @@ ThrowCompletionOr<void> ECMAScriptFunctionObject::function_declaration_instantia
} else if (i < execution_context_arguments.size() && !execution_context_arguments[i].is_undefined()) {
argument_value = execution_context_arguments[i];
} else if (parameter.default_value) {
auto* bytecode_interpreter = vm.bytecode_interpreter_if_exists();
if (static_cast<FunctionKind>(m_kind) == FunctionKind::Generator || static_cast<FunctionKind>(m_kind) == FunctionKind::AsyncGenerator)
bytecode_interpreter = &vm.bytecode_interpreter();
if (bytecode_interpreter) {
auto value_and_frame = bytecode_interpreter->run_and_return_frame(realm, *m_default_parameter_bytecode_executables[default_parameter_index - 1], nullptr);
if (value_and_frame.value.is_error())
return value_and_frame.value.release_error();
// Resulting value is in the accumulator.
argument_value = value_and_frame.frame->registers.at(0);
} else if (interpreter) {
argument_value = TRY(parameter.default_value->execute(*interpreter)).release_value();
}
auto value_and_frame = vm.bytecode_interpreter().run_and_return_frame(realm, *m_default_parameter_bytecode_executables[default_parameter_index - 1], nullptr);
if (value_and_frame.value.is_error())
return value_and_frame.value.release_error();
// Resulting value is in the accumulator.
argument_value = value_and_frame.frame->registers.at(0);
} else {
argument_value = js_undefined();
}
@ -579,18 +568,15 @@ ThrowCompletionOr<void> ECMAScriptFunctionObject::function_declaration_instantia
Environment* used_environment = has_duplicates ? nullptr : environment;
if constexpr (IsSame<NonnullRefPtr<Identifier const> const&, decltype(param)>) {
if ((vm.bytecode_interpreter_if_exists() || kind() == FunctionKind::Generator || kind() == FunctionKind::AsyncGenerator) && param->is_local()) {
// NOTE: Local variables are supported only in bytecode interpreter
if (param->is_local()) {
callee_context.local_variables[param->local_variable_index()] = argument_value;
return {};
} else {
Reference reference = TRY(vm.resolve_binding(param->string(), used_environment));
// Here the difference from hasDuplicates is important
if (has_duplicates)
return reference.put_value(vm, argument_value);
else
return reference.initialize_referenced_binding(vm, argument_value);
}
Reference reference = TRY(vm.resolve_binding(param->string(), used_environment));
// Here the difference from hasDuplicates is important
if (has_duplicates)
return reference.put_value(vm, argument_value);
return reference.initialize_referenced_binding(vm, argument_value);
}
if constexpr (IsSame<NonnullRefPtr<BindingPattern const> const&, decltype(param)>) {
// Here the difference from hasDuplicates is important
@ -957,16 +943,12 @@ void async_block_start(VM& vm, T const& async_body, PromiseCapability const& pro
// a. If asyncBody is a Parse Node, then
if constexpr (!IsCallableWithArguments<T, Completion>) {
// a. Let result be the result of evaluating asyncBody.
if (auto* bytecode_interpreter = vm.bytecode_interpreter_if_exists()) {
// FIXME: Cache this executable somewhere.
auto maybe_executable = Bytecode::compile(vm, async_body, FunctionKind::Async, "AsyncBlockStart"sv);
if (maybe_executable.is_error())
result = maybe_executable.release_error();
else
result = bytecode_interpreter->run_and_return_frame(realm, *maybe_executable.value(), nullptr).value;
} else {
result = async_body->execute(vm.interpreter());
}
// FIXME: Cache this executable somewhere.
auto maybe_executable = Bytecode::compile(vm, async_body, FunctionKind::Async, "AsyncBlockStart"sv);
if (maybe_executable.is_error())
result = maybe_executable.release_error();
else
result = vm.bytecode_interpreter().run_and_return_frame(realm, *maybe_executable.value(), nullptr).value;
}
// b. Else,
else {
@ -1035,8 +1017,8 @@ void async_block_start(VM& vm, T const& async_body, PromiseCapability const& pro
// 8. Return unused.
}
template void async_block_start(VM&, NonnullGCPtr<Statement const> const& async_body, PromiseCapability const&, ExecutionContext&);
template void async_function_start(VM&, PromiseCapability const&, NonnullGCPtr<Statement const> const& async_function_body);
template void async_block_start(VM&, NonnullRefPtr<Statement const> const& async_body, PromiseCapability const&, ExecutionContext&);
template void async_function_start(VM&, PromiseCapability const&, NonnullRefPtr<Statement const> const& async_function_body);
template void async_block_start(VM&, SafeFunction<Completion()> const& async_body, PromiseCapability const&, ExecutionContext&);
template void async_function_start(VM&, PromiseCapability const&, SafeFunction<Completion()> const& async_function_body);
@ -1048,136 +1030,68 @@ Completion ECMAScriptFunctionObject::ordinary_call_evaluate_body()
auto& vm = this->vm();
auto& realm = *vm.current_realm();
auto* bytecode_interpreter = vm.bytecode_interpreter_if_exists();
// NOTE: There's a subtle ordering issue here:
// - We have to compile the default parameter values before instantiating the function.
// - We have to instantiate the function before compiling the function body.
// This is why FunctionDeclarationInstantiation is invoked in the middle.
// The issue is that FunctionDeclarationInstantiation may mark certain functions as hoisted
// per Annex B. This affects code generation for FunctionDeclaration nodes.
// The bytecode interpreter can execute generator functions while the AST interpreter cannot.
// This simply makes it create a new bytecode interpreter when one doesn't exist when executing a generator function.
// Doing so makes it automatically switch to the bytecode interpreter to execute any future code until it exits the generator. See below.
// This allows us to keep all of the existing functionality that works in AST while adding generator support on top of it.
// However, this does cause an awkward situation with features not supported in bytecode, where features that work outside of generators with AST
// suddenly stop working inside of generators.
// This is a stop gap until bytecode mode becomes the default.
if ((m_kind == FunctionKind::Generator || m_kind == FunctionKind::AsyncGenerator) && !bytecode_interpreter) {
bytecode_interpreter = &vm.bytecode_interpreter();
if (!m_bytecode_executable) {
size_t default_parameter_index = 0;
for (auto& parameter : m_formal_parameters) {
if (!parameter.default_value)
continue;
auto executable = TRY(Bytecode::compile(vm, *parameter.default_value, FunctionKind::Normal, DeprecatedString::formatted("default parameter #{} for {}", default_parameter_index, m_name)));
m_default_parameter_bytecode_executables.append(move(executable));
}
}
if (bytecode_interpreter) {
// NOTE: There's a subtle ordering issue here:
// - We have to compile the default parameter values before instantiating the function.
// - We have to instantiate the function before compiling the function body.
// This is why FunctionDeclarationInstantiation is invoked in the middle.
// The issue is that FunctionDeclarationInstantiation may mark certain functions as hoisted
// per Annex B. This affects code generation for FunctionDeclaration nodes.
auto declaration_result = function_declaration_instantiation();
if (!m_bytecode_executable) {
size_t default_parameter_index = 0;
for (auto& parameter : m_formal_parameters) {
if (!parameter.default_value)
continue;
auto executable = TRY(Bytecode::compile(vm, *parameter.default_value, FunctionKind::Normal, DeprecatedString::formatted("default parameter #{} for {}", default_parameter_index, m_name)));
m_default_parameter_bytecode_executables.append(move(executable));
}
}
if (m_kind == FunctionKind::Normal || m_kind == FunctionKind::Generator || m_kind == FunctionKind::AsyncGenerator) {
if (declaration_result.is_error())
return declaration_result.release_error();
}
auto declaration_result = function_declaration_instantiation(nullptr);
if (!m_bytecode_executable)
m_bytecode_executable = TRY(Bytecode::compile(vm, *m_ecmascript_code, m_kind, m_name));
if (m_kind == FunctionKind::Normal || m_kind == FunctionKind::Generator || m_kind == FunctionKind::AsyncGenerator) {
if (declaration_result.is_error())
return declaration_result.release_error();
}
if (!m_bytecode_executable)
m_bytecode_executable = TRY(Bytecode::compile(vm, *m_ecmascript_code, m_kind, m_name));
if (m_kind == FunctionKind::Async) {
if (declaration_result.is_throw_completion()) {
auto promise_capability = MUST(new_promise_capability(vm, realm.intrinsics().promise_constructor()));
MUST(call(vm, *promise_capability->reject(), js_undefined(), *declaration_result.throw_completion().value()));
return Completion { Completion::Type::Return, promise_capability->promise(), {} };
}
}
auto result_and_frame = bytecode_interpreter->run_and_return_frame(realm, *m_bytecode_executable, nullptr);
VERIFY(result_and_frame.frame != nullptr);
if (result_and_frame.value.is_error())
return result_and_frame.value.release_error();
auto result = result_and_frame.value.release_value();
// NOTE: Running the bytecode should eventually return a completion.
// Until it does, we assume "return" and include the undefined fallback from the call site.
if (m_kind == FunctionKind::Normal)
return { Completion::Type::Return, result.value_or(js_undefined()), {} };
if (m_kind == FunctionKind::AsyncGenerator) {
auto async_generator_object = TRY(AsyncGenerator::create(realm, result, this, vm.running_execution_context().copy(), move(*result_and_frame.frame)));
return { Completion::Type::Return, async_generator_object, {} };
}
auto generator_object = TRY(GeneratorObject::create(realm, result, this, vm.running_execution_context().copy(), move(*result_and_frame.frame)));
// NOTE: Async functions are entirely transformed to generator functions, and wrapped in a custom driver that returns a promise
// See AwaitExpression::generate_bytecode() for the transformation.
if (m_kind == FunctionKind::Async)
return { Completion::Type::Return, TRY(AsyncFunctionDriverWrapper::create(realm, generator_object)), {} };
VERIFY(m_kind == FunctionKind::Generator);
return { Completion::Type::Return, generator_object, {} };
} else {
if (m_kind == FunctionKind::Generator)
return vm.throw_completion<InternalError>(ErrorType::NotImplemented, "Generator function execution in AST interpreter");
if (m_kind == FunctionKind::AsyncGenerator)
return vm.throw_completion<InternalError>(ErrorType::NotImplemented, "Async generator function execution in AST interpreter");
OwnPtr<Interpreter> local_interpreter;
Interpreter* ast_interpreter = vm.interpreter_if_exists();
if (!ast_interpreter) {
local_interpreter = Interpreter::create_with_existing_realm(realm);
ast_interpreter = local_interpreter.ptr();
}
VM::InterpreterExecutionScope scope(*ast_interpreter);
// FunctionBody : FunctionStatementList
if (m_kind == FunctionKind::Normal) {
// 1. Perform ? FunctionDeclarationInstantiation(functionObject, argumentsList).
TRY(function_declaration_instantiation(ast_interpreter));
// 2. Let result be result of evaluating FunctionStatementList.
auto result = m_ecmascript_code->execute(*ast_interpreter);
// 3. Let env be the running execution context's LexicalEnvironment.
auto env = vm.running_execution_context().lexical_environment;
VERIFY(is<DeclarativeEnvironment>(*env));
// 4. Return ? DisposeResources(env, result).
return dispose_resources(vm, static_cast<DeclarativeEnvironment*>(env.ptr()), result);
}
// AsyncFunctionBody : FunctionBody
else if (m_kind == FunctionKind::Async) {
// 1. Let promiseCapability be ! NewPromiseCapability(%Promise%).
if (m_kind == FunctionKind::Async) {
if (declaration_result.is_throw_completion()) {
auto promise_capability = MUST(new_promise_capability(vm, realm.intrinsics().promise_constructor()));
// 2. Let declResult be Completion(FunctionDeclarationInstantiation(functionObject, argumentsList)).
auto declaration_result = function_declaration_instantiation(ast_interpreter);
// 3. If declResult is an abrupt completion, then
if (declaration_result.is_throw_completion()) {
// a. Perform ! Call(promiseCapability.[[Reject]], undefined, « declResult.[[Value]] »).
MUST(call(vm, *promise_capability->reject(), js_undefined(), *declaration_result.throw_completion().value()));
}
// 4. Else,
else {
// a. Perform AsyncFunctionStart(promiseCapability, FunctionBody).
async_function_start(vm, promise_capability, m_ecmascript_code);
}
// 5. Return Completion Record { [[Type]]: return, [[Value]]: promiseCapability.[[Promise]], [[Target]]: empty }.
MUST(call(vm, *promise_capability->reject(), js_undefined(), *declaration_result.throw_completion().value()));
return Completion { Completion::Type::Return, promise_capability->promise(), {} };
}
}
VERIFY_NOT_REACHED();
auto result_and_frame = vm.bytecode_interpreter().run_and_return_frame(realm, *m_bytecode_executable, nullptr);
VERIFY(result_and_frame.frame != nullptr);
if (result_and_frame.value.is_error())
return result_and_frame.value.release_error();
auto result = result_and_frame.value.release_value();
// NOTE: Running the bytecode should eventually return a completion.
// Until it does, we assume "return" and include the undefined fallback from the call site.
if (m_kind == FunctionKind::Normal)
return { Completion::Type::Return, result.value_or(js_undefined()), {} };
if (m_kind == FunctionKind::AsyncGenerator) {
auto async_generator_object = TRY(AsyncGenerator::create(realm, result, this, vm.running_execution_context().copy(), move(*result_and_frame.frame)));
return { Completion::Type::Return, async_generator_object, {} };
}
auto generator_object = TRY(GeneratorObject::create(realm, result, this, vm.running_execution_context().copy(), move(*result_and_frame.frame)));
// NOTE: Async functions are entirely transformed to generator functions, and wrapped in a custom driver that returns a promise
// See AwaitExpression::generate_bytecode() for the transformation.
if (m_kind == FunctionKind::Async)
return { Completion::Type::Return, TRY(AsyncFunctionDriverWrapper::create(realm, generator_object)), {} };
VERIFY(m_kind == FunctionKind::Generator);
return { Completion::Type::Return, generator_object, {} };
}
void ECMAScriptFunctionObject::set_name(DeprecatedFlyString const& name)
@ -1187,5 +1101,4 @@ void ECMAScriptFunctionObject::set_name(DeprecatedFlyString const& name)
m_name = name;
MUST(define_property_or_throw(vm.names.name, { .value = PrimitiveString::create(vm, m_name), .writable = false, .enumerable = false, .configurable = true }));
}
}

View file

@ -108,7 +108,7 @@ private:
ThrowCompletionOr<void> prepare_for_ordinary_call(ExecutionContext& callee_context, Object* new_target);
void ordinary_call_bind_this(ExecutionContext&, Value this_argument);
ThrowCompletionOr<void> function_declaration_instantiation(Interpreter*);
ThrowCompletionOr<void> function_declaration_instantiation();
DeprecatedFlyString m_name;
OwnPtr<Bytecode::Executable> m_bytecode_executable;

View file

@ -4,7 +4,6 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/ECMAScriptFunctionObject.h>
#include <LibJS/Runtime/FunctionEnvironment.h>

View file

@ -6,10 +6,10 @@
*/
#include <AK/TypeCasts.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/FunctionObject.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/VM.h>
namespace JS {

View file

@ -8,7 +8,6 @@
#include <AK/StringBuilder.h>
#include <AK/TypeCasts.h>
#include <LibJS/Heap/MarkedVector.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/BoundFunction.h>
#include <LibJS/Runtime/ECMAScriptFunctionObject.h>

View file

@ -13,7 +13,6 @@
#include <AK/Utf16View.h>
#include <AK/Utf8View.h>
#include <LibJS/Heap/DeferGC.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/AggregateErrorConstructor.h>
#include <LibJS/Runtime/ArrayBufferConstructor.h>

View file

@ -6,7 +6,6 @@
*/
#include <LibJS/AST.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/FunctionEnvironment.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/NativeFunction.h>
@ -142,9 +141,6 @@ ThrowCompletionOr<Value> NativeFunction::internal_call(Value this_argument, Mark
// NOTE: This is a LibJS specific hack for NativeFunction to inherit the strictness of its caller.
callee_context.is_strict_mode = vm.in_strict_mode();
if (auto* interpreter = vm.interpreter_if_exists(); interpreter && interpreter->current_node())
callee_context.source_range = interpreter->current_node()->unrealized_source_range();
// </8.> --------------------------------------------------------------------------
// 9. Push calleeContext onto the execution context stack; calleeContext is now the running execution context.
@ -205,9 +201,6 @@ ThrowCompletionOr<NonnullGCPtr<Object>> NativeFunction::internal_construct(Marke
// NOTE: This is a LibJS specific hack for NativeFunction to inherit the strictness of its caller.
callee_context.is_strict_mode = vm.in_strict_mode();
if (auto* interpreter = vm.interpreter_if_exists(); interpreter && interpreter->current_node())
callee_context.source_range = interpreter->current_node()->unrealized_source_range();
// </8.> --------------------------------------------------------------------------
// 9. Push calleeContext onto the execution context stack; calleeContext is now the running execution context.

View file

@ -7,7 +7,6 @@
#include <AK/DeprecatedString.h>
#include <AK/TypeCasts.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Accessor.h>
#include <LibJS/Runtime/Array.h>

View file

@ -5,7 +5,6 @@
*/
#include <AK/Function.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/AggregateError.h>
#include <LibJS/Runtime/Array.h>

View file

@ -5,7 +5,6 @@
*/
#include <AK/Function.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h>

View file

@ -6,7 +6,6 @@
#include <LibJS/Bytecode/Executable.h>
#include <LibJS/Bytecode/Interpreter.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Lexer.h>
#include <LibJS/Parser.h>
#include <LibJS/Runtime/AbstractOperations.h>
@ -173,26 +172,19 @@ ThrowCompletionOr<Value> perform_shadow_realm_eval(VM& vm, StringView source_tex
// 17. If result.[[Type]] is normal, then
if (!eval_result.is_throw_completion()) {
// a. Set result to the result of evaluating body.
if (auto* bytecode_interpreter = vm.bytecode_interpreter_if_exists()) {
auto maybe_executable = Bytecode::compile(vm, program, FunctionKind::Normal, "ShadowRealmEval"sv);
if (maybe_executable.is_error())
result = maybe_executable.release_error();
else {
auto executable = maybe_executable.release_value();
auto maybe_executable = Bytecode::compile(vm, program, FunctionKind::Normal, "ShadowRealmEval"sv);
if (maybe_executable.is_error())
result = maybe_executable.release_error();
else {
auto executable = maybe_executable.release_value();
auto value_and_frame = bytecode_interpreter->run_and_return_frame(eval_realm, *executable, nullptr);
if (value_and_frame.value.is_error()) {
result = value_and_frame.value.release_error();
} else {
// Resulting value is in the accumulator.
result = value_and_frame.frame->registers.at(0).value_or(js_undefined());
}
auto value_and_frame = vm.bytecode_interpreter().run_and_return_frame(eval_realm, *executable, nullptr);
if (value_and_frame.value.is_error()) {
result = value_and_frame.value.release_error();
} else {
// Resulting value is in the accumulator.
result = value_and_frame.frame->registers.at(0).value_or(js_undefined());
}
} else {
// FIXME: Remove once everything uses the VM's current realm.
auto eval_realm_interpreter = Interpreter::create_with_existing_realm(eval_realm);
result = program->execute(*eval_realm_interpreter);
}
}

View file

@ -15,7 +15,6 @@
#include <LibFileSystem/FileSystem.h>
#include <LibJS/AST.h>
#include <LibJS/Bytecode/Interpreter.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/BoundFunction.h>
@ -188,19 +187,6 @@ void VM::enable_default_host_import_module_dynamically_hook()
};
}
Interpreter& VM::interpreter()
{
VERIFY(!m_interpreters.is_empty());
return *m_interpreters.last();
}
Interpreter* VM::interpreter_if_exists()
{
if (m_interpreters.is_empty())
return nullptr;
return m_interpreters.last();
}
Bytecode::Interpreter& VM::bytecode_interpreter()
{
return *m_bytecode_interpreter;
@ -211,29 +197,6 @@ Bytecode::Interpreter* VM::bytecode_interpreter_if_exists()
return m_bytecode_interpreter;
}
void VM::push_interpreter(Interpreter& interpreter)
{
m_interpreters.append(&interpreter);
}
void VM::pop_interpreter(Interpreter& interpreter)
{
VERIFY(!m_interpreters.is_empty());
auto* popped_interpreter = m_interpreters.take_last();
VERIFY(popped_interpreter == &interpreter);
}
VM::InterpreterExecutionScope::InterpreterExecutionScope(Interpreter& interpreter)
: m_interpreter(interpreter)
{
m_interpreter.vm().push_interpreter(m_interpreter);
}
VM::InterpreterExecutionScope::~InterpreterExecutionScope()
{
m_interpreter.vm().pop_interpreter(m_interpreter);
}
void VM::gather_roots(HashTable<Cell*>& roots)
{
roots.set(m_empty_string);
@ -356,15 +319,11 @@ ThrowCompletionOr<void> VM::binding_initialization(NonnullRefPtr<BindingPattern
ThrowCompletionOr<Value> VM::execute_ast_node(ASTNode const& node)
{
if (auto* bytecode_interpreter = bytecode_interpreter_if_exists()) {
auto executable = TRY(Bytecode::compile(*this, node, FunctionKind::Normal, ""sv));
auto result_or_error = bytecode_interpreter->run_and_return_frame(*current_realm(), *executable, nullptr);
if (result_or_error.value.is_error())
return result_or_error.value.release_error();
return result_or_error.frame->registers[0];
}
return TRY(node.execute(interpreter())).value();
auto executable = TRY(Bytecode::compile(*this, node, FunctionKind::Normal, ""sv));
auto result_or_error = bytecode_interpreter().run_and_return_frame(*current_realm(), *executable, nullptr);
if (result_or_error.value.is_error())
return result_or_error.value.release_error();
return result_or_error.frame->registers[0];
}
// 13.15.5.3 Runtime Semantics: PropertyDestructuringAssignmentEvaluation, https://tc39.es/ecma262/#sec-runtime-semantics-propertydestructuringassignmentevaluation
@ -385,8 +344,6 @@ ThrowCompletionOr<void> VM::property_binding_initialization(BindingPattern const
Reference assignment_target;
if (auto identifier_ptr = property.name.get_pointer<NonnullRefPtr<Identifier const>>()) {
assignment_target = TRY(resolve_binding((*identifier_ptr)->string(), environment));
} else if (auto member_ptr = property.alias.get_pointer<NonnullRefPtr<MemberExpression const>>()) {
assignment_target = TRY((*member_ptr)->to_reference(interpreter()));
} else {
VERIFY_NOT_REACHED();
}
@ -436,8 +393,8 @@ ThrowCompletionOr<void> VM::property_binding_initialization(BindingPattern const
return TRY(resolve_binding(identifier->string(), environment));
},
[&](NonnullRefPtr<BindingPattern const> const&) -> ThrowCompletionOr<Optional<Reference>> { return Optional<Reference> {}; },
[&](NonnullRefPtr<MemberExpression const> const& member_expression) -> ThrowCompletionOr<Optional<Reference>> {
return TRY(member_expression->to_reference(interpreter()));
[&](NonnullRefPtr<MemberExpression const> const&) -> ThrowCompletionOr<Optional<Reference>> {
VERIFY_NOT_REACHED();
}));
auto value_to_assign = TRY(object->get(name));
@ -480,8 +437,8 @@ ThrowCompletionOr<void> VM::iterator_binding_initialization(BindingPattern const
return TRY(resolve_binding(identifier->string(), environment));
},
[&](NonnullRefPtr<BindingPattern const> const&) -> ThrowCompletionOr<Optional<Reference>> { return Optional<Reference> {}; },
[&](NonnullRefPtr<MemberExpression const> const& member_expression) -> ThrowCompletionOr<Optional<Reference>> {
return TRY(member_expression->to_reference(interpreter()));
[&](NonnullRefPtr<MemberExpression const> const&) -> ThrowCompletionOr<Optional<Reference>> {
VERIFY_NOT_REACHED();
}));
// BindingRestElement : ... BindingIdentifier
@ -861,11 +818,6 @@ VM::StoredModule* VM::get_stored_module(ScriptOrModule const&, DeprecatedString
return &(*end_or_module);
}
ThrowCompletionOr<void> VM::link_and_eval_module(Badge<Interpreter>, SourceTextModule& module)
{
return link_and_eval_module(module);
}
ThrowCompletionOr<void> VM::link_and_eval_module(Badge<Bytecode::Interpreter>, SourceTextModule& module)
{
return link_and_eval_module(module);

View file

@ -46,25 +46,8 @@ public:
Bytecode::Interpreter& bytecode_interpreter();
Bytecode::Interpreter* bytecode_interpreter_if_exists();
Interpreter& interpreter();
Interpreter* interpreter_if_exists();
void push_interpreter(Interpreter&);
void pop_interpreter(Interpreter&);
void dump_backtrace() const;
class InterpreterExecutionScope {
public:
InterpreterExecutionScope(Interpreter&);
~InterpreterExecutionScope();
Interpreter& interpreter() { return m_interpreter; }
private:
Interpreter& m_interpreter;
};
void gather_roots(HashTable<Cell*>&);
#define __JS_ENUMERATE(SymbolName, snake_name) \
@ -249,7 +232,6 @@ public:
void restore_execution_context_stack();
// Do not call this method unless you are sure this is the only and first module to be loaded in this vm.
ThrowCompletionOr<void> link_and_eval_module(Badge<Interpreter>, SourceTextModule& module);
ThrowCompletionOr<void> link_and_eval_module(Badge<Bytecode::Interpreter>, SourceTextModule& module);
ScriptOrModule get_active_script_or_module() const;
@ -304,7 +286,6 @@ private:
HashMap<DeprecatedString, GCPtr<PrimitiveString>> m_deprecated_string_cache;
Heap m_heap;
Vector<Interpreter*> m_interpreters;
Vector<ExecutionContext*> m_execution_context_stack;