Ryan Dahl | 55048cd | 2011-03-10 08:54:52 | [diff] [blame] | 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. |
Bert Belder | e0f47be | 2011-01-17 23:22:36 | [diff] [blame] | 21 | |
Ryan | d6c9d31 | 2009-09-11 14:02:29 | [diff] [blame] | 22 | #include <node.h> |
Ryan | 63a9cd3 | 2009-04-15 08:08:28 | [diff] [blame] | 23 | |
Ryan Dahl | efca334 | 2011-05-13 02:16:40 | [diff] [blame] | 24 | #include <uv.h> |
| 25 | |
Bert Belder | e0f47be | 2011-01-17 23:22:36 | [diff] [blame] | 26 | #include <v8-debug.h> |
Ryan Dahl | b3ddb89 | 2011-10-26 18:31:12 | [diff] [blame] | 27 | #ifdef HAVE_DTRACE |
| 28 | # include <node_dtrace.h> |
| 29 | #endif |
Ryan Dahl | 4635ed7 | 2010-03-11 20:40:19 | [diff] [blame] | 30 | |
Bert Belder | e0f47be | 2011-01-17 23:22:36 | [diff] [blame] | 31 | #include <locale.h> |
Bert Belder | 9cec08e | 2011-05-23 23:42:22 | [diff] [blame] | 32 | #include <signal.h> |
Ryan | 19478ed | 2009-03-03 00:56:15 | [diff] [blame] | 33 | #include <stdio.h> |
Peter Bright | 13d6a1f | 2011-08-06 04:23:25 | [diff] [blame] | 34 | #if defined(_MSC_VER) |
| 35 | #define snprintf _snprintf |
| 36 | #endif |
Ryan | 34a6f10 | 2009-05-28 12:47:16 | [diff] [blame] | 37 | #include <stdlib.h> |
Ryan Dahl | c90e44e | 2010-05-10 23:38:47 | [diff] [blame] | 38 | #include <string.h> |
Peter Bright | 13d6a1f | 2011-08-06 04:23:25 | [diff] [blame] | 39 | #if !defined(_MSC_VER) |
| 40 | #include <strings.h> |
| 41 | #else |
| 42 | #define strcasecmp _stricmp |
| 43 | #endif |
Ryan | 82d986d | 2009-09-02 18:18:50 | [diff] [blame] | 44 | #include <limits.h> /* PATH_MAX */ |
Ryan | 19478ed | 2009-03-03 00:56:15 | [diff] [blame] | 45 | #include <assert.h> |
Peter Bright | 13d6a1f | 2011-08-06 04:23:25 | [diff] [blame] | 46 | #if !defined(_MSC_VER) |
| 47 | #include <unistd.h> /* setuid, getuid */ |
| 48 | #else |
| 49 | #include <direct.h> |
Peter Bright | 13d6a1f | 2011-08-06 04:23:25 | [diff] [blame] | 50 | #include <process.h> |
| 51 | #define getpid _getpid |
Peter Bright | b9d7777 | 2011-08-11 01:45:56 | [diff] [blame] | 52 | #include <io.h> |
| 53 | #define umask _umask |
| 54 | typedef int mode_t; |
Peter Bright | 13d6a1f | 2011-08-06 04:23:25 | [diff] [blame] | 55 | #endif |
Michael Carter | 8ea6adc | 2009-09-01 09:39:30 | [diff] [blame] | 56 | #include <errno.h> |
Michael Carter | a386076 | 2010-02-08 06:13:10 | [diff] [blame] | 57 | #include <sys/types.h> |
Ryan | e02b71e | 2009-03-03 23:31:37 | [diff] [blame] | 58 | |
Bert Belder | e0f47be | 2011-01-17 23:22:36 | [diff] [blame] | 59 | #ifdef __POSIX__ |
Bert Belder | a177d60 | 2010-11-25 00:02:55 | [diff] [blame] | 60 | # include <pwd.h> /* getpwnam() */ |
| 61 | # include <grp.h> /* getgrnam() */ |
| 62 | #endif |
| 63 | |
Ryan Dahl | 630bb7a | 2009-12-13 07:42:45 | [diff] [blame] | 64 | #include <node_buffer.h> |
Igor Zinkovsky | f35a396 | 2011-10-28 22:34:24 | [diff] [blame] | 65 | #ifdef __POSIX__ |
| 66 | # include <node_io_watcher.h> |
| 67 | #endif |
Ryan Dahl | a5df0f6 | 2009-10-27 10:46:58 | [diff] [blame] | 68 | #include <node_file.h> |
Ryan Dahl | 42ee169 | 2010-01-24 19:21:45 | [diff] [blame] | 69 | #include <node_http_parser.h> |
Bert Belder | 406f44a | 2011-06-08 02:41:28 | [diff] [blame] | 70 | #ifdef __POSIX__ |
| 71 | # include <node_signal_watcher.h> |
| 72 | # include <node_stat_watcher.h> |
| 73 | #endif |
Ryan Dahl | a5df0f6 | 2009-10-27 10:46:58 | [diff] [blame] | 74 | #include <node_constants.h> |
Ryan Dahl | c4636a5 | 2010-10-12 18:49:41 | [diff] [blame] | 75 | #include <node_javascript.h> |
Ryan Dahl | 0ae02c9 | 2009-10-09 15:47:04 | [diff] [blame] | 76 | #include <node_version.h> |
Tom Hughes | 74954ce | 2011-03-08 06:00:51 | [diff] [blame] | 77 | #include <node_string.h> |
Peter Bright | 13d6a1f | 2011-08-06 04:23:25 | [diff] [blame] | 78 | #if HAVE_OPENSSL |
Bert Belder | e0f47be | 2011-01-17 23:22:36 | [diff] [blame] | 79 | # include <node_crypto.h> |
Rhys Jones | fb3a9cd | 2010-04-02 23:11:53 | [diff] [blame] | 80 | #endif |
Herbert Vojcik | c2a0672 | 2010-04-17 15:18:15 | [diff] [blame] | 81 | #include <node_script.h> |
Ben Noordhuis | 7cab4d6 | 2011-07-26 21:20:29 | [diff] [blame] | 82 | #include <v8_typed_array.h> |
Ryan | 6dd850a | 2009-09-08 12:59:43 | [diff] [blame] | 83 | |
Ryan | 19478ed | 2009-03-03 00:56:15 | [diff] [blame] | 84 | using namespace v8; |
Ryan | 19478ed | 2009-03-03 00:56:15 | [diff] [blame] | 85 | |
Rasmus Andersson | 50443f0 | 2010-10-11 15:19:11 | [diff] [blame] | 86 | # ifdef __APPLE__ |
| 87 | # include <crt_externs.h> |
| 88 | # define environ (*_NSGetEnviron()) |
Peter Bright | 13d6a1f | 2011-08-06 04:23:25 | [diff] [blame] | 89 | # elif !defined(_MSC_VER) |
Ryan | 3e4fc9f | 2009-09-10 14:48:38 | [diff] [blame] | 90 | extern char **environ; |
Rasmus Andersson | 50443f0 | 2010-10-11 15:19:11 | [diff] [blame] | 91 | # endif |
Ryan | 3e4fc9f | 2009-09-10 14:48:38 | [diff] [blame] | 92 | |
Ryan | d6c9d31 | 2009-09-11 14:02:29 | [diff] [blame] | 93 | namespace node { |
| 94 | |
Ben Noordhuis | 809fdf2 | 2011-12-09 20:49:10 | [diff] [blame] | 95 | |
Ben Noordhuis | 74a8215 | 2012-02-03 15:32:00 | [diff] [blame^] | 96 | static Persistent<Object> process; |
| 97 | |
| 98 | static Persistent<String> errno_symbol; |
| 99 | static Persistent<String> syscall_symbol; |
| 100 | static Persistent<String> errpath_symbol; |
| 101 | static Persistent<String> code_symbol; |
| 102 | |
| 103 | static Persistent<String> rss_symbol; |
| 104 | static Persistent<String> heap_total_symbol; |
| 105 | static Persistent<String> heap_used_symbol; |
| 106 | |
| 107 | static Persistent<String> listeners_symbol; |
| 108 | static Persistent<String> uncaught_exception_symbol; |
| 109 | static Persistent<String> emit_symbol; |
| 110 | |
| 111 | |
| 112 | static bool print_eval = false; |
| 113 | static char *eval_string = NULL; |
| 114 | static int option_end_index = 0; |
| 115 | static bool use_debug_agent = false; |
| 116 | static bool debug_wait_connect = false; |
| 117 | static int debug_port=5858; |
| 118 | static int max_stack_size = 0; |
| 119 | |
| 120 | static uv_check_t check_tick_watcher; |
| 121 | static uv_prepare_t prepare_tick_watcher; |
| 122 | static uv_idle_t tick_spinner; |
| 123 | static bool need_tick_cb; |
| 124 | static Persistent<String> tick_callback_sym; |
| 125 | |
| 126 | |
| 127 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 128 | static bool use_npn = true; |
| 129 | #else |
| 130 | static bool use_npn = false; |
| 131 | #endif |
| 132 | |
| 133 | #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB |
| 134 | static bool use_sni = true; |
| 135 | #else |
| 136 | static bool use_sni = false; |
| 137 | #endif |
| 138 | |
| 139 | #ifdef __POSIX__ |
| 140 | // Buffer for getpwnam_r(), getgrpam_r() and other misc callers; keep this |
| 141 | // scoped at file-level rather than method-level to avoid excess stack usage. |
| 142 | static char getbuf[PATH_MAX + 1]; |
| 143 | #endif |
| 144 | |
| 145 | // We need to notify V8 when we're idle so that it can run the garbage |
| 146 | // collector. The interface to this is V8::IdleNotification(). It returns |
| 147 | // true if the heap hasn't be fully compacted, and needs to be run again. |
| 148 | // Returning false means that it doesn't have anymore work to do. |
| 149 | // |
| 150 | // A rather convoluted algorithm has been devised to determine when Node is |
| 151 | // idle. You'll have to figure it out for yourself. |
| 152 | static uv_check_t gc_check; |
| 153 | static uv_idle_t gc_idle; |
| 154 | static uv_timer_t gc_timer; |
| 155 | bool need_gc; |
| 156 | |
| 157 | // process-relative uptime base, initialized at start-up |
| 158 | static double prog_start_time; |
| 159 | |
| 160 | #define FAST_TICK 700. |
| 161 | #define GC_WAIT_TIME 5000. |
| 162 | #define RPM_SAMPLES 100 |
| 163 | #define TICK_TIME(n) tick_times[(tick_time_head - (n)) % RPM_SAMPLES] |
| 164 | static int64_t tick_times[RPM_SAMPLES]; |
| 165 | static int tick_time_head; |
Ben Noordhuis | 2df81c5 | 2011-12-09 17:49:17 | [diff] [blame] | 166 | |
Ryan Dahl | f657d58 | 2011-06-22 12:06:26 | [diff] [blame] | 167 | static void CheckStatus(uv_timer_t* watcher, int status); |
Ben Noordhuis | 809fdf2 | 2011-12-09 20:49:10 | [diff] [blame] | 168 | |
Ryan Dahl | 3ac6dee | 2010-05-08 02:05:59 | [diff] [blame] | 169 | static void StartGCTimer () { |
Ryan Dahl | 7a5977b | 2011-06-07 16:37:27 | [diff] [blame] | 170 | if (!uv_is_active((uv_handle_t*) &gc_timer)) { |
Ryan Dahl | 7547c7d | 2011-12-07 01:00:33 | [diff] [blame] | 171 | uv_timer_start(&gc_timer, node::CheckStatus, 5000, 5000); |
Ryan Dahl | ac3bc2e | 2010-04-15 08:29:39 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
Ryan Dahl | 3ac6dee | 2010-05-08 02:05:59 | [diff] [blame] | 175 | static void StopGCTimer () { |
Ryan Dahl | 7a5977b | 2011-06-07 16:37:27 | [diff] [blame] | 176 | if (uv_is_active((uv_handle_t*) &gc_timer)) { |
Bert Belder | 9cec08e | 2011-05-23 23:42:22 | [diff] [blame] | 177 | uv_timer_stop(&gc_timer); |
Ryan Dahl | ac3bc2e | 2010-04-15 08:29:39 | [diff] [blame] | 178 | } |
| 179 | } |
Ryan Dahl | daacb81 | 2010-02-21 02:23:21 | [diff] [blame] | 180 | |
Ryan Dahl | f657d58 | 2011-06-22 12:06:26 | [diff] [blame] | 181 | static void Idle(uv_idle_t* watcher, int status) { |
Ryan Dahl | 7a5977b | 2011-06-07 16:37:27 | [diff] [blame] | 182 | assert((uv_idle_t*) watcher == &gc_idle); |
Ryan Dahl | 801fb8a | 2010-04-05 20:51:32 | [diff] [blame] | 183 | |
Ryan Dahl | 801fb8a | 2010-04-05 20:51:32 | [diff] [blame] | 184 | if (V8::IdleNotification()) { |
Ryan Dahl | 7a5977b | 2011-06-07 16:37:27 | [diff] [blame] | 185 | uv_idle_stop(&gc_idle); |
Ryan Dahl | 3ac6dee | 2010-05-08 02:05:59 | [diff] [blame] | 186 | StopGCTimer(); |
Ryan Dahl | 801fb8a | 2010-04-05 20:51:32 | [diff] [blame] | 187 | } |
Ryan Dahl | 801fb8a | 2010-04-05 20:51:32 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | |
Ryan Dahl | 3ac6dee | 2010-05-08 02:05:59 | [diff] [blame] | 191 | // Called directly after every call to select() (or epoll, or whatever) |
Ryan Dahl | f657d58 | 2011-06-22 12:06:26 | [diff] [blame] | 192 | static void Check(uv_check_t* watcher, int status) { |
| 193 | assert(watcher == &gc_check); |
Ryan Dahl | 801fb8a | 2010-04-05 20:51:32 | [diff] [blame] | 194 | |
Ben Noordhuis | 74a8215 | 2012-02-03 15:32:00 | [diff] [blame^] | 195 | tick_times[tick_time_head] = uv_now(uv_default_loop()); |
Ryan Dahl | 3ac6dee | 2010-05-08 02:05:59 | [diff] [blame] | 196 | tick_time_head = (tick_time_head + 1) % RPM_SAMPLES; |
Ryan Dahl | 801fb8a | 2010-04-05 20:51:32 | [diff] [blame] | 197 | |
Ryan Dahl | 3ac6dee | 2010-05-08 02:05:59 | [diff] [blame] | 198 | StartGCTimer(); |
Ryan Dahl | 801fb8a | 2010-04-05 20:51:32 | [diff] [blame] | 199 | |
Ryan Dahl | 3ac6dee | 2010-05-08 02:05:59 | [diff] [blame] | 200 | for (int i = 0; i < (int)(GC_WAIT_TIME/FAST_TICK); i++) { |
| 201 | double d = TICK_TIME(i+1) - TICK_TIME(i+2); |
| 202 | //printf("d = %f\n", d); |
| 203 | // If in the last 5 ticks the difference between |
| 204 | // ticks was less than 0.7 seconds, then continue. |
| 205 | if (d < FAST_TICK) { |
| 206 | //printf("---\n"); |
| 207 | return; |
| 208 | } |
Ryan Dahl | 801fb8a | 2010-04-05 20:51:32 | [diff] [blame] | 209 | } |
Ryan Dahl | 3ac6dee | 2010-05-08 02:05:59 | [diff] [blame] | 210 | |
| 211 | // Otherwise start the gc! |
| 212 | |
| 213 | //fprintf(stderr, "start idle 2\n"); |
Ryan Dahl | 7547c7d | 2011-12-07 01:00:33 | [diff] [blame] | 214 | uv_idle_start(&gc_idle, node::Idle); |
Ryan Dahl | daacb81 | 2010-02-21 02:23:21 | [diff] [blame] | 215 | } |
| 216 | |
Ryan Dahl | f80cc69 | 2010-01-06 09:17:58 | [diff] [blame] | 217 | |
Ryan Dahl | 17f3ffa | 2010-09-09 17:30:37 | [diff] [blame] | 218 | static void Tick(void) { |
Ryan Dahl | 4e7e2f8 | 2010-04-13 22:39:15 | [diff] [blame] | 219 | // Avoid entering a V8 scope. |
| 220 | if (!need_tick_cb) return; |
| 221 | |
| 222 | need_tick_cb = false; |
Ryan Dahl | 7a5977b | 2011-06-07 16:37:27 | [diff] [blame] | 223 | if (uv_is_active((uv_handle_t*) &tick_spinner)) { |
Ryan Dahl | 207901e | 2011-05-23 22:38:54 | [diff] [blame] | 224 | uv_idle_stop(&tick_spinner); |
Ben Noordhuis | 74a8215 | 2012-02-03 15:32:00 | [diff] [blame^] | 225 | uv_unref(uv_default_loop()); |
Ryan Dahl | 207901e | 2011-05-23 22:38:54 | [diff] [blame] | 226 | } |
Ryan Dahl | 4e7e2f8 | 2010-04-13 22:39:15 | [diff] [blame] | 227 | |
| 228 | HandleScope scope; |
| 229 | |
| 230 | if (tick_callback_sym.IsEmpty()) { |
| 231 | // Lazily set the symbol |
| 232 | tick_callback_sym = |
| 233 | Persistent<String>::New(String::NewSymbol("_tickCallback")); |
| 234 | } |
| 235 | |
| 236 | Local<Value> cb_v = process->Get(tick_callback_sym); |
| 237 | if (!cb_v->IsFunction()) return; |
| 238 | Local<Function> cb = Local<Function>::Cast(cb_v); |
| 239 | |
| 240 | TryCatch try_catch; |
| 241 | |
| 242 | cb->Call(process, 0, NULL); |
| 243 | |
| 244 | if (try_catch.HasCaught()) { |
| 245 | FatalException(try_catch); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | |
Ryan Dahl | f657d58 | 2011-06-22 12:06:26 | [diff] [blame] | 250 | static void Spin(uv_idle_t* handle, int status) { |
Bert Belder | 09ac99f | 2011-06-08 01:46:29 | [diff] [blame] | 251 | assert((uv_idle_t*) handle == &tick_spinner); |
| 252 | assert(status == 0); |
| 253 | Tick(); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | static Handle<Value> NeedTickCallback(const Arguments& args) { |
| 258 | HandleScope scope; |
| 259 | need_tick_cb = true; |
| 260 | // TODO: this tick_spinner shouldn't be necessary. An ev_prepare should be |
| 261 | // sufficent, the problem is only in the case of the very last "tick" - |
| 262 | // there is nothing left to do in the event loop and libev will exit. The |
| 263 | // ev_prepare callback isn't called before exiting. Thus we start this |
| 264 | // tick_spinner to keep the event loop alive long enough to handle it. |
| 265 | if (!uv_is_active((uv_handle_t*) &tick_spinner)) { |
| 266 | uv_idle_start(&tick_spinner, Spin); |
Ben Noordhuis | 74a8215 | 2012-02-03 15:32:00 | [diff] [blame^] | 267 | uv_ref(uv_default_loop()); |
Bert Belder | 09ac99f | 2011-06-08 01:46:29 | [diff] [blame] | 268 | } |
| 269 | return Undefined(); |
| 270 | } |
| 271 | |
| 272 | |
Ryan Dahl | f657d58 | 2011-06-22 12:06:26 | [diff] [blame] | 273 | static void PrepareTick(uv_prepare_t* handle, int status) { |
| 274 | assert(handle == &prepare_tick_watcher); |
Ryan Dahl | a46c63b | 2011-05-13 14:06:20 | [diff] [blame] | 275 | assert(status == 0); |
Ryan Dahl | 17f3ffa | 2010-09-09 17:30:37 | [diff] [blame] | 276 | Tick(); |
| 277 | } |
| 278 | |
| 279 | |
Ryan Dahl | f657d58 | 2011-06-22 12:06:26 | [diff] [blame] | 280 | static void CheckTick(uv_check_t* handle, int status) { |
| 281 | assert(handle == &check_tick_watcher); |
Ryan Dahl | 91bd144 | 2011-05-13 14:09:28 | [diff] [blame] | 282 | assert(status == 0); |
Ryan Dahl | 17f3ffa | 2010-09-09 17:30:37 | [diff] [blame] | 283 | Tick(); |
| 284 | } |
| 285 | |
Ryan Dahl | c9e27b1 | 2010-04-23 00:53:45 | [diff] [blame] | 286 | static inline const char *errno_string(int errorno) { |
| 287 | #define ERRNO_CASE(e) case e: return #e; |
| 288 | switch (errorno) { |
| 289 | |
| 290 | #ifdef EACCES |
| 291 | ERRNO_CASE(EACCES); |
| 292 | #endif |
| 293 | |
| 294 | #ifdef EADDRINUSE |
| 295 | ERRNO_CASE(EADDRINUSE); |
| 296 | #endif |
| 297 | |
| 298 | #ifdef EADDRNOTAVAIL |
| 299 | ERRNO_CASE(EADDRNOTAVAIL); |
| 300 | #endif |
| 301 | |
| 302 | #ifdef EAFNOSUPPORT |
| 303 | ERRNO_CASE(EAFNOSUPPORT); |
| 304 | #endif |
| 305 | |
| 306 | #ifdef EAGAIN |
| 307 | ERRNO_CASE(EAGAIN); |
Ryan Dahl | 9b2aac6 | 2010-04-28 19:58:00 | [diff] [blame] | 308 | #endif |
| 309 | |
| 310 | #ifdef EWOULDBLOCK |
| 311 | # if EAGAIN != EWOULDBLOCK |
Ryan Dahl | c9e27b1 | 2010-04-23 00:53:45 | [diff] [blame] | 312 | ERRNO_CASE(EWOULDBLOCK); |
| 313 | # endif |
| 314 | #endif |
| 315 | |
| 316 | #ifdef EALREADY |
| 317 | ERRNO_CASE(EALREADY); |
| 318 | #endif |
| 319 | |
| 320 | #ifdef EBADF |
| 321 | ERRNO_CASE(EBADF); |
| 322 | #endif |
| 323 | |
| 324 | #ifdef EBADMSG |
| 325 | ERRNO_CASE(EBADMSG); |
| 326 | #endif |
| 327 | |
| 328 | #ifdef EBUSY |
| 329 | ERRNO_CASE(EBUSY); |
| 330 | #endif |
| 331 | |
| 332 | #ifdef ECANCELED |
| 333 | ERRNO_CASE(ECANCELED); |
| 334 | #endif |
| 335 | |
| 336 | #ifdef ECHILD |
| 337 | ERRNO_CASE(ECHILD); |
| 338 | #endif |
| 339 | |
| 340 | #ifdef ECONNABORTED |
| 341 | ERRNO_CASE(ECONNABORTED); |
| 342 | #endif |
| 343 | |
| 344 | #ifdef ECONNREFUSED |
| 345 | ERRNO_CASE(ECONNREFUSED); |
| 346 | #endif |
| 347 | |
| 348 | #ifdef ECONNRESET |
| 349 | ERRNO_CASE(ECONNRESET); |
| 350 | #endif |
| 351 | |
| 352 | #ifdef EDEADLK |
| 353 | ERRNO_CASE(EDEADLK); |
| 354 | #endif |
| 355 | |
| 356 | #ifdef EDESTADDRREQ |
| 357 | ERRNO_CASE(EDESTADDRREQ); |
| 358 | #endif |
| 359 | |
| 360 | #ifdef EDOM |
| 361 | ERRNO_CASE(EDOM); |
| 362 | #endif |
| 363 | |
| 364 | #ifdef EDQUOT |
| 365 | ERRNO_CASE(EDQUOT); |
| 366 | #endif |
| 367 | |
| 368 | #ifdef EEXIST |
| 369 | ERRNO_CASE(EEXIST); |
| 370 | #endif |
| 371 | |
| 372 | #ifdef EFAULT |
| 373 | ERRNO_CASE(EFAULT); |
| 374 | #endif |
| 375 | |
| 376 | #ifdef EFBIG |
| 377 | ERRNO_CASE(EFBIG); |
| 378 | #endif |
| 379 | |
| 380 | #ifdef EHOSTUNREACH |
| 381 | ERRNO_CASE(EHOSTUNREACH); |
| 382 | #endif |
| 383 | |
| 384 | #ifdef EIDRM |
| 385 | ERRNO_CASE(EIDRM); |
| 386 | #endif |
| 387 | |
| 388 | #ifdef EILSEQ |
| 389 | ERRNO_CASE(EILSEQ); |
| 390 | #endif |
| 391 | |
| 392 | #ifdef EINPROGRESS |
| 393 | ERRNO_CASE(EINPROGRESS); |
| 394 | #endif |
| 395 | |
| 396 | #ifdef EINTR |
| 397 | ERRNO_CASE(EINTR); |
| 398 | #endif |
| 399 | |
| 400 | #ifdef EINVAL |
| 401 | ERRNO_CASE(EINVAL); |
| 402 | #endif |
| 403 | |
| 404 | #ifdef EIO |
| 405 | ERRNO_CASE(EIO); |
| 406 | #endif |
| 407 | |
| 408 | #ifdef EISCONN |
| 409 | ERRNO_CASE(EISCONN); |
| 410 | #endif |
| 411 | |
| 412 | #ifdef EISDIR |
| 413 | ERRNO_CASE(EISDIR); |
| 414 | #endif |
| 415 | |
| 416 | #ifdef ELOOP |
| 417 | ERRNO_CASE(ELOOP); |
| 418 | #endif |
| 419 | |
| 420 | #ifdef EMFILE |
| 421 | ERRNO_CASE(EMFILE); |
| 422 | #endif |
| 423 | |
| 424 | #ifdef EMLINK |
| 425 | ERRNO_CASE(EMLINK); |
| 426 | #endif |
| 427 | |
| 428 | #ifdef EMSGSIZE |
| 429 | ERRNO_CASE(EMSGSIZE); |
| 430 | #endif |
| 431 | |
| 432 | #ifdef EMULTIHOP |
| 433 | ERRNO_CASE(EMULTIHOP); |
| 434 | #endif |
| 435 | |
| 436 | #ifdef ENAMETOOLONG |
| 437 | ERRNO_CASE(ENAMETOOLONG); |
| 438 | #endif |
| 439 | |
| 440 | #ifdef ENETDOWN |
| 441 | ERRNO_CASE(ENETDOWN); |
| 442 | #endif |
| 443 | |
| 444 | #ifdef ENETRESET |
| 445 | ERRNO_CASE(ENETRESET); |
| 446 | #endif |
| 447 | |
| 448 | #ifdef ENETUNREACH |
| 449 | ERRNO_CASE(ENETUNREACH); |
| 450 | #endif |
| 451 | |
| 452 | #ifdef ENFILE |
| 453 | ERRNO_CASE(ENFILE); |
| 454 | #endif |
| 455 | |
| 456 | #ifdef ENOBUFS |
| 457 | ERRNO_CASE(ENOBUFS); |
| 458 | #endif |
| 459 | |
| 460 | #ifdef ENODATA |
| 461 | ERRNO_CASE(ENODATA); |
| 462 | #endif |
| 463 | |
| 464 | #ifdef ENODEV |
| 465 | ERRNO_CASE(ENODEV); |
| 466 | #endif |
| 467 | |
| 468 | #ifdef ENOENT |
| 469 | ERRNO_CASE(ENOENT); |
| 470 | #endif |
| 471 | |
| 472 | #ifdef ENOEXEC |
| 473 | ERRNO_CASE(ENOEXEC); |
| 474 | #endif |
| 475 | |
Ryan Dahl | c9e27b1 | 2010-04-23 00:53:45 | [diff] [blame] | 476 | #ifdef ENOLINK |
| 477 | ERRNO_CASE(ENOLINK); |
| 478 | #endif |
| 479 | |
Ryan Dahl | 3bb21b5 | 2010-04-28 22:07:15 | [diff] [blame] | 480 | #ifdef ENOLCK |
| 481 | # if ENOLINK != ENOLCK |
| 482 | ERRNO_CASE(ENOLCK); |
| 483 | # endif |
| 484 | #endif |
| 485 | |
Ryan Dahl | c9e27b1 | 2010-04-23 00:53:45 | [diff] [blame] | 486 | #ifdef ENOMEM |
| 487 | ERRNO_CASE(ENOMEM); |
| 488 | #endif |
| 489 | |
| 490 | #ifdef ENOMSG |
| 491 | ERRNO_CASE(ENOMSG); |
| 492 | #endif |
| 493 | |
| 494 | #ifdef ENOPROTOOPT |
| 495 | ERRNO_CASE(ENOPROTOOPT); |
| 496 | #endif |
| 497 | |
| 498 | #ifdef ENOSPC |
| 499 | ERRNO_CASE(ENOSPC); |
| 500 | #endif |
| 501 | |
| 502 | #ifdef ENOSR |
| 503 | ERRNO_CASE(ENOSR); |
| 504 | #endif |
| 505 | |
| 506 | #ifdef ENOSTR |
| 507 | ERRNO_CASE(ENOSTR); |
| 508 | #endif |
| 509 | |
| 510 | #ifdef ENOSYS |
| 511 | ERRNO_CASE(ENOSYS); |
| 512 | #endif |
| 513 | |
| 514 | #ifdef ENOTCONN |
| 515 | ERRNO_CASE(ENOTCONN); |
| 516 | #endif |
| 517 | |
| 518 | #ifdef ENOTDIR |
| 519 | ERRNO_CASE(ENOTDIR); |
| 520 | #endif |
| 521 | |
| 522 | #ifdef ENOTEMPTY |
| 523 | ERRNO_CASE(ENOTEMPTY); |
| 524 | #endif |
| 525 | |
| 526 | #ifdef ENOTSOCK |
| 527 | ERRNO_CASE(ENOTSOCK); |
| 528 | #endif |
| 529 | |
| 530 | #ifdef ENOTSUP |
| 531 | ERRNO_CASE(ENOTSUP); |
| 532 | #else |
| 533 | # ifdef EOPNOTSUPP |
| 534 | ERRNO_CASE(EOPNOTSUPP); |
| 535 | # endif |
| 536 | #endif |
| 537 | |
| 538 | #ifdef ENOTTY |
| 539 | ERRNO_CASE(ENOTTY); |
| 540 | #endif |
| 541 | |
| 542 | #ifdef ENXIO |
| 543 | ERRNO_CASE(ENXIO); |
| 544 | #endif |
| 545 | |
| 546 | |
| 547 | #ifdef EOVERFLOW |
| 548 | ERRNO_CASE(EOVERFLOW); |
| 549 | #endif |
| 550 | |
| 551 | #ifdef EPERM |
| 552 | ERRNO_CASE(EPERM); |
| 553 | #endif |
| 554 | |
| 555 | #ifdef EPIPE |
| 556 | ERRNO_CASE(EPIPE); |
| 557 | #endif |
| 558 | |
| 559 | #ifdef EPROTO |
| 560 | ERRNO_CASE(EPROTO); |
| 561 | #endif |
| 562 | |
| 563 | #ifdef EPROTONOSUPPORT |
| 564 | ERRNO_CASE(EPROTONOSUPPORT); |
| 565 | #endif |
| 566 | |
| 567 | #ifdef EPROTOTYPE |
| 568 | ERRNO_CASE(EPROTOTYPE); |
| 569 | #endif |
| 570 | |
| 571 | #ifdef ERANGE |
| 572 | ERRNO_CASE(ERANGE); |
| 573 | #endif |
| 574 | |
| 575 | #ifdef EROFS |
| 576 | ERRNO_CASE(EROFS); |
| 577 | #endif |
| 578 | |
| 579 | #ifdef ESPIPE |
| 580 | ERRNO_CASE(ESPIPE); |
| 581 | #endif |
| 582 | |
| 583 | #ifdef ESRCH |
| 584 | ERRNO_CASE(ESRCH); |
| 585 | #endif |
| 586 | |
| 587 | #ifdef ESTALE |
| 588 | ERRNO_CASE(ESTALE); |
| 589 | #endif |
| 590 | |
| 591 | #ifdef ETIME |
| 592 | ERRNO_CASE(ETIME); |
| 593 | #endif |
| 594 | |
| 595 | #ifdef ETIMEDOUT |
| 596 | ERRNO_CASE(ETIMEDOUT); |
| 597 | #endif |
| 598 | |
| 599 | #ifdef ETXTBSY |
| 600 | ERRNO_CASE(ETXTBSY); |
| 601 | #endif |
| 602 | |
| 603 | #ifdef EXDEV |
| 604 | ERRNO_CASE(EXDEV); |
| 605 | #endif |
| 606 | |
| 607 | default: return ""; |
| 608 | } |
| 609 | } |
| 610 | |
Felix Geisendörfer | f8a3cf9 | 2010-04-28 13:04:08 | [diff] [blame] | 611 | const char *signo_string(int signo) { |
| 612 | #define SIGNO_CASE(e) case e: return #e; |
| 613 | switch (signo) { |
| 614 | |
| 615 | #ifdef SIGHUP |
| 616 | SIGNO_CASE(SIGHUP); |
| 617 | #endif |
| 618 | |
| 619 | #ifdef SIGINT |
| 620 | SIGNO_CASE(SIGINT); |
| 621 | #endif |
| 622 | |
| 623 | #ifdef SIGQUIT |
| 624 | SIGNO_CASE(SIGQUIT); |
| 625 | #endif |
| 626 | |
| 627 | #ifdef SIGILL |
| 628 | SIGNO_CASE(SIGILL); |
| 629 | #endif |
| 630 | |
| 631 | #ifdef SIGTRAP |
| 632 | SIGNO_CASE(SIGTRAP); |
| 633 | #endif |
| 634 | |
| 635 | #ifdef SIGABRT |
| 636 | SIGNO_CASE(SIGABRT); |
| 637 | #endif |
| 638 | |
| 639 | #ifdef SIGIOT |
| 640 | # if SIGABRT != SIGIOT |
| 641 | SIGNO_CASE(SIGIOT); |
| 642 | # endif |
| 643 | #endif |
| 644 | |
| 645 | #ifdef SIGBUS |
| 646 | SIGNO_CASE(SIGBUS); |
| 647 | #endif |
| 648 | |
| 649 | #ifdef SIGFPE |
| 650 | SIGNO_CASE(SIGFPE); |
| 651 | #endif |
| 652 | |
| 653 | #ifdef SIGKILL |
| 654 | SIGNO_CASE(SIGKILL); |
| 655 | #endif |
| 656 | |
| 657 | #ifdef SIGUSR1 |
| 658 | SIGNO_CASE(SIGUSR1); |
| 659 | #endif |
| 660 | |
| 661 | #ifdef SIGSEGV |
| 662 | SIGNO_CASE(SIGSEGV); |
| 663 | #endif |
| 664 | |
| 665 | #ifdef SIGUSR2 |
| 666 | SIGNO_CASE(SIGUSR2); |
| 667 | #endif |
| 668 | |
| 669 | #ifdef SIGPIPE |
| 670 | SIGNO_CASE(SIGPIPE); |
| 671 | #endif |
| 672 | |
| 673 | #ifdef SIGALRM |
| 674 | SIGNO_CASE(SIGALRM); |
| 675 | #endif |
| 676 | |
| 677 | SIGNO_CASE(SIGTERM); |
Bert Belder | dcc3508 | 2010-11-25 00:04:31 | [diff] [blame] | 678 | |
| 679 | #ifdef SIGCHLD |
Felix Geisendörfer | f8a3cf9 | 2010-04-28 13:04:08 | [diff] [blame] | 680 | SIGNO_CASE(SIGCHLD); |
Bert Belder | dcc3508 | 2010-11-25 00:04:31 | [diff] [blame] | 681 | #endif |
Felix Geisendörfer | f8a3cf9 | 2010-04-28 13:04:08 | [diff] [blame] | 682 | |
| 683 | #ifdef SIGSTKFLT |
| 684 | SIGNO_CASE(SIGSTKFLT); |
| 685 | #endif |
| 686 | |
| 687 | |
| 688 | #ifdef SIGCONT |
| 689 | SIGNO_CASE(SIGCONT); |
| 690 | #endif |
| 691 | |
| 692 | #ifdef SIGSTOP |
| 693 | SIGNO_CASE(SIGSTOP); |
| 694 | #endif |
| 695 | |
| 696 | #ifdef SIGTSTP |
| 697 | SIGNO_CASE(SIGTSTP); |
| 698 | #endif |
| 699 | |
| 700 | #ifdef SIGTTIN |
| 701 | SIGNO_CASE(SIGTTIN); |
| 702 | #endif |
| 703 | |
| 704 | #ifdef SIGTTOU |
| 705 | SIGNO_CASE(SIGTTOU); |
| 706 | #endif |
| 707 | |
| 708 | #ifdef SIGURG |
| 709 | SIGNO_CASE(SIGURG); |
| 710 | #endif |
| 711 | |
| 712 | #ifdef SIGXCPU |
| 713 | SIGNO_CASE(SIGXCPU); |
| 714 | #endif |
| 715 | |
| 716 | #ifdef SIGXFSZ |
| 717 | SIGNO_CASE(SIGXFSZ); |
| 718 | #endif |
| 719 | |
| 720 | #ifdef SIGVTALRM |
| 721 | SIGNO_CASE(SIGVTALRM); |
| 722 | #endif |
| 723 | |
| 724 | #ifdef SIGPROF |
| 725 | SIGNO_CASE(SIGPROF); |
| 726 | #endif |
| 727 | |
| 728 | #ifdef SIGWINCH |
| 729 | SIGNO_CASE(SIGWINCH); |
| 730 | #endif |
| 731 | |
| 732 | #ifdef SIGIO |
| 733 | SIGNO_CASE(SIGIO); |
| 734 | #endif |
| 735 | |
| 736 | #ifdef SIGPOLL |
Ryan Dahl | 3bb21b5 | 2010-04-28 22:07:15 | [diff] [blame] | 737 | # if SIGPOLL != SIGIO |
Felix Geisendörfer | f8a3cf9 | 2010-04-28 13:04:08 | [diff] [blame] | 738 | SIGNO_CASE(SIGPOLL); |
Ryan Dahl | 3bb21b5 | 2010-04-28 22:07:15 | [diff] [blame] | 739 | # endif |
Felix Geisendörfer | f8a3cf9 | 2010-04-28 13:04:08 | [diff] [blame] | 740 | #endif |
| 741 | |
| 742 | #ifdef SIGLOST |
| 743 | SIGNO_CASE(SIGLOST); |
| 744 | #endif |
| 745 | |
| 746 | #ifdef SIGPWR |
Raffaele Sena | b3b81d6 | 2010-06-09 04:08:05 | [diff] [blame] | 747 | # if SIGPWR != SIGLOST |
Felix Geisendörfer | f8a3cf9 | 2010-04-28 13:04:08 | [diff] [blame] | 748 | SIGNO_CASE(SIGPWR); |
Raffaele Sena | b3b81d6 | 2010-06-09 04:08:05 | [diff] [blame] | 749 | # endif |
Felix Geisendörfer | f8a3cf9 | 2010-04-28 13:04:08 | [diff] [blame] | 750 | #endif |
| 751 | |
| 752 | #ifdef SIGSYS |
| 753 | SIGNO_CASE(SIGSYS); |
| 754 | #endif |
| 755 | |
Felix Geisendörfer | f8a3cf9 | 2010-04-28 13:04:08 | [diff] [blame] | 756 | default: return ""; |
| 757 | } |
| 758 | } |
| 759 | |
Ryan Dahl | c9e27b1 | 2010-04-23 00:53:45 | [diff] [blame] | 760 | |
| 761 | Local<Value> ErrnoException(int errorno, |
| 762 | const char *syscall, |
visionmedia | 45948e0 | 2010-05-14 14:52:49 | [diff] [blame] | 763 | const char *msg, |
| 764 | const char *path) { |
| 765 | Local<Value> e; |
Ryan Dahl | c9e27b1 | 2010-04-23 00:53:45 | [diff] [blame] | 766 | Local<String> estring = String::NewSymbol(errno_string(errorno)); |
Bert Belder | 2ce0961 | 2011-01-17 21:47:59 | [diff] [blame] | 767 | if (!msg[0]) { |
Bert Belder | 2ce0961 | 2011-01-17 21:47:59 | [diff] [blame] | 768 | msg = strerror(errorno); |
Bert Belder | 2ce0961 | 2011-01-17 21:47:59 | [diff] [blame] | 769 | } |
Ryan Dahl | c9e27b1 | 2010-04-23 00:53:45 | [diff] [blame] | 770 | Local<String> message = String::NewSymbol(msg); |
| 771 | |
| 772 | Local<String> cons1 = String::Concat(estring, String::NewSymbol(", ")); |
| 773 | Local<String> cons2 = String::Concat(cons1, message); |
| 774 | |
Ryan Dahl | 6cc4292 | 2011-10-19 23:53:07 | [diff] [blame] | 775 | if (syscall_symbol.IsEmpty()) { |
Ryan Dahl | c9e27b1 | 2010-04-23 00:53:45 | [diff] [blame] | 776 | syscall_symbol = NODE_PSYMBOL("syscall"); |
| 777 | errno_symbol = NODE_PSYMBOL("errno"); |
visionmedia | 45948e0 | 2010-05-14 14:52:49 | [diff] [blame] | 778 | errpath_symbol = NODE_PSYMBOL("path"); |
Ryan Dahl | aa95e57 | 2011-02-04 22:35:14 | [diff] [blame] | 779 | code_symbol = NODE_PSYMBOL("code"); |
Ryan Dahl | c9e27b1 | 2010-04-23 00:53:45 | [diff] [blame] | 780 | } |
| 781 | |
visionmedia | 45948e0 | 2010-05-14 14:52:49 | [diff] [blame] | 782 | if (path) { |
| 783 | Local<String> cons3 = String::Concat(cons2, String::NewSymbol(" '")); |
| 784 | Local<String> cons4 = String::Concat(cons3, String::New(path)); |
| 785 | Local<String> cons5 = String::Concat(cons4, String::NewSymbol("'")); |
| 786 | e = Exception::Error(cons5); |
| 787 | } else { |
| 788 | e = Exception::Error(cons2); |
| 789 | } |
| 790 | |
| 791 | Local<Object> obj = e->ToObject(); |
| 792 | |
Ryan Dahl | c9e27b1 | 2010-04-23 00:53:45 | [diff] [blame] | 793 | obj->Set(errno_symbol, Integer::New(errorno)); |
Ryan Dahl | aa95e57 | 2011-02-04 22:35:14 | [diff] [blame] | 794 | obj->Set(code_symbol, estring); |
visionmedia | 45948e0 | 2010-05-14 14:52:49 | [diff] [blame] | 795 | if (path) obj->Set(errpath_symbol, String::New(path)); |
Ryan Dahl | c9e27b1 | 2010-04-23 00:53:45 | [diff] [blame] | 796 | if (syscall) obj->Set(syscall_symbol, String::NewSymbol(syscall)); |
| 797 | return e; |
| 798 | } |
| 799 | |
| 800 | |
Bert Belder | 823a443 | 2011-12-01 23:02:51 | [diff] [blame] | 801 | static const char* get_uv_errno_string(int errorno) { |
| 802 | uv_err_t err; |
| 803 | memset(&err, 0, sizeof err); |
| 804 | err.code = (uv_err_code)errorno; |
| 805 | return uv_err_name(err); |
| 806 | } |
| 807 | |
| 808 | |
| 809 | static const char* get_uv_errno_message(int errorno) { |
| 810 | uv_err_t err; |
| 811 | memset(&err, 0, sizeof err); |
| 812 | err.code = (uv_err_code)errorno; |
| 813 | return uv_strerror(err); |
| 814 | } |
| 815 | |
| 816 | |
| 817 | // hack alert! copy of ErrnoException, tuned for uv errors |
| 818 | Local<Value> UVException(int errorno, |
| 819 | const char *syscall, |
| 820 | const char *msg, |
| 821 | const char *path) { |
Bert Belder | 823a443 | 2011-12-01 23:02:51 | [diff] [blame] | 822 | if (syscall_symbol.IsEmpty()) { |
| 823 | syscall_symbol = NODE_PSYMBOL("syscall"); |
| 824 | errno_symbol = NODE_PSYMBOL("errno"); |
| 825 | errpath_symbol = NODE_PSYMBOL("path"); |
| 826 | code_symbol = NODE_PSYMBOL("code"); |
| 827 | } |
| 828 | |
| 829 | if (!msg || !msg[0]) |
| 830 | msg = get_uv_errno_message(errorno); |
| 831 | |
Bert Belder | 641f2be | 2011-12-02 01:14:04 | [diff] [blame] | 832 | Local<String> estring = String::NewSymbol(get_uv_errno_string(errorno)); |
Bert Belder | 823a443 | 2011-12-01 23:02:51 | [diff] [blame] | 833 | Local<String> message = String::NewSymbol(msg); |
| 834 | Local<String> cons1 = String::Concat(estring, String::NewSymbol(", ")); |
| 835 | Local<String> cons2 = String::Concat(cons1, message); |
| 836 | |
| 837 | Local<Value> e; |
| 838 | |
| 839 | Local<String> path_str; |
| 840 | |
| 841 | if (path) { |
| 842 | #ifdef _WIN32 |
| 843 | if (strncmp(path, "\\\\?\\UNC\\", 8) == 0) { |
| 844 | path_str = String::Concat(String::New("\\\\"), String::New(path + 8)); |
| 845 | } else if (strncmp(path, "\\\\?\\", 4) == 0) { |
| 846 | path_str = String::New(path + 4); |
| 847 | } else { |
| 848 | path_str = String::New(path); |
| 849 | } |
| 850 | #else |
| 851 | path_str = String::New(path); |
| 852 | #endif |
| 853 | |
| 854 | Local<String> cons3 = String::Concat(cons2, String::NewSymbol(" '")); |
| 855 | Local<String> cons4 = String::Concat(cons3, path_str); |
| 856 | Local<String> cons5 = String::Concat(cons4, String::NewSymbol("'")); |
| 857 | e = Exception::Error(cons5); |
| 858 | } else { |
| 859 | e = Exception::Error(cons2); |
| 860 | } |
| 861 | |
| 862 | Local<Object> obj = e->ToObject(); |
| 863 | |
| 864 | // TODO errno should probably go |
| 865 | obj->Set(errno_symbol, Integer::New(errorno)); |
| 866 | obj->Set(code_symbol, estring); |
| 867 | if (path) obj->Set(errpath_symbol, path_str); |
| 868 | if (syscall) obj->Set(syscall_symbol, String::NewSymbol(syscall)); |
| 869 | return e; |
| 870 | } |
| 871 | |
| 872 | |
Bert Belder | 6ee73a2 | 2011-11-04 15:10:48 | [diff] [blame] | 873 | #ifdef _WIN32 |
Igor Zinkovsky | 500c8f4 | 2011-12-15 20:36:05 | [diff] [blame] | 874 | // Does about the same as strerror(), |
| 875 | // but supports all windows error messages |
| 876 | static const char *winapi_strerror(const int errorno) { |
| 877 | char *errmsg = NULL; |
| 878 | |
| 879 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | |
| 880 | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno, |
| 881 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&errmsg, 0, NULL); |
| 882 | |
| 883 | if (errmsg) { |
| 884 | // Remove trailing newlines |
| 885 | for (int i = strlen(errmsg) - 1; |
| 886 | i >= 0 && (errmsg[i] == '\n' || errmsg[i] == '\r'); i--) { |
| 887 | errmsg[i] = '\0'; |
| 888 | } |
| 889 | |
| 890 | return errmsg; |
| 891 | } else { |
| 892 | // FormatMessage failed |
| 893 | return "Unknown error"; |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | |
Bert Belder | 6ee73a2 | 2011-11-04 15:10:48 | [diff] [blame] | 898 | Local<Value> WinapiErrnoException(int errorno, |
Bert Belder | 829735e | 2011-11-04 15:23:02 | [diff] [blame] | 899 | const char* syscall, |
| 900 | const char* msg, |
| 901 | const char* path) { |
Bert Belder | 6ee73a2 | 2011-11-04 15:10:48 | [diff] [blame] | 902 | Local<Value> e; |
Bert Belder | 829735e | 2011-11-04 15:23:02 | [diff] [blame] | 903 | if (!msg || !msg[0]) { |
Bert Belder | 6ee73a2 | 2011-11-04 15:10:48 | [diff] [blame] | 904 | msg = winapi_strerror(errorno); |
| 905 | } |
| 906 | Local<String> message = String::NewSymbol(msg); |
| 907 | |
| 908 | if (syscall_symbol.IsEmpty()) { |
| 909 | syscall_symbol = NODE_PSYMBOL("syscall"); |
| 910 | errno_symbol = NODE_PSYMBOL("errno"); |
| 911 | errpath_symbol = NODE_PSYMBOL("path"); |
| 912 | code_symbol = NODE_PSYMBOL("code"); |
| 913 | } |
| 914 | |
| 915 | if (path) { |
| 916 | Local<String> cons1 = String::Concat(message, String::NewSymbol(" '")); |
| 917 | Local<String> cons2 = String::Concat(cons1, String::New(path)); |
| 918 | Local<String> cons3 = String::Concat(cons2, String::NewSymbol("'")); |
| 919 | e = Exception::Error(cons3); |
| 920 | } else { |
| 921 | e = Exception::Error(message); |
| 922 | } |
| 923 | |
| 924 | Local<Object> obj = e->ToObject(); |
| 925 | |
| 926 | obj->Set(errno_symbol, Integer::New(errorno)); |
| 927 | if (path) obj->Set(errpath_symbol, String::New(path)); |
| 928 | if (syscall) obj->Set(syscall_symbol, String::NewSymbol(syscall)); |
| 929 | return e; |
| 930 | } |
| 931 | #endif |
| 932 | |
| 933 | |
Ben Noordhuis | 55c65cc | 2010-09-24 15:02:08 | [diff] [blame] | 934 | Handle<Value> FromConstructorTemplate(Persistent<FunctionTemplate>& t, |
| 935 | const Arguments& args) { |
| 936 | HandleScope scope; |
| 937 | |
| 938 | const int argc = args.Length(); |
Ryan Dahl | 5dd08c6 | 2010-10-09 21:21:26 | [diff] [blame] | 939 | Local<Value>* argv = new Local<Value>[argc]; |
Ben Noordhuis | 55c65cc | 2010-09-24 15:02:08 | [diff] [blame] | 940 | |
| 941 | for (int i = 0; i < argc; ++i) { |
| 942 | argv[i] = args[i]; |
| 943 | } |
| 944 | |
| 945 | Local<Object> instance = t->GetFunction()->NewInstance(argc, argv); |
Ryan Dahl | 5dd08c6 | 2010-10-09 21:21:26 | [diff] [blame] | 946 | |
| 947 | delete[] argv; |
| 948 | |
Ben Noordhuis | 55c65cc | 2010-09-24 15:02:08 | [diff] [blame] | 949 | return scope.Close(instance); |
| 950 | } |
| 951 | |
| 952 | |
Ryan Dahl | 650a308 | 2011-05-28 20:44:03 | [diff] [blame] | 953 | // MakeCallback may only be made directly off the event loop. |
| 954 | // That is there can be no JavaScript stack frames underneath it. |
| 955 | // (Is there any way to assert that?) |
| 956 | // |
| 957 | // Maybe make this a method of a node::Handle super class |
| 958 | // |
| 959 | void MakeCallback(Handle<Object> object, |
| 960 | const char* method, |
| 961 | int argc, |
| 962 | Handle<Value> argv[]) { |
| 963 | HandleScope scope; |
| 964 | |
Bert Belder | 889620d | 2011-10-28 09:34:53 | [diff] [blame] | 965 | Local<Value> callback_v = object->Get(String::New(method)); |
Ryan Dahl | 26c5905 | 2011-10-07 07:57:37 | [diff] [blame] | 966 | if (!callback_v->IsFunction()) { |
| 967 | fprintf(stderr, "method = %s", method); |
| 968 | } |
Ryan Dahl | 650a308 | 2011-05-28 20:44:03 | [diff] [blame] | 969 | assert(callback_v->IsFunction()); |
| 970 | Local<Function> callback = Local<Function>::Cast(callback_v); |
| 971 | |
| 972 | // TODO Hook for long stack traces to be made here. |
| 973 | |
| 974 | TryCatch try_catch; |
| 975 | |
| 976 | callback->Call(object, argc, argv); |
| 977 | |
| 978 | if (try_catch.HasCaught()) { |
| 979 | FatalException(try_catch); |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | |
Ryan Dahl | 6cc4292 | 2011-10-19 23:53:07 | [diff] [blame] | 984 | void SetErrno(uv_err_t err) { |
| 985 | HandleScope scope; |
| 986 | |
| 987 | if (errno_symbol.IsEmpty()) { |
| 988 | errno_symbol = NODE_PSYMBOL("errno"); |
| 989 | } |
| 990 | |
| 991 | if (err.code == UV_UNKNOWN) { |
| 992 | char errno_buf[100]; |
| 993 | snprintf(errno_buf, 100, "Unknown system errno %d", err.sys_errno_); |
| 994 | Context::GetCurrent()->Global()->Set(errno_symbol, String::New(errno_buf)); |
| 995 | } else { |
| 996 | Context::GetCurrent()->Global()->Set(errno_symbol, |
| 997 | String::NewSymbol(uv_err_name(err))); |
| 998 | } |
Ryan Dahl | 650a308 | 2011-05-28 20:44:03 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | |
Ryan Dahl | 07792af | 2009-09-21 10:27:22 | [diff] [blame] | 1002 | enum encoding ParseEncoding(Handle<Value> encoding_v, enum encoding _default) { |
| 1003 | HandleScope scope; |
| 1004 | |
| 1005 | if (!encoding_v->IsString()) return _default; |
| 1006 | |
| 1007 | String::Utf8Value encoding(encoding_v->ToString()); |
| 1008 | |
| 1009 | if (strcasecmp(*encoding, "utf8") == 0) { |
| 1010 | return UTF8; |
Ryan Dahl | 2b994d9 | 2009-10-06 08:45:18 | [diff] [blame] | 1011 | } else if (strcasecmp(*encoding, "utf-8") == 0) { |
| 1012 | return UTF8; |
Ryan Dahl | 07792af | 2009-09-21 10:27:22 | [diff] [blame] | 1013 | } else if (strcasecmp(*encoding, "ascii") == 0) { |
| 1014 | return ASCII; |
Ben Noordhuis | 95638c9 | 2010-07-28 12:20:23 | [diff] [blame] | 1015 | } else if (strcasecmp(*encoding, "base64") == 0) { |
| 1016 | return BASE64; |
Konstantin Käfer | 9e101f2 | 2011-02-06 20:49:52 | [diff] [blame] | 1017 | } else if (strcasecmp(*encoding, "ucs2") == 0) { |
| 1018 | return UCS2; |
| 1019 | } else if (strcasecmp(*encoding, "ucs-2") == 0) { |
| 1020 | return UCS2; |
Ryan Dahl | 07792af | 2009-09-21 10:27:22 | [diff] [blame] | 1021 | } else if (strcasecmp(*encoding, "binary") == 0) { |
| 1022 | return BINARY; |
isaacs | 0aa1a8a | 2011-02-20 01:29:01 | [diff] [blame] | 1023 | } else if (strcasecmp(*encoding, "hex") == 0) { |
| 1024 | return HEX; |
Ryan Dahl | 07792af | 2009-09-21 10:27:22 | [diff] [blame] | 1025 | } else if (strcasecmp(*encoding, "raw") == 0) { |
| 1026 | fprintf(stderr, "'raw' (array of integers) has been removed. " |
| 1027 | "Use 'binary'.\n"); |
| 1028 | return BINARY; |
| 1029 | } else if (strcasecmp(*encoding, "raws") == 0) { |
| 1030 | fprintf(stderr, "'raws' encoding has been renamed to 'binary'. " |
| 1031 | "Please update your code.\n"); |
| 1032 | return BINARY; |
| 1033 | } else { |
| 1034 | return _default; |
| 1035 | } |
| 1036 | } |
| 1037 | |
Ryan | d6c9d31 | 2009-09-11 14:02:29 | [diff] [blame] | 1038 | Local<Value> Encode(const void *buf, size_t len, enum encoding encoding) { |
Ryan | 21a1b04 | 2009-09-09 13:51:49 | [diff] [blame] | 1039 | HandleScope scope; |
| 1040 | |
Ryan Dahl | 46ebaa0 | 2010-02-22 20:07:07 | [diff] [blame] | 1041 | if (!len) return scope.Close(String::Empty()); |
Ryan | 21a1b04 | 2009-09-09 13:51:49 | [diff] [blame] | 1042 | |
Ryan Dahl | 07792af | 2009-09-21 10:27:22 | [diff] [blame] | 1043 | if (encoding == BINARY) { |
Ryan | 8890070 | 2009-09-09 15:22:20 | [diff] [blame] | 1044 | const unsigned char *cbuf = static_cast<const unsigned char*>(buf); |
Ryan | d6c9d31 | 2009-09-11 14:02:29 | [diff] [blame] | 1045 | uint16_t * twobytebuf = new uint16_t[len]; |
Ryan | 8890070 | 2009-09-09 15:22:20 | [diff] [blame] | 1046 | for (size_t i = 0; i < len; i++) { |
Ryan | d6c9d31 | 2009-09-11 14:02:29 | [diff] [blame] | 1047 | // XXX is the following line platform independent? |
Ryan | 8890070 | 2009-09-09 15:22:20 | [diff] [blame] | 1048 | twobytebuf[i] = cbuf[i]; |
| 1049 | } |
| 1050 | Local<String> chunk = String::New(twobytebuf, len); |
Ryan Dahl | 663deb3 | 2009-09-22 16:35:15 | [diff] [blame] | 1051 | delete [] twobytebuf; // TODO use ExternalTwoByteString? |
Ryan | 8890070 | 2009-09-09 15:22:20 | [diff] [blame] | 1052 | return scope.Close(chunk); |
| 1053 | } |
| 1054 | |
Ryan | 21a1b04 | 2009-09-09 13:51:49 | [diff] [blame] | 1055 | // utf8 or ascii encoding |
| 1056 | Local<String> chunk = String::New((const char*)buf, len); |
| 1057 | return scope.Close(chunk); |
| 1058 | } |
| 1059 | |
Ryan | d6c9d31 | 2009-09-11 14:02:29 | [diff] [blame] | 1060 | // Returns -1 if the handle was not valid for decoding |
| 1061 | ssize_t DecodeBytes(v8::Handle<v8::Value> val, enum encoding encoding) { |
Ryan | 21a1b04 | 2009-09-09 13:51:49 | [diff] [blame] | 1062 | HandleScope scope; |
| 1063 | |
| 1064 | if (val->IsArray()) { |
Ryan Dahl | 07792af | 2009-09-21 10:27:22 | [diff] [blame] | 1065 | fprintf(stderr, "'raw' encoding (array of integers) has been removed. " |
| 1066 | "Use 'binary'.\n"); |
| 1067 | assert(0); |
| 1068 | return -1; |
Ryan | 21a1b04 | 2009-09-09 13:51:49 | [diff] [blame] | 1069 | } |
| 1070 | |
Ben Noordhuis | 9a79bb6 | 2012-01-08 23:45:19 | [diff] [blame] | 1071 | if (encoding == BINARY && Buffer::HasInstance(val)) { |
| 1072 | return Buffer::Length(val->ToObject()); |
| 1073 | } |
| 1074 | |
Ryan Dahl | 07792af | 2009-09-21 10:27:22 | [diff] [blame] | 1075 | Local<String> str = val->ToString(); |
Ryan | 21a1b04 | 2009-09-09 13:51:49 | [diff] [blame] | 1076 | |
Ryan | d6c9d31 | 2009-09-11 14:02:29 | [diff] [blame] | 1077 | if (encoding == UTF8) return str->Utf8Length(); |
Konstantin Käfer | 9e101f2 | 2011-02-06 20:49:52 | [diff] [blame] | 1078 | else if (encoding == UCS2) return str->Length() * 2; |
isaacs | 0aa1a8a | 2011-02-20 01:29:01 | [diff] [blame] | 1079 | else if (encoding == HEX) return str->Length() / 2; |
Ryan | 21a1b04 | 2009-09-09 13:51:49 | [diff] [blame] | 1080 | |
Ryan | d6c9d31 | 2009-09-11 14:02:29 | [diff] [blame] | 1081 | return str->Length(); |
Ryan | 21a1b04 | 2009-09-09 13:51:49 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | #ifndef MIN |
Ryan | d6c9d31 | 2009-09-11 14:02:29 | [diff] [blame] | 1085 | # define MIN(a, b) ((a) < (b) ? (a) : (b)) |
Ryan | 21a1b04 | 2009-09-09 13:51:49 | [diff] [blame] | 1086 | #endif |
| 1087 | |
| 1088 | // Returns number of bytes written. |
Ryan Dahl | 53530e9 | 2010-04-02 21:55:28 | [diff] [blame] | 1089 | ssize_t DecodeWrite(char *buf, |
| 1090 | size_t buflen, |
Ryan | d6c9d31 | 2009-09-11 14:02:29 | [diff] [blame] | 1091 | v8::Handle<v8::Value> val, |
| 1092 | enum encoding encoding) { |
Ryan | 21a1b04 | 2009-09-09 13:51:49 | [diff] [blame] | 1093 | HandleScope scope; |
| 1094 | |
| 1095 | // XXX |
| 1096 | // A lot of improvement can be made here. See: |
| 1097 | // http://code.google.com/p/v8/issues/detail?id=270 |
| 1098 | // http://groups.google.com/group/v8-dev/browse_thread/thread/dba28a81d9215291/ece2b50a3b4022c |
| 1099 | // http://groups.google.com/group/v8-users/browse_thread/thread/1f83b0ba1f0a611 |
| 1100 | |
| 1101 | if (val->IsArray()) { |
Ryan Dahl | 07792af | 2009-09-21 10:27:22 | [diff] [blame] | 1102 | fprintf(stderr, "'raw' encoding (array of integers) has been removed. " |
| 1103 | "Use 'binary'.\n"); |
| 1104 | assert(0); |
| 1105 | return -1; |
Ryan | 21a1b04 | 2009-09-09 13:51:49 | [diff] [blame] | 1106 | } |
| 1107 | |
Ryan Dahl | 07792af | 2009-09-21 10:27:22 | [diff] [blame] | 1108 | Local<String> str = val->ToString(); |
Ryan | 21a1b04 | 2009-09-09 13:51:49 | [diff] [blame] | 1109 | |
| 1110 | if (encoding == UTF8) { |
Ryan Dahl | 41ef171 | 2010-04-14 17:34:17 | [diff] [blame] | 1111 | str->WriteUtf8(buf, buflen, NULL, String::HINT_MANY_WRITES_EXPECTED); |
Ryan | 21a1b04 | 2009-09-09 13:51:49 | [diff] [blame] | 1112 | return buflen; |
| 1113 | } |
| 1114 | |
Ryan | 8890070 | 2009-09-09 15:22:20 | [diff] [blame] | 1115 | if (encoding == ASCII) { |
Ryan Dahl | 41ef171 | 2010-04-14 17:34:17 | [diff] [blame] | 1116 | str->WriteAscii(buf, 0, buflen, String::HINT_MANY_WRITES_EXPECTED); |
Ryan | 8890070 | 2009-09-09 15:22:20 | [diff] [blame] | 1117 | return buflen; |
| 1118 | } |
| 1119 | |
| 1120 | // THIS IS AWFUL!!! FIXME |
| 1121 | |
Ryan Dahl | 07792af | 2009-09-21 10:27:22 | [diff] [blame] | 1122 | assert(encoding == BINARY); |
| 1123 | |
Ryan | d6c9d31 | 2009-09-11 14:02:29 | [diff] [blame] | 1124 | uint16_t * twobytebuf = new uint16_t[buflen]; |
Ryan | 8890070 | 2009-09-09 15:22:20 | [diff] [blame] | 1125 | |
Ryan Dahl | 41ef171 | 2010-04-14 17:34:17 | [diff] [blame] | 1126 | str->Write(twobytebuf, 0, buflen, String::HINT_MANY_WRITES_EXPECTED); |
Ryan | 8890070 | 2009-09-09 15:22:20 | [diff] [blame] | 1127 | |
| 1128 | for (size_t i = 0; i < buflen; i++) { |
| 1129 | unsigned char *b = reinterpret_cast<unsigned char*>(&twobytebuf[i]); |
Ryan | 8890070 | 2009-09-09 15:22:20 | [diff] [blame] | 1130 | buf[i] = b[0]; |
| 1131 | } |
| 1132 | |
Ryan Dahl | 663deb3 | 2009-09-22 16:35:15 | [diff] [blame] | 1133 | delete [] twobytebuf; |
Ryan | d6c9d31 | 2009-09-11 14:02:29 | [diff] [blame] | 1134 | |
Ryan | 21a1b04 | 2009-09-09 13:51:49 | [diff] [blame] | 1135 | return buflen; |
| 1136 | } |
| 1137 | |
Ryan | 5131e0a | 2009-03-09 00:23:41 | [diff] [blame] | 1138 | |
Ryan Dahl | b57c1f5 | 2010-11-24 02:46:13 | [diff] [blame] | 1139 | void DisplayExceptionLine (TryCatch &try_catch) { |
| 1140 | HandleScope scope; |
| 1141 | |
Ryan Dahl | b20c343 | 2010-02-12 05:55:08 | [diff] [blame] | 1142 | Handle<Message> message = try_catch.Message(); |
Ryan Dahl | ef300d1 | 2009-09-13 15:43:19 | [diff] [blame] | 1143 | |
Ryan Dahl | cdf5d91 | 2011-10-11 20:41:33 | [diff] [blame] | 1144 | uv_tty_reset_mode(); |
| 1145 | |
Ryan Dahl | b3c0359 | 2010-07-27 02:08:21 | [diff] [blame] | 1146 | fprintf(stderr, "\n"); |
Ryan Dahl | 41f213b | 2010-05-31 18:50:35 | [diff] [blame] | 1147 | |
Ryan Dahl | b57c1f5 | 2010-11-24 02:46:13 | [diff] [blame] | 1148 | if (!message.IsEmpty()) { |
Ryan Dahl | 53a841d | 2009-12-29 19:20:51 | [diff] [blame] | 1149 | // Print (filename):(line number): (message). |
| 1150 | String::Utf8Value filename(message->GetScriptResourceName()); |
Ryan Dahl | b57c1f5 | 2010-11-24 02:46:13 | [diff] [blame] | 1151 | const char* filename_string = *filename; |
Ryan Dahl | 53a841d | 2009-12-29 19:20:51 | [diff] [blame] | 1152 | int linenum = message->GetLineNumber(); |
| 1153 | fprintf(stderr, "%s:%i\n", filename_string, linenum); |
| 1154 | // Print line of source code. |
| 1155 | String::Utf8Value sourceline(message->GetSourceLine()); |
Ryan Dahl | b57c1f5 | 2010-11-24 02:46:13 | [diff] [blame] | 1156 | const char* sourceline_string = *sourceline; |
Ryan Dahl | ab068db | 2010-05-09 20:54:58 | [diff] [blame] | 1157 | |
| 1158 | // HACK HACK HACK |
| 1159 | // |
| 1160 | // FIXME |
| 1161 | // |
| 1162 | // Because of how CommonJS modules work, all scripts are wrapped with a |
| 1163 | // "function (function (exports, __filename, ...) {" |
| 1164 | // to provide script local variables. |
| 1165 | // |
| 1166 | // When reporting errors on the first line of a script, this wrapper |
| 1167 | // function is leaked to the user. This HACK is to remove it. The length |
isaacs | 703a1ff | 2011-07-29 18:56:05 | [diff] [blame] | 1168 | // of the wrapper is 62. That wrapper is defined in src/node.js |
Ryan Dahl | ab068db | 2010-05-09 20:54:58 | [diff] [blame] | 1169 | // |
| 1170 | // If that wrapper is ever changed, then this number also has to be |
| 1171 | // updated. Or - someone could clean this up so that the two peices |
| 1172 | // don't need to be changed. |
| 1173 | // |
| 1174 | // Even better would be to get support into V8 for wrappers that |
| 1175 | // shouldn't be reported to users. |
isaacs | 703a1ff | 2011-07-29 18:56:05 | [diff] [blame] | 1176 | int offset = linenum == 1 ? 62 : 0; |
Ryan Dahl | ab068db | 2010-05-09 20:54:58 | [diff] [blame] | 1177 | |
| 1178 | fprintf(stderr, "%s\n", sourceline_string + offset); |
Ryan Dahl | 53a841d | 2009-12-29 19:20:51 | [diff] [blame] | 1179 | // Print wavy underline (GetUnderline is deprecated). |
| 1180 | int start = message->GetStartColumn(); |
Ryan Dahl | ab068db | 2010-05-09 20:54:58 | [diff] [blame] | 1181 | for (int i = offset; i < start; i++) { |
Ryan Dahl | 53a841d | 2009-12-29 19:20:51 | [diff] [blame] | 1182 | fprintf(stderr, " "); |
| 1183 | } |
| 1184 | int end = message->GetEndColumn(); |
| 1185 | for (int i = start; i < end; i++) { |
| 1186 | fprintf(stderr, "^"); |
| 1187 | } |
| 1188 | fprintf(stderr, "\n"); |
Ryan Dahl | 8e6dd52 | 2010-01-15 18:45:04 | [diff] [blame] | 1189 | } |
Ryan Dahl | b57c1f5 | 2010-11-24 02:46:13 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | |
| 1193 | static void ReportException(TryCatch &try_catch, bool show_line) { |
| 1194 | HandleScope scope; |
| 1195 | Handle<Message> message = try_catch.Message(); |
| 1196 | |
| 1197 | if (show_line) DisplayExceptionLine(try_catch); |
Ryan Dahl | 53a841d | 2009-12-29 19:20:51 | [diff] [blame] | 1198 | |
Ryan Dahl | da93230 | 2010-05-14 18:48:14 | [diff] [blame] | 1199 | String::Utf8Value trace(try_catch.StackTrace()); |
| 1200 | |
isaacs | 8df6f9e | 2011-04-25 19:22:18 | [diff] [blame] | 1201 | // range errors have a trace member set to undefined |
| 1202 | if (trace.length() > 0 && !try_catch.StackTrace()->IsUndefined()) { |
Ryan | afd9e71 | 2009-08-31 16:22:09 | [diff] [blame] | 1203 | fprintf(stderr, "%s\n", *trace); |
isaacs | e9b6b0b | 2010-10-02 06:22:34 | [diff] [blame] | 1204 | } else { |
| 1205 | // this really only happens for RangeErrors, since they're the only |
isaacs | 8df6f9e | 2011-04-25 19:22:18 | [diff] [blame] | 1206 | // kind that won't have all this info in the trace, or when non-Error |
| 1207 | // objects are thrown manually. |
isaacs | e9b6b0b | 2010-10-02 06:22:34 | [diff] [blame] | 1208 | Local<Value> er = try_catch.Exception(); |
isaacs | 8df6f9e | 2011-04-25 19:22:18 | [diff] [blame] | 1209 | bool isErrorObject = er->IsObject() && |
| 1210 | !(er->ToObject()->Get(String::New("message"))->IsUndefined()) && |
| 1211 | !(er->ToObject()->Get(String::New("name"))->IsUndefined()); |
| 1212 | |
| 1213 | if (isErrorObject) { |
| 1214 | String::Utf8Value name(er->ToObject()->Get(String::New("name"))); |
| 1215 | fprintf(stderr, "%s: ", *name); |
| 1216 | } |
| 1217 | |
| 1218 | String::Utf8Value msg(!isErrorObject ? er->ToString() |
isaacs | e9b6b0b | 2010-10-02 06:22:34 | [diff] [blame] | 1219 | : er->ToObject()->Get(String::New("message"))->ToString()); |
| 1220 | fprintf(stderr, "%s\n", *msg); |
Ryan | 5131e0a | 2009-03-09 00:23:41 | [diff] [blame] | 1221 | } |
isaacs | e9b6b0b | 2010-10-02 06:22:34 | [diff] [blame] | 1222 | |
Ryan | d7e220c | 2009-09-09 20:35:40 | [diff] [blame] | 1223 | fflush(stderr); |
Ryan | 5131e0a | 2009-03-09 00:23:41 | [diff] [blame] | 1224 | } |
| 1225 | |
Ryan | d6c9d31 | 2009-09-11 14:02:29 | [diff] [blame] | 1226 | // Executes a str within the current v8 context. |
Tom Hughes | 74954ce | 2011-03-08 06:00:51 | [diff] [blame] | 1227 | Local<Value> ExecuteString(Handle<String> source, Handle<Value> filename) { |
Ryan | 63a9cd3 | 2009-04-15 08:08:28 | [diff] [blame] | 1228 | HandleScope scope; |
Ryan | 408526a | 2009-04-21 11:52:21 | [diff] [blame] | 1229 | TryCatch try_catch; |
| 1230 | |
Herbert Vojcik | c2a0672 | 2010-04-17 15:18:15 | [diff] [blame] | 1231 | Local<v8::Script> script = v8::Script::Compile(source, filename); |
Ryan | 63a9cd3 | 2009-04-15 08:08:28 | [diff] [blame] | 1232 | if (script.IsEmpty()) { |
Ryan Dahl | ab068db | 2010-05-09 20:54:58 | [diff] [blame] | 1233 | ReportException(try_catch, true); |
Ryan | 34a6f10 | 2009-05-28 12:47:16 | [diff] [blame] | 1234 | exit(1); |
Ryan | 63a9cd3 | 2009-04-15 08:08:28 | [diff] [blame] | 1235 | } |
| 1236 | |
Ryan Dahl | 9f5643f | 2010-01-31 07:22:34 | [diff] [blame] | 1237 | Local<Value> result = script->Run(); |
Ryan | 63a9cd3 | 2009-04-15 08:08:28 | [diff] [blame] | 1238 | if (result.IsEmpty()) { |
Ryan Dahl | ab068db | 2010-05-09 20:54:58 | [diff] [blame] | 1239 | ReportException(try_catch, true); |
Ryan | 34a6f10 | 2009-05-28 12:47:16 | [diff] [blame] | 1240 | exit(1); |
Ryan | 63a9cd3 | 2009-04-15 08:08:28 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | return scope.Close(result); |
| 1244 | } |
| 1245 | |
Felix Geisendörfer | 7371fcb | 2009-11-11 17:10:58 | [diff] [blame] | 1246 | |
Robert Mustacchi | 2240486 | 2011-12-15 01:02:15 | [diff] [blame] | 1247 | static Handle<Value> Abort(const Arguments& args) { |
| 1248 | abort(); |
Igor Zinkovsky | 88cc688 | 2011-12-16 01:18:28 | [diff] [blame] | 1249 | return Undefined(); |
Robert Mustacchi | 2240486 | 2011-12-15 01:02:15 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | |
Brandon Beacher | 47fcf78 | 2009-11-03 18:13:38 | [diff] [blame] | 1253 | static Handle<Value> Chdir(const Arguments& args) { |
| 1254 | HandleScope scope; |
Ryan Dahl | b20c343 | 2010-02-12 05:55:08 | [diff] [blame] | 1255 | |
Brandon Beacher | 47fcf78 | 2009-11-03 18:13:38 | [diff] [blame] | 1256 | if (args.Length() != 1 || !args[0]->IsString()) { |
| 1257 | return ThrowException(Exception::Error(String::New("Bad argument."))); |
| 1258 | } |
Ryan Dahl | b20c343 | 2010-02-12 05:55:08 | [diff] [blame] | 1259 | |
Brandon Beacher | 47fcf78 | 2009-11-03 18:13:38 | [diff] [blame] | 1260 | String::Utf8Value path(args[0]->ToString()); |
Ryan Dahl | b20c343 | 2010-02-12 05:55:08 | [diff] [blame] | 1261 | |
Bert Belder | e84edd2 | 2011-12-01 23:24:44 | [diff] [blame] | 1262 | uv_err_t r = uv_chdir(*path); |
Ryan Dahl | b20c343 | 2010-02-12 05:55:08 | [diff] [blame] | 1263 | |
Bert Belder | e84edd2 | 2011-12-01 23:24:44 | [diff] [blame] | 1264 | if (r.code != UV_OK) { |
| 1265 | return ThrowException(UVException(r.code, "uv_chdir")); |
Brandon Beacher | 47fcf78 | 2009-11-03 18:13:38 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | return Undefined(); |
| 1269 | } |
| 1270 | |
Bert Belder | cbcf4fe | 2011-11-24 01:19:54 | [diff] [blame] | 1271 | |
Ryan | d6c9d31 | 2009-09-11 14:02:29 | [diff] [blame] | 1272 | static Handle<Value> Cwd(const Arguments& args) { |
Michael Carter | 8ea6adc | 2009-09-01 09:39:30 | [diff] [blame] | 1273 | HandleScope scope; |
Bert Belder | e84edd2 | 2011-12-01 23:24:44 | [diff] [blame] | 1274 | #ifdef _WIN32 |
| 1275 | /* MAX_PATH is in characters, not bytes. Make sure we have enough headroom. */ |
| 1276 | char buf[MAX_PATH * 4 + 1]; |
| 1277 | #else |
| 1278 | char buf[PATH_MAX + 1]; |
| 1279 | #endif |
Michael Carter | 8ea6adc | 2009-09-01 09:39:30 | [diff] [blame] | 1280 | |
Bert Belder | e84edd2 | 2011-12-01 23:24:44 | [diff] [blame] | 1281 | uv_err_t r = uv_cwd(buf, ARRAY_SIZE(buf) - 1); |
| 1282 | if (r.code != UV_OK) { |
| 1283 | return ThrowException(UVException(r.code, "uv_cwd")); |
Michael Carter | 8ea6adc | 2009-09-01 09:39:30 | [diff] [blame] | 1284 | } |
Peter Griess | 4e3c5d8 | 2010-07-12 15:47:45 | [diff] [blame] | 1285 | |
Bert Belder | e84edd2 | 2011-12-01 23:24:44 | [diff] [blame] | 1286 | buf[ARRAY_SIZE(buf) - 1] = '\0'; |
| 1287 | Local<String> cwd = String::New(buf); |
Michael Carter | 8ea6adc | 2009-09-01 09:39:30 | [diff] [blame] | 1288 | |
| 1289 | return scope.Close(cwd); |
| 1290 | } |
| 1291 | |
Bert Belder | e84edd2 | 2011-12-01 23:24:44 | [diff] [blame] | 1292 | |
Bert Belder | 86fba38 | 2011-11-24 01:38:34 | [diff] [blame] | 1293 | #ifdef _WIN32 |
| 1294 | static Handle<Value> CwdForDrive(const Arguments& args) { |
| 1295 | HandleScope scope; |
| 1296 | |
| 1297 | if (args.Length() < 1) { |
| 1298 | Local<Value> exception = Exception::Error( |
| 1299 | String::New("process._cwdForDrive takes exactly 1 argument.")); |
| 1300 | return ThrowException(exception); |
| 1301 | } |
| 1302 | |
| 1303 | Local<String> driveLetter = args[0]->ToString(); |
| 1304 | if (driveLetter->Length() != 1) { |
| 1305 | Local<Value> exception = Exception::Error( |
| 1306 | String::New("Drive name should be 1 character.")); |
| 1307 | return ThrowException(exception); |
| 1308 | } |
| 1309 | |
| 1310 | char drive; |
| 1311 | |
| 1312 | driveLetter->WriteAscii(&drive, 0, 1, 0); |
| 1313 | if (drive >= 'a' && drive <= 'z') { |
| 1314 | // Convert to uppercase |
| 1315 | drive += 'A' - 'a'; |
| 1316 | } else if (drive < 'A' || drive > 'Z') { |
| 1317 | // Not a letter |
| 1318 | Local<Value> exception = Exception::Error( |
| 1319 | String::New("Drive name should be a letter.")); |
| 1320 | return ThrowException(exception); |
| 1321 | } |
| 1322 | |
| 1323 | WCHAR env_key[] = L"=X:"; |
| 1324 | env_key[1] = (WCHAR) drive; |
| 1325 | |
| 1326 | DWORD len = GetEnvironmentVariableW(env_key, NULL, 0); |
| 1327 | if (len == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND) { |
| 1328 | // There is no current directory for that drive. Default to drive + ":\". |
| 1329 | Local<String> cwd = String::Concat(String::New(&drive, 1), |
| 1330 | String::New(":\\")); |
| 1331 | return scope.Close(cwd); |
| 1332 | |
| 1333 | } else if (len == 0) { |
| 1334 | // Error |
| 1335 | Local<Value> exception = Exception::Error( |
| 1336 | String::New(winapi_strerror(GetLastError()))); |
| 1337 | return ThrowException(exception); |
| 1338 | } |
| 1339 | |
| 1340 | WCHAR* buffer = new WCHAR[len]; |
| 1341 | if (buffer == NULL) { |
| 1342 | Local<Value> exception = Exception::Error( |
| 1343 | String::New("Out of memory.")); |
| 1344 | return ThrowException(exception); |
| 1345 | } |
| 1346 | |
| 1347 | DWORD len2 = GetEnvironmentVariableW(env_key, buffer, len); |
| 1348 | if (len2 == 0 || len2 >= len) { |
| 1349 | // Error |
| 1350 | delete[] buffer; |
| 1351 | Local<Value> exception = Exception::Error( |
| 1352 | String::New(winapi_strerror(GetLastError()))); |
| 1353 | return ThrowException(exception); |
| 1354 | } |
| 1355 | |
| 1356 | Local<String> cwd = String::New(reinterpret_cast<uint16_t*>(buffer), len2); |
| 1357 | delete[] buffer; |
| 1358 | return scope.Close(cwd); |
| 1359 | } |
| 1360 | #endif |
| 1361 | |
| 1362 | |
Ryan Dahl | acc120a | 2011-08-09 20:53:56 | [diff] [blame] | 1363 | static Handle<Value> Umask(const Arguments& args) { |
Friedemann Altrock | 0433d82 | 2009-11-22 18:52:52 | [diff] [blame] | 1364 | HandleScope scope; |
Rasmus Andersson | 374300c | 2010-02-27 17:18:41 | [diff] [blame] | 1365 | unsigned int old; |
isaacs | 5f2e909 | 2011-01-25 18:40:12 | [diff] [blame] | 1366 | |
Ryan Dahl | acc120a | 2011-08-09 20:53:56 | [diff] [blame] | 1367 | if (args.Length() < 1 || args[0]->IsUndefined()) { |
Rasmus Andersson | 374300c | 2010-02-27 17:18:41 | [diff] [blame] | 1368 | old = umask(0); |
| 1369 | umask((mode_t)old); |
isaacs | 5f2e909 | 2011-01-25 18:40:12 | [diff] [blame] | 1370 | |
| 1371 | } else if(!args[0]->IsInt32() && !args[0]->IsString()) { |
Friedemann Altrock | 0433d82 | 2009-11-22 18:52:52 | [diff] [blame] | 1372 | return ThrowException(Exception::TypeError( |
isaacs | 5f2e909 | 2011-01-25 18:40:12 | [diff] [blame] | 1373 | String::New("argument must be an integer or octal string."))); |
| 1374 | |
| 1375 | } else { |
| 1376 | int oct; |
| 1377 | if(args[0]->IsInt32()) { |
| 1378 | oct = args[0]->Uint32Value(); |
| 1379 | } else { |
| 1380 | oct = 0; |
| 1381 | String::Utf8Value str(args[0]); |
| 1382 | |
| 1383 | // Parse the octal string. |
| 1384 | for (int i = 0; i < str.length(); i++) { |
| 1385 | char c = (*str)[i]; |
| 1386 | if (c > '7' || c < '0') { |
| 1387 | return ThrowException(Exception::TypeError( |
| 1388 | String::New("invalid octal string"))); |
| 1389 | } |
| 1390 | oct *= 8; |
| 1391 | oct += c - '0'; |
| 1392 | } |
| 1393 | } |
| 1394 | old = umask(static_cast<mode_t>(oct)); |
Friedemann Altrock | 0433d82 | 2009-11-22 18:52:52 | [diff] [blame] | 1395 | } |
isaacs | 5f2e909 | 2011-01-25 18:40:12 | [diff] [blame] | 1396 | |
Friedemann Altrock | 0433d82 | 2009-11-22 18:52:52 | [diff] [blame] | 1397 | return scope.Close(Uint32::New(old)); |
| 1398 | } |
| 1399 | |
Michael Carter | a386076 | 2010-02-08 06:13:10 | [diff] [blame] | 1400 | |
Ryan Dahl | acc120a | 2011-08-09 20:53:56 | [diff] [blame] | 1401 | #ifdef __POSIX__ |
| 1402 | |
Michael Carter | a386076 | 2010-02-08 06:13:10 | [diff] [blame] | 1403 | static Handle<Value> GetUid(const Arguments& args) { |
| 1404 | HandleScope scope; |
Ryan Dahl | 3994340 | 2010-03-15 19:49:40 | [diff] [blame] | 1405 | assert(args.Length() == 0); |
Michael Carter | a386076 | 2010-02-08 06:13:10 | [diff] [blame] | 1406 | int uid = getuid(); |
| 1407 | return scope.Close(Integer::New(uid)); |
| 1408 | } |
| 1409 | |
Ryan Dahl | acc120a | 2011-08-09 20:53:56 | [diff] [blame] | 1410 | |
James Duncan | df1c1e5 | 2010-02-23 22:45:02 | [diff] [blame] | 1411 | static Handle<Value> GetGid(const Arguments& args) { |
| 1412 | HandleScope scope; |
Ryan Dahl | 3994340 | 2010-03-15 19:49:40 | [diff] [blame] | 1413 | assert(args.Length() == 0); |
James Duncan | df1c1e5 | 2010-02-23 22:45:02 | [diff] [blame] | 1414 | int gid = getgid(); |
| 1415 | return scope.Close(Integer::New(gid)); |
| 1416 | } |
| 1417 | |
| 1418 | |
| 1419 | static Handle<Value> SetGid(const Arguments& args) { |
| 1420 | HandleScope scope; |
Ryan Dahl | 3994340 | 2010-03-15 19:49:40 | [diff] [blame] | 1421 | |
James Duncan | df1c1e5 | 2010-02-23 22:45:02 | [diff] [blame] | 1422 | if (args.Length() < 1) { |
| 1423 | return ThrowException(Exception::Error( |
Ryan Dahl | 3994340 | 2010-03-15 19:49:40 | [diff] [blame] | 1424 | String::New("setgid requires 1 argument"))); |
James Duncan | df1c1e5 | 2010-02-23 22:45:02 | [diff] [blame] | 1425 | } |
| 1426 | |
Peter Griess | 2420f07 | 2010-05-19 00:40:44 | [diff] [blame] | 1427 | int gid; |
Blake Mizerany | 8c85340 | 2010-06-30 06:12:46 | [diff] [blame] | 1428 | |
Peter Griess | 2420f07 | 2010-05-19 00:40:44 | [diff] [blame] | 1429 | if (args[0]->IsNumber()) { |
| 1430 | gid = args[0]->Int32Value(); |
| 1431 | } else if (args[0]->IsString()) { |
| 1432 | String::Utf8Value grpnam(args[0]->ToString()); |
| 1433 | struct group grp, *grpp = NULL; |
| 1434 | int err; |
| 1435 | |
Peter Griess | 4e3c5d8 | 2010-07-12 15:47:45 | [diff] [blame] | 1436 | if ((err = getgrnam_r(*grpnam, &grp, getbuf, ARRAY_SIZE(getbuf), &grpp)) || |
Peter Griess | 2420f07 | 2010-05-19 00:40:44 | [diff] [blame] | 1437 | grpp == NULL) { |
Brian White | 52b9ede | 2011-03-18 05:39:39 | [diff] [blame] | 1438 | if (errno == 0) |
| 1439 | return ThrowException(Exception::Error( |
| 1440 | String::New("setgid group id does not exist"))); |
| 1441 | else |
| 1442 | return ThrowException(ErrnoException(errno, "getgrnam_r")); |
Peter Griess | 2420f07 | 2010-05-19 00:40:44 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | gid = grpp->gr_gid; |
| 1446 | } else { |
| 1447 | return ThrowException(Exception::Error( |
| 1448 | String::New("setgid argument must be a number or a string"))); |
| 1449 | } |
| 1450 | |
James Duncan | df1c1e5 | 2010-02-23 22:45:02 | [diff] [blame] | 1451 | int result; |
isaacs | 0dba38e | 2010-03-03 09:11:47 | [diff] [blame] | 1452 | if ((result = setgid(gid)) != 0) { |
Peter Griess | 2420f07 | 2010-05-19 00:40:44 | [diff] [blame] | 1453 | return ThrowException(ErrnoException(errno, "setgid")); |
James Duncan | df1c1e5 | 2010-02-23 22:45:02 | [diff] [blame] | 1454 | } |
| 1455 | return Undefined(); |
| 1456 | } |
Michael Carter | a386076 | 2010-02-08 06:13:10 | [diff] [blame] | 1457 | |
Ryan Dahl | acc120a | 2011-08-09 20:53:56 | [diff] [blame] | 1458 | |
Michael Carter | a386076 | 2010-02-08 06:13:10 | [diff] [blame] | 1459 | static Handle<Value> SetUid(const Arguments& args) { |
| 1460 | HandleScope scope; |
| 1461 | |
| 1462 | if (args.Length() < 1) { |
| 1463 | return ThrowException(Exception::Error( |
| 1464 | String::New("setuid requires 1 argument"))); |
| 1465 | } |
| 1466 | |
Peter Griess | 2420f07 | 2010-05-19 00:40:44 | [diff] [blame] | 1467 | int uid; |
| 1468 | |
| 1469 | if (args[0]->IsNumber()) { |
| 1470 | uid = args[0]->Int32Value(); |
| 1471 | } else if (args[0]->IsString()) { |
| 1472 | String::Utf8Value pwnam(args[0]->ToString()); |
| 1473 | struct passwd pwd, *pwdp = NULL; |
| 1474 | int err; |
| 1475 | |
Peter Griess | 4e3c5d8 | 2010-07-12 15:47:45 | [diff] [blame] | 1476 | if ((err = getpwnam_r(*pwnam, &pwd, getbuf, ARRAY_SIZE(getbuf), &pwdp)) || |
Peter Griess | 2420f07 | 2010-05-19 00:40:44 | [diff] [blame] | 1477 | pwdp == NULL) { |
Brian White | 52b9ede | 2011-03-18 05:39:39 | [diff] [blame] | 1478 | if (errno == 0) |
| 1479 | return ThrowException(Exception::Error( |
| 1480 | String::New("setuid user id does not exist"))); |
| 1481 | else |
| 1482 | return ThrowException(ErrnoException(errno, "getpwnam_r")); |
Peter Griess | 2420f07 | 2010-05-19 00:40:44 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | uid = pwdp->pw_uid; |
| 1486 | } else { |
| 1487 | return ThrowException(Exception::Error( |
| 1488 | String::New("setuid argument must be a number or a string"))); |
| 1489 | } |
| 1490 | |
Michael Carter | a386076 | 2010-02-08 06:13:10 | [diff] [blame] | 1491 | int result; |
| 1492 | if ((result = setuid(uid)) != 0) { |
Peter Griess | 2420f07 | 2010-05-19 00:40:44 | [diff] [blame] | 1493 | return ThrowException(ErrnoException(errno, "setuid")); |
Michael Carter | a386076 | 2010-02-08 06:13:10 | [diff] [blame] | 1494 | } |
| 1495 | return Undefined(); |
| 1496 | } |
| 1497 | |
Ryan Dahl | acc120a | 2011-08-09 20:53:56 | [diff] [blame] | 1498 | |
Bert Belder | 30bab52 | 2010-11-25 00:09:06 | [diff] [blame] | 1499 | #endif // __POSIX__ |
| 1500 | |
Michael Carter | a386076 | 2010-02-08 06:13:10 | [diff] [blame] | 1501 | |
Ryan | d6c9d31 | 2009-09-11 14:02:29 | [diff] [blame] | 1502 | v8::Handle<v8::Value> Exit(const v8::Arguments& args) { |
Ryan Dahl | 6f92d8f | 2010-02-08 05:59:56 | [diff] [blame] | 1503 | HandleScope scope; |
Ryan Dahl | 6f92d8f | 2010-02-08 05:59:56 | [diff] [blame] | 1504 | exit(args[0]->IntegerValue()); |
Ryan | 116f4de | 2009-08-26 20:03:19 | [diff] [blame] | 1505 | return Undefined(); |
Ryan | 0f51703 | 2009-04-29 09:09:32 | [diff] [blame] | 1506 | } |
| 1507 | |
Ryan Dahl | 3ac6dee | 2010-05-08 02:05:59 | [diff] [blame] | 1508 | |
Ryan Dahl | f657d58 | 2011-06-22 12:06:26 | [diff] [blame] | 1509 | static void CheckStatus(uv_timer_t* watcher, int status) { |
| 1510 | assert(watcher == &gc_timer); |
Ryan Dahl | 3ac6dee | 2010-05-08 02:05:59 | [diff] [blame] | 1511 | |
Ryan Dahl | 3ac6dee | 2010-05-08 02:05:59 | [diff] [blame] | 1512 | // check memory |
Ryan Dahl | 7a5977b | 2011-06-07 16:37:27 | [diff] [blame] | 1513 | if (!uv_is_active((uv_handle_t*) &gc_idle)) { |
Ryan Dahl | aeed966 | 2011-03-18 18:39:44 | [diff] [blame] | 1514 | HeapStatistics stats; |
| 1515 | V8::GetHeapStatistics(&stats); |
| 1516 | if (stats.total_heap_size() > 1024 * 1024 * 128) { |
Ryan Dahl | 3ac6dee | 2010-05-08 02:05:59 | [diff] [blame] | 1517 | // larger than 128 megs, just start the idle watcher |
Ryan Dahl | 7547c7d | 2011-12-07 01:00:33 | [diff] [blame] | 1518 | uv_idle_start(&gc_idle, node::Idle); |
Ryan Dahl | 3ac6dee | 2010-05-08 02:05:59 | [diff] [blame] | 1519 | return; |
| 1520 | } |
| 1521 | } |
Ryan Dahl | 3ac6dee | 2010-05-08 02:05:59 | [diff] [blame] | 1522 | |
Ben Noordhuis | 74a8215 | 2012-02-03 15:32:00 | [diff] [blame^] | 1523 | double d = uv_now(uv_default_loop()) - TICK_TIME(3); |
Ryan Dahl | 3ac6dee | 2010-05-08 02:05:59 | [diff] [blame] | 1524 | |
| 1525 | //printfb("timer d = %f\n", d); |
| 1526 | |
| 1527 | if (d >= GC_WAIT_TIME - 1.) { |
| 1528 | //fprintf(stderr, "start idle\n"); |
Ryan Dahl | 7547c7d | 2011-12-07 01:00:33 | [diff] [blame] | 1529 | uv_idle_start(&gc_idle, node::Idle); |
Ryan Dahl | 3ac6dee | 2010-05-08 02:05:59 | [diff] [blame] | 1530 | } |
| 1531 | } |
| 1532 | |
Igor Zinkovsky | 500c8f4 | 2011-12-15 20:36:05 | [diff] [blame] | 1533 | |
Tom Hughes | cf78ce5 | 2011-03-04 23:57:54 | [diff] [blame] | 1534 | static Handle<Value> Uptime(const Arguments& args) { |
| 1535 | HandleScope scope; |
| 1536 | assert(args.Length() == 0); |
Igor Zinkovsky | 500c8f4 | 2011-12-15 20:36:05 | [diff] [blame] | 1537 | double uptime; |
Tom Hughes | cf78ce5 | 2011-03-04 23:57:54 | [diff] [blame] | 1538 | |
Igor Zinkovsky | 500c8f4 | 2011-12-15 20:36:05 | [diff] [blame] | 1539 | uv_err_t err = uv_uptime(&uptime); |
Tom Hughes | cf78ce5 | 2011-03-04 23:57:54 | [diff] [blame] | 1540 | |
Igor Zinkovsky | 500c8f4 | 2011-12-15 20:36:05 | [diff] [blame] | 1541 | if (err.code != UV_OK) { |
Tom Hughes | cf78ce5 | 2011-03-04 23:57:54 | [diff] [blame] | 1542 | return Undefined(); |
| 1543 | } |
| 1544 | |
Igor Zinkovsky | 500c8f4 | 2011-12-15 20:36:05 | [diff] [blame] | 1545 | return scope.Close(Number::New(uptime - prog_start_time)); |
Tom Hughes | cf78ce5 | 2011-03-04 23:57:54 | [diff] [blame] | 1546 | } |
Ryan Dahl | 3ac6dee | 2010-05-08 02:05:59 | [diff] [blame] | 1547 | |
Ryan Dahl | c344fbc | 2011-10-06 21:59:38 | [diff] [blame] | 1548 | |
| 1549 | v8::Handle<v8::Value> UVCounters(const v8::Arguments& args) { |
| 1550 | HandleScope scope; |
| 1551 | |
Ben Noordhuis | 74a8215 | 2012-02-03 15:32:00 | [diff] [blame^] | 1552 | uv_counters_t* c = &uv_default_loop()->counters; |
Ryan Dahl | c344fbc | 2011-10-06 21:59:38 | [diff] [blame] | 1553 | |
| 1554 | Local<Object> obj = Object::New(); |
| 1555 | |
Bert Belder | 889620d | 2011-10-28 09:34:53 | [diff] [blame] | 1556 | #define setc(name) \ |
| 1557 | obj->Set(String::New(#name), Integer::New(static_cast<int32_t>(c->name))); |
Ryan Dahl | c344fbc | 2011-10-06 21:59:38 | [diff] [blame] | 1558 | |
| 1559 | setc(eio_init) |
| 1560 | setc(req_init) |
| 1561 | setc(handle_init) |
| 1562 | setc(stream_init) |
| 1563 | setc(tcp_init) |
| 1564 | setc(udp_init) |
| 1565 | setc(pipe_init) |
| 1566 | setc(tty_init) |
| 1567 | setc(prepare_init) |
| 1568 | setc(check_init) |
| 1569 | setc(idle_init) |
| 1570 | setc(async_init) |
| 1571 | setc(timer_init) |
| 1572 | setc(process_init) |
| 1573 | setc(fs_event_init) |
| 1574 | |
| 1575 | #undef setc |
| 1576 | |
| 1577 | return scope.Close(obj); |
| 1578 | } |
| 1579 | |
| 1580 | |
Ryan Dahl | b3b3cfe | 2009-11-03 12:00:42 | [diff] [blame] | 1581 | v8::Handle<v8::Value> MemoryUsage(const v8::Arguments& args) { |
| 1582 | HandleScope scope; |
Ryan Dahl | 3994340 | 2010-03-15 19:49:40 | [diff] [blame] | 1583 | assert(args.Length() == 0); |
Ryan Dahl | b3b3cfe | 2009-11-03 12:00:42 | [diff] [blame] | 1584 | |
Ryan Dahl | 5783a52 | 2011-10-18 21:30:31 | [diff] [blame] | 1585 | size_t rss; |
Ryan Dahl | b3b3cfe | 2009-11-03 12:00:42 | [diff] [blame] | 1586 | |
Igor Zinkovsky | 500c8f4 | 2011-12-15 20:36:05 | [diff] [blame] | 1587 | uv_err_t err = uv_resident_set_memory(&rss); |
Ryan Dahl | b3b3cfe | 2009-11-03 12:00:42 | [diff] [blame] | 1588 | |
Igor Zinkovsky | 500c8f4 | 2011-12-15 20:36:05 | [diff] [blame] | 1589 | if (err.code != UV_OK) { |
| 1590 | return ThrowException(UVException(err.code, "uv_resident_set_memory")); |
Ryan Dahl | 3a70129 | 2009-11-03 00:30:01 | [diff] [blame] | 1591 | } |
| 1592 | |
Ryan Dahl | 3a70129 | 2009-11-03 00:30:01 | [diff] [blame] | 1593 | Local<Object> info = Object::New(); |
| 1594 | |
Ryan Dahl | 45a806a | 2009-12-09 08:02:21 | [diff] [blame] | 1595 | if (rss_symbol.IsEmpty()) { |
| 1596 | rss_symbol = NODE_PSYMBOL("rss"); |
Ryan Dahl | 45a806a | 2009-12-09 08:02:21 | [diff] [blame] | 1597 | heap_total_symbol = NODE_PSYMBOL("heapTotal"); |
| 1598 | heap_used_symbol = NODE_PSYMBOL("heapUsed"); |
| 1599 | } |
| 1600 | |
Russ Bradberry | 754e23d | 2011-11-29 22:28:22 | [diff] [blame] | 1601 | info->Set(rss_symbol, Number::New(rss)); |
Ryan Dahl | 3a70129 | 2009-11-03 00:30:01 | [diff] [blame] | 1602 | |
Ryan Dahl | 38e425d | 2009-11-28 15:31:29 | [diff] [blame] | 1603 | // V8 memory usage |
| 1604 | HeapStatistics v8_heap_stats; |
| 1605 | V8::GetHeapStatistics(&v8_heap_stats); |
Ryan Dahl | 45a806a | 2009-12-09 08:02:21 | [diff] [blame] | 1606 | info->Set(heap_total_symbol, |
Ryan Dahl | 38e425d | 2009-11-28 15:31:29 | [diff] [blame] | 1607 | Integer::NewFromUnsigned(v8_heap_stats.total_heap_size())); |
Ryan Dahl | 45a806a | 2009-12-09 08:02:21 | [diff] [blame] | 1608 | info->Set(heap_used_symbol, |
Ryan Dahl | 38e425d | 2009-11-28 15:31:29 | [diff] [blame] | 1609 | Integer::NewFromUnsigned(v8_heap_stats.used_heap_size())); |
| 1610 | |
Ryan Dahl | 3a70129 | 2009-11-03 00:30:01 | [diff] [blame] | 1611 | return scope.Close(info); |
| 1612 | } |
Ryan Dahl | b3b3cfe | 2009-11-03 12:00:42 | [diff] [blame] | 1613 | |
Bert Belder | 4a2cb07 | 2010-11-29 17:40:14 | [diff] [blame] | 1614 | |
Ryan Dahl | 6eca948 | 2010-09-17 06:13:03 | [diff] [blame] | 1615 | Handle<Value> Kill(const Arguments& args) { |
Brandon Beacher | 334d56d | 2009-10-14 21:56:12 | [diff] [blame] | 1616 | HandleScope scope; |
Ryan Dahl | b20c343 | 2010-02-12 05:55:08 | [diff] [blame] | 1617 | |
Ryan Dahl | 4227e9d | 2010-12-21 23:40:10 | [diff] [blame] | 1618 | if (args.Length() != 2) { |
Brandon Beacher | 334d56d | 2009-10-14 21:56:12 | [diff] [blame] | 1619 | return ThrowException(Exception::Error(String::New("Bad argument."))); |
| 1620 | } |
Ryan Dahl | b20c343 | 2010-02-12 05:55:08 | [diff] [blame] | 1621 | |
Igor Zinkovsky | 24a69d2 | 2011-11-02 22:06:48 | [diff] [blame] | 1622 | int pid = args[0]->IntegerValue(); |
Ryan Dahl | 6eca948 | 2010-09-17 06:13:03 | [diff] [blame] | 1623 | int sig = args[1]->Int32Value(); |
Igor Zinkovsky | 24a69d2 | 2011-11-02 22:06:48 | [diff] [blame] | 1624 | uv_err_t err = uv_kill(pid, sig); |
Brandon Beacher | 334d56d | 2009-10-14 21:56:12 | [diff] [blame] | 1625 | |
Igor Zinkovsky | 24a69d2 | 2011-11-02 22:06:48 | [diff] [blame] | 1626 | if (err.code != UV_OK) { |
| 1627 | SetErrno(err); |
| 1628 | return scope.Close(Integer::New(-1)); |
| 1629 | } |
Brandon Beacher | 334d56d | 2009-10-14 21:56:12 | [diff] [blame] | 1630 | |
| 1631 | return Undefined(); |
| 1632 | } |
| 1633 | |
Bert Belder | dd93c53 | 2011-10-28 10:05:09 | [diff] [blame] | 1634 | |
| 1635 | typedef void (UV_DYNAMIC* extInit)(Handle<Object> exports); |
Ryan | 2b6d724 | 2009-06-20 13:07:10 | [diff] [blame] | 1636 | |
Ryan Dahl | 3881455 | 2009-10-09 15:15:47 | [diff] [blame] | 1637 | // DLOpen is node.dlopen(). Used to load 'module.node' dynamically shared |
| 1638 | // objects. |
Ryan | d6c9d31 | 2009-09-11 14:02:29 | [diff] [blame] | 1639 | Handle<Value> DLOpen(const v8::Arguments& args) { |
Ryan | 2b6d724 | 2009-06-20 13:07:10 | [diff] [blame] | 1640 | HandleScope scope; |
Bert Belder | dd93c53 | 2011-10-28 10:05:09 | [diff] [blame] | 1641 | char symbol[1024], *base, *pos; |
| 1642 | uv_lib_t lib; |
| 1643 | node_module_struct compat_mod; |
| 1644 | uv_err_t err; |
| 1645 | int r; |
Ryan | 2b6d724 | 2009-06-20 13:07:10 | [diff] [blame] | 1646 | |
Bert Belder | dd93c53 | 2011-10-28 10:05:09 | [diff] [blame] | 1647 | if (args.Length() < 2) { |
Bert Belder | 35f4182 | 2011-11-04 15:11:39 | [diff] [blame] | 1648 | Local<Value> exception = Exception::Error( |
| 1649 | String::New("process.dlopen takes exactly 2 arguments.")); |
| 1650 | return ThrowException(exception); |
Bert Belder | dd93c53 | 2011-10-28 10:05:09 | [diff] [blame] | 1651 | } |
Ryan | a97dce7 | 2009-08-31 09:14:34 | [diff] [blame] | 1652 | |
Ryan Dahl | 3881455 | 2009-10-09 15:15:47 | [diff] [blame] | 1653 | String::Utf8Value filename(args[0]->ToString()); // Cast |
| 1654 | Local<Object> target = args[1]->ToObject(); // Cast |
Ryan | 2b6d724 | 2009-06-20 13:07:10 | [diff] [blame] | 1655 | |
Bert Belder | dd93c53 | 2011-10-28 10:05:09 | [diff] [blame] | 1656 | err = uv_dlopen(*filename, &lib); |
| 1657 | if (err.code != UV_OK) { |
Bert Belder | 35f4182 | 2011-11-04 15:11:39 | [diff] [blame] | 1658 | Local<Value> exception = Exception::Error( |
| 1659 | String::Concat(String::New("Unable to load shared library "), |
| 1660 | args[0]->ToString())); |
| 1661 | return ThrowException(exception); |
Ryan | 2b6d724 | 2009-06-20 13:07:10 | [diff] [blame] | 1662 | } |
| 1663 | |
Bert Belder | dd93c53 | 2011-10-28 10:05:09 | [diff] [blame] | 1664 | String::Utf8Value path(args[0]->ToString()); |
| 1665 | base = *path; |
Paul Querna | 367b87d | 2010-07-13 08:33:51 | [diff] [blame] | 1666 | |
Bert Belder | dd93c53 | 2011-10-28 10:05:09 | [diff] [blame] | 1667 | /* Find the shared library filename within the full path. */ |
| 1668 | #ifdef __POSIX__ |
| 1669 | pos = strrchr(base, '/'); |
| 1670 | if (pos != NULL) { |
Ben Noordhuis | a1fa3ef | 2011-10-30 16:33:42 | [diff] [blame] | 1671 | base = pos + 1; |
Bert Belder | dd93c53 | 2011-10-28 10:05:09 | [diff] [blame] | 1672 | } |
| 1673 | #else // Windows |
| 1674 | for (;;) { |
| 1675 | pos = strpbrk(base, "\\/:"); |
| 1676 | if (pos == NULL) { |
| 1677 | break; |
Paul Querna | 367b87d | 2010-07-13 08:33:51 | [diff] [blame] | 1678 | } |
Bert Belder | dd93c53 | 2011-10-28 10:05:09 | [diff] [blame] | 1679 | base = pos + 1; |
| 1680 | } |
| 1681 | #endif |
Paul Querna | 367b87d | 2010-07-13 08:33:51 | [diff] [blame] | 1682 | |
Bert Belder | dd93c53 | 2011-10-28 10:05:09 | [diff] [blame] | 1683 | /* Strip the .node extension. */ |
| 1684 | pos = strrchr(base, '.'); |
| 1685 | if (pos != NULL) { |
| 1686 | *pos = '\0'; |
| 1687 | } |
| 1688 | |
| 1689 | /* Add the `_module` suffix to the extension name. */ |
| 1690 | r = snprintf(symbol, sizeof symbol, "%s_module", base); |
| 1691 | if (r <= 0 || r >= sizeof symbol) { |
Bert Belder | 35f4182 | 2011-11-04 15:11:39 | [diff] [blame] | 1692 | Local<Value> exception = |
| 1693 | Exception::Error(String::New("Out of memory.")); |
| 1694 | return ThrowException(exception); |
Paul Querna | 367b87d | 2010-07-13 08:33:51 | [diff] [blame] | 1695 | } |
| 1696 | |
Ryan Dahl | 3881455 | 2009-10-09 15:15:47 | [diff] [blame] | 1697 | // Get the init() function from the dynamically shared object. |
Bert Belder | dd93c53 | 2011-10-28 10:05:09 | [diff] [blame] | 1698 | node_module_struct *mod; |
| 1699 | err = uv_dlsym(lib, symbol, reinterpret_cast<void**>(&mod)); |
Ben Noordhuis | eaac881 | 2011-08-13 23:25:39 | [diff] [blame] | 1700 | |
Bert Belder | dd93c53 | 2011-10-28 10:05:09 | [diff] [blame] | 1701 | if (err.code != UV_OK) { |
Paul Querna | 367b87d | 2010-07-13 08:33:51 | [diff] [blame] | 1702 | /* Start Compatibility hack: Remove once everyone is using NODE_MODULE macro */ |
Ben Noordhuis | eaac881 | 2011-08-13 23:25:39 | [diff] [blame] | 1703 | memset(&compat_mod, 0, sizeof compat_mod); |
| 1704 | |
Paul Querna | 367b87d | 2010-07-13 08:33:51 | [diff] [blame] | 1705 | mod = &compat_mod; |
| 1706 | mod->version = NODE_MODULE_VERSION; |
| 1707 | |
Bert Belder | dd93c53 | 2011-10-28 10:05:09 | [diff] [blame] | 1708 | err = uv_dlsym(lib, "init", reinterpret_cast<void**>(&mod->register_func)); |
| 1709 | if (err.code != UV_OK) { |
| 1710 | uv_dlclose(lib); |
Ben Noordhuis | 216019b | 2011-11-27 21:38:54 | [diff] [blame] | 1711 | |
| 1712 | const char* message; |
| 1713 | if (err.code == UV_ENOENT) |
| 1714 | message = "Module entry point not found."; |
| 1715 | else |
Ben Noordhuis | 4b455ba | 2011-12-18 21:27:21 | [diff] [blame] | 1716 | message = uv_strerror(err); |
Ben Noordhuis | 216019b | 2011-11-27 21:38:54 | [diff] [blame] | 1717 | |
| 1718 | return ThrowException(Exception::Error(String::New(message))); |
Paul Querna | 367b87d | 2010-07-13 08:33:51 | [diff] [blame] | 1719 | } |
Paul Querna | 367b87d | 2010-07-13 08:33:51 | [diff] [blame] | 1720 | /* End Compatibility hack */ |
| 1721 | } |
| 1722 | |
| 1723 | if (mod->version != NODE_MODULE_VERSION) { |
Bert Belder | 35f4182 | 2011-11-04 15:11:39 | [diff] [blame] | 1724 | Local<Value> exception = Exception::Error( |
| 1725 | String::New("Module version mismatch, refusing to load.")); |
Ryan | 3862fda | 2009-08-31 16:48:47 | [diff] [blame] | 1726 | return ThrowException(exception); |
Ryan | 2b6d724 | 2009-06-20 13:07:10 | [diff] [blame] | 1727 | } |
Ryan | 2b6d724 | 2009-06-20 13:07:10 | [diff] [blame] | 1728 | |
Ryan Dahl | 3881455 | 2009-10-09 15:15:47 | [diff] [blame] | 1729 | // Execute the C++ module |
Paul Querna | 367b87d | 2010-07-13 08:33:51 | [diff] [blame] | 1730 | mod->register_func(target); |
Ryan | 2b6d724 | 2009-06-20 13:07:10 | [diff] [blame] | 1731 | |
Peter Griess | 4e3c5d8 | 2010-07-12 15:47:45 | [diff] [blame] | 1732 | // Tell coverity that 'handle' should not be freed when we return. |
| 1733 | // coverity[leaked_storage] |
Ryan | 2b6d724 | 2009-06-20 13:07:10 | [diff] [blame] | 1734 | return Undefined(); |
| 1735 | } |
| 1736 | |
Tim-Smart | ae10a48 | 2010-03-12 08:36:00 | [diff] [blame] | 1737 | |
Ryan | d6c9d31 | 2009-09-11 14:02:29 | [diff] [blame] | 1738 | static void OnFatalError(const char* location, const char* message) { |
Ryan Dahl | 53a841d | 2009-12-29 19:20:51 | [diff] [blame] | 1739 | if (location) { |
| 1740 | fprintf(stderr, "FATAL ERROR: %s %s\n", location, message); |
| 1741 | } else { |
| 1742 | fprintf(stderr, "FATAL ERROR: %s\n", message); |
| 1743 | } |
Ryan | 34a6f10 | 2009-05-28 12:47:16 | [diff] [blame] | 1744 | exit(1); |
Ryan | 63a9cd3 | 2009-04-15 08:08:28 | [diff] [blame] | 1745 | } |
| 1746 | |
Ben Noordhuis | 74a8215 | 2012-02-03 15:32:00 | [diff] [blame^] | 1747 | static int uncaught_exception_counter = 0; |
Felix Geisendörfer | 2b252ac | 2009-11-14 22:07:54 | [diff] [blame] | 1748 | |
Ryan | d6c9d31 | 2009-09-11 14:02:29 | [diff] [blame] | 1749 | void FatalException(TryCatch &try_catch) { |
Felix Geisendörfer | 2b252ac | 2009-11-14 22:07:54 | [diff] [blame] | 1750 | HandleScope scope; |
| 1751 | |
| 1752 | // Check if uncaught_exception_counter indicates a recursion |
| 1753 | if (uncaught_exception_counter > 0) { |
Ryan Dahl | ab068db | 2010-05-09 20:54:58 | [diff] [blame] | 1754 | ReportException(try_catch, true); |
Felix Geisendörfer | 2b252ac | 2009-11-14 22:07:54 | [diff] [blame] | 1755 | exit(1); |
| 1756 | } |
| 1757 | |
Ryan Dahl | 45a806a | 2009-12-09 08:02:21 | [diff] [blame] | 1758 | if (listeners_symbol.IsEmpty()) { |
| 1759 | listeners_symbol = NODE_PSYMBOL("listeners"); |
| 1760 | uncaught_exception_symbol = NODE_PSYMBOL("uncaughtException"); |
| 1761 | emit_symbol = NODE_PSYMBOL("emit"); |
| 1762 | } |
| 1763 | |
| 1764 | Local<Value> listeners_v = process->Get(listeners_symbol); |
Felix Geisendörfer | 2b252ac | 2009-11-14 22:07:54 | [diff] [blame] | 1765 | assert(listeners_v->IsFunction()); |
| 1766 | |
| 1767 | Local<Function> listeners = Local<Function>::Cast(listeners_v); |
| 1768 | |
Ryan Dahl | 45a806a | 2009-12-09 08:02:21 | [diff] [blame] | 1769 | Local<String> uncaught_exception_symbol_l = Local<String>::New(uncaught_exception_symbol); |
| 1770 | Local<Value> argv[1] = { uncaught_exception_symbol_l }; |
Felix Geisendörfer | 2b252ac | 2009-11-14 22:07:54 | [diff] [blame] | 1771 | Local<Value> ret = listeners->Call(process, 1, argv); |
| 1772 | |
| 1773 | assert(ret->IsArray()); |
| 1774 | |
| 1775 | Local<Array> listener_array = Local<Array>::Cast(ret); |
| 1776 | |
| 1777 | uint32_t length = listener_array->Length(); |
| 1778 | // Report and exit if process has no "uncaughtException" listener |
| 1779 | if (length == 0) { |
Ryan Dahl | ab068db | 2010-05-09 20:54:58 | [diff] [blame] | 1780 | ReportException(try_catch, true); |
Felix Geisendörfer | 2b252ac | 2009-11-14 22:07:54 | [diff] [blame] | 1781 | exit(1); |
| 1782 | } |
| 1783 | |
| 1784 | // Otherwise fire the process "uncaughtException" event |
Ryan Dahl | 45a806a | 2009-12-09 08:02:21 | [diff] [blame] | 1785 | Local<Value> emit_v = process->Get(emit_symbol); |
Felix Geisendörfer | 2b252ac | 2009-11-14 22:07:54 | [diff] [blame] | 1786 | assert(emit_v->IsFunction()); |
| 1787 | |
| 1788 | Local<Function> emit = Local<Function>::Cast(emit_v); |
| 1789 | |
| 1790 | Local<Value> error = try_catch.Exception(); |
Ryan Dahl | 45a806a | 2009-12-09 08:02:21 | [diff] [blame] | 1791 | Local<Value> event_argv[2] = { uncaught_exception_symbol_l, error }; |
Felix Geisendörfer | 2b252ac | 2009-11-14 22:07:54 | [diff] [blame] | 1792 | |
| 1793 | uncaught_exception_counter++; |
| 1794 | emit->Call(process, 2, event_argv); |
| 1795 | // Decrement so we know if the next exception is a recursion or not |
| 1796 | uncaught_exception_counter--; |
Ryan | e78917b | 2009-03-09 13:08:31 | [diff] [blame] | 1797 | } |
| 1798 | |
Ryan | 0e9e927 | 2009-04-04 14:53:43 | [diff] [blame] | 1799 | |
Fedor Indutny | b0388cc | 2011-12-10 16:52:07 | [diff] [blame] | 1800 | static void DebugBreakMessageHandler(const v8::Debug::Message& message) { |
Danny Coates | dc8c079 | 2010-08-01 22:46:48 | [diff] [blame] | 1801 | // do nothing with debug messages. |
| 1802 | // The message handler will get changed by DebuggerAgent::CreateSession in |
| 1803 | // debug-agent.cc of v8/src when a new session is created |
Zoran Tomicic | d98ea70 | 2010-02-22 05:15:44 | [diff] [blame] | 1804 | } |
| 1805 | |
Ryan Dahl | 4ab5476 | 2011-03-01 17:59:17 | [diff] [blame] | 1806 | |
Ben Noordhuis | 74a8215 | 2012-02-03 15:32:00 | [diff] [blame^] | 1807 | Persistent<Object> binding_cache; |
| 1808 | Persistent<Array> module_load_list; |
| 1809 | |
Ryan Dahl | 627fb5a | 2010-03-15 20:48:03 | [diff] [blame] | 1810 | static Handle<Value> Binding(const Arguments& args) { |
| 1811 | HandleScope scope; |
| 1812 | |
| 1813 | Local<String> module = args[0]->ToString(); |
| 1814 | String::Utf8Value module_v(module); |
Paul Querna | 30dadfc | 2010-07-14 06:22:41 | [diff] [blame] | 1815 | node_module_struct* modp; |
Ryan Dahl | 627fb5a | 2010-03-15 20:48:03 | [diff] [blame] | 1816 | |
| 1817 | if (binding_cache.IsEmpty()) { |
| 1818 | binding_cache = Persistent<Object>::New(Object::New()); |
| 1819 | } |
| 1820 | |
| 1821 | Local<Object> exports; |
| 1822 | |
Paul Querna | 30dadfc | 2010-07-14 06:22:41 | [diff] [blame] | 1823 | if (binding_cache->Has(module)) { |
| 1824 | exports = binding_cache->Get(module)->ToObject(); |
Ryan Dahl | ea9ee1f | 2011-07-28 02:30:32 | [diff] [blame] | 1825 | return scope.Close(exports); |
| 1826 | } |
Ryan Dahl | 6eca948 | 2010-09-17 06:13:03 | [diff] [blame] | 1827 | |
Ryan Dahl | ea9ee1f | 2011-07-28 02:30:32 | [diff] [blame] | 1828 | // Append a string to process.moduleLoadList |
| 1829 | char buf[1024]; |
| 1830 | snprintf(buf, 1024, "Binding %s", *module_v); |
| 1831 | uint32_t l = module_load_list->Length(); |
| 1832 | module_load_list->Set(l, String::New(buf)); |
| 1833 | |
| 1834 | if ((modp = get_builtin_module(*module_v)) != NULL) { |
Paul Querna | 30dadfc | 2010-07-14 06:22:41 | [diff] [blame] | 1835 | exports = Object::New(); |
| 1836 | modp->register_func(exports); |
| 1837 | binding_cache->Set(module, exports); |
Ryan Dahl | 6eca948 | 2010-09-17 06:13:03 | [diff] [blame] | 1838 | |
| 1839 | } else if (!strcmp(*module_v, "constants")) { |
| 1840 | exports = Object::New(); |
| 1841 | DefineConstants(exports); |
| 1842 | binding_cache->Set(module, exports); |
| 1843 | |
Igor Zinkovsky | f35a396 | 2011-10-28 22:34:24 | [diff] [blame] | 1844 | #ifdef __POSIX__ |
| 1845 | } else if (!strcmp(*module_v, "io_watcher")) { |
| 1846 | exports = Object::New(); |
| 1847 | IOWatcher::Initialize(exports); |
| 1848 | binding_cache->Set(module, exports); |
| 1849 | #endif |
| 1850 | |
Ryan Dahl | c90546f | 2010-03-15 21:22:50 | [diff] [blame] | 1851 | } else if (!strcmp(*module_v, "natives")) { |
Paul Querna | 8dbfe5e | 2010-07-14 06:32:38 | [diff] [blame] | 1852 | exports = Object::New(); |
Ryan Dahl | c4636a5 | 2010-10-12 18:49:41 | [diff] [blame] | 1853 | DefineJavaScript(exports); |
Paul Querna | 8dbfe5e | 2010-07-14 06:32:38 | [diff] [blame] | 1854 | binding_cache->Set(module, exports); |
Ryan Dahl | c4636a5 | 2010-10-12 18:49:41 | [diff] [blame] | 1855 | |
Ryan Dahl | 627fb5a | 2010-03-15 20:48:03 | [diff] [blame] | 1856 | } else { |
Ryan Dahl | 6eca948 | 2010-09-17 06:13:03 | [diff] [blame] | 1857 | |
Ryan Dahl | 627fb5a | 2010-03-15 20:48:03 | [diff] [blame] | 1858 | return ThrowException(Exception::Error(String::New("No such module"))); |
| 1859 | } |
| 1860 | |
| 1861 | return scope.Close(exports); |
| 1862 | } |
| 1863 | |
Ryan Dahl | e742d07 | 2009-10-09 11:25:04 | [diff] [blame] | 1864 | |
Ryan Dahl | 5185c15 | 2010-06-18 07:26:49 | [diff] [blame] | 1865 | static Handle<Value> ProcessTitleGetter(Local<String> property, |
| 1866 | const AccessorInfo& info) { |
| 1867 | HandleScope scope; |
Igor Zinkovsky | 500c8f4 | 2011-12-15 20:36:05 | [diff] [blame] | 1868 | char buffer[512]; |
| 1869 | uv_get_process_title(buffer, sizeof(buffer)); |
| 1870 | return scope.Close(String::New(buffer)); |
Ryan Dahl | 5185c15 | 2010-06-18 07:26:49 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | |
| 1874 | static void ProcessTitleSetter(Local<String> property, |
| 1875 | Local<Value> value, |
| 1876 | const AccessorInfo& info) { |
| 1877 | HandleScope scope; |
| 1878 | String::Utf8Value title(value->ToString()); |
Igor Zinkovsky | 500c8f4 | 2011-12-15 20:36:05 | [diff] [blame] | 1879 | // TODO: protect with a lock |
| 1880 | uv_set_process_title(*title); |
Ryan Dahl | 5185c15 | 2010-06-18 07:26:49 | [diff] [blame] | 1881 | } |
| 1882 | |
| 1883 | |
Ben Noordhuis | b4def48 | 2010-10-15 13:48:34 | [diff] [blame] | 1884 | static Handle<Value> EnvGetter(Local<String> property, |
| 1885 | const AccessorInfo& info) { |
| 1886 | String::Utf8Value key(property); |
| 1887 | const char* val = getenv(*key); |
| 1888 | if (val) { |
| 1889 | HandleScope scope; |
| 1890 | return scope.Close(String::New(val)); |
| 1891 | } |
| 1892 | return Undefined(); |
| 1893 | } |
| 1894 | |
| 1895 | |
| 1896 | static Handle<Value> EnvSetter(Local<String> property, |
| 1897 | Local<Value> value, |
| 1898 | const AccessorInfo& info) { |
Ryan Dahl | e6b06bc | 2011-08-11 00:14:23 | [diff] [blame] | 1899 | HandleScope scope; |
Ben Noordhuis | b4def48 | 2010-10-15 13:48:34 | [diff] [blame] | 1900 | String::Utf8Value key(property); |
| 1901 | String::Utf8Value val(value); |
Ryan Dahl | e6b06bc | 2011-08-11 00:14:23 | [diff] [blame] | 1902 | |
Bert Belder | 30bab52 | 2010-11-25 00:09:06 | [diff] [blame] | 1903 | #ifdef __POSIX__ |
Ben Noordhuis | b4def48 | 2010-10-15 13:48:34 | [diff] [blame] | 1904 | setenv(*key, *val, 1); |
Bert Belder | 30bab52 | 2010-11-25 00:09:06 | [diff] [blame] | 1905 | #else // __WIN32__ |
Ryan Dahl | e6b06bc | 2011-08-11 00:14:23 | [diff] [blame] | 1906 | int n = key.length() + val.length() + 2; |
| 1907 | char* pair = new char[n]; |
| 1908 | snprintf(pair, n, "%s=%s", *key, *val); |
| 1909 | int r = _putenv(pair); |
| 1910 | if (r) { |
| 1911 | fprintf(stderr, "error putenv: '%s'\n", pair); |
| 1912 | } |
| 1913 | delete [] pair; |
Bert Belder | 30bab52 | 2010-11-25 00:09:06 | [diff] [blame] | 1914 | #endif |
Ryan Dahl | e6b06bc | 2011-08-11 00:14:23 | [diff] [blame] | 1915 | |
Ben Noordhuis | b4def48 | 2010-10-15 13:48:34 | [diff] [blame] | 1916 | return value; |
| 1917 | } |
| 1918 | |
| 1919 | |
| 1920 | static Handle<Integer> EnvQuery(Local<String> property, |
| 1921 | const AccessorInfo& info) { |
| 1922 | String::Utf8Value key(property); |
| 1923 | if (getenv(*key)) { |
| 1924 | HandleScope scope; |
| 1925 | return scope.Close(Integer::New(None)); |
| 1926 | } |
| 1927 | return Handle<Integer>(); |
| 1928 | } |
| 1929 | |
| 1930 | |
| 1931 | static Handle<Boolean> EnvDeleter(Local<String> property, |
| 1932 | const AccessorInfo& info) { |
Ryan Dahl | e6b06bc | 2011-08-11 00:14:23 | [diff] [blame] | 1933 | HandleScope scope; |
| 1934 | |
Ben Noordhuis | b4def48 | 2010-10-15 13:48:34 | [diff] [blame] | 1935 | String::Utf8Value key(property); |
Ryan Dahl | e6b06bc | 2011-08-11 00:14:23 | [diff] [blame] | 1936 | |
Ben Noordhuis | b4def48 | 2010-10-15 13:48:34 | [diff] [blame] | 1937 | if (getenv(*key)) { |
Bert Belder | 30bab52 | 2010-11-25 00:09:06 | [diff] [blame] | 1938 | #ifdef __POSIX__ |
Ben Noordhuis | b4def48 | 2010-10-15 13:48:34 | [diff] [blame] | 1939 | unsetenv(*key); // prototyped as `void unsetenv(const char*)` on some platforms |
Bert Belder | 30bab52 | 2010-11-25 00:09:06 | [diff] [blame] | 1940 | #else |
Ryan Dahl | e6b06bc | 2011-08-11 00:14:23 | [diff] [blame] | 1941 | int n = key.length() + 2; |
| 1942 | char* pair = new char[n]; |
| 1943 | snprintf(pair, n, "%s=", *key); |
| 1944 | int r = _putenv(pair); |
| 1945 | if (r) { |
| 1946 | fprintf(stderr, "error unsetenv: '%s'\n", pair); |
| 1947 | } |
| 1948 | delete [] pair; |
Bert Belder | 30bab52 | 2010-11-25 00:09:06 | [diff] [blame] | 1949 | #endif |
Ben Noordhuis | b4def48 | 2010-10-15 13:48:34 | [diff] [blame] | 1950 | return True(); |
| 1951 | } |
Ryan Dahl | e6b06bc | 2011-08-11 00:14:23 | [diff] [blame] | 1952 | |
Ben Noordhuis | b4def48 | 2010-10-15 13:48:34 | [diff] [blame] | 1953 | return False(); |
| 1954 | } |
| 1955 | |
| 1956 | |
| 1957 | static Handle<Array> EnvEnumerator(const AccessorInfo& info) { |
| 1958 | HandleScope scope; |
| 1959 | |
| 1960 | int size = 0; |
| 1961 | while (environ[size]) size++; |
| 1962 | |
| 1963 | Local<Array> env = Array::New(size); |
| 1964 | |
| 1965 | for (int i = 0; i < size; ++i) { |
| 1966 | const char* var = environ[i]; |
| 1967 | const char* s = strchr(var, '='); |
| 1968 | const int length = s ? s - var : strlen(var); |
| 1969 | env->Set(i, String::New(var, length)); |
| 1970 | } |
| 1971 | |
| 1972 | return scope.Close(env); |
| 1973 | } |
| 1974 | |
| 1975 | |
Ben Noordhuis | aa0308d | 2011-07-23 21:16:48 | [diff] [blame] | 1976 | static Handle<Object> GetFeatures() { |
| 1977 | HandleScope scope; |
| 1978 | |
| 1979 | Local<Object> obj = Object::New(); |
Ryan Dahl | 52a40e0 | 2011-08-24 21:16:35 | [diff] [blame] | 1980 | obj->Set(String::NewSymbol("debug"), |
| 1981 | #if defined(DEBUG) && DEBUG |
| 1982 | True() |
| 1983 | #else |
| 1984 | False() |
| 1985 | #endif |
| 1986 | ); |
| 1987 | |
Ryan Dahl | c8dbaf5 | 2011-10-11 21:38:38 | [diff] [blame] | 1988 | obj->Set(String::NewSymbol("uv"), True()); |
Ben Noordhuis | aa0308d | 2011-07-23 21:16:48 | [diff] [blame] | 1989 | obj->Set(String::NewSymbol("ipv6"), True()); // TODO ping libuv |
Fedor Indutny | 9010f5f | 2011-07-28 15:52:04 | [diff] [blame] | 1990 | obj->Set(String::NewSymbol("tls_npn"), Boolean::New(use_npn)); |
| 1991 | obj->Set(String::NewSymbol("tls_sni"), Boolean::New(use_sni)); |
Ben Noordhuis | aa0308d | 2011-07-23 21:16:48 | [diff] [blame] | 1992 | obj->Set(String::NewSymbol("tls"), |
| 1993 | Boolean::New(get_builtin_module("crypto") != NULL)); |
| 1994 | |
| 1995 | return scope.Close(obj); |
| 1996 | } |
| 1997 | |
| 1998 | |
Bert Belder | 829735e | 2011-11-04 15:23:02 | [diff] [blame] | 1999 | static Handle<Value> DebugProcess(const Arguments& args); |
Fedor Indutny | b0388cc | 2011-12-10 16:52:07 | [diff] [blame] | 2000 | static Handle<Value> DebugPause(const Arguments& args); |
Bert Belder | 829735e | 2011-11-04 15:23:02 | [diff] [blame] | 2001 | |
Dean McNamee | f67e8f2 | 2011-03-15 22:39:16 | [diff] [blame] | 2002 | Handle<Object> SetupProcessObject(int argc, char *argv[]) { |
Ryan | 27b268b | 2009-06-17 13:05:44 | [diff] [blame] | 2003 | HandleScope scope; |
| 2004 | |
Ryan Dahl | cd1ec27 | 2011-01-02 09:44:42 | [diff] [blame] | 2005 | int i, j; |
| 2006 | |
Ryan Dahl | ad0a4ce | 2009-10-29 22:34:10 | [diff] [blame] | 2007 | Local<FunctionTemplate> process_template = FunctionTemplate::New(); |
Ryan | 27b268b | 2009-06-17 13:05:44 | [diff] [blame] | 2008 | |
Ryan Dahl | ad0a4ce | 2009-10-29 22:34:10 | [diff] [blame] | 2009 | process = Persistent<Object>::New(process_template->GetFunction()->NewInstance()); |
| 2010 | |
Ben Noordhuis | 74a8215 | 2012-02-03 15:32:00 | [diff] [blame^] | 2011 | |
Ryan Dahl | 5185c15 | 2010-06-18 07:26:49 | [diff] [blame] | 2012 | process->SetAccessor(String::New("title"), |
| 2013 | ProcessTitleGetter, |
| 2014 | ProcessTitleSetter); |
| 2015 | |
Ryan Dahl | f481183 | 2009-11-02 23:21:00 | [diff] [blame] | 2016 | // process.version |
Ryan Dahl | ad0a4ce | 2009-10-29 22:34:10 | [diff] [blame] | 2017 | process->Set(String::NewSymbol("version"), String::New(NODE_VERSION)); |
Ryan Dahl | 39b432e | 2010-08-17 18:24:10 | [diff] [blame] | 2018 | |
Ryan Dahl | a979ab9 | 2011-08-04 23:40:07 | [diff] [blame] | 2019 | #ifdef NODE_PREFIX |
Ryan Dahl | f481183 | 2009-11-02 23:21:00 | [diff] [blame] | 2020 | // process.installPrefix |
Ryan Dahl | ad0a4ce | 2009-10-29 22:34:10 | [diff] [blame] | 2021 | process->Set(String::NewSymbol("installPrefix"), String::New(NODE_PREFIX)); |
Ryan Dahl | a979ab9 | 2011-08-04 23:40:07 | [diff] [blame] | 2022 | #endif |
Ryan | 1bf9be6 | 2009-08-03 15:51:35 | [diff] [blame] | 2023 | |
Ryan Dahl | ea9ee1f | 2011-07-28 02:30:32 | [diff] [blame] | 2024 | // process.moduleLoadList |
| 2025 | module_load_list = Persistent<Array>::New(Array::New()); |
| 2026 | process->Set(String::NewSymbol("moduleLoadList"), module_load_list); |
| 2027 | |
Ryan Dahl | 39b432e | 2010-08-17 18:24:10 | [diff] [blame] | 2028 | Local<Object> versions = Object::New(); |
| 2029 | char buf[20]; |
| 2030 | process->Set(String::NewSymbol("versions"), versions); |
| 2031 | // +1 to get rid of the leading 'v' |
| 2032 | versions->Set(String::NewSymbol("node"), String::New(NODE_VERSION+1)); |
| 2033 | versions->Set(String::NewSymbol("v8"), String::New(V8::GetVersion())); |
| 2034 | versions->Set(String::NewSymbol("ares"), String::New(ARES_VERSION_STR)); |
Bert Belder | 9cec08e | 2011-05-23 23:42:22 | [diff] [blame] | 2035 | snprintf(buf, 20, "%d.%d", UV_VERSION_MAJOR, UV_VERSION_MINOR); |
| 2036 | versions->Set(String::NewSymbol("uv"), String::New(buf)); |
Peter Bright | 13d6a1f | 2011-08-06 04:23:25 | [diff] [blame] | 2037 | #if HAVE_OPENSSL |
Ryan Dahl | cd1ec27 | 2011-01-02 09:44:42 | [diff] [blame] | 2038 | // Stupid code to slice out the version string. |
| 2039 | int c, l = strlen(OPENSSL_VERSION_TEXT); |
Ben Noordhuis | e0297ca | 2011-10-14 15:05:02 | [diff] [blame] | 2040 | for (i = j = 0; i < l; i++) { |
Ryan Dahl | cd1ec27 | 2011-01-02 09:44:42 | [diff] [blame] | 2041 | c = OPENSSL_VERSION_TEXT[i]; |
| 2042 | if ('0' <= c && c <= '9') { |
| 2043 | for (j = i + 1; j < l; j++) { |
| 2044 | c = OPENSSL_VERSION_TEXT[j]; |
| 2045 | if (c == ' ') break; |
| 2046 | } |
| 2047 | break; |
| 2048 | } |
| 2049 | } |
| 2050 | versions->Set(String::NewSymbol("openssl"), |
| 2051 | String::New(OPENSSL_VERSION_TEXT + i, j - i)); |
| 2052 | #endif |
Ryan Dahl | 39b432e | 2010-08-17 18:24:10 | [diff] [blame] | 2053 | |
| 2054 | |
| 2055 | |
Nathan Rajlich | b1be540 | 2011-04-26 03:24:51 | [diff] [blame] | 2056 | // process.arch |
| 2057 | process->Set(String::NewSymbol("arch"), String::New(ARCH)); |
| 2058 | |
Ryan Dahl | f481183 | 2009-11-02 23:21:00 | [diff] [blame] | 2059 | // process.platform |
Ryan Dahl | 3770462 | 2011-01-06 03:05:59 | [diff] [blame] | 2060 | process->Set(String::NewSymbol("platform"), String::New(PLATFORM)); |
Ryan Dahl | f481183 | 2009-11-02 23:21:00 | [diff] [blame] | 2061 | |
Ryan Dahl | f3ad635 | 2010-02-03 20:19:08 | [diff] [blame] | 2062 | // process.argv |
Jeremy Ashkenas | 2916a2a | 2010-02-21 06:41:27 | [diff] [blame] | 2063 | Local<Array> arguments = Array::New(argc - option_end_index + 1); |
Ryan | 1910c11 | 2009-09-11 18:05:22 | [diff] [blame] | 2064 | arguments->Set(Integer::New(0), String::New(argv[0])); |
Peter Griess | 78d33f4 | 2010-06-04 15:29:10 | [diff] [blame] | 2065 | for (j = 1, i = option_end_index; i < argc; j++, i++) { |
Ryan | 27b268b | 2009-06-17 13:05:44 | [diff] [blame] | 2066 | Local<String> arg = String::New(argv[i]); |
Ryan | 1910c11 | 2009-09-11 18:05:22 | [diff] [blame] | 2067 | arguments->Set(Integer::New(j), arg); |
Ryan | 27b268b | 2009-06-17 13:05:44 | [diff] [blame] | 2068 | } |
Ryan Dahl | 3881455 | 2009-10-09 15:15:47 | [diff] [blame] | 2069 | // assign it |
Ryan Dahl | f3ad635 | 2010-02-03 20:19:08 | [diff] [blame] | 2070 | process->Set(String::NewSymbol("argv"), arguments); |
Ryan | 27b268b | 2009-06-17 13:05:44 | [diff] [blame] | 2071 | |
Ryan Dahl | f3ad635 | 2010-02-03 20:19:08 | [diff] [blame] | 2072 | // create process.env |
Ben Noordhuis | b4def48 | 2010-10-15 13:48:34 | [diff] [blame] | 2073 | Local<ObjectTemplate> envTemplate = ObjectTemplate::New(); |
Ryan Dahl | ea9006a | 2010-12-02 23:59:35 | [diff] [blame] | 2074 | envTemplate->SetNamedPropertyHandler(EnvGetter, |
| 2075 | EnvSetter, |
| 2076 | EnvQuery, |
| 2077 | EnvDeleter, |
| 2078 | EnvEnumerator, |
| 2079 | Undefined()); |
Ben Noordhuis | b4def48 | 2010-10-15 13:48:34 | [diff] [blame] | 2080 | Local<Object> env = envTemplate->NewInstance(); |
Ryan Dahl | f3ad635 | 2010-02-03 20:19:08 | [diff] [blame] | 2081 | process->Set(String::NewSymbol("env"), env); |
| 2082 | |
Brandon Beacher | 334d56d | 2009-10-14 21:56:12 | [diff] [blame] | 2083 | process->Set(String::NewSymbol("pid"), Integer::New(getpid())); |
Ben Noordhuis | aa0308d | 2011-07-23 21:16:48 | [diff] [blame] | 2084 | process->Set(String::NewSymbol("features"), GetFeatures()); |
Ryan | dc39e82 | 2009-09-10 12:07:35 | [diff] [blame] | 2085 | |
TJ Holowaychuk | 9481bc1 | 2010-10-07 02:05:01 | [diff] [blame] | 2086 | // -e, --eval |
| 2087 | if (eval_string) { |
| 2088 | process->Set(String::NewSymbol("_eval"), String::New(eval_string)); |
Ben Noordhuis | 3d22dbf | 2011-12-01 16:21:00 | [diff] [blame] | 2089 | process->Set(String::NewSymbol("_print_eval"), Boolean::New(print_eval)); |
TJ Holowaychuk | 9481bc1 | 2010-10-07 02:05:01 | [diff] [blame] | 2090 | } |
| 2091 | |
Marshall Culpepper | ca35ba6 | 2010-06-22 06:31:19 | [diff] [blame] | 2092 | size_t size = 2*PATH_MAX; |
Peter Bright | 13d6a1f | 2011-08-06 04:23:25 | [diff] [blame] | 2093 | char* execPath = new char[size]; |
Ryan Dahl | 48f65b3 | 2011-07-15 17:46:11 | [diff] [blame] | 2094 | if (uv_exepath(execPath, &size) != 0) { |
Marshall Culpepper | ca35ba6 | 2010-06-22 06:31:19 | [diff] [blame] | 2095 | // as a last ditch effort, fallback on argv[0] ? |
| 2096 | process->Set(String::NewSymbol("execPath"), String::New(argv[0])); |
| 2097 | } else { |
Ryan Dahl | 00a1d61 | 2010-11-16 03:06:18 | [diff] [blame] | 2098 | process->Set(String::NewSymbol("execPath"), String::New(execPath, size)); |
Marshall Culpepper | ca35ba6 | 2010-06-22 06:31:19 | [diff] [blame] | 2099 | } |
Peter Bright | 13d6a1f | 2011-08-06 04:23:25 | [diff] [blame] | 2100 | delete [] execPath; |
Marshall Culpepper | ca35ba6 | 2010-06-22 06:31:19 | [diff] [blame] | 2101 | |
| 2102 | |
Ryan Dahl | 3881455 | 2009-10-09 15:15:47 | [diff] [blame] | 2103 | // define various internal methods |
Ryan Dahl | 4e7e2f8 | 2010-04-13 22:39:15 | [diff] [blame] | 2104 | NODE_SET_METHOD(process, "_needTickCallback", NeedTickCallback); |
Ryan Dahl | ad0a4ce | 2009-10-29 22:34:10 | [diff] [blame] | 2105 | NODE_SET_METHOD(process, "reallyExit", Exit); |
Robert Mustacchi | 2240486 | 2011-12-15 01:02:15 | [diff] [blame] | 2106 | NODE_SET_METHOD(process, "abort", Abort); |
Brandon Beacher | 47fcf78 | 2009-11-03 18:13:38 | [diff] [blame] | 2107 | NODE_SET_METHOD(process, "chdir", Chdir); |
Ryan Dahl | ad0a4ce | 2009-10-29 22:34:10 | [diff] [blame] | 2108 | NODE_SET_METHOD(process, "cwd", Cwd); |
Bert Belder | 30bab52 | 2010-11-25 00:09:06 | [diff] [blame] | 2109 | |
Bert Belder | 86fba38 | 2011-11-24 01:38:34 | [diff] [blame] | 2110 | #ifdef _WIN32 |
| 2111 | NODE_SET_METHOD(process, "_cwdForDrive", CwdForDrive); |
| 2112 | #endif |
| 2113 | |
Ryan Dahl | acc120a | 2011-08-09 20:53:56 | [diff] [blame] | 2114 | NODE_SET_METHOD(process, "umask", Umask); |
| 2115 | |
Bert Belder | 30bab52 | 2010-11-25 00:09:06 | [diff] [blame] | 2116 | #ifdef __POSIX__ |
Michael Carter | a386076 | 2010-02-08 06:13:10 | [diff] [blame] | 2117 | NODE_SET_METHOD(process, "getuid", GetUid); |
| 2118 | NODE_SET_METHOD(process, "setuid", SetUid); |
James Duncan | df1c1e5 | 2010-02-23 22:45:02 | [diff] [blame] | 2119 | |
| 2120 | NODE_SET_METHOD(process, "setgid", SetGid); |
| 2121 | NODE_SET_METHOD(process, "getgid", GetGid); |
Igor Zinkovsky | 24a69d2 | 2011-11-02 22:06:48 | [diff] [blame] | 2122 | #endif // __POSIX__ |
James Duncan | df1c1e5 | 2010-02-23 22:45:02 | [diff] [blame] | 2123 | |
Ryan Dahl | 6eca948 | 2010-09-17 06:13:03 | [diff] [blame] | 2124 | NODE_SET_METHOD(process, "_kill", Kill); |
Bert Belder | 30bab52 | 2010-11-25 00:09:06 | [diff] [blame] | 2125 | |
Bert Belder | 829735e | 2011-11-04 15:23:02 | [diff] [blame] | 2126 | NODE_SET_METHOD(process, "_debugProcess", DebugProcess); |
Fedor Indutny | b0388cc | 2011-12-10 16:52:07 | [diff] [blame] | 2127 | NODE_SET_METHOD(process, "_debugPause", DebugPause); |
Bert Belder | 829735e | 2011-11-04 15:23:02 | [diff] [blame] | 2128 | |
Bert Belder | dd93c53 | 2011-10-28 10:05:09 | [diff] [blame] | 2129 | NODE_SET_METHOD(process, "dlopen", DLOpen); |
| 2130 | |
Tom Hughes | cf78ce5 | 2011-03-04 23:57:54 | [diff] [blame] | 2131 | NODE_SET_METHOD(process, "uptime", Uptime); |
Ryan Dahl | b3b3cfe | 2009-11-03 12:00:42 | [diff] [blame] | 2132 | NODE_SET_METHOD(process, "memoryUsage", MemoryUsage); |
Ryan Dahl | c344fbc | 2011-10-06 21:59:38 | [diff] [blame] | 2133 | NODE_SET_METHOD(process, "uvCounters", UVCounters); |
Ryan | 27b268b | 2009-06-17 13:05:44 | [diff] [blame] | 2134 | |
Ryan Dahl | 627fb5a | 2010-03-15 20:48:03 | [diff] [blame] | 2135 | NODE_SET_METHOD(process, "binding", Binding); |
| 2136 | |
Dean McNamee | f67e8f2 | 2011-03-15 22:39:16 | [diff] [blame] | 2137 | return process; |
| 2138 | } |
| 2139 | |
| 2140 | |
| 2141 | static void AtExit() { |
Ryan Dahl | 4e43afd | 2011-09-30 20:11:47 | [diff] [blame] | 2142 | uv_tty_reset_mode(); |
Dean McNamee | f67e8f2 | 2011-03-15 22:39:16 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | |
| 2146 | static void SignalExit(int signal) { |
Ryan Dahl | 4e43afd | 2011-09-30 20:11:47 | [diff] [blame] | 2147 | uv_tty_reset_mode(); |
Dean McNamee | f67e8f2 | 2011-03-15 22:39:16 | [diff] [blame] | 2148 | _exit(1); |
| 2149 | } |
| 2150 | |
| 2151 | |
Ryan Dahl | 7547c7d | 2011-12-07 01:00:33 | [diff] [blame] | 2152 | void Load(Handle<Object> process_l) { |
Ryan Dahl | 9f5643f | 2010-01-31 07:22:34 | [diff] [blame] | 2153 | // Compile, execute the src/node.js file. (Which was included as static C |
| 2154 | // string in node_natives.h. 'natve_node' is the string containing that |
| 2155 | // source code.) |
Ryan Dahl | b20c343 | 2010-02-12 05:55:08 | [diff] [blame] | 2156 | |
Ryan Dahl | 9f5643f | 2010-01-31 07:22:34 | [diff] [blame] | 2157 | // The node.js file returns a function 'f' |
Ryan Dahl | b20c343 | 2010-02-12 05:55:08 | [diff] [blame] | 2158 | |
Dean McNamee | f67e8f2 | 2011-03-15 22:39:16 | [diff] [blame] | 2159 | atexit(AtExit); |
| 2160 | |
Ryan Dahl | 9f5643f | 2010-01-31 07:22:34 | [diff] [blame] | 2161 | TryCatch try_catch; |
Ryan Dahl | 9f5643f | 2010-01-31 07:22:34 | [diff] [blame] | 2162 | |
Tom Hughes | 74954ce | 2011-03-08 06:00:51 | [diff] [blame] | 2163 | Local<Value> f_value = ExecuteString(MainSource(), |
| 2164 | IMMUTABLE_STRING("node.js")); |
Ryan Dahl | 9f5643f | 2010-01-31 07:22:34 | [diff] [blame] | 2165 | if (try_catch.HasCaught()) { |
Ryan Dahl | ab068db | 2010-05-09 20:54:58 | [diff] [blame] | 2166 | ReportException(try_catch, true); |
Ryan Dahl | 9f5643f | 2010-01-31 07:22:34 | [diff] [blame] | 2167 | exit(10); |
| 2168 | } |
Ryan Dahl | 9f5643f | 2010-01-31 07:22:34 | [diff] [blame] | 2169 | assert(f_value->IsFunction()); |
| 2170 | Local<Function> f = Local<Function>::Cast(f_value); |
| 2171 | |
| 2172 | // Now we call 'f' with the 'process' variable that we've built up with |
| 2173 | // all our bindings. Inside node.js we'll take care of assigning things to |
| 2174 | // their places. |
Ryan Dahl | b20c343 | 2010-02-12 05:55:08 | [diff] [blame] | 2175 | |
Ryan Dahl | 9f5643f | 2010-01-31 07:22:34 | [diff] [blame] | 2176 | // We start the process this way in order to be more modular. Developers |
| 2177 | // who do not like how 'src/node.js' setups the module system but do like |
| 2178 | // Node's I/O bindings may want to replace 'f' with their own function. |
| 2179 | |
Ryan Dahl | f8ce848 | 2010-09-17 07:01:07 | [diff] [blame] | 2180 | // Add a reference to the global object |
| 2181 | Local<Object> global = v8::Context::GetCurrent()->Global(); |
Ryan Dahl | 7547c7d | 2011-12-07 01:00:33 | [diff] [blame] | 2182 | Local<Value> args[1] = { Local<Value>::New(process_l) }; |
Zoran Tomicic | d98ea70 | 2010-02-22 05:15:44 | [diff] [blame] | 2183 | |
Ryan Dahl | e9257b8 | 2011-02-10 02:50:26 | [diff] [blame] | 2184 | #ifdef HAVE_DTRACE |
Ryan Dahl | 068b733 | 2011-01-25 01:50:10 | [diff] [blame] | 2185 | InitDTrace(global); |
Ryan Dahl | e9257b8 | 2011-02-10 02:50:26 | [diff] [blame] | 2186 | #endif |
Ryan Dahl | 068b733 | 2011-01-25 01:50:10 | [diff] [blame] | 2187 | |
Ryan Dahl | b20c343 | 2010-02-12 05:55:08 | [diff] [blame] | 2188 | f->Call(global, 1, args); |
Ryan Dahl | 9f5643f | 2010-01-31 07:22:34 | [diff] [blame] | 2189 | |
Ryan Dahl | 8a52fb7 | 2010-07-01 18:10:22 | [diff] [blame] | 2190 | if (try_catch.HasCaught()) { |
| 2191 | ReportException(try_catch, true); |
| 2192 | exit(11); |
Ryan Dahl | 9f5643f | 2010-01-31 07:22:34 | [diff] [blame] | 2193 | } |
Ryan | 27b268b | 2009-06-17 13:05:44 | [diff] [blame] | 2194 | } |
| 2195 | |
Zoran Tomicic | d98ea70 | 2010-02-22 05:15:44 | [diff] [blame] | 2196 | static void PrintHelp(); |
| 2197 | |
| 2198 | static void ParseDebugOpt(const char* arg) { |
| 2199 | const char *p = 0; |
| 2200 | |
| 2201 | use_debug_agent = true; |
| 2202 | if (!strcmp (arg, "--debug-brk")) { |
| 2203 | debug_wait_connect = true; |
| 2204 | return; |
| 2205 | } else if (!strcmp(arg, "--debug")) { |
| 2206 | return; |
| 2207 | } else if (strstr(arg, "--debug-brk=") == arg) { |
| 2208 | debug_wait_connect = true; |
| 2209 | p = 1 + strchr(arg, '='); |
| 2210 | debug_port = atoi(p); |
| 2211 | } else if (strstr(arg, "--debug=") == arg) { |
| 2212 | p = 1 + strchr(arg, '='); |
| 2213 | debug_port = atoi(p); |
| 2214 | } |
| 2215 | if (p && debug_port > 1024 && debug_port < 65536) |
| 2216 | return; |
| 2217 | |
| 2218 | fprintf(stderr, "Bad debug option.\n"); |
| 2219 | if (p) fprintf(stderr, "Debug port must be in range 1025 to 65535.\n"); |
| 2220 | |
| 2221 | PrintHelp(); |
| 2222 | exit(1); |
| 2223 | } |
| 2224 | |
Ryan | d6c9d31 | 2009-09-11 14:02:29 | [diff] [blame] | 2225 | static void PrintHelp() { |
Steve Engledow | 292345f | 2011-07-05 11:07:08 | [diff] [blame] | 2226 | printf("Usage: node [options] [ -e script | script.js ] [arguments] \n" |
Ryan Dahl | 87339a2 | 2011-10-12 09:56:29 | [diff] [blame] | 2227 | " node debug script.js [arguments] \n" |
Ryan Dahl | 4fa712c | 2011-01-13 23:28:16 | [diff] [blame] | 2228 | "\n" |
Ryan Dahl | 209b219 | 2010-03-08 16:33:10 | [diff] [blame] | 2229 | "Options:\n" |
Tom Hughes | 78da9cb | 2010-10-18 22:50:56 | [diff] [blame] | 2230 | " -v, --version print node's version\n" |
Steve Engledow | 292345f | 2011-07-05 11:07:08 | [diff] [blame] | 2231 | " -e, --eval script evaluate script\n" |
Ben Noordhuis | 3d22dbf | 2011-12-01 16:21:00 | [diff] [blame] | 2232 | " -p, --print print result of --eval\n" |
Tom Hughes | 78da9cb | 2010-10-18 22:50:56 | [diff] [blame] | 2233 | " --v8-options print v8 command line options\n" |
| 2234 | " --vars print various compiled-in variables\n" |
| 2235 | " --max-stack-size=val set max v8 stack size (bytes)\n" |
Ryan Dahl | 209b219 | 2010-03-08 16:33:10 | [diff] [blame] | 2236 | "\n" |
Ben Noordhuis | fdf180f | 2011-12-05 07:42:11 | [diff] [blame] | 2237 | "Environment variables:\n" |
| 2238 | #ifdef _WIN32 |
| 2239 | "NODE_PATH ';'-separated list of directories\n" |
| 2240 | #else |
Tom Hughes | 78da9cb | 2010-10-18 22:50:56 | [diff] [blame] | 2241 | "NODE_PATH ':'-separated list of directories\n" |
Ben Noordhuis | fdf180f | 2011-12-05 07:42:11 | [diff] [blame] | 2242 | #endif |
isaacs | c050d0f | 2011-07-25 01:04:45 | [diff] [blame] | 2243 | " prefixed to the module search path.\n" |
Tom Hughes | 78da9cb | 2010-10-18 22:50:56 | [diff] [blame] | 2244 | "NODE_MODULE_CONTEXTS Set to 1 to load modules in their own\n" |
| 2245 | " global contexts.\n" |
Ryan Dahl | 4fa712c | 2011-01-13 23:28:16 | [diff] [blame] | 2246 | "NODE_DISABLE_COLORS Set to 1 to disable colors in the REPL\n" |
Ryan Dahl | 209b219 | 2010-03-08 16:33:10 | [diff] [blame] | 2247 | "\n" |
Ryan Dahl | 4fa712c | 2011-01-13 23:28:16 | [diff] [blame] | 2248 | "Documentation can be found at http://nodejs.org/\n"); |
Ryan | 11df252 | 2009-08-03 16:19:40 | [diff] [blame] | 2249 | } |
| 2250 | |
Ryan Dahl | 3881455 | 2009-10-09 15:15:47 | [diff] [blame] | 2251 | // Parse node command line arguments. |
Dean McNamee | f67e8f2 | 2011-03-15 22:39:16 | [diff] [blame] | 2252 | static void ParseArgs(int argc, char **argv) { |
Peter Griess | 78d33f4 | 2010-06-04 15:29:10 | [diff] [blame] | 2253 | int i; |
| 2254 | |
Ryan Dahl | e742d07 | 2009-10-09 11:25:04 | [diff] [blame] | 2255 | // TODO use parse opts |
Dean McNamee | f67e8f2 | 2011-03-15 22:39:16 | [diff] [blame] | 2256 | for (i = 1; i < argc; i++) { |
Ryan | 11df252 | 2009-08-03 16:19:40 | [diff] [blame] | 2257 | const char *arg = argv[i]; |
Zoran Tomicic | d98ea70 | 2010-02-22 05:15:44 | [diff] [blame] | 2258 | if (strstr(arg, "--debug") == arg) { |
| 2259 | ParseDebugOpt(arg); |
Jonas Pfenniger | 8f59ccc | 2010-02-24 21:11:08 | [diff] [blame] | 2260 | argv[i] = const_cast<char*>(""); |
Ryan | 1910c11 | 2009-09-11 18:05:22 | [diff] [blame] | 2261 | } else if (strcmp(arg, "--version") == 0 || strcmp(arg, "-v") == 0) { |
Ryan | 11df252 | 2009-08-03 16:19:40 | [diff] [blame] | 2262 | printf("%s\n", NODE_VERSION); |
| 2263 | exit(0); |
Ryan Dahl | 40e42e8 | 2010-03-08 17:10:24 | [diff] [blame] | 2264 | } else if (strcmp(arg, "--vars") == 0) { |
Ryan Dahl | a979ab9 | 2011-08-04 23:40:07 | [diff] [blame] | 2265 | #ifdef NODE_PREFIX |
Ryan Dahl | 40e42e8 | 2010-03-08 17:10:24 | [diff] [blame] | 2266 | printf("NODE_PREFIX: %s\n", NODE_PREFIX); |
Ryan Dahl | a979ab9 | 2011-08-04 23:40:07 | [diff] [blame] | 2267 | #endif |
| 2268 | #ifdef NODE_CFLAGS |
Ryan Dahl | 40e42e8 | 2010-03-08 17:10:24 | [diff] [blame] | 2269 | printf("NODE_CFLAGS: %s\n", NODE_CFLAGS); |
Ryan Dahl | a979ab9 | 2011-08-04 23:40:07 | [diff] [blame] | 2270 | #endif |
Ryan | ed9c336 | 2009-09-01 13:28:10 | [diff] [blame] | 2271 | exit(0); |
Tom Hughes | 78da9cb | 2010-10-18 22:50:56 | [diff] [blame] | 2272 | } else if (strstr(arg, "--max-stack-size=") == arg) { |
| 2273 | const char *p = 0; |
| 2274 | p = 1 + strchr(arg, '='); |
| 2275 | max_stack_size = atoi(p); |
| 2276 | argv[i] = const_cast<char*>(""); |
Ryan | 11df252 | 2009-08-03 16:19:40 | [diff] [blame] | 2277 | } else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) { |
| 2278 | PrintHelp(); |
| 2279 | exit(0); |
Ben Noordhuis | 3d22dbf | 2011-12-01 16:21:00 | [diff] [blame] | 2280 | } else if (strcmp(arg, "--eval") == 0 || strcmp(arg, "-e") == 0 || |
| 2281 | strcmp(arg, "-pe") == 0) { |
Dean McNamee | f67e8f2 | 2011-03-15 22:39:16 | [diff] [blame] | 2282 | if (argc <= i + 1) { |
TJ Holowaychuk | 9481bc1 | 2010-10-07 02:05:01 | [diff] [blame] | 2283 | fprintf(stderr, "Error: --eval requires an argument\n"); |
| 2284 | exit(1); |
| 2285 | } |
Ben Noordhuis | 3d22dbf | 2011-12-01 16:21:00 | [diff] [blame] | 2286 | if (arg[1] == 'p') { |
| 2287 | print_eval = true; |
| 2288 | } |
TJ Holowaychuk | 9481bc1 | 2010-10-07 02:05:01 | [diff] [blame] | 2289 | argv[i] = const_cast<char*>(""); |
| 2290 | eval_string = argv[++i]; |
Ben Noordhuis | 3d22dbf | 2011-12-01 16:21:00 | [diff] [blame] | 2291 | } else if (strcmp(arg, "--print") == 0 || strcmp(arg, "-p") == 0) { |
| 2292 | print_eval = true; |
| 2293 | argv[i] = const_cast<char*>(""); |
Ryan | 11df252 | 2009-08-03 16:19:40 | [diff] [blame] | 2294 | } else if (strcmp(arg, "--v8-options") == 0) { |
Jonas Pfenniger | 8f59ccc | 2010-02-24 21:11:08 | [diff] [blame] | 2295 | argv[i] = const_cast<char*>("--help"); |
Jeremy Ashkenas | 2916a2a | 2010-02-21 06:41:27 | [diff] [blame] | 2296 | } else if (argv[i][0] != '-') { |
Jeremy Ashkenas | 2916a2a | 2010-02-21 06:41:27 | [diff] [blame] | 2297 | break; |
Ryan | 11df252 | 2009-08-03 16:19:40 | [diff] [blame] | 2298 | } |
| 2299 | } |
Peter Griess | 78d33f4 | 2010-06-04 15:29:10 | [diff] [blame] | 2300 | |
| 2301 | option_end_index = i; |
Ryan | 11df252 | 2009-08-03 16:19:40 | [diff] [blame] | 2302 | } |
| 2303 | |
Bert Belder | 829735e | 2011-11-04 15:23:02 | [diff] [blame] | 2304 | |
Ben Noordhuis | 74a8215 | 2012-02-03 15:32:00 | [diff] [blame^] | 2305 | static Isolate* node_isolate = NULL; |
| 2306 | static volatile bool debugger_running = false; |
| 2307 | |
Ryan Dahl | 2a7e7b1 | 2010-12-18 19:17:29 | [diff] [blame] | 2308 | static void EnableDebug(bool wait_connect) { |
Bert Belder | 829735e | 2011-11-04 15:23:02 | [diff] [blame] | 2309 | // If we're called from another thread, make sure to enter the right |
| 2310 | // v8 isolate. |
| 2311 | node_isolate->Enter(); |
| 2312 | |
Ryan Dahl | 2a7e7b1 | 2010-12-18 19:17:29 | [diff] [blame] | 2313 | // Start the debug thread and it's associated TCP server on port 5858. |
Fedor Indutny | b0388cc | 2011-12-10 16:52:07 | [diff] [blame] | 2314 | bool r = v8::Debug::EnableAgent("node " NODE_VERSION, debug_port); |
Ryan Dahl | 2a7e7b1 | 2010-12-18 19:17:29 | [diff] [blame] | 2315 | |
| 2316 | if (wait_connect) { |
| 2317 | // Set up an empty handler so v8 will not continue until a debugger |
| 2318 | // attaches. This is the same behavior as Debug::EnableAgent(_,_,true) |
| 2319 | // except we don't break at the beginning of the script. |
| 2320 | // see Debugger::StartAgent in debug.cc of v8/src |
Fedor Indutny | b0388cc | 2011-12-10 16:52:07 | [diff] [blame] | 2321 | v8::Debug::SetMessageHandler2(node::DebugBreakMessageHandler); |
Ryan Dahl | 2a7e7b1 | 2010-12-18 19:17:29 | [diff] [blame] | 2322 | } |
| 2323 | |
| 2324 | // Crappy check that everything went well. FIXME |
| 2325 | assert(r); |
| 2326 | |
| 2327 | // Print out some information. |
Fedor Indutny | 26aab0d | 2011-09-24 07:28:27 | [diff] [blame] | 2328 | fprintf(stderr, "debugger listening on port %d\n", debug_port); |
Bert Belder | 829735e | 2011-11-04 15:23:02 | [diff] [blame] | 2329 | fflush(stderr); |
Fedor Indutny | 9e09fc0 | 2011-09-24 05:44:08 | [diff] [blame] | 2330 | |
| 2331 | debugger_running = true; |
Bert Belder | 829735e | 2011-11-04 15:23:02 | [diff] [blame] | 2332 | |
| 2333 | node_isolate->Exit(); |
Ryan Dahl | 2a7e7b1 | 2010-12-18 19:17:29 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | |
Fedor Indutny | 82d0ac7 | 2011-09-24 13:51:59 | [diff] [blame] | 2337 | #ifdef __POSIX__ |
Fedor Indutny | 26aab0d | 2011-09-24 07:28:27 | [diff] [blame] | 2338 | static void EnableDebugSignalHandler(int signal) { |
| 2339 | // Break once process will return execution to v8 |
Bert Belder | 829735e | 2011-11-04 15:23:02 | [diff] [blame] | 2340 | v8::Debug::DebugBreak(node_isolate); |
Ryan Dahl | 4ab5476 | 2011-03-01 17:59:17 | [diff] [blame] | 2341 | |
Fedor Indutny | 26aab0d | 2011-09-24 07:28:27 | [diff] [blame] | 2342 | if (!debugger_running) { |
Ryan Dahl | 4ab5476 | 2011-03-01 17:59:17 | [diff] [blame] | 2343 | fprintf(stderr, "Hit SIGUSR1 - starting debugger agent.\n"); |
| 2344 | EnableDebug(false); |
| 2345 | } |
Ryan Dahl | 2a7e7b1 | 2010-12-18 19:17:29 | [diff] [blame] | 2346 | } |
| 2347 | |
| 2348 | |
Bert Belder | 829735e | 2011-11-04 15:23:02 | [diff] [blame] | 2349 | static void RegisterSignalHandler(int signal, void (*handler)(int)) { |
Tom Hughes | f61b110 | 2010-10-12 21:01:58 | [diff] [blame] | 2350 | struct sigaction sa; |
| 2351 | |
| 2352 | memset(&sa, 0, sizeof(sa)); |
| 2353 | sa.sa_handler = handler; |
| 2354 | sigfillset(&sa.sa_mask); |
Bert Belder | 829735e | 2011-11-04 15:23:02 | [diff] [blame] | 2355 | sigaction(signal, &sa, NULL); |
| 2356 | } |
| 2357 | |
| 2358 | |
| 2359 | Handle<Value> DebugProcess(const Arguments& args) { |
| 2360 | HandleScope scope; |
| 2361 | |
| 2362 | if (args.Length() != 1) { |
| 2363 | return ThrowException(Exception::Error( |
| 2364 | String::New("Invalid number of arguments."))); |
| 2365 | } |
| 2366 | |
| 2367 | pid_t pid; |
| 2368 | int r; |
| 2369 | |
| 2370 | pid = args[0]->IntegerValue(); |
| 2371 | r = kill(pid, SIGUSR1); |
| 2372 | if (r != 0) { |
| 2373 | return ThrowException(ErrnoException(errno, "kill")); |
| 2374 | } |
| 2375 | |
| 2376 | return Undefined(); |
Tom Hughes | f61b110 | 2010-10-12 21:01:58 | [diff] [blame] | 2377 | } |
Bert Belder | 30bab52 | 2010-11-25 00:09:06 | [diff] [blame] | 2378 | #endif // __POSIX__ |
Tom Hughes | f61b110 | 2010-10-12 21:01:58 | [diff] [blame] | 2379 | |
| 2380 | |
Bert Belder | 829735e | 2011-11-04 15:23:02 | [diff] [blame] | 2381 | #ifdef _WIN32 |
| 2382 | DWORD WINAPI EnableDebugThreadProc(void* arg) { |
| 2383 | // Break once process will return execution to v8 |
| 2384 | if (!debugger_running) { |
| 2385 | for (int i = 0; i < 1; i++) { |
| 2386 | fprintf(stderr, "Starting debugger agent.\r\n"); |
| 2387 | fflush(stderr); |
| 2388 | EnableDebug(false); |
| 2389 | } |
| 2390 | } |
| 2391 | |
| 2392 | v8::Debug::DebugBreak(); |
| 2393 | |
| 2394 | return 0; |
| 2395 | } |
| 2396 | |
| 2397 | |
| 2398 | static int GetDebugSignalHandlerMappingName(DWORD pid, char* buf, size_t buf_len) { |
| 2399 | return snprintf(buf, buf_len, "node-debug-handler-%u", pid); |
| 2400 | } |
| 2401 | |
| 2402 | |
| 2403 | static int RegisterDebugSignalHandler() { |
| 2404 | char mapping_name[32]; |
| 2405 | HANDLE mapping_handle; |
| 2406 | DWORD pid; |
| 2407 | LPTHREAD_START_ROUTINE* handler; |
| 2408 | |
| 2409 | pid = GetCurrentProcessId(); |
| 2410 | |
| 2411 | if (GetDebugSignalHandlerMappingName(pid, |
| 2412 | mapping_name, |
| 2413 | sizeof mapping_name) < 0) { |
| 2414 | return -1; |
| 2415 | } |
| 2416 | |
| 2417 | mapping_handle = CreateFileMappingA(INVALID_HANDLE_VALUE, |
| 2418 | NULL, |
| 2419 | PAGE_READWRITE, |
| 2420 | 0, |
| 2421 | sizeof *handler, |
| 2422 | mapping_name); |
| 2423 | if (mapping_handle == NULL) { |
| 2424 | return -1; |
| 2425 | } |
| 2426 | |
| 2427 | handler = (LPTHREAD_START_ROUTINE*) MapViewOfFile(mapping_handle, |
| 2428 | FILE_MAP_ALL_ACCESS, |
| 2429 | 0, |
| 2430 | 0, |
| 2431 | sizeof *handler); |
| 2432 | if (handler == NULL) { |
| 2433 | CloseHandle(mapping_handle); |
| 2434 | return -1; |
| 2435 | } |
| 2436 | |
| 2437 | *handler = EnableDebugThreadProc; |
| 2438 | |
| 2439 | UnmapViewOfFile((void*) handler); |
| 2440 | |
| 2441 | return 0; |
| 2442 | } |
| 2443 | |
| 2444 | |
| 2445 | static Handle<Value> DebugProcess(const Arguments& args) { |
| 2446 | HandleScope scope; |
| 2447 | Handle<Value> rv = Undefined(); |
| 2448 | DWORD pid; |
Ryan Dahl | 7547c7d | 2011-12-07 01:00:33 | [diff] [blame] | 2449 | HANDLE process_l = NULL; |
Bert Belder | 829735e | 2011-11-04 15:23:02 | [diff] [blame] | 2450 | HANDLE thread = NULL; |
| 2451 | HANDLE mapping = NULL; |
| 2452 | char mapping_name[32]; |
| 2453 | LPTHREAD_START_ROUTINE* handler = NULL; |
| 2454 | |
| 2455 | if (args.Length() != 1) { |
| 2456 | rv = ThrowException(Exception::Error(String::New("Invalid number of arguments."))); |
| 2457 | goto out; |
| 2458 | } |
| 2459 | |
| 2460 | pid = (DWORD) args[0]->IntegerValue(); |
| 2461 | |
Ryan Dahl | 7547c7d | 2011-12-07 01:00:33 | [diff] [blame] | 2462 | process_l = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | |
Bert Belder | 829735e | 2011-11-04 15:23:02 | [diff] [blame] | 2463 | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | |
| 2464 | PROCESS_VM_READ, |
| 2465 | FALSE, |
| 2466 | pid); |
Ryan Dahl | 7547c7d | 2011-12-07 01:00:33 | [diff] [blame] | 2467 | if (process_l == NULL) { |
Bert Belder | 829735e | 2011-11-04 15:23:02 | [diff] [blame] | 2468 | rv = ThrowException(WinapiErrnoException(GetLastError(), "OpenProcess")); |
| 2469 | goto out; |
| 2470 | } |
| 2471 | |
| 2472 | if (GetDebugSignalHandlerMappingName(pid, |
| 2473 | mapping_name, |
| 2474 | sizeof mapping_name) < 0) { |
| 2475 | rv = ThrowException(ErrnoException(errno, "sprintf")); |
| 2476 | goto out; |
| 2477 | } |
| 2478 | |
| 2479 | mapping = OpenFileMapping(FILE_MAP_READ, FALSE, mapping_name); |
| 2480 | if (mapping == NULL) { |
| 2481 | rv = ThrowException(WinapiErrnoException(GetLastError(), "sprintf")); |
| 2482 | goto out; |
| 2483 | } |
| 2484 | |
| 2485 | handler = (LPTHREAD_START_ROUTINE*) MapViewOfFile(mapping, |
| 2486 | FILE_MAP_READ, |
| 2487 | 0, |
| 2488 | 0, |
| 2489 | sizeof *handler); |
| 2490 | if (handler == NULL || *handler == NULL) { |
| 2491 | rv = ThrowException(WinapiErrnoException(GetLastError(), "MapViewOfFile")); |
| 2492 | goto out; |
| 2493 | } |
| 2494 | |
Ryan Dahl | 7547c7d | 2011-12-07 01:00:33 | [diff] [blame] | 2495 | thread = CreateRemoteThread(process_l, |
Bert Belder | 829735e | 2011-11-04 15:23:02 | [diff] [blame] | 2496 | NULL, |
| 2497 | 0, |
| 2498 | *handler, |
| 2499 | NULL, |
| 2500 | 0, |
| 2501 | NULL); |
| 2502 | if (thread == NULL) { |
| 2503 | rv = ThrowException(WinapiErrnoException(GetLastError(), |
| 2504 | "CreateRemoteThread")); |
| 2505 | goto out; |
| 2506 | } |
| 2507 | |
| 2508 | // Wait for the thread to terminate |
| 2509 | if (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0) { |
| 2510 | rv = ThrowException(WinapiErrnoException(GetLastError(), |
| 2511 | "WaitForSingleObject")); |
| 2512 | goto out; |
| 2513 | } |
| 2514 | |
| 2515 | out: |
Ryan Dahl | 7547c7d | 2011-12-07 01:00:33 | [diff] [blame] | 2516 | if (process_l != NULL) { |
| 2517 | CloseHandle(process_l); |
Bert Belder | 829735e | 2011-11-04 15:23:02 | [diff] [blame] | 2518 | } |
| 2519 | if (thread != NULL) { |
| 2520 | CloseHandle(thread); |
| 2521 | } |
| 2522 | if (handler != NULL) { |
| 2523 | UnmapViewOfFile(handler); |
| 2524 | } |
| 2525 | if (mapping != NULL) { |
| 2526 | CloseHandle(mapping); |
| 2527 | } |
| 2528 | |
| 2529 | return Undefined(); |
| 2530 | } |
| 2531 | #endif // _WIN32 |
| 2532 | |
| 2533 | |
Fedor Indutny | b0388cc | 2011-12-10 16:52:07 | [diff] [blame] | 2534 | static Handle<Value> DebugPause(const Arguments& args) { |
| 2535 | v8::Debug::DebugBreak(node_isolate); |
Ben Noordhuis | aac717d | 2011-12-19 23:30:41 | [diff] [blame] | 2536 | return Undefined(); |
Fedor Indutny | b0388cc | 2011-12-10 16:52:07 | [diff] [blame] | 2537 | } |
| 2538 | |
| 2539 | |
Ben Noordhuis | 74a8215 | 2012-02-03 15:32:00 | [diff] [blame^] | 2540 | char** Init(int argc, char *argv[]) { |
| 2541 | // Initialize prog_start_time to get relative uptime. |
| 2542 | uv_uptime(&prog_start_time); |
| 2543 | |
Ryan Dahl | 5185c15 | 2010-06-18 07:26:49 | [diff] [blame] | 2544 | // Hack aroung with the argv pointer. Used for process.title = "blah". |
Igor Zinkovsky | 500c8f4 | 2011-12-15 20:36:05 | [diff] [blame] | 2545 | argv = uv_setup_args(argc, argv); |
Ryan Dahl | 5185c15 | 2010-06-18 07:26:49 | [diff] [blame] | 2546 | |
Ryan Dahl | 3881455 | 2009-10-09 15:15:47 | [diff] [blame] | 2547 | // Parse a few arguments which are specific to Node. |
Dean McNamee | f67e8f2 | 2011-03-15 22:39:16 | [diff] [blame] | 2548 | node::ParseArgs(argc, argv); |
Jeremy Ashkenas | 2916a2a | 2010-02-21 06:41:27 | [diff] [blame] | 2549 | // Parse the rest of the args (up to the 'option_end_index' (where '--' was |
Ryan Dahl | 3881455 | 2009-10-09 15:15:47 | [diff] [blame] | 2550 | // in the command line)) |
Ryan Dahl | 7547c7d | 2011-12-07 01:00:33 | [diff] [blame] | 2551 | int v8argc = option_end_index; |
Danny Coates | dc8c079 | 2010-08-01 22:46:48 | [diff] [blame] | 2552 | char **v8argv = argv; |
| 2553 | |
Ryan Dahl | 7547c7d | 2011-12-07 01:00:33 | [diff] [blame] | 2554 | if (debug_wait_connect) { |
Ryan Dahl | adec544 | 2010-08-04 17:38:19 | [diff] [blame] | 2555 | // v8argv is a copy of argv up to the script file argument +2 if --debug-brk |
Ryan Dahl | d408de8 | 2010-08-06 19:31:41 | [diff] [blame] | 2556 | // to expose the v8 debugger js object so that node.js can set |
Ryan Dahl | adec544 | 2010-08-04 17:38:19 | [diff] [blame] | 2557 | // a breakpoint on the first line of the startup script |
| 2558 | v8argc += 2; |
| 2559 | v8argv = new char*[v8argc]; |
Ryan Dahl | 7547c7d | 2011-12-07 01:00:33 | [diff] [blame] | 2560 | memcpy(v8argv, argv, sizeof(argv) * option_end_index); |
| 2561 | v8argv[option_end_index] = const_cast<char*>("--expose_debug_as"); |
| 2562 | v8argv[option_end_index + 1] = const_cast<char*>("v8debug"); |
Ryan Dahl | adec544 | 2010-08-04 17:38:19 | [diff] [blame] | 2563 | } |
Tom Hughes | 78da9cb | 2010-10-18 22:50:56 | [diff] [blame] | 2564 | |
| 2565 | // For the normal stack which moves from high to low addresses when frames |
| 2566 | // are pushed, we can compute the limit as stack_size bytes below the |
| 2567 | // the address of a stack variable (e.g. &stack_var) as an approximation |
| 2568 | // of the start of the stack (we're assuming that we haven't pushed a lot |
| 2569 | // of frames yet). |
Ryan Dahl | 7547c7d | 2011-12-07 01:00:33 | [diff] [blame] | 2570 | if (max_stack_size != 0) { |
Tom Hughes | 78da9cb | 2010-10-18 22:50:56 | [diff] [blame] | 2571 | uint32_t stack_var; |
| 2572 | ResourceConstraints constraints; |
| 2573 | |
Ryan Dahl | 7547c7d | 2011-12-07 01:00:33 | [diff] [blame] | 2574 | uint32_t *stack_limit = &stack_var - (max_stack_size / sizeof(uint32_t)); |
Tom Hughes | 78da9cb | 2010-10-18 22:50:56 | [diff] [blame] | 2575 | constraints.set_stack_limit(stack_limit); |
| 2576 | SetResourceConstraints(&constraints); // Must be done before V8::Initialize |
| 2577 | } |
Danny Coates | dc8c079 | 2010-08-01 22:46:48 | [diff] [blame] | 2578 | V8::SetFlagsFromCommandLine(&v8argc, v8argv, false); |
Ryan | 2337630 | 2009-09-10 10:34:29 | [diff] [blame] | 2579 | |
Bert Belder | 30bab52 | 2010-11-25 00:09:06 | [diff] [blame] | 2580 | #ifdef __POSIX__ |
Ryan Dahl | b6c5cf6 | 2010-05-04 17:41:56 | [diff] [blame] | 2581 | // Ignore SIGPIPE |
Tom Hughes | f61b110 | 2010-10-12 21:01:58 | [diff] [blame] | 2582 | RegisterSignalHandler(SIGPIPE, SIG_IGN); |
| 2583 | RegisterSignalHandler(SIGINT, SignalExit); |
| 2584 | RegisterSignalHandler(SIGTERM, SignalExit); |
Bert Belder | 30bab52 | 2010-11-25 00:09:06 | [diff] [blame] | 2585 | #endif // __POSIX__ |
Ryan Dahl | b6c5cf6 | 2010-05-04 17:41:56 | [diff] [blame] | 2586 | |
Ben Noordhuis | 74a8215 | 2012-02-03 15:32:00 | [diff] [blame^] | 2587 | uv_prepare_init(uv_default_loop(), &prepare_tick_watcher); |
| 2588 | uv_prepare_start(&prepare_tick_watcher, PrepareTick); |
| 2589 | uv_unref(uv_default_loop()); |
| 2590 | |
| 2591 | uv_check_init(uv_default_loop(), &check_tick_watcher); |
| 2592 | uv_check_start(&check_tick_watcher, node::CheckTick); |
| 2593 | uv_unref(uv_default_loop()); |
| 2594 | |
| 2595 | uv_idle_init(uv_default_loop(), &tick_spinner); |
| 2596 | uv_unref(uv_default_loop()); |
| 2597 | |
| 2598 | uv_check_init(uv_default_loop(), &gc_check); |
| 2599 | uv_check_start(&gc_check, node::Check); |
| 2600 | uv_unref(uv_default_loop()); |
| 2601 | |
| 2602 | uv_idle_init(uv_default_loop(), &gc_idle); |
| 2603 | uv_unref(uv_default_loop()); |
| 2604 | |
| 2605 | uv_timer_init(uv_default_loop(), &gc_timer); |
| 2606 | uv_unref(uv_default_loop()); |
| 2607 | |
| 2608 | V8::SetFatalErrorHandler(node::OnFatalError); |
| 2609 | |
| 2610 | // Fetch a reference to the main isolate, so we have a reference to it |
| 2611 | // even when we need it to access it from another (debugger) thread. |
| 2612 | node_isolate = Isolate::GetCurrent(); |
| 2613 | |
| 2614 | // If the --debug flag was specified then initialize the debug thread. |
| 2615 | if (use_debug_agent) { |
| 2616 | EnableDebug(debug_wait_connect); |
| 2617 | } else { |
| 2618 | #ifdef _WIN32 |
| 2619 | RegisterDebugSignalHandler(); |
| 2620 | #else // Posix |
| 2621 | RegisterSignalHandler(SIGUSR1, EnableDebugSignalHandler); |
| 2622 | #endif // __POSIX__ |
| 2623 | } |
| 2624 | |
Ben Noordhuis | 5866f1a | 2011-12-09 18:02:33 | [diff] [blame] | 2625 | return argv; |
| 2626 | } |
Ben Noordhuis | 356992f | 2011-11-22 16:10:09 | [diff] [blame] | 2627 | |
Ben Noordhuis | 5866f1a | 2011-12-09 18:02:33 | [diff] [blame] | 2628 | |
| 2629 | void EmitExit(v8::Handle<v8::Object> process_l) { |
| 2630 | // process.emit('exit') |
| 2631 | Local<Value> emit_v = process_l->Get(String::New("emit")); |
| 2632 | assert(emit_v->IsFunction()); |
| 2633 | Local<Function> emit = Local<Function>::Cast(emit_v); |
| 2634 | Local<Value> args[] = { String::New("exit") }; |
| 2635 | TryCatch try_catch; |
| 2636 | emit->Call(process_l, 1, args); |
| 2637 | if (try_catch.HasCaught()) { |
| 2638 | FatalException(try_catch); |
| 2639 | } |
| 2640 | } |
| 2641 | |
| 2642 | |
Ben Noordhuis | 74a8215 | 2012-02-03 15:32:00 | [diff] [blame^] | 2643 | int Start(int argc, char *argv[]) { |
| 2644 | // This needs to run *before* V8::Initialize() |
| 2645 | argv = Init(argc, argv); |
Ben Noordhuis | 5866f1a | 2011-12-09 18:02:33 | [diff] [blame] | 2646 | |
Ben Noordhuis | 74a8215 | 2012-02-03 15:32:00 | [diff] [blame^] | 2647 | v8::V8::Initialize(); |
| 2648 | v8::HandleScope handle_scope; |
Ben Noordhuis | 809fdf2 | 2011-12-09 20:49:10 | [diff] [blame] | 2649 | |
Ben Noordhuis | 74a8215 | 2012-02-03 15:32:00 | [diff] [blame^] | 2650 | // Create the one and only Context. |
| 2651 | Persistent<v8::Context> context = v8::Context::New(); |
| 2652 | v8::Context::Scope context_scope(context); |
Ryan Dahl | e742d07 | 2009-10-09 11:25:04 | [diff] [blame] | 2653 | |
Ryan Dahl | 7547c7d | 2011-12-07 01:00:33 | [diff] [blame] | 2654 | Handle<Object> process_l = SetupProcessObject(argc, argv); |
Ben Noordhuis | 74a8215 | 2012-02-03 15:32:00 | [diff] [blame^] | 2655 | v8_typed_array::AttachBindings(context->Global()); |
Ryan Dahl | 4d02e77 | 2011-12-17 07:23:34 | [diff] [blame] | 2656 | |
Ryan Dahl | 3881455 | 2009-10-09 15:15:47 | [diff] [blame] | 2657 | // Create all the objects, load modules, do everything. |
| 2658 | // so your next reading stop should be node::Load()! |
Ryan Dahl | 7547c7d | 2011-12-07 01:00:33 | [diff] [blame] | 2659 | Load(process_l); |
Ryan | 19478ed | 2009-03-03 00:56:15 | [diff] [blame] | 2660 | |
Ryan Dahl | d0beac7 | 2010-12-17 02:40:23 | [diff] [blame] | 2661 | // All our arguments are loaded. We've evaluated all of the scripts. We |
| 2662 | // might even have created TCP servers. Now we enter the main eventloop. If |
| 2663 | // there are no watchers on the loop (except for the ones that were |
Bert Belder | 9cec08e | 2011-05-23 23:42:22 | [diff] [blame] | 2664 | // uv_unref'd) then this function exits. As long as there are active |
Ryan Dahl | d0beac7 | 2010-12-17 02:40:23 | [diff] [blame] | 2665 | // watchers, it blocks. |
Ben Noordhuis | 74a8215 | 2012-02-03 15:32:00 | [diff] [blame^] | 2666 | uv_run(uv_default_loop()); |
Ryan Dahl | c7b24ef | 2010-11-12 00:33:21 | [diff] [blame] | 2667 | |
Ryan Dahl | 7547c7d | 2011-12-07 01:00:33 | [diff] [blame] | 2668 | EmitExit(process_l); |
Ben Noordhuis | 6611692 | 2011-11-23 19:50:28 | [diff] [blame] | 2669 | |
Ryan Dahl | 3881455 | 2009-10-09 15:15:47 | [diff] [blame] | 2670 | #ifndef NDEBUG |
Ryan Dahl | 3881455 | 2009-10-09 15:15:47 | [diff] [blame] | 2671 | // Clean up. |
Ben Noordhuis | 74a8215 | 2012-02-03 15:32:00 | [diff] [blame^] | 2672 | context.Dispose(); |
Ryan | 27b268b | 2009-06-17 13:05:44 | [diff] [blame] | 2673 | V8::Dispose(); |
Ryan Dahl | 3881455 | 2009-10-09 15:15:47 | [diff] [blame] | 2674 | #endif // NDEBUG |
Igor Zinkovsky | a58b643 | 2011-07-07 20:54:30 | [diff] [blame] | 2675 | |
Ryan | f6a7fe2 | 2009-06-08 14:17:33 | [diff] [blame] | 2676 | return 0; |
Ryan | 19478ed | 2009-03-03 00:56:15 | [diff] [blame] | 2677 | } |
Ryan Dahl | 124fbed | 2010-09-19 20:13:57 | [diff] [blame] | 2678 | |
| 2679 | |
| 2680 | } // namespace node |