/* * Copyright (c) 2022, Andrew Kaster * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include namespace Web::FileAPI { GC_DEFINE_ALLOCATOR(FileList); GC::Ref FileList::create(JS::Realm& realm) { return realm.create(realm); } FileList::FileList(JS::Realm& realm) : Bindings::PlatformObject(realm) { m_legacy_platform_object_flags = LegacyPlatformObjectFlags { .supports_indexed_properties = 1 }; } FileList::~FileList() = default; void FileList::initialize(JS::Realm& realm) { Base::initialize(realm); WEB_SET_PROTOTYPE_FOR_INTERFACE(FileList); } Optional FileList::item_value(size_t index) const { if (index >= m_files.size()) return {}; return m_files[index].ptr(); } void FileList::visit_edges(Cell::Visitor& visitor) { Base::visit_edges(visitor); visitor.visit(m_files); } WebIDL::ExceptionOr FileList::serialization_steps(HTML::SerializationRecord& serialized, bool for_storage, HTML::SerializationMemory& memory) { auto& vm = this->vm(); // 1. Set serialized.[[Files]] to an empty list. // 2. For each file in value, append the sub-serialization of file to serialized.[[Files]]. HTML::serialize_primitive_type(serialized, m_files.size()); for (auto& file : m_files) serialized.extend(TRY(HTML::structured_serialize_internal(vm, file, for_storage, memory))); return {}; } WebIDL::ExceptionOr FileList::deserialization_steps(ReadonlySpan const& serialized, size_t& position, HTML::DeserializationMemory& memory) { auto& vm = this->vm(); auto& realm = *vm.current_realm(); // 1. For each file of serialized.[[Files]], add the sub-deserialization of file to value. auto size = HTML::deserialize_primitive_type(serialized, position); for (size_t i = 0; i < size; ++i) { auto deserialized_record = TRY(HTML::structured_deserialize_internal(vm, serialized, realm, memory, position)); if (deserialized_record.value.has_value() && is(deserialized_record.value.value().as_object())) m_files.append(dynamic_cast(deserialized_record.value.release_value().as_object())); position = deserialized_record.position; } return {}; } }