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);
};