Remove all calls to node::MakeCallback

node::MakeCallback is doing too much for us, avoid calling it.
This commit is contained in:
Cheng Zhao 2015-06-23 20:14:03 +08:00
parent 78459b913b
commit 42e21d15bf
4 changed files with 32 additions and 13 deletions

View file

@ -21,15 +21,28 @@ v8::Local<v8::Value> CallEmitWithArgs(v8::Isolate* isolate,
} // namespace internal
// obj.emit(name, args...);
// obj.emit.apply(obj, name, args...);
// The caller is responsible of allocating a HandleScope.
template<typename... Args>
template<typename StringType, typename... Args>
v8::Local<v8::Value> EmitEvent(v8::Isolate* isolate,
v8::Local<v8::Object> obj,
const base::StringPiece& name,
const StringType& name,
const internal::ValueVector& args) {
internal::ValueVector concatenated_args = { StringToV8(isolate, name) };
concatenated_args.reserve(1 + args.size());
concatenated_args.insert(concatenated_args.end(), args.begin(), args.end());
return internal::CallEmitWithArgs(isolate, obj, &concatenated_args);
}
// obj.emit(name, args...);
// The caller is responsible of allocating a HandleScope.
template<typename StringType, typename... Args>
v8::Local<v8::Value> EmitEvent(v8::Isolate* isolate,
v8::Local<v8::Object> obj,
const StringType& name,
const Args&... args) {
internal::ValueVector converted_args = {
ConvertToV8(isolate, name),
StringToV8(isolate, name),
ConvertToV8(isolate, args)...,
};
return internal::CallEmitWithArgs(isolate, obj, &converted_args);

View file

@ -29,6 +29,12 @@ struct Converter<base::string16> {
}
};
inline v8::Local<v8::String> StringToV8(
v8::Isolate* isolate,
const base::string16& input) {
return ConvertToV8(isolate, input).As<v8::String>();
}
} // namespace mate
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_STRING16_CONVERTER_H_

View file

@ -8,6 +8,7 @@
#include <vector>
#include "atom/common/atom_command_line.h"
#include "atom/common/event_emitter_caller.h"
#include "atom/common/native_mate_converters/file_path_converter.h"
#include "base/command_line.h"
#include "base/base_paths.h"
@ -185,8 +186,7 @@ void NodeBindings::LoadEnvironment(node::Environment* env) {
if (node::use_debug_agent)
node::EnableDebug(env);
v8::Local<v8::Value> msg = mate::StringToV8(env->isolate(), "loaded");
node::MakeCallback(env->isolate(), env->process_object(), "emit", 1, &msg);
mate::EmitEvent(env->isolate(), env->process_object(), "loaded");
}
void NodeBindings::PrepareMessageLoop() {

View file

@ -7,8 +7,11 @@
#include <string>
#include <vector>
#include "atom/common/api/api_messages.h"
// Put this before event_emitter_caller.h to have string16 support.
#include "atom/common/native_mate_converters/string16_converter.h"
#include "atom/common/api/api_messages.h"
#include "atom/common/event_emitter_caller.h"
#include "atom/common/native_mate_converters/value_converter.h"
#include "atom/common/options_switches.h"
#include "atom/renderer/atom_renderer_client.h"
@ -134,13 +137,10 @@ void AtomRenderViewObserver::OnBrowserMessage(const base::string16& channel,
v8::Local<v8::Context> context = frame->mainWorldScriptContext();
v8::Context::Scope context_scope(context);
std::vector<v8::Local<v8::Value>> arguments = ListValueToVector(
isolate, args);
arguments.insert(arguments.begin(), mate::ConvertToV8(isolate, channel));
v8::Local<v8::Object> ipc;
if (GetIPCObject(isolate, context, &ipc))
node::MakeCallback(isolate, ipc, "emit", arguments.size(), &arguments[0]);
if (GetIPCObject(isolate, context, &ipc)) {
mate::EmitEvent(isolate, ipc, channel, ListValueToVector(isolate, args));
}
}
} // namespace atom