-
Notifications
You must be signed in to change notification settings - Fork 514
/
main.c
286 lines (240 loc) · 6.2 KB
/
main.c
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
/*
* Main for evloop command line tool.
*
* Runs a given script from file or stdin inside an eventloop. The
* script can then access setTimeout() etc.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if !defined(NO_SIGNAL)
#include <signal.h>
#endif
#include "duktape.h"
extern void poll_register(duk_context *ctx);
extern void socket_register(duk_context *ctx);
extern void fileio_register(duk_context *ctx);
extern void fileio_push_file_buffer(duk_context *ctx, const char *filename);
extern void fileio_push_file_string(duk_context *ctx, const char *filename);
extern void eventloop_register(duk_context *ctx);
extern duk_ret_t eventloop_run(duk_context *ctx, void *udata);
static int c_evloop = 0;
#if !defined(NO_SIGNAL)
static void my_sighandler(int x) {
fprintf(stderr, "Got signal %d\n", x);
fflush(stderr);
}
static void set_sigint_handler(void) {
(void) signal(SIGINT, my_sighandler);
}
#endif /* NO_SIGNAL */
/* Print error to stderr and pop error. */
static void print_error(duk_context *ctx, FILE *f) {
if (duk_is_object(ctx, -1) && duk_has_prop_string(ctx, -1, "stack")) {
/* XXX: print error objects specially */
/* XXX: pcall the string coercion */
duk_get_prop_string(ctx, -1, "stack");
if (duk_is_string(ctx, -1)) {
fprintf(f, "%s\n", duk_get_string(ctx, -1));
fflush(f);
duk_pop_2(ctx);
return;
} else {
duk_pop(ctx);
}
}
duk_to_string(ctx, -1);
fprintf(f, "%s\n", duk_get_string(ctx, -1));
fflush(f);
duk_pop(ctx);
}
static duk_ret_t native_print(duk_context *ctx) {
duk_push_string(ctx, " ");
duk_insert(ctx, 0);
duk_join(ctx, duk_get_top(ctx) - 1);
printf("%s\n", duk_safe_to_string(ctx, -1));
return 0;
}
static void print_register(duk_context *ctx) {
duk_push_c_function(ctx, native_print, DUK_VARARGS);
duk_put_global_string(ctx, "print");
}
static duk_ret_t wrapped_compile_execute(duk_context *ctx, void *udata) {
int comp_flags = 0;
int rc;
(void) udata;
/* Compile input and place it into global _USERCODE */
duk_compile(ctx, comp_flags);
duk_push_global_object(ctx);
duk_insert(ctx, -2); /* [ ... global func ] */
duk_put_prop_string(ctx, -2, "_USERCODE");
duk_pop(ctx);
#if 0
printf("compiled usercode\n");
#endif
/* Start a zero timer which will call _USERCODE from within
* the event loop.
*/
fprintf(stderr, "set _USERCODE timer\n");
fflush(stderr);
duk_eval_string(ctx, "setTimeout(function() { _USERCODE(); }, 0);");
duk_pop(ctx);
/* Finally, launch eventloop. This call only returns after the
* eventloop terminates.
*/
if (c_evloop) {
fprintf(stderr, "calling eventloop_run()\n");
fflush(stderr);
rc = duk_safe_call(ctx, eventloop_run, NULL, 0 /*nargs*/, 1 /*nrets*/);
if (rc != 0) {
fprintf(stderr, "eventloop_run() failed: %s\n", duk_to_string(ctx, -1));
fflush(stderr);
}
duk_pop(ctx);
} else {
fprintf(stderr, "calling EventLoop.run()\n");
fflush(stderr);
duk_eval_string(ctx, "EventLoop.run();");
duk_pop(ctx);
}
return 0;
}
static int handle_fh(duk_context *ctx, FILE *f, const char *filename) {
char *buf = NULL;
int len;
int got;
int rc;
int retval = -1;
if (fseek(f, 0, SEEK_END) < 0) {
goto error;
}
len = (int) ftell(f);
if (fseek(f, 0, SEEK_SET) < 0) {
goto error;
}
buf = (char *) malloc(len);
if (!buf) {
goto error;
}
got = fread((void *) buf, (size_t) 1, (size_t) len, f);
duk_push_lstring(ctx, buf, got);
duk_push_string(ctx, filename);
free(buf);
buf = NULL;
rc = duk_safe_call(ctx, wrapped_compile_execute, NULL, 2 /*nargs*/, 1 /*nret*/);
if (rc != DUK_EXEC_SUCCESS) {
print_error(ctx, stderr);
goto error;
} else {
duk_pop(ctx);
retval = 0;
}
/* fall thru */
error:
if (buf) {
free(buf);
}
return retval;
}
static int handle_file(duk_context *ctx, const char *filename) {
FILE *f = NULL;
int retval;
f = fopen(filename, "rb");
if (!f) {
fprintf(stderr, "failed to open source file: %s\n", filename);
fflush(stderr);
goto error;
}
retval = handle_fh(ctx, f, filename);
fclose(f);
return retval;
error:
return -1;
}
static int handle_stdin(duk_context *ctx) {
int retval;
retval = handle_fh(ctx, stdin, "stdin");
return retval;
}
static duk_ret_t init_duktape_context(duk_context *ctx, void *udata) {
(void) udata;
print_register(ctx);
poll_register(ctx);
socket_register(ctx);
fileio_register(ctx);
if (c_evloop) {
fprintf(stderr, "Using C based eventloop (omit -c to use ECMAScript based eventloop)\n");
fflush(stderr);
eventloop_register(ctx);
fileio_push_file_string(ctx, "c_eventloop.js");
duk_eval(ctx);
} else {
fprintf(stderr, "Using ECMAScript based eventloop (give -c to use C based eventloop)\n");
fflush(stderr);
fileio_push_file_string(ctx, "ecma_eventloop.js");
duk_eval(ctx);
}
return 0;
}
int main(int argc, char *argv[]) {
duk_context *ctx = NULL;
int retval = 1;
const char *filename = NULL;
int i;
#if !defined(NO_SIGNAL)
set_sigint_handler();
/* This is useful at the global level; libraries should avoid SIGPIPE though */
/*signal(SIGPIPE, SIG_IGN);*/
#endif
for (i = 1; i < argc; i++) {
char *arg = argv[i];
if (!arg) {
goto usage;
}
if (strcmp(arg, "-c") == 0) {
c_evloop = 1;
} else if (strlen(arg) > 1 && arg[0] == '-') {
goto usage;
} else {
if (filename) {
goto usage;
}
filename = arg;
}
}
if (!filename) {
goto usage;
}
ctx = duk_create_heap_default();
if (duk_safe_call(ctx, init_duktape_context, NULL, 0 /*nargs*/, 1 /*nrets*/) != 0) {
fprintf(stderr, "Failed to initialize: %s\n", duk_safe_to_string(ctx, -1));
goto cleanup;
}
duk_pop(ctx);
fprintf(stderr, "Executing code from: '%s'\n", filename);
fflush(stderr);
if (strcmp(filename, "-") == 0) {
if (handle_stdin(ctx) != 0) {
goto cleanup;
}
} else {
if (handle_file(ctx, filename) != 0) {
goto cleanup;
}
}
retval = 0;
/* fall through */
cleanup:
if (ctx) {
duk_destroy_heap(ctx);
}
return retval;
usage:
fprintf(stderr, "Usage: evloop [-c] <filename>\n");
fprintf(stderr, "\n");
fprintf(stderr, "Uses an ECMAScript based eventloop (ecma_eventloop.js) by default.\n");
fprintf(stderr, "If -c option given, uses a C based eventloop (c_eventloop.{c,js}).\n");
fprintf(stderr, "If <filename> is '-', the entire STDIN executed.\n");
fflush(stderr);
exit(1);
}