FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
node/src/node.cc at master · arnabc/node · GitHub
arnabc
/
node
Public
forked from
nodejs/node-v0.x-archive
Notifications
You must be signed in to change notification settings
Fork
1
Star
1
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
node
/
src
/
node.cc
Copy path
More file actions
More file actions
Latest commit
History
History
History
1897 lines (1479 loc) · 46.8 KB
Breadcrumbs
node
/
src
/
node.cc
Copy path
File metadata and controls
1897 lines (1479 loc) · 46.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
Copyright 2009 Ryan Dahl <ry@tinyclouds.org>
#
include
<
node.h
>
#
include
<
locale.h
>
#
include
<
stdio.h
>
#
include
<
stdlib.h
>
#
include
<
strings.h
>
#
include
<
string.h
>
#
include
<
limits.h
>
/*
PATH_MAX
*/
#
include
<
assert.h
>
#
include
<
unistd.h
>
#
include
<
errno.h
>
#
include
<
dlfcn.h
>
/*
dlopen(), dlsym()
*/
#
include
<
sys/types.h
>
#
include
<
unistd.h
>
/*
setuid, getuid
*/
#
include
<
pwd.h
>
/*
getpwnam()
*/
#
include
<
grp.h
>
/*
getgrnam()
*/
#
include
"
platform.h
"
#
include
<
node_buffer.h
>
#
include
<
node_io_watcher.h
>
#
include
<
node_net.h
>
#
include
<
node_events.h
>
#
include
<
node_cares.h
>
#
include
<
node_file.h
>
#
if
0
// not in use
# include <node_idle_watcher.h>
#
endif
#
include
<
node_http_parser.h
>
#
include
<
node_signal_watcher.h
>
#
include
<
node_stat_watcher.h
>
#
include
<
node_timer.h
>
#
include
<
node_child_process.h
>
#
include
<
node_constants.h
>
#
include
<
node_stdio.h
>
#
include
<
node_natives.h
>
#
include
<
node_version.h
>
#
ifdef
HAVE_OPENSSL
#
include
<
node_crypto.h
>
#
endif
#
include
<
node_script.h
>
#
include
<
v8-debug.h
>
#
define
ARRAY_SIZE
(
a
) (
sizeof
(a) /
sizeof
(*(a)))
using
namespace
v8
;
extern
char
**environ;
namespace
node
{
static
Persistent<Object> process;
static
Persistent<String> errno_symbol;
static
Persistent<String> syscall_symbol;
static
Persistent<String> errpath_symbol;
static
Persistent<String> rss_symbol;
static
Persistent<String> vsize_symbol;
static
Persistent<String> heap_total_symbol;
static
Persistent<String> heap_used_symbol;
static
Persistent<String> listeners_symbol;
static
Persistent<String> uncaught_exception_symbol;
static
Persistent<String> emit_symbol;
static
int
option_end_index =
0
;
static
bool
use_debug_agent =
false
;
static
bool
debug_wait_connect =
false
;
static
int
debug_port=
5858
;
static
ev_prepare next_tick_watcher;
static
ev_idle tick_spinner;
static
bool
need_tick_cb;
static
Persistent<String> tick_callback_sym;
static
ev_async eio_want_poll_notifier;
static
ev_async eio_done_poll_notifier;
static
ev_idle eio_poller;
//
Buffer for getpwnam_r(), getgrpam_r() and other misc callers; keep this
//
scoped at file-level rather than method-level to avoid excess stack usage.
static
char
getbuf[
PATH_MAX
+
1
];
//
We need to notify V8 when we're idle so that it can run the garbage
//
collector. The interface to this is V8::IdleNotification(). It returns
//
true if the heap hasn't be fully compacted, and needs to be run again.
//
Returning false means that it doesn't have anymore work to do.
//
//
A rather convoluted algorithm has been devised to determine when Node is
//
idle. You'll have to figure it out for yourself.
static
ev_check gc_check;
static
ev_idle gc_idle;
static
ev_timer gc_timer;
bool
need_gc;
#
define
FAST_TICK
0.7
#
define
GC_WAIT_TIME
5
.
#
define
RPM_SAMPLES
100
#
define
TICK_TIME
(
n
) tick_times[(tick_time_head - (n)) %
RPM_SAMPLES
]
static
ev_tstamp tick_times[
RPM_SAMPLES
];
static
int
tick_time_head;
static
void
StartGCTimer
() {
if
(!
ev_is_active
(&gc_timer)) {
ev_timer_start
(
EV_DEFAULT_UC_
&gc_timer);
ev_unref
(
EV_DEFAULT_UC
);
}
}
static
void
StopGCTimer
() {
if
(
ev_is_active
(&gc_timer)) {
ev_ref
(
EV_DEFAULT_UC
);
ev_timer_stop
(
EV_DEFAULT_UC_
&gc_timer);
}
}
static
void
Idle
(
EV_P_
ev_idle *watcher,
int
revents) {
assert
(watcher == &gc_idle);
assert
(revents ==
EV_IDLE
);
//
fprintf(stderr, "idle\n");
if
(
V8::IdleNotification
()) {
ev_idle_stop
(
EV_A_
watcher);
StopGCTimer
();
}
}
//
Called directly after every call to select() (or epoll, or whatever)
static
void
Check
(
EV_P_
ev_check *watcher,
int
revents) {
assert
(watcher == &gc_check);
assert
(revents ==
EV_CHECK
);
tick_times[tick_time_head] =
ev_now
(
EV_DEFAULT_UC
);
tick_time_head = (tick_time_head +
1
) %
RPM_SAMPLES
;
StartGCTimer
();
for
(
int
i =
0
; i < (
int
)(
GC_WAIT_TIME
/
FAST_TICK
); i++) {
double
d =
TICK_TIME
(i+
1
) -
TICK_TIME
(i+
2
);
//
printf("d = %f\n", d);
//
If in the last 5 ticks the difference between
//
ticks was less than 0.7 seconds, then continue.
if
(d <
FAST_TICK
) {
//
printf("---\n");
return
;
}
}
//
Otherwise start the gc!
//
fprintf(stderr, "start idle 2\n");
ev_idle_start
(
EV_A_
&gc_idle);
}
static
Handle<Value>
NeedTickCallback
(
const
Arguments& args) {
HandleScope scope;
need_tick_cb =
true
;
ev_idle_start
(
EV_DEFAULT_UC_
&tick_spinner);
return
Undefined
();
}
static
void
Spin
(
EV_P_
ev_idle *watcher,
int
revents) {
assert
(watcher == &tick_spinner);
assert
(revents ==
EV_IDLE
);
}
static
void
Tick
(
EV_P_
ev_prepare *watcher,
int
revents) {
assert
(watcher == &next_tick_watcher);
assert
(revents ==
EV_PREPARE
);
//
Avoid entering a V8 scope.
if
(!need_tick_cb)
return
;
need_tick_cb =
false
;
ev_idle_stop
(
EV_DEFAULT_UC_
&tick_spinner);
HandleScope scope;
if
(tick_callback_sym.
IsEmpty
()) {
//
Lazily set the symbol
tick_callback_sym =
Persistent<String>::
New
(
String::NewSymbol
(
"
_tickCallback
"
));
}
Local<Value> cb_v = process->
Get
(tick_callback_sym);
if
(!cb_v->
IsFunction
())
return
;
Local<Function> cb = Local<Function>::
Cast
(cb_v);
TryCatch try_catch;
cb->
Call
(process,
0
,
NULL
);
if
(try_catch.
HasCaught
()) {
FatalException
(try_catch);
}
}
static
void
DoPoll
(
EV_P_
ev_idle *watcher,
int
revents) {
assert
(watcher == &eio_poller);
assert
(revents ==
EV_IDLE
);
//
printf("eio_poller\n");
if
(
eio_poll
() != -
1
) {
//
printf("eio_poller stop\n");
ev_idle_stop
(
EV_DEFAULT_UC_
watcher);
}
}
//
Called from the main thread.
static
void
WantPollNotifier
(
EV_P_
ev_async *watcher,
int
revents) {
assert
(watcher == &eio_want_poll_notifier);
assert
(revents ==
EV_ASYNC
);
//
printf("want poll notifier\n");
if
(
eio_poll
() == -
1
) {
//
printf("eio_poller start\n");
ev_idle_start
(
EV_DEFAULT_UC_
&eio_poller);
}
}
static
void
DonePollNotifier
(
EV_P_
ev_async *watcher,
int
revents) {
assert
(watcher == &eio_done_poll_notifier);
assert
(revents ==
EV_ASYNC
);
//
printf("done poll notifier\n");
if
(
eio_poll
() != -
1
) {
//
printf("eio_poller stop\n");
ev_idle_stop
(
EV_DEFAULT_UC_
&eio_poller);
}
}
//
EIOWantPoll() is called from the EIO thread pool each time an EIO
//
request (that is, one of the node.fs.* functions) has completed.
static
void
EIOWantPoll
(
void
) {
//
Signal the main thread that eio_poll need to be processed.
ev_async_send
(
EV_DEFAULT_UC_
&eio_want_poll_notifier);
}
static
void
EIODonePoll
(
void
) {
//
Signal the main thread that we should stop calling eio_poll().
//
from the idle watcher.
ev_async_send
(
EV_DEFAULT_UC_
&eio_done_poll_notifier);
}
static
inline
const
char
*
errno_string
(
int
errorno) {
#
define
ERRNO_CASE
(
e
)
case
e:
return
#e;
switch
(errorno) {
#
ifdef
EACCES
ERRNO_CASE
(
EACCES
);
#
endif
#
ifdef
EADDRINUSE
ERRNO_CASE
(
EADDRINUSE
);
#
endif
#
ifdef
EADDRNOTAVAIL
ERRNO_CASE
(
EADDRNOTAVAIL
);
#
endif
#
ifdef
EAFNOSUPPORT
ERRNO_CASE
(
EAFNOSUPPORT
);
#
endif
#
ifdef
EAGAIN
ERRNO_CASE
(
EAGAIN
);
#
endif
#
ifdef
EWOULDBLOCK
#
if
EAGAIN != EWOULDBLOCK
ERRNO_CASE
(
EWOULDBLOCK
);
#
endif
#
endif
#
ifdef
EALREADY
ERRNO_CASE
(
EALREADY
);
#
endif
#
ifdef
EBADF
ERRNO_CASE
(
EBADF
);
#
endif
#
ifdef
EBADMSG
ERRNO_CASE
(
EBADMSG
);
#
endif
#
ifdef
EBUSY
ERRNO_CASE
(
EBUSY
);
#
endif
#
ifdef
ECANCELED
ERRNO_CASE
(
ECANCELED
);
#
endif
#
ifdef
ECHILD
ERRNO_CASE
(
ECHILD
);
#
endif
#
ifdef
ECONNABORTED
ERRNO_CASE
(
ECONNABORTED
);
#
endif
#
ifdef
ECONNREFUSED
ERRNO_CASE
(
ECONNREFUSED
);
#
endif
#
ifdef
ECONNRESET
ERRNO_CASE
(
ECONNRESET
);
#
endif
#
ifdef
EDEADLK
ERRNO_CASE
(
EDEADLK
);
#
endif
#
ifdef
EDESTADDRREQ
ERRNO_CASE
(
EDESTADDRREQ
);
#
endif
#
ifdef
EDOM
ERRNO_CASE
(
EDOM
);
#
endif
#
ifdef
EDQUOT
ERRNO_CASE
(
EDQUOT
);
#
endif
#
ifdef
EEXIST
ERRNO_CASE
(
EEXIST
);
#
endif
#
ifdef
EFAULT
ERRNO_CASE
(
EFAULT
);
#
endif
#
ifdef
EFBIG
ERRNO_CASE
(
EFBIG
);
#
endif
#
ifdef
EHOSTUNREACH
ERRNO_CASE
(
EHOSTUNREACH
);
#
endif
#
ifdef
EIDRM
ERRNO_CASE
(
EIDRM
);
#
endif
#
ifdef
EILSEQ
ERRNO_CASE
(
EILSEQ
);
#
endif
#
ifdef
EINPROGRESS
ERRNO_CASE
(
EINPROGRESS
);
#
endif
#
ifdef
EINTR
ERRNO_CASE
(
EINTR
);
#
endif
#
ifdef
EINVAL
ERRNO_CASE
(
EINVAL
);
#
endif
#
ifdef
EIO
ERRNO_CASE
(
EIO
);
#
endif
#
ifdef
EISCONN
ERRNO_CASE
(
EISCONN
);
#
endif
#
ifdef
EISDIR
ERRNO_CASE
(
EISDIR
);
#
endif
#
ifdef
ELOOP
ERRNO_CASE
(
ELOOP
);
#
endif
#
ifdef
EMFILE
ERRNO_CASE
(
EMFILE
);
#
endif
#
ifdef
EMLINK
ERRNO_CASE
(
EMLINK
);
#
endif
#
ifdef
EMSGSIZE
ERRNO_CASE
(
EMSGSIZE
);
#
endif
#
ifdef
EMULTIHOP
ERRNO_CASE
(
EMULTIHOP
);
#
endif
#
ifdef
ENAMETOOLONG
ERRNO_CASE
(
ENAMETOOLONG
);
#
endif
#
ifdef
ENETDOWN
ERRNO_CASE
(
ENETDOWN
);
#
endif
#
ifdef
ENETRESET
ERRNO_CASE
(
ENETRESET
);
#
endif
#
ifdef
ENETUNREACH
ERRNO_CASE
(
ENETUNREACH
);
#
endif
#
ifdef
ENFILE
ERRNO_CASE
(
ENFILE
);
#
endif
#
ifdef
ENOBUFS
ERRNO_CASE
(
ENOBUFS
);
#
endif
#
ifdef
ENODATA
ERRNO_CASE
(
ENODATA
);
#
endif
#
ifdef
ENODEV
ERRNO_CASE
(
ENODEV
);
#
endif
#
ifdef
ENOENT
ERRNO_CASE
(
ENOENT
);
#
endif
#
ifdef
ENOEXEC
ERRNO_CASE
(
ENOEXEC
);
#
endif
#
ifdef
ENOLINK
ERRNO_CASE
(
ENOLINK
);
#
endif
#
ifdef
ENOLCK
#
if
ENOLINK != ENOLCK
ERRNO_CASE
(
ENOLCK
);
#
endif
#
endif
#
ifdef
ENOMEM
ERRNO_CASE
(
ENOMEM
);
#
endif
#
ifdef
ENOMSG
ERRNO_CASE
(
ENOMSG
);
#
endif
#
ifdef
ENOPROTOOPT
ERRNO_CASE
(
ENOPROTOOPT
);
#
endif
#
ifdef
ENOSPC
ERRNO_CASE
(
ENOSPC
);
#
endif
#
ifdef
ENOSR
ERRNO_CASE
(
ENOSR
);
#
endif
#
ifdef
ENOSTR
ERRNO_CASE
(
ENOSTR
);
#
endif
#
ifdef
ENOSYS
ERRNO_CASE
(
ENOSYS
);
#
endif
#
ifdef
ENOTCONN
ERRNO_CASE
(
ENOTCONN
);
#
endif
#
ifdef
ENOTDIR
ERRNO_CASE
(
ENOTDIR
);
#
endif
#
ifdef
ENOTEMPTY
ERRNO_CASE
(
ENOTEMPTY
);
#
endif
#
ifdef
ENOTSOCK
ERRNO_CASE
(
ENOTSOCK
);
#
endif
#
ifdef
ENOTSUP
ERRNO_CASE
(
ENOTSUP
);
#
else
#
ifdef
EOPNOTSUPP
ERRNO_CASE
(
EOPNOTSUPP
);
#
endif
#
endif
#
ifdef
ENOTTY
ERRNO_CASE
(
ENOTTY
);
#
endif
#
ifdef
ENXIO
ERRNO_CASE
(
ENXIO
);
#
endif
#
ifdef
EOVERFLOW
ERRNO_CASE
(
EOVERFLOW
);
#
endif
#
ifdef
EPERM
ERRNO_CASE
(
EPERM
);
#
endif
#
ifdef
EPIPE
ERRNO_CASE
(
EPIPE
);
#
endif
#
ifdef
EPROTO
ERRNO_CASE
(
EPROTO
);
#
endif
#
ifdef
EPROTONOSUPPORT
ERRNO_CASE
(
EPROTONOSUPPORT
);
#
endif
#
ifdef
EPROTOTYPE
ERRNO_CASE
(
EPROTOTYPE
);
#
endif
#
ifdef
ERANGE
ERRNO_CASE
(
ERANGE
);
#
endif
#
ifdef
EROFS
ERRNO_CASE
(
EROFS
);
#
endif
#
ifdef
ESPIPE
ERRNO_CASE
(
ESPIPE
);
#
endif
#
ifdef
ESRCH
ERRNO_CASE
(
ESRCH
);
#
endif
#
ifdef
ESTALE
ERRNO_CASE
(
ESTALE
);
#
endif
#
ifdef
ETIME
ERRNO_CASE
(
ETIME
);
#
endif
#
ifdef
ETIMEDOUT
ERRNO_CASE
(
ETIMEDOUT
);
#
endif
#
ifdef
ETXTBSY
ERRNO_CASE
(
ETXTBSY
);
#
endif
#
ifdef
EXDEV
ERRNO_CASE
(
EXDEV
);
#
endif
default
:
return
"
"
;
}
}
const
char
*
signo_string
(
int
signo) {
#
define
SIGNO_CASE
(
e
)
case
e:
return
#e;
switch
(signo) {
#
ifdef
SIGHUP
SIGNO_CASE
(
SIGHUP
);
#
endif
#
ifdef
SIGINT
SIGNO_CASE
(
SIGINT
);
#
endif
#
ifdef
SIGQUIT
SIGNO_CASE
(
SIGQUIT
);
#
endif
#
ifdef
SIGILL
SIGNO_CASE
(
SIGILL
);
#
endif
#
ifdef
SIGTRAP
SIGNO_CASE
(
SIGTRAP
);
#
endif
#
ifdef
SIGABRT
SIGNO_CASE
(
SIGABRT
);
#
endif
#
ifdef
SIGIOT
#
if
SIGABRT != SIGIOT
SIGNO_CASE
(
SIGIOT
);
#
endif
#
endif
#
ifdef
SIGBUS
SIGNO_CASE
(
SIGBUS
);
#
endif
#
ifdef
SIGFPE
SIGNO_CASE
(
SIGFPE
);
#
endif
#
ifdef
SIGKILL
SIGNO_CASE
(
SIGKILL
);
#
endif
#
ifdef
SIGUSR1
SIGNO_CASE
(
SIGUSR1
);
#
endif
#
ifdef
SIGSEGV
SIGNO_CASE
(
SIGSEGV
);
#
endif
#
ifdef
SIGUSR2
SIGNO_CASE
(
SIGUSR2
);
#
endif
#
ifdef
SIGPIPE
SIGNO_CASE
(
SIGPIPE
);
#
endif
#
ifdef
SIGALRM
SIGNO_CASE
(
SIGALRM
);
#
endif
SIGNO_CASE
(
SIGTERM
);
SIGNO_CASE
(
SIGCHLD
);
#
ifdef
SIGSTKFLT
SIGNO_CASE
(
SIGSTKFLT
);
#
endif
#
ifdef
SIGCONT
SIGNO_CASE
(
SIGCONT
);
#
endif
#
ifdef
SIGSTOP
SIGNO_CASE
(
SIGSTOP
);
#
endif
#
ifdef
SIGTSTP
SIGNO_CASE
(
SIGTSTP
);
#
endif
#
ifdef
SIGTTIN
SIGNO_CASE
(
SIGTTIN
);
#
endif
#
ifdef
SIGTTOU
SIGNO_CASE
(
SIGTTOU
);
#
endif
#
ifdef
SIGURG
SIGNO_CASE
(
SIGURG
);
#
endif
#
ifdef
SIGXCPU
SIGNO_CASE
(
SIGXCPU
);
#
endif
#
ifdef
SIGXFSZ
SIGNO_CASE
(
SIGXFSZ
);
#
endif
#
ifdef
SIGVTALRM
SIGNO_CASE
(
SIGVTALRM
);
#
endif
#
ifdef
SIGPROF
SIGNO_CASE
(
SIGPROF
);
#
endif
#
ifdef
SIGWINCH
SIGNO_CASE
(
SIGWINCH
);
#
endif
#
ifdef
SIGIO
SIGNO_CASE
(
SIGIO
);
#
endif
#
ifdef
SIGPOLL
#
if
SIGPOLL != SIGIO
SIGNO_CASE
(
SIGPOLL
);
#
endif
#
endif
#
ifdef
SIGLOST
SIGNO_CASE
(
SIGLOST
);
#
endif
#
ifdef
SIGPWR
#
if
SIGPWR != SIGLOST
SIGNO_CASE
(
SIGPWR
);
#
endif
#
endif
#
ifdef
SIGSYS
SIGNO_CASE
(
SIGSYS
);
#
endif
default
:
return
"
"
;
}
}
Local<Value>
ErrnoException
(
int
errorno,
const
char
*syscall,
const
char
*msg,
const
char
*path) {
Local<Value> e;
Local<String> estring =
String::NewSymbol
(
errno_string
(errorno));
if
(!msg[
0
]) msg =
strerror
(errorno);
Local<String> message =
String::NewSymbol
(msg);
Local<String> cons1 =
String::Concat
(estring,
String::NewSymbol
(
"
,
"
));
Local<String> cons2 =
String::Concat
(cons1, message);
if
(errno_symbol.
IsEmpty
()) {
syscall_symbol =
NODE_PSYMBOL
(
"
syscall
"
);
errno_symbol =
NODE_PSYMBOL
(
"
errno
"
);
errpath_symbol =
NODE_PSYMBOL
(
"
path
"
);
}
if
(path) {
Local<String> cons3 =
String::Concat
(cons2,
String::NewSymbol
(
"
'
"
));
Local<String> cons4 =
String::Concat
(cons3,
String::New
(path));
Local<String> cons5 =
String::Concat
(cons4,
String::NewSymbol
(
"
'
"
));
e =
Exception::Error
(cons5);
}
else
{
e =
Exception::Error
(cons2);
}
Local<Object> obj = e->
ToObject
();
obj->
Set
(errno_symbol,
Integer::New
(errorno));
if
(path) obj->
Set
(errpath_symbol,
String::New
(path));
if
(syscall) obj->
Set
(syscall_symbol,
String::NewSymbol
(syscall));
return
e;
}
enum
encoding
ParseEncoding
(Handle<Value> encoding_v,
enum
encoding _default) {
HandleScope scope;
if
(!encoding_v->
IsString
())
return
_default;
String::Utf8Value
encoding
(encoding_v->
ToString
());
if
(
strcasecmp
(*encoding,
"
utf8
"
) ==
0
) {
return
UTF8
;
}
else
if
(
strcasecmp
(*encoding,
"
utf-8
"
) ==
0
) {
return
UTF8
;
}
else
if
(
strcasecmp
(*encoding,
"
ascii
"
) ==
0
) {
return
ASCII
;
}
else
if
(
strcasecmp
(*encoding,
"
base64
"
) ==
0
) {
return
BASE64
;
}
else
if
(
strcasecmp
(*encoding,
"
binary
"
) ==
0
) {
return
BINARY
;
}
else
if
(
strcasecmp
(*encoding,
"
raw
"
) ==
0
) {
fprintf
(stderr,
"
'raw' (array of integers) has been removed.
"
"
Use 'binary'.
\n
"
);
return
BINARY
;
}
else
if
(
strcasecmp
(*encoding,
"
raws
"
) ==
0
) {
fprintf
(stderr,
"
'raws' encoding has been renamed to 'binary'.
"
"
Please update your code.
\n
"
);
return
BINARY
;
}
else
{
return
_default;
}
}
Local<Value>
Encode
(
const
void
*buf,
size_t
len,
enum
encoding encoding) {
HandleScope scope;
if
(!len)
return
scope.
Close
(
String::Empty
());
if
(encoding ==
BINARY
) {
const
unsigned
char
*cbuf =
static_cast
<
const
unsigned
char
*>(buf);
uint16_t
* twobytebuf =
new
uint16_t
[len];
for
(
size_t
i =
0
; i < len; i++) {
//
XXX is the following line platform independent?
twobytebuf[i] = cbuf[i];
}
Local<String> chunk =
String::New
(twobytebuf, len);
delete []
twobytebuf;
//
TODO use ExternalTwoByteString?
return
scope.
Close
(chunk);
}
//
utf8 or ascii encoding
Local<String> chunk =
String::New
((
const
char
*)buf, len);
return
scope.
Close
(chunk);
}
//
Returns -1 if the handle was not valid for decoding
ssize_t
DecodeBytes
(v8::Handle<v8::Value> val,
enum
encoding encoding) {
HandleScope scope;
if
(val->
IsArray
()) {
fprintf
(stderr,
"
'raw' encoding (array of integers) has been removed.
"
"
Use 'binary'.
\n
"
);
assert
(
0
);
return
-
1
;
}
Local<String> str = val->
ToString
();
if
(encoding ==
UTF8
)
return
str->
Utf8Length
();
return
str->
Length
();
}
#
ifndef
MIN
#
define
MIN
(
a, b
) ((a) < (b) ? (a) : (b))
#
endif
//
Returns number of bytes written.
ssize_t
DecodeWrite
(
char
*buf,
size_t
buflen,
v8::Handle<v8::Value> val,
enum
encoding encoding) {
HandleScope scope;
//
XXX
//
A lot of improvement can be made here. See:
//
http://code.google.com/p/v8/issues/detail?id=270
//
http://groups.google.com/group/v8-dev/browse_thread/thread/dba28a81d9215291/ece2b50a3b4022c
//
http://groups.google.com/group/v8-users/browse_thread/thread/1f83b0ba1f0a611
if
(val->
IsArray
()) {
fprintf
(stderr,
"
'raw' encoding (array of integers) has been removed.
"
"
Use 'binary'.
\n
"
);
assert
(
0
);
return
-
1
;
}
Local<String> str = val->
ToString
();
if
(encoding ==
UTF8
) {
str->
WriteUtf8
(buf, buflen,
NULL
, String::
HINT_MANY_WRITES_EXPECTED
);
return
buflen;
}
if
(encoding ==
ASCII
) {
str->
WriteAscii
(buf,
0
, buflen, String::
HINT_MANY_WRITES_EXPECTED
);
return
buflen;
}
//
THIS IS AWFUL!!! FIXME
assert
(encoding ==
BINARY
);
uint16_t
* twobytebuf =
new
uint16_t
[buflen];
str->
Write
(twobytebuf,
0
, buflen, String::
HINT_MANY_WRITES_EXPECTED
);
for
(
size_t
i =
0
; i < buflen; i++) {
unsigned
char
*b =
reinterpret_cast
<
unsigned
char
*>(&twobytebuf[i]);
assert
(b[
1
] ==
0
);
buf[i] = b[
0
];
}
delete []
twobytebuf;
return
buflen;
}
//
Extracts a C str from a V8 Utf8Value.
const
char
*
ToCString
(
const
v8::String::Utf8Value& value) {
return
*value ? *value :
"
<str conversion failed>
"
;
}
static
void
ReportException
(TryCatch &try_catch,
bool
show_line) {
Handle<Message> message = try_catch.
Message
();
node::Stdio::DisableRawMode
(
STDIN_FILENO
);
fprintf
(stderr,
"
\n
"
);
if
(show_line && !message.
IsEmpty
()) {
//
Print (filename):(line number): (message).
String::Utf8Value
filename
(message->
GetScriptResourceName
());
const
char
* filename_string =
ToCString
(filename);
int
linenum = message->
GetLineNumber
();
fprintf
(stderr,
"
%s:%i
\n
"
, filename_string, linenum);
//
Print line of source code.
String::Utf8Value
sourceline
(message->
GetSourceLine
());
const
char
* sourceline_string =
ToCString
(sourceline);
//
HACK HACK HACK
//
//
FIXME
//
//
Because of how CommonJS modules work, all scripts are wrapped with a
//
"function (function (exports, __filename, ...) {"
//
to provide script local variables.
//
//
When reporting errors on the first line of a script, this wrapper
//
function is leaked to the user. This HACK is to remove it. The length
//
of the wrapper is 62. That wrapper is defined in src/node.js
//
//
If that wrapper is ever changed, then this number also has to be
//
updated. Or - someone could clean this up so that the two peices
//
don't need to be changed.
//
//
Even better would be to get support into V8 for wrappers that
//
shouldn't be reported to users.
int
offset = linenum ==
1
?
62
:
0
;
fprintf
(stderr,
"
%s
\n
"
, sourceline_string + offset);
//
Print wavy underline (GetUnderline is deprecated).
int
start = message->
GetStartColumn
();
for
(
int
i = offset; i < start; i++) {
fprintf
(stderr,
"
"
);
}
int
end = message->
GetEndColumn
();
for
(
int
i = start; i < end; i++) {
fprintf
(stderr,
"
^
"
);
}
fprintf
(stderr,
"
\n
"
);
}
String::Utf8Value
trace
(try_catch.
StackTrace
());
if
(trace.
length
() >
0
) {
fprintf
(stderr,
"
%s
\n
"
, *trace);
}
fflush
(stderr);
}
//
Executes a str within the current v8 context.
Local<Value>
ExecuteString
(Local<String> source, Local<Value> filename) {
HandleScope scope;
TryCatch try_catch;
Local<v8::Script> script =
v8::Script::Compile
(source, filename);
if
(script.
IsEmpty
()) {
ReportException
(try_catch,
true
);
exit
(
1
);
}
Local<Value> result = script->
Run
();
if
(result.
IsEmpty
()) {
ReportException
(try_catch,
true
);
exit
(
1
);
}
return
scope.
Close
(result);
}
static
Handle<Value>
ByteLength
(
const
Arguments& args) {
HandleScope scope;
if
(args.
Length
() <
1
|| !args[
0
]->
IsString
()) {
return
ThrowException
(
Exception::Error
(
String::New
(
"
Bad argument.
"
)));
}
Local<Integer> length =
Integer::New
(
DecodeBytes
(args[
0
],
ParseEncoding
(args[
1
],
UTF8
)));
return
scope.
Close
(length);
}
static
Handle<Value>
Loop
(
const
Arguments& args) {
HandleScope scope;
assert
(args.
Length
() ==
0
);
//
TODO Probably don't need to start this each time.
//
Avoids failing on test/simple/test-eio-race3.js though
ev_idle_start
(
EV_DEFAULT_UC_
&eio_poller);
ev_loop
(
EV_DEFAULT_UC_
0
);
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL