summaryrefslogtreecommitdiff
path: root/src/req_wrap.h
diff options
context:
space:
mode:
authorBen Noordhuis <info@bnoordhuis.nl>2013-08-11 00:26:11 +0200
committerBen Noordhuis <info@bnoordhuis.nl>2013-09-06 05:51:42 +0200
commit756b6222956b5d25b2e7db81f4e79033a3a4d20e (patch)
treeeb03b6f0bf33c36e011858c4a31b10a6eaadfc19 /src/req_wrap.h
parent81655a224a36948291e1f21f03273b5b604b7e6a (diff)
downloadandroid-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/req_wrap.h')
-rw-r--r--src/req_wrap.h40
1 files changed, 25 insertions, 15 deletions
diff --git a/src/req_wrap.h b/src/req_wrap.h
index 44749a3004..8a701e5158 100644
--- a/src/req_wrap.h
+++ b/src/req_wrap.h
@@ -22,29 +22,31 @@
#ifndef SRC_REQ_WRAP_H_
#define SRC_REQ_WRAP_H_
-#include "node.h"
-#include "node_internals.h"
+#include "env.h"
+#include "env-inl.h"
#include "queue.h"
+#include "util.h"
namespace node {
// defined in node.cc
-extern Cached<v8::String> process_symbol;
-extern Cached<v8::String> domain_symbol;
extern QUEUE req_wrap_queue;
template <typename T>
class ReqWrap {
public:
- ReqWrap(v8::Handle<v8::Object> object = v8::Handle<v8::Object>()) {
- v8::HandleScope scope(node_isolate);
- if (object.IsEmpty()) object = v8::Object::New();
- persistent().Reset(node_isolate, object);
-
- if (InDomain()) {
- v8::Local<v8::Value> domain = GetDomain();
- if (domain->IsObject())
- object->Set(domain_symbol, domain);
+ ReqWrap(Environment* env,
+ v8::Handle<v8::Object> object = v8::Handle<v8::Object>())
+ : env_(env) {
+ v8::HandleScope handle_scope(env->isolate());
+
+ if (object.IsEmpty()) {
+ object = v8::Object::New();
+ }
+ persistent().Reset(env->isolate(), object);
+
+ if (env->in_domain()) {
+ object->Set(env->domain_string(), env->domain_array()->Get(0));
}
QUEUE_INSERT_TAIL(&req_wrap_queue, &req_wrap_queue_);
@@ -64,17 +66,25 @@ class ReqWrap {
req_.data = this;
}
+ inline Environment* env() const {
+ return env_;
+ }
+
inline v8::Local<v8::Object> object() {
- return PersistentToLocal(node_isolate, persistent());
+ return PersistentToLocal(env()->isolate(), persistent());
}
inline v8::Persistent<v8::Object>& persistent() {
return object_;
}
- v8::Persistent<v8::Object> object_;
+ // TODO(bnoordhuis) Make these private.
QUEUE req_wrap_queue_;
T req_; // *must* be last, GetActiveRequests() in node.cc depends on it
+
+ private:
+ v8::Persistent<v8::Object> object_;
+ Environment* const env_;
};