Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
poll.c File Reference
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <poll.h>
#include <time.h>
#include "duktape.h"

Go to the source code of this file.

Macros

#define _GNU_SOURCE
 

Functions

static int poll_poll (duk_context *ctx)
 
void poll_register (duk_context *ctx)
 

Variables

static duk_function_list_entry poll_funcs []
 
static duk_number_list_entry poll_consts []
 

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 5 of file duktape-1.8.0/examples/eventloop/poll.c.

Function Documentation

◆ poll_poll()

static int poll_poll ( duk_context * ctx)
static

Definition at line 14 of file duktape-1.8.0/examples/eventloop/poll.c.

14 {
15 int timeout = duk_to_int(ctx, 1);
16 int i, n, nchanged;
17 int fd, rc;
18 struct pollfd fds[20];
19 struct timespec ts;
20
21 memset(fds, 0, sizeof(fds));
22
23 n = 0;
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)) {
27 return -1;
28 }
29
30 /* [... enum key] */
31 duk_dup_top(ctx); /* -> [... enum key key] */
32 duk_get_prop(ctx, 0); /* -> [... enum key val] */
33 fd = duk_to_int(ctx, -2);
34
35 duk_push_string(ctx, "events");
36 duk_get_prop(ctx, -2); /* -> [... enum key val events] */
37
38 fds[n].fd = fd;
39 fds[n].events = duk_to_int(ctx, -1);
40 fds[n].revents = 0;
41
42 duk_pop_n(ctx, 3); /* -> [... enum] */
43
44 n++;
45 }
46 /* leave enum on stack */
47
48 memset(&ts, 0, sizeof(ts));
49 ts.tv_nsec = (timeout % 1000) * 1000000;
50 ts.tv_sec = timeout / 1000;
51
52 /*rc = ppoll(fds, n, &ts, NULL);*/
53 rc = poll(fds, n, timeout);
54 if (rc < 0) {
55 duk_error(ctx, DUK_ERR_ERROR, "%s (errno=%d)", strerror(errno), errno);
56 }
57
58 duk_push_array(ctx);
59 nchanged = 0;
60 for (i = 0; i < n; i++) {
61 /* update revents */
62
63 if (fds[i].revents) {
64 duk_push_int(ctx, fds[i].fd); /* -> [... retarr fd] */
65 duk_put_prop_index(ctx, -2, nchanged);
66 nchanged++;
67 }
68
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] */
74 duk_pop(ctx);
75 }
76
77 /* [retarr] */
78
79 return 1;
80}
DUK_EXTERNAL void duk_enum(duk_context *ctx, duk_idx_t obj_index, duk_uint_t enum_flags)
DUK_EXTERNAL const char * duk_push_string(duk_context *ctx, const char *str)
DUK_EXTERNAL duk_bool_t duk_put_prop_index(duk_context *ctx, duk_idx_t obj_idx, duk_uarridx_t arr_idx)
DUK_EXTERNAL duk_bool_t duk_next(duk_context *ctx, duk_idx_t enum_index, duk_bool_t get_value)
DUK_EXTERNAL void duk_push_int(duk_context *ctx, duk_int_t val)
DUK_EXTERNAL void duk_dup_top(duk_context *ctx)
DUK_EXTERNAL duk_bool_t duk_get_prop(duk_context *ctx, duk_idx_t obj_index)
DUK_EXTERNAL void duk_pop_n(duk_context *ctx, duk_idx_t count)
DUK_EXTERNAL void duk_pop(duk_context *ctx)
DUK_EXTERNAL duk_idx_t duk_push_array(duk_context *ctx)
DUK_EXTERNAL duk_int_t duk_to_int(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_bool_t duk_put_prop(duk_context *ctx, duk_idx_t obj_idx)

References duk_dup_top(), duk_enum(), DUK_ERR_ERROR, duk_error, duk_get_prop(), duk_next(), duk_pop(), duk_pop_n(), duk_push_array(), duk_push_int(), duk_push_string(), duk_put_prop(), duk_put_prop_index(), and duk_to_int().

◆ poll_register()

void poll_register ( duk_context * ctx)

Definition at line 103 of file duktape-1.8.0/examples/eventloop/poll.c.

103 {
104 /* Set global 'Poll' with functions and constants. */
106 duk_push_object(ctx);
109 duk_put_prop_string(ctx, -2, "Poll");
110 duk_pop(ctx);
111}
DUK_EXTERNAL duk_bool_t duk_put_prop_string(duk_context *ctx, duk_idx_t obj_idx, const char *key)
DUK_EXTERNAL duk_idx_t duk_push_object(duk_context *ctx)
DUK_EXTERNAL void duk_put_number_list(duk_context *ctx, duk_idx_t obj_index, const duk_number_list_entry *numbers)
DUK_EXTERNAL void duk_push_global_object(duk_context *ctx)
DUK_EXTERNAL void duk_put_function_list(duk_context *ctx, duk_idx_t obj_index, const duk_function_list_entry *funcs)
static duk_function_list_entry poll_funcs[]
static duk_number_list_entry poll_consts[]

References duk_pop(), duk_push_global_object(), duk_push_object(), duk_put_function_list(), duk_put_number_list(), duk_put_prop_string(), poll_consts, poll_consts, poll_funcs, and poll_funcs.

Referenced by main().

Variable Documentation

◆ poll_consts

duk_number_list_entry poll_consts[]
static
Initial value:
= {
{ "POLLIN", (double) POLLIN },
{ "POLLPRI", (double) POLLPRI },
{ "POLLOUT", (double) POLLOUT },
{ "POLLERR", (double) POLLERR },
{ "POLLHUP", (double) POLLHUP },
{ "POLLNVAL", (double) POLLNVAL },
{ NULL, 0.0 }
}
#define NULL
Definition gmacros.h:924

Definition at line 87 of file duktape-1.8.0/examples/eventloop/poll.c.

87 {
88 { "POLLIN", (double) POLLIN },
89 { "POLLPRI", (double) POLLPRI },
90 { "POLLOUT", (double) POLLOUT },
91#if 0
92 /* Linux 2.6.17 and upwards, requires _GNU_SOURCE etc, not added
93 * now because we don't use it.
94 */
95 { "POLLRDHUP", (double) POLLRDHUP },
96#endif
97 { "POLLERR", (double) POLLERR },
98 { "POLLHUP", (double) POLLHUP },
99 { "POLLNVAL", (double) POLLNVAL },
100 { NULL, 0.0 }
101};

Referenced by poll_register().

◆ poll_funcs

duk_function_list_entry poll_funcs[]
static
Initial value:
= {
{ "poll", poll_poll, 2 },
{ NULL, NULL, 0 }
}
static int poll_poll(duk_context *ctx)

Definition at line 82 of file duktape-1.8.0/examples/eventloop/poll.c.

82 {
83 { "poll", poll_poll, 2 },
84 { NULL, NULL, 0 }
85};

Referenced by poll_register().