-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathmqjs.c
More file actions
774 lines (704 loc) · 22 KB
/
mqjs.c
File metadata and controls
774 lines (704 loc) · 22 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
/*
* Micro QuickJS REPL
*
* Copyright (c) 2017-2025 Fabrice Bellard
* Copyright (c) 2017-2025 Charlie Gordon
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <inttypes.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <time.h>
#include <sys/time.h>
#include <math.h>
#include <fcntl.h>
#include "cutils.h"
#include "readline_tty.h"
#include "mquickjs.h"
static uint8_t *load_file(const char *filename, int *plen);
static void dump_error(JSContext *ctx);
static JSValue js_print(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv)
{
int i;
JSValue v;
for(i = 0; i < argc; i++) {
if (i != 0)
putchar(' ');
v = argv[i];
if (JS_IsString(ctx, v)) {
JSCStringBuf buf;
const char *str;
size_t len;
str = JS_ToCStringLen(ctx, &len, v, &buf);
fwrite(str, 1, len, stdout);
} else {
JS_PrintValueF(ctx, argv[i], JS_DUMP_LONG);
}
}
putchar('\n');
return JS_UNDEFINED;
}
static JSValue js_gc(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv)
{
JS_GC(ctx);
return JS_UNDEFINED;
}
#if defined(__linux__) || defined(__APPLE__)
static int64_t get_time_ms(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000 + (ts.tv_nsec / 1000000);
}
#else
static int64_t get_time_ms(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (int64_t)tv.tv_sec * 1000 + (tv.tv_usec / 1000);
}
#endif
static JSValue js_date_now(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return JS_NewInt64(ctx, (int64_t)tv.tv_sec * 1000 + (tv.tv_usec / 1000));
}
static JSValue js_performance_now(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv)
{
return JS_NewInt64(ctx, get_time_ms());
}
/* load a script */
static JSValue js_load(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv)
{
const char *filename;
JSCStringBuf buf_str;
uint8_t *buf;
int buf_len;
JSValue ret;
filename = JS_ToCString(ctx, argv[0], &buf_str);
if (!filename)
return JS_EXCEPTION;
buf = load_file(filename, &buf_len);
ret = JS_Eval(ctx, (const char *)buf, buf_len, filename, 0);
free(buf);
return ret;
}
/* timers */
typedef struct {
BOOL allocated;
JSGCRef func;
int64_t timeout; /* in ms */
} JSTimer;
#define MAX_TIMERS 16
static JSTimer js_timer_list[MAX_TIMERS];
static JSValue js_setTimeout(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv)
{
JSTimer *th;
int delay, i;
JSValue *pfunc;
if (!JS_IsFunction(ctx, argv[0]))
return JS_ThrowTypeError(ctx, "not a function");
if (JS_ToInt32(ctx, &delay, argv[1]))
return JS_EXCEPTION;
for(i = 0; i < MAX_TIMERS; i++) {
th = &js_timer_list[i];
if (!th->allocated) {
pfunc = JS_AddGCRef(ctx, &th->func);
*pfunc = argv[0];
th->timeout = get_time_ms() + delay;
th->allocated = TRUE;
return JS_NewInt32(ctx, i);
}
}
return JS_ThrowInternalError(ctx, "too many timers");
}
static JSValue js_clearTimeout(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv)
{
int timer_id;
JSTimer *th;
if (JS_ToInt32(ctx, &timer_id, argv[0]))
return JS_EXCEPTION;
if (timer_id >= 0 && timer_id < MAX_TIMERS) {
th = &js_timer_list[timer_id];
if (th->allocated) {
JS_DeleteGCRef(ctx, &th->func);
th->allocated = FALSE;
}
}
return JS_UNDEFINED;
}
static void run_timers(JSContext *ctx)
{
int64_t min_delay, delay, cur_time;
BOOL has_timer;
int i;
JSTimer *th;
struct timespec ts;
for(;;) {
min_delay = 1000;
cur_time = get_time_ms();
has_timer = FALSE;
for(i = 0; i < MAX_TIMERS; i++) {
th = &js_timer_list[i];
if (th->allocated) {
has_timer = TRUE;
delay = th->timeout - cur_time;
if (delay <= 0) {
JSValue ret;
/* the timer expired */
if (JS_StackCheck(ctx, 2))
goto fail;
JS_PushArg(ctx, th->func.val); /* func name */
JS_PushArg(ctx, JS_NULL); /* this */
JS_DeleteGCRef(ctx, &th->func);
th->allocated = FALSE;
ret = JS_Call(ctx, 0);
if (JS_IsException(ret)) {
fail:
dump_error(ctx);
exit(1);
}
min_delay = 0;
break;
} else if (delay < min_delay) {
min_delay = delay;
}
}
}
if (!has_timer)
break;
if (min_delay > 0) {
ts.tv_sec = min_delay / 1000;
ts.tv_nsec = (min_delay % 1000) * 1000000;
nanosleep(&ts, NULL);
}
}
}
#include "mqjs_stdlib.h"
#define STYLE_DEFAULT COLOR_BRIGHT_GREEN
#define STYLE_COMMENT COLOR_WHITE
#define STYLE_STRING COLOR_BRIGHT_CYAN
#define STYLE_REGEX COLOR_CYAN
#define STYLE_NUMBER COLOR_GREEN
#define STYLE_KEYWORD COLOR_BRIGHT_WHITE
#define STYLE_FUNCTION COLOR_BRIGHT_YELLOW
#define STYLE_TYPE COLOR_BRIGHT_MAGENTA
#define STYLE_IDENTIFIER COLOR_BRIGHT_GREEN
#define STYLE_ERROR COLOR_RED
#define STYLE_RESULT COLOR_BRIGHT_WHITE
#define STYLE_ERROR_MSG COLOR_BRIGHT_RED
static uint8_t *load_file(const char *filename, int *plen)
{
FILE *f;
uint8_t *buf;
int buf_len;
f = fopen(filename, "rb");
if (!f) {
perror(filename);
exit(1);
}
fseek(f, 0, SEEK_END);
buf_len = ftell(f);
fseek(f, 0, SEEK_SET);
buf = malloc(buf_len + 1);
fread(buf, 1, buf_len, f);
buf[buf_len] = '\0';
fclose(f);
if (plen)
*plen = buf_len;
return buf;
}
static int js_log_err_flag;
static void js_log_func(void *opaque, const void *buf, size_t buf_len)
{
fwrite(buf, 1, buf_len, js_log_err_flag ? stderr : stdout);
}
static void dump_error(JSContext *ctx)
{
JSValue obj;
obj = JS_GetException(ctx);
fprintf(stderr, "%s", term_colors[STYLE_ERROR_MSG]);
js_log_err_flag++;
JS_PrintValueF(ctx, obj, JS_DUMP_LONG);
js_log_err_flag--;
fprintf(stderr, "%s\n", term_colors[COLOR_NONE]);
}
static int eval_buf(JSContext *ctx, const char *eval_str, const char *filename, BOOL is_repl, int parse_flags)
{
JSValue val;
int flags;
flags = parse_flags;
if (is_repl)
flags |= JS_EVAL_RETVAL | JS_EVAL_REPL;
val = JS_Parse(ctx, eval_str, strlen(eval_str), filename, flags);
if (JS_IsException(val))
goto exception;
val = JS_Run(ctx, val);
if (JS_IsException(val)) {
exception:
dump_error(ctx);
return 1;
} else {
if (is_repl) {
printf("%s", term_colors[STYLE_RESULT]);
JS_PrintValueF(ctx, val, JS_DUMP_LONG);
printf("%s\n", term_colors[COLOR_NONE]);
}
return 0;
}
}
static int eval_file(JSContext *ctx, const char *filename,
int argc, const char **argv, int parse_flags,
BOOL allow_bytecode)
{
uint8_t *buf;
int ret, buf_len;
JSValue val;
buf = load_file(filename, &buf_len);
if (allow_bytecode && JS_IsBytecode(buf, buf_len)) {
if (JS_RelocateBytecode(ctx, buf, buf_len)) {
fprintf(stderr, "Could not relocate bytecode\n");
exit(1);
}
val = JS_LoadBytecode(ctx, buf);
} else {
val = JS_Parse(ctx, (char *)buf, buf_len, filename, parse_flags);
}
if (JS_IsException(val))
goto exception;
if (argc > 0) {
JSValue obj, arr;
JSGCRef arr_ref, val_ref;
int i;
JS_PUSH_VALUE(ctx, val);
/* must be defined after JS_LoadBytecode() */
arr = JS_NewArray(ctx, argc);
JS_PUSH_VALUE(ctx, arr);
for(i = 0; i < argc; i++) {
JS_SetPropertyUint32(ctx, arr_ref.val, i,
JS_NewString(ctx, argv[i]));
}
JS_POP_VALUE(ctx, arr);
obj = JS_GetGlobalObject(ctx);
JS_SetPropertyStr(ctx, obj, "scriptArgs", arr);
JS_POP_VALUE(ctx, val);
}
val = JS_Run(ctx, val);
if (JS_IsException(val)) {
exception:
dump_error(ctx);
ret = 1;
} else {
ret = 0;
}
free(buf);
return ret;
}
static void compile_file(const char *filename, const char *outfilename,
size_t mem_size, int dump_memory, int parse_flags, BOOL force_32bit)
{
uint8_t *mem_buf;
JSContext *ctx;
char *eval_str;
JSValue val;
union {
JSBytecodeHeader hdr;
#if JSW == 8
JSBytecodeHeader32 hdr32;
#endif
} hdr_buf;
int hdr_len;
const uint8_t *data_buf;
uint32_t data_len;
FILE *f;
/* When compiling to a file, the actual content of the stdlib does
not matter because the generated bytecode does not depend on
it. We still need it so that the atoms for the parsing are
defined. The JSContext must be discarded once the compilation
is done. */
mem_buf = malloc(mem_size);
ctx = JS_NewContext2(mem_buf, mem_size, &js_stdlib, TRUE);
JS_SetLogFunc(ctx, js_log_func);
eval_str = (char *)load_file(filename, NULL);
val = JS_Parse(ctx, eval_str, strlen(eval_str), filename, parse_flags);
free(eval_str);
if (JS_IsException(val)) {
dump_error(ctx);
return;
}
#if JSW == 8
if (force_32bit) {
if (JS_PrepareBytecode64to32(ctx, &hdr_buf.hdr32, &data_buf, &data_len, val)) {
fprintf(stderr, "Could not convert the bytecode from 64 to 32 bits\n");
exit(1);
}
hdr_len = sizeof(JSBytecodeHeader32);
} else
#endif
{
JS_PrepareBytecode(ctx, &hdr_buf.hdr, &data_buf, &data_len, val);
if (dump_memory)
JS_DumpMemory(ctx, (dump_memory >= 2));
/* Relocate to zero to have a deterministic
output. JS_DumpMemory() cannot work once the heap is relocated,
so we relocate after it. */
JS_RelocateBytecode2(ctx, &hdr_buf.hdr, (uint8_t *)data_buf, data_len, 0, FALSE);
hdr_len = sizeof(JSBytecodeHeader);
}
f = fopen(outfilename, "wb");
if (!f) {
perror(outfilename);
exit(1);
}
fwrite(&hdr_buf, 1, hdr_len, f);
fwrite(data_buf, 1, data_len, f);
fclose(f);
JS_FreeContext(ctx);
free(mem_buf);
}
/* repl */
static ReadlineState readline_state;
static uint8_t readline_cmd_buf[256];
static uint8_t readline_kill_buf[256];
static char readline_history[512];
void readline_find_completion(const char *cmdline)
{
}
static BOOL is_word(int c)
{
return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
c == '_' || c == '$';
}
static const char js_keywords[] =
"break|case|catch|continue|debugger|default|delete|do|"
"else|finally|for|function|if|in|instanceof|new|"
"return|switch|this|throw|try|typeof|while|with|"
"class|const|enum|import|export|extends|super|"
"implements|interface|let|package|private|protected|"
"public|static|yield|"
"undefined|null|true|false|Infinity|NaN|"
"eval|arguments|"
"await|";
static const char js_types[] = "void|var|";
static BOOL find_keyword(const char *buf, size_t buf_len, const char *dict)
{
const char *r, *p = dict;
while (*p != '\0') {
r = strchr(p, '|');
if (!r)
break;
if ((r - p) == buf_len && !memcmp(buf, p, buf_len))
return TRUE;
p = r + 1;
}
return FALSE;
}
/* return the color for the character at position 'pos' and the number
of characters of the same color */
static int term_get_color(int *plen, const char *buf, int pos, int buf_len)
{
int c, color, pos1, len;
c = buf[pos];
if (c == '"' || c == '\'') {
pos1 = pos + 1;
for(;;) {
if (buf[pos1] == '\0' || buf[pos1] == c)
break;
if (buf[pos1] == '\\' && buf[pos1 + 1] != '\0')
pos1 += 2;
else
pos1++;
}
if (buf[pos1] != '\0')
pos1++;
len = pos1 - pos;
color = STYLE_STRING;
} else if (c == '/' && buf[pos + 1] == '*') {
pos1 = pos + 2;
while (buf[pos1] != '\0' &&
!(buf[pos1] == '*' && buf[pos1 + 1] == '/')) {
pos1++;
}
if (buf[pos1] != '\0')
pos1 += 2;
len = pos1 - pos;
color = STYLE_COMMENT;
} else if ((c >= '0' && c <= '9') || c == '.') {
pos1 = pos + 1;
while (is_word(buf[pos1]))
pos1++;
len = pos1 - pos;
color = STYLE_NUMBER;
} else if (is_word(c)) {
pos1 = pos + 1;
while (is_word(buf[pos1]))
pos1++;
len = pos1 - pos;
if (find_keyword(buf + pos, len, js_keywords)) {
color = STYLE_KEYWORD;
} else {
while (buf[pos1] == ' ')
pos1++;
if (buf[pos1] == '(') {
color = STYLE_FUNCTION;
} else {
if (find_keyword(buf + pos, len, js_types)) {
color = STYLE_TYPE;
} else {
color = STYLE_IDENTIFIER;
}
}
}
} else {
color = STYLE_DEFAULT;
len = 1;
}
*plen = len;
return color;
}
static int js_interrupt_handler(JSContext *ctx, void *opaque)
{
return readline_is_interrupted();
}
static void repl_run(JSContext *ctx)
{
ReadlineState *s = &readline_state;
const char *cmd;
s->term_width = readline_tty_init();
s->term_cmd_buf = readline_cmd_buf;
s->term_kill_buf = readline_kill_buf;
s->term_cmd_buf_size = sizeof(readline_cmd_buf);
s->term_history = readline_history;
s->term_history_buf_size = sizeof(readline_history);
s->get_color = term_get_color;
JS_SetInterruptHandler(ctx, js_interrupt_handler);
for(;;) {
cmd = readline_tty(&readline_state, "mqjs > ", FALSE);
if (!cmd)
break;
eval_buf(ctx, cmd, "<cmdline>", TRUE, 0);
run_timers(ctx);
}
}
static void help(void)
{
printf("MicroQuickJS" "\n"
"usage: mqjs [options] [file [args]]\n"
"-h --help list options\n"
"-e --eval EXPR evaluate EXPR\n"
"-i --interactive go to interactive mode\n"
"-I --include file include an additional file\n"
"-d --dump dump the memory usage stats\n"
" --memory-limit n limit the memory usage to 'n' bytes\n"
"--no-column no column number in debug information\n"
"-o FILE save the bytecode to FILE\n"
"-m32 force 32 bit bytecode output (use with -o)\n"
"-b --allow-bytecode allow bytecode in input file\n");
exit(1);
}
int main(int argc, const char **argv)
{
int optind;
size_t mem_size;
int dump_memory = 0;
int interactive = 0;
const char *expr = NULL;
const char *out_filename = NULL;
const char *include_list[32];
int include_count = 0;
uint8_t *mem_buf;
JSContext *ctx;
int i, parse_flags;
BOOL force_32bit, allow_bytecode;
mem_size = 16 << 20;
dump_memory = 0;
parse_flags = 0;
force_32bit = FALSE;
allow_bytecode = FALSE;
/* cannot use getopt because we want to pass the command line to
the script */
optind = 1;
while (optind < argc && *argv[optind] == '-') {
const char *arg = argv[optind] + 1;
const char *longopt = "";
/* a single - is not an option, it also stops argument scanning */
if (!*arg)
break;
optind++;
if (*arg == '-') {
longopt = arg + 1;
arg += strlen(arg);
/* -- stops argument scanning */
if (!*longopt)
break;
}
for (; *arg || *longopt; longopt = "") {
char opt = *arg;
if (opt)
arg++;
if (opt == 'h' || opt == '?' || !strcmp(longopt, "help")) {
help();
continue;
}
if (opt == 'e' || !strcmp(longopt, "eval")) {
if (*arg) {
expr = arg;
break;
}
if (optind < argc) {
expr = argv[optind++];
break;
}
fprintf(stderr, "missing expression for -e\n");
exit(2);
}
if (!strcmp(longopt, "memory-limit")) {
char *p;
double count;
if (optind >= argc) {
fprintf(stderr, "expecting memory limit");
exit(1);
}
count = strtod(argv[optind++], &p);
switch (tolower((unsigned char)*p)) {
case 'g':
count *= 1024;
/* fall thru */
case 'm':
count *= 1024;
/* fall thru */
case 'k':
count *= 1024;
/* fall thru */
default:
mem_size = (size_t)(count);
break;
}
continue;
}
if (opt == 'd' || !strcmp(longopt, "dump")) {
dump_memory++;
continue;
}
if (opt == 'i' || !strcmp(longopt, "interactive")) {
interactive++;
continue;
}
if (opt == 'o') {
if (*arg) {
out_filename = arg;
break;
}
if (optind < argc) {
out_filename = argv[optind++];
break;
}
fprintf(stderr, "missing filename for -o\n");
exit(2);
}
if (opt == 'I' || !strcmp(longopt, "include")) {
if (optind >= argc) {
fprintf(stderr, "expecting filename");
exit(1);
}
if (include_count >= countof(include_list)) {
fprintf(stderr, "too many included files");
exit(1);
}
include_list[include_count++] = argv[optind++];
continue;
}
if (!strcmp(longopt, "no-column")) {
parse_flags |= JS_EVAL_STRIP_COL;
continue;
}
if (opt == 'm' && !strcmp(arg, "32")) {
/* XXX: using a long option is not consistent here */
force_32bit = TRUE;
arg += strlen(arg);
continue;
}
if (opt == 'b' || !strcmp(longopt, "allow-bytecode")) {
allow_bytecode = TRUE;
continue;
}
if (opt) {
fprintf(stderr, "qjs: unknown option '-%c'\n", opt);
} else {
fprintf(stderr, "qjs: unknown option '--%s'\n", longopt);
}
help();
}
}
if (out_filename) {
if (optind >= argc) {
fprintf(stderr, "expecting input filename\n");
exit(1);
}
compile_file(argv[optind], out_filename, mem_size, dump_memory,
parse_flags, force_32bit);
} else {
mem_buf = malloc(mem_size);
ctx = JS_NewContext(mem_buf, mem_size, &js_stdlib);
JS_SetLogFunc(ctx, js_log_func);
{
struct timeval tv;
gettimeofday(&tv, NULL);
JS_SetRandomSeed(ctx, ((uint64_t)tv.tv_sec << 32) ^ tv.tv_usec);
}
for(i = 0; i < include_count; i++) {
if (eval_file(ctx, include_list[i], 0, NULL,
parse_flags, allow_bytecode)) {
goto fail;
}
}
if (expr) {
if (eval_buf(ctx, expr, "<cmdline>", FALSE, parse_flags | JS_EVAL_REPL))
goto fail;
} else if (optind >= argc) {
interactive = 1;
} else {
if (eval_file(ctx, argv[optind], argc - optind, argv + optind,
parse_flags, allow_bytecode)) {
goto fail;
}
}
if (interactive) {
repl_run(ctx);
} else {
run_timers(ctx);
}
if (dump_memory)
JS_DumpMemory(ctx, (dump_memory >= 2));
JS_FreeContext(ctx);
free(mem_buf);
}
return 0;
fail:
JS_FreeContext(ctx);
free(mem_buf);
return 1;
}