Simplify atom_bindings

This commit is contained in:
Cheng Zhao 2015-01-23 21:33:40 -08:00
parent 76fc62808c
commit a755edebfa
2 changed files with 5 additions and 34 deletions

View file

@ -11,7 +11,7 @@
#include "atom/common/chrome_version.h" #include "atom/common/chrome_version.h"
#include "atom/common/native_mate_converters/string16_converter.h" #include "atom/common/native_mate_converters/string16_converter.h"
#include "base/logging.h" #include "base/logging.h"
#include "native_mate/callback.h" #include "base/debug/debugger.h"
#include "native_mate/dictionary.h" #include "native_mate/dictionary.h"
#include "atom/common/node_includes.h" #include "atom/common/node_includes.h"
@ -20,22 +20,8 @@ namespace atom {
namespace { namespace {
// Async handle to execute the stored v8 callback.
uv_async_t g_callback_uv_handle;
// Stored v8 callback, to be called by the async handler.
base::Closure g_v8_callback;
// Dummy class type that used for crashing the program.
struct DummyClass { bool crash; };
// Async handler to execute the stored v8 callback.
void UvOnCallback(uv_async_t* handle) {
g_v8_callback.Run();
}
void Crash() { void Crash() {
static_cast<DummyClass*>(NULL)->crash = true; base::debug::BreakDebugger();
} }
// Called when there is a fatal error in V8, we just crash the process here so // Called when there is a fatal error in V8, we just crash the process here so
@ -49,19 +35,12 @@ void Log(const base::string16& message) {
logging::LogMessage("CONSOLE", 0, 0).stream() << message; logging::LogMessage("CONSOLE", 0, 0).stream() << message;
} }
void ScheduleCallback(const base::Closure& callback) {
g_v8_callback = callback;
uv_async_send(&g_callback_uv_handle);
}
} // namespace } // namespace
AtomBindings::AtomBindings() { AtomBindings::AtomBindings() {
uv_async_init(uv_default_loop(), &call_next_tick_async_, OnCallNextTick); uv_async_init(uv_default_loop(), &call_next_tick_async_, OnCallNextTick);
call_next_tick_async_.data = this; call_next_tick_async_.data = this;
uv_async_init(uv_default_loop(), &g_callback_uv_handle, UvOnCallback);
} }
AtomBindings::~AtomBindings() { AtomBindings::~AtomBindings() {
@ -74,16 +53,13 @@ void AtomBindings::BindTo(v8::Isolate* isolate,
mate::Dictionary dict(isolate, process); mate::Dictionary dict(isolate, process);
dict.SetMethod("crash", &Crash); dict.SetMethod("crash", &Crash);
dict.SetMethod("log", &Log); dict.SetMethod("log", &Log);
dict.SetMethod("scheduleCallback", &ScheduleCallback);
dict.SetMethod("activateUvLoop", dict.SetMethod("activateUvLoop",
base::Bind(&AtomBindings::ActivateUVLoop, base::Unretained(this))); base::Bind(&AtomBindings::ActivateUVLoop, base::Unretained(this)));
v8::Handle<v8::Object> versions; mate::Dictionary versions;
if (dict.Get("versions", &versions)) { if (dict.Get("versions", &versions)) {
versions->Set(mate::StringToV8(isolate, "atom-shell"), versions.Set("atom-shell", ATOM_VERSION_STRING);
mate::StringToV8(isolate, ATOM_VERSION_STRING)); versions.Set("chrome", CHROME_VERSION_STRING);
versions->Set(mate::StringToV8(isolate, "chrome"),
mate::StringToV8(isolate, CHROME_VERSION_STRING));
} }
} }

View file

@ -54,11 +54,6 @@ describe 'node feature', ->
fs.readFile __filename, -> fs.readFile __filename, ->
setTimeout done, 0 setTimeout done, 0
describe 'setTimeout in pure uv callback', ->
it 'does not crash', (done) ->
process.scheduleCallback ->
setTimeout done, 0
describe 'throw error in node context', -> describe 'throw error in node context', ->
it 'gets caught', (done) -> it 'gets caught', (done) ->
error = new Error('boo!') error = new Error('boo!')