2020-11-28 15:02:27 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2020-2022, Andreas Kling <andreas@ladybird.org>
|
2020-11-28 15:02:27 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-11-28 15:02:27 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-03-17 00:26:49 +00:00
|
|
|
#include <AK/StringView.h>
|
2021-10-09 15:52:34 +00:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2020-11-28 15:02:27 +00:00
|
|
|
#include <LibJS/Runtime/Object.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
struct Variable {
|
|
|
|
Value value;
|
|
|
|
DeclarationKind declaration_kind;
|
|
|
|
};
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
#define JS_ENVIRONMENT(class_, base_class) GC_CELL(class_, base_class)
|
2020-11-28 15:02:27 +00:00
|
|
|
|
2021-07-01 10:24:46 +00:00
|
|
|
class Environment : public Cell {
|
2024-11-14 15:01:23 +00:00
|
|
|
GC_CELL(Environment, Cell);
|
2022-08-28 20:11:20 +00:00
|
|
|
|
2020-11-28 15:02:27 +00:00
|
|
|
public:
|
2022-12-14 12:26:10 +00:00
|
|
|
enum class InitializeBindingHint {
|
|
|
|
Normal,
|
|
|
|
SyncDispose,
|
|
|
|
};
|
|
|
|
|
2021-06-22 11:30:48 +00:00
|
|
|
virtual bool has_this_binding() const { return false; }
|
2022-08-21 14:12:43 +00:00
|
|
|
virtual ThrowCompletionOr<Value> get_this_binding(VM&) const { return Value {}; }
|
2020-11-28 15:02:27 +00:00
|
|
|
|
2021-06-24 11:24:44 +00:00
|
|
|
virtual Object* with_base_object() const { return nullptr; }
|
|
|
|
|
2023-01-09 00:23:00 +00:00
|
|
|
virtual ThrowCompletionOr<bool> has_binding([[maybe_unused]] DeprecatedFlyString const& name, [[maybe_unused]] Optional<size_t>* out_index = nullptr) const { return false; }
|
|
|
|
virtual ThrowCompletionOr<void> create_mutable_binding(VM&, [[maybe_unused]] DeprecatedFlyString const& name, [[maybe_unused]] bool can_be_deleted) { return {}; }
|
|
|
|
virtual ThrowCompletionOr<void> create_immutable_binding(VM&, [[maybe_unused]] DeprecatedFlyString const& name, [[maybe_unused]] bool strict) { return {}; }
|
2022-12-14 12:26:10 +00:00
|
|
|
virtual ThrowCompletionOr<void> initialize_binding(VM&, [[maybe_unused]] DeprecatedFlyString const& name, Value, InitializeBindingHint) { return {}; }
|
2023-01-09 00:23:00 +00:00
|
|
|
virtual ThrowCompletionOr<void> set_mutable_binding(VM&, [[maybe_unused]] DeprecatedFlyString const& name, Value, [[maybe_unused]] bool strict) { return {}; }
|
|
|
|
virtual ThrowCompletionOr<Value> get_binding_value(VM&, [[maybe_unused]] DeprecatedFlyString const& name, [[maybe_unused]] bool strict) { return Value {}; }
|
|
|
|
virtual ThrowCompletionOr<bool> delete_binding(VM&, [[maybe_unused]] DeprecatedFlyString const& name) { return false; }
|
2021-06-23 10:26:37 +00:00
|
|
|
|
2021-06-21 21:35:30 +00:00
|
|
|
// [[OuterEnv]]
|
2021-07-01 10:24:46 +00:00
|
|
|
Environment* outer_environment() { return m_outer_environment; }
|
|
|
|
Environment const* outer_environment() const { return m_outer_environment; }
|
2020-11-28 15:02:27 +00:00
|
|
|
|
2024-05-11 11:31:33 +00:00
|
|
|
[[nodiscard]] bool is_declarative_environment() const { return m_declarative; }
|
2021-07-01 10:24:46 +00:00
|
|
|
virtual bool is_global_environment() const { return false; }
|
|
|
|
virtual bool is_function_environment() const { return false; }
|
2021-06-23 11:04:52 +00:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool fast_is() const = delete;
|
|
|
|
|
2021-10-07 09:36:44 +00: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 15:02:27 +00:00
|
|
|
protected:
|
2024-05-11 11:31:33 +00:00
|
|
|
enum class IsDeclarative {
|
|
|
|
No,
|
|
|
|
Yes,
|
|
|
|
};
|
|
|
|
explicit Environment(Environment* parent, IsDeclarative = IsDeclarative::No);
|
2020-11-28 15:02:27 +00:00
|
|
|
|
|
|
|
virtual void visit_edges(Visitor&) override;
|
|
|
|
|
|
|
|
private:
|
2022-01-31 11:36:43 +00:00
|
|
|
bool m_permanently_screwed_by_eval { false };
|
2024-05-11 11:31:33 +00:00
|
|
|
bool m_declarative { false };
|
2022-01-31 11:36:43 +00:00
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ptr<Environment> m_outer_environment;
|
2020-11-28 15:02:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|