Discard all our custom V8 helpers, use native-mate instead.

This commit is contained in:
Cheng Zhao 2014-04-22 23:07:21 +08:00
parent ef5342b86e
commit a040a96652
28 changed files with 484 additions and 1552 deletions

View file

@ -1,74 +0,0 @@
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "atom/common/api/atom_api_event_emitter.h"
#include <vector>
#include "atom/browser/api/atom_api_event.h"
#include "atom/common/browser_v8_locker.h"
#include "atom/common/v8/native_type_conversions.h"
#include "base/logging.h"
#include "atom/common/node_includes.h"
namespace atom {
namespace api {
EventEmitter::EventEmitter(v8::Handle<v8::Object> wrapper) {
Wrap(wrapper);
}
EventEmitter::~EventEmitter() {
// Clear the aligned pointer, it should have been done by ObjectWrap but
// somehow node v0.11.x changed this behaviour.
BrowserV8Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
handle()->SetAlignedPointerInInternalField(0, NULL);
}
bool EventEmitter::Emit(const std::string& name) {
base::ListValue args;
return Emit(name, &args);
}
bool EventEmitter::Emit(const std::string& name, base::ListValue* args) {
BrowserV8Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
v8::Handle<v8::Context> context = v8::Context::GetCurrent();
scoped_ptr<V8ValueConverter> converter(new V8ValueConverter);
v8::Handle<v8::Object> v8_event = Event::CreateV8Object();
Event* event = Event::Unwrap<Event>(v8_event);
// Generate arguments for calling handle.emit.
std::vector<v8::Handle<v8::Value>> v8_args;
v8_args.reserve(args->GetSize() + 2);
v8_args.push_back(ToV8Value(name));
v8_args.push_back(v8_event);
for (size_t i = 0; i < args->GetSize(); i++) {
base::Value* value = NULL;
if (args->Get(i, &value)) {
DCHECK(value);
v8_args.push_back(converter->ToV8Value(value, context));
} else {
NOTREACHED() << "Wrong offset " << i << " for " << *args;
}
}
node::MakeCallback(handle(), "emit", v8_args.size(), &v8_args[0]);
bool prevent_default = event->prevent_default();
// Don't wait for V8 GC, delete it immediately.
delete event;
return prevent_default;
}
} // namespace api
} // namespace atom

View file

@ -1,43 +0,0 @@
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ATOM_COMMON_API_ATOM_API_EVENT_EMITTER_H_
#define ATOM_COMMON_API_ATOM_API_EVENT_EMITTER_H_
#include <string>
#include "base/basictypes.h"
#include "vendor/node/src/node_object_wrap.h"
namespace base {
class ListValue;
}
namespace atom {
namespace api {
// Class interiting EventEmitter should assume it's a javascript object which
// interits require('events').EventEmitter, this class provides many helper
// methods to do event processing in C++.
class EventEmitter : public node::ObjectWrap {
public:
virtual ~EventEmitter();
// Emit an event and returns whether the handler has called preventDefault().
bool Emit(const std::string& name);
bool Emit(const std::string& name, base::ListValue* args);
protected:
explicit EventEmitter(v8::Handle<v8::Object> wrapper);
private:
DISALLOW_COPY_AND_ASSIGN(EventEmitter);
};
} // namespace api
} // namespace atom
#endif // ATOM_COMMON_API_ATOM_API_EVENT_EMITTER_H_

View file

@ -6,30 +6,11 @@
#include "atom/common/platform_util.h"
#include "atom/common/native_mate_converters/file_path_converter.h"
#include "atom/common/native_mate_converters/gurl_converter.h"
#include "native_mate/dictionary.h"
#include "url/gurl.h"
#include "atom/common/node_includes.h"
namespace mate {
template<>
struct Converter<GURL> {
static bool FromV8(v8::Isolate* isolate,
v8::Handle<v8::Value> val,
GURL* out) {
std::string url;
if (Converter<std::string>::FromV8(isolate, val, &url)) {
*out = GURL(url);
return true;
} else {
return false;
}
}
};
} // namespace mate
namespace {
void Initialize(v8::Handle<v8::Object> exports) {

View file

@ -1,13 +1,12 @@
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
// Copyright (c) 2012 Intel Corp. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ATOM_COMMON_API_OBJECT_LIFE_MONITOR_H_
#define ATOM_COMMON_API_OBJECT_LIFE_MONITOR_H_
#include "atom/common/v8/scoped_persistent.h"
#include "base/basictypes.h"
#include "native_mate/scoped_persistent.h"
namespace atom {
@ -23,7 +22,7 @@ class ObjectLifeMonitor {
v8::Persistent<v8::Object>* value,
ObjectLifeMonitor* self);
ScopedPersistent<v8::Object> handle_;
mate::ScopedPersistent<v8::Object> handle_;
DISALLOW_COPY_AND_ASSIGN(ObjectLifeMonitor);
};

View file

@ -0,0 +1,34 @@
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ATOM_COMMON_NATIVE_MATE_CONVERTERS_GURL_CONVERTER_H_
#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_GURL_CONVERTER_H_
#include "native_mate/converter.h"
#include "url/gurl.h"
namespace mate {
template<>
struct Converter<GURL> {
static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
const GURL& val) {
return Converter<std::string>::ToV8(isolate, val.spec());
}
static bool FromV8(v8::Isolate* isolate,
v8::Handle<v8::Value> val,
GURL* out) {
std::string url;
if (Converter<std::string>::FromV8(isolate, val, &url)) {
*out = GURL(url);
return true;
} else {
return false;
}
}
};
} // namespace mate
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_GURL_CONVERTER_H_

View file

@ -9,6 +9,20 @@
namespace mate {
bool Converter<base::DictionaryValue>::FromV8(v8::Isolate* isolate,
v8::Handle<v8::Value> val,
base::DictionaryValue* out) {
scoped_ptr<atom::V8ValueConverter> converter(new atom::V8ValueConverter);
scoped_ptr<base::Value> value(converter->FromV8Value(
val, v8::Context::GetCurrent()));
if (value->IsType(base::Value::TYPE_DICTIONARY)) {
out->Swap(static_cast<DictionaryValue*>(value.get()));
return true;
} else {
return false;
}
}
bool Converter<base::ListValue>::FromV8(v8::Isolate* isolate,
v8::Handle<v8::Value> val,
base::ListValue* out) {

View file

@ -8,11 +8,19 @@
#include "native_mate/converter.h"
namespace base {
class DictionaryValue;
class ListValue;
}
namespace mate {
template<>
struct Converter<base::DictionaryValue> {
static bool FromV8(v8::Isolate* isolate,
v8::Handle<v8::Value> val,
base::DictionaryValue* out);
};
template<>
struct Converter<base::ListValue> {
static bool FromV8(v8::Isolate* isolate,

View file

@ -14,6 +14,7 @@
#include "vendor/node/src/env.h"
#include "vendor/node/src/env-inl.h"
#include "vendor/node/src/node.h"
#include "vendor/node/src/node_buffer.h"
#include "vendor/node/src/node_internals.h"
using node::node_isolate;

View file

@ -1,71 +0,0 @@
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ATOM_COMMON_SWAP_OR_ASSIGN_H_
#define ATOM_COMMON_SWAP_OR_ASSIGN_H_
#include "base/compiler_specific.h"
namespace internal {
#if defined(OS_WIN)
template<typename T> inline
void SwapOrAssign(T& v1, const T& v2) {
__if_exists(T::swap) {
v1.swap(const_cast<T&>(v2));
}
__if_not_exists(T::swap) {
v1 = v2;
}
}
template<typename T> inline
void SwapOrAssign(T*& v1, T* v2) {
v1 = v2;
}
inline
void SwapOrAssign(int& v1, int v2) {
v1 = v2;
}
inline
void SwapOrAssign(bool& v1, bool v2) {
v1 = v2;
}
#else // defined(OS_WIN)
// Helper to detect whether value has specified method.
template <typename T>
class HasSwapMethod {
typedef char one;
typedef long two;
template <typename C> static one test(char[sizeof(&C::swap)]) ;
template <typename C> static two test(...);
public:
enum { value = sizeof(test<T>(0)) == sizeof(char) };
};
template<bool B, class T = void>
struct enable_if {};
template<class T>
struct enable_if<true, T> { typedef T type; };
template<typename T> inline
typename enable_if<HasSwapMethod<T>::value>::type SwapOrAssign(
T& v1, const T& v2) {
v1.swap(const_cast<T&>(v2));
}
template<typename T> inline
typename enable_if<!HasSwapMethod<T>::value>::type SwapOrAssign(
T& v1, const T& v2) {
v1 = v2;
}
#endif // !defined(OS_WIN)
} // namespace internal
#endif // ATOM_COMMON_SWAP_OR_ASSIGN_H_

View file

@ -1,344 +0,0 @@
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ATOM_COMMON_V8_NATIVE_TYPE_CONVERSIONS_H_
#define ATOM_COMMON_V8_NATIVE_TYPE_CONVERSIONS_H_
#include <map>
#include <string>
#include <vector>
#include "atom/browser/api/atom_api_window.h"
#include "atom/common/swap_or_assign.h"
#include "atom/common/v8/scoped_persistent.h"
#include "atom/common/native_mate_converters/v8_value_converter.h"
#include "base/files/file_path.h"
#include "base/strings/string16.h"
#include "base/template_util.h"
#include "base/values.h"
#include "ui/gfx/point.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/size.h"
#include "url/gurl.h"
// Convert V8 value to arbitrary supported types.
struct FromV8Value {
explicit FromV8Value(v8::Handle<v8::Value> value) : value_(value) {}
operator int() {
return value_->IntegerValue();
}
operator bool() {
return value_->BooleanValue();
}
operator std::string() {
return *v8::String::Utf8Value(value_);
}
operator string16() {
v8::String::Value s(value_);
return string16(reinterpret_cast<const char16*>(*s), s.length());
}
operator GURL() {
std::string str = FromV8Value(value_);
return GURL(str);
}
operator base::FilePath() {
return base::FilePath::FromUTF8Unsafe(FromV8Value(value_));
}
operator gfx::Rect() {
v8::Handle<v8::Object> rect = value_->ToObject();
v8::Handle<v8::Value> x = rect->Get(v8::String::New("x"));
v8::Handle<v8::Value> y = rect->Get(v8::String::New("y"));
v8::Handle<v8::Value> width = rect->Get(v8::String::New("width"));
v8::Handle<v8::Value> height = rect->Get(v8::String::New("height"));
if (!x->IsNumber() || !y->IsNumber() ||
!width->IsNumber() || !height->IsNumber())
return gfx::Rect();
else
return gfx::Rect(x->IntegerValue(), y->IntegerValue(),
width->IntegerValue(), height->IntegerValue());
}
operator scoped_ptr<base::Value>() {
scoped_ptr<atom::V8ValueConverter> converter(new atom::V8ValueConverter);
return scoped_ptr<base::Value>(
converter->FromV8Value(value_, v8::Context::GetCurrent()));
}
template<class T>
operator std::vector<T>() {
std::vector<T> array;
v8::Handle<v8::Array> v8_array = v8::Handle<v8::Array>::Cast(value_);
for (uint32_t i = 0; i < v8_array->Length(); ++i)
array.push_back(FromV8Value(v8_array->Get(i)));
return array;
}
template<class K, class V>
operator std::map<K, V>() {
std::map<K, V> dict;
v8::Handle<v8::Object> v8_dict = value_->ToObject();
v8::Handle<v8::Array> v8_keys = v8_dict->GetOwnPropertyNames();
for (uint32_t i = 0; i < v8_keys->Length(); ++i) {
v8::Handle<v8::Value> v8_key = v8_keys->Get(i);
K key = FromV8Value(v8_key);
dict[key] = V(FromV8Value(v8_dict->Get(v8_key)));
}
return dict;
}
operator atom::NativeWindow*() {
using atom::api::Window;
if (value_->IsObject()) {
Window* window = Window::Unwrap<Window>(value_->ToObject());
if (window && window->window())
return window->window();
}
return NULL;
}
operator atom::RefCountedV8Function() {
v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(value_);
return atom::RefCountedV8Function(
new atom::RefCountedPersistent<v8::Function>(func));
}
v8::Handle<v8::Value> value_;
};
// Convert arbitrary supported native type to V8 value.
inline v8::Handle<v8::Value> ToV8Value(int i) {
return v8::Integer::New(i);
}
inline v8::Handle<v8::Value> ToV8Value(bool b) {
return v8::Boolean::New(b);
}
inline v8::Handle<v8::Value> ToV8Value(float f) {
return v8::Number::New(f);
}
inline v8::Handle<v8::Value> ToV8Value(double f) {
return v8::Number::New(f);
}
inline v8::Handle<v8::Value> ToV8Value(const char* s) {
return v8::String::New(s);
}
inline v8::Handle<v8::Value> ToV8Value(const std::string& s) {
return v8::String::New(s.data(), s.size());
}
inline v8::Handle<v8::Value> ToV8Value(const string16& s) {
return v8::String::New(reinterpret_cast<const uint16_t*>(s.data()), s.size());
}
inline v8::Handle<v8::Value> ToV8Value(const GURL& url) {
return ToV8Value(url.spec());
}
inline v8::Handle<v8::Value> ToV8Value(const base::FilePath& path) {
std::string path_string(path.AsUTF8Unsafe());
return v8::String::New(path_string.data(), path_string.size());
}
inline v8::Handle<v8::Value> ToV8Value(void* whatever) {
return v8::Undefined();
}
template<class T> inline
v8::Handle<v8::Value> ToV8Value(const std::vector<T>& arr) {
v8::Handle<v8::Array> result = v8::Array::New(arr.size());
for (size_t i = 0; i < arr.size(); ++i)
result->Set(i, ToV8Value(arr[i]));
return result;
}
inline v8::Handle<v8::Value> ToV8Value(const gfx::Point& point) {
v8::Handle<v8::Object> obj = v8::Object::New();
obj->Set(ToV8Value("x"), ToV8Value(point.x()));
obj->Set(ToV8Value("y"), ToV8Value(point.y()));
return obj;
}
inline v8::Handle<v8::Value> ToV8Value(const gfx::Rect& rect) {
v8::Handle<v8::Object> obj = v8::Object::New();
obj->Set(ToV8Value("x"), ToV8Value(rect.x()));
obj->Set(ToV8Value("y"), ToV8Value(rect.y()));
obj->Set(ToV8Value("width"), ToV8Value(rect.width()));
obj->Set(ToV8Value("height"), ToV8Value(rect.height()));
return obj;
}
inline v8::Handle<v8::Value> ToV8Value(const gfx::Size& size) {
v8::Handle<v8::Object> obj = v8::Object::New();
obj->Set(ToV8Value("width"), ToV8Value(size.width()));
obj->Set(ToV8Value("height"), ToV8Value(size.height()));
return obj;
}
// Check if a V8 Value is of specified type.
template<class T> inline
bool V8ValueCanBeConvertedTo(v8::Handle<v8::Value> value) {
return false;
}
template<> inline
bool V8ValueCanBeConvertedTo<int>(v8::Handle<v8::Value> value) {
return value->IsNumber();
}
template<> inline
bool V8ValueCanBeConvertedTo<bool>(v8::Handle<v8::Value> value) {
return value->IsBoolean();
}
template<> inline
bool V8ValueCanBeConvertedTo<std::string>(v8::Handle<v8::Value> value) {
return value->IsString();
}
template<> inline
bool V8ValueCanBeConvertedTo<string16>(v8::Handle<v8::Value> value) {
return V8ValueCanBeConvertedTo<std::string>(value);
}
template<> inline
bool V8ValueCanBeConvertedTo<GURL>(v8::Handle<v8::Value> value) {
return V8ValueCanBeConvertedTo<std::string>(value);
}
template<> inline
bool V8ValueCanBeConvertedTo<base::FilePath>(v8::Handle<v8::Value> value) {
return V8ValueCanBeConvertedTo<std::string>(value);
}
template<> inline
bool V8ValueCanBeConvertedTo<gfx::Rect>(v8::Handle<v8::Value> value) {
return value->IsObject();
}
template<> inline
bool V8ValueCanBeConvertedTo<scoped_ptr<base::Value>>(
v8::Handle<v8::Value> value) {
return value->IsObject();
}
template<> inline
bool V8ValueCanBeConvertedTo<std::vector<std::string>>(
v8::Handle<v8::Value> value) {
return value->IsArray();
}
template<> inline
bool V8ValueCanBeConvertedTo<std::map<std::string, std::string>>(
v8::Handle<v8::Value> value) {
return value->IsObject();
}
template<> inline
bool V8ValueCanBeConvertedTo<atom::NativeWindow*>(v8::Handle<v8::Value> value) {
using atom::api::Window;
if (value->IsObject()) {
Window* window = Window::Unwrap<Window>(value->ToObject());
if (window && window->window())
return true;
}
return false;
}
template<> inline
bool V8ValueCanBeConvertedTo<atom::RefCountedV8Function>(
v8::Handle<v8::Value> value) {
return value->IsFunction();
}
// Check and convert V8's Arguments to native types.
template<typename T1> inline
bool FromV8Arguments(const v8::FunctionCallbackInfo<v8::Value>& args,
T1* value,
int index = 0) {
if (!V8ValueCanBeConvertedTo<T1>(args[index]))
return false;
internal::SwapOrAssign(*value,
FromV8Value(args[index]).operator T1());
return true;
}
template<typename T1, typename T2> inline
bool FromV8Arguments(const v8::FunctionCallbackInfo<v8::Value>& args,
T1* a1,
T2* a2) {
return FromV8Arguments<T1>(args, a1) && FromV8Arguments<T2>(args, a2, 1);
}
template<typename T1, typename T2, typename T3> inline
bool FromV8Arguments(const v8::FunctionCallbackInfo<v8::Value>& args,
T1* a1,
T2* a2,
T3* a3) {
return FromV8Arguments<T1, T2>(args, a1, a2) &&
FromV8Arguments<T3>(args, a3, 2);
}
template<typename T1, typename T2, typename T3, typename T4> inline
bool FromV8Arguments(const v8::FunctionCallbackInfo<v8::Value>& args,
T1* a1,
T2* a2,
T3* a3,
T4* a4) {
return FromV8Arguments<T1, T2, T3>(args, a1, a2, a3) &&
FromV8Arguments<T4>(args, a4, 3);
}
template<typename T1, typename T2, typename T3, typename T4, typename T5> inline
bool FromV8Arguments(const v8::FunctionCallbackInfo<v8::Value>& args,
T1* a1,
T2* a2,
T3* a3,
T4* a4,
T5* a5) {
return FromV8Arguments<T1, T2, T3, T4>(args, a1, a2, a3, a4) &&
FromV8Arguments<T5>(args, a5, 4);
}
template<typename T1, typename T2, typename T3, typename T4, typename T5,
typename T6> inline
bool FromV8Arguments(const v8::FunctionCallbackInfo<v8::Value>& args,
T1* a1,
T2* a2,
T3* a3,
T4* a4,
T5* a5,
T6* a6) {
return FromV8Arguments<T1, T2, T3, T4, T5>(args, a1, a2, a3, a4, a5) &&
FromV8Arguments<T6>(args, a6, 5);
}
template<typename T1, typename T2, typename T3, typename T4, typename T5,
typename T6, typename T7> inline
bool FromV8Arguments(const v8::FunctionCallbackInfo<v8::Value>& args,
T1* a1,
T2* a2,
T3* a3,
T4* a4,
T5* a5,
T6* a6,
T7* a7) {
return
FromV8Arguments<T1, T2, T3, T4, T5, T6>(args, a1, a2, a3, a4, a5, a6) &&
FromV8Arguments<T7>(args, a7, 6);
}
#endif // ATOM_COMMON_V8_NATIVE_TYPE_CONVERSIONS_H_

View file

@ -1,115 +0,0 @@
// 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 file.
#ifndef ATOM_COMMON_V8_SCOPED_PERSISTENT_H_
#define ATOM_COMMON_V8_SCOPED_PERSISTENT_H_
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "v8/include/v8.h"
namespace atom {
// A v8::Persistent handle to a V8 value which destroys and clears the
// underlying handle on destruction.
template <typename T>
class ScopedPersistent {
public:
ScopedPersistent() {
}
explicit ScopedPersistent(v8::Handle<T> handle) {
reset(handle);
}
~ScopedPersistent() {
reset();
}
void reset(v8::Handle<T> handle) {
if (!handle.IsEmpty())
handle_.Reset(GetIsolate(handle), handle);
else
reset();
}
void reset() {
handle_.Reset();
}
bool IsEmpty() const {
return handle_.IsEmpty();
}
v8::Handle<T> NewHandle() const {
if (handle_.IsEmpty())
return v8::Local<T>();
return v8::Local<T>::New(GetIsolate(handle_), handle_);
}
v8::Handle<T> NewHandle(v8::Isolate* isolate) const {
if (handle_.IsEmpty())
return v8::Local<T>();
return v8::Local<T>::New(isolate, handle_);
}
template <typename P>
void MakeWeak(P* parameters,
typename v8::WeakReferenceCallbacks<T, P>::Revivable callback) {
handle_.MakeWeak(parameters, callback);
}
private:
template <typename U>
static v8::Isolate* GetIsolate(v8::Handle<U> object_handle) {
// Only works for v8::Object and its subclasses. Add specialisations for
// anything else.
if (!object_handle.IsEmpty())
return GetIsolate(object_handle->CreationContext());
return v8::Isolate::GetCurrent();
}
static v8::Isolate* GetIsolate(v8::Handle<v8::Context> context_handle) {
if (!context_handle.IsEmpty())
return context_handle->GetIsolate();
return v8::Isolate::GetCurrent();
}
static v8::Isolate* GetIsolate(
v8::Handle<v8::ObjectTemplate> template_handle) {
return v8::Isolate::GetCurrent();
}
template <typename U>
static v8::Isolate* GetIsolate(const U& any_handle) {
return v8::Isolate::GetCurrent();
}
v8::Persistent<T> handle_;
DISALLOW_COPY_AND_ASSIGN(ScopedPersistent);
};
template <typename T>
class RefCountedPersistent : public ScopedPersistent<T>,
public base::RefCounted<RefCountedPersistent<T>> {
public:
RefCountedPersistent() {}
explicit RefCountedPersistent(v8::Handle<T> handle)
: ScopedPersistent<T>(handle) {
}
protected:
friend class base::RefCounted<RefCountedPersistent<T>>;
~RefCountedPersistent() {}
private:
DISALLOW_COPY_AND_ASSIGN(RefCountedPersistent);
};
typedef scoped_refptr<RefCountedPersistent<v8::Function>> RefCountedV8Function;
typedef scoped_refptr<RefCountedPersistent<v8::Object>> RefCountedV8Object;
} // namespace atom
#endif // ATOM_COMMON_V8_SCOPED_PERSISTENT_H_