1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
// 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) {
auto maybe =
object->DefineOwnProperty(isolate->GetCurrentContext(), key, value);
return !maybe.IsNothing() && maybe.FromJust();
}
template<typename T, typename Enable = void>
struct Converter {};
namespace internal {
template <typename T>
concept ToV8ReturnsMaybe = requires(v8::Isolate* isolate, T value) {
{
Converter<T>::ToV8(isolate, value)
} -> std::same_as<v8::MaybeLocal<v8::Value>>;
};
} // namespace internal
template<>
struct GIN_EXPORT Converter<bool> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
bool val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
bool* out);
};
template<>
struct GIN_EXPORT Converter<int32_t> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
int32_t val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
int32_t* out);
};
template<>
struct GIN_EXPORT Converter<uint32_t> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
uint32_t val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
uint32_t* out);
};
template<>
struct GIN_EXPORT Converter<int64_t> {
// Warning: JavaScript cannot represent 64 integers precisely.
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
int64_t val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
int64_t* out);
};
template<>
struct GIN_EXPORT Converter<uint64_t> {
// Warning: JavaScript cannot represent 64 integers precisely.
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
uint64_t val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
uint64_t* out);
};
template<>
struct GIN_EXPORT Converter<float> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
float val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
float* out);
};
template<>
struct GIN_EXPORT Converter<double> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
double val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
double* out);
};
template <>
struct GIN_EXPORT Converter<std::string_view> {
// This crashes when val.size() > v8::String::kMaxLength.
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const std::string_view& val);
// No conversion out is possible because StringPiece does not contain storage.
};
template<>
struct GIN_EXPORT Converter<std::string> {
// This crashes when val.size() > v8::String::kMaxLength.
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const std::string& val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
std::string* out);
};
template <>
struct GIN_EXPORT Converter<std::u16string> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const std::u16string& val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
std::u16string* out);
};
// 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> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, base::TimeTicks val);
};
template <>
struct GIN_EXPORT Converter<v8::Local<v8::Function>> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
v8::Local<v8::Function> val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
v8::Local<v8::Function>* out);
};
template<>
struct GIN_EXPORT Converter<v8::Local<v8::Object> > {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
v8::Local<v8::Object> val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
v8::Local<v8::Object>* out);
};
template <>
struct GIN_EXPORT Converter<v8::Local<v8::Promise>> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
v8::Local<v8::Promise> val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
v8::Local<v8::Promise>* out);
};
template<>
struct GIN_EXPORT Converter<v8::Local<v8::ArrayBuffer> > {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
v8::Local<v8::ArrayBuffer> val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
v8::Local<v8::ArrayBuffer>* out);
};
template<>
struct GIN_EXPORT Converter<v8::Local<v8::External> > {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
v8::Local<v8::External> val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
v8::Local<v8::External>* out);
};
template<>
struct GIN_EXPORT Converter<v8::Local<v8::Value> > {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
v8::Local<v8::Value> val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
v8::Local<v8::Value>* out);
};
template<typename T>
struct Converter<std::vector<T> > {
static std::conditional_t<internal::ToV8ReturnsMaybe<T>,
v8::MaybeLocal<v8::Value>,
v8::Local<v8::Value>>
ToV8(v8::Isolate* isolate, const std::vector<T>& val) {
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::Local<v8::Array> result(
v8::Array::New(isolate, static_cast<int>(val.size())));
for (uint32_t i = 0; i < val.size(); ++i) {
v8::MaybeLocal<v8::Value> maybe = Converter<T>::ToV8(isolate, val[i]);
v8::Local<v8::Value> element;
if (!maybe.ToLocal(&element))
return {};
bool property_created;
if (!result->CreateDataProperty(context, i, element)
.To(&property_created) ||
!property_created) {
NOTREACHED() << "CreateDataProperty should always succeed here.";
}
}
return result;
}
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
std::vector<T>* out) {
if (!val->IsArray())
return false;
std::vector<T> result;
v8::Local<v8::Array> array(v8::Local<v8::Array>::Cast(val));
uint32_t length = array->Length();
for (uint32_t i = 0; i < length; ++i) {
v8::Local<v8::Value> v8_item;
if (!array->Get(isolate->GetCurrentContext(), i).ToLocal(&v8_item))
return false;
T item;
if (!Converter<T>::FromV8(isolate, v8_item, &item))
return false;
result.push_back(std::move(item));
}
out->swap(result);
return true;
}
};
template <typename T>
struct Converter<v8::LocalVector<T>> {
static std::conditional_t<internal::ToV8ReturnsMaybe<v8::Local<T>>,
v8::MaybeLocal<v8::Value>,
v8::Local<v8::Value>>
ToV8(v8::Isolate* isolate, const v8::LocalVector<T>& val) {
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::Local<v8::Array> result(
v8::Array::New(isolate, static_cast<int>(val.size())));
for (uint32_t i = 0; i < val.size(); ++i) {
v8::MaybeLocal<v8::Value> maybe =
Converter<v8::Local<T>>::ToV8(isolate, val[i]);
v8::Local<v8::Value> element;
if (!maybe.ToLocal(&element)) {
return {};
}
bool property_created;
if (!result->CreateDataProperty(context, i, element)
.To(&property_created) ||
!property_created) {
NOTREACHED() << "CreateDataProperty should always succeed here.";
}
}
return result;
}
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
v8::LocalVector<T>* out) {
if (!val->IsArray()) {
return false;
}
v8::LocalVector<T> result(isolate);
v8::Local<v8::Array> array(v8::Local<v8::Array>::Cast(val));
uint32_t length = array->Length();
for (uint32_t i = 0; i < length; ++i) {
v8::Local<v8::Value> v8_item;
if (!array->Get(isolate->GetCurrentContext(), i).ToLocal(&v8_item)) {
return false;
}
v8::Local<T> item;
if (!Converter<v8::Local<T>>::FromV8(isolate, v8_item, &item)) {
return false;
}
result.push_back(item);
}
out->swap(result);
return true;
}
};
// 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) {
return Converter<T>::ToV8(isolate, input);
}
template <typename T>
bool TryConvertToV8(v8::Isolate* isolate,
const T& input,
v8::Local<v8::Value>* output) {
if constexpr (internal::ToV8ReturnsMaybe<T>) {
return ConvertToV8(isolate, input).ToLocal(output);
} else {
*output = ConvertToV8(isolate, input);
return true;
}
}
// This crashes when input.size() > v8::String::kMaxLength.
GIN_EXPORT inline v8::Local<v8::String> StringToV8(
v8::Isolate* isolate,
const std::string_view& input) {
return ConvertToV8(isolate, input).As<v8::String>();
}
// 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) {
DCHECK(isolate);
return Converter<T>::FromV8(isolate, input, result);
}
GIN_EXPORT std::string V8ToString(v8::Isolate* isolate,
v8::Local<v8::Value> value);
} // namespace gin
#endif // GIN_CONVERTER_H_
|