// 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 file. #ifndef GIN_CONVERTER_H_ #define GIN_CONVERTER_H_ #include #include #include "base/strings/string_piece.h" #include "gin/gin_export.h" #include "v8/include/v8.h" namespace gin { template struct Converter {}; template<> struct GIN_EXPORT Converter { static v8::Handle ToV8(v8::Isolate* isolate, bool val); static bool FromV8(v8::Isolate* isolate, v8::Handle val, bool* out); }; template<> struct GIN_EXPORT Converter { static v8::Handle ToV8(v8::Isolate* isolate, int32_t val); static bool FromV8(v8::Isolate* isolate, v8::Handle val, int32_t* out); }; template<> struct GIN_EXPORT Converter { static v8::Handle ToV8(v8::Isolate* isolate, uint32_t val); static bool FromV8(v8::Isolate* isolate, v8::Handle val, uint32_t* out); }; template<> struct GIN_EXPORT Converter { // Warning: JavaScript cannot represent 64 integers precisely. static v8::Handle ToV8(v8::Isolate* isolate, int64_t val); static bool FromV8(v8::Isolate* isolate, v8::Handle val, int64_t* out); }; template<> struct GIN_EXPORT Converter { // Warning: JavaScript cannot represent 64 integers precisely. static v8::Handle ToV8(v8::Isolate* isolate, uint64_t val); static bool FromV8(v8::Isolate* isolate, v8::Handle val, uint64_t* out); }; template<> struct GIN_EXPORT Converter { static v8::Handle ToV8(v8::Isolate* isolate, double val); static bool FromV8(v8::Isolate* isolate, v8::Handle val, double* out); }; template<> struct GIN_EXPORT Converter { static v8::Handle ToV8(v8::Isolate* isolate, const base::StringPiece& val); // No conversion out is possible because StringPiece does not contain storage. }; template<> struct GIN_EXPORT Converter { static v8::Handle ToV8(v8::Isolate* isolate, const std::string& val); static bool FromV8(v8::Isolate* isolate, v8::Handle val, std::string* out); }; template<> struct GIN_EXPORT Converter > { static bool FromV8(v8::Isolate* isolate, v8::Handle val, v8::Handle* out); }; template<> struct GIN_EXPORT Converter > { static v8::Handle ToV8(v8::Isolate* isolate, v8::Handle val); static bool FromV8(v8::Isolate* isolate, v8::Handle val, v8::Handle* out); }; template<> struct GIN_EXPORT Converter > { static v8::Handle ToV8(v8::Isolate* isolate, v8::Handle val); static bool FromV8(v8::Isolate* isolate, v8::Handle val, v8::Handle* out); }; template<> struct GIN_EXPORT Converter > { static v8::Handle ToV8(v8::Isolate* isolate, v8::Handle val); static bool FromV8(v8::Isolate* isolate, v8::Handle val, v8::Handle* out); }; template<> struct GIN_EXPORT Converter > { static v8::Handle ToV8(v8::Isolate* isolate, v8::Handle val); static bool FromV8(v8::Isolate* isolate, v8::Handle val, v8::Handle* out); }; template struct Converter > { static v8::Handle ToV8(v8::Isolate* isolate, const std::vector& val) { v8::Handle result( v8::Array::New(isolate, static_cast(val.size()))); for (size_t i = 0; i < val.size(); ++i) { result->Set(static_cast(i), Converter::ToV8(isolate, val[i])); } return result; } static bool FromV8(v8::Isolate* isolate, v8::Handle val, std::vector* out) { if (!val->IsArray()) return false; std::vector result; v8::Handle array(v8::Handle::Cast(val)); uint32_t length = array->Length(); for (uint32_t i = 0; i < length; ++i) { T item; if (!Converter::FromV8(isolate, array->Get(i), &item)) return false; result.push_back(item); } out->swap(result); return true; } }; // Convenience functions that deduce T. template v8::Handle ConvertToV8(v8::Isolate* isolate, T input) { return Converter::ToV8(isolate, input); } GIN_EXPORT inline v8::Handle StringToV8( v8::Isolate* isolate, const base::StringPiece& input) { return ConvertToV8(isolate, input).As(); } GIN_EXPORT v8::Handle StringToSymbol(v8::Isolate* isolate, const base::StringPiece& val); template bool ConvertFromV8(v8::Isolate* isolate, v8::Handle input, T* result) { return Converter::FromV8(isolate, input, result); } GIN_EXPORT std::string V8ToString(v8::Handle value); } // namespace gin #endif // GIN_CONVERTER_H_