Replace the deprecated __attribute__((no_address_safety_analysis))
[chromium-blink-merge.git] / gin / converter.cc
blobe60e3a0d288064ac76114f56f869a6f1e32966ee
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::Handle;
14 using v8::Integer;
15 using v8::Isolate;
16 using v8::Number;
17 using v8::Object;
18 using v8::String;
19 using v8::Value;
21 namespace gin {
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();
29 return true;
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) {
37 if (!val->IsInt32())
38 return false;
39 *out = val->Int32Value();
40 return true;
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) {
48 if (!val->IsUint32())
49 return false;
50 *out = val->Uint32Value();
51 return true;
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) {
59 if (!val->IsNumber())
60 return false;
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();
64 return true;
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) {
72 if (!val->IsNumber())
73 return false;
74 *out = static_cast<uint64_t>(val->IntegerValue());
75 return true;
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) {
83 if (!val->IsNumber())
84 return false;
85 *out = val->NumberValue();
86 return true;
89 Handle<Value> Converter<std::string>::ToV8(Isolate* isolate,
90 const std::string& val) {
91 return String::NewFromUtf8(isolate,
92 val.data(),
93 String::kNormalString,
94 static_cast<uint32_t>(val.length()));
97 bool Converter<std::string>::FromV8(Handle<Value> val,
98 std::string* out) {
99 if (!val->IsString())
100 return false;
101 Handle<String> str = Handle<String>::Cast(val);
102 int length = str->Utf8Length();
103 out->resize(length);
104 str->WriteUtf8(&(*out)[0], length, NULL, String::NO_NULL_TERMINATION);
105 return true;
108 bool Converter<Handle<Function> >::FromV8(Handle<Value> val,
109 Handle<Function>* out) {
110 if (!val->IsFunction())
111 return false;
112 *out = Handle<Function>::Cast(val);
113 return true;
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())
124 return false;
125 *out = Handle<Object>::Cast(val);
126 return true;
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())
137 return false;
138 *out = Handle<ArrayBuffer>::Cast(val);
139 return true;
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())
150 return false;
151 *out = Handle<External>::Cast(val);
152 return true;
155 Handle<Value> Converter<Handle<Value> >::ToV8(v8::Isolate* isolate,
156 Handle<Value> val) {
157 return val;
160 bool Converter<Handle<Value> >::FromV8(Handle<Value> val,
161 Handle<Value>* out) {
162 *out = val;
163 return true;
166 v8::Handle<v8::String> StringToSymbol(v8::Isolate* isolate,
167 const std::string& val) {
168 return String::NewFromUtf8(isolate,
169 val.data(),
170 String::kInternalizedString,
171 static_cast<uint32_t>(val.length()));
175 } // namespace gin