native-mate/native_mate/converter.h

365 строки
11 KiB
C
Исходник Обычный вид История

2014-04-15 07:04:36 +04:00
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE.chromium file.
#ifndef NATIVE_MATE_CONVERTER_H_
#define NATIVE_MATE_CONVERTER_H_
2016-06-13 03:26:04 +03:00
#include <map>
2014-04-15 07:04:36 +04:00
#include <string>
#include <vector>
2015-04-17 23:03:54 +03:00
#include <set>
2014-04-15 07:04:36 +04:00
#include "base/strings/string_piece.h"
#include "v8/include/v8.h"
namespace mate {
template<typename KeyType>
bool SetProperty(v8::Isolate* isolate,
v8::Local<v8::Object> object,
KeyType key,
v8::Local<v8::Value> value) {
auto maybe = object->Set(isolate->GetCurrentContext(), key, value);
return !maybe.IsNothing() && maybe.FromJust();
}
template<typename T>
struct ToV8ReturnsMaybe {
static const bool value = false;
};
2014-04-15 07:04:36 +04:00
template<typename T, typename Enable = void>
struct Converter {};
2014-04-16 10:56:33 +04:00
template<>
struct Converter<void*> {
2015-05-22 14:11:02 +03:00
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, void* val) {
2018-05-18 00:36:15 +03:00
return v8::Undefined(isolate);
2014-04-16 10:56:33 +04:00
}
};
2017-08-15 18:39:11 +03:00
template<>
struct Converter<std::nullptr_t> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, std::nullptr_t val) {
return v8::Null(isolate);
}
};
2014-04-15 07:04:36 +04:00
template<>
struct Converter<bool> {
2015-05-22 14:11:02 +03:00
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
2014-04-15 07:04:36 +04:00
bool val);
static bool FromV8(v8::Isolate* isolate,
2015-05-22 14:11:02 +03:00
v8::Local<v8::Value> val,
2014-04-15 07:04:36 +04:00
bool* out);
};
2018-03-22 21:14:26 +03:00
#if !defined(OS_LINUX) && !defined(OS_FREEBSD)
2015-03-11 02:08:30 +03:00
template<>
struct Converter<unsigned long> {
2015-05-22 14:11:02 +03:00
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
2015-03-11 02:08:30 +03:00
unsigned long val);
static bool FromV8(v8::Isolate* isolate,
2015-05-22 14:11:02 +03:00
v8::Local<v8::Value> val,
2015-03-11 02:08:30 +03:00
unsigned long* out);
};
2015-03-14 21:47:08 +03:00
#endif
2015-03-11 02:08:30 +03:00
2014-04-15 07:04:36 +04:00
template<>
struct Converter<int32_t> {
2015-05-22 14:11:02 +03:00
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
2014-04-15 07:04:36 +04:00
int32_t val);
static bool FromV8(v8::Isolate* isolate,
2015-05-22 14:11:02 +03:00
v8::Local<v8::Value> val,
2014-04-15 07:04:36 +04:00
int32_t* out);
};
template<>
struct Converter<uint32_t> {
2015-05-22 14:11:02 +03:00
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
2014-04-15 07:04:36 +04:00
uint32_t val);
static bool FromV8(v8::Isolate* isolate,
2015-05-22 14:11:02 +03:00
v8::Local<v8::Value> val,
2014-04-15 07:04:36 +04:00
uint32_t* out);
};
template<>
struct Converter<int64_t> {
// Warning: JavaScript cannot represent 64 integers precisely.
2015-05-22 14:11:02 +03:00
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
2014-04-15 07:04:36 +04:00
int64_t val);
static bool FromV8(v8::Isolate* isolate,
2015-05-22 14:11:02 +03:00
v8::Local<v8::Value> val,
2014-04-15 07:04:36 +04:00
int64_t* out);
};
template<>
struct Converter<uint64_t> {
// Warning: JavaScript cannot represent 64 integers precisely.
2015-05-22 14:11:02 +03:00
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
2014-04-15 07:04:36 +04:00
uint64_t val);
static bool FromV8(v8::Isolate* isolate,
2015-05-22 14:11:02 +03:00
v8::Local<v8::Value> val,
2014-04-15 07:04:36 +04:00
uint64_t* out);
};
template<>
struct Converter<float> {
2015-05-22 14:11:02 +03:00
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
2014-04-15 07:04:36 +04:00
float val);
static bool FromV8(v8::Isolate* isolate,
2015-05-22 14:11:02 +03:00
v8::Local<v8::Value> val,
2014-04-15 07:04:36 +04:00
float* out);
};
template<>
struct Converter<double> {
2015-05-22 14:11:02 +03:00
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
2014-04-15 07:04:36 +04:00
double val);
static bool FromV8(v8::Isolate* isolate,
2015-05-22 14:11:02 +03:00
v8::Local<v8::Value> val,
2014-04-15 07:04:36 +04:00
double* out);
};
2014-06-23 17:24:18 +04:00
template<>
struct Converter<const char*> {
2015-05-22 14:11:02 +03:00
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, const char* val);
2014-06-23 17:24:18 +04:00
};
2014-04-15 07:04:36 +04:00
template<>
struct Converter<base::StringPiece> {
2015-05-22 14:11:02 +03:00
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
2014-04-15 07:04:36 +04:00
const base::StringPiece& val);
// No conversion out is possible because StringPiece does not contain storage.
};
template<>
struct Converter<std::string> {
2015-05-22 14:11:02 +03:00
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
2014-04-15 07:04:36 +04:00
const std::string& val);
static bool FromV8(v8::Isolate* isolate,
2015-05-22 14:11:02 +03:00
v8::Local<v8::Value> val,
2014-04-15 07:04:36 +04:00
std::string* out);
};
2016-06-13 03:26:04 +03:00
v8::Local<v8::String> StringToSymbol(v8::Isolate* isolate,
const base::StringPiece& input);
std::string V8ToString(v8::Local<v8::Value> value);
2014-04-15 07:04:36 +04:00
template<>
2015-05-22 14:11:02 +03:00
struct Converter<v8::Local<v8::Function> > {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
v8::Local<v8::Function> val);
2014-04-15 07:04:36 +04:00
static bool FromV8(v8::Isolate* isolate,
2015-05-22 14:11:02 +03:00
v8::Local<v8::Value> val,
v8::Local<v8::Function>* out);
2014-04-15 07:04:36 +04:00
};
template<>
2015-05-22 14:11:02 +03:00
struct Converter<v8::Local<v8::Object> > {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
v8::Local<v8::Object> val);
2014-04-15 07:04:36 +04:00
static bool FromV8(v8::Isolate* isolate,
2015-05-22 14:11:02 +03:00
v8::Local<v8::Value> val,
v8::Local<v8::Object>* out);
2014-04-15 07:04:36 +04:00
};
template<>
2015-05-22 14:11:02 +03:00
struct Converter<v8::Local<v8::String> > {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
v8::Local<v8::String> val);
static bool FromV8(v8::Isolate* isolate,
2015-05-22 14:11:02 +03:00
v8::Local<v8::Value> val,
v8::Local<v8::String>* out);
};
2014-04-15 07:04:36 +04:00
template<>
2015-05-22 14:11:02 +03:00
struct Converter<v8::Local<v8::External> > {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
v8::Local<v8::External> val);
2014-04-15 07:04:36 +04:00
static bool FromV8(v8::Isolate* isolate,
2015-05-22 14:11:02 +03:00
v8::Local<v8::Value> val,
v8::Local<v8::External>* out);
2014-04-15 07:04:36 +04:00
};
2015-06-10 06:14:42 +03:00
template<>
struct Converter<v8::Local<v8::Array> > {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
v8::Local<v8::Array> val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
v8::Local<v8::Array>* out);
};
2014-04-15 07:04:36 +04:00
template<>
2015-05-22 14:11:02 +03:00
struct Converter<v8::Local<v8::Value> > {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
v8::Local<v8::Value> val);
2014-04-15 07:04:36 +04:00
static bool FromV8(v8::Isolate* isolate,
2015-05-22 14:11:02 +03:00
v8::Local<v8::Value> val,
v8::Local<v8::Value>* out);
2014-04-15 07:04:36 +04:00
};
template<typename T>
struct Converter<std::vector<T> > {
2015-05-22 14:11:02 +03:00
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
2014-04-15 07:04:36 +04:00
const std::vector<T>& val) {
2015-05-22 14:11:02 +03:00
v8::Local<v8::Array> result(
2018-05-18 00:36:15 +03:00
v8::Array::New(isolate, static_cast<int>(val.size())));
2014-04-15 07:04:36 +04:00
for (size_t i = 0; i < val.size(); ++i) {
result->Set(static_cast<int>(i), Converter<T>::ToV8(isolate, val[i]));
}
return result;
}
static bool FromV8(v8::Isolate* isolate,
2015-05-22 14:11:02 +03:00
v8::Local<v8::Value> val,
2014-04-15 07:04:36 +04:00
std::vector<T>* out) {
if (!val->IsArray())
return false;
std::vector<T> result;
2015-05-22 14:11:02 +03:00
v8::Local<v8::Array> array(v8::Local<v8::Array>::Cast(val));
2014-04-15 07:04:36 +04:00
uint32_t length = array->Length();
for (uint32_t i = 0; i < length; ++i) {
T item;
if (!Converter<T>::FromV8(isolate, array->Get(i), &item))
return false;
result.push_back(item);
}
out->swap(result);
2015-04-17 23:03:54 +03:00
return true;
}
};
template<typename T>
struct Converter<std::set<T> > {
2015-05-22 14:11:02 +03:00
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
2015-04-17 23:03:54 +03:00
const std::set<T>& val) {
2015-05-22 14:11:02 +03:00
v8::Local<v8::Array> result(
2018-05-18 00:36:15 +03:00
v8::Array::New(isolate, static_cast<int>(val.size())));
2015-04-17 23:03:54 +03:00
typename std::set<T>::const_iterator it;
int i;
for (i = 0, it = val.begin(); it != val.end(); ++it, ++i)
2015-12-11 16:13:33 +03:00
result->Set(i, Converter<T>::ToV8(isolate, *it));
2015-04-17 23:03:54 +03:00
return result;
}
static bool FromV8(v8::Isolate* isolate,
2015-05-22 14:11:02 +03:00
v8::Local<v8::Value> val,
2015-04-17 23:03:54 +03:00
std::set<T>* out) {
if (!val->IsArray())
return false;
std::set<T> result;
2015-05-22 14:11:02 +03:00
v8::Local<v8::Array> array(v8::Local<v8::Array>::Cast(val));
2015-04-17 23:03:54 +03:00
uint32_t length = array->Length();
for (uint32_t i = 0; i < length; ++i) {
T item;
if (!Converter<T>::FromV8(isolate, array->Get(i), &item))
return false;
2015-12-11 16:13:33 +03:00
result.insert(item);
2015-04-17 23:03:54 +03:00
}
out->swap(result);
2014-04-15 07:04:36 +04:00
return true;
}
};
2016-06-13 03:26:04 +03:00
template<typename T>
struct Converter<std::map<std::string, T> > {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
std::map<std::string, T> * out) {
if (!val->IsObject())
return false;
v8::Local<v8::Object> dict = val->ToObject();
v8::Local<v8::Array> keys = dict->GetOwnPropertyNames();
for (uint32_t i = 0; i < keys->Length(); ++i) {
v8::Local<v8::Value> key = keys->Get(i);
T value;
if (Converter<T>::FromV8(isolate, dict->Get(key), &value))
(*out)[V8ToString(key)] = std::move(value);
}
return true;
}
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const std::map<std::string, T>& val) {
v8::Local<v8::Object> result = v8::Object::New(isolate);
for (auto i = val.begin(); i != val.end(); i++) {
result->Set(Converter<T>::ToV8(isolate, i->first),
Converter<T>::ToV8(isolate, i->second));
}
return result;
}
2016-06-13 03:26:04 +03:00
};
2014-04-15 07:04:36 +04:00
// Convenience functions that deduce T.
template<typename T>
v8::Local<v8::Value> ConvertToV8(v8::Isolate* isolate, const T& input) {
2014-04-15 07:04:36 +04:00
return Converter<T>::ToV8(isolate, input);
}
2015-05-22 14:11:02 +03:00
inline v8::Local<v8::Value> ConvertToV8(v8::Isolate* isolate,
const char* input) {
2015-01-15 04:51:07 +03:00
return Converter<const char*>::ToV8(isolate, input);
}
template<typename T>
v8::MaybeLocal<v8::Value> ConvertToV8(v8::Local<v8::Context> context,
const T& input) {
return Converter<T>::ToV8(context, input);
}
template<typename T, bool = ToV8ReturnsMaybe<T>::value> struct ToV8Traits;
template <typename T>
struct ToV8Traits<T, true> {
static bool TryConvertToV8(v8::Isolate* isolate,
const T& input,
v8::Local<v8::Value>* output) {
auto maybe = ConvertToV8(isolate->GetCurrentContext(), input);
if (maybe.IsEmpty())
return false;
*output = maybe.ToLocalChecked();
return true;
}
};
template <typename T>
struct ToV8Traits<T, false> {
static bool TryConvertToV8(v8::Isolate* isolate,
const T& input,
v8::Local<v8::Value>* output) {
*output = ConvertToV8(isolate, input);
return true;
}
};
template <typename T>
bool TryConvertToV8(v8::Isolate* isolate,
2018-04-26 01:18:27 +03:00
const T& input,
v8::Local<v8::Value>* output) {
return ToV8Traits<T>::TryConvertToV8(isolate, input, output);
}
2014-04-15 07:04:36 +04:00
template<typename T>
2015-05-22 14:11:02 +03:00
bool ConvertFromV8(v8::Isolate* isolate, v8::Local<v8::Value> input,
2014-04-15 07:04:36 +04:00
T* result) {
return Converter<T>::FromV8(isolate, input, result);
}
2016-06-13 03:26:04 +03:00
inline v8::Local<v8::String> StringToV8(
v8::Isolate* isolate,
const base::StringPiece& input) {
return ConvertToV8(isolate, input).As<v8::String>();
}
2014-04-15 07:04:36 +04:00
} // namespace mate
#endif // NATIVE_MATE_CONVERTER_H_