diff options
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_ |