mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:38:11 +00:00

This patch adds the concept of variable bindings to the various environment record classes. The bindings are not yet hooked up to anything, this is just fleshing out all the operations. Most of this is following the spec exactly, but in a few cases we are missing the requisite abstract operations to do the exact right thing. I've added FIXME's in those cases where I noticed it.
73 lines
2.6 KiB
C++
73 lines
2.6 KiB
C++
/*
|
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/FlyString.h>
|
|
#include <AK/HashMap.h>
|
|
#include <LibJS/Runtime/EnvironmentRecord.h>
|
|
#include <LibJS/Runtime/Value.h>
|
|
|
|
namespace JS {
|
|
|
|
struct Binding {
|
|
Value value;
|
|
bool strict;
|
|
bool mutable_ { false };
|
|
bool can_be_deleted { false };
|
|
bool initialized { false };
|
|
};
|
|
|
|
class DeclarativeEnvironmentRecord : public EnvironmentRecord {
|
|
JS_OBJECT(DeclarativeEnvironmentRecord, EnvironmentRecord);
|
|
|
|
public:
|
|
enum class EnvironmentRecordType {
|
|
Declarative,
|
|
Function,
|
|
Object,
|
|
Module,
|
|
};
|
|
|
|
DeclarativeEnvironmentRecord();
|
|
DeclarativeEnvironmentRecord(EnvironmentRecordType);
|
|
explicit DeclarativeEnvironmentRecord(EnvironmentRecord* parent_scope);
|
|
DeclarativeEnvironmentRecord(HashMap<FlyString, Variable> variables, EnvironmentRecord* parent_scope);
|
|
DeclarativeEnvironmentRecord(HashMap<FlyString, Variable> variables, EnvironmentRecord* parent_scope, EnvironmentRecordType);
|
|
virtual ~DeclarativeEnvironmentRecord() override;
|
|
|
|
// ^EnvironmentRecord
|
|
virtual Optional<Variable> get_from_environment_record(FlyString const&) const override;
|
|
virtual void put_into_environment_record(FlyString const&, Variable) override;
|
|
virtual bool delete_from_environment_record(FlyString const&) override;
|
|
|
|
HashMap<FlyString, Variable> const& variables() const { return m_variables; }
|
|
|
|
EnvironmentRecordType type() const { return m_environment_record_type; }
|
|
|
|
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;
|
|
|
|
protected:
|
|
virtual void visit_edges(Visitor&) override;
|
|
|
|
private:
|
|
virtual bool is_declarative_environment_record() const override { return true; }
|
|
|
|
EnvironmentRecordType m_environment_record_type : 8 { EnvironmentRecordType::Declarative };
|
|
HashMap<FlyString, Variable> m_variables;
|
|
HashMap<FlyString, Binding> m_bindings;
|
|
};
|
|
|
|
template<>
|
|
inline bool Object::fast_is<DeclarativeEnvironmentRecord>() const { return is_declarative_environment_record(); }
|
|
|
|
}
|