From 756b6222956b5d25b2e7db81f4e79033a3a4d20e Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sun, 11 Aug 2013 00:26:11 +0200 Subject: 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. --- src/util-inl.h | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/util-inl.h (limited to 'src/util-inl.h') diff --git a/src/util-inl.h b/src/util-inl.h new file mode 100644 index 0000000000..1ea31d94db --- /dev/null +++ b/src/util-inl.h @@ -0,0 +1,83 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +#ifndef SRC_UTIL_INL_H_ +#define SRC_UTIL_INL_H_ + +#include "util.h" + +namespace node { + +template +inline v8::Local PersistentToLocal( + v8::Isolate* isolate, + const v8::Persistent& persistent) { + if (persistent.IsWeak()) { + return WeakPersistentToLocal(isolate, persistent); + } else { + return StrongPersistentToLocal(persistent); + } +} + +template +inline v8::Local StrongPersistentToLocal( + const v8::Persistent& persistent) { + return *reinterpret_cast*>( + const_cast*>(&persistent)); +} + +template +inline v8::Local WeakPersistentToLocal( + v8::Isolate* isolate, + const v8::Persistent& persistent) { + return v8::Local::New(isolate, persistent); +} + +inline v8::Local OneByteString(v8::Isolate* isolate, + const char* data, + int length) { + return v8::String::NewFromOneByte(isolate, + reinterpret_cast(data), + v8::String::kNormalString, + length); +} + +inline v8::Local OneByteString(v8::Isolate* isolate, + const signed char* data, + int length) { + return v8::String::NewFromOneByte(isolate, + reinterpret_cast(data), + v8::String::kNormalString, + length); +} + +inline v8::Local OneByteString(v8::Isolate* isolate, + const unsigned char* data, + int length) { + return v8::String::NewFromOneByte(isolate, + reinterpret_cast(data), + v8::String::kNormalString, + length); +} + +} // namespace node + +#endif // SRC_UTIL_INL_H_ -- cgit v1.2.3