Truly independent web browser
/*
* Copyright (c) 2021-2022, Linus Groh <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Optional.h>
#include <AK/Utf16String.h>
#include <AK/Vector.h>
#include <LibGC/Cell.h>
#include <LibGC/Ptr.h>
#include <LibJS/Export.h>
#include <LibJS/Forward.h>
#include <LibJS/Runtime/Value.h>
namespace JS {
// 6.2.5 The Property Descriptor Specification Type, https://tc39.es/ecma262/#sec-property-descriptor-specification-type
class JS_API PropertyDescriptor {
public:
[[nodiscard]] bool is_accessor_descriptor() const;
[[nodiscard]] bool is_data_descriptor() const;
// Not a standard abstract operation, but "If every field in Desc is absent".
[[nodiscard]] bool is_empty() const
{
return !value.has_value() && !get.has_value() && !set.has_value() && !writable.has_value() && !enumerable.has_value() && !configurable.has_value();
}
void visit_edges(GC::Cell::Visitor&);
Optional<Value> value {};
Optional<GC::Ptr<FunctionObject>> get {};
Optional<GC::Ptr<FunctionObject>> set {};
Optional<bool> writable {};
Optional<bool> enumerable {};
Optional<bool> configurable {};
// The property's slot in its object's storage, which the runtime's inline caches are filled from.
Optional<u32> property_offset {};
};
}
namespace AK {
template<>
struct Formatter<JS::PropertyDescriptor> : Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, JS::PropertyDescriptor const& property_descriptor)
{
Vector<Utf16String> parts;
if (property_descriptor.value.has_value())
TRY(parts.try_append(Utf16String::formatted("[[Value]]: {}", property_descriptor.value->to_utf16_string_without_side_effects())));
if (property_descriptor.get.has_value())
TRY(parts.try_append(Utf16String::formatted("[[Get]]: JS::Function* @ {:p}", property_descriptor.get->ptr())));
if (property_descriptor.set.has_value())
TRY(parts.try_append(Utf16String::formatted("[[Set]]: JS::Function* @ {:p}", property_descriptor.set->ptr())));
if (property_descriptor.writable.has_value())
TRY(parts.try_append(Utf16String::formatted("[[Writable]]: {}", *property_descriptor.writable)));
if (property_descriptor.enumerable.has_value())
TRY(parts.try_append(Utf16String::formatted("[[Enumerable]]: {}", *property_descriptor.enumerable)));
if (property_descriptor.configurable.has_value())
TRY(parts.try_append(Utf16String::formatted("[[Configurable]]: {}", *property_descriptor.configurable)));
return Formatter<Utf16String> {}.format(builder, Utf16String::formatted("PropertyDescriptor {{ {} }}", Utf16String::join(", "sv, parts)));
}
};
}