2020-11-28 16:02:27 +01:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2020-2022, Andreas Kling <andreas@ladybird.org>
|
2020-11-28 16:02:27 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-11-28 16:02:27 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-03-16 18:26:49 -06:00
|
|
|
#include <AK/StringView.h>
|
2021-10-09 16:52:34 +01:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2020-11-28 16:02:27 +01:00
|
|
|
#include <LibJS/Runtime/Object.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
struct Variable {
|
|
|
|
Value value;
|
|
|
|
DeclarationKind declaration_kind;
|
|
|
|
};
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
#define JS_ENVIRONMENT(class_, base_class) GC_CELL(class_, base_class)
|
2020-11-28 16:02:27 +01:00
|
|
|
|
2025-06-28 21:39:13 -07:00
|
|
|
class JS_API Environment : public Cell {
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_CELL(Environment, Cell);
|
2022-08-28 22:11:20 +02:00
|
|
|
|
2020-11-28 16:02:27 +01:00
|
|
|
public:
|
2022-12-14 13:26:10 +01:00
|
|
|
enum class InitializeBindingHint {
|
|
|
|
Normal,
|
|
|
|
SyncDispose,
|
2025-01-16 16:51:26 -05:00
|
|
|
AsyncDispose,
|
2022-12-14 13:26:10 +01:00
|
|
|
};
|
|
|
|
|
2021-06-22 13:30:48 +02:00
|
|
|
virtual bool has_this_binding() const { return false; }
|
2022-08-21 15:12:43 +01:00
|
|
|
virtual ThrowCompletionOr<Value> get_this_binding(VM&) const { return Value {}; }
|
2020-11-28 16:02:27 +01:00
|
|
|
|
2021-06-24 13:24:44 +02:00
|
|
|
virtual Object* with_base_object() const { return nullptr; }
|
|
|
|
|
2025-04-04 18:48:07 +02:00
|
|
|
virtual ThrowCompletionOr<bool> has_binding([[maybe_unused]] FlyString const& name, [[maybe_unused]] Optional<size_t>* out_index = nullptr) const = 0;
|
|
|
|
virtual ThrowCompletionOr<void> create_mutable_binding(VM&, [[maybe_unused]] FlyString const& name, [[maybe_unused]] bool can_be_deleted) = 0;
|
|
|
|
virtual ThrowCompletionOr<void> create_immutable_binding(VM&, [[maybe_unused]] FlyString const& name, [[maybe_unused]] bool strict) = 0;
|
|
|
|
virtual ThrowCompletionOr<void> initialize_binding(VM&, [[maybe_unused]] FlyString const& name, Value, InitializeBindingHint) = 0;
|
|
|
|
virtual ThrowCompletionOr<void> set_mutable_binding(VM&, [[maybe_unused]] FlyString const& name, Value, [[maybe_unused]] bool strict) = 0;
|
|
|
|
virtual ThrowCompletionOr<Value> get_binding_value(VM&, [[maybe_unused]] FlyString const& name, [[maybe_unused]] bool strict) = 0;
|
|
|
|
virtual ThrowCompletionOr<bool> delete_binding(VM&, [[maybe_unused]] FlyString const& name) = 0;
|
2021-06-23 12:26:37 +02:00
|
|
|
|
2021-06-21 23:35:30 +02:00
|
|
|
// [[OuterEnv]]
|
2021-07-01 12:24:46 +02:00
|
|
|
Environment* outer_environment() { return m_outer_environment; }
|
|
|
|
Environment const* outer_environment() const { return m_outer_environment; }
|
2020-11-28 16:02:27 +01:00
|
|
|
|
2024-05-11 13:31:33 +02:00
|
|
|
[[nodiscard]] bool is_declarative_environment() const { return m_declarative; }
|
2021-07-01 12:24:46 +02:00
|
|
|
virtual bool is_global_environment() const { return false; }
|
|
|
|
virtual bool is_function_environment() const { return false; }
|
2021-06-23 13:04:52 +02:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool fast_is() const = delete;
|
|
|
|
|
2021-10-07 11:36:44 +02:00
|
|
|
// This flag is set on the entire variable environment chain when direct eval() is performed.
|
|
|
|
// It is used to disable non-local variable access caching.
|
|
|
|
bool is_permanently_screwed_by_eval() const { return m_permanently_screwed_by_eval; }
|
|
|
|
void set_permanently_screwed_by_eval();
|
|
|
|
|
2020-11-28 16:02:27 +01:00
|
|
|
protected:
|
2024-05-11 13:31:33 +02:00
|
|
|
enum class IsDeclarative {
|
|
|
|
No,
|
|
|
|
Yes,
|
|
|
|
};
|
|
|
|
explicit Environment(Environment* parent, IsDeclarative = IsDeclarative::No);
|
2020-11-28 16:02:27 +01:00
|
|
|
|
|
|
|
virtual void visit_edges(Visitor&) override;
|
|
|
|
|
|
|
|
private:
|
2022-01-31 12:36:43 +01:00
|
|
|
bool m_permanently_screwed_by_eval { false };
|
2024-05-11 13:31:33 +02:00
|
|
|
bool m_declarative { false };
|
2022-01-31 12:36:43 +01:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ptr<Environment> m_outer_environment;
|
2020-11-28 16:02:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|