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 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();
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
,
41 *out
= val
->Int32Value();
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
,
54 *out
= val
->Uint32Value();
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
,
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();
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
,
82 *out
= static_cast<uint64_t>(val
->IntegerValue());
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
) {
93 *out
= static_cast<float>(val
->NumberValue());
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
,
104 if (!val
->IsNumber())
106 *out
= val
->NumberValue();
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
,
124 if (!val
->IsString())
126 Local
<String
> str
= Local
<String
>::Cast(val
);
127 int length
= str
->Utf8Length();
129 str
->WriteUtf8(&(*out
)[0], length
, NULL
, String::NO_NULL_TERMINATION
);
133 bool Converter
<Local
<Function
>>::FromV8(Isolate
* isolate
,
135 Local
<Function
>* out
) {
136 if (!val
->IsFunction())
138 *out
= Local
<Function
>::Cast(val
);
142 Local
<Value
> Converter
<Local
<Object
>>::ToV8(Isolate
* isolate
,
144 return val
.As
<Value
>();
147 bool Converter
<Local
<Object
>>::FromV8(Isolate
* isolate
,
149 Local
<Object
>* out
) {
150 if (!val
->IsObject())
152 *out
= Local
<Object
>::Cast(val
);
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
,
163 Local
<ArrayBuffer
>* out
) {
164 if (!val
->IsArrayBuffer())
166 *out
= Local
<ArrayBuffer
>::Cast(val
);
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())
180 *out
= Local
<External
>::Cast(val
);
184 Local
<Value
> Converter
<Local
<Value
>>::ToV8(Isolate
* isolate
, Local
<Value
> val
) {
188 bool Converter
<Local
<Value
>>::FromV8(Isolate
* isolate
,
195 v8::Local
<v8::String
> StringToSymbol(v8::Isolate
* isolate
,
196 const base::StringPiece
& val
) {
197 return String::NewFromUtf8(isolate
,
199 String::kInternalizedString
,
200 static_cast<uint32_t>(val
.length()));
203 std::string
V8ToString(v8::Local
<v8::Value
> value
) {
205 return std::string();
207 if (!ConvertFromV8(NULL
, value
, &result
))
208 return std::string();