2 * C wrapper for poll().
14 static int poll_poll(duk_context
*ctx
) {
15 int timeout
= duk_to_int(ctx
, 1);
18 struct pollfd fds
[20];
21 memset(fds
, 0, sizeof(fds
));
24 duk_enum(ctx
, 0, 0 /*enum_flags*/);
25 while (duk_next(ctx
, -1, 0)) {
26 if ((size_t) n
>= sizeof(fds
) / sizeof(struct pollfd
)) {
31 duk_dup_top(ctx
); /* -> [... enum key key] */
32 duk_get_prop(ctx
, 0); /* -> [... enum key val] */
33 fd
= duk_to_int(ctx
, -2);
35 duk_push_string(ctx
, "events");
36 duk_get_prop(ctx
, -2); /* -> [... enum key val events] */
39 fds
[n
].events
= duk_to_int(ctx
, -1);
42 duk_pop_n(ctx
, 3); /* -> [... enum] */
46 /* leave enum on stack */
48 memset(&ts
, 0, sizeof(ts
));
49 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
50 ts
.tv_sec
= timeout
/ 1000;
52 /*rc = ppoll(fds, n, &ts, NULL);*/
53 rc
= poll(fds
, n
, timeout
);
55 duk_error(ctx
, DUK_ERR_ERROR
, "%s (errno=%d)", strerror(errno
), errno
);
60 for (i
= 0; i
< n
; i
++) {
64 duk_push_int(ctx
, fds
[i
].fd
); /* -> [... retarr fd] */
65 duk_put_prop_index(ctx
, -2, nchanged
);
69 duk_push_int(ctx
, fds
[i
].fd
); /* -> [... retarr key] */
70 duk_get_prop(ctx
, 0); /* -> [... retarr val] */
71 duk_push_string(ctx
, "revents");
72 duk_push_int(ctx
, fds
[i
].revents
); /* -> [... retarr val "revents" fds[i].revents] */
73 duk_put_prop(ctx
, -3); /* -> [... retarr val] */
82 static duk_function_list_entry poll_funcs
[] = {
83 { "poll", poll_poll
, 2 },
87 static duk_number_list_entry poll_consts
[] = {
88 { "POLLIN", (double) POLLIN
},
89 { "POLLPRI", (double) POLLPRI
},
90 { "POLLOUT", (double) POLLOUT
},
92 /* Linux 2.6.17 and upwards, requires _GNU_SOURCE etc, not added
93 * now because we don't use it.
95 { "POLLRDHUP", (double) POLLRDHUP
},
97 { "POLLERR", (double) POLLERR
},
98 { "POLLHUP", (double) POLLHUP
},
99 { "POLLNVAL", (double) POLLNVAL
},
103 void poll_register(duk_context
*ctx
) {
104 /* Set global 'Poll' with functions and constants. */
105 duk_push_global_object(ctx
);
106 duk_push_object(ctx
);
107 duk_put_function_list(ctx
, -1, poll_funcs
);
108 duk_put_number_list(ctx
, -1, poll_consts
);
109 duk_put_prop_string(ctx
, -2, "Poll");