2020-04-15 21:58:22 +02:00
|
|
|
/*
|
2021-06-21 23:17:24 +02:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
2020-04-15 21:58:22 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-15 21:58:22 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/FlyString.h>
|
|
|
|
|
#include <AK/HashMap.h>
|
2021-06-21 23:17:24 +02:00
|
|
|
#include <LibJS/Runtime/EnvironmentRecord.h>
|
2020-04-15 21:58:22 +02:00
|
|
|
#include <LibJS/Runtime/Value.h>
|
|
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
2021-06-21 23:17:24 +02:00
|
|
|
class DeclarativeEnvironmentRecord final : public EnvironmentRecord {
|
|
|
|
|
JS_OBJECT(DeclarativeEnvironmentRecord, EnvironmentRecord);
|
2020-04-15 21:58:22 +02:00
|
|
|
|
|
|
|
|
public:
|
2020-06-08 13:31:21 -05:00
|
|
|
enum class ThisBindingStatus {
|
|
|
|
|
Lexical,
|
|
|
|
|
Initialized,
|
|
|
|
|
Uninitialized,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum class EnvironmentRecordType {
|
|
|
|
|
Declarative,
|
|
|
|
|
Function,
|
|
|
|
|
Object,
|
|
|
|
|
Module,
|
|
|
|
|
};
|
|
|
|
|
|
2021-06-21 23:17:24 +02:00
|
|
|
DeclarativeEnvironmentRecord();
|
|
|
|
|
DeclarativeEnvironmentRecord(EnvironmentRecordType);
|
|
|
|
|
DeclarativeEnvironmentRecord(HashMap<FlyString, Variable> variables, EnvironmentRecord* parent_scope);
|
|
|
|
|
DeclarativeEnvironmentRecord(HashMap<FlyString, Variable> variables, EnvironmentRecord* parent_scope, EnvironmentRecordType);
|
|
|
|
|
virtual ~DeclarativeEnvironmentRecord() override;
|
2020-04-15 21:58:22 +02:00
|
|
|
|
2021-06-21 23:17:24 +02:00
|
|
|
// ^EnvironmentRecord
|
2021-06-21 23:26:50 +02:00
|
|
|
virtual Optional<Variable> get_from_scope(FlyString const&) const override;
|
|
|
|
|
virtual void put_to_scope(FlyString const&, Variable) override;
|
2021-06-08 04:00:53 +03:00
|
|
|
virtual bool delete_from_scope(FlyString const&) override;
|
2020-11-28 16:02:27 +01:00
|
|
|
virtual bool has_this_binding() const override;
|
|
|
|
|
virtual Value get_this_binding(GlobalObject&) const override;
|
2020-04-15 21:58:22 +02:00
|
|
|
|
2021-06-21 23:26:50 +02:00
|
|
|
HashMap<FlyString, Variable> const& variables() const { return m_variables; }
|
2020-04-15 21:58:22 +02:00
|
|
|
|
2020-06-08 13:31:21 -05:00
|
|
|
void set_home_object(Value object) { m_home_object = object; }
|
|
|
|
|
bool has_super_binding() const;
|
|
|
|
|
Value get_super_base();
|
|
|
|
|
|
|
|
|
|
ThisBindingStatus this_binding_status() const { return m_this_binding_status; }
|
2020-09-29 16:41:28 +02:00
|
|
|
void bind_this_value(GlobalObject&, Value this_value);
|
2020-06-08 13:31:21 -05:00
|
|
|
|
|
|
|
|
// Not a standard operation.
|
|
|
|
|
void replace_this_binding(Value this_value) { m_this_value = this_value; }
|
|
|
|
|
|
|
|
|
|
Value new_target() const { return m_new_target; };
|
|
|
|
|
void set_new_target(Value new_target) { m_new_target = new_target; }
|
|
|
|
|
|
|
|
|
|
Function* current_function() const { return m_current_function; }
|
|
|
|
|
void set_current_function(Function& function) { m_current_function = &function; }
|
|
|
|
|
|
2020-09-27 15:18:55 +02:00
|
|
|
EnvironmentRecordType type() const { return m_environment_record_type; }
|
|
|
|
|
|
2020-04-15 21:58:22 +02:00
|
|
|
private:
|
2021-06-21 23:17:24 +02:00
|
|
|
virtual bool is_declarative_environment_record() const override { return true; }
|
2020-11-28 14:33:36 +01:00
|
|
|
virtual void visit_edges(Visitor&) override;
|
2020-04-15 21:58:22 +02:00
|
|
|
|
2020-10-22 17:03:40 +02:00
|
|
|
EnvironmentRecordType m_environment_record_type : 8 { EnvironmentRecordType::Declarative };
|
|
|
|
|
ThisBindingStatus m_this_binding_status : 8 { ThisBindingStatus::Uninitialized };
|
2020-04-15 21:58:22 +02:00
|
|
|
HashMap<FlyString, Variable> m_variables;
|
2020-06-08 13:31:21 -05:00
|
|
|
Value m_home_object;
|
|
|
|
|
Value m_this_value;
|
|
|
|
|
Value m_new_target;
|
|
|
|
|
// Corresponds to [[FunctionObject]]
|
|
|
|
|
Function* m_current_function { nullptr };
|
2020-04-15 21:58:22 +02:00
|
|
|
};
|
|
|
|
|
|
2021-06-13 11:23:23 +02:00
|
|
|
template<>
|
2021-06-21 23:17:24 +02:00
|
|
|
inline bool Object::fast_is<DeclarativeEnvironmentRecord>() const { return is_declarative_environment_record(); }
|
2021-06-13 11:23:23 +02:00
|
|
|
|
2020-04-15 21:58:22 +02:00
|
|
|
}
|