5 # ifndef BUILDING_NODE_EXTENSION
6 # define NODE_EXTERN __declspec(dllexport)
8 # define NODE_EXTERN __declspec(dllimport)
11 # define NODE_EXTERN /* nothing */
14 #ifdef BUILDING_NODE_EXTENSION
15 # undef BUILDING_V8_SHARED
16 # undef BUILDING_UV_SHARED
17 # define USING_V8_SHARED 1
18 # define USING_UV_SHARED 1
21 // This should be defined in make system.
22 // See issue https://github.com/joyent/node/issues/1236
23 #if defined(__MINGW32__) || defined(_MSC_VER)
25 # define _WIN32_WINNT 0x0501
35 #define PATH_MAX MAX_PATH
42 #include "v8.h" // NOLINT(build/include_order)
43 #include "node_version.h" // NODE_MODULE_VERSION
45 #define NODE_MAKE_VERSION(major, minor, patch) \
46 ((major) * 0x1000 + (minor) * 0x100 + (patch))
49 # define NODE_CLANG_AT_LEAST(major, minor, patch) \
50 (NODE_MAKE_VERSION(major, minor, patch) <= \
51 NODE_MAKE_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__))
53 # define NODE_CLANG_AT_LEAST(major, minor, patch) (0)
57 # define NODE_GNUC_AT_LEAST(major, minor, patch) \
58 (NODE_MAKE_VERSION(major, minor, patch) <= \
59 NODE_MAKE_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__))
61 # define NODE_GNUC_AT_LEAST(major, minor, patch) (0)
64 #if NODE_CLANG_AT_LEAST(2, 9, 0) || NODE_GNUC_AT_LEAST(4, 5, 0)
65 # define NODE_DEPRECATED(message, declarator) \
66 __attribute__((deprecated(message))) declarator
67 #elif defined(_MSC_VER)
68 # define NODE_DEPRECATED(message, declarator) \
69 __declspec(deprecated) declarator
71 # define NODE_DEPRECATED(message, declarator) \
75 // Forward-declare libuv loop
78 // Forward-declare these functions now to stop MSVS from becoming
79 // terminally confused when it's done in node_internals.h
82 NODE_EXTERN v8::Local<v8::Value> ErrnoException(v8::Isolate* isolate,
84 const char* syscall = NULL,
85 const char* message = NULL,
86 const char* path = NULL);
87 NODE_EXTERN v8::Local<v8::Value> UVException(v8::Isolate* isolate,
89 const char* syscall = NULL,
90 const char* message = NULL,
91 const char* path = NULL);
92 NODE_EXTERN v8::Local<v8::Value> UVException(v8::Isolate* isolate,
99 NODE_DEPRECATED("Use UVException(isolate, ...)",
100 inline v8::Local<v8::Value> ErrnoException(
102 const char* syscall = NULL,
103 const char* message = NULL,
104 const char* path = NULL) {
105 return ErrnoException(v8::Isolate::GetCurrent(),
112 inline v8::Local<v8::Value> UVException(int errorno,
113 const char* syscall = NULL,
114 const char* message = NULL,
115 const char* path = NULL) {
116 return UVException(v8::Isolate::GetCurrent(),
124 * MakeCallback doesn't have a HandleScope. That means the callers scope
125 * will retain ownership of created handles from MakeCallback and related.
126 * There is by default a wrapping HandleScope before uv_run, if the caller
127 * doesn't have a HandleScope on the stack the global will take ownership
128 * which won't be reaped until the uv loop exits.
130 * If a uv callback is fired, and there is no enclosing HandleScope in the
131 * cb, you will appear to leak 4-bytes for every invocation. Take heed.
134 NODE_EXTERN v8::Local<v8::Value> MakeCallback(
135 v8::Isolate* isolate,
136 v8::Local<v8::Object> recv,
139 v8::Local<v8::Value>* argv);
140 NODE_EXTERN v8::Local<v8::Value> MakeCallback(
141 v8::Isolate* isolate,
142 v8::Local<v8::Object> recv,
143 v8::Local<v8::String> symbol,
145 v8::Local<v8::Value>* argv);
146 NODE_EXTERN v8::Local<v8::Value> MakeCallback(
147 v8::Isolate* isolate,
148 v8::Local<v8::Object> recv,
149 v8::Local<v8::Function> callback,
151 v8::Local<v8::Value>* argv);
155 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
156 #include "node_internals.h"
162 #ifndef NODE_STRINGIFY
163 #define NODE_STRINGIFY(n) NODE_STRINGIFY_HELPER(n)
164 #define NODE_STRINGIFY_HELPER(n) #n
168 // TODO(tjfontaine) consider changing the usage of ssize_t to ptrdiff_t
169 #if !defined(_SSIZE_T_) && !defined(_SSIZE_T_DEFINED)
170 typedef intptr_t ssize_t;
172 # define _SSIZE_T_DEFINED
175 # include <sys/types.h> // size_t, ssize_t
181 NODE_EXTERN extern bool no_deprecation;
183 NODE_EXTERN int Start(int argc, char *argv[]);
184 NODE_EXTERN void Init(int* argc,
187 const char*** exec_argv);
191 NODE_EXTERN Environment* CreateEnvironment(v8::Isolate* isolate,
192 struct uv_loop_s* loop,
193 v8::Local<v8::Context> context,
195 const char* const* argv,
197 const char* const* exec_argv);
198 NODE_EXTERN void LoadEnvironment(Environment* env);
200 // NOTE: Calling this is the same as calling
201 // CreateEnvironment() + LoadEnvironment() from above.
202 // `uv_default_loop()` will be passed as `loop`.
203 NODE_EXTERN Environment* CreateEnvironment(v8::Isolate* isolate,
204 v8::Local<v8::Context> context,
206 const char* const* argv,
208 const char* const* exec_argv);
211 NODE_EXTERN void EmitBeforeExit(Environment* env);
212 NODE_EXTERN int EmitExit(Environment* env);
213 NODE_EXTERN void RunAtExit(Environment* env);
215 /* Converts a unixtime to V8 Date */
216 #define NODE_UNIXTIME_V8(t) v8::Date::New(v8::Isolate::GetCurrent(), \
217 1000 * static_cast<double>(t))
218 #define NODE_V8_UNIXTIME(v) (static_cast<double>((v)->NumberValue())/1000.0);
220 // Used to be a macro, hence the uppercase name.
221 #define NODE_DEFINE_CONSTANT(target, constant) \
223 v8::Isolate* isolate = target->GetIsolate(); \
224 v8::Local<v8::String> constant_name = \
225 v8::String::NewFromUtf8(isolate, #constant); \
226 v8::Local<v8::Number> constant_value = \
227 v8::Number::New(isolate, static_cast<double>(constant)); \
228 v8::PropertyAttribute constant_attributes = \
229 static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete); \
230 (target)->ForceSet(constant_name, constant_value, constant_attributes); \
234 // Used to be a macro, hence the uppercase name.
235 template <typename TypeName>
236 inline void NODE_SET_METHOD(const TypeName& recv,
238 v8::FunctionCallback callback) {
239 v8::Isolate* isolate = v8::Isolate::GetCurrent();
240 v8::HandleScope handle_scope(isolate);
241 v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate,
243 v8::Local<v8::Function> fn = t->GetFunction();
244 v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name);
245 fn->SetName(fn_name);
246 recv->Set(fn_name, fn);
248 #define NODE_SET_METHOD node::NODE_SET_METHOD
250 // Used to be a macro, hence the uppercase name.
251 // Not a template because it only makes sense for FunctionTemplates.
252 inline void NODE_SET_PROTOTYPE_METHOD(v8::Local<v8::FunctionTemplate> recv,
254 v8::FunctionCallback callback) {
255 v8::Isolate* isolate = v8::Isolate::GetCurrent();
256 v8::HandleScope handle_scope(isolate);
257 v8::Local<v8::Signature> s = v8::Signature::New(isolate, recv);
258 v8::Local<v8::FunctionTemplate> t =
259 v8::FunctionTemplate::New(isolate, callback, v8::Local<v8::Value>(), s);
260 v8::Local<v8::Function> fn = t->GetFunction();
261 recv->PrototypeTemplate()->Set(v8::String::NewFromUtf8(isolate, name), fn);
262 v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name);
263 fn->SetName(fn_name);
265 #define NODE_SET_PROTOTYPE_METHOD node::NODE_SET_PROTOTYPE_METHOD
267 enum encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER};
268 NODE_EXTERN enum encoding ParseEncoding(
269 v8::Isolate* isolate,
270 v8::Local<v8::Value> encoding_v,
271 enum encoding default_encoding = BINARY);
272 NODE_DEPRECATED("Use ParseEncoding(isolate, ...)",
273 inline enum encoding ParseEncoding(
274 v8::Local<v8::Value> encoding_v,
275 enum encoding default_encoding = BINARY) {
276 return ParseEncoding(v8::Isolate::GetCurrent(), encoding_v, default_encoding);
279 NODE_EXTERN void FatalException(v8::Isolate* isolate,
280 const v8::TryCatch& try_catch);
282 NODE_DEPRECATED("Use FatalException(isolate, ...)",
283 inline void FatalException(const v8::TryCatch& try_catch) {
284 return FatalException(v8::Isolate::GetCurrent(), try_catch);
287 // Don't call with encoding=UCS2.
288 NODE_EXTERN v8::Local<v8::Value> Encode(v8::Isolate* isolate,
291 enum encoding encoding = BINARY);
293 // The input buffer should be in host endianness.
294 NODE_EXTERN v8::Local<v8::Value> Encode(v8::Isolate* isolate,
298 NODE_DEPRECATED("Use Encode(isolate, ...)",
299 inline v8::Local<v8::Value> Encode(
302 enum encoding encoding = BINARY) {
303 v8::Isolate* isolate = v8::Isolate::GetCurrent();
304 if (encoding == UCS2) {
305 assert(reinterpret_cast<uintptr_t>(buf) % sizeof(uint16_t) == 0 &&
306 "UCS2 buffer must be aligned on two-byte boundary.");
307 const uint16_t* that = static_cast<const uint16_t*>(buf);
308 return Encode(isolate, that, len / sizeof(*that));
310 return Encode(isolate, static_cast<const char*>(buf), len, encoding);
313 // Returns -1 if the handle was not valid for decoding
314 NODE_EXTERN ssize_t DecodeBytes(v8::Isolate* isolate,
315 v8::Local<v8::Value>,
316 enum encoding encoding = BINARY);
317 NODE_DEPRECATED("Use DecodeBytes(isolate, ...)",
318 inline ssize_t DecodeBytes(
319 v8::Local<v8::Value> val,
320 enum encoding encoding = BINARY) {
321 return DecodeBytes(v8::Isolate::GetCurrent(), val, encoding);
324 // returns bytes written.
325 NODE_EXTERN ssize_t DecodeWrite(v8::Isolate* isolate,
328 v8::Local<v8::Value>,
329 enum encoding encoding = BINARY);
330 NODE_DEPRECATED("Use DecodeWrite(isolate, ...)",
331 inline ssize_t DecodeWrite(char* buf,
333 v8::Local<v8::Value> val,
334 enum encoding encoding = BINARY) {
335 return DecodeWrite(v8::Isolate::GetCurrent(), buf, buflen, val, encoding);
339 NODE_EXTERN v8::Local<v8::Value> WinapiErrnoException(
340 v8::Isolate* isolate,
342 const char *syscall = NULL,
343 const char *msg = "",
344 const char *path = NULL);
346 NODE_DEPRECATED("Use WinapiErrnoException(isolate, ...)",
347 inline v8::Local<v8::Value> WinapiErrnoException(int errorno,
348 const char *syscall = NULL, const char *msg = "",
349 const char *path = NULL) {
350 return WinapiErrnoException(v8::Isolate::GetCurrent(),
358 const char *signo_string(int errorno);
361 typedef void (*addon_register_func)(
362 v8::Local<v8::Object> exports,
363 v8::Local<v8::Value> module,
366 typedef void (*addon_context_register_func)(
367 v8::Local<v8::Object> exports,
368 v8::Local<v8::Value> module,
369 v8::Local<v8::Context> context,
372 #define NM_F_BUILTIN 0x01
373 #define NM_F_LINKED 0x02
377 unsigned int nm_flags;
379 const char* nm_filename;
380 node::addon_register_func nm_register_func;
381 node::addon_context_register_func nm_context_register_func;
382 const char* nm_modname;
384 struct node_module* nm_link;
387 node_module* get_builtin_module(const char *name);
388 node_module* get_linked_module(const char *name);
390 extern "C" NODE_EXTERN void node_module_register(void* mod);
393 # define NODE_MODULE_EXPORT __declspec(dllexport)
395 # define NODE_MODULE_EXPORT __attribute__((visibility("default")))
398 #if defined(_MSC_VER)
399 #pragma section(".CRT$XCU", read)
400 #define NODE_C_CTOR(fn) \
401 static void __cdecl fn(void); \
402 __declspec(dllexport, allocate(".CRT$XCU")) \
403 void (__cdecl*fn ## _)(void) = fn; \
404 static void __cdecl fn(void)
406 #define NODE_C_CTOR(fn) \
407 static void fn(void) __attribute__((constructor)); \
411 #define NODE_MODULE_X(modname, regfunc, priv, flags) \
413 static node::node_module _module = \
415 NODE_MODULE_VERSION, \
419 (node::addon_register_func) (regfunc), \
421 NODE_STRINGIFY(modname), \
425 NODE_C_CTOR(_register_ ## modname) { \
426 node_module_register(&_module); \
430 #define NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, priv, flags) \
432 static node::node_module _module = \
434 NODE_MODULE_VERSION, \
439 (node::addon_context_register_func) (regfunc), \
440 NODE_STRINGIFY(modname), \
444 NODE_C_CTOR(_register_ ## modname) { \
445 node_module_register(&_module); \
449 #define NODE_MODULE(modname, regfunc) \
450 NODE_MODULE_X(modname, regfunc, NULL, 0)
452 #define NODE_MODULE_CONTEXT_AWARE(modname, regfunc) \
453 NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, NULL, 0)
455 #define NODE_MODULE_CONTEXT_AWARE_BUILTIN(modname, regfunc) \
456 NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, NULL, NM_F_BUILTIN) \
459 * For backward compatibility in add-on modules.
461 #define NODE_MODULE_DECL /* nothing */
463 /* Called after the event loop exits but before the VM is disposed.
464 * Callbacks are run in reverse order of registration, i.e. newest first.
466 NODE_EXTERN void AtExit(void (*cb)(void* arg), void* arg = 0);
470 #endif // SRC_NODE_H_