2020-03-07 19:42:11 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2022-01-16 13:16:04 +01:00
|
|
|
* Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
|
2020-03-07 19:42:11 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-07 19:42:11 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-03-24 22:03:50 +01:00
|
|
|
#include <AK/FlyString.h>
|
2020-03-09 21:13:55 +01:00
|
|
|
#include <AK/HashMap.h>
|
2020-03-12 19:53:31 +01:00
|
|
|
#include <AK/String.h>
|
2020-05-23 12:53:09 -04:00
|
|
|
#include <AK/Weakable.h>
|
2020-05-27 22:22:08 -07:00
|
|
|
#include <LibJS/AST.h>
|
2020-03-07 19:42:11 +01:00
|
|
|
#include <LibJS/Forward.h>
|
2020-09-20 19:24:44 +02:00
|
|
|
#include <LibJS/Heap/DeferGC.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Heap/Heap.h>
|
2022-02-09 10:06:40 +00:00
|
|
|
#include <LibJS/Heap/MarkedVector.h>
|
2022-01-08 21:28:27 +01:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2021-07-01 12:24:46 +02:00
|
|
|
#include <LibJS/Runtime/DeclarativeEnvironment.h>
|
2020-06-09 22:48:01 -07:00
|
|
|
#include <LibJS/Runtime/ErrorTypes.h>
|
2022-01-16 13:16:04 +01:00
|
|
|
#include <LibJS/Runtime/GlobalEnvironment.h>
|
2021-09-11 19:36:25 +01:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
|
|
|
#include <LibJS/Runtime/Realm.h>
|
2020-09-20 19:24:44 +02:00
|
|
|
#include <LibJS/Runtime/VM.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Runtime/Value.h>
|
2022-01-16 13:16:04 +01:00
|
|
|
#include <LibJS/Script.h>
|
|
|
|
#include <LibJS/SourceTextModule.h>
|
2020-03-07 19:42:11 +01:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2021-03-21 17:38:42 +01:00
|
|
|
struct ExecutingASTNodeChain {
|
|
|
|
ExecutingASTNodeChain* previous { nullptr };
|
2022-04-01 20:58:27 +03:00
|
|
|
ASTNode const& node;
|
2021-03-21 17:38:42 +01:00
|
|
|
};
|
|
|
|
|
2020-05-23 12:53:09 -04:00
|
|
|
class Interpreter : public Weakable<Interpreter> {
|
2020-03-07 19:42:11 +01:00
|
|
|
public:
|
2022-08-04 21:25:32 +02:00
|
|
|
template<typename GlobalObjectType, typename... Args>
|
|
|
|
static NonnullOwnPtr<Interpreter> create(VM& vm, Args&&... args) requires(IsBaseOf<GlobalObject, GlobalObjectType>)
|
2020-04-01 18:53:28 +02:00
|
|
|
{
|
2020-09-20 19:24:44 +02:00
|
|
|
DeferGC defer_gc(vm.heap());
|
|
|
|
auto interpreter = adopt_own(*new Interpreter(vm));
|
2020-09-21 13:36:32 +02:00
|
|
|
VM::InterpreterExecutionScope scope(*interpreter);
|
2022-01-16 13:16:04 +01:00
|
|
|
|
2022-08-22 18:56:16 +01:00
|
|
|
Realm* realm { nullptr };
|
2022-01-16 13:16:04 +01:00
|
|
|
|
2022-08-04 21:25:32 +02:00
|
|
|
interpreter->m_global_execution_context = MUST(Realm::initialize_host_defined_realm(
|
|
|
|
vm,
|
2022-08-22 18:56:16 +01:00
|
|
|
[&](Realm& realm_) -> GlobalObject* {
|
|
|
|
realm = &realm_;
|
2022-08-22 19:35:23 +01:00
|
|
|
return interpreter->heap().allocate_without_realm<GlobalObjectType>(realm_, forward<Args>(args)...);
|
2022-08-04 21:25:32 +02:00
|
|
|
},
|
2022-08-06 01:01:13 +01:00
|
|
|
nullptr));
|
2022-03-17 22:45:54 +00:00
|
|
|
|
2022-01-16 13:16:04 +01:00
|
|
|
// NOTE: These are not in the spec.
|
|
|
|
static FlyString global_execution_context_name = "(global execution context)";
|
2022-08-04 21:25:32 +02:00
|
|
|
interpreter->m_global_execution_context->function_name = global_execution_context_name;
|
2022-01-16 13:16:04 +01:00
|
|
|
|
2022-08-22 18:56:16 +01:00
|
|
|
interpreter->m_realm = make_handle(realm);
|
2022-01-16 13:16:04 +01:00
|
|
|
|
2020-04-01 21:04:51 +02:00
|
|
|
return interpreter;
|
2020-04-01 18:53:28 +02:00
|
|
|
}
|
|
|
|
|
2021-09-12 12:33:54 +01:00
|
|
|
static NonnullOwnPtr<Interpreter> create_with_existing_realm(Realm&);
|
2020-09-27 17:24:14 +02:00
|
|
|
|
2022-03-14 10:25:06 -06:00
|
|
|
~Interpreter() = default;
|
2020-04-01 21:04:51 +02:00
|
|
|
|
2022-01-16 13:16:04 +01:00
|
|
|
ThrowCompletionOr<Value> run(Script&);
|
|
|
|
ThrowCompletionOr<Value> run(SourceTextModule&);
|
2020-09-11 22:47:43 +01:00
|
|
|
|
2021-09-11 19:36:25 +01:00
|
|
|
Realm& realm();
|
|
|
|
Realm const& realm() const;
|
|
|
|
|
2021-03-21 17:39:43 +01:00
|
|
|
ALWAYS_INLINE VM& vm() { return *m_vm; }
|
|
|
|
ALWAYS_INLINE const VM& vm() const { return *m_vm; }
|
|
|
|
ALWAYS_INLINE Heap& heap() { return vm().heap(); }
|
2020-03-08 19:23:58 +01:00
|
|
|
|
2021-07-01 12:24:46 +02:00
|
|
|
Environment* lexical_environment() { return vm().lexical_environment(); }
|
2021-06-21 23:47:44 +02:00
|
|
|
|
2021-03-21 17:38:42 +01:00
|
|
|
void push_ast_node(ExecutingASTNodeChain& chain_node)
|
|
|
|
{
|
|
|
|
chain_node.previous = m_ast_node_chain;
|
|
|
|
m_ast_node_chain = &chain_node;
|
|
|
|
}
|
2020-12-28 20:45:22 +03:30
|
|
|
|
2021-03-21 17:38:42 +01:00
|
|
|
void pop_ast_node()
|
|
|
|
{
|
|
|
|
VERIFY(m_ast_node_chain);
|
|
|
|
m_ast_node_chain = m_ast_node_chain->previous;
|
|
|
|
}
|
2021-03-21 12:18:56 +01:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ASTNode const* current_node() const { return m_ast_node_chain ? &m_ast_node_chain->node : nullptr; }
|
2020-09-27 16:56:58 +02:00
|
|
|
|
2020-03-14 13:56:49 +02:00
|
|
|
private:
|
2020-09-20 19:24:44 +02:00
|
|
|
explicit Interpreter(VM&);
|
2020-04-01 21:04:51 +02:00
|
|
|
|
2021-03-21 17:38:42 +01:00
|
|
|
ExecutingASTNodeChain* m_ast_node_chain { nullptr };
|
2020-10-03 17:02:43 -07:00
|
|
|
|
2020-09-20 19:24:44 +02:00
|
|
|
NonnullRefPtr<VM> m_vm;
|
2021-09-11 19:36:25 +01:00
|
|
|
Handle<Realm> m_realm;
|
2022-01-16 13:16:04 +01:00
|
|
|
|
|
|
|
// This is here to keep the global execution context alive for the entire lifespan of the Interpreter.
|
2022-08-04 21:25:32 +02:00
|
|
|
OwnPtr<ExecutionContext> m_global_execution_context;
|
2020-03-07 19:42:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|