mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 10:28:10 +00:00
LibJS: Implement Module linking and evaluating
This commit is contained in:
parent
be9d478d92
commit
1b8ccf9a66
9 changed files with 1569 additions and 22 deletions
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -11,6 +12,46 @@
|
|||
|
||||
namespace JS {
|
||||
|
||||
struct ResolvedBinding {
|
||||
enum Type {
|
||||
BindingName,
|
||||
Namespace,
|
||||
Ambiguous,
|
||||
Null,
|
||||
};
|
||||
|
||||
static ResolvedBinding null()
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
static ResolvedBinding ambiguous()
|
||||
{
|
||||
ResolvedBinding binding;
|
||||
binding.type = Ambiguous;
|
||||
return binding;
|
||||
}
|
||||
|
||||
Type type { Null };
|
||||
Module* module { nullptr };
|
||||
FlyString export_name;
|
||||
|
||||
bool is_valid() const
|
||||
{
|
||||
return type == BindingName || type == Namespace;
|
||||
}
|
||||
|
||||
bool is_namespace() const
|
||||
{
|
||||
return type == Namespace;
|
||||
}
|
||||
|
||||
bool is_ambiguous() const
|
||||
{
|
||||
return type == Ambiguous;
|
||||
}
|
||||
};
|
||||
|
||||
// 16.2.1.4 Abstract Module Records, https://tc39.es/ecma262/#sec-abstract-module-records
|
||||
class Module : public RefCounted<Module> {
|
||||
public:
|
||||
|
@ -22,15 +63,34 @@ public:
|
|||
StringView filename() const { return m_filename; }
|
||||
|
||||
Environment* environment() { return m_environment.cell(); }
|
||||
Object* namespace_() { return m_namespace.cell(); }
|
||||
|
||||
ThrowCompletionOr<Object*> get_module_namespace(VM& vm);
|
||||
|
||||
virtual ThrowCompletionOr<void> link(VM& vm) = 0;
|
||||
virtual ThrowCompletionOr<Promise*> evaluate(VM& vm) = 0;
|
||||
|
||||
virtual ThrowCompletionOr<Vector<FlyString>> get_exported_names(VM& vm, Vector<Module*> export_star_set = {}) = 0;
|
||||
virtual ThrowCompletionOr<ResolvedBinding> resolve_export(VM& vm, FlyString const& export_name, Vector<ResolvedBinding> resolve_set = {}) = 0;
|
||||
|
||||
virtual ThrowCompletionOr<u32> inner_module_linking(VM& vm, Vector<Module*>& stack, u32 index);
|
||||
virtual ThrowCompletionOr<u32> inner_module_evaluation(VM& vm, Vector<Module*>& stack, u32 index);
|
||||
|
||||
protected:
|
||||
explicit Module(Realm&, StringView filename);
|
||||
Module(Realm&, String filename);
|
||||
|
||||
void set_environment(Environment* environment)
|
||||
{
|
||||
m_environment = make_handle(environment);
|
||||
}
|
||||
|
||||
private:
|
||||
// Handles are not safe unless we keep the VM alive.
|
||||
NonnullRefPtr<VM> m_vm;
|
||||
Object* module_namespace_create(VM& vm, Vector<FlyString> unambiguous_names);
|
||||
|
||||
// These handles are only safe as long as the VM they live in is valid.
|
||||
// But evaluated modules SHOULD be stored in the VM so unless you intentionally
|
||||
// destroy the VM but keep the modules this should not happen. Because VM
|
||||
// stores modules with a RefPtr we cannot just store the VM as that leads to
|
||||
// cycles.
|
||||
Handle<Realm> m_realm; // [[Realm]]
|
||||
Handle<Environment> m_environment; // [[Environment]]
|
||||
Handle<Object> m_namespace; // [[Namespace]]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue