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_NODE_INTERNALS_H_
23 #define SRC_NODE_INTERNALS_H_
32 extern v8::Isolate* node_isolate;
34 // Defined in node.cc at startup.
35 extern v8::Persistent<v8::Object> process;
38 // emulate snprintf() on windows, _snprintf() doesn't zero-terminate the buffer
41 inline static int snprintf(char* buf, unsigned int len, const char* fmt, ...) {
44 int n = _vsprintf_p(buf, len, fmt, ap);
45 if (len) buf[len - 1] = '\0';
51 #if defined(__x86_64__)
52 # define BITS_PER_LONG 64
54 # define BITS_PER_LONG 32
58 // g++ in strict mode complains loudly about the system offsetof() macro
59 // because it uses NULL as the base address.
60 # define offset_of(type, member) \
61 ((intptr_t) ((char *) &(((type *) 8)->member) - 8))
65 # define container_of(ptr, type, member) \
66 ((type *) ((char *) (ptr) - offset_of(type, member)))
70 # define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
74 # define ROUND_UP(a, b) ((a) % (b) ? ((a) + (b)) - ((a) % (b)) : (a))
77 // this would have been a template function were it not for the fact that g++
78 // sometimes fails to resolve it...
79 #define THROW_ERROR(fun) \
81 v8::HandleScope scope; \
82 return v8::ThrowException(fun(v8::String::New(errmsg))); \
86 inline static v8::Handle<v8::Value> ThrowError(const char* errmsg) {
87 THROW_ERROR(v8::Exception::Error);
90 inline static v8::Handle<v8::Value> ThrowTypeError(const char* errmsg) {
91 THROW_ERROR(v8::Exception::TypeError);
94 inline static v8::Handle<v8::Value> ThrowRangeError(const char* errmsg) {
95 THROW_ERROR(v8::Exception::RangeError);
98 #define UNWRAP(type) \
99 assert(!args.Holder().IsEmpty()); \
100 assert(args.Holder()->InternalFieldCount() > 0); \
101 type* wrap = static_cast<type*>( \
102 args.Holder()->GetPointerFromInternalField(0)); \
104 fprintf(stderr, #type ": Aborting due to unwrap failure at %s:%d\n", \
105 __FILE__, __LINE__); \
109 v8::Handle<v8::Value> FromConstructorTemplate(
110 v8::Persistent<v8::FunctionTemplate> t,
111 const v8::Arguments& args);
115 #endif // SRC_NODE_INTERNALS_H_