diff options
author | Ben Noordhuis <info@bnoordhuis.nl> | 2013-08-11 00:26:11 +0200 |
---|---|---|
committer | Ben Noordhuis <info@bnoordhuis.nl> | 2013-09-06 05:51:42 +0200 |
commit | 756b6222956b5d25b2e7db81f4e79033a3a4d20e (patch) | |
tree | eb03b6f0bf33c36e011858c4a31b10a6eaadfc19 /src/udp_wrap.cc | |
parent | 81655a224a36948291e1f21f03273b5b604b7e6a (diff) | |
download | node-new-756b6222956b5d25b2e7db81f4e79033a3a4d20e.tar.gz |
src: add multi-context support
This commit makes it possible to use multiple V8 execution contexts
within a single event loop. Put another way, handle and request wrap
objects now "remember" the context they belong to and switch back to
that context when the time comes to call into JS land.
This could have been done in a quick and hacky way by calling
v8::Object::GetCreationContext() on the wrap object right before
making a callback but that leaves a fairly wide margin for bugs.
Instead, we make the context explicit through a new Environment class
that encapsulates everything (or almost everything) that belongs to
the context. Variables that used to be a static or a global are now
members of the aforementioned class. An additional benefit is that
this approach should make it relatively straightforward to add full
isolate support in due course.
There is no JavaScript API yet but that will be added in the near
future.
This work was graciously sponsored by GitHub, Inc.
Diffstat (limited to 'src/udp_wrap.cc')
-rw-r--r-- | src/udp_wrap.cc | 82 |
1 files changed, 46 insertions, 36 deletions
diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index 1e5d32269b..fd679ab4d8 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -20,7 +20,8 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. #include "udp_wrap.h" -#include "node.h" +#include "env.h" +#include "env-inl.h" #include "node_buffer.h" #include "handle_wrap.h" #include "req_wrap.h" @@ -30,6 +31,7 @@ namespace node { +using v8::Context; using v8::Function; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; @@ -38,7 +40,6 @@ using v8::HandleScope; using v8::Integer; using v8::Local; using v8::Object; -using v8::Persistent; using v8::PropertyAttribute; using v8::PropertyCallbackInfo; using v8::String; @@ -49,20 +50,17 @@ using v8::Value; class SendWrap : public ReqWrap<uv_udp_send_t> { public: - SendWrap(Local<Object> req_wrap_obj, bool have_callback); + SendWrap(Environment* env, Local<Object> req_wrap_obj, bool have_callback); inline bool have_callback() const; private: const bool have_callback_; }; -static Persistent<Function> constructor; -static Cached<String> oncomplete_sym; -static Cached<String> onmessage_sym; - - -SendWrap::SendWrap(Local<Object> req_wrap_obj, bool have_callback) - : ReqWrap<uv_udp_send_t>(req_wrap_obj) +SendWrap::SendWrap(Environment* env, + Local<Object> req_wrap_obj, + bool have_callback) + : ReqWrap<uv_udp_send_t>(env, req_wrap_obj) , have_callback_(have_callback) { } @@ -72,9 +70,9 @@ inline bool SendWrap::have_callback() const { } -UDPWrap::UDPWrap(Handle<Object> object) - : HandleWrap(object, reinterpret_cast<uv_handle_t*>(&handle_)) { - int r = uv_udp_init(uv_default_loop(), &handle_); +UDPWrap::UDPWrap(Environment* env, Handle<Object> object) + : HandleWrap(env, object, reinterpret_cast<uv_handle_t*>(&handle_)) { + int r = uv_udp_init(env->event_loop(), &handle_); assert(r == 0); // can't fail anyway } @@ -83,11 +81,10 @@ UDPWrap::~UDPWrap() { } -void UDPWrap::Initialize(Handle<Object> target) { - HandleScope scope(node_isolate); - - oncomplete_sym = FIXED_ONE_BYTE_STRING(node_isolate, "oncomplete"); - onmessage_sym = FIXED_ONE_BYTE_STRING(node_isolate, "onmessage"); +void UDPWrap::Initialize(Handle<Object> target, + Handle<Value> unused, + Handle<Context> context) { + Environment* env = Environment::GetCurrent(context); Local<FunctionTemplate> t = FunctionTemplate::New(New); t->InstanceTemplate()->SetInternalFieldCount(1); @@ -120,15 +117,16 @@ void UDPWrap::Initialize(Handle<Object> target) { NODE_SET_PROTOTYPE_METHOD(t, "ref", HandleWrap::Ref); NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref); - constructor.Reset(node_isolate, t->GetFunction()); target->Set(FIXED_ONE_BYTE_STRING(node_isolate, "UDP"), t->GetFunction()); + env->set_udp_constructor_function(t->GetFunction()); } void UDPWrap::New(const FunctionCallbackInfo<Value>& args) { - HandleScope scope(node_isolate); assert(args.IsConstructCall()); - new UDPWrap(args.This()); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope handle_scope(args.GetIsolate()); + new UDPWrap(env, args.This()); } @@ -244,7 +242,8 @@ void UDPWrap::DropMembership(const FunctionCallbackInfo<Value>& args) { void UDPWrap::DoSend(const FunctionCallbackInfo<Value>& args, int family) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope handle_scope(args.GetIsolate()); UDPWrap* wrap; NODE_UNWRAP(args.This(), UDPWrap, wrap); @@ -269,7 +268,7 @@ void UDPWrap::DoSend(const FunctionCallbackInfo<Value>& args, int family) { assert(offset < Buffer::Length(buffer_obj)); assert(length <= Buffer::Length(buffer_obj) - offset); - SendWrap* req_wrap = new SendWrap(req_wrap_obj, have_callback); + SendWrap* req_wrap = new SendWrap(env, req_wrap_obj, have_callback); uv_buf_t buf = uv_buf_init(Buffer::Data(buffer_obj) + offset, length); @@ -337,7 +336,9 @@ void UDPWrap::RecvStop(const FunctionCallbackInfo<Value>& args) { void UDPWrap::GetSockName(const FunctionCallbackInfo<Value>& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope handle_scope(args.GetIsolate()); + struct sockaddr_storage address; UDPWrap* wrap; NODE_UNWRAP(args.This(), UDPWrap, wrap); @@ -352,7 +353,7 @@ void UDPWrap::GetSockName(const FunctionCallbackInfo<Value>& args) { if (err == 0) { const sockaddr* addr = reinterpret_cast<const sockaddr*>(&address); - AddressToJS(addr, obj); + AddressToJS(env, addr, obj); } args.GetReturnValue().Set(err); @@ -363,10 +364,12 @@ void UDPWrap::GetSockName(const FunctionCallbackInfo<Value>& args) { void UDPWrap::OnSend(uv_udp_send_t* req, int status) { SendWrap* req_wrap = static_cast<SendWrap*>(req->data); if (req_wrap->have_callback()) { - HandleScope scope(node_isolate); + Environment* env = req_wrap->env(); + Context::Scope context_scope(env->context()); + HandleScope handle_scope(env->isolate()); Local<Object> req_wrap_obj = req_wrap->object(); Local<Value> arg = Integer::New(status, node_isolate); - MakeCallback(req_wrap_obj, oncomplete_sym, 1, &arg); + MakeCallback(env, req_wrap_obj, env->oncomplete_string(), 1, &arg); } delete req_wrap; } @@ -397,8 +400,11 @@ void UDPWrap::OnRecv(uv_udp_t* handle, } UDPWrap* wrap = static_cast<UDPWrap*>(handle->data); + Environment* env = wrap->env(); + + Context::Scope context_scope(env->context()); + HandleScope handle_scope(env->isolate()); - HandleScope scope(node_isolate); Local<Object> wrap_obj = wrap->object(); Local<Value> argv[] = { Integer::New(nread, node_isolate), @@ -410,14 +416,18 @@ void UDPWrap::OnRecv(uv_udp_t* handle, if (nread < 0) { if (buf->base != NULL) free(buf->base); - MakeCallback(wrap_obj, onmessage_sym, ARRAY_SIZE(argv), argv); + MakeCallback(env, + wrap_obj, + env->onmessage_string(), + ARRAY_SIZE(argv), + argv); return; } char* base = static_cast<char*>(realloc(buf->base, nread)); - argv[2] = Buffer::Use(base, nread); - argv[3] = AddressToJS(addr); - MakeCallback(wrap_obj, onmessage_sym, ARRAY_SIZE(argv), argv); + argv[2] = Buffer::Use(env, base, nread); + argv[3] = AddressToJS(env, addr); + MakeCallback(env, wrap_obj, env->onmessage_string(), ARRAY_SIZE(argv), argv); } @@ -428,10 +438,10 @@ UDPWrap* UDPWrap::Unwrap(Local<Object> obj) { } -Local<Object> UDPWrap::Instantiate() { +Local<Object> UDPWrap::Instantiate(Environment* env) { // If this assert fires then Initialize hasn't been called yet. - assert(constructor.IsEmpty() == false); - return NewInstance(constructor); + assert(env->udp_constructor_function().IsEmpty() == false); + return env->udp_constructor_function()->NewInstance(); } @@ -442,4 +452,4 @@ uv_udp_t* UDPWrap::UVHandle() { } // namespace node -NODE_MODULE(node_udp_wrap, node::UDPWrap::Initialize) +NODE_MODULE_CONTEXT_AWARE(node_udp_wrap, node::UDPWrap::Initialize) |