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_
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(
47 const v8::Persistent<TypeName>& persistent);
49 // Call with valid HandleScope and while inside Context scope.
50 v8::Handle<v8::Value> MakeCallback(Environment* env,
51 v8::Handle<v8::Object> recv,
54 v8::Handle<v8::Value>* argv = NULL);
56 // Call with valid HandleScope and while inside Context scope.
57 v8::Handle<v8::Value> MakeCallback(Environment* env,
58 v8::Handle<v8::Object> recv,
61 v8::Handle<v8::Value>* argv = NULL);
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,
68 v8::Handle<v8::Value>* argv = NULL);
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,
75 v8::Handle<v8::Value>* argv = NULL);
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(
83 v8::Local<v8::Object> info = v8::Handle<v8::Object>());
86 // emulate snprintf() on windows, _snprintf() doesn't zero-terminate the buffer
89 inline static int snprintf(char* buf, unsigned int len, const char* fmt, ...) {
92 int n = _vsprintf_p(buf, len, fmt, ap);
100 #if defined(__x86_64__)
101 # define BITS_PER_LONG 64
103 # define BITS_PER_LONG 32
107 # define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
111 # define ROUND_UP(a, b) ((a) % (b) ? ((a) + (b)) - ((a) % (b)) : (a))
114 #if defined(__GNUC__) && __GNUC__ >= 4
115 # define MUST_USE_RESULT __attribute__((warn_unused_result))
116 # define NO_RETURN __attribute__((noreturn))
118 # define MUST_USE_RESULT
122 void AppendExceptionLine(Environment* env,
123 v8::Handle<v8::Value> er,
124 v8::Handle<v8::Message> message);
126 NO_RETURN void FatalError(const char* location, const char* message);
128 v8::Local<v8::Object> BuildStatsObject(Environment* env, const uv_stat_t* s);
131 kLittleEndian, // _Not_ LITTLE_ENDIAN, clashes with endian.h.
135 inline enum Endianness GetEndianness() {
136 // Constant-folded by the compiler.
143 return u.u16 == 1 ? kLittleEndian : kBigEndian;
146 inline bool IsLittleEndian() {
147 return GetEndianness() == kLittleEndian;
150 inline bool IsBigEndian() {
151 return GetEndianness() == kBigEndian;
154 // parse index for external array data
155 inline MUST_USE_RESULT bool ParseArrayIndex(v8::Handle<v8::Value> arg,
158 if (arg->IsUndefined()) {
163 int32_t tmp_i = arg->Int32Value();
168 *ret = static_cast<size_t>(tmp_i);
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);
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);
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);
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);
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);
206 #endif // SRC_NODE_INTERNALS_H_