chromium/gin/converter.h

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

#ifndef GIN_CONVERTER_H_
#define GIN_CONVERTER_H_

#include <stdint.h>

#include <concepts>
#include <ostream>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>

#include "base/check.h"
#include "base/location.h"
#include "base/notreached.h"
#include "gin/gin_export.h"
#include "v8/include/v8-container.h"
#include "v8/include/v8-forward.h"
#include "v8/include/v8-isolate.h"
#include "v8/include/v8-source-location.h"

namespace base {
class TimeTicks;
}

namespace gin {

template<typename KeyType>
bool SetProperty(v8::Isolate* isolate,
                 v8::Local<v8::Object> object,
                 KeyType key,
                 v8::Local<v8::Value> value) {}

template<typename T, typename Enable = void>
struct Converter {};

namespace internal {

ToV8ReturnsMaybe;

}  // namespace internal

template<>
struct GIN_EXPORT Converter<bool> {};

template<>
struct GIN_EXPORT Converter<int32_t> {};

template<>
struct GIN_EXPORT Converter<uint32_t> {};

template<>
struct GIN_EXPORT Converter<int64_t> {};

template<>
struct GIN_EXPORT Converter<uint64_t> {};

template<>
struct GIN_EXPORT Converter<float> {};

template<>
struct GIN_EXPORT Converter<double> {};

template <>
struct GIN_EXPORT Converter<std::string_view> {};

template<>
struct GIN_EXPORT Converter<std::string> {};

template <>
struct GIN_EXPORT Converter<std::u16string> {};

// Converter for C++ TimeTicks to Javascript BigInt (unit: microseconds).
// TimeTicks can't be converted using the existing Converter<int64_t> because
// the target type will be Number and will lose precision.
template <>
struct GIN_EXPORT Converter<base::TimeTicks> {};

template <>
struct GIN_EXPORT Converter<v8::Local<v8::Function>> {};

template<>
struct GIN_EXPORT Converter<v8::Local<v8::Object> > {};

template <>
struct GIN_EXPORT Converter<v8::Local<v8::Promise>> {};

template<>
struct GIN_EXPORT Converter<v8::Local<v8::ArrayBuffer> > {};

template<>
struct GIN_EXPORT Converter<v8::Local<v8::External> > {};

template<>
struct GIN_EXPORT Converter<v8::Local<v8::Value> > {};

Converter<std::vector<T>>;

Converter<v8::LocalVector<T>>;

// Convenience functions that deduce T.
template <typename T>
std::conditional_t<internal::ToV8ReturnsMaybe<T>,
                   v8::MaybeLocal<v8::Value>,
                   v8::Local<v8::Value>>
ConvertToV8(v8::Isolate* isolate, const T& input) {}

template <typename T>
bool TryConvertToV8(v8::Isolate* isolate,
                    const T& input,
                    v8::Local<v8::Value>* output) {}

// This crashes when input.size() > v8::String::kMaxLength.
GIN_EXPORT inline v8::Local<v8::String> StringToV8(
    v8::Isolate* isolate,
    const std::string_view& input) {}

// This crashes when input.size() > v8::String::kMaxLength.
GIN_EXPORT v8::Local<v8::String> StringToSymbol(v8::Isolate* isolate,
                                                const std::string_view& val);

// This crashes when input.size() > v8::String::kMaxLength.
GIN_EXPORT v8::Local<v8::String> StringToSymbol(v8::Isolate* isolate,
                                                const std::u16string_view& val);

GIN_EXPORT base::Location V8ToBaseLocation(const v8::SourceLocation& location);

template<typename T>
bool ConvertFromV8(v8::Isolate* isolate, v8::Local<v8::Value> input,
                   T* result) {}

GIN_EXPORT std::string V8ToString(v8::Isolate* isolate,
                                  v8::Local<v8::Value> value);

}  // namespace gin

#endif  // GIN_CONVERTER_H_