]>
git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/src/third_party/duktape-1.8.0/examples/eventloop/c_eventloop.js
2 * C eventloop example (c_eventloop.c).
4 * Ecmascript code to initialize the exposed API (setTimeout() etc) when
5 * using the C eventloop.
7 * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Timers
14 function setTimeout(func
, delay
) {
19 if (typeof delay
!== 'number') {
20 throw new TypeError('delay is not a number');
23 if (typeof func
=== 'string') {
24 // Legacy case: callback is a string.
25 cb_func
= eval
.bind(this, func
);
26 } else if (typeof func
!== 'function') {
27 throw new TypeError('callback is not a function/string');
28 } else if (arguments
.length
> 2) {
29 // Special case: callback arguments are provided.
30 bind_args
= Array
.prototype.slice
.call(arguments
, 2); // [ arg1, arg2, ... ]
31 bind_args
.unshift(this); // [ global(this), arg1, arg2, ... ]
32 cb_func
= func
.bind
.apply(func
, bind_args
);
34 // Normal case: callback given as a function without arguments.
38 timer_id
= EventLoop
.createTimer(cb_func
, delay
, true /*oneshot*/);
43 function clearTimeout(timer_id
) {
44 if (typeof timer_id
!== 'number') {
45 throw new TypeError('timer ID is not a number');
47 var success
= EventLoop
.deleteTimer(timer_id
); /* retval ignored */
50 function setInterval(func
, delay
) {
55 if (typeof delay
!== 'number') {
56 throw new TypeError('delay is not a number');
59 if (typeof func
=== 'string') {
60 // Legacy case: callback is a string.
61 cb_func
= eval
.bind(this, func
);
62 } else if (typeof func
!== 'function') {
63 throw new TypeError('callback is not a function/string');
64 } else if (arguments
.length
> 2) {
65 // Special case: callback arguments are provided.
66 bind_args
= Array
.prototype.slice
.call(arguments
, 2); // [ arg1, arg2, ... ]
67 bind_args
.unshift(this); // [ global(this), arg1, arg2, ... ]
68 cb_func
= func
.bind
.apply(func
, bind_args
);
70 // Normal case: callback given as a function without arguments.
74 timer_id
= EventLoop
.createTimer(cb_func
, delay
, false /*oneshot*/);
79 function clearInterval(timer_id
) {
80 if (typeof timer_id
!== 'number') {
81 throw new TypeError('timer ID is not a number');
83 EventLoop
.deleteTimer(timer_id
);
86 function requestEventLoopExit() {
87 EventLoop
.requestExit();
93 * Ideally this would be implemented more in C than here for more speed
94 * and smaller footprint: C code would directly maintain the callback state
97 * Also for more optimal I/O, the buffer churn caused by allocating and
98 * freeing a lot of buffer values could be eliminated by reusing buffers.
99 * Socket reads would then go into a pre-allocated buffer, for instance.
102 EventLoop
.socketListening
= {};
103 EventLoop
.socketReading
= {};
104 EventLoop
.socketConnecting
= {};
106 EventLoop
.fdPollHandler = function(fd
, revents
) {
112 //print('activity on fd', fd, 'revents', revents);
114 if (revents
& Poll
.POLLIN
) {
115 cb
= this.socketReading
[fd
];
117 data
= Socket
.read(fd
); // no size control now
118 //print('READ', Duktape.enc('jx', data));
119 if (data
.length
=== 0) {
125 cb
= this.socketListening
[fd
];
127 acc_res
= Socket
.accept(fd
);
128 //print('ACCEPT:', Duktape.enc('jx', acc_res));
129 cb(acc_res
.fd
, acc_res
.addr
, acc_res
.port
);
136 if (revents
& Poll
.POLLOUT
) {
138 cb
= this.socketConnecting
[fd
];
140 delete this.socketConnecting
[fd
];
145 if ((revents
& ~(Poll
.POLLIN
| Poll
.POLLOUT
)) !== 0) {
146 //print('unexpected revents, close fd');
151 EventLoop
.server = function(address
, port
, cb_accepted
) {
152 var fd
= Socket
.createServerSocket(address
, port
);
153 this.socketListening
[fd
] = cb_accepted
;
154 this.listenFd(fd
, Poll
.POLLIN
);
157 EventLoop
.connect = function(address
, port
, cb_connected
) {
158 var fd
= Socket
.connect(address
, port
);
159 this.socketConnecting
[fd
] = cb_connected
;
160 this.listenFd(fd
, Poll
.POLLOUT
);
163 EventLoop
.close = function(fd
) {
164 EventLoop
.listenFd(fd
, 0);
165 delete this.socketListening
[fd
];
166 delete this.socketReading
[fd
];
167 delete this.socketConnecting
[fd
];
171 EventLoop
.setReader = function(fd
, cb_read
) {
172 this.socketReading
[fd
] = cb_read
;
173 this.listenFd(fd
, Poll
.POLLIN
);
176 EventLoop
.write = function(fd
, data
) {
177 // This simple example doesn't have support for write blocking / draining
178 var rc
= Socket
.write(fd
, Duktape
.Buffer(data
));