Revert of [Password Manager] Support <input> elements not in a <form> element (patchs...
[chromium-blink-merge.git] / gin / converter.cc
blob9a3462d5a2dd44b2734ea7731b445f9feb471a64
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.
5 #include "gin/converter.h"
7 #include "v8/include/v8.h"
9 using v8::ArrayBuffer;
10 using v8::Boolean;
11 using v8::External;
12 using v8::Function;
13 using v8::Int32;
14 using v8::Integer;
15 using v8::Isolate;
16 using v8::Local;
17 using v8::Maybe;
18 using v8::MaybeLocal;
19 using v8::Number;
20 using v8::Object;
21 using v8::String;
22 using v8::Uint32;
23 using v8::Value;
25 namespace {
27 template <typename T, typename U>
28 bool FromMaybe(Maybe<T> maybe, U* out) {
29 if (maybe.IsNothing())
30 return false;
31 *out = static_cast<U>(maybe.FromJust());
32 return true;
35 } // namespace
37 namespace gin {
39 Local<Value> Converter<bool>::ToV8(Isolate* isolate, bool val) {
40 return Boolean::New(isolate, val).As<Value>();
43 bool Converter<bool>::FromV8(Isolate* isolate, Local<Value> val, bool* out) {
44 return FromMaybe(val->BooleanValue(isolate->GetCurrentContext()), out);
47 Local<Value> Converter<int32_t>::ToV8(Isolate* isolate, int32_t val) {
48 return Integer::New(isolate, val).As<Value>();
51 bool Converter<int32_t>::FromV8(Isolate* isolate,
52 Local<Value> val,
53 int32_t* out) {
54 if (!val->IsInt32())
55 return false;
56 *out = val.As<Int32>()->Value();
57 return true;
60 Local<Value> Converter<uint32_t>::ToV8(Isolate* isolate, uint32_t val) {
61 return Integer::NewFromUnsigned(isolate, val).As<Value>();
64 bool Converter<uint32_t>::FromV8(Isolate* isolate,
65 Local<Value> val,
66 uint32_t* out) {
67 if (!val->IsUint32())
68 return false;
69 *out = val.As<Uint32>()->Value();
70 return true;
73 Local<Value> Converter<int64_t>::ToV8(Isolate* isolate, int64_t val) {
74 return Number::New(isolate, static_cast<double>(val)).As<Value>();
77 bool Converter<int64_t>::FromV8(Isolate* isolate,
78 Local<Value> val,
79 int64_t* out) {
80 if (!val->IsNumber())
81 return false;
82 // Even though IntegerValue returns int64_t, JavaScript cannot represent
83 // the full precision of int64_t, which means some rounding might occur.
84 return FromMaybe(val->IntegerValue(isolate->GetCurrentContext()), out);
87 Local<Value> Converter<uint64_t>::ToV8(Isolate* isolate, uint64_t val) {
88 return Number::New(isolate, static_cast<double>(val)).As<Value>();
91 bool Converter<uint64_t>::FromV8(Isolate* isolate,
92 Local<Value> val,
93 uint64_t* out) {
94 if (!val->IsNumber())
95 return false;
96 return FromMaybe(val->IntegerValue(isolate->GetCurrentContext()), out);
99 Local<Value> Converter<float>::ToV8(Isolate* isolate, float val) {
100 return Number::New(isolate, val).As<Value>();
103 bool Converter<float>::FromV8(Isolate* isolate, Local<Value> val, float* out) {
104 if (!val->IsNumber())
105 return false;
106 *out = static_cast<float>(val.As<Number>()->Value());
107 return true;
110 Local<Value> Converter<double>::ToV8(Isolate* isolate, double val) {
111 return Number::New(isolate, val).As<Value>();
114 bool Converter<double>::FromV8(Isolate* isolate,
115 Local<Value> val,
116 double* out) {
117 if (!val->IsNumber())
118 return false;
119 *out = val.As<Number>()->Value();
120 return true;
123 Local<Value> Converter<base::StringPiece>::ToV8(Isolate* isolate,
124 const base::StringPiece& val) {
125 return String::NewFromUtf8(isolate, val.data(),
126 v8::NewStringType::kNormal,
127 static_cast<uint32_t>(val.length()))
128 .ToLocalChecked();
131 Local<Value> Converter<std::string>::ToV8(Isolate* isolate,
132 const std::string& val) {
133 return Converter<base::StringPiece>::ToV8(isolate, val);
136 bool Converter<std::string>::FromV8(Isolate* isolate,
137 Local<Value> val,
138 std::string* out) {
139 if (!val->IsString())
140 return false;
141 Local<String> str = Local<String>::Cast(val);
142 int length = str->Utf8Length();
143 out->resize(length);
144 str->WriteUtf8(&(*out)[0], length, NULL, String::NO_NULL_TERMINATION);
145 return true;
148 bool Converter<Local<Function>>::FromV8(Isolate* isolate,
149 Local<Value> val,
150 Local<Function>* out) {
151 if (!val->IsFunction())
152 return false;
153 *out = Local<Function>::Cast(val);
154 return true;
157 Local<Value> Converter<Local<Object>>::ToV8(Isolate* isolate,
158 Local<Object> val) {
159 return val.As<Value>();
162 bool Converter<Local<Object>>::FromV8(Isolate* isolate,
163 Local<Value> val,
164 Local<Object>* out) {
165 if (!val->IsObject())
166 return false;
167 *out = Local<Object>::Cast(val);
168 return true;
171 Local<Value> Converter<Local<ArrayBuffer>>::ToV8(Isolate* isolate,
172 Local<ArrayBuffer> val) {
173 return val.As<Value>();
176 bool Converter<Local<ArrayBuffer>>::FromV8(Isolate* isolate,
177 Local<Value> val,
178 Local<ArrayBuffer>* out) {
179 if (!val->IsArrayBuffer())
180 return false;
181 *out = Local<ArrayBuffer>::Cast(val);
182 return true;
185 Local<Value> Converter<Local<External>>::ToV8(Isolate* isolate,
186 Local<External> val) {
187 return val.As<Value>();
190 bool Converter<Local<External>>::FromV8(Isolate* isolate,
191 v8::Local<Value> val,
192 Local<External>* out) {
193 if (!val->IsExternal())
194 return false;
195 *out = Local<External>::Cast(val);
196 return true;
199 Local<Value> Converter<Local<Value>>::ToV8(Isolate* isolate, Local<Value> val) {
200 return val;
203 bool Converter<Local<Value>>::FromV8(Isolate* isolate,
204 Local<Value> val,
205 Local<Value>* out) {
206 *out = val;
207 return true;
210 v8::Local<v8::String> StringToSymbol(v8::Isolate* isolate,
211 const base::StringPiece& val) {
212 return String::NewFromUtf8(isolate, val.data(),
213 v8::NewStringType::kInternalized,
214 static_cast<uint32_t>(val.length()))
215 .ToLocalChecked();
218 std::string V8ToString(v8::Local<v8::Value> value) {
219 if (value.IsEmpty())
220 return std::string();
221 std::string result;
222 if (!ConvertFromV8(NULL, value, &result))
223 return std::string();
224 return result;
227 } // namespace gin