FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
node/src/node.cc at master · waveto/node · GitHub
waveto
/
node
Public
forked from
nodejs/node-v0.x-archive
Notifications
You must be signed in to change notification settings
Fork
1
Star
2
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
1675 lines (1334 loc) · 47.3 KB
Breadcrumbs
node
/
src
/
node.cc
Copy path
File metadata and controls
1675 lines (1334 loc) · 47.3 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
<
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
<
node_buffer.h
>
#
include
<
node_io_watcher.h
>
#
include
<
node_net2.h
>
#
include
<
node_events.h
>
#
include
<
node_cares.h
>
#
include
<
node_net.h
>
#
include
<
node_file.h
>
#
if
0
// not in use
# include <node_idle_watcher.h>
#
endif
#
include
<
node_http.h
>
#
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
>
#
include
<
v8-debug.h
>
using
namespace
v8
;
extern
char
**environ;
namespace
node
{
static
Persistent<Object> process;
static
Persistent<String> dev_symbol;
static
Persistent<String> ino_symbol;
static
Persistent<String> mode_symbol;
static
Persistent<String> nlink_symbol;
static
Persistent<String> uid_symbol;
static
Persistent<String> gid_symbol;
static
Persistent<String> rdev_symbol;
static
Persistent<String> size_symbol;
static
Persistent<String> blksize_symbol;
static
Persistent<String> blocks_symbol;
static
Persistent<String> atime_symbol;
static
Persistent<String> mtime_symbol;
static
Persistent<String> ctime_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;
//
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.
//
//
We try to wait for a period of GC_INTERVAL (2 seconds) of idleness, where
//
idleness means that no libev watchers have been executed. Since
//
everything in node uses libev watchers, this is a pretty good measure of
//
idleness. This is done with gc_check, which records the timestamp
//
last_active on every tick of the event loop, and with gc_timer which
//
executes every few seconds to measure if
//
last_active + GC_INTERVAL < ev_now()
//
If we do find a period of idleness, then we start the gc_idle timer which
//
will very repaidly call IdleNotification until the heap is fully
//
compacted.
static
ev_tstamp last_active;
static
ev_timer gc_timer;
static
ev_check gc_check;
static
ev_idle gc_idle;
#
define
GC_INTERVAL
1.0
static
void
gc_timer_start
() {
if
(!
ev_is_active
(&gc_timer)) {
ev_timer_start
(
EV_DEFAULT_UC_
&gc_timer);
ev_unref
(
EV_DEFAULT_UC
);
}
}
static
void
gc_timer_stop
() {
if
(
ev_is_active
(&gc_timer)) {
ev_ref
(
EV_DEFAULT_UC
);
ev_timer_stop
(
EV_DEFAULT_UC_
&gc_timer);
}
}
static
void
CheckIdleness
(
EV_P_
ev_timer *watcher,
int
revents) {
assert
(watcher == &gc_timer);
assert
(revents ==
EV_TIMER
);
//
fprintf(stderr, "check idle\n");
ev_tstamp idle_time =
ev_now
(
EV_DEFAULT_UC
) - last_active;
if
(idle_time >
GC_INTERVAL
) {
if
(!
V8::IdleNotification
()) {
ev_idle_start
(
EV_DEFAULT_UC_
&gc_idle);
}
gc_timer_stop
();
}
}
static
void
NotifyIdleness
(
EV_P_
ev_idle *watcher,
int
revents) {
assert
(watcher == &gc_idle);
assert
(revents ==
EV_IDLE
);
//
fprintf(stderr, "notify idle\n");
if
(
V8::IdleNotification
()) {
ev_idle_stop
(
EV_A_
watcher);
gc_timer_stop
();
}
}
static
void
Activity
(
EV_P_
ev_check *watcher,
int
revents) {
assert
(watcher == &gc_check);
assert
(revents ==
EV_CHECK
);
int
pending =
ev_pending_count
(
EV_DEFAULT_UC
);
//
Don't count GC watchers as activity.
if
(
ev_is_pending
(&gc_timer)) pending--;
if
(
ev_is_pending
(&gc_idle)) pending--;
if
(
ev_is_pending
(&gc_check)) pending--;
assert
(pending >=
0
);
//
fprintf(stderr, "activity, pending: %d\n", pending);
if
(pending) {
last_active =
ev_now
(
EV_DEFAULT_UC
);
ev_idle_stop
(
EV_DEFAULT_UC_
&gc_idle);
gc_timer_start
();
}
}
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);
}
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,
"
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;
}
static
Persistent<FunctionTemplate> stats_constructor_template;
Local<Object>
BuildStatsObject
(
struct
stat
* s) {
HandleScope scope;
if
(dev_symbol.
IsEmpty
()) {
dev_symbol =
NODE_PSYMBOL
(
"
dev
"
);
ino_symbol =
NODE_PSYMBOL
(
"
ino
"
);
mode_symbol =
NODE_PSYMBOL
(
"
mode
"
);
nlink_symbol =
NODE_PSYMBOL
(
"
nlink
"
);
uid_symbol =
NODE_PSYMBOL
(
"
uid
"
);
gid_symbol =
NODE_PSYMBOL
(
"
gid
"
);
rdev_symbol =
NODE_PSYMBOL
(
"
rdev
"
);
size_symbol =
NODE_PSYMBOL
(
"
size
"
);
blksize_symbol =
NODE_PSYMBOL
(
"
blksize
"
);
blocks_symbol =
NODE_PSYMBOL
(
"
blocks
"
);
atime_symbol =
NODE_PSYMBOL
(
"
atime
"
);
mtime_symbol =
NODE_PSYMBOL
(
"
mtime
"
);
ctime_symbol =
NODE_PSYMBOL
(
"
ctime
"
);
}
Local<Object> stats =
stats_constructor_template->
GetFunction
()->
NewInstance
();
/*
ID of device containing file
*/
stats->
Set
(dev_symbol,
Integer::New
(s->
st_dev
));
/*
inode number
*/
stats->
Set
(ino_symbol,
Integer::New
(s->
st_ino
));
/*
protection
*/
stats->
Set
(mode_symbol,
Integer::New
(s->
st_mode
));
/*
number of hard links
*/
stats->
Set
(nlink_symbol,
Integer::New
(s->
st_nlink
));
/*
user ID of owner
*/
stats->
Set
(uid_symbol,
Integer::New
(s->
st_uid
));
/*
group ID of owner
*/
stats->
Set
(gid_symbol,
Integer::New
(s->
st_gid
));
/*
device ID (if special file)
*/
stats->
Set
(rdev_symbol,
Integer::New
(s->
st_rdev
));
/*
total size, in bytes
*/
stats->
Set
(size_symbol,
Integer::New
(s->
st_size
));
/*
blocksize for filesystem I/O
*/
stats->
Set
(blksize_symbol,
Integer::New
(s->
st_blksize
));
/*
number of blocks allocated
*/
stats->
Set
(blocks_symbol,
Integer::New
(s->
st_blocks
));
/*
time of last access
*/
stats->
Set
(atime_symbol,
NODE_UNIXTIME_V8
(s->
st_atime
));
/*
time of last modification
*/
stats->
Set
(mtime_symbol,
NODE_UNIXTIME_V8
(s->
st_mtime
));
/*
time of last status change
*/
stats->
Set
(ctime_symbol,
NODE_UNIXTIME_V8
(s->
st_ctime
));
return
scope.
Close
(stats);
}
//
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 =
false
) {
Handle<Message> message = try_catch.
Message
();
Handle<Value> error = try_catch.
Exception
();
Handle<String> stack;
if
(error->
IsObject
()) {
Handle<Object> obj = Handle<Object>::
Cast
(error);
Handle<Value> raw_stack = obj->
Get
(
String::New
(
"
stack
"
));
if
(raw_stack->
IsString
()) stack = Handle<String>::
Cast
(raw_stack);
}
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);
fprintf
(stderr,
"
%s
\n
"
, sourceline_string);
//
Print wavy underline (GetUnderline is deprecated).
int
start = message->
GetStartColumn
();
for
(
int
i =
0
; i < start; i++) {
fprintf
(stderr,
"
"
);
}
int
end = message->
GetEndColumn
();
for
(
int
i = start; i < end; i++) {
fprintf
(stderr,
"
^
"
);
}
fprintf
(stderr,
"
\n
"
);
}
if
(stack.
IsEmpty
()) {
message->
PrintCurrentStackTrace
(stderr);
}
else
{
String::Utf8Value
trace
(stack);
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<Script> script =
Script::Compile
(source, filename);
if
(script.
IsEmpty
()) {
ReportException
(try_catch);
exit
(
1
);
}
Local<Value> result = script->
Run
();
if
(result.
IsEmpty
()) {
ReportException
(try_catch);
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/mjsunit/test-eio-race3.js though
ev_idle_start
(
EV_DEFAULT_UC_
&eio_poller);
ev_loop
(
EV_DEFAULT_UC_
0
);
return
Undefined
();
}
static
Handle<Value>
Unloop
(
const
Arguments& args) {
fprintf
(stderr,
"
Deprecation: Don't use process.unloop(). It will be removed soon.
\n
"
);
HandleScope scope;
int
how =
EVUNLOOP_ONE
;
if
(args[
0
]->
IsString
()) {
String::Utf8Value
how_s
(args[
0
]->
ToString
());
if
(
0
==
strcmp
(*how_s,
"
all
"
)) {
how =
EVUNLOOP_ALL
;
}
}
ev_unloop
(
EV_DEFAULT_
how);
return
Undefined
();
}
static
Handle<Value>
Chdir
(
const
Arguments& args) {
HandleScope scope;
if
(args.
Length
() !=
1
|| !args[
0
]->
IsString
()) {
return
ThrowException
(
Exception::Error
(
String::New
(
"
Bad argument.
"
)));
}
String::Utf8Value
path
(args[
0
]->
ToString
());
int
r =
chdir
(*path);
if
(r !=
0
) {
return
ThrowException
(
Exception::Error
(
String::New
(
strerror
(errno))));
}
return
Undefined
();
}
static
Handle<Value>
Cwd
(
const
Arguments& args) {
HandleScope scope;
assert
(args.
Length
() ==
0
);
char
output[
PATH_MAX
];
char
*r =
getcwd
(output,
PATH_MAX
);
if
(r ==
NULL
) {
return
ThrowException
(
Exception::Error
(
String::New
(
strerror
(errno))));
}
Local<String> cwd =
String::New
(output);
return
scope.
Close
(cwd);
}
static
Handle<Value>
Umask
(
const
Arguments& args){
HandleScope scope;
unsigned
int
old;
if
(args.
Length
() <
1
) {
old =
umask
(
0
);
umask
((
mode_t
)old);
}
else
if
(!args[
0
]->
IsInt32
()) {
return
ThrowException
(
Exception::TypeError
(
String::New
(
"
argument must be an integer.
"
)));
}
else
{
old =
umask
((
mode_t
)args[
0
]->
Uint32Value
());
}
return
scope.
Close
(
Uint32::New
(old));
}
static
Handle<Value>
GetUid
(
const
Arguments& args) {
HandleScope scope;
assert
(args.
Length
() ==
0
);
int
uid =
getuid
();
return
scope.
Close
(
Integer::New
(uid));
}
static
Handle<Value>
GetGid
(
const
Arguments& args) {
HandleScope scope;
assert
(args.
Length
() ==
0
);
int
gid =
getgid
();
return
scope.
Close
(
Integer::New
(gid));
}
static
Handle<Value>
SetGid
(
const
Arguments& args) {
HandleScope scope;
if
(args.
Length
() <
1
) {
return
ThrowException
(
Exception::Error
(
String::New
(
"
setgid requires 1 argument
"
)));
}
Local<Integer> given_gid = args[
0
]->
ToInteger
();
int
gid = given_gid->
Int32Value
();
int
result;
if
((result =
setgid
(gid)) !=
0
) {
return
ThrowException
(
Exception::Error
(
String::New
(
strerror
(errno))));
}
return
Undefined
();
}
static
Handle<Value>
SetUid
(
const
Arguments& args) {
HandleScope scope;
if
(args.
Length
() <
1
) {
return
ThrowException
(
Exception::Error
(
String::New
(
"
setuid requires 1 argument
"
)));
}
Local<Integer> given_uid = args[
0
]->
ToInteger
();
int
uid = given_uid->
Int32Value
();
int
result;
if
((result =
setuid
(uid)) !=
0
) {
return
ThrowException
(
Exception::Error
(
String::New
(
strerror
(errno))));
}
return
Undefined
();
}
v8::Handle<v8::Value>
Exit
(
const
v8::Arguments& args) {
HandleScope scope;
fflush
(stderr);
Stdio::Flush
();
exit
(args[
0
]->
IntegerValue
());
return
Undefined
();
}
#
ifdef
__sun
#
define
HAVE_GETMEM
1
#
include
<
unistd.h
>
/*
getpagesize()
*/
#
if
(!defined(_LP64)) && (_FILE_OFFSET_BITS - 0 == 64)
#
define
PROCFS_FILE_OFFSET_BITS_HACK
1
#
undef
_FILE_OFFSET_BITS
#
else
#
define
PROCFS_FILE_OFFSET_BITS_HACK
0
#
endif
#
include
<
procfs.h
>
#
if
(PROCFS_FILE_OFFSET_BITS_HACK - 0 == 1)
#
define
_FILE_OFFSET_BITS
64
#
endif
int
getmem
(
size_t
*rss,
size_t
*vsize) {
pid_t
pid =
getpid
();
size_t
page_size =
getpagesize
();
char
pidpath[
1024
];
sprintf
(pidpath,
"
/proc/%d/psinfo
"
, pid);
psinfo_t
psinfo;
FILE
*f =
fopen
(pidpath,
"
r
"
);
if
(!f)
return
-
1
;
if
(
fread
(&psinfo,
sizeof
(
psinfo_t
),
1
, f) !=
1
) {
fclose
(f);
return
-
1
;
}
/*
XXX correct?
*/
*vsize = (
size_t
) psinfo.
pr_size
* page_size;
*rss = (
size_t
) psinfo.
pr_rssize
*
1024
;
fclose
(f);
return
0
;
}
#
endif
#
ifdef
__FreeBSD__
#
define
HAVE_GETMEM
1
#
include
<
kvm.h
>
#
include
<
sys/param.h
>
#
include
<
sys/sysctl.h
>
#
include
<
sys/user.h
>
#
include
<
paths.h
>
#
include
<
fcntl.h
>
#
include
<
unistd.h
>
int
getmem
(
size_t
*rss,
size_t
*vsize) {
kvm_t
*kd =
NULL
;
struct
kinfo_proc
*kinfo =
NULL
;
pid_t
pid;
int
nprocs;
size_t
page_size =
getpagesize
();
pid =
getpid
();
kd =
kvm_open
(
NULL
, _PATH_DEVNULL,
NULL
,
O_RDONLY
,
"
kvm_open
"
);
if
(kd ==
NULL
)
goto
error;
kinfo =
kvm_getprocs
(kd,
KERN_PROC_PID
, pid, &nprocs);
if
(kinfo ==
NULL
)
goto
error;
*rss = kinfo->
ki_rssize
* page_size;
*vsize = kinfo->
ki_size
;
kvm_close
(kd);
return
0
;
error:
if
(kd)
kvm_close
(kd);
return
-
1
;
}
#
endif
//
__FreeBSD__
#
ifdef
__APPLE__
#
define
HAVE_GETMEM
1
/*
Researched by Tim Becker and Michael Knight
* http://blog.kuriositaet.de/?p=257
*/
#
include
<
mach/task.h
>
#
include
<
mach/mach_init.h
>
int
getmem
(
size_t
*rss,
size_t
*vsize) {
struct
task_basic_info
t_info;
mach_msg_type_number_t
t_info_count =
TASK_BASIC_INFO_COUNT
;
int
r =
task_info
(
mach_task_self
(),
TASK_BASIC_INFO
,
(
task_info_t
)&t_info,
&t_info_count);
if
(r !=
KERN_SUCCESS
)
return
-
1
;
*rss = t_info.
resident_size
;
*vsize = t_info.
virtual_size
;
return
0
;
}
#
endif
//
__APPLE__
#
ifdef
__linux__
#
define
HAVE_GETMEM
1
#
include
<
sys/param.h
>
/*
for MAXPATHLEN
*/
int
getmem
(
size_t
*rss,
size_t
*vsize) {
FILE
*f =
fopen
(
"
/proc/self/stat
"
,
"
r
"
);
if
(!f)
return
-
1
;
int
itmp;
char
ctmp;
char
buffer[
MAXPATHLEN
];
size_t
page_size =
getpagesize
();
/*
PID
*/
if
(
fscanf
(f,
"
%d
"
, &itmp) ==
0
)
goto
error;
/*
Exec file
*/
if
(
fscanf
(f,
"
%s
"
, &buffer[
0
]) ==
0
)
goto
error;
/*
State
*/
if
(
fscanf
(f,
"
%c
"
, &ctmp) ==
0
)
goto
error;
/*
Parent process
*/
if
(
fscanf
(f,
"
%d
"
, &itmp) ==
0
)
goto
error;
/*
Process group
*/
if
(
fscanf
(f,
"
%d
"
, &itmp) ==
0
)
goto
error;
/*
Session id
*/
if
(
fscanf
(f,
"
%d
"
, &itmp) ==
0
)
goto
error;
/*
TTY
*/
if
(
fscanf
(f,
"
%d
"
, &itmp) ==
0
)
goto
error;
/*
TTY owner process group
*/
if
(
fscanf
(f,
"
%d
"
, &itmp) ==
0
)
goto
error;
/*
Flags
*/
if
(
fscanf
(f,
"
%u
"
, &itmp) ==
0
)
goto
error;
/*
Minor faults (no memory page)
*/
if
(
fscanf
(f,
"
%u
"
, &itmp) ==
0
)
goto
error;
/*
Minor faults, children
*/
if
(
fscanf
(f,
"
%u
"
, &itmp) ==
0
)
goto
error;
/*
Major faults (memory page faults)
*/
if
(
fscanf
(f,
"
%u
"
, &itmp) ==
0
)
goto
error;
/*
Major faults, children
*/
if
(
fscanf
(f,
"
%u
"
, &itmp) ==
0
)
goto
error;
/*
utime
*/
if
(
fscanf
(f,
"
%d
"
, &itmp) ==
0
)
goto
error;
/*
stime
*/
if
(
fscanf
(f,
"
%d
"
, &itmp) ==
0
)
goto
error;
/*
utime, children
*/
if
(
fscanf
(f,
"
%d
"
, &itmp) ==
0
)
goto
error;
/*
stime, children
*/
if
(
fscanf
(f,
"
%d
"
, &itmp) ==
0
)
goto
error;
/*
jiffies remaining in current time slice
*/
if
(
fscanf
(f,
"
%d
"
, &itmp) ==
0
)
goto
error;
/*
'nice' value
*/
if
(
fscanf
(f,
"
%d
"
, &itmp) ==
0
)
goto
error;
/*
jiffies until next timeout
*/
if
(
fscanf
(f,
"
%u
"
, &itmp) ==
0
)
goto
error;
/*
jiffies until next SIGALRM
*/
if
(
fscanf
(f,
"
%u
"
, &itmp) ==
0
)
goto
error;
/*
start time (jiffies since system boot)
*/
if
(
fscanf
(f,
"
%d
"
, &itmp) ==
0
)
goto
error;
/*
Virtual memory size
*/
if
(
fscanf
(f,
"
%u
"
, &itmp) ==
0
)
goto
error;
*vsize = (
size_t
) itmp;
/*
Resident set size
*/
if
(
fscanf
(f,
"
%u
"
, &itmp) ==
0
)
goto
error;
*rss = (
size_t
) itmp * page_size;
/*
rlim
*/
if
(
fscanf
(f,
"
%u
"
, &itmp) ==
0
)
goto
error;
/*
Start of text
*/
if
(
fscanf
(f,
"
%u
"
, &itmp) ==
0
)
goto
error;
/*
End of text
*/
if
(
fscanf
(f,
"
%u
"
, &itmp) ==
0
)
goto
error;
/*
Start of stack
*/
if
(
fscanf
(f,
"
%u
"
, &itmp) ==
0
)
goto
error;
fclose
(f);
return
0
;
error:
fclose
(f);
return
-
1
;
}
#
endif
//
__linux__
v8::Handle<v8::Value>
MemoryUsage
(
const
v8::Arguments& args) {
HandleScope scope;
assert
(args.
Length
() ==
0
);
#
ifndef
HAVE_GETMEM
return
ThrowException
(
Exception::Error
(
String::New
(
"
Not support on your platform. (Talk to Ryan.)
"
)));
#
else
size_t
rss, vsize;
int
r =
getmem
(&rss, &vsize);
if
(r !=
0
) {
return
ThrowException
(
Exception::Error
(
String::New
(
strerror
(errno))));
}
Local<Object> info =
Object::New
();
if
(rss_symbol.
IsEmpty
()) {
rss_symbol =
NODE_PSYMBOL
(
"
rss
"
);
vsize_symbol =
NODE_PSYMBOL
(
"
vsize
"
);
heap_total_symbol =
NODE_PSYMBOL
(
"
heapTotal
"
);
heap_used_symbol =
NODE_PSYMBOL
(
"
heapUsed
"
);
}
info->
Set
(rss_symbol,
Integer::NewFromUnsigned
(rss));
info->
Set
(vsize_symbol,
Integer::NewFromUnsigned
(vsize));
//
V8 memory usage
HeapStatistics v8_heap_stats;
V8::GetHeapStatistics
(&v8_heap_stats);
info->
Set
(heap_total_symbol,
Integer::NewFromUnsigned
(v8_heap_stats.
total_heap_size
()));
info->
Set
(heap_used_symbol,
Integer::NewFromUnsigned
(v8_heap_stats.
used_heap_size
()));
return
scope.
Close
(info);
#
endif
}
v8::Handle<v8::Value>
Kill
(
const
v8::Arguments& args) {
HandleScope scope;
if
(args.
Length
() <
1
|| !args[
0
]->
IsNumber
()) {
return
ThrowException
(
Exception::Error
(
String::New
(
"
Bad argument.
"
)));
}
pid_t
pid = args[
0
]->
IntegerValue
();
int
sig =
SIGTERM
;
if
(args.
Length
() >=
2
) {
if
(args[
1
]->
IsNumber
()) {
sig = args[
1
]->
Int32Value
();
}
else
if
(args[
1
]->
IsString
()) {
Local<String> signame = args[
1
]->
ToString
();
Local<Value> sig_v = process->
Get
(signame);
if
(!sig_v->
IsNumber
()) {
return
ThrowException
(
Exception::Error
(
String::New
(
"
Unknown signal
"
)));
}
sig = sig_v->
Int32Value
();
}
}
int
r =
kill
(pid, sig);
if
(r !=
0
) {
return
ThrowException
(
Exception::Error
(
String::New
(
strerror
(errno))));
}
return
Undefined
();
}
typedef
void
(*extInit)(Handle<Object> exports);
//
DLOpen is node.dlopen(). Used to load 'module.node' dynamically shared
//
objects.
Handle<Value>
DLOpen
(
const
v8::Arguments& args) {
HandleScope scope;
if
(args.
Length
() <
2
)
return
Undefined
();
String::Utf8Value
filename
(args[
0
]->
ToString
());
//
Cast
Local<Object> target = args[
1
]->
ToObject
();
//
Cast
//
Actually call dlopen().
//
FIXME: This is a blocking function and should be called asynchronously!
//
This function should be moved to file.cc and use libeio to make this
//
system call.
void
*handle =
dlopen
(*filename,
RTLD_LAZY
);
//
Handle errors.
if
(handle ==
NULL
) {
Local<Value> exception =
Exception::Error
(
String::New
(
dlerror
()));
return
ThrowException
(exception);
}
//
Get the init() function from the dynamically shared object.
void
*init_handle =
dlsym
(handle,
"
init
"
);
//
Error out if not found.
if
(init_handle ==
NULL
) {
Local<Value> exception =
Exception::Error
(
String::New
(
"
No 'init' symbol found in module.
"
));
return
ThrowException
(exception);
}
extInit init = (extInit)(init_handle);
//
Cast
//
Execute the C++ module
init
(target);
return
Undefined
();
}
//
evalcx(code, sandbox={})
//
Executes code in a new context
Handle<Value>
EvalCX
(
const
Arguments& args) {
HandleScope scope;
Local<String> code = args[
0
]->
ToString
();
Local<Object> sandbox = args.
Length
() >
1
? args[
1
]->
ToObject
()
:
Object::New
();
Local<String> filename = args.
Length
() >
2
? args[
2
]->
ToString
()
:
String::New
(
"
evalcx
"
);
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL