Home | History | Annotate | Download | only in gin
      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.
      4 
      5 #include "gin/converter.h"
      6 
      7 #include "v8/include/v8.h"
      8 
      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;
     20 
     21 namespace gin {
     22 
     23 Handle<Value> Converter<bool>::ToV8(Isolate* isolate, bool val) {
     24   return Boolean::New(isolate, val).As<Value>();
     25 }
     26 
     27 bool Converter<bool>::FromV8(Isolate* isolate, Handle<Value> val, bool* out) {
     28   *out = val->BooleanValue();
     29   return true;
     30 }
     31 
     32 Handle<Value> Converter<int32_t>::ToV8(Isolate* isolate, int32_t val) {
     33   return Integer::New(isolate, val).As<Value>();
     34 }
     35 
     36 bool Converter<int32_t>::FromV8(Isolate* isolate, Handle<Value> val,
     37                                 int32_t* out) {
     38   if (!val->IsInt32())
     39     return false;
     40   *out = val->Int32Value();
     41   return true;
     42 }
     43 
     44 Handle<Value> Converter<uint32_t>::ToV8(Isolate* isolate, uint32_t val) {
     45   return Integer::NewFromUnsigned(isolate, val).As<Value>();
     46 }
     47 
     48 bool Converter<uint32_t>::FromV8(Isolate* isolate, Handle<Value> val,
     49                                  uint32_t* out) {
     50   if (!val->IsUint32())
     51     return false;
     52   *out = val->Uint32Value();
     53   return true;
     54 }
     55 
     56 Handle<Value> Converter<int64_t>::ToV8(Isolate* isolate, int64_t val) {
     57   return Number::New(isolate, static_cast<double>(val)).As<Value>();
     58 }
     59 
     60 bool Converter<int64_t>::FromV8(Isolate* isolate, Handle<Value> val,
     61                                 int64_t* out) {
     62   if (!val->IsNumber())
     63     return false;
     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();
     67   return true;
     68 }
     69 
     70 Handle<Value> Converter<uint64_t>::ToV8(Isolate* isolate, uint64_t val) {
     71   return Number::New(isolate, static_cast<double>(val)).As<Value>();
     72 }
     73 
     74 bool Converter<uint64_t>::FromV8(Isolate* isolate, Handle<Value> val,
     75                                  uint64_t* out) {
     76   if (!val->IsNumber())
     77     return false;
     78   *out = static_cast<uint64_t>(val->IntegerValue());
     79   return true;
     80 }
     81 
     82 Handle<Value> Converter<double>::ToV8(Isolate* isolate, double val) {
     83   return Number::New(isolate, val).As<Value>();
     84 }
     85 
     86 bool Converter<double>::FromV8(Isolate* isolate, Handle<Value> val,
     87                                double* out) {
     88   if (!val->IsNumber())
     89     return false;
     90   *out = val->NumberValue();
     91   return true;
     92 }
     93 
     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()));
     98 }
     99 
    100 Handle<Value> Converter<std::string>::ToV8(Isolate* isolate,
    101                                            const std::string& val) {
    102   return Converter<base::StringPiece>::ToV8(isolate, val);
    103 }
    104 
    105 bool Converter<std::string>::FromV8(Isolate* isolate, Handle<Value> val,
    106                                     std::string* out) {
    107   if (!val->IsString())
    108     return false;
    109   Handle<String> str = Handle<String>::Cast(val);
    110   int length = str->Utf8Length();
    111   out->resize(length);
    112   str->WriteUtf8(&(*out)[0], length, NULL, String::NO_NULL_TERMINATION);
    113   return true;
    114 }
    115 
    116 bool Converter<Handle<Function> >::FromV8(Isolate* isolate, Handle<Value> val,
    117                                           Handle<Function>* out) {
    118   if (!val->IsFunction())
    119     return false;
    120   *out = Handle<Function>::Cast(val);
    121   return true;
    122 }
    123 
    124 Handle<Value> Converter<Handle<Object> >::ToV8(Isolate* isolate,
    125                                                Handle<Object> val) {
    126   return val.As<Value>();
    127 }
    128 
    129 bool Converter<Handle<Object> >::FromV8(Isolate* isolate, Handle<Value> val,
    130                                         Handle<Object>* out) {
    131   if (!val->IsObject())
    132     return false;
    133   *out = Handle<Object>::Cast(val);
    134   return true;
    135 }
    136 
    137 Handle<Value> Converter<Handle<ArrayBuffer> >::ToV8(Isolate* isolate,
    138                                                     Handle<ArrayBuffer> val) {
    139   return val.As<Value>();
    140 }
    141 
    142 bool Converter<Handle<ArrayBuffer> >::FromV8(Isolate* isolate,
    143                                              Handle<Value> val,
    144                                              Handle<ArrayBuffer>* out) {
    145   if (!val->IsArrayBuffer())
    146     return false;
    147   *out = Handle<ArrayBuffer>::Cast(val);
    148   return true;
    149 }
    150 
    151 Handle<Value> Converter<Handle<External> >::ToV8(Isolate* isolate,
    152                                                  Handle<External> val) {
    153   return val.As<Value>();
    154 }
    155 
    156 bool Converter<Handle<External> >::FromV8(Isolate* isolate,
    157                                           v8::Handle<Value> val,
    158                                           Handle<External>* out) {
    159   if (!val->IsExternal())
    160     return false;
    161   *out = Handle<External>::Cast(val);
    162   return true;
    163 }
    164 
    165 Handle<Value> Converter<Handle<Value> >::ToV8(Isolate* isolate,
    166                                               Handle<Value> val) {
    167   return val;
    168 }
    169 
    170 bool Converter<Handle<Value> >::FromV8(Isolate* isolate, Handle<Value> val,
    171                                        Handle<Value>* out) {
    172   *out = val;
    173   return true;
    174 }
    175 
    176 v8::Handle<v8::String> StringToSymbol(v8::Isolate* isolate,
    177                                       const base::StringPiece& val) {
    178   return String::NewFromUtf8(isolate,
    179                              val.data(),
    180                              String::kInternalizedString,
    181                              static_cast<uint32_t>(val.length()));
    182 }
    183 
    184 std::string V8ToString(v8::Handle<v8::Value> value) {
    185   if (value.IsEmpty())
    186     return std::string();
    187   std::string result;
    188   if (!ConvertFromV8(NULL, value, &result))
    189     return std::string();
    190   return result;
    191 }
    192 
    193 }  // namespace gin
    194