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