src: deduplicate CHECK_EQ/CHECK_NE macros
[platform/upstream/nodejs.git] / src / node_internals.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_NODE_INTERNALS_H_
23 #define SRC_NODE_INTERNALS_H_
24
25 #include "node.h"
26 #include "env.h"
27 #include "env-inl.h"
28 #include "util.h"
29 #include "util-inl.h"
30 #include "uv.h"
31 #include "v8.h"
32
33 #include <assert.h>
34 #include <stdint.h>
35 #include <stdlib.h>
36
37 struct sockaddr;
38
39 namespace node {
40
41 // If persistent.IsWeak() == false, then do not call persistent.Reset()
42 // while the returned Local<T> is still in scope, it will destroy the
43 // reference to the object.
44 template <class TypeName>
45 inline v8::Local<TypeName> PersistentToLocal(
46     v8::Isolate* isolate,
47     const v8::Persistent<TypeName>& persistent);
48
49 // Call with valid HandleScope and while inside Context scope.
50 v8::Handle<v8::Value> MakeCallback(Environment* env,
51                                    v8::Handle<v8::Object> recv,
52                                    const char* method,
53                                    int argc = 0,
54                                    v8::Handle<v8::Value>* argv = NULL);
55
56 // Call with valid HandleScope and while inside Context scope.
57 v8::Handle<v8::Value> MakeCallback(Environment* env,
58                                    v8::Handle<v8::Object> recv,
59                                    uint32_t index,
60                                    int argc = 0,
61                                    v8::Handle<v8::Value>* argv = NULL);
62
63 // Call with valid HandleScope and while inside Context scope.
64 v8::Handle<v8::Value> MakeCallback(Environment* env,
65                                    v8::Handle<v8::Object> recv,
66                                    v8::Handle<v8::String> symbol,
67                                    int argc = 0,
68                                    v8::Handle<v8::Value>* argv = NULL);
69
70 // Call with valid HandleScope and while inside Context scope.
71 v8::Handle<v8::Value> MakeCallback(Environment* env,
72                                    v8::Handle<v8::Value> recv,
73                                    v8::Handle<v8::Function> callback,
74                                    int argc = 0,
75                                    v8::Handle<v8::Value>* argv = NULL);
76
77 // Convert a struct sockaddr to a { address: '1.2.3.4', port: 1234 } JS object.
78 // Sets address and port properties on the info object and returns it.
79 // If |info| is omitted, a new object is returned.
80 v8::Local<v8::Object> AddressToJS(
81     Environment* env,
82     const sockaddr* addr,
83     v8::Local<v8::Object> info = v8::Handle<v8::Object>());
84
85 #ifdef _WIN32
86 // emulate snprintf() on windows, _snprintf() doesn't zero-terminate the buffer
87 // on overflow...
88 #include <stdarg.h>
89 inline static int snprintf(char* buf, unsigned int len, const char* fmt, ...) {
90   va_list ap;
91   va_start(ap, fmt);
92   int n = _vsprintf_p(buf, len, fmt, ap);
93   if (len)
94     buf[len - 1] = '\0';
95   va_end(ap);
96   return n;
97 }
98 #endif
99
100 #if defined(__x86_64__)
101 # define BITS_PER_LONG 64
102 #else
103 # define BITS_PER_LONG 32
104 #endif
105
106 #ifndef ARRAY_SIZE
107 # define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
108 #endif
109
110 #ifndef ROUND_UP
111 # define ROUND_UP(a, b) ((a) % (b) ? ((a) + (b)) - ((a) % (b)) : (a))
112 #endif
113
114 #if defined(__GNUC__) && __GNUC__ >= 4
115 # define MUST_USE_RESULT __attribute__((warn_unused_result))
116 # define NO_RETURN __attribute__((noreturn))
117 #else
118 # define MUST_USE_RESULT
119 # define NO_RETURN
120 #endif
121
122 void AppendExceptionLine(Environment* env,
123                          v8::Handle<v8::Value> er,
124                          v8::Handle<v8::Message> message);
125
126 NO_RETURN void FatalError(const char* location, const char* message);
127
128 v8::Local<v8::Object> BuildStatsObject(Environment* env, const uv_stat_t* s);
129
130 enum Endianness {
131   kLittleEndian,  // _Not_ LITTLE_ENDIAN, clashes with endian.h.
132   kBigEndian
133 };
134
135 inline enum Endianness GetEndianness() {
136   // Constant-folded by the compiler.
137   const union {
138     uint8_t u8[2];
139     uint16_t u16;
140   } u = {
141     { 1, 0 }
142   };
143   return u.u16 == 1 ? kLittleEndian : kBigEndian;
144 }
145
146 inline bool IsLittleEndian() {
147   return GetEndianness() == kLittleEndian;
148 }
149
150 inline bool IsBigEndian() {
151   return GetEndianness() == kBigEndian;
152 }
153
154 // parse index for external array data
155 inline MUST_USE_RESULT bool ParseArrayIndex(v8::Handle<v8::Value> arg,
156                                             size_t def,
157                                             size_t* ret) {
158   if (arg->IsUndefined()) {
159     *ret = def;
160     return true;
161   }
162
163   int32_t tmp_i = arg->Int32Value();
164
165   if (tmp_i < 0)
166     return false;
167
168   *ret = static_cast<size_t>(tmp_i);
169   return true;
170 }
171
172 NODE_DEPRECATED("Use env->ThrowError()",
173                 inline void ThrowError(const char* errmsg) {
174   Environment* env = Environment::GetCurrent(v8::Isolate::GetCurrent());
175   return env->ThrowError(errmsg);
176 })
177 NODE_DEPRECATED("Use env->ThrowTypeError()",
178                 inline void ThrowTypeError(const char* errmsg) {
179   Environment* env = Environment::GetCurrent(v8::Isolate::GetCurrent());
180   return env->ThrowTypeError(errmsg);
181 })
182 NODE_DEPRECATED("Use env->ThrowRangeError()",
183                 inline void ThrowRangeError(const char* errmsg) {
184   Environment* env = Environment::GetCurrent(v8::Isolate::GetCurrent());
185   return env->ThrowRangeError(errmsg);
186 })
187 NODE_DEPRECATED("Use env->ThrowErrnoException()",
188                 inline void ThrowErrnoException(int errorno,
189                                                 const char* syscall = NULL,
190                                                 const char* message = NULL,
191                                                 const char* path = NULL) {
192   Environment* env = Environment::GetCurrent(v8::Isolate::GetCurrent());
193   return env->ThrowErrnoException(errorno, syscall, message, path);
194 })
195 NODE_DEPRECATED("Use env->ThrowUVException()",
196                 inline void ThrowUVException(int errorno,
197                                                 const char* syscall = NULL,
198                                                 const char* message = NULL,
199                                                 const char* path = NULL) {
200   Environment* env = Environment::GetCurrent(v8::Isolate::GetCurrent());
201   return env->ThrowUVException(errorno, syscall, message, path);
202 })
203
204 }  // namespace node
205
206 #endif  // SRC_NODE_INTERNALS_H_