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"
27 template <typename T
, typename U
>
28 bool FromMaybe(Maybe
<T
> maybe
, U
* out
) {
29 if (maybe
.IsNothing())
31 *out
= static_cast<U
>(maybe
.FromJust());
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
,
56 *out
= val
.As
<Int32
>()->Value();
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
,
69 *out
= val
.As
<Uint32
>()->Value();
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
,
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
,
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())
106 *out
= static_cast<float>(val
.As
<Number
>()->Value());
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
,
117 if (!val
->IsNumber())
119 *out
= val
.As
<Number
>()->Value();
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()))
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
,
139 if (!val
->IsString())
141 Local
<String
> str
= Local
<String
>::Cast(val
);
142 int length
= str
->Utf8Length();
144 str
->WriteUtf8(&(*out
)[0], length
, NULL
, String::NO_NULL_TERMINATION
);
148 bool Converter
<Local
<Function
>>::FromV8(Isolate
* isolate
,
150 Local
<Function
>* out
) {
151 if (!val
->IsFunction())
153 *out
= Local
<Function
>::Cast(val
);
157 Local
<Value
> Converter
<Local
<Object
>>::ToV8(Isolate
* isolate
,
159 return val
.As
<Value
>();
162 bool Converter
<Local
<Object
>>::FromV8(Isolate
* isolate
,
164 Local
<Object
>* out
) {
165 if (!val
->IsObject())
167 *out
= Local
<Object
>::Cast(val
);
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
,
178 Local
<ArrayBuffer
>* out
) {
179 if (!val
->IsArrayBuffer())
181 *out
= Local
<ArrayBuffer
>::Cast(val
);
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())
195 *out
= Local
<External
>::Cast(val
);
199 Local
<Value
> Converter
<Local
<Value
>>::ToV8(Isolate
* isolate
, Local
<Value
> val
) {
203 bool Converter
<Local
<Value
>>::FromV8(Isolate
* isolate
,
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()))
218 std::string
V8ToString(v8::Local
<v8::Value
> value
) {
220 return std::string();
222 if (!ConvertFromV8(NULL
, value
, &result
))
223 return std::string();