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(val
).As
<Value
>();
27 bool Converter
<bool>::FromV8(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(val
, isolate
).As
<Value
>();
36 bool Converter
<int32_t>::FromV8(Handle
<Value
> val
, int32_t* out
) {
39 *out
= val
->Int32Value();
43 Handle
<Value
> Converter
<uint32_t>::ToV8(Isolate
* isolate
, uint32_t val
) {
44 return Integer::NewFromUnsigned(val
, isolate
).As
<Value
>();
47 bool Converter
<uint32_t>::FromV8(Handle
<Value
> val
, uint32_t* out
) {
50 *out
= val
->Uint32Value();
54 Handle
<Value
> Converter
<int64_t>::ToV8(Isolate
* isolate
, int64_t val
) {
55 return Number::New(isolate
, static_cast<double>(val
)).As
<Value
>();
58 bool Converter
<int64_t>::FromV8(Handle
<Value
> val
, int64_t* out
) {
61 // Even though IntegerValue returns int64_t, JavaScript cannot represent
62 // the full precision of int64_t, which means some rounding might occur.
63 *out
= val
->IntegerValue();
67 Handle
<Value
> Converter
<uint64_t>::ToV8(Isolate
* isolate
, uint64_t val
) {
68 return Number::New(isolate
, static_cast<double>(val
)).As
<Value
>();
71 bool Converter
<uint64_t>::FromV8(Handle
<Value
> val
, uint64_t* out
) {
74 *out
= static_cast<uint64_t>(val
->IntegerValue());
78 Handle
<Value
> Converter
<double>::ToV8(Isolate
* isolate
, double val
) {
79 return Number::New(isolate
, val
).As
<Value
>();
82 bool Converter
<double>::FromV8(Handle
<Value
> val
, double* out
) {
85 *out
= val
->NumberValue();
89 Handle
<Value
> Converter
<std::string
>::ToV8(Isolate
* isolate
,
90 const std::string
& val
) {
91 return String::NewFromUtf8(isolate
,
93 String::kNormalString
,
94 static_cast<uint32_t>(val
.length()));
97 bool Converter
<std::string
>::FromV8(Handle
<Value
> val
,
101 Handle
<String
> str
= Handle
<String
>::Cast(val
);
102 int length
= str
->Utf8Length();
104 str
->WriteUtf8(&(*out
)[0], length
, NULL
, String::NO_NULL_TERMINATION
);
108 bool Converter
<Handle
<Function
> >::FromV8(Handle
<Value
> val
,
109 Handle
<Function
>* out
) {
110 if (!val
->IsFunction())
112 *out
= Handle
<Function
>::Cast(val
);
116 Handle
<Value
> Converter
<Handle
<Object
> >::ToV8(v8::Isolate
* isolate
,
117 Handle
<Object
> val
) {
118 return val
.As
<Value
>();
121 bool Converter
<Handle
<Object
> >::FromV8(Handle
<Value
> val
,
122 Handle
<Object
>* out
) {
123 if (!val
->IsObject())
125 *out
= Handle
<Object
>::Cast(val
);
129 Handle
<Value
> Converter
<Handle
<ArrayBuffer
> >::ToV8(v8::Isolate
* isolate
,
130 Handle
<ArrayBuffer
> val
) {
131 return val
.As
<Value
>();
134 bool Converter
<Handle
<ArrayBuffer
> >::FromV8(Handle
<Value
> val
,
135 Handle
<ArrayBuffer
>* out
) {
136 if (!val
->IsArrayBuffer())
138 *out
= Handle
<ArrayBuffer
>::Cast(val
);
142 Handle
<Value
> Converter
<Handle
<External
> >::ToV8(v8::Isolate
* isolate
,
143 Handle
<External
> val
) {
144 return val
.As
<Value
>();
147 bool Converter
<Handle
<External
> >::FromV8(Handle
<Value
> val
,
148 Handle
<External
>* out
) {
149 if (!val
->IsExternal())
151 *out
= Handle
<External
>::Cast(val
);
155 Handle
<Value
> Converter
<Handle
<Value
> >::ToV8(v8::Isolate
* isolate
,
160 bool Converter
<Handle
<Value
> >::FromV8(Handle
<Value
> val
,
161 Handle
<Value
>* out
) {
166 v8::Handle
<v8::String
> StringToSymbol(v8::Isolate
* isolate
,
167 const std::string
& val
) {
168 return String::NewFromUtf8(isolate
,
170 String::kInternalizedString
,
171 static_cast<uint32_t>(val
.length()));