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/util.h | |
parent | 81655a224a36948291e1f21f03273b5b604b7e6a (diff) | |
download | node-new-756b6222956b5d25b2e7db81f4e79033a3a4d20e.tar.gz |
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.h')
-rw-r--r-- | src/util.h | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000000..7e71f36c70 --- /dev/null +++ b/src/util.h @@ -0,0 +1,82 @@ +// 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_H_ +#define SRC_UTIL_H_ + +#include "v8.h" +#include <stddef.h> + +namespace node { + +#define OFFSET_OF(TypeName, Field) \ + (reinterpret_cast<uintptr_t>(&(reinterpret_cast<TypeName*>(8)->Field)) - 8) + +#define CONTAINER_OF(Pointer, TypeName, Field) \ + reinterpret_cast<TypeName*>( \ + reinterpret_cast<uintptr_t>(Pointer) - OFFSET_OF(TypeName, Field)) + +#define FIXED_ONE_BYTE_STRING(isolate, string) \ + (node::OneByteString((isolate), (string), sizeof(string) - 1)) + +#define DISALLOW_COPY_AND_ASSIGN(TypeName) \ + void operator=(const TypeName&); \ + TypeName(const TypeName&) + +// If persistent.IsWeak() == false, then do not call persistent.Dispose() +// while the returned Local<T> is still in scope, it will destroy the +// reference to the object. +template <class TypeName> +inline v8::Local<TypeName> PersistentToLocal( + v8::Isolate* isolate, + const v8::Persistent<TypeName>& persistent); + +// Unchecked conversion from a non-weak Persistent<T> to Local<TLocal<T>, +// use with care! +// +// Do not call persistent.Dispose() while the returned Local<T> is still in +// scope, it will destroy the reference to the object. +template <class TypeName> +inline v8::Local<TypeName> StrongPersistentToLocal( + const v8::Persistent<TypeName>& persistent); + +template <class TypeName> +inline v8::Local<TypeName> WeakPersistentToLocal( + v8::Isolate* isolate, + const v8::Persistent<TypeName>& persistent); + +// Convenience wrapper around v8::String::NewFromOneByte(). +inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate, + const char* data, + int length = -1); + +// For the people that compile with -funsigned-char. +inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate, + const signed char* data, + int length = -1); + +inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate, + const unsigned char* data, + int length = -1); + +} // namespace node + +#endif // SRC_UTIL_H_ |