b0264fbbd2aa5c5d881413027c6077feaf01ec2a
[platform/upstream/nodejs.git] / src / util-inl.h
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
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:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
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.
21
22 #ifndef SRC_UTIL_INL_H_
23 #define SRC_UTIL_INL_H_
24
25 #include "util.h"
26
27 namespace node {
28
29 template <typename Inner, typename Outer>
30 ContainerOfHelper<Inner, Outer>::ContainerOfHelper(Inner Outer::*field,
31                                                    Inner* pointer)
32     : pointer_(reinterpret_cast<Outer*>(
33           reinterpret_cast<uintptr_t>(pointer) -
34           reinterpret_cast<uintptr_t>(&(static_cast<Outer*>(0)->*field)))) {
35 }
36
37 template <typename Inner, typename Outer>
38 template <typename TypeName>
39 ContainerOfHelper<Inner, Outer>::operator TypeName*() const {
40   return static_cast<TypeName*>(pointer_);
41 }
42
43 template <typename Inner, typename Outer>
44 inline ContainerOfHelper<Inner, Outer> ContainerOf(Inner Outer::*field,
45                                                    Inner* pointer) {
46   return ContainerOfHelper<Inner, Outer>(field, pointer);
47 }
48
49 template <class TypeName>
50 inline v8::Local<TypeName> PersistentToLocal(
51     v8::Isolate* isolate,
52     const v8::Persistent<TypeName>& persistent) {
53   if (persistent.IsWeak()) {
54     return WeakPersistentToLocal(isolate, persistent);
55   } else {
56     return StrongPersistentToLocal(persistent);
57   }
58 }
59
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));
65 }
66
67 template <class TypeName>
68 inline v8::Local<TypeName> WeakPersistentToLocal(
69     v8::Isolate* isolate,
70     const v8::Persistent<TypeName>& persistent) {
71   return v8::Local<TypeName>::New(isolate, persistent);
72 }
73
74 inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
75                                            const char* data,
76                                            int length) {
77   return v8::String::NewFromOneByte(isolate,
78                                     reinterpret_cast<const uint8_t*>(data),
79                                     v8::String::kNormalString,
80                                     length);
81 }
82
83 inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
84                                            const signed char* data,
85                                            int length) {
86   return v8::String::NewFromOneByte(isolate,
87                                     reinterpret_cast<const uint8_t*>(data),
88                                     v8::String::kNormalString,
89                                     length);
90 }
91
92 inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
93                                            const unsigned char* data,
94                                            int length) {
95   return v8::String::NewFromOneByte(isolate,
96                                     reinterpret_cast<const uint8_t*>(data),
97                                     v8::String::kNormalString,
98                                     length);
99 }
100
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);
106 }
107
108 void ClearWrap(v8::Local<v8::Object> object) {
109   Wrap<void>(object, nullptr);
110 }
111
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);
118 }
119
120 }  // namespace node
121
122 #endif  // SRC_UTIL_INL_H_