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 ErrnoException(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;
182 #if HAVE_OPENSSL && NODE_FIPS_MODE
183 NODE_EXTERN extern bool enable_fips_crypto;
184 NODE_EXTERN extern bool force_fips_crypto;
187 // Whether node should open some low level hooks.
188 NODE_EXTERN extern bool g_standalone_mode;
189 NODE_EXTERN extern bool g_upstream_node_mode;
191 NODE_EXTERN int Start(int argc, char *argv[]);
192 NODE_EXTERN void Init(int* argc, // apart from NodeBindings::Initialize(), no one seems to call
195 const char*** exec_argv,
196 uv_loop_t* uv_loop_);
201 NODE_EXTERN IsolateData* CreateIsolateData(v8::Isolate* isolate,
202 struct uv_loop_s* loop);
203 NODE_EXTERN void FreeIsolateData(IsolateData* isolate_data);
205 NODE_EXTERN Environment* CreateEnvironment(IsolateData* isolate_data,
206 v8::Local<v8::Context> context,
208 const char* const* argv,
210 const char* const* exec_argv);
212 NODE_EXTERN void LoadEnvironment(Environment* env);
213 NODE_EXTERN void FreeEnvironment(Environment* env);
215 NODE_EXTERN void EmitBeforeExit(Environment* env);
216 NODE_EXTERN int EmitExit(Environment* env);
217 NODE_EXTERN void RunAtExit(Environment* env);
219 /* Converts a unixtime to V8 Date */
220 #define NODE_UNIXTIME_V8(t) v8::Date::New(v8::Isolate::GetCurrent(), \
221 1000 * static_cast<double>(t))
222 #define NODE_V8_UNIXTIME(v) (static_cast<double>((v)->NumberValue())/1000.0);
224 // Used to be a macro, hence the uppercase name.
225 #define NODE_DEFINE_CONSTANT(target, constant) \
227 v8::Isolate* isolate = target->GetIsolate(); \
228 v8::Local<v8::Context> context = isolate->GetCurrentContext(); \
229 v8::Local<v8::String> constant_name = \
230 v8::String::NewFromUtf8(isolate, #constant); \
231 v8::Local<v8::Number> constant_value = \
232 v8::Number::New(isolate, static_cast<double>(constant)); \
233 v8::PropertyAttribute constant_attributes = \
234 static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete); \
235 (target)->DefineOwnProperty(context, \
238 constant_attributes).FromJust(); \
242 // Used to be a macro, hence the uppercase name.
243 inline void NODE_SET_METHOD(v8::Local<v8::Template> recv,
245 v8::FunctionCallback callback) {
246 v8::Isolate* isolate = v8::Isolate::GetCurrent();
247 v8::HandleScope handle_scope(isolate);
248 v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate,
250 v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name);
251 t->SetClassName(fn_name);
252 recv->Set(fn_name, t);
255 // Used to be a macro, hence the uppercase name.
256 inline void NODE_SET_METHOD(v8::Local<v8::Object> recv,
258 v8::FunctionCallback callback) {
259 v8::Isolate* isolate = v8::Isolate::GetCurrent();
260 v8::HandleScope handle_scope(isolate);
261 v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate,
263 v8::Local<v8::Function> fn = t->GetFunction();
264 v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name);
265 fn->SetName(fn_name);
266 recv->Set(fn_name, fn);
268 #define NODE_SET_METHOD node::NODE_SET_METHOD
270 // Used to be a macro, hence the uppercase name.
271 // Not a template because it only makes sense for FunctionTemplates.
272 inline void NODE_SET_PROTOTYPE_METHOD(v8::Local<v8::FunctionTemplate> recv,
274 v8::FunctionCallback callback) {
275 v8::Isolate* isolate = v8::Isolate::GetCurrent();
276 v8::HandleScope handle_scope(isolate);
277 v8::Local<v8::Signature> s = v8::Signature::New(isolate, recv);
278 v8::Local<v8::FunctionTemplate> t =
279 v8::FunctionTemplate::New(isolate, callback, v8::Local<v8::Value>(), s);
280 v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name);
281 t->SetClassName(fn_name);
282 recv->PrototypeTemplate()->Set(v8::String::NewFromUtf8(isolate, name), t);
284 #define NODE_SET_PROTOTYPE_METHOD node::NODE_SET_PROTOTYPE_METHOD
286 // BINARY is a deprecated alias of LATIN1.
287 enum encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER, LATIN1 = BINARY};
289 NODE_EXTERN enum encoding ParseEncoding(
290 v8::Isolate* isolate,
291 v8::Local<v8::Value> encoding_v,
292 enum encoding default_encoding = LATIN1);
293 NODE_DEPRECATED("Use ParseEncoding(isolate, ...)",
294 inline enum encoding ParseEncoding(
295 v8::Local<v8::Value> encoding_v,
296 enum encoding default_encoding = LATIN1) {
297 return ParseEncoding(v8::Isolate::GetCurrent(), encoding_v, default_encoding);
300 NODE_EXTERN void FatalException(v8::Isolate* isolate,
301 const v8::TryCatch& try_catch);
303 NODE_DEPRECATED("Use FatalException(isolate, ...)",
304 inline void FatalException(const v8::TryCatch& try_catch) {
305 return FatalException(v8::Isolate::GetCurrent(), try_catch);
308 // Don't call with encoding=UCS2.
309 NODE_EXTERN v8::Local<v8::Value> Encode(v8::Isolate* isolate,
312 enum encoding encoding = LATIN1);
314 // The input buffer should be in host endianness.
315 NODE_EXTERN v8::Local<v8::Value> Encode(v8::Isolate* isolate,
319 NODE_DEPRECATED("Use Encode(isolate, ...)",
320 inline v8::Local<v8::Value> Encode(
323 enum encoding encoding = LATIN1) {
324 v8::Isolate* isolate = v8::Isolate::GetCurrent();
325 if (encoding == UCS2) {
326 assert(reinterpret_cast<uintptr_t>(buf) % sizeof(uint16_t) == 0 &&
327 "UCS2 buffer must be aligned on two-byte boundary.");
328 const uint16_t* that = static_cast<const uint16_t*>(buf);
329 return Encode(isolate, that, len / sizeof(*that));
331 return Encode(isolate, static_cast<const char*>(buf), len, encoding);
334 // Returns -1 if the handle was not valid for decoding
335 NODE_EXTERN ssize_t DecodeBytes(v8::Isolate* isolate,
336 v8::Local<v8::Value>,
337 enum encoding encoding = LATIN1);
338 NODE_DEPRECATED("Use DecodeBytes(isolate, ...)",
339 inline ssize_t DecodeBytes(
340 v8::Local<v8::Value> val,
341 enum encoding encoding = LATIN1) {
342 return DecodeBytes(v8::Isolate::GetCurrent(), val, encoding);
345 // returns bytes written.
346 NODE_EXTERN ssize_t DecodeWrite(v8::Isolate* isolate,
349 v8::Local<v8::Value>,
350 enum encoding encoding = LATIN1);
351 NODE_DEPRECATED("Use DecodeWrite(isolate, ...)",
352 inline ssize_t DecodeWrite(char* buf,
354 v8::Local<v8::Value> val,
355 enum encoding encoding = LATIN1) {
356 return DecodeWrite(v8::Isolate::GetCurrent(), buf, buflen, val, encoding);
360 NODE_EXTERN v8::Local<v8::Value> WinapiErrnoException(
361 v8::Isolate* isolate,
363 const char *syscall = NULL,
364 const char *msg = "",
365 const char *path = NULL);
367 NODE_DEPRECATED("Use WinapiErrnoException(isolate, ...)",
368 inline v8::Local<v8::Value> WinapiErrnoException(int errorno,
369 const char *syscall = NULL, const char *msg = "",
370 const char *path = NULL) {
371 return WinapiErrnoException(v8::Isolate::GetCurrent(),
379 const char *signo_string(int errorno);
382 typedef void (*addon_register_func)(
383 v8::Local<v8::Object> exports,
384 v8::Local<v8::Value> module,
387 typedef void (*addon_context_register_func)(
388 v8::Local<v8::Object> exports,
389 v8::Local<v8::Value> module,
390 v8::Local<v8::Context> context,
393 #define NM_F_BUILTIN 0x01
394 #define NM_F_LINKED 0x02
398 unsigned int nm_flags;
400 const char* nm_filename;
401 node::addon_register_func nm_register_func;
402 node::addon_context_register_func nm_context_register_func;
403 const char* nm_modname;
405 struct node_module* nm_link;
408 NODE_EXTERN node_module* get_builtin_module(const char *name);
409 node_module* get_linked_module(const char *name);
411 extern "C" NODE_EXTERN void node_module_register(void* mod);
414 # define NODE_MODULE_EXPORT __declspec(dllexport)
416 # define NODE_MODULE_EXPORT __attribute__((visibility("default")))
419 #ifdef NODE_SHARED_MODE
420 # define NODE_CTOR_PREFIX
422 # define NODE_CTOR_PREFIX static
425 #if defined(_MSC_VER)
426 #pragma section(".CRT$XCU", read)
427 #define NODE_C_CTOR(fn) \
428 NODE_CTOR_PREFIX void __cdecl fn(void); \
429 __declspec(dllexport, allocate(".CRT$XCU")) \
430 void (__cdecl*fn ## _)(void) = fn; \
431 NODE_CTOR_PREFIX void __cdecl fn(void)
433 #define NODE_C_CTOR(fn) \
434 NODE_CTOR_PREFIX void fn(void) __attribute__((constructor)); \
435 NODE_CTOR_PREFIX void fn(void)
438 #define NODE_MODULE_X(modname, regfunc, priv, flags) \
440 static node::node_module _module = \
442 NODE_MODULE_VERSION, \
446 (node::addon_register_func) (regfunc), \
448 NODE_STRINGIFY(modname), \
452 NODE_C_CTOR(_register_ ## modname) { \
453 node_module_register(&_module); \
457 #define NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, priv, flags) \
459 static node::node_module _module = \
461 NODE_MODULE_VERSION, \
466 (node::addon_context_register_func) (regfunc), \
467 NODE_STRINGIFY(modname), \
471 NODE_C_CTOR(_register_ ## modname) { \
472 node_module_register(&_module); \
476 #define NODE_MODULE(modname, regfunc) \
477 NODE_MODULE_X(modname, regfunc, NULL, 0)
479 #define NODE_MODULE_CONTEXT_AWARE(modname, regfunc) \
480 NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, NULL, 0)
482 #define NODE_MODULE_CONTEXT_AWARE_BUILTIN(modname, regfunc) \
483 NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, NULL, NM_F_BUILTIN) \
486 * For backward compatibility in add-on modules.
488 #define NODE_MODULE_DECL /* nothing */
490 /* Called after the event loop exits but before the VM is disposed.
491 * Callbacks are run in reverse order of registration, i.e. newest first.
493 NODE_EXTERN void AtExit(void (*cb)(void* arg), void* arg = 0);
497 #endif // SRC_NODE_H_