| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef GIN_CONVERTER_H_ | 5 #ifndef GIN_CONVERTER_H_ |
| 6 #define GIN_CONVERTER_H_ | 6 #define GIN_CONVERTER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/strings/string_piece.h" | 11 #include "base/strings/string_piece.h" |
| 12 #include "gin/gin_export.h" | 12 #include "gin/gin_export.h" |
| 13 #include "v8/include/v8.h" | 13 #include "v8/include/v8.h" |
| 14 | 14 |
| 15 namespace gin { | 15 namespace gin { |
| 16 | 16 |
| 17 template<typename T, typename Enable = void> | 17 template<typename T, typename Enable = void> |
| 18 struct Converter {}; | 18 struct Converter {}; |
| 19 | 19 |
| 20 template<> | 20 template<> |
| 21 struct GIN_EXPORT Converter<bool> { | 21 struct GIN_EXPORT Converter<bool> { |
| 22 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 22 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 23 bool val); | 23 bool val); |
| 24 static bool FromV8(v8::Isolate* isolate, | 24 static bool FromV8(v8::Isolate* isolate, |
| 25 v8::Handle<v8::Value> val, | 25 v8::Local<v8::Value> val, |
| 26 bool* out); | 26 bool* out); |
| 27 }; | 27 }; |
| 28 | 28 |
| 29 template<> | 29 template<> |
| 30 struct GIN_EXPORT Converter<int32_t> { | 30 struct GIN_EXPORT Converter<int32_t> { |
| 31 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 31 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 32 int32_t val); | 32 int32_t val); |
| 33 static bool FromV8(v8::Isolate* isolate, | 33 static bool FromV8(v8::Isolate* isolate, |
| 34 v8::Handle<v8::Value> val, | 34 v8::Local<v8::Value> val, |
| 35 int32_t* out); | 35 int32_t* out); |
| 36 }; | 36 }; |
| 37 | 37 |
| 38 template<> | 38 template<> |
| 39 struct GIN_EXPORT Converter<uint32_t> { | 39 struct GIN_EXPORT Converter<uint32_t> { |
| 40 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 40 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 41 uint32_t val); | 41 uint32_t val); |
| 42 static bool FromV8(v8::Isolate* isolate, | 42 static bool FromV8(v8::Isolate* isolate, |
| 43 v8::Handle<v8::Value> val, | 43 v8::Local<v8::Value> val, |
| 44 uint32_t* out); | 44 uint32_t* out); |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 template<> | 47 template<> |
| 48 struct GIN_EXPORT Converter<int64_t> { | 48 struct GIN_EXPORT Converter<int64_t> { |
| 49 // Warning: JavaScript cannot represent 64 integers precisely. | 49 // Warning: JavaScript cannot represent 64 integers precisely. |
| 50 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 50 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 51 int64_t val); | 51 int64_t val); |
| 52 static bool FromV8(v8::Isolate* isolate, | 52 static bool FromV8(v8::Isolate* isolate, |
| 53 v8::Handle<v8::Value> val, | 53 v8::Local<v8::Value> val, |
| 54 int64_t* out); | 54 int64_t* out); |
| 55 }; | 55 }; |
| 56 | 56 |
| 57 template<> | 57 template<> |
| 58 struct GIN_EXPORT Converter<uint64_t> { | 58 struct GIN_EXPORT Converter<uint64_t> { |
| 59 // Warning: JavaScript cannot represent 64 integers precisely. | 59 // Warning: JavaScript cannot represent 64 integers precisely. |
| 60 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 60 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 61 uint64_t val); | 61 uint64_t val); |
| 62 static bool FromV8(v8::Isolate* isolate, | 62 static bool FromV8(v8::Isolate* isolate, |
| 63 v8::Handle<v8::Value> val, | 63 v8::Local<v8::Value> val, |
| 64 uint64_t* out); | 64 uint64_t* out); |
| 65 }; | 65 }; |
| 66 | 66 |
| 67 template<> | 67 template<> |
| 68 struct GIN_EXPORT Converter<float> { | 68 struct GIN_EXPORT Converter<float> { |
| 69 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 69 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 70 float val); | 70 float val); |
| 71 static bool FromV8(v8::Isolate* isolate, | 71 static bool FromV8(v8::Isolate* isolate, |
| 72 v8::Handle<v8::Value> val, | 72 v8::Local<v8::Value> val, |
| 73 float* out); | 73 float* out); |
| 74 }; | 74 }; |
| 75 | 75 |
| 76 template<> | 76 template<> |
| 77 struct GIN_EXPORT Converter<double> { | 77 struct GIN_EXPORT Converter<double> { |
| 78 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 78 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 79 double val); | 79 double val); |
| 80 static bool FromV8(v8::Isolate* isolate, | 80 static bool FromV8(v8::Isolate* isolate, |
| 81 v8::Handle<v8::Value> val, | 81 v8::Local<v8::Value> val, |
| 82 double* out); | 82 double* out); |
| 83 }; | 83 }; |
| 84 | 84 |
| 85 template<> | 85 template<> |
| 86 struct GIN_EXPORT Converter<base::StringPiece> { | 86 struct GIN_EXPORT Converter<base::StringPiece> { |
| 87 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 87 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 88 const base::StringPiece& val); | 88 const base::StringPiece& val); |
| 89 // No conversion out is possible because StringPiece does not contain storage. | 89 // No conversion out is possible because StringPiece does not contain storage. |
| 90 }; | 90 }; |
| 91 | 91 |
| 92 template<> | 92 template<> |
| 93 struct GIN_EXPORT Converter<std::string> { | 93 struct GIN_EXPORT Converter<std::string> { |
| 94 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 94 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 95 const std::string& val); | 95 const std::string& val); |
| 96 static bool FromV8(v8::Isolate* isolate, | 96 static bool FromV8(v8::Isolate* isolate, |
| 97 v8::Handle<v8::Value> val, | 97 v8::Local<v8::Value> val, |
| 98 std::string* out); | 98 std::string* out); |
| 99 }; | 99 }; |
| 100 | 100 |
| 101 template<> | 101 template<> |
| 102 struct GIN_EXPORT Converter<v8::Handle<v8::Function> > { | 102 struct GIN_EXPORT Converter<v8::Local<v8::Function> > { |
| 103 static bool FromV8(v8::Isolate* isolate, | 103 static bool FromV8(v8::Isolate* isolate, |
| 104 v8::Handle<v8::Value> val, | 104 v8::Local<v8::Value> val, |
| 105 v8::Handle<v8::Function>* out); | 105 v8::Local<v8::Function>* out); |
| 106 }; | 106 }; |
| 107 | 107 |
| 108 template<> | 108 template<> |
| 109 struct GIN_EXPORT Converter<v8::Handle<v8::Object> > { | 109 struct GIN_EXPORT Converter<v8::Local<v8::Object> > { |
| 110 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 110 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 111 v8::Handle<v8::Object> val); | 111 v8::Local<v8::Object> val); |
| 112 static bool FromV8(v8::Isolate* isolate, | 112 static bool FromV8(v8::Isolate* isolate, |
| 113 v8::Handle<v8::Value> val, | 113 v8::Local<v8::Value> val, |
| 114 v8::Handle<v8::Object>* out); | 114 v8::Local<v8::Object>* out); |
| 115 }; | 115 }; |
| 116 | 116 |
| 117 template<> | 117 template<> |
| 118 struct GIN_EXPORT Converter<v8::Handle<v8::ArrayBuffer> > { | 118 struct GIN_EXPORT Converter<v8::Local<v8::ArrayBuffer> > { |
| 119 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 119 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 120 v8::Handle<v8::ArrayBuffer> val); | 120 v8::Local<v8::ArrayBuffer> val); |
| 121 static bool FromV8(v8::Isolate* isolate, | 121 static bool FromV8(v8::Isolate* isolate, |
| 122 v8::Handle<v8::Value> val, | 122 v8::Local<v8::Value> val, |
| 123 v8::Handle<v8::ArrayBuffer>* out); | 123 v8::Local<v8::ArrayBuffer>* out); |
| 124 }; | 124 }; |
| 125 | 125 |
| 126 template<> | 126 template<> |
| 127 struct GIN_EXPORT Converter<v8::Handle<v8::External> > { | 127 struct GIN_EXPORT Converter<v8::Local<v8::External> > { |
| 128 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 128 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 129 v8::Handle<v8::External> val); | 129 v8::Local<v8::External> val); |
| 130 static bool FromV8(v8::Isolate* isolate, | 130 static bool FromV8(v8::Isolate* isolate, |
| 131 v8::Handle<v8::Value> val, | 131 v8::Local<v8::Value> val, |
| 132 v8::Handle<v8::External>* out); | 132 v8::Local<v8::External>* out); |
| 133 }; | 133 }; |
| 134 | 134 |
| 135 template<> | 135 template<> |
| 136 struct GIN_EXPORT Converter<v8::Handle<v8::Value> > { | 136 struct GIN_EXPORT Converter<v8::Local<v8::Value> > { |
| 137 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 137 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 138 v8::Handle<v8::Value> val); | 138 v8::Local<v8::Value> val); |
| 139 static bool FromV8(v8::Isolate* isolate, | 139 static bool FromV8(v8::Isolate* isolate, |
| 140 v8::Handle<v8::Value> val, | 140 v8::Local<v8::Value> val, |
| 141 v8::Handle<v8::Value>* out); | 141 v8::Local<v8::Value>* out); |
| 142 }; | 142 }; |
| 143 | 143 |
| 144 template<typename T> | 144 template<typename T> |
| 145 struct Converter<std::vector<T> > { | 145 struct Converter<std::vector<T> > { |
| 146 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 146 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 147 const std::vector<T>& val) { | 147 const std::vector<T>& val) { |
| 148 v8::Handle<v8::Array> result( | 148 v8::Local<v8::Array> result( |
| 149 v8::Array::New(isolate, static_cast<int>(val.size()))); | 149 v8::Array::New(isolate, static_cast<int>(val.size()))); |
| 150 for (size_t i = 0; i < val.size(); ++i) { | 150 for (size_t i = 0; i < val.size(); ++i) { |
| 151 result->Set(static_cast<int>(i), Converter<T>::ToV8(isolate, val[i])); | 151 result->Set(static_cast<int>(i), Converter<T>::ToV8(isolate, val[i])); |
| 152 } | 152 } |
| 153 return result; | 153 return result; |
| 154 } | 154 } |
| 155 | 155 |
| 156 static bool FromV8(v8::Isolate* isolate, | 156 static bool FromV8(v8::Isolate* isolate, |
| 157 v8::Handle<v8::Value> val, | 157 v8::Local<v8::Value> val, |
| 158 std::vector<T>* out) { | 158 std::vector<T>* out) { |
| 159 if (!val->IsArray()) | 159 if (!val->IsArray()) |
| 160 return false; | 160 return false; |
| 161 | 161 |
| 162 std::vector<T> result; | 162 std::vector<T> result; |
| 163 v8::Handle<v8::Array> array(v8::Handle<v8::Array>::Cast(val)); | 163 v8::Local<v8::Array> array(v8::Local<v8::Array>::Cast(val)); |
| 164 uint32_t length = array->Length(); | 164 uint32_t length = array->Length(); |
| 165 for (uint32_t i = 0; i < length; ++i) { | 165 for (uint32_t i = 0; i < length; ++i) { |
| 166 T item; | 166 T item; |
| 167 if (!Converter<T>::FromV8(isolate, array->Get(i), &item)) | 167 if (!Converter<T>::FromV8(isolate, array->Get(i), &item)) |
| 168 return false; | 168 return false; |
| 169 result.push_back(item); | 169 result.push_back(item); |
| 170 } | 170 } |
| 171 | 171 |
| 172 out->swap(result); | 172 out->swap(result); |
| 173 return true; | 173 return true; |
| 174 } | 174 } |
| 175 }; | 175 }; |
| 176 | 176 |
| 177 // Convenience functions that deduce T. | 177 // Convenience functions that deduce T. |
| 178 template<typename T> | 178 template<typename T> |
| 179 v8::Handle<v8::Value> ConvertToV8(v8::Isolate* isolate, T input) { | 179 v8::Local<v8::Value> ConvertToV8(v8::Isolate* isolate, T input) { |
| 180 return Converter<T>::ToV8(isolate, input); | 180 return Converter<T>::ToV8(isolate, input); |
| 181 } | 181 } |
| 182 | 182 |
| 183 GIN_EXPORT inline v8::Handle<v8::String> StringToV8( | 183 GIN_EXPORT inline v8::Local<v8::String> StringToV8( |
| 184 v8::Isolate* isolate, | 184 v8::Isolate* isolate, |
| 185 const base::StringPiece& input) { | 185 const base::StringPiece& input) { |
| 186 return ConvertToV8(isolate, input).As<v8::String>(); | 186 return ConvertToV8(isolate, input).As<v8::String>(); |
| 187 } | 187 } |
| 188 | 188 |
| 189 GIN_EXPORT v8::Handle<v8::String> StringToSymbol(v8::Isolate* isolate, | 189 GIN_EXPORT v8::Local<v8::String> StringToSymbol(v8::Isolate* isolate, |
| 190 const base::StringPiece& val); | 190 const base::StringPiece& val); |
| 191 | 191 |
| 192 template<typename T> | 192 template<typename T> |
| 193 bool ConvertFromV8(v8::Isolate* isolate, v8::Handle<v8::Value> input, | 193 bool ConvertFromV8(v8::Isolate* isolate, v8::Local<v8::Value> input, |
| 194 T* result) { | 194 T* result) { |
| 195 return Converter<T>::FromV8(isolate, input, result); | 195 return Converter<T>::FromV8(isolate, input, result); |
| 196 } | 196 } |
| 197 | 197 |
| 198 GIN_EXPORT std::string V8ToString(v8::Handle<v8::Value> value); | 198 GIN_EXPORT std::string V8ToString(v8::Local<v8::Value> value); |
| 199 | 199 |
| 200 } // namespace gin | 200 } // namespace gin |
| 201 | 201 |
| 202 #endif // GIN_CONVERTER_H_ | 202 #endif // GIN_CONVERTER_H_ |
| OLD | NEW |