summaryrefslogtreecommitdiff
path: root/src/process_wrap.cc
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/process_wrap.cc
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/process_wrap.cc')
-rw-r--r--src/process_wrap.cc55
1 files changed, 30 insertions, 25 deletions
diff --git a/src/process_wrap.cc b/src/process_wrap.cc
index 1784a163da..bff4e0ba86 100644
--- a/src/process_wrap.cc
+++ b/src/process_wrap.cc
@@ -19,7 +19,8 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
-#include "node.h"
+#include "env.h"
+#include "env-inl.h"
#include "handle_wrap.h"
#include "node_wrap.h"
@@ -29,6 +30,7 @@
namespace node {
using v8::Array;
+using v8::Context;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
@@ -41,13 +43,11 @@ using v8::Object;
using v8::String;
using v8::Value;
-static Cached<String> onexit_sym;
-
class ProcessWrap : public HandleWrap {
public:
- static void Initialize(Handle<Object> target) {
- HandleScope scope(node_isolate);
-
+ static void Initialize(Handle<Object> target,
+ Handle<Value> unused,
+ Handle<Context> context) {
Local<FunctionTemplate> constructor = FunctionTemplate::New(New);
constructor->InstanceTemplate()->SetInternalFieldCount(1);
constructor->SetClassName(FIXED_ONE_BYTE_STRING(node_isolate, "Process"));
@@ -70,22 +70,25 @@ class ProcessWrap : public HandleWrap {
// Therefore we assert that we are not trying to call this as a
// normal function.
assert(args.IsConstructCall());
- HandleScope scope(node_isolate);
- new ProcessWrap(args.This());
+ Environment* env = Environment::GetCurrent(args.GetIsolate());
+ HandleScope handle_scope(args.GetIsolate());
+ new ProcessWrap(env, args.This());
}
- explicit ProcessWrap(Handle<Object> object)
- : HandleWrap(object, reinterpret_cast<uv_handle_t*>(&process_)) {
+ ProcessWrap(Environment* env, Handle<Object> object)
+ : HandleWrap(env, object, reinterpret_cast<uv_handle_t*>(&process_)) {
}
~ProcessWrap() {
}
- static void ParseStdioOptions(Local<Object> js_options,
+ static void ParseStdioOptions(Environment* env,
+ Local<Object> js_options,
uv_process_options_t* options) {
Local<String> stdio_key =
FIXED_ONE_BYTE_STRING(node_isolate, "stdio");
Local<Array> stdios = js_options->Get(stdio_key).As<Array>();
+
uint32_t len = stdios->Length();
options->stdio = new uv_stdio_container_t[len];
options->stdio_count = len;
@@ -110,7 +113,7 @@ class ProcessWrap : public HandleWrap {
Local<String> handle_key =
FIXED_ONE_BYTE_STRING(node_isolate, "handle");
Local<Object> handle = stdio->Get(handle_key).As<Object>();
- uv_stream_t* stream = HandleToStream(handle);
+ uv_stream_t* stream = HandleToStream(env, handle);
assert(stream != NULL);
options->stdio[i].flags = UV_INHERIT_STREAM;
@@ -118,7 +121,6 @@ class ProcessWrap : public HandleWrap {
} else {
Local<String> fd_key = FIXED_ONE_BYTE_STRING(node_isolate, "fd");
int fd = static_cast<int>(stdio->Get(fd_key)->IntegerValue());
-
options->stdio[i].flags = UV_INHERIT_FD;
options->stdio[i].data.fd = fd;
}
@@ -126,7 +128,8 @@ class ProcessWrap : public HandleWrap {
}
static void Spawn(const FunctionCallbackInfo<Value>& args) {
- HandleScope scope(node_isolate);
+ Environment* env = Environment::GetCurrent(args.GetIsolate());
+ HandleScope handle_scope(args.GetIsolate());
ProcessWrap* wrap;
NODE_UNWRAP(args.This(), ProcessWrap, wrap);
@@ -216,7 +219,7 @@ class ProcessWrap : public HandleWrap {
}
// options.stdio
- ParseStdioOptions(js_options, &options);
+ ParseStdioOptions(env, js_options, &options);
// options.windows_verbatim_arguments
Local<String> windows_verbatim_arguments_key =
@@ -232,7 +235,7 @@ class ProcessWrap : public HandleWrap {
options.flags |= UV_PROCESS_DETACHED;
}
- int err = uv_spawn(uv_default_loop(), &wrap->process_, &options);
+ int err = uv_spawn(env->event_loop(), &wrap->process_, &options);
if (err == 0) {
assert(wrap->process_.data == wrap);
@@ -268,22 +271,24 @@ class ProcessWrap : public HandleWrap {
static void OnExit(uv_process_t* handle,
int64_t exit_status,
int term_signal) {
- HandleScope scope(node_isolate);
-
ProcessWrap* wrap = static_cast<ProcessWrap*>(handle->data);
- assert(wrap);
+ assert(wrap != NULL);
assert(&wrap->process_ == handle);
+ Environment* env = wrap->env();
+ Context::Scope context_scope(env->context());
+ HandleScope handle_scope(env->isolate());
+
Local<Value> argv[] = {
Number::New(node_isolate, static_cast<double>(exit_status)),
OneByteString(node_isolate, signo_string(term_signal))
};
- if (onexit_sym.IsEmpty()) {
- onexit_sym = FIXED_ONE_BYTE_STRING(node_isolate, "onexit");
- }
-
- MakeCallback(wrap->object(), onexit_sym, ARRAY_SIZE(argv), argv);
+ MakeCallback(env,
+ wrap->object(),
+ env->onexit_string(),
+ ARRAY_SIZE(argv),
+ argv);
}
uv_process_t process_;
@@ -292,4 +297,4 @@ class ProcessWrap : public HandleWrap {
} // namespace node
-NODE_MODULE(node_process_wrap, node::ProcessWrap::Initialize)
+NODE_MODULE_CONTEXT_AWARE(node_process_wrap, node::ProcessWrap::Initialize)