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"
23 Handle
<Value
> Converter
<bool>::ToV8(Isolate
* isolate
, bool val
) {
24 return Boolean::New(isolate
, val
).As
<Value
>();
27 bool Converter
<bool>::FromV8(Isolate
* isolate
, Handle
<Value
> val
, bool* out
) {
28 *out
= val
->BooleanValue();
32 Handle
<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
, Handle
<Value
> val
,
40 *out
= val
->Int32Value();
44 Handle
<Value
> Converter
<uint32_t>::ToV8(Isolate
* isolate
, uint32_t val
) {
45 return Integer::NewFromUnsigned(isolate
, val
).As
<Value
>();
48 bool Converter
<uint32_t>::FromV8(Isolate
* isolate
, Handle
<Value
> val
,
52 *out
= val
->Uint32Value();
56 Handle
<Value
> Converter
<int64_t>::ToV8(Isolate
* isolate
, int64_t val
) {
57 return Number::New(isolate
, static_cast<double>(val
)).As
<Value
>();
60 bool Converter
<int64_t>::FromV8(Isolate
* isolate
, Handle
<Value
> val
,
64 // Even though IntegerValue returns int64_t, JavaScript cannot represent
65 // the full precision of int64_t, which means some rounding might occur.
66 *out
= val
->IntegerValue();
70 Handle
<Value
> Converter
<uint64_t>::ToV8(Isolate
* isolate
, uint64_t val
) {
71 return Number::New(isolate
, static_cast<double>(val
)).As
<Value
>();
74 bool Converter
<uint64_t>::FromV8(Isolate
* isolate
, Handle
<Value
> val
,
78 *out
= static_cast<uint64_t>(val
->IntegerValue());
82 Handle
<Value
> Converter
<double>::ToV8(Isolate
* isolate
, double val
) {
83 return Number::New(isolate
, val
).As
<Value
>();
86 bool Converter
<double>::FromV8(Isolate
* isolate
, Handle
<Value
> val
,
90 *out
= val
->NumberValue();
94 Handle
<Value
> Converter
<base::StringPiece
>::ToV8(
95 Isolate
* isolate
, const base::StringPiece
& val
) {
96 return String::NewFromUtf8(isolate
, val
.data(), String::kNormalString
,
97 static_cast<uint32_t>(val
.length()));
100 Handle
<Value
> Converter
<std::string
>::ToV8(Isolate
* isolate
,
101 const std::string
& val
) {
102 return Converter
<base::StringPiece
>::ToV8(isolate
, val
);
105 bool Converter
<std::string
>::FromV8(Isolate
* isolate
, Handle
<Value
> val
,
107 if (!val
->IsString())
109 Handle
<String
> str
= Handle
<String
>::Cast(val
);
110 int length
= str
->Utf8Length();
112 str
->WriteUtf8(&(*out
)[0], length
, NULL
, String::NO_NULL_TERMINATION
);
116 bool Converter
<Handle
<Function
> >::FromV8(Isolate
* isolate
, Handle
<Value
> val
,
117 Handle
<Function
>* out
) {
118 if (!val
->IsFunction())
120 *out
= Handle
<Function
>::Cast(val
);
124 Handle
<Value
> Converter
<Handle
<Object
> >::ToV8(Isolate
* isolate
,
125 Handle
<Object
> val
) {
126 return val
.As
<Value
>();
129 bool Converter
<Handle
<Object
> >::FromV8(Isolate
* isolate
, Handle
<Value
> val
,
130 Handle
<Object
>* out
) {
131 if (!val
->IsObject())
133 *out
= Handle
<Object
>::Cast(val
);
137 Handle
<Value
> Converter
<Handle
<ArrayBuffer
> >::ToV8(Isolate
* isolate
,
138 Handle
<ArrayBuffer
> val
) {
139 return val
.As
<Value
>();
142 bool Converter
<Handle
<ArrayBuffer
> >::FromV8(Isolate
* isolate
,
144 Handle
<ArrayBuffer
>* out
) {
145 if (!val
->IsArrayBuffer())
147 *out
= Handle
<ArrayBuffer
>::Cast(val
);
151 Handle
<Value
> Converter
<Handle
<External
> >::ToV8(Isolate
* isolate
,
152 Handle
<External
> val
) {
153 return val
.As
<Value
>();
156 bool Converter
<Handle
<External
> >::FromV8(Isolate
* isolate
,
157 v8::Handle
<Value
> val
,
158 Handle
<External
>* out
) {
159 if (!val
->IsExternal())
161 *out
= Handle
<External
>::Cast(val
);
165 Handle
<Value
> Converter
<Handle
<Value
> >::ToV8(Isolate
* isolate
,
170 bool Converter
<Handle
<Value
> >::FromV8(Isolate
* isolate
, Handle
<Value
> val
,
171 Handle
<Value
>* out
) {
176 v8::Handle
<v8::String
> StringToSymbol(v8::Isolate
* isolate
,
177 const base::StringPiece
& val
) {
178 return String::NewFromUtf8(isolate
,
180 String::kInternalizedString
,
181 static_cast<uint32_t>(val
.length()));
184 std::string
V8ToString(v8::Handle
<v8::Value
> value
) {
186 return std::string();
188 if (!ConvertFromV8(NULL
, value
, &result
))
189 return std::string();