chromium/third_party/blink/renderer/bindings/core/v8/native_value_traits_impl.h

// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_NATIVE_VALUE_TRAITS_IMPL_H_
#define THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_NATIVE_VALUE_TRAITS_IMPL_H_

#include <concepts>
#include <optional>
#include <type_traits>

#include "third_party/blink/renderer/bindings/core/v8/idl_types.h"
#include "third_party/blink/renderer/bindings/core/v8/native_value_traits.h"
#include "third_party/blink/renderer/bindings/core/v8/pass_as_span.h"
#include "third_party/blink/renderer/bindings/core/v8/script_iterator.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/bindings/core/v8/script_value.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_trusted_html.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_trusted_script.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_trusted_script_url.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/trustedtypes/trusted_html.h"
#include "third_party/blink/renderer/core/trustedtypes/trusted_script.h"
#include "third_party/blink/renderer/core/trustedtypes/trusted_script_url.h"
#include "third_party/blink/renderer/core/trustedtypes/trusted_types_util.h"
#include "third_party/blink/renderer/core/typed_arrays/dom_data_view.h"
#include "third_party/blink/renderer/core/typed_arrays/dom_typed_array.h"
#include "third_party/blink/renderer/platform/bindings/bigint.h"
#include "third_party/blink/renderer/platform/bindings/exception_messages.h"
#include "third_party/blink/renderer/platform/heap/heap_traits.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "v8/include/v8-fast-api-calls.h"

namespace blink {

class CallbackFunctionBase;
class CallbackInterfaceBase;
class EventListener;
class GPUColorTargetState;
class GPURenderPassColorAttachment;
class GPUVertexBufferLayout;
class ScriptWrappable;
struct WrapperTypeInfo;

namespace bindings {

class DictionaryBase;
class EnumerationBase;
class UnionBase;

CORE_EXPORT void NativeValueTraitsInterfaceNotOfType(
    const WrapperTypeInfo* wrapper_type_info,
    ExceptionState& exception_state);

CORE_EXPORT void NativeValueTraitsInterfaceNotOfType(
    const WrapperTypeInfo* wrapper_type_info,
    int argument_index,
    ExceptionState& exception_state);

// Class created for IDLAny types. Converts to either ScriptValue or
// v8::Local<v8::Value>.
class CORE_EXPORT NativeValueTraitsAnyAdapter {};

}  // namespace bindings

// any
template <>
struct CORE_EXPORT NativeValueTraits<IDLAny>
    : public NativeValueTraitsBase<IDLAny> {};

// IDLNullable<IDLAny> must not be used.
template <>
struct NativeValueTraits<IDLNullable<IDLAny>>;

template <>
struct CORE_EXPORT NativeValueTraits<IDLOptional<IDLAny>>
    : public NativeValueTraitsBase<IDLOptional<IDLAny>> {};

// boolean
template <>
struct CORE_EXPORT NativeValueTraits<IDLBoolean>
    : public NativeValueTraitsBase<IDLBoolean> {};

template <>
struct CORE_EXPORT NativeValueTraits<IDLOptional<IDLBoolean>>
    : public NativeValueTraitsBase<IDLOptional<IDLBoolean>> {};

// bigint
template <>
struct CORE_EXPORT NativeValueTraits<IDLBigint>
    : public NativeValueTraitsBase<IDLBigint> {};

// Integer types
#define DEFINE_NATIVE_VALUE_TRAITS_INTEGER_TYPE
DEFINE_NATIVE_VALUE_TRAITS_INTEGER_TYPE;
DEFINE_NATIVE_VALUE_TRAITS_INTEGER_TYPE;
DEFINE_NATIVE_VALUE_TRAITS_INTEGER_TYPE;
DEFINE_NATIVE_VALUE_TRAITS_INTEGER_TYPE;
DEFINE_NATIVE_VALUE_TRAITS_INTEGER_TYPE;
DEFINE_NATIVE_VALUE_TRAITS_INTEGER_TYPE;
DEFINE_NATIVE_VALUE_TRAITS_INTEGER_TYPE;
DEFINE_NATIVE_VALUE_TRAITS_INTEGER_TYPE;
#undef DEFINE_NATIVE_VALUE_TRAITS_INTEGER_TYPE

// Floats and doubles
template <>
struct CORE_EXPORT NativeValueTraits<IDLDouble>
    : public NativeValueTraitsBase<IDLDouble> {};

template <>
struct CORE_EXPORT NativeValueTraits<IDLUnrestrictedDouble>
    : public NativeValueTraitsBase<IDLUnrestrictedDouble> {};

template <>
struct CORE_EXPORT NativeValueTraits<IDLFloat>
    : public NativeValueTraitsBase<IDLFloat> {};

template <>
struct CORE_EXPORT NativeValueTraits<IDLUnrestrictedFloat>
    : public NativeValueTraitsBase<IDLUnrestrictedFloat> {};

// Strings

namespace bindings {

// ToBlinkString implements AtomicString- and String-specific conversions from
// v8::String.  NativeValueTraitsStringAdapter helps select the best conversion.
//
// Example:
//   void F(const AtomicString& s);
//   void G(const String& s);
//
//   const NativeValueTraitsStringAdapter& x = ...;
//   F(x);  // ToBlinkString<AtomicString> is used.
//   G(x);  // ToBlinkString<String> is used.
class CORE_EXPORT NativeValueTraitsStringAdapter {};

}  // namespace bindings

NativeValueTraits<IDLByteStringBase<mode>>;

template <>
struct CORE_EXPORT NativeValueTraits<IDLNullable<IDLByteString>>
    : public NativeValueTraitsBase<IDLNullable<IDLByteString>> {};

template <>
struct CORE_EXPORT NativeValueTraits<IDLOptional<IDLByteString>>
    : public NativeValueTraitsBase<IDLOptional<IDLByteString>> {};

NativeValueTraits<IDLStringBase<mode>>;

template <>
struct CORE_EXPORT NativeValueTraits<IDLNullable<IDLString>>
    : public NativeValueTraitsBase<IDLNullable<IDLString>> {};

template <>
struct CORE_EXPORT NativeValueTraits<IDLOptional<IDLString>>
    : public NativeValueTraitsBase<IDLOptional<IDLString>> {};

NativeValueTraits<IDLUSVStringBase<mode>>;

template <>
struct CORE_EXPORT NativeValueTraits<IDLNullable<IDLUSVString>>
    : public NativeValueTraitsBase<IDLNullable<IDLUSVString>> {};

template <>
struct CORE_EXPORT NativeValueTraits<IDLOptional<IDLUSVString>>
    : public NativeValueTraitsBase<IDLOptional<IDLUSVString>> {};

NativeValueTraits<IDLStringStringContextTrustedHTMLBase<mode>>;

template <>
struct CORE_EXPORT
    NativeValueTraits<IDLNullable<IDLStringStringContextTrustedHTML>>
    : public NativeValueTraitsBase<
          IDLNullable<IDLStringStringContextTrustedHTML>> {};

NativeValueTraits<IDLStringStringContextTrustedScriptBase<mode>>;

template <>
struct CORE_EXPORT
    NativeValueTraits<IDLNullable<IDLStringStringContextTrustedScript>>
    : public NativeValueTraitsBase<
          IDLNullable<IDLStringStringContextTrustedScript>> {};

NativeValueTraits<IDLUSVStringStringContextTrustedScriptURLBase<mode>>;

template <>
struct CORE_EXPORT
    NativeValueTraits<IDLNullable<IDLUSVStringStringContextTrustedScriptURL>>
    : public NativeValueTraitsBase<
          IDLNullable<IDLUSVStringStringContextTrustedScriptURL>> {};

// Buffer source types
template <>
struct CORE_EXPORT NativeValueTraits<DOMArrayBuffer>
    : public NativeValueTraitsBase<DOMArrayBuffer*> {};

template <>
struct CORE_EXPORT NativeValueTraits<IDLNullable<DOMArrayBuffer>>
    : public NativeValueTraitsBase<DOMArrayBuffer*> {};

template <>
struct CORE_EXPORT NativeValueTraits<IDLAllowResizable<DOMArrayBuffer>>
    : public NativeValueTraitsBase<DOMArrayBuffer*> {};

template <>
struct CORE_EXPORT NativeValueTraits<DOMSharedArrayBuffer>
    : public NativeValueTraitsBase<DOMSharedArrayBuffer*> {};

template <>
struct CORE_EXPORT NativeValueTraits<IDLNullable<DOMSharedArrayBuffer>>
    : public NativeValueTraitsBase<DOMSharedArrayBuffer*> {};

template <>
struct CORE_EXPORT NativeValueTraits<IDLAllowResizable<DOMSharedArrayBuffer>>
    : public NativeValueTraitsBase<DOMSharedArrayBuffer*> {};

// DOMArrayBufferBase is the common base class of DOMArrayBuffer and
// DOMSharedArrayBuffer, so it behaves as "[AllowShared] ArrayBuffer" in
// Web IDL.
template <>
struct CORE_EXPORT NativeValueTraits<DOMArrayBufferBase>
    : public NativeValueTraitsBase<DOMArrayBufferBase*> {};

template <>
struct CORE_EXPORT
    NativeValueTraits<IDLBufferSourceTypeNoSizeLimit<DOMArrayBufferBase>>
    : public NativeValueTraitsBase<DOMArrayBufferBase*> {};

template <>
struct CORE_EXPORT NativeValueTraits<IDLNullable<DOMArrayBufferBase>>
    : public NativeValueTraitsBase<DOMArrayBufferBase*> {};

template <>
struct CORE_EXPORT NativeValueTraits<
    IDLNullable<IDLBufferSourceTypeNoSizeLimit<DOMArrayBufferBase>>>
    : public NativeValueTraitsBase<DOMArrayBufferBase*> {};

NativeValueTraits<T>;

NativeValueTraits<IDLNullable<T>>;

NativeValueTraits<NotShared<T>>;

NativeValueTraits<IDLNullable<NotShared<T>>>;

NativeValueTraits<MaybeShared<T>>;

NativeValueTraits<IDLBufferSourceTypeNoSizeLimit<MaybeShared<T>>>;

NativeValueTraits<IDLNullable<MaybeShared<T>>>;

NativeValueTraits<IDLNullable<IDLBufferSourceTypeNoSizeLimit<MaybeShared<T>>>>;

// object
template <>
struct CORE_EXPORT NativeValueTraits<IDLObject>
    : public NativeValueTraitsBase<IDLObject> {};

template <>
struct CORE_EXPORT NativeValueTraits<IDLNullable<IDLObject>>
    : public NativeValueTraitsBase<IDLNullable<IDLObject>> {};

// Promise types
template <>
struct CORE_EXPORT NativeValueTraits<IDLPromise>
    : public NativeValueTraitsBase<IDLPromise> {};

// IDLNullable<IDLPromise> must not be used.
template <>
struct NativeValueTraits<IDLNullable<IDLPromise>>;

// Sequence types

// IDLSequence's implementation is a little tricky due to a historical reason.
// The following type mapping is used for IDLSequence and its variants.
//
// tl;dr: Only IDLNullable<IDLSequence<traceable_type>> is a reference type.
//   The others are value types.
//
// - IDLSequence<T> where T is not traceable
//   => Vector<T> as a value type
// - IDLSequence<T> where T is traceable
//   => HeapVector<T> as a value type despite that HeapVector is
//      GarbageCollected because HeapVector had been implemented as a non-GC
//      type (a value type) for years until 2021 January.  This point is very
//      inconsistent but kept unchanged so far.
// - IDLNullable<IDLSequence<T>> where T is not traceable
//   => std::optional<Vector<T>> as a value type
// - IDLNullable<IDLSequence<T>> where T is traceable
//   => HeapVector<T>* as a reference type.  std::optional<HeapVector<T>> is
//      not an option because it's not appropriately traceable despite that
//      the content HeapVector needs tracing.  As same as other
//      GarbageCollected types, pointer type is used to represent IDL nullable
//      type.
NativeValueTraits<IDLSequence<T>>;

namespace bindings {

// Slow case: follow WebIDL's "Creating a sequence from an iterable" steps to
// iterate through each element.
template <typename T>
typename NativeValueTraits<IDLSequence<T>>::ImplType
CreateIDLSequenceFromIterator(v8::Isolate* isolate,
                              ScriptIterator script_iterator,
                              ExceptionState& exception_state) {}

// Faster case: non template-specialized implementation that iterates over an
// Array that adheres to %ArrayIteratorPrototype%'s protocol.
template <typename T>
typename NativeValueTraits<IDLSequence<T>>::ImplType
CreateIDLSequenceFromV8ArraySlow(v8::Isolate* isolate,
                                 v8::Local<v8::Array> v8_array,
                                 ExceptionState& exception_state) {}

// Fastest case: template-specialized implementation that directly copies the
// contents of this JavaScript array into a C++ buffer.
template <typename T>
typename NativeValueTraits<IDLSequence<T>>::ImplType
CreateIDLSequenceFromV8Array(v8::Isolate* isolate,
                             v8::Local<v8::Array> v8_array,
                             ExceptionState& exception_state) {}

template <>
CORE_EXTERN_TEMPLATE_EXPORT
    typename NativeValueTraits<IDLSequence<IDLLong>>::ImplType
    CreateIDLSequenceFromV8Array<IDLLong>(v8::Isolate* isolate,
                                          v8::Local<v8::Array> v8_array,
                                          ExceptionState& exception_state);

}  // namespace bindings

template <typename T>
typename NativeValueTraits<IDLSequence<T>>::ImplType
NativeValueTraits<IDLSequence<T>>::NativeValue(
    v8::Isolate* isolate,
    v8::Local<v8::Value> value,
    ExceptionState& exception_state) {}

NativeValueTraits<IDLNullable<IDLSequence<T>>>;

NativeValueTraits<IDLOptional<IDLSequence<T>>>;

// Frozen array types
//
// Just for convenience, NativeValueTraits<IDLArray<T>> returns a mutable
// (Heap)Vector<T> rather than an immutable FrozenArray<T>. It's easy (and cheap
// when the move semantics is used) to convert a (Heap)Vector<T> to a
// FrozenArray<T>, but the reverse conversion is not.
//
// Note that it's possible that Blink implementation wants to make some
// modifications on the sequence before making it frozen. Thus this returns
// a mutable (Heap)Vector.
NativeValueTraits<IDLArray<T>>;

NativeValueTraits<IDLNullable<IDLArray<T>>>;

// Record types
NativeValueTraits<IDLRecord<K, V>>;

// Callback function types
NativeValueTraits<T>;

NativeValueTraits<IDLNullable<T>>;

// Callback interface types
NativeValueTraits<T>;

// Interface types
NativeValueTraits<IDLNullable<T>>;

// Dictionary types
NativeValueTraits<T>;

// We don't support nullable dictionary types in general since it's quite
// confusing and often misused.
NativeValueTraits<IDLNullable<T>>;

// Enumeration types
NativeValueTraits<T>;

// Interface types
NativeValueTraits<T>;

NativeValueTraits<IDLNullable<T>>;

NativeValueTraits<T>;

NativeValueTraits<IDLNullable<T>>;

// Nullable types
NativeValueTraits<IDLNullable<InnerType>>;

// IDLNullable<IDLNullable<T>> must not be used.
NativeValueTraits<IDLNullable<IDLNullable<T>>>;

// Optional types
NativeValueTraits<IDLOptional<T>>;

NativeValueTraits<IDLOptional<T>>;

// Date
template <>
struct CORE_EXPORT NativeValueTraits<IDLDate>
    : public NativeValueTraitsBase<IDLDate> {};

template <>
struct CORE_EXPORT NativeValueTraits<IDLNullable<IDLDate>>
    : public NativeValueTraitsBase<IDLNullable<IDLDate>> {};

// EventHandler
template <>
struct CORE_EXPORT NativeValueTraits<IDLEventHandler>
    : public NativeValueTraitsBase<IDLEventHandler> {};

template <>
struct CORE_EXPORT NativeValueTraits<IDLOnBeforeUnloadEventHandler>
    : public NativeValueTraitsBase<IDLOnBeforeUnloadEventHandler> {};

template <>
struct CORE_EXPORT NativeValueTraits<IDLOnErrorEventHandler>
    : public NativeValueTraitsBase<IDLOnErrorEventHandler> {};

// EventHandler and its family are nullable, so IDLNullable<IDLEventHandler>
// must not be used.
template <>
struct NativeValueTraits<IDLNullable<IDLEventHandler>>;
template <>
struct NativeValueTraits<IDLNullable<IDLOnBeforeUnloadEventHandler>>;
template <>
struct NativeValueTraits<IDLNullable<IDLOnErrorEventHandler>>;

NativeValueTraits<T>;

NativeValueTraits<T>;

NativeValueTraits<IDLOptional<T>>;

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_NATIVE_VALUE_TRAITS_IMPL_H_