Make sure 'ARCH' get's defined with the CMake build system.
[platform/upstream/nodejs.git] / src / node.h
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
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:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
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.
21
22 #ifndef SRC_NODE_H_
23 #define SRC_NODE_H_
24
25 #include <ev.h>
26 #include <eio.h>
27 #include <v8.h>
28 #include <sys/types.h> /* struct stat */
29 #include <sys/stat.h>
30
31 #include <node_object_wrap.h>
32
33 #ifndef NODE_STRINGIFY
34 #define NODE_STRINGIFY(n) NODE_STRINGIFY_HELPER(n)
35 #define NODE_STRINGIFY_HELPER(n) #n
36 #endif
37
38 namespace node {
39
40 int Start(int argc, char *argv[]);
41
42 char** Init(int argc, char *argv[]);
43 v8::Handle<v8::Object> SetupProcessObject(int argc, char *argv[]);
44 void Load(v8::Handle<v8::Object> process);
45 void EmitExit(v8::Handle<v8::Object> process);
46
47 #define NODE_PSYMBOL(s) Persistent<String>::New(String::NewSymbol(s))
48
49 /* Converts a unixtime to V8 Date */
50 #define NODE_UNIXTIME_V8(t) v8::Date::New(1000*static_cast<double>(t))
51 #define NODE_V8_UNIXTIME(v) (static_cast<double>((v)->IntegerValue())/1000.0);
52
53 #define NODE_DEFINE_CONSTANT(target, constant)                            \
54   (target)->Set(v8::String::NewSymbol(#constant),                         \
55                 v8::Integer::New(constant),                               \
56                 static_cast<v8::PropertyAttribute>(v8::ReadOnly|v8::DontDelete))
57
58 #define NODE_SET_METHOD(obj, name, callback)                              \
59   obj->Set(v8::String::NewSymbol(name),                                   \
60            v8::FunctionTemplate::New(callback)->GetFunction())
61
62 #define NODE_SET_PROTOTYPE_METHOD(templ, name, callback)                  \
63 do {                                                                      \
64   v8::Local<v8::Signature> __callback##_SIG = v8::Signature::New(templ);  \
65   v8::Local<v8::FunctionTemplate> __callback##_TEM =                      \
66     v8::FunctionTemplate::New(callback, v8::Handle<v8::Value>(),          \
67                           __callback##_SIG);                              \
68   templ->PrototypeTemplate()->Set(v8::String::NewSymbol(name),            \
69                                   __callback##_TEM);                      \
70 } while (0)
71
72 enum encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX};
73 enum encoding ParseEncoding(v8::Handle<v8::Value> encoding_v,
74                             enum encoding _default = BINARY);
75 void FatalException(v8::TryCatch &try_catch);
76 void DisplayExceptionLine(v8::TryCatch &try_catch); // hack
77
78 v8::Local<v8::Value> Encode(const void *buf, size_t len,
79                             enum encoding encoding = BINARY);
80
81 // Returns -1 if the handle was not valid for decoding
82 ssize_t DecodeBytes(v8::Handle<v8::Value>,
83                     enum encoding encoding = BINARY);
84
85 // returns bytes written.
86 ssize_t DecodeWrite(char *buf,
87                     size_t buflen,
88                     v8::Handle<v8::Value>,
89                     enum encoding encoding = BINARY);
90
91 // Use different stat structs & calls on windows and posix;
92 // on windows, _stati64 is utf-8 and big file aware.
93 #if __POSIX__
94 # define NODE_STAT        stat
95 # define NODE_FSTAT       fstat
96 # define NODE_STAT_STRUCT struct stat
97 #else // __MINGW32__
98 # define NODE_STAT        _stati64
99 # define NODE_FSTAT       _fstati64
100 # define NODE_STAT_STRUCT struct _stati64
101 #endif
102
103 v8::Local<v8::Object> BuildStatsObject(NODE_STAT_STRUCT *s);
104
105
106 /**
107  * Call this when your constructor is invoked as a regular function, e.g. Buffer(10) instead of new Buffer(10).
108  * @param constructorTemplate Constructor template to instantiate from.
109  * @param args The arguments object passed to your constructor.
110  * @see v8::Arguments::IsConstructCall
111  */
112 v8::Handle<v8::Value> FromConstructorTemplate(v8::Persistent<v8::FunctionTemplate>& constructorTemplate, const v8::Arguments& args);
113
114
115 static inline v8::Persistent<v8::Function>* cb_persist(
116     const v8::Local<v8::Value> &v) {
117   v8::Persistent<v8::Function> *fn = new v8::Persistent<v8::Function>();
118   *fn = v8::Persistent<v8::Function>::New(v8::Local<v8::Function>::Cast(v));
119   return fn;
120 }
121
122 static inline v8::Persistent<v8::Function>* cb_unwrap(void *data) {
123   v8::Persistent<v8::Function> *cb =
124     reinterpret_cast<v8::Persistent<v8::Function>*>(data);
125   assert((*cb)->IsFunction());
126   return cb;
127 }
128
129 static inline void cb_destroy(v8::Persistent<v8::Function> * cb) {
130   cb->Dispose();
131   delete cb;
132 }
133
134 v8::Local<v8::Value> ErrnoException(int errorno,
135                                     const char *syscall = NULL,
136                                     const char *msg = "",
137                                     const char *path = NULL);
138
139 const char *signo_string(int errorno);
140
141 struct node_module_struct {
142   int version;
143   void *dso_handle;
144   const char *filename;
145   void (*register_func) (v8::Handle<v8::Object> target);
146   const char *modname;
147 };
148
149 node_module_struct* get_builtin_module(const char *name);
150
151 /**
152  * When this version number is changed, node.js will refuse
153  * to load older modules.  This should be done whenever
154  * an API is broken in the C++ side, including in v8 or
155  * other dependencies
156  */
157 #define NODE_MODULE_VERSION (1)
158
159 #define NODE_STANDARD_MODULE_STUFF \
160           NODE_MODULE_VERSION,     \
161           NULL,                    \
162           __FILE__
163
164 #define NODE_MODULE(modname, regfunc)   \
165   node::node_module_struct modname ## _module =    \
166   {                                     \
167       NODE_STANDARD_MODULE_STUFF,       \
168       regfunc,                          \
169       NODE_STRINGIFY(modname)           \
170   };
171
172 #define NODE_MODULE_DECL(modname) \
173   extern node::node_module_struct modname ## _module;
174
175
176 }  // namespace node
177 #endif  // SRC_NODE_H_