Add process.scheduleCallback.

It will schedule a callback to be executed by pure uv loop.
This commit is contained in:
Cheng Zhao 2014-01-09 20:33:07 +08:00
parent a12754f980
commit d0494024a9
2 changed files with 30 additions and 3 deletions

View file

@ -16,13 +16,29 @@ namespace {
static int kMaxCallStackSize = 200; // Same with WebKit. static int kMaxCallStackSize = 200; // Same with WebKit.
static uv_async_t dummy_uv_handle; // Async handle to wake up uv loop.
static uv_async_t g_dummy_uv_handle;
// Async handle to execute the stored v8 callback.
static uv_async_t g_callback_uv_handle;
// Stored v8 callback, to be called by the async handler.
RefCountedV8Function g_v8_callback;
// Dummy class type that used for crashing the program.
struct DummyClass { bool crash; }; struct DummyClass { bool crash; };
// Dummy async handler that does nothing.
void UvNoOp(uv_async_t* handle, int status) { void UvNoOp(uv_async_t* handle, int status) {
} }
// Async handler to execute the stored v8 callback.
void UvOnCallback(uv_async_t* handle, int status) {
v8::HandleScope handle_scope(node_isolate);
v8::Handle<v8::Object> global = v8::Context::GetCurrent()->Global();
g_v8_callback->NewHandle()->Call(global, 0, NULL);
}
v8::Handle<v8::Object> DumpStackFrame(v8::Handle<v8::StackFrame> stack_frame) { v8::Handle<v8::Object> DumpStackFrame(v8::Handle<v8::StackFrame> stack_frame) {
v8::Local<v8::Object> result = v8::Object::New(); v8::Local<v8::Object> result = v8::Object::New();
result->Set(ToV8Value("line"), ToV8Value(stack_frame->GetLineNumber())); result->Set(ToV8Value("line"), ToV8Value(stack_frame->GetLineNumber()));
@ -44,7 +60,8 @@ v8::Handle<v8::Object> DumpStackFrame(v8::Handle<v8::StackFrame> stack_frame) {
node::node_module_struct* GetBuiltinModule(const char *name, bool is_browser); node::node_module_struct* GetBuiltinModule(const char *name, bool is_browser);
AtomBindings::AtomBindings() { AtomBindings::AtomBindings() {
uv_async_init(uv_default_loop(), &dummy_uv_handle, UvNoOp); uv_async_init(uv_default_loop(), &g_dummy_uv_handle, UvNoOp);
uv_async_init(uv_default_loop(), &g_callback_uv_handle, UvOnCallback);
} }
AtomBindings::~AtomBindings() { AtomBindings::~AtomBindings() {
@ -58,6 +75,7 @@ void AtomBindings::BindTo(v8::Handle<v8::Object> process) {
NODE_SET_METHOD(process, "activateUvLoop", ActivateUVLoop); NODE_SET_METHOD(process, "activateUvLoop", ActivateUVLoop);
NODE_SET_METHOD(process, "log", Log); NODE_SET_METHOD(process, "log", Log);
NODE_SET_METHOD(process, "getCurrentStackTrace", GetCurrentStackTrace); NODE_SET_METHOD(process, "getCurrentStackTrace", GetCurrentStackTrace);
NODE_SET_METHOD(process, "scheduleCallback", ScheduleCallback);
process->Get(v8::String::New("versions"))->ToObject()-> process->Get(v8::String::New("versions"))->ToObject()->
Set(v8::String::New("atom-shell"), v8::String::New(ATOM_VERSION_STRING)); Set(v8::String::New("atom-shell"), v8::String::New(ATOM_VERSION_STRING));
@ -115,7 +133,7 @@ void AtomBindings::Crash(const v8::FunctionCallbackInfo<v8::Value>& args) {
// static // static
void AtomBindings::ActivateUVLoop( void AtomBindings::ActivateUVLoop(
const v8::FunctionCallbackInfo<v8::Value>& args) { const v8::FunctionCallbackInfo<v8::Value>& args) {
uv_async_send(&dummy_uv_handle); uv_async_send(&g_dummy_uv_handle);
} }
// static // static
@ -146,4 +164,12 @@ void AtomBindings::GetCurrentStackTrace(
args.GetReturnValue().Set(result); args.GetReturnValue().Set(result);
} }
// static
void AtomBindings::ScheduleCallback(
const v8::FunctionCallbackInfo<v8::Value>& args) {
if (!FromV8Arguments(args, &g_v8_callback))
return node::ThrowTypeError("Bad arguments");
uv_async_send(&g_callback_uv_handle);
}
} // namespace atom } // namespace atom

View file

@ -26,6 +26,7 @@ class AtomBindings {
static void Log(const v8::FunctionCallbackInfo<v8::Value>& args); static void Log(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetCurrentStackTrace( static void GetCurrentStackTrace(
const v8::FunctionCallbackInfo<v8::Value>& args); const v8::FunctionCallbackInfo<v8::Value>& args);
static void ScheduleCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
DISALLOW_COPY_AND_ASSIGN(AtomBindings); DISALLOW_COPY_AND_ASSIGN(AtomBindings);
}; };