[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef GIN_CONVERTER_H_ |
| 6 | #define GIN_CONVERTER_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "v8/include/v8.h" |
| 12 | |
| 13 | namespace gin { |
| 14 | |
| 15 | template<typename T> |
| 16 | struct Converter {}; |
| 17 | |
| 18 | template<> |
| 19 | struct Converter<bool> { |
| 20 | static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
| 21 | bool val); |
| 22 | static bool FromV8(v8::Handle<v8::Value> val, |
| 23 | bool* out); |
| 24 | }; |
| 25 | |
| 26 | template<> |
| 27 | struct Converter<int32_t> { |
| 28 | static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
| 29 | int32_t val); |
| 30 | static bool FromV8(v8::Handle<v8::Value> val, |
| 31 | int32_t* out); |
| 32 | }; |
| 33 | |
| 34 | template<> |
| 35 | struct Converter<uint32_t> { |
| 36 | static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
| 37 | uint32_t val); |
| 38 | static bool FromV8(v8::Handle<v8::Value> val, |
| 39 | uint32_t* out); |
| 40 | }; |
| 41 | |
| 42 | template<> |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 43 | struct Converter<int64_t> { |
| 44 | // Warning: JavaScript cannot represent 64 integers precisely. |
| 45 | static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
| 46 | int64_t val); |
| 47 | static bool FromV8(v8::Handle<v8::Value> val, |
| 48 | int64_t* out); |
| 49 | }; |
| 50 | |
| 51 | template<> |
| 52 | struct Converter<uint64_t> { |
| 53 | // Warning: JavaScript cannot represent 64 integers precisely. |
| 54 | static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
| 55 | uint64_t val); |
| 56 | static bool FromV8(v8::Handle<v8::Value> val, |
| 57 | uint64_t* out); |
| 58 | }; |
| 59 | |
| 60 | template<> |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 61 | struct Converter<double> { |
| 62 | static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
| 63 | double val); |
| 64 | static bool FromV8(v8::Handle<v8::Value> val, |
| 65 | double* out); |
| 66 | }; |
| 67 | |
| 68 | template<> |
| 69 | struct Converter<std::string> { |
| 70 | static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
| 71 | const std::string& val); |
| 72 | static bool FromV8(v8::Handle<v8::Value> val, |
| 73 | std::string* out); |
| 74 | }; |
| 75 | |
| 76 | template<> |
| 77 | struct Converter<v8::Handle<v8::Function> > { |
| 78 | static bool FromV8(v8::Handle<v8::Value> val, |
| 79 | v8::Handle<v8::Function>* out); |
| 80 | }; |
| 81 | |
| 82 | template<> |
| 83 | struct Converter<v8::Handle<v8::Object> > { |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 84 | static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
| 85 | v8::Handle<v8::Object> val); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 86 | static bool FromV8(v8::Handle<v8::Value> val, |
| 87 | v8::Handle<v8::Object>* out); |
| 88 | }; |
| 89 | |
[email protected] | 97f21ca | 2013-11-17 17:46:07 | [diff] [blame^] | 90 | template<> |
| 91 | struct Converter<v8::Handle<v8::External> > { |
| 92 | static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
| 93 | v8::Handle<v8::External> val); |
| 94 | static bool FromV8(v8::Handle<v8::Value> val, |
| 95 | v8::Handle<v8::External>* out); |
| 96 | }; |
| 97 | |
| 98 | template<> |
| 99 | struct Converter<v8::Handle<v8::Value> > { |
| 100 | static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
| 101 | v8::Handle<v8::Value> val); |
| 102 | static bool FromV8(v8::Handle<v8::Value> val, |
| 103 | v8::Handle<v8::Value>* out); |
| 104 | }; |
| 105 | |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 106 | template<typename T> |
| 107 | struct Converter<std::vector<T> > { |
| 108 | static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
| 109 | const std::vector<T>& val) { |
| 110 | v8::Handle<v8::Array> result(v8::Array::New(static_cast<int>(val.size()))); |
| 111 | for (size_t i = 0; i < val.size(); ++i) { |
| 112 | result->Set(static_cast<int>(i), Converter<T>::ToV8(isolate, val[i])); |
| 113 | } |
| 114 | return result; |
| 115 | } |
| 116 | |
| 117 | static bool FromV8(v8::Handle<v8::Value> val, |
| 118 | std::vector<T>* out) { |
| 119 | if (!val->IsArray()) |
| 120 | return false; |
| 121 | |
| 122 | std::vector<T> result; |
| 123 | v8::Handle<v8::Array> array(v8::Handle<v8::Array>::Cast(val)); |
| 124 | uint32_t length = array->Length(); |
| 125 | for (uint32_t i = 0; i < length; ++i) { |
| 126 | T item; |
| 127 | if (!Converter<T>::FromV8(array->Get(i), &item)) |
| 128 | return false; |
| 129 | result.push_back(item); |
| 130 | } |
| 131 | |
| 132 | out->swap(result); |
| 133 | return true; |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | // Convenience functions that deduce T. |
| 138 | template<typename T> |
| 139 | v8::Handle<v8::Value> ConvertToV8(v8::Isolate* isolate, |
| 140 | T input) { |
| 141 | return Converter<T>::ToV8(isolate, input); |
| 142 | } |
| 143 | |
| 144 | inline v8::Handle<v8::String> StringToV8( |
| 145 | v8::Isolate* isolate, std::string input) { |
| 146 | return ConvertToV8(isolate, input).As<v8::String>(); |
| 147 | } |
| 148 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 149 | v8::Handle<v8::String> StringToSymbol(v8::Isolate* isolate, |
| 150 | const std::string& val); |
| 151 | |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 152 | template<typename T> |
| 153 | bool ConvertFromV8(v8::Handle<v8::Value> input, T* result) { |
| 154 | return Converter<T>::FromV8(input, result); |
| 155 | } |
| 156 | |
| 157 | } // namespace gin |
| 158 | |
| 159 | #endif // GIN_CONVERTER_H_ |