2020-04-15 16:58:22 -03:00
|
|
|
/*
|
2021-06-21 18:17:24 -03:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
2020-04-15 16:58:22 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-15 16:58:22 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/FlyString.h>
|
|
|
|
|
#include <AK/HashMap.h>
|
2021-07-01 07:24:46 -03:00
|
|
|
#include <LibJS/Runtime/Environment.h>
|
2020-04-15 16:58:22 -03:00
|
|
|
#include <LibJS/Runtime/Value.h>
|
|
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
2021-07-01 07:24:46 -03:00
|
|
|
class DeclarativeEnvironment : public Environment {
|
|
|
|
|
JS_ENVIRONMENT(DeclarativeEnvironment, Environment);
|
2020-04-15 16:58:22 -03:00
|
|
|
|
|
|
|
|
public:
|
2021-07-01 07:24:46 -03:00
|
|
|
DeclarativeEnvironment();
|
|
|
|
|
explicit DeclarativeEnvironment(Environment* parent_scope);
|
|
|
|
|
DeclarativeEnvironment(HashMap<FlyString, Variable> variables, Environment* parent_scope);
|
|
|
|
|
virtual ~DeclarativeEnvironment() override;
|
2020-04-15 16:58:22 -03:00
|
|
|
|
2021-07-01 07:24:46 -03:00
|
|
|
// ^Environment
|
|
|
|
|
virtual Optional<Variable> get_from_environment(FlyString const&) const override;
|
2021-07-01 19:14:38 -03:00
|
|
|
virtual bool put_into_environment(FlyString const&, Variable) override;
|
2021-07-01 07:24:46 -03:00
|
|
|
virtual bool delete_from_environment(FlyString const&) override;
|
2020-04-15 16:58:22 -03:00
|
|
|
|
2021-06-21 18:26:50 -03:00
|
|
|
HashMap<FlyString, Variable> const& variables() const { return m_variables; }
|
2020-04-15 16:58:22 -03:00
|
|
|
|
2021-06-23 07:26:37 -03:00
|
|
|
virtual bool has_binding(FlyString const& name) const override;
|
|
|
|
|
virtual void create_mutable_binding(GlobalObject&, FlyString const& name, bool can_be_deleted) override;
|
|
|
|
|
virtual void create_immutable_binding(GlobalObject&, FlyString const& name, bool strict) override;
|
|
|
|
|
virtual void initialize_binding(GlobalObject&, FlyString const& name, Value) override;
|
|
|
|
|
virtual void set_mutable_binding(GlobalObject&, FlyString const& name, Value, bool strict) override;
|
|
|
|
|
virtual Value get_binding_value(GlobalObject&, FlyString const& name, bool strict) override;
|
|
|
|
|
virtual bool delete_binding(GlobalObject&, FlyString const& name) override;
|
|
|
|
|
|
2021-06-22 08:30:48 -03:00
|
|
|
protected:
|
|
|
|
|
virtual void visit_edges(Visitor&) override;
|
|
|
|
|
|
2020-04-15 16:58:22 -03:00
|
|
|
private:
|
2021-07-01 07:24:46 -03:00
|
|
|
virtual bool is_declarative_environment() const override { return true; }
|
2020-04-15 16:58:22 -03:00
|
|
|
|
|
|
|
|
HashMap<FlyString, Variable> m_variables;
|
2021-07-01 07:31:39 -03:00
|
|
|
|
|
|
|
|
struct Binding {
|
|
|
|
|
Value value;
|
|
|
|
|
bool strict { false };
|
|
|
|
|
bool mutable_ { false };
|
|
|
|
|
bool can_be_deleted { false };
|
|
|
|
|
bool initialized { false };
|
|
|
|
|
};
|
|
|
|
|
|
2021-06-23 07:26:37 -03:00
|
|
|
HashMap<FlyString, Binding> m_bindings;
|
2020-04-15 16:58:22 -03:00
|
|
|
};
|
|
|
|
|
|
2021-06-13 06:23:23 -03:00
|
|
|
template<>
|
2021-07-01 07:24:46 -03:00
|
|
|
inline bool Environment::fast_is<DeclarativeEnvironment>() const { return is_declarative_environment(); }
|
2021-06-13 06:23:23 -03:00
|
|
|
|
2020-04-15 16:58:22 -03:00
|
|
|
}
|