summaryrefslogtreecommitdiff
path: root/src/util-inl.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/util-inl.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/util-inl.h')
-rw-r--r--src/util-inl.h83
1 files changed, 83 insertions, 0 deletions
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 <class TypeName>
+inline v8::Local<TypeName> PersistentToLocal(
+ v8::Isolate* isolate,
+ const v8::Persistent<TypeName>& persistent) {
+ if (persistent.IsWeak()) {
+ return WeakPersistentToLocal(isolate, persistent);
+ } else {
+ return StrongPersistentToLocal(persistent);
+ }
+}
+
+template <class TypeName>
+inline v8::Local<TypeName> StrongPersistentToLocal(
+ const v8::Persistent<TypeName>& persistent) {
+ return *reinterpret_cast<v8::Local<TypeName>*>(
+ const_cast<v8::Persistent<TypeName>*>(&persistent));
+}
+
+template <class TypeName>
+inline v8::Local<TypeName> WeakPersistentToLocal(
+ v8::Isolate* isolate,
+ const v8::Persistent<TypeName>& persistent) {
+ return v8::Local<TypeName>::New(isolate, persistent);
+}
+
+inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
+ const char* data,
+ int length) {
+ return v8::String::NewFromOneByte(isolate,
+ reinterpret_cast<const uint8_t*>(data),
+ v8::String::kNormalString,
+ length);
+}
+
+inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
+ const signed char* data,
+ int length) {
+ return v8::String::NewFromOneByte(isolate,
+ reinterpret_cast<const uint8_t*>(data),
+ v8::String::kNormalString,
+ length);
+}
+
+inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
+ const unsigned char* data,
+ int length) {
+ return v8::String::NewFromOneByte(isolate,
+ reinterpret_cast<const uint8_t*>(data),
+ v8::String::kNormalString,
+ length);
+}
+
+} // namespace node
+
+#endif // SRC_UTIL_INL_H_