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/node_stat_watcher.cc | |
parent | 81655a224a36948291e1f21f03273b5b604b7e6a (diff) | |
download | android-node-v8-756b6222956b5d25b2e7db81f4e79033a3a4d20e.tar.gz android-node-v8-756b6222956b5d25b2e7db81f4e79033a3a4d20e.tar.bz2 android-node-v8-756b6222956b5d25b2e7db81f4e79033a3a4d20e.zip |
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/node_stat_watcher.cc')
-rw-r--r-- | src/node_stat_watcher.cc | 49 |
1 files changed, 26 insertions, 23 deletions
diff --git a/src/node_stat_watcher.cc b/src/node_stat_watcher.cc index fb3cf4f8aa..1cd13c0847 100644 --- a/src/node_stat_watcher.cc +++ b/src/node_stat_watcher.cc @@ -20,6 +20,8 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. #include "node_stat_watcher.h" +#include "env.h" +#include "env-inl.h" #include <assert.h> #include <string.h> @@ -27,6 +29,7 @@ namespace node { +using v8::Context; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::Handle; @@ -37,9 +40,6 @@ using v8::Object; using v8::String; using v8::Value; -static Cached<String> onchange_sym; -static Cached<String> onstop_sym; - void StatWatcher::Initialize(Handle<Object> target) { HandleScope scope(node_isolate); @@ -61,9 +61,11 @@ static void Delete(uv_handle_t* handle) { } -StatWatcher::StatWatcher() : ObjectWrap(), - watcher_(new uv_fs_poll_t) { - uv_fs_poll_init(uv_default_loop(), watcher_); +StatWatcher::StatWatcher(Environment* env) + : ObjectWrap() + , watcher_(new uv_fs_poll_t) + , env_(env) { + uv_fs_poll_init(env->event_loop(), watcher_); watcher_->data = static_cast<void*>(this); } @@ -80,16 +82,17 @@ void StatWatcher::Callback(uv_fs_poll_t* handle, const uv_stat_t* curr) { StatWatcher* wrap = static_cast<StatWatcher*>(handle->data); assert(wrap->watcher_ == handle); - HandleScope scope(node_isolate); - Local<Value> argv[3]; - argv[0] = BuildStatsObject(curr); - argv[1] = BuildStatsObject(prev); - argv[2] = Integer::New(status, node_isolate); - if (onchange_sym.IsEmpty()) { - onchange_sym = FIXED_ONE_BYTE_STRING(node_isolate, "onchange"); - } - MakeCallback(wrap->handle(node_isolate), - onchange_sym, + Environment* env = wrap->env(); + Context::Scope context_scope(env->context()); + HandleScope handle_scope(env->isolate()); + Local<Value> argv[] = { + BuildStatsObject(env, curr), + BuildStatsObject(env, prev), + Integer::New(status, node_isolate) + }; + MakeCallback(env, + wrap->handle(node_isolate), + env->onchange_string(), ARRAY_SIZE(argv), argv); } @@ -97,8 +100,9 @@ void StatWatcher::Callback(uv_fs_poll_t* handle, void StatWatcher::New(const FunctionCallbackInfo<Value>& args) { assert(args.IsConstructCall()); - HandleScope scope(node_isolate); - StatWatcher* s = new StatWatcher(); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope handle_scope(args.GetIsolate()); + StatWatcher* s = new StatWatcher(env); s->Wrap(args.This()); } @@ -119,12 +123,11 @@ void StatWatcher::Start(const FunctionCallbackInfo<Value>& args) { void StatWatcher::Stop(const FunctionCallbackInfo<Value>& args) { - HandleScope scope(node_isolate); StatWatcher* wrap = ObjectWrap::Unwrap<StatWatcher>(args.This()); - if (onstop_sym.IsEmpty()) { - onstop_sym = FIXED_ONE_BYTE_STRING(node_isolate, "onstop"); - } - MakeCallback(wrap->handle(node_isolate), onstop_sym, 0, NULL); + Environment* env = wrap->env(); + Context::Scope context_scope(env->context()); + HandleScope handle_scope(env->isolate()); + MakeCallback(env, wrap->handle(node_isolate), env->onstop_string()); wrap->Stop(); } |