forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc_eventloop.js
More file actions
179 lines (153 loc) · 5.03 KB
/
Copy pathc_eventloop.js
File metadata and controls
179 lines (153 loc) · 5.03 KB
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
/*
* C eventloop example (c_eventloop.c).
*
* Ecmascript code to initialize the exposed API (setTimeout() etc) when
* using the C eventloop.
*
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Timers
*/
/*
* Timer API
*/
function setTimeout(func, delay) {
var cb_func;
var bind_args;
var timer_id;
if (typeof delay !== 'number') {
throw new TypeError('delay is not a number');
}
if (typeof func === 'string') {
// Legacy case: callback is a string.
cb_func = eval.bind(this, func);
} else if (typeof func !== 'function') {
throw new TypeError('callback is not a function/string');
} else if (arguments.length > 2) {
// Special case: callback arguments are provided.
bind_args = Array.prototype.slice.call(arguments, 2); // [ arg1, arg2, ... ]
bind_args.unshift(this); // [ global(this), arg1, arg2, ... ]
cb_func = func.bind.apply(func, bind_args);
} else {
// Normal case: callback given as a function without arguments.
cb_func = func;
}
timer_id = EventLoop.createTimer(cb_func, delay, true /*oneshot*/);
return timer_id;
}
function clearTimeout(timer_id) {
if (typeof timer_id !== 'number') {
throw new TypeError('timer ID is not a number');
}
var success = EventLoop.deleteTimer(timer_id); /* retval ignored */
}
function setInterval(func, delay) {
var cb_func;
var bind_args;
var timer_id;
if (typeof delay !== 'number') {
throw new TypeError('delay is not a number');
}
if (typeof func === 'string') {
// Legacy case: callback is a string.
cb_func = eval.bind(this, func);
} else if (typeof func !== 'function') {
throw new TypeError('callback is not a function/string');
} else if (arguments.length > 2) {
// Special case: callback arguments are provided.
bind_args = Array.prototype.slice.call(arguments, 2); // [ arg1, arg2, ... ]
bind_args.unshift(this); // [ global(this), arg1, arg2, ... ]
cb_func = func.bind.apply(func, bind_args);
} else {
// Normal case: callback given as a function without arguments.
cb_func = func;
}
timer_id = EventLoop.createTimer(cb_func, delay, false /*oneshot*/);
return timer_id;
}
function clearInterval(timer_id) {
if (typeof timer_id !== 'number') {
throw new TypeError('timer ID is not a number');
}
EventLoop.deleteTimer(timer_id);
}
function requestEventLoopExit() {
EventLoop.requestExit();
}
/*
* Socket handling
*
* Ideally this would be implemented more in C than here for more speed
* and smaller footprint: C code would directly maintain the callback state
* and such.
*
* Also for more optimal I/O, the buffer churn caused by allocating and
* freeing a lot of buffer values could be eliminated by reusing buffers.
* Socket reads would then go into a pre-allocated buffer, for instance.
*/
EventLoop.socketListening = {};
EventLoop.socketReading = {};
EventLoop.socketConnecting = {};
EventLoop.fdPollHandler = function(fd, revents) {
var data;
var cb;
var rc;
var acc_res;
//print('activity on fd', fd, 'revents', revents);
if (revents & Poll.POLLIN) {
cb = this.socketReading[fd];
if (cb) {
data = Socket.read(fd); // no size control now
//print('READ', Duktape.enc('jx', data));
if (data.length === 0) {
this.close(fd);
return;
}
cb(fd, data);
} else {
cb = this.socketListening[fd];
if (cb) {
acc_res = Socket.accept(fd);
//print('ACCEPT:', Duktape.enc('jx', acc_res));
cb(acc_res.fd, acc_res.addr, acc_res.port);
} else {
//print('UNKNOWN');
}
}
}
if (revents & Poll.POLLOUT) {
// Connected
cb = this.socketConnecting[fd];
if (cb) {
delete this.socketConnecting[fd];
cb(fd);
}
}
if ((revents & ~(Poll.POLLIN | Poll.POLLOUT)) !== 0) {
//print('unexpected revents, close fd');
this.close(fd);
}
}
EventLoop.server = function(address, port, cb_accepted) {
var fd = Socket.createServerSocket(address, port);
this.socketListening[fd] = cb_accepted;
this.listenFd(fd, Poll.POLLIN);
}
EventLoop.connect = function(address, port, cb_connected) {
var fd = Socket.connect(address, port);
this.socketConnecting[fd] = cb_connected;
this.listenFd(fd, Poll.POLLOUT);
}
EventLoop.close = function(fd) {
EventLoop.listenFd(fd, 0);
delete this.socketListening[fd];
delete this.socketReading[fd];
delete this.socketConnecting[fd];
Socket.close(fd);
}
EventLoop.setReader = function(fd, cb_read) {
this.socketReading[fd] = cb_read;
this.listenFd(fd, Poll.POLLIN);
}
EventLoop.write = function(fd, data) {
// This simple example doesn't have support for write blocking / draining
var rc = Socket.write(fd, Duktape.Buffer(data));
}