2020-04-27 10:10:16 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <andreas@ladybird.org>
|
2020-04-27 10:10:16 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-27 10:10:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-07-01 10:24:46 +00:00
|
|
|
#include <LibJS/Runtime/Environment.h>
|
2021-10-06 23:06:21 +00:00
|
|
|
#include <LibJS/Runtime/EnvironmentCoordinate.h>
|
2021-10-24 14:01:24 +00:00
|
|
|
#include <LibJS/Runtime/PropertyKey.h>
|
2020-04-27 10:10:16 +00:00
|
|
|
#include <LibJS/Runtime/Value.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2023-01-09 00:23:00 +00:00
|
|
|
Reference make_private_reference(VM&, Value base_value, DeprecatedFlyString const& private_identifier);
|
2021-10-12 19:48:50 +00:00
|
|
|
|
2020-04-27 10:10:16 +00:00
|
|
|
class Reference {
|
|
|
|
public:
|
2021-06-25 14:27:59 +00:00
|
|
|
enum class BaseType : u8 {
|
|
|
|
Unresolvable,
|
|
|
|
Value,
|
2021-07-01 10:24:46 +00:00
|
|
|
Environment,
|
2021-06-25 14:27:59 +00:00
|
|
|
};
|
|
|
|
|
2021-10-24 14:01:24 +00:00
|
|
|
Reference(BaseType type, PropertyKey name, bool strict)
|
2021-06-25 14:27:59 +00:00
|
|
|
: m_base_type(type)
|
2021-09-11 17:03:38 +00:00
|
|
|
, m_name(move(name))
|
2020-04-27 10:10:16 +00:00
|
|
|
, m_strict(strict)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-10-24 14:01:24 +00:00
|
|
|
Reference(Value base, PropertyKey name, Value this_value, bool strict = false)
|
2021-06-25 14:27:59 +00:00
|
|
|
: m_base_type(BaseType::Value)
|
|
|
|
, m_base_value(base)
|
2021-09-11 17:03:38 +00:00
|
|
|
, m_name(move(name))
|
2021-06-25 14:27:59 +00:00
|
|
|
, m_this_value(this_value)
|
2020-04-27 10:37:27 +00:00
|
|
|
, m_strict(strict)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-09 00:23:00 +00:00
|
|
|
Reference(Environment& base, DeprecatedFlyString referenced_name, bool strict = false, Optional<EnvironmentCoordinate> environment_coordinate = {})
|
2021-07-01 10:24:46 +00:00
|
|
|
: m_base_type(BaseType::Environment)
|
|
|
|
, m_base_environment(&base)
|
2021-09-11 17:03:38 +00:00
|
|
|
, m_name(move(referenced_name))
|
2020-04-28 12:44:48 +00:00
|
|
|
, m_strict(strict)
|
2021-10-06 23:06:21 +00:00
|
|
|
, m_environment_coordinate(move(environment_coordinate))
|
2020-04-28 12:44:48 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-10-12 19:48:50 +00:00
|
|
|
Reference(Value base, PrivateName name)
|
|
|
|
: m_base_type(BaseType::Value)
|
|
|
|
, m_base_value(base)
|
2024-11-01 20:36:48 +00:00
|
|
|
, m_name(move(name))
|
2021-10-12 19:48:50 +00:00
|
|
|
, m_strict(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-25 14:27:59 +00:00
|
|
|
Value base() const
|
|
|
|
{
|
|
|
|
VERIFY(m_base_type == BaseType::Value);
|
|
|
|
return m_base_value;
|
|
|
|
}
|
2020-04-27 10:10:16 +00:00
|
|
|
|
2021-07-01 10:24:46 +00:00
|
|
|
Environment& base_environment() const
|
2020-04-27 10:10:16 +00:00
|
|
|
{
|
2021-07-01 10:24:46 +00:00
|
|
|
VERIFY(m_base_type == BaseType::Environment);
|
|
|
|
return *m_base_environment;
|
2020-04-27 10:10:16 +00:00
|
|
|
}
|
|
|
|
|
2024-11-01 20:36:48 +00:00
|
|
|
PropertyKey const& name() const { return m_name.get<PropertyKey>(); }
|
|
|
|
PrivateName const& private_name() const { return m_name.get<PrivateName>(); }
|
2021-06-25 14:27:59 +00:00
|
|
|
bool is_strict() const { return m_strict; }
|
|
|
|
|
|
|
|
// 6.2.4.2 IsUnresolvableReference ( V ), https://tc39.es/ecma262/#sec-isunresolvablereference
|
|
|
|
bool is_unresolvable() const { return m_base_type == BaseType::Unresolvable; }
|
|
|
|
|
|
|
|
// 6.2.4.1 IsPropertyReference ( V ), https://tc39.es/ecma262/#sec-ispropertyreference
|
|
|
|
bool is_property_reference() const
|
2020-04-27 10:10:16 +00:00
|
|
|
{
|
2021-06-25 14:27:59 +00:00
|
|
|
if (is_unresolvable())
|
|
|
|
return false;
|
2021-07-01 10:24:46 +00:00
|
|
|
if (m_base_type == BaseType::Environment)
|
2021-06-25 14:27:59 +00:00
|
|
|
return false;
|
2021-10-12 19:48:50 +00:00
|
|
|
return true;
|
2020-04-27 10:10:16 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 14:27:59 +00:00
|
|
|
// 6.2.4.7 GetThisValue ( V ), https://tc39.es/ecma262/#sec-getthisvalue
|
|
|
|
Value get_this_value() const
|
2020-04-27 10:37:27 +00:00
|
|
|
{
|
2021-06-25 14:27:59 +00:00
|
|
|
VERIFY(is_property_reference());
|
|
|
|
if (is_super_reference())
|
|
|
|
return m_this_value;
|
|
|
|
return m_base_value;
|
2020-04-27 10:37:27 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 14:27:59 +00:00
|
|
|
// 6.2.4.3 IsSuperReference ( V ), https://tc39.es/ecma262/#sec-issuperreference
|
|
|
|
bool is_super_reference() const
|
2020-04-28 12:44:48 +00:00
|
|
|
{
|
2021-06-25 14:27:59 +00:00
|
|
|
return !m_this_value.is_empty();
|
2020-04-28 12:44:48 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 19:48:50 +00:00
|
|
|
// 6.2.4.4 IsPrivateReference ( V ), https://tc39.es/ecma262/#sec-isprivatereference
|
|
|
|
bool is_private_reference() const
|
|
|
|
{
|
2024-11-01 20:36:48 +00:00
|
|
|
return m_name.has<PrivateName>();
|
2021-10-12 19:48:50 +00:00
|
|
|
}
|
|
|
|
|
2021-09-14 02:26:31 +00:00
|
|
|
// Note: Non-standard helper.
|
|
|
|
bool is_environment_reference() const
|
|
|
|
{
|
|
|
|
return m_base_type == BaseType::Environment;
|
|
|
|
}
|
|
|
|
|
2022-12-14 12:26:10 +00:00
|
|
|
ThrowCompletionOr<void> initialize_referenced_binding(VM&, Value value, Environment::InitializeBindingHint hint = Environment::InitializeBindingHint::Normal) const;
|
2021-09-22 10:44:56 +00:00
|
|
|
|
2022-08-21 14:39:13 +00:00
|
|
|
ThrowCompletionOr<void> put_value(VM&, Value);
|
|
|
|
ThrowCompletionOr<Value> get_value(VM&) const;
|
|
|
|
ThrowCompletionOr<bool> delete_(VM&);
|
2020-04-27 10:56:09 +00:00
|
|
|
|
2024-11-01 20:36:48 +00:00
|
|
|
bool is_valid_reference() const { return true; }
|
2021-09-27 20:28:56 +00:00
|
|
|
|
2021-10-06 23:06:21 +00:00
|
|
|
Optional<EnvironmentCoordinate> environment_coordinate() const { return m_environment_coordinate; }
|
|
|
|
|
2020-04-27 10:10:16 +00:00
|
|
|
private:
|
2022-08-21 14:39:13 +00:00
|
|
|
Completion throw_reference_error(VM&) const;
|
2020-04-28 12:44:48 +00:00
|
|
|
|
2021-06-25 14:27:59 +00:00
|
|
|
BaseType m_base_type { BaseType::Unresolvable };
|
|
|
|
union {
|
2021-06-25 15:25:55 +00:00
|
|
|
Value m_base_value {};
|
2021-10-07 16:43:22 +00:00
|
|
|
mutable Environment* m_base_environment;
|
2021-06-25 14:27:59 +00:00
|
|
|
};
|
2024-11-01 20:36:48 +00:00
|
|
|
Variant<PropertyKey, PrivateName> m_name;
|
2021-06-25 14:27:59 +00:00
|
|
|
Value m_this_value;
|
2020-04-27 10:10:16 +00:00
|
|
|
bool m_strict { false };
|
2021-10-12 19:48:50 +00:00
|
|
|
|
2021-10-06 23:06:21 +00:00
|
|
|
Optional<EnvironmentCoordinate> m_environment_coordinate;
|
2020-04-27 10:10:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|