Run Tizen Webapps in single process mode
[platform/framework/web/crosswalk-tizen.git] / vendor / node / src / node.h
1 #ifndef SRC_NODE_H_
2 #define SRC_NODE_H_
3
4 #ifdef _WIN32
5 # ifndef BUILDING_NODE_EXTENSION
6 #   define NODE_EXTERN __declspec(dllexport)
7 # else
8 #   define NODE_EXTERN __declspec(dllimport)
9 # endif
10 #else
11 # define NODE_EXTERN /* nothing */
12 #endif
13
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
19 #endif
20
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)
24 #ifndef _WIN32_WINNT
25 # define _WIN32_WINNT   0x0501
26 #endif
27
28 #ifndef NOMINMAX
29 # define NOMINMAX
30 #endif
31
32 #endif
33
34 #if defined(_MSC_VER)
35 #define PATH_MAX MAX_PATH
36 #endif
37
38 #ifdef _WIN32
39 # define SIGKILL         9
40 #endif
41
42 #include "v8.h"  // NOLINT(build/include_order)
43 #include "node_version.h"  // NODE_MODULE_VERSION
44
45 #define NODE_MAKE_VERSION(major, minor, patch)                                \
46   ((major) * 0x1000 + (minor) * 0x100 + (patch))
47
48 #ifdef __clang__
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__))
52 #else
53 # define NODE_CLANG_AT_LEAST(major, minor, patch) (0)
54 #endif
55
56 #ifdef __GNUC__
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__))
60 #else
61 # define NODE_GNUC_AT_LEAST(major, minor, patch) (0)
62 #endif
63
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
70 #else
71 # define NODE_DEPRECATED(message, declarator)                                 \
72     declarator
73 #endif
74
75 // Forward-declare libuv loop
76 struct uv_loop_s;
77
78 // Forward-declare these functions now to stop MSVS from becoming
79 // terminally confused when it's done in node_internals.h
80 namespace node {
81
82 NODE_EXTERN v8::Local<v8::Value> ErrnoException(v8::Isolate* isolate,
83                                                 int errorno,
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,
88                                              int errorno,
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,
93                                              int errorno,
94                                              const char* syscall,
95                                              const char* message,
96                                              const char* path,
97                                              const char* dest);
98
99 NODE_DEPRECATED("Use ErrnoException(isolate, ...)",
100                 inline v8::Local<v8::Value> ErrnoException(
101       int errorno,
102       const char* syscall = NULL,
103       const char* message = NULL,
104       const char* path = NULL) {
105   return ErrnoException(v8::Isolate::GetCurrent(),
106                         errorno,
107                         syscall,
108                         message,
109                         path);
110 })
111
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(),
117                      errorno,
118                      syscall,
119                      message,
120                      path);
121 }
122
123 /*
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.
129  *
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.
132  */
133
134 NODE_EXTERN v8::Local<v8::Value> MakeCallback(
135     v8::Isolate* isolate,
136     v8::Local<v8::Object> recv,
137     const char* method,
138     int argc,
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,
144     int argc,
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,
150     int argc,
151     v8::Local<v8::Value>* argv);
152
153 }  // namespace node
154
155 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
156 #include "node_internals.h"
157 #endif
158
159 #include <assert.h>
160 #include <stdint.h>
161
162 #ifndef NODE_STRINGIFY
163 #define NODE_STRINGIFY(n) NODE_STRINGIFY_HELPER(n)
164 #define NODE_STRINGIFY_HELPER(n) #n
165 #endif
166
167 #ifdef _WIN32
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;
171 # define _SSIZE_T_
172 # define _SSIZE_T_DEFINED
173 #endif
174 #else  // !_WIN32
175 # include <sys/types.h>  // size_t, ssize_t
176 #endif  // _WIN32
177
178
179 namespace node {
180
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;
185 #endif
186
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;
190
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
193                       const char** argv,
194                       int* exec_argc,
195                       const char*** exec_argv,
196                       uv_loop_t* uv_loop_);
197
198 class IsolateData;
199 class Environment;
200
201 NODE_EXTERN IsolateData* CreateIsolateData(v8::Isolate* isolate,
202                                            struct uv_loop_s* loop);
203 NODE_EXTERN void FreeIsolateData(IsolateData* isolate_data);
204
205 NODE_EXTERN Environment* CreateEnvironment(IsolateData* isolate_data,
206                                            v8::Local<v8::Context> context,
207                                            int argc,
208                                            const char* const* argv,
209                                            int exec_argc,
210                                            const char* const* exec_argv);
211
212 NODE_EXTERN void LoadEnvironment(Environment* env);
213 NODE_EXTERN void FreeEnvironment(Environment* env);
214
215 NODE_EXTERN void EmitBeforeExit(Environment* env);
216 NODE_EXTERN int EmitExit(Environment* env);
217 NODE_EXTERN void RunAtExit(Environment* env);
218
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);
223
224 // Used to be a macro, hence the uppercase name.
225 #define NODE_DEFINE_CONSTANT(target, constant)                                \
226   do {                                                                        \
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,                                      \
236                                 constant_name,                                \
237                                 constant_value,                               \
238                                 constant_attributes).FromJust();              \
239   }                                                                           \
240   while (0)
241
242 // Used to be a macro, hence the uppercase name.
243 inline void NODE_SET_METHOD(v8::Local<v8::Template> recv,
244                             const char* name,
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,
249                                                                 callback);
250   v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name);
251   t->SetClassName(fn_name);
252   recv->Set(fn_name, t);
253 }
254
255 // Used to be a macro, hence the uppercase name.
256 inline void NODE_SET_METHOD(v8::Local<v8::Object> recv,
257                             const char* name,
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,
262                                                                 callback);
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);
267 }
268 #define NODE_SET_METHOD node::NODE_SET_METHOD
269
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,
273                                       const char* name,
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);
283 }
284 #define NODE_SET_PROTOTYPE_METHOD node::NODE_SET_PROTOTYPE_METHOD
285
286 // BINARY is a deprecated alias of LATIN1.
287 enum encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER, LATIN1 = BINARY};
288
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);
298 })
299
300 NODE_EXTERN void FatalException(v8::Isolate* isolate,
301                                 const v8::TryCatch& try_catch);
302
303 NODE_DEPRECATED("Use FatalException(isolate, ...)",
304                 inline void FatalException(const v8::TryCatch& try_catch) {
305   return FatalException(v8::Isolate::GetCurrent(), try_catch);
306 })
307
308 // Don't call with encoding=UCS2.
309 NODE_EXTERN v8::Local<v8::Value> Encode(v8::Isolate* isolate,
310                                         const char* buf,
311                                         size_t len,
312                                         enum encoding encoding = LATIN1);
313
314 // The input buffer should be in host endianness.
315 NODE_EXTERN v8::Local<v8::Value> Encode(v8::Isolate* isolate,
316                                         const uint16_t* buf,
317                                         size_t len);
318
319 NODE_DEPRECATED("Use Encode(isolate, ...)",
320                 inline v8::Local<v8::Value> Encode(
321     const void* buf,
322     size_t len,
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));
330   }
331   return Encode(isolate, static_cast<const char*>(buf), len, encoding);
332 })
333
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);
343 })
344
345 // returns bytes written.
346 NODE_EXTERN ssize_t DecodeWrite(v8::Isolate* isolate,
347                                 char* buf,
348                                 size_t buflen,
349                                 v8::Local<v8::Value>,
350                                 enum encoding encoding = LATIN1);
351 NODE_DEPRECATED("Use DecodeWrite(isolate, ...)",
352                 inline ssize_t DecodeWrite(char* buf,
353                                            size_t buflen,
354                                            v8::Local<v8::Value> val,
355                                            enum encoding encoding = LATIN1) {
356   return DecodeWrite(v8::Isolate::GetCurrent(), buf, buflen, val, encoding);
357 })
358
359 #ifdef _WIN32
360 NODE_EXTERN v8::Local<v8::Value> WinapiErrnoException(
361     v8::Isolate* isolate,
362     int errorno,
363     const char *syscall = NULL,
364     const char *msg = "",
365     const char *path = NULL);
366
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(),
372                               errorno,
373                               syscall,
374                               msg,
375                               path);
376 })
377 #endif
378
379 const char *signo_string(int errorno);
380
381
382 typedef void (*addon_register_func)(
383     v8::Local<v8::Object> exports,
384     v8::Local<v8::Value> module,
385     void* priv);
386
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,
391     void* priv);
392
393 #define NM_F_BUILTIN 0x01
394 #define NM_F_LINKED  0x02
395
396 struct node_module {
397   int nm_version;
398   unsigned int nm_flags;
399   void* nm_dso_handle;
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;
404   void* nm_priv;
405   struct node_module* nm_link;
406 };
407
408 NODE_EXTERN node_module* get_builtin_module(const char *name);
409 node_module* get_linked_module(const char *name);
410
411 extern "C" NODE_EXTERN void node_module_register(void* mod);
412
413 #ifdef _WIN32
414 # define NODE_MODULE_EXPORT __declspec(dllexport)
415 #else
416 # define NODE_MODULE_EXPORT __attribute__((visibility("default")))
417 #endif
418
419 #ifdef NODE_SHARED_MODE
420 # define NODE_CTOR_PREFIX
421 #else
422 # define NODE_CTOR_PREFIX static
423 #endif
424
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)
432 #else
433 #define NODE_C_CTOR(fn)                                               \
434   NODE_CTOR_PREFIX void fn(void) __attribute__((constructor));        \
435   NODE_CTOR_PREFIX void fn(void)
436 #endif
437
438 #define NODE_MODULE_X(modname, regfunc, priv, flags)                  \
439   extern "C" {                                                        \
440     static node::node_module _module =                                \
441     {                                                                 \
442       NODE_MODULE_VERSION,                                            \
443       flags,                                                          \
444       NULL,                                                           \
445       __FILE__,                                                       \
446       (node::addon_register_func) (regfunc),                          \
447       NULL,                                                           \
448       NODE_STRINGIFY(modname),                                        \
449       priv,                                                           \
450       NULL                                                            \
451     };                                                                \
452     NODE_C_CTOR(_register_ ## modname) {                              \
453       node_module_register(&_module);                                 \
454     }                                                                 \
455   }
456
457 #define NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, priv, flags)    \
458   extern "C" {                                                        \
459     static node::node_module _module =                                \
460     {                                                                 \
461       NODE_MODULE_VERSION,                                            \
462       flags,                                                          \
463       NULL,                                                           \
464       __FILE__,                                                       \
465       NULL,                                                           \
466       (node::addon_context_register_func) (regfunc),                  \
467       NODE_STRINGIFY(modname),                                        \
468       priv,                                                           \
469       NULL                                                            \
470     };                                                                \
471     NODE_C_CTOR(_register_ ## modname) {                              \
472       node_module_register(&_module);                                 \
473     }                                                                 \
474   }
475
476 #define NODE_MODULE(modname, regfunc)                                 \
477   NODE_MODULE_X(modname, regfunc, NULL, 0)
478
479 #define NODE_MODULE_CONTEXT_AWARE(modname, regfunc)                   \
480   NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, NULL, 0)
481
482 #define NODE_MODULE_CONTEXT_AWARE_BUILTIN(modname, regfunc)           \
483   NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, NULL, NM_F_BUILTIN)   \
484
485 /*
486  * For backward compatibility in add-on modules.
487  */
488 #define NODE_MODULE_DECL /* nothing */
489
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.
492  */
493 NODE_EXTERN void AtExit(void (*cb)(void* arg), void* arg = 0);
494
495 }  // namespace node
496
497 #endif  // SRC_NODE_H_