1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #ifndef SRC_UTIL_INL_H_
23 #define SRC_UTIL_INL_H_
29 template <typename Inner, typename Outer>
30 ContainerOfHelper<Inner, Outer>::ContainerOfHelper(Inner Outer::*field,
32 : pointer_(reinterpret_cast<Outer*>(
33 reinterpret_cast<uintptr_t>(pointer) -
34 reinterpret_cast<uintptr_t>(&(static_cast<Outer*>(0)->*field)))) {
37 template <typename Inner, typename Outer>
38 template <typename TypeName>
39 ContainerOfHelper<Inner, Outer>::operator TypeName*() const {
40 return static_cast<TypeName*>(pointer_);
43 template <typename Inner, typename Outer>
44 inline ContainerOfHelper<Inner, Outer> ContainerOf(Inner Outer::*field,
46 return ContainerOfHelper<Inner, Outer>(field, pointer);
49 template <class TypeName>
50 inline v8::Local<TypeName> PersistentToLocal(
52 const v8::Persistent<TypeName>& persistent) {
53 if (persistent.IsWeak()) {
54 return WeakPersistentToLocal(isolate, persistent);
56 return StrongPersistentToLocal(persistent);
60 template <class TypeName>
61 inline v8::Local<TypeName> StrongPersistentToLocal(
62 const v8::Persistent<TypeName>& persistent) {
63 return *reinterpret_cast<v8::Local<TypeName>*>(
64 const_cast<v8::Persistent<TypeName>*>(&persistent));
67 template <class TypeName>
68 inline v8::Local<TypeName> WeakPersistentToLocal(
70 const v8::Persistent<TypeName>& persistent) {
71 return v8::Local<TypeName>::New(isolate, persistent);
74 inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
77 return v8::String::NewFromOneByte(isolate,
78 reinterpret_cast<const uint8_t*>(data),
79 v8::String::kNormalString,
83 inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
84 const signed char* data,
86 return v8::String::NewFromOneByte(isolate,
87 reinterpret_cast<const uint8_t*>(data),
88 v8::String::kNormalString,
92 inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
93 const unsigned char* data,
95 return v8::String::NewFromOneByte(isolate,
96 reinterpret_cast<const uint8_t*>(data),
97 v8::String::kNormalString,
101 template <typename TypeName>
102 void Wrap(v8::Local<v8::Object> object, TypeName* pointer) {
103 CHECK_EQ(false, object.IsEmpty());
104 CHECK_GT(object->InternalFieldCount(), 0);
105 object->SetAlignedPointerInInternalField(0, pointer);
108 void ClearWrap(v8::Local<v8::Object> object) {
109 Wrap<void>(object, nullptr);
112 template <typename TypeName>
113 TypeName* Unwrap(v8::Local<v8::Object> object) {
114 CHECK_EQ(false, object.IsEmpty());
115 CHECK_GT(object->InternalFieldCount(), 0);
116 void* pointer = object->GetAlignedPointerFromInternalField(0);
117 return static_cast<TypeName*>(pointer);
122 #endif // SRC_UTIL_INL_H_