From edbec3f8f3e8f8239a9b1c9a9a0a229efcdb4938 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 23 Nov 2011 21:46:22 +0100 Subject: [PATCH] isolates: add _newIsolate() and _joinIsolate() to process object --- src/node.cc | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/node_isolate.cc | 8 +++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/node.cc b/src/node.cc index e80fec8..6352583 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1847,6 +1847,52 @@ static Handle Binding(const Arguments& args) { } +static void RunIsolate(void* arg) { + uv_loop_t* loop = uv_loop_new(); + Isolate* isolate = Isolate::New(loop); +} + + +static char magic_isolate_cookie_[] = "magic isolate cookie"; + + +static Handle NewIsolate(const Arguments& args) { + HandleScope scope; + + uv_thread_t* tid = new uv_thread_t; + + if (uv_thread_create(tid, RunIsolate, NULL)) + return Null(); + + Local tpl = ObjectTemplate::New(); + tpl->SetInternalFieldCount(2); + + Local obj = tpl->NewInstance(); + obj->SetPointerInInternalField(0, magic_isolate_cookie_); + obj->SetPointerInInternalField(1, tid); + + return scope.Close(obj); +} + + +static Handle JoinIsolate(const Arguments& args) { + HandleScope scope; + + assert(args[0]->IsObject()); + + Local obj = args[0]->ToObject(); + assert(obj->InternalFieldCount() == 2); + assert(obj->GetPointerFromInternalField(0) == magic_isolate_cookie_); + + uv_thread_t* tid = (uv_thread_t*) obj->GetPointerFromInternalField(1); + + if (uv_thread_join(tid)) + return False(); // error + else + return True(); // ok +} + + static Handle ProcessTitleGetter(Local property, const AccessorInfo& info) { HandleScope scope; @@ -2119,6 +2165,9 @@ Handle SetupProcessObject(int argc, char *argv[]) { NODE_SET_METHOD(process, "binding", Binding); + NODE_SET_METHOD(process, "_newIsolate", NewIsolate); + NODE_SET_METHOD(process, "_joinIsolate", JoinIsolate); + return process; } diff --git a/src/node_isolate.cc b/src/node_isolate.cc index 1255850..808a872 100644 --- a/src/node_isolate.cc +++ b/src/node_isolate.cc @@ -35,9 +35,15 @@ Isolate* Isolate::New(uv_loop_t* loop) { Isolate::Isolate(uv_loop_t* loop) { + SLIST_INIT(&at_exit_callbacks_); loop_ = loop; + isolate_ = v8::Isolate::GetCurrent(); - SLIST_INIT(&at_exit_callbacks_); + if (isolate_ == NULL) { + isolate_ = v8::Isolate::New(); + isolate_->Enter(); + } + assert(isolate_->GetData() == NULL); isolate_->SetData(this); } -- 2.7.4