Remove hashimoto@chromium.org from file_manager/webstore_widget_private OWNERS
[chromium-blink-merge.git] / gin / converter.cc
blobd870bebc4bb816e49612b15df3039c43286b4998
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::Integer;
14 using v8::Isolate;
15 using v8::Local;
16 using v8::Number;
17 using v8::Object;
18 using v8::String;
19 using v8::Value;
21 namespace gin {
23 Local<Value> Converter<bool>::ToV8(Isolate* isolate, bool val) {
24 return Boolean::New(isolate, val).As<Value>();
27 bool Converter<bool>::FromV8(Isolate* isolate, Local<Value> val, bool* out) {
28 *out = val->BooleanValue();
29 return true;
32 Local<Value> Converter<int32_t>::ToV8(Isolate* isolate, int32_t val) {
33 return Integer::New(isolate, val).As<Value>();
36 bool Converter<int32_t>::FromV8(Isolate* isolate,
37 Local<Value> val,
38 int32_t* out) {
39 if (!val->IsInt32())
40 return false;
41 *out = val->Int32Value();
42 return true;
45 Local<Value> Converter<uint32_t>::ToV8(Isolate* isolate, uint32_t val) {
46 return Integer::NewFromUnsigned(isolate, val).As<Value>();
49 bool Converter<uint32_t>::FromV8(Isolate* isolate,
50 Local<Value> val,
51 uint32_t* out) {
52 if (!val->IsUint32())
53 return false;
54 *out = val->Uint32Value();
55 return true;
58 Local<Value> Converter<int64_t>::ToV8(Isolate* isolate, int64_t val) {
59 return Number::New(isolate, static_cast<double>(val)).As<Value>();
62 bool Converter<int64_t>::FromV8(Isolate* isolate,
63 Local<Value> val,
64 int64_t* out) {
65 if (!val->IsNumber())
66 return false;
67 // Even though IntegerValue returns int64_t, JavaScript cannot represent
68 // the full precision of int64_t, which means some rounding might occur.
69 *out = val->IntegerValue();
70 return true;
73 Local<Value> Converter<uint64_t>::ToV8(Isolate* isolate, uint64_t val) {
74 return Number::New(isolate, static_cast<double>(val)).As<Value>();
77 bool Converter<uint64_t>::FromV8(Isolate* isolate,
78 Local<Value> val,
79 uint64_t* out) {
80 if (!val->IsNumber())
81 return false;
82 *out = static_cast<uint64_t>(val->IntegerValue());
83 return true;
86 Local<Value> Converter<float>::ToV8(Isolate* isolate, float val) {
87 return Number::New(isolate, val).As<Value>();
90 bool Converter<float>::FromV8(Isolate* isolate, Local<Value> val, float* out) {
91 if (!val->IsNumber())
92 return false;
93 *out = static_cast<float>(val->NumberValue());
94 return true;
97 Local<Value> Converter<double>::ToV8(Isolate* isolate, double val) {
98 return Number::New(isolate, val).As<Value>();
101 bool Converter<double>::FromV8(Isolate* isolate,
102 Local<Value> val,
103 double* out) {
104 if (!val->IsNumber())
105 return false;
106 *out = val->NumberValue();
107 return true;
110 Local<Value> Converter<base::StringPiece>::ToV8(Isolate* isolate,
111 const base::StringPiece& val) {
112 return String::NewFromUtf8(isolate, val.data(), String::kNormalString,
113 static_cast<uint32_t>(val.length()));
116 Local<Value> Converter<std::string>::ToV8(Isolate* isolate,
117 const std::string& val) {
118 return Converter<base::StringPiece>::ToV8(isolate, val);
121 bool Converter<std::string>::FromV8(Isolate* isolate,
122 Local<Value> val,
123 std::string* out) {
124 if (!val->IsString())
125 return false;
126 Local<String> str = Local<String>::Cast(val);
127 int length = str->Utf8Length();
128 out->resize(length);
129 str->WriteUtf8(&(*out)[0], length, NULL, String::NO_NULL_TERMINATION);
130 return true;
133 bool Converter<Local<Function>>::FromV8(Isolate* isolate,
134 Local<Value> val,
135 Local<Function>* out) {
136 if (!val->IsFunction())
137 return false;
138 *out = Local<Function>::Cast(val);
139 return true;
142 Local<Value> Converter<Local<Object>>::ToV8(Isolate* isolate,
143 Local<Object> val) {
144 return val.As<Value>();
147 bool Converter<Local<Object>>::FromV8(Isolate* isolate,
148 Local<Value> val,
149 Local<Object>* out) {
150 if (!val->IsObject())
151 return false;
152 *out = Local<Object>::Cast(val);
153 return true;
156 Local<Value> Converter<Local<ArrayBuffer>>::ToV8(Isolate* isolate,
157 Local<ArrayBuffer> val) {
158 return val.As<Value>();
161 bool Converter<Local<ArrayBuffer>>::FromV8(Isolate* isolate,
162 Local<Value> val,
163 Local<ArrayBuffer>* out) {
164 if (!val->IsArrayBuffer())
165 return false;
166 *out = Local<ArrayBuffer>::Cast(val);
167 return true;
170 Local<Value> Converter<Local<External>>::ToV8(Isolate* isolate,
171 Local<External> val) {
172 return val.As<Value>();
175 bool Converter<Local<External>>::FromV8(Isolate* isolate,
176 v8::Local<Value> val,
177 Local<External>* out) {
178 if (!val->IsExternal())
179 return false;
180 *out = Local<External>::Cast(val);
181 return true;
184 Local<Value> Converter<Local<Value>>::ToV8(Isolate* isolate, Local<Value> val) {
185 return val;
188 bool Converter<Local<Value>>::FromV8(Isolate* isolate,
189 Local<Value> val,
190 Local<Value>* out) {
191 *out = val;
192 return true;
195 v8::Local<v8::String> StringToSymbol(v8::Isolate* isolate,
196 const base::StringPiece& val) {
197 return String::NewFromUtf8(isolate,
198 val.data(),
199 String::kInternalizedString,
200 static_cast<uint32_t>(val.length()));
203 std::string V8ToString(v8::Local<v8::Value> value) {
204 if (value.IsEmpty())
205 return std::string();
206 std::string result;
207 if (!ConvertFromV8(NULL, value, &result))
208 return std::string();
209 return result;
212 } // namespace gin