// Copyright 2013 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE.chromium file. #include "native_mate/converter.h" #include "native_mate/compat.h" #include "v8/include/v8.h" using v8::Array; using v8::Boolean; using v8::External; using v8::Function; using v8::Integer; using v8::Isolate; using v8::Local; using v8::Number; using v8::Object; using v8::String; using v8::Value; namespace mate { Local Converter::ToV8(Isolate* isolate, bool val) { return MATE_BOOLEAN_NEW(isolate, val); } bool Converter::FromV8(Isolate* isolate, Local val, bool* out) { if (!val->IsBoolean()) return false; *out = val->BooleanValue(); return true; } #if !defined(OS_LINUX) && !defined(OS_FREEBSD) Local Converter::ToV8(Isolate* isolate, unsigned long val) { return MATE_INTEGER_NEW(isolate, val); } bool Converter::FromV8(Isolate* isolate, Local val, unsigned long* out) { if (!val->IsNumber()) return false; *out = val->IntegerValue(); return true; } #endif Local Converter::ToV8(Isolate* isolate, int32_t val) { return MATE_INTEGER_NEW(isolate, val); } bool Converter::FromV8(Isolate* isolate, Local val, int32_t* out) { if (!val->IsInt32()) return false; *out = val->Int32Value(); return true; } Local Converter::ToV8(Isolate* isolate, uint32_t val) { return MATE_INTEGER_NEW_UNSIGNED(isolate, val); } bool Converter::FromV8(Isolate* isolate, Local val, uint32_t* out) { if (!val->IsUint32()) return false; *out = val->Uint32Value(); return true; } Local Converter::ToV8(Isolate* isolate, int64_t val) { return MATE_NUMBER_NEW(isolate, static_cast(val)); } bool Converter::FromV8(Isolate* isolate, Local val, int64_t* out) { if (!val->IsNumber()) return false; // Even though IntegerValue returns int64_t, JavaScript cannot represent // the full precision of int64_t, which means some rounding might occur. *out = val->IntegerValue(); return true; } Local Converter::ToV8(Isolate* isolate, uint64_t val) { return MATE_NUMBER_NEW(isolate, static_cast(val)); } bool Converter::FromV8(Isolate* isolate, Local val, uint64_t* out) { if (!val->IsNumber()) return false; *out = static_cast(val->IntegerValue()); return true; } Local Converter::ToV8(Isolate* isolate, float val) { return MATE_NUMBER_NEW(isolate, val); } bool Converter::FromV8(Isolate* isolate, Local val, float* out) { if (!val->IsNumber()) return false; *out = static_cast(val->NumberValue()); return true; } Local Converter::ToV8(Isolate* isolate, double val) { return MATE_NUMBER_NEW(isolate, val); } bool Converter::FromV8(Isolate* isolate, Local val, double* out) { if (!val->IsNumber()) return false; *out = val->NumberValue(); return true; } Local Converter::ToV8( Isolate* isolate, const char* val) { return MATE_STRING_NEW_FROM_UTF8(isolate, val, -1); } Local Converter::ToV8( Isolate* isolate, const base::StringPiece& val) { return MATE_STRING_NEW_FROM_UTF8(isolate, val.data(), static_cast(val.length())); } Local Converter::ToV8(Isolate* isolate, const std::string& val) { return Converter::ToV8(isolate, val); } bool Converter::FromV8(Isolate* isolate, Local val, std::string* out) { if (!val->IsString()) return false; Local str = Local::Cast(val); int length = str->Utf8Length(); out->resize(length); str->WriteUtf8(&(*out)[0], length, NULL, String::NO_NULL_TERMINATION); return true; } Local Converter>::ToV8(Isolate* isolate, Local val) { return val; } bool Converter >::FromV8(Isolate* isolate, Local val, Local* out) { if (!val->IsFunction()) return false; *out = Local::Cast(val); return true; } Local Converter >::ToV8(Isolate* isolate, Local val) { return val; } bool Converter >::FromV8(Isolate* isolate, Local val, Local* out) { if (!val->IsObject()) return false; *out = Local::Cast(val); return true; } Local Converter >::ToV8(Isolate* isolate, Local val) { return val; } bool Converter >::FromV8(Isolate* isolate, Local val, Local* out) { if (!val->IsString()) return false; *out = Local::Cast(val); return true; } Local Converter >::ToV8(Isolate* isolate, Local val) { return val; } bool Converter >::FromV8(Isolate* isolate, v8::Local val, Local* out) { if (!val->IsExternal()) return false; *out = Local::Cast(val); return true; } Local Converter >::ToV8(Isolate* isolate, Local val) { return val; } bool Converter >::FromV8(Isolate* isolate, v8::Local val, Local* out) { if (!val->IsArray()) return false; *out = Local::Cast(val); return true; } Local Converter >::ToV8(Isolate* isolate, Local val) { return val; } bool Converter >::FromV8(Isolate* isolate, Local val, Local* out) { *out = val; return true; } v8::Local StringToSymbol(v8::Isolate* isolate, const base::StringPiece& val) { return MATE_STRING_NEW_SYMBOL(isolate, val.data(), static_cast(val.length())); } std::string V8ToString(v8::Local value) { if (value.IsEmpty()) return std::string(); std::string result; if (!ConvertFromV8(NULL, value, &result)) return std::string(); return result; } } // namespace mate