Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 100 lines (83 sloc) 3.777 kb
09dc577 @bnoordhuis build: move internals to separate header file
bnoordhuis authored
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
45de259 @Sannis Make UNWRAP macro generic.
Sannis authored
25 #include <stdlib.h>
26
2589d55 @bnoordhuis core: add ThrowError(), ThrowTypeError(), ThrowRangeError()
bnoordhuis authored
27 #include "v8.h"
28
09dc577 @bnoordhuis build: move internals to separate header file
bnoordhuis authored
29 namespace node {
30
bc834c3 @bnoordhuis Alias _snprintf to snprintf, fix Windows build.
bnoordhuis authored
31 #ifdef _WIN32
a4a04f9 @bnoordhuis node: provide snprintf implementation on windows
bnoordhuis authored
32 // emulate snprintf() on windows, _snprintf() doesn't zero-terminate the buffer
33 // on overflow...
34 #include <stdarg.h>
35 inline static int snprintf(char* buf, unsigned int len, const char* fmt, ...) {
36 va_list ap;
37 va_start(ap, fmt);
38 int n = _vsprintf_p(buf, len, fmt, ap);
39 if (len) buf[len - 1] = '\0';
40 va_end(ap);
41 return n;
42 }
bc834c3 @bnoordhuis Alias _snprintf to snprintf, fix Windows build.
bnoordhuis authored
43 #endif
44
09dc577 @bnoordhuis build: move internals to separate header file
bnoordhuis authored
45 #ifndef offset_of
46 // g++ in strict mode complains loudly about the system offsetof() macro
47 // because it uses NULL as the base address.
a58659c @bnoordhuis core: add ROUND_UP() macro
bnoordhuis authored
48 # define offset_of(type, member) \
09dc577 @bnoordhuis build: move internals to separate header file
bnoordhuis authored
49 ((intptr_t) ((char *) &(((type *) 8)->member) - 8))
50 #endif
51
52 #ifndef container_of
a58659c @bnoordhuis core: add ROUND_UP() macro
bnoordhuis authored
53 # define container_of(ptr, type, member) \
09dc577 @bnoordhuis build: move internals to separate header file
bnoordhuis authored
54 ((type *) ((char *) (ptr) - offset_of(type, member)))
55 #endif
56
57 #ifndef ARRAY_SIZE
a58659c @bnoordhuis core: add ROUND_UP() macro
bnoordhuis authored
58 # define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
59 #endif
60
61 #ifndef ROUND_UP
62 # define ROUND_UP(a, b) ((a) % (b) ? ((a) + (b)) - ((a) % (b)) : (a))
09dc577 @bnoordhuis build: move internals to separate header file
bnoordhuis authored
63 #endif
64
2589d55 @bnoordhuis core: add ThrowError(), ThrowTypeError(), ThrowRangeError()
bnoordhuis authored
65 // this would have been a template function were it not for the fact that g++
66 // sometimes fails to resolve it...
67 #define THROW_ERROR(fun) \
68 do { \
69 v8::HandleScope scope; \
70 return v8::ThrowException(fun(v8::String::New(errmsg))); \
71 } \
72 while (0)
73
74 inline static v8::Handle<v8::Value> ThrowError(const char* errmsg) {
75 THROW_ERROR(v8::Exception::Error);
76 }
77
78 inline static v8::Handle<v8::Value> ThrowTypeError(const char* errmsg) {
79 THROW_ERROR(v8::Exception::TypeError);
80 }
81
82 inline static v8::Handle<v8::Value> ThrowRangeError(const char* errmsg) {
83 THROW_ERROR(v8::Exception::RangeError);
84 }
85
45de259 @Sannis Make UNWRAP macro generic.
Sannis authored
86 #define UNWRAP(type) \
87 assert(!args.Holder().IsEmpty()); \
88 assert(args.Holder()->InternalFieldCount() > 0); \
89 type* wrap = \
90 static_cast<type*>(args.Holder()->GetPointerFromInternalField(0)); \
91 if (!wrap) { \
92 fprintf(stderr, #type ": Aborting due to unwrap failure at %s:%d\n", \
93 __FILE__, __LINE__); \
94 abort(); \
95 }
96
09dc577 @bnoordhuis build: move internals to separate header file
bnoordhuis authored
97 } // namespace node
98
99 #endif // SRC_NODE_INTERNALS_H_
Something went wrong with that request. Please try again.