2014-04-15 07:04:36 +04:00
|
|
|
// 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 "v8/include/v8.h"
|
|
|
|
|
2015-06-10 06:14:42 +03:00
|
|
|
using v8::Array;
|
2014-04-15 07:04:36 +04:00
|
|
|
using v8::Boolean;
|
|
|
|
using v8::External;
|
|
|
|
using v8::Function;
|
|
|
|
using v8::Integer;
|
|
|
|
using v8::Isolate;
|
2015-05-22 14:11:02 +03:00
|
|
|
using v8::Local;
|
2014-04-15 07:04:36 +04:00
|
|
|
using v8::Number;
|
|
|
|
using v8::Object;
|
|
|
|
using v8::String;
|
|
|
|
using v8::Value;
|
|
|
|
|
|
|
|
namespace mate {
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
Local<Value> Converter<bool>::ToV8(Isolate* isolate, bool val) {
|
2018-05-18 00:36:15 +03:00
|
|
|
return v8::Boolean::New(isolate, val);
|
2014-04-15 07:04:36 +04:00
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
bool Converter<bool>::FromV8(Isolate* isolate, Local<Value> val, bool* out) {
|
2014-06-23 17:47:39 +04:00
|
|
|
if (!val->IsBoolean())
|
|
|
|
return false;
|
2014-04-15 07:04:36 +04:00
|
|
|
*out = val->BooleanValue();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-22 21:14:26 +03:00
|
|
|
#if !defined(OS_LINUX) && !defined(OS_FREEBSD)
|
2015-05-22 14:11:02 +03:00
|
|
|
Local<Value> Converter<unsigned long>::ToV8(Isolate* isolate,
|
2015-03-11 02:08:30 +03:00
|
|
|
unsigned long val) {
|
2018-05-18 00:36:15 +03:00
|
|
|
return v8::Integer::New(isolate, val);
|
2015-03-11 02:08:30 +03:00
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
bool Converter<unsigned long>::FromV8(Isolate* isolate, Local<Value> val,
|
2015-03-11 02:08:30 +03:00
|
|
|
unsigned long* out) {
|
|
|
|
if (!val->IsNumber())
|
|
|
|
return false;
|
|
|
|
*out = val->IntegerValue();
|
|
|
|
return true;
|
|
|
|
}
|
2015-03-14 21:47:08 +03:00
|
|
|
#endif
|
2015-03-11 02:08:30 +03:00
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
Local<Value> Converter<int32_t>::ToV8(Isolate* isolate, int32_t val) {
|
2018-05-18 00:36:15 +03:00
|
|
|
return v8::Integer::New(isolate, val);
|
2014-04-15 07:04:36 +04:00
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
bool Converter<int32_t>::FromV8(Isolate* isolate, Local<Value> val,
|
2014-04-15 07:04:36 +04:00
|
|
|
int32_t* out) {
|
|
|
|
if (!val->IsInt32())
|
|
|
|
return false;
|
|
|
|
*out = val->Int32Value();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
Local<Value> Converter<uint32_t>::ToV8(Isolate* isolate, uint32_t val) {
|
2018-05-18 00:36:15 +03:00
|
|
|
return v8::Integer::NewFromUnsigned(isolate, val);
|
2014-04-15 07:04:36 +04:00
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
bool Converter<uint32_t>::FromV8(Isolate* isolate, Local<Value> val,
|
2014-04-15 07:04:36 +04:00
|
|
|
uint32_t* out) {
|
|
|
|
if (!val->IsUint32())
|
|
|
|
return false;
|
|
|
|
*out = val->Uint32Value();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
Local<Value> Converter<int64_t>::ToV8(Isolate* isolate, int64_t val) {
|
2018-05-18 00:36:15 +03:00
|
|
|
return v8::Number::New(isolate, static_cast<double>(val));
|
2014-04-15 07:04:36 +04:00
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
bool Converter<int64_t>::FromV8(Isolate* isolate, Local<Value> val,
|
2014-04-15 07:04:36 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
Local<Value> Converter<uint64_t>::ToV8(Isolate* isolate, uint64_t val) {
|
2018-05-18 00:36:15 +03:00
|
|
|
return v8::Number::New(isolate, static_cast<double>(val));
|
2014-04-15 07:04:36 +04:00
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
bool Converter<uint64_t>::FromV8(Isolate* isolate, Local<Value> val,
|
2014-04-15 07:04:36 +04:00
|
|
|
uint64_t* out) {
|
|
|
|
if (!val->IsNumber())
|
|
|
|
return false;
|
|
|
|
*out = static_cast<uint64_t>(val->IntegerValue());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
Local<Value> Converter<float>::ToV8(Isolate* isolate, float val) {
|
2018-05-18 00:36:15 +03:00
|
|
|
return v8::Number::New(isolate, val);
|
2014-04-15 07:04:36 +04:00
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
bool Converter<float>::FromV8(Isolate* isolate, Local<Value> val,
|
2014-04-15 07:04:36 +04:00
|
|
|
float* out) {
|
|
|
|
if (!val->IsNumber())
|
|
|
|
return false;
|
|
|
|
*out = static_cast<float>(val->NumberValue());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
Local<Value> Converter<double>::ToV8(Isolate* isolate, double val) {
|
2018-05-18 00:36:15 +03:00
|
|
|
return v8::Number::New(isolate, val);
|
2014-04-15 07:04:36 +04:00
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
bool Converter<double>::FromV8(Isolate* isolate, Local<Value> val,
|
2014-04-15 07:04:36 +04:00
|
|
|
double* out) {
|
|
|
|
if (!val->IsNumber())
|
|
|
|
return false;
|
|
|
|
*out = val->NumberValue();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
Local<Value> Converter<const char*>::ToV8(
|
2014-06-23 17:24:18 +04:00
|
|
|
Isolate* isolate, const char* val) {
|
2018-05-18 00:36:15 +03:00
|
|
|
return v8::String::NewFromUtf8(isolate, val);
|
2014-06-23 17:24:18 +04:00
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
Local<Value> Converter<base::StringPiece>::ToV8(
|
2014-04-15 07:04:36 +04:00
|
|
|
Isolate* isolate, const base::StringPiece& val) {
|
2018-05-18 00:36:15 +03:00
|
|
|
return v8::String::NewFromUtf8(isolate,
|
|
|
|
val.data(),
|
|
|
|
v8::String::kNormalString,
|
|
|
|
static_cast<uint32_t>(val.length()));
|
2014-04-15 07:04:36 +04:00
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
Local<Value> Converter<std::string>::ToV8(Isolate* isolate,
|
2014-04-15 07:04:36 +04:00
|
|
|
const std::string& val) {
|
|
|
|
return Converter<base::StringPiece>::ToV8(isolate, val);
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
bool Converter<std::string>::FromV8(Isolate* isolate, Local<Value> val,
|
2014-04-15 07:04:36 +04:00
|
|
|
std::string* out) {
|
|
|
|
if (!val->IsString())
|
|
|
|
return false;
|
2015-05-22 14:11:02 +03:00
|
|
|
Local<String> str = Local<String>::Cast(val);
|
2014-04-15 07:04:36 +04:00
|
|
|
int length = str->Utf8Length();
|
|
|
|
out->resize(length);
|
|
|
|
str->WriteUtf8(&(*out)[0], length, NULL, String::NO_NULL_TERMINATION);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-02 11:20:41 +03:00
|
|
|
Local<Value> Converter<Local<Function>>::ToV8(Isolate* isolate,
|
|
|
|
Local<Function> val) {
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
bool Converter<Local<Function> >::FromV8(Isolate* isolate, Local<Value> val,
|
|
|
|
Local<Function>* out) {
|
2014-04-15 07:04:36 +04:00
|
|
|
if (!val->IsFunction())
|
|
|
|
return false;
|
2015-05-22 14:11:02 +03:00
|
|
|
*out = Local<Function>::Cast(val);
|
2014-04-15 07:04:36 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
Local<Value> Converter<Local<Object> >::ToV8(Isolate* isolate,
|
|
|
|
Local<Object> val) {
|
2014-04-15 07:04:36 +04:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
bool Converter<Local<Object> >::FromV8(Isolate* isolate, Local<Value> val,
|
|
|
|
Local<Object>* out) {
|
2014-04-15 07:04:36 +04:00
|
|
|
if (!val->IsObject())
|
|
|
|
return false;
|
2015-05-22 14:11:02 +03:00
|
|
|
*out = Local<Object>::Cast(val);
|
2014-04-15 07:04:36 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
Local<Value> Converter<Local<String> >::ToV8(Isolate* isolate,
|
|
|
|
Local<String> val) {
|
2014-04-16 11:28:43 +04:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
bool Converter<Local<String> >::FromV8(Isolate* isolate, Local<Value> val,
|
|
|
|
Local<String>* out) {
|
2014-04-16 11:28:43 +04:00
|
|
|
if (!val->IsString())
|
|
|
|
return false;
|
2015-05-22 14:11:02 +03:00
|
|
|
*out = Local<String>::Cast(val);
|
2014-04-16 11:28:43 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
Local<Value> Converter<Local<External> >::ToV8(Isolate* isolate,
|
|
|
|
Local<External> val) {
|
2014-04-15 07:04:36 +04:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
bool Converter<Local<External> >::FromV8(Isolate* isolate,
|
|
|
|
v8::Local<Value> val,
|
|
|
|
Local<External>* out) {
|
2014-04-15 07:04:36 +04:00
|
|
|
if (!val->IsExternal())
|
|
|
|
return false;
|
2015-05-22 14:11:02 +03:00
|
|
|
*out = Local<External>::Cast(val);
|
2014-04-15 07:04:36 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-10 06:14:42 +03:00
|
|
|
Local<Value> Converter<Local<Array> >::ToV8(Isolate* isolate,
|
|
|
|
Local<Array> val) {
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Converter<Local<Array> >::FromV8(Isolate* isolate,
|
|
|
|
v8::Local<Value> val,
|
|
|
|
Local<Array>* out) {
|
|
|
|
if (!val->IsArray())
|
|
|
|
return false;
|
|
|
|
*out = Local<Array>::Cast(val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
Local<Value> Converter<Local<Value> >::ToV8(Isolate* isolate,
|
|
|
|
Local<Value> val) {
|
2014-04-15 07:04:36 +04:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
bool Converter<Local<Value> >::FromV8(Isolate* isolate, Local<Value> val,
|
|
|
|
Local<Value>* out) {
|
2014-04-15 07:04:36 +04:00
|
|
|
*out = val;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
v8::Local<v8::String> StringToSymbol(v8::Isolate* isolate,
|
2014-04-15 07:04:36 +04:00
|
|
|
const base::StringPiece& val) {
|
2018-05-18 00:36:15 +03:00
|
|
|
return v8::String::NewFromUtf8(isolate,
|
|
|
|
val.data(),
|
|
|
|
v8::String::kInternalizedString,
|
|
|
|
static_cast<uint32_t>(val.length()));
|
2014-04-15 07:04:36 +04:00
|
|
|
}
|
|
|
|
|
2015-05-22 14:11:02 +03:00
|
|
|
std::string V8ToString(v8::Local<v8::Value> value) {
|
2014-04-15 07:04:36 +04:00
|
|
|
if (value.IsEmpty())
|
|
|
|
return std::string();
|
|
|
|
std::string result;
|
|
|
|
if (!ConvertFromV8(NULL, value, &result))
|
|
|
|
return std::string();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mate
|