quickjs 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +4 -0
  3. data/LICENSE +21 -0
  4. data/Rakefile +22 -0
  5. data/ext/quickjsrb/extconf.rb +45 -0
  6. data/ext/quickjsrb/quickjs/LICENSE +22 -0
  7. data/ext/quickjsrb/quickjs/cutils.c +631 -0
  8. data/ext/quickjsrb/quickjs/cutils.h +347 -0
  9. data/ext/quickjsrb/quickjs/libbf.c +8475 -0
  10. data/ext/quickjsrb/quickjs/libbf.h +535 -0
  11. data/ext/quickjsrb/quickjs/libregexp-opcode.h +57 -0
  12. data/ext/quickjsrb/quickjs/libregexp.c +2501 -0
  13. data/ext/quickjsrb/quickjs/libregexp.h +55 -0
  14. data/ext/quickjsrb/quickjs/libunicode-table.h +4557 -0
  15. data/ext/quickjsrb/quickjs/libunicode.c +1910 -0
  16. data/ext/quickjsrb/quickjs/libunicode.h +182 -0
  17. data/ext/quickjsrb/quickjs/list.h +99 -0
  18. data/ext/quickjsrb/quickjs/qjs.c +564 -0
  19. data/ext/quickjsrb/quickjs/qjsc.c +761 -0
  20. data/ext/quickjsrb/quickjs/qjscalc.c +4005 -0
  21. data/ext/quickjsrb/quickjs/quickjs-atom.h +273 -0
  22. data/ext/quickjsrb/quickjs/quickjs-libc.c +4052 -0
  23. data/ext/quickjsrb/quickjs/quickjs-libc.h +60 -0
  24. data/ext/quickjsrb/quickjs/quickjs-opcode.h +372 -0
  25. data/ext/quickjsrb/quickjs/quickjs.c +55978 -0
  26. data/ext/quickjsrb/quickjs/quickjs.h +1087 -0
  27. data/ext/quickjsrb/quickjs/repl.c +2057 -0
  28. data/ext/quickjsrb/quickjs/run-test262.c +2216 -0
  29. data/ext/quickjsrb/quickjs/unicode_gen.c +3225 -0
  30. data/ext/quickjsrb/quickjs/unicode_gen_def.h +291 -0
  31. data/ext/quickjsrb/quickjsrb.c +105 -0
  32. data/ext/quickjsrb/quickjsrb.h +14 -0
  33. data/lib/quickjs/version.rb +5 -0
  34. data/lib/quickjs.rb +28 -0
  35. data/sig/quickjs.rbs +4 -0
  36. metadata +81 -0
@@ -0,0 +1,761 @@
1
+ /*
2
+ * QuickJS command line compiler
3
+ *
4
+ * Copyright (c) 2018-2021 Fabrice Bellard
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ * THE SOFTWARE.
23
+ */
24
+ #include <stdlib.h>
25
+ #include <stdio.h>
26
+ #include <stdarg.h>
27
+ #include <inttypes.h>
28
+ #include <string.h>
29
+ #include <assert.h>
30
+ #include <unistd.h>
31
+ #include <errno.h>
32
+ #if !defined(_WIN32)
33
+ #include <sys/wait.h>
34
+ #endif
35
+
36
+ #include "cutils.h"
37
+ #include "quickjs-libc.h"
38
+
39
+ typedef struct {
40
+ char *name;
41
+ char *short_name;
42
+ int flags;
43
+ } namelist_entry_t;
44
+
45
+ typedef struct namelist_t {
46
+ namelist_entry_t *array;
47
+ int count;
48
+ int size;
49
+ } namelist_t;
50
+
51
+ typedef struct {
52
+ const char *option_name;
53
+ const char *init_name;
54
+ } FeatureEntry;
55
+
56
+ static namelist_t cname_list;
57
+ static namelist_t cmodule_list;
58
+ static namelist_t init_module_list;
59
+ static uint64_t feature_bitmap;
60
+ static FILE *outfile;
61
+ static BOOL byte_swap;
62
+ static BOOL dynamic_export;
63
+ static const char *c_ident_prefix = "qjsc_";
64
+
65
+ #define FE_ALL (-1)
66
+
67
+ static const FeatureEntry feature_list[] = {
68
+ { "date", "Date" },
69
+ { "eval", "Eval" },
70
+ { "string-normalize", "StringNormalize" },
71
+ { "regexp", "RegExp" },
72
+ { "json", "JSON" },
73
+ { "proxy", "Proxy" },
74
+ { "map", "MapSet" },
75
+ { "typedarray", "TypedArrays" },
76
+ { "promise", "Promise" },
77
+ #define FE_MODULE_LOADER 9
78
+ { "module-loader", NULL },
79
+ { "bigint", "BigInt" },
80
+ };
81
+
82
+ void namelist_add(namelist_t *lp, const char *name, const char *short_name,
83
+ int flags)
84
+ {
85
+ namelist_entry_t *e;
86
+ if (lp->count == lp->size) {
87
+ size_t newsize = lp->size + (lp->size >> 1) + 4;
88
+ namelist_entry_t *a =
89
+ realloc(lp->array, sizeof(lp->array[0]) * newsize);
90
+ /* XXX: check for realloc failure */
91
+ lp->array = a;
92
+ lp->size = newsize;
93
+ }
94
+ e = &lp->array[lp->count++];
95
+ e->name = strdup(name);
96
+ if (short_name)
97
+ e->short_name = strdup(short_name);
98
+ else
99
+ e->short_name = NULL;
100
+ e->flags = flags;
101
+ }
102
+
103
+ void namelist_free(namelist_t *lp)
104
+ {
105
+ while (lp->count > 0) {
106
+ namelist_entry_t *e = &lp->array[--lp->count];
107
+ free(e->name);
108
+ free(e->short_name);
109
+ }
110
+ free(lp->array);
111
+ lp->array = NULL;
112
+ lp->size = 0;
113
+ }
114
+
115
+ namelist_entry_t *namelist_find(namelist_t *lp, const char *name)
116
+ {
117
+ int i;
118
+ for(i = 0; i < lp->count; i++) {
119
+ namelist_entry_t *e = &lp->array[i];
120
+ if (!strcmp(e->name, name))
121
+ return e;
122
+ }
123
+ return NULL;
124
+ }
125
+
126
+ static void get_c_name(char *buf, size_t buf_size, const char *file)
127
+ {
128
+ const char *p, *r;
129
+ size_t len, i;
130
+ int c;
131
+ char *q;
132
+
133
+ p = strrchr(file, '/');
134
+ if (!p)
135
+ p = file;
136
+ else
137
+ p++;
138
+ r = strrchr(p, '.');
139
+ if (!r)
140
+ len = strlen(p);
141
+ else
142
+ len = r - p;
143
+ pstrcpy(buf, buf_size, c_ident_prefix);
144
+ q = buf + strlen(buf);
145
+ for(i = 0; i < len; i++) {
146
+ c = p[i];
147
+ if (!((c >= '0' && c <= '9') ||
148
+ (c >= 'A' && c <= 'Z') ||
149
+ (c >= 'a' && c <= 'z'))) {
150
+ c = '_';
151
+ }
152
+ if ((q - buf) < buf_size - 1)
153
+ *q++ = c;
154
+ }
155
+ *q = '\0';
156
+ }
157
+
158
+ static void dump_hex(FILE *f, const uint8_t *buf, size_t len)
159
+ {
160
+ size_t i, col;
161
+ col = 0;
162
+ for(i = 0; i < len; i++) {
163
+ fprintf(f, " 0x%02x,", buf[i]);
164
+ if (++col == 8) {
165
+ fprintf(f, "\n");
166
+ col = 0;
167
+ }
168
+ }
169
+ if (col != 0)
170
+ fprintf(f, "\n");
171
+ }
172
+
173
+ static void output_object_code(JSContext *ctx,
174
+ FILE *fo, JSValueConst obj, const char *c_name,
175
+ BOOL load_only)
176
+ {
177
+ uint8_t *out_buf;
178
+ size_t out_buf_len;
179
+ int flags;
180
+ flags = JS_WRITE_OBJ_BYTECODE;
181
+ if (byte_swap)
182
+ flags |= JS_WRITE_OBJ_BSWAP;
183
+ out_buf = JS_WriteObject(ctx, &out_buf_len, obj, flags);
184
+ if (!out_buf) {
185
+ js_std_dump_error(ctx);
186
+ exit(1);
187
+ }
188
+
189
+ namelist_add(&cname_list, c_name, NULL, load_only);
190
+
191
+ fprintf(fo, "const uint32_t %s_size = %u;\n\n",
192
+ c_name, (unsigned int)out_buf_len);
193
+ fprintf(fo, "const uint8_t %s[%u] = {\n",
194
+ c_name, (unsigned int)out_buf_len);
195
+ dump_hex(fo, out_buf, out_buf_len);
196
+ fprintf(fo, "};\n\n");
197
+
198
+ js_free(ctx, out_buf);
199
+ }
200
+
201
+ static int js_module_dummy_init(JSContext *ctx, JSModuleDef *m)
202
+ {
203
+ /* should never be called when compiling JS code */
204
+ abort();
205
+ }
206
+
207
+ static void find_unique_cname(char *cname, size_t cname_size)
208
+ {
209
+ char cname1[1024];
210
+ int suffix_num;
211
+ size_t len, max_len;
212
+ assert(cname_size >= 32);
213
+ /* find a C name not matching an existing module C name by
214
+ adding a numeric suffix */
215
+ len = strlen(cname);
216
+ max_len = cname_size - 16;
217
+ if (len > max_len)
218
+ cname[max_len] = '\0';
219
+ suffix_num = 1;
220
+ for(;;) {
221
+ snprintf(cname1, sizeof(cname1), "%s_%d", cname, suffix_num);
222
+ if (!namelist_find(&cname_list, cname1))
223
+ break;
224
+ suffix_num++;
225
+ }
226
+ pstrcpy(cname, cname_size, cname1);
227
+ }
228
+
229
+ JSModuleDef *jsc_module_loader(JSContext *ctx,
230
+ const char *module_name, void *opaque)
231
+ {
232
+ JSModuleDef *m;
233
+ namelist_entry_t *e;
234
+
235
+ /* check if it is a declared C or system module */
236
+ e = namelist_find(&cmodule_list, module_name);
237
+ if (e) {
238
+ /* add in the static init module list */
239
+ namelist_add(&init_module_list, e->name, e->short_name, 0);
240
+ /* create a dummy module */
241
+ m = JS_NewCModule(ctx, module_name, js_module_dummy_init);
242
+ } else if (has_suffix(module_name, ".so")) {
243
+ fprintf(stderr, "Warning: binary module '%s' will be dynamically loaded\n", module_name);
244
+ /* create a dummy module */
245
+ m = JS_NewCModule(ctx, module_name, js_module_dummy_init);
246
+ /* the resulting executable will export its symbols for the
247
+ dynamic library */
248
+ dynamic_export = TRUE;
249
+ } else {
250
+ size_t buf_len;
251
+ uint8_t *buf;
252
+ JSValue func_val;
253
+ char cname[1024];
254
+
255
+ buf = js_load_file(ctx, &buf_len, module_name);
256
+ if (!buf) {
257
+ JS_ThrowReferenceError(ctx, "could not load module filename '%s'",
258
+ module_name);
259
+ return NULL;
260
+ }
261
+
262
+ /* compile the module */
263
+ func_val = JS_Eval(ctx, (char *)buf, buf_len, module_name,
264
+ JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
265
+ js_free(ctx, buf);
266
+ if (JS_IsException(func_val))
267
+ return NULL;
268
+ get_c_name(cname, sizeof(cname), module_name);
269
+ if (namelist_find(&cname_list, cname)) {
270
+ find_unique_cname(cname, sizeof(cname));
271
+ }
272
+ output_object_code(ctx, outfile, func_val, cname, TRUE);
273
+
274
+ /* the module is already referenced, so we must free it */
275
+ m = JS_VALUE_GET_PTR(func_val);
276
+ JS_FreeValue(ctx, func_val);
277
+ }
278
+ return m;
279
+ }
280
+
281
+ static void compile_file(JSContext *ctx, FILE *fo,
282
+ const char *filename,
283
+ const char *c_name1,
284
+ int module)
285
+ {
286
+ uint8_t *buf;
287
+ char c_name[1024];
288
+ int eval_flags;
289
+ JSValue obj;
290
+ size_t buf_len;
291
+
292
+ buf = js_load_file(ctx, &buf_len, filename);
293
+ if (!buf) {
294
+ fprintf(stderr, "Could not load '%s'\n", filename);
295
+ exit(1);
296
+ }
297
+ eval_flags = JS_EVAL_FLAG_COMPILE_ONLY;
298
+ if (module < 0) {
299
+ module = (has_suffix(filename, ".mjs") ||
300
+ JS_DetectModule((const char *)buf, buf_len));
301
+ }
302
+ if (module)
303
+ eval_flags |= JS_EVAL_TYPE_MODULE;
304
+ else
305
+ eval_flags |= JS_EVAL_TYPE_GLOBAL;
306
+ obj = JS_Eval(ctx, (const char *)buf, buf_len, filename, eval_flags);
307
+ if (JS_IsException(obj)) {
308
+ js_std_dump_error(ctx);
309
+ exit(1);
310
+ }
311
+ js_free(ctx, buf);
312
+ if (c_name1) {
313
+ pstrcpy(c_name, sizeof(c_name), c_name1);
314
+ } else {
315
+ get_c_name(c_name, sizeof(c_name), filename);
316
+ }
317
+ output_object_code(ctx, fo, obj, c_name, FALSE);
318
+ JS_FreeValue(ctx, obj);
319
+ }
320
+
321
+ static const char main_c_template1[] =
322
+ "int main(int argc, char **argv)\n"
323
+ "{\n"
324
+ " JSRuntime *rt;\n"
325
+ " JSContext *ctx;\n"
326
+ " rt = JS_NewRuntime();\n"
327
+ " js_std_set_worker_new_context_func(JS_NewCustomContext);\n"
328
+ " js_std_init_handlers(rt);\n"
329
+ ;
330
+
331
+ static const char main_c_template2[] =
332
+ " js_std_loop(ctx);\n"
333
+ " js_std_free_handlers(rt);\n"
334
+ " JS_FreeContext(ctx);\n"
335
+ " JS_FreeRuntime(rt);\n"
336
+ " return 0;\n"
337
+ "}\n";
338
+
339
+ #define PROG_NAME "qjsc"
340
+
341
+ void help(void)
342
+ {
343
+ printf("QuickJS Compiler version " CONFIG_VERSION "\n"
344
+ "usage: " PROG_NAME " [options] [files]\n"
345
+ "\n"
346
+ "options are:\n"
347
+ "-c only output bytecode to a C file\n"
348
+ "-e output main() and bytecode to a C file (default = executable output)\n"
349
+ "-o output set the output filename\n"
350
+ "-N cname set the C name of the generated data\n"
351
+ "-m compile as Javascript module (default=autodetect)\n"
352
+ "-D module_name compile a dynamically loaded module or worker\n"
353
+ "-M module_name[,cname] add initialization code for an external C module\n"
354
+ "-x byte swapped output\n"
355
+ "-p prefix set the prefix of the generated C names\n"
356
+ "-S n set the maximum stack size to 'n' bytes (default=%d)\n",
357
+ JS_DEFAULT_STACK_SIZE);
358
+ #ifdef CONFIG_LTO
359
+ {
360
+ int i;
361
+ printf("-flto use link time optimization\n");
362
+ printf("-fbignum enable bignum extensions\n");
363
+ printf("-fno-[");
364
+ for(i = 0; i < countof(feature_list); i++) {
365
+ if (i != 0)
366
+ printf("|");
367
+ printf("%s", feature_list[i].option_name);
368
+ }
369
+ printf("]\n"
370
+ " disable selected language features (smaller code size)\n");
371
+ }
372
+ #endif
373
+ exit(1);
374
+ }
375
+
376
+ #if defined(CONFIG_CC) && !defined(_WIN32)
377
+
378
+ int exec_cmd(char **argv)
379
+ {
380
+ int pid, status, ret;
381
+
382
+ pid = fork();
383
+ if (pid == 0) {
384
+ execvp(argv[0], argv);
385
+ exit(1);
386
+ }
387
+
388
+ for(;;) {
389
+ ret = waitpid(pid, &status, 0);
390
+ if (ret == pid && WIFEXITED(status))
391
+ break;
392
+ }
393
+ return WEXITSTATUS(status);
394
+ }
395
+
396
+ static int output_executable(const char *out_filename, const char *cfilename,
397
+ BOOL use_lto, BOOL verbose, const char *exename)
398
+ {
399
+ const char *argv[64];
400
+ const char **arg, *bn_suffix, *lto_suffix;
401
+ char libjsname[1024];
402
+ char exe_dir[1024], inc_dir[1024], lib_dir[1024], buf[1024], *p;
403
+ int ret;
404
+
405
+ /* get the directory of the executable */
406
+ pstrcpy(exe_dir, sizeof(exe_dir), exename);
407
+ p = strrchr(exe_dir, '/');
408
+ if (p) {
409
+ *p = '\0';
410
+ } else {
411
+ pstrcpy(exe_dir, sizeof(exe_dir), ".");
412
+ }
413
+
414
+ /* if 'quickjs.h' is present at the same path as the executable, we
415
+ use it as include and lib directory */
416
+ snprintf(buf, sizeof(buf), "%s/quickjs.h", exe_dir);
417
+ if (access(buf, R_OK) == 0) {
418
+ pstrcpy(inc_dir, sizeof(inc_dir), exe_dir);
419
+ pstrcpy(lib_dir, sizeof(lib_dir), exe_dir);
420
+ } else {
421
+ snprintf(inc_dir, sizeof(inc_dir), "%s/include/quickjs", CONFIG_PREFIX);
422
+ snprintf(lib_dir, sizeof(lib_dir), "%s/lib/quickjs", CONFIG_PREFIX);
423
+ }
424
+
425
+ lto_suffix = "";
426
+ bn_suffix = "";
427
+
428
+ arg = argv;
429
+ *arg++ = CONFIG_CC;
430
+ *arg++ = "-O2";
431
+ #ifdef CONFIG_LTO
432
+ if (use_lto) {
433
+ *arg++ = "-flto";
434
+ lto_suffix = ".lto";
435
+ }
436
+ #endif
437
+ /* XXX: use the executable path to find the includes files and
438
+ libraries */
439
+ *arg++ = "-D";
440
+ *arg++ = "_GNU_SOURCE";
441
+ *arg++ = "-I";
442
+ *arg++ = inc_dir;
443
+ *arg++ = "-o";
444
+ *arg++ = out_filename;
445
+ if (dynamic_export)
446
+ *arg++ = "-rdynamic";
447
+ *arg++ = cfilename;
448
+ snprintf(libjsname, sizeof(libjsname), "%s/libquickjs%s%s.a",
449
+ lib_dir, bn_suffix, lto_suffix);
450
+ *arg++ = libjsname;
451
+ *arg++ = "-lm";
452
+ *arg++ = "-ldl";
453
+ *arg++ = "-lpthread";
454
+ *arg = NULL;
455
+
456
+ if (verbose) {
457
+ for(arg = argv; *arg != NULL; arg++)
458
+ printf("%s ", *arg);
459
+ printf("\n");
460
+ }
461
+
462
+ ret = exec_cmd((char **)argv);
463
+ unlink(cfilename);
464
+ return ret;
465
+ }
466
+ #else
467
+ static int output_executable(const char *out_filename, const char *cfilename,
468
+ BOOL use_lto, BOOL verbose, const char *exename)
469
+ {
470
+ fprintf(stderr, "Executable output is not supported for this target\n");
471
+ exit(1);
472
+ return 0;
473
+ }
474
+ #endif
475
+
476
+
477
+ typedef enum {
478
+ OUTPUT_C,
479
+ OUTPUT_C_MAIN,
480
+ OUTPUT_EXECUTABLE,
481
+ } OutputTypeEnum;
482
+
483
+ int main(int argc, char **argv)
484
+ {
485
+ int c, i, verbose;
486
+ const char *out_filename, *cname;
487
+ char cfilename[1024];
488
+ FILE *fo;
489
+ JSRuntime *rt;
490
+ JSContext *ctx;
491
+ BOOL use_lto;
492
+ int module;
493
+ OutputTypeEnum output_type;
494
+ size_t stack_size;
495
+ #ifdef CONFIG_BIGNUM
496
+ BOOL bignum_ext = FALSE;
497
+ #endif
498
+ namelist_t dynamic_module_list;
499
+
500
+ out_filename = NULL;
501
+ output_type = OUTPUT_EXECUTABLE;
502
+ cname = NULL;
503
+ feature_bitmap = FE_ALL;
504
+ module = -1;
505
+ byte_swap = FALSE;
506
+ verbose = 0;
507
+ use_lto = FALSE;
508
+ stack_size = 0;
509
+ memset(&dynamic_module_list, 0, sizeof(dynamic_module_list));
510
+
511
+ /* add system modules */
512
+ namelist_add(&cmodule_list, "std", "std", 0);
513
+ namelist_add(&cmodule_list, "os", "os", 0);
514
+
515
+ for(;;) {
516
+ c = getopt(argc, argv, "ho:cN:f:mxevM:p:S:D:");
517
+ if (c == -1)
518
+ break;
519
+ switch(c) {
520
+ case 'h':
521
+ help();
522
+ case 'o':
523
+ out_filename = optarg;
524
+ break;
525
+ case 'c':
526
+ output_type = OUTPUT_C;
527
+ break;
528
+ case 'e':
529
+ output_type = OUTPUT_C_MAIN;
530
+ break;
531
+ case 'N':
532
+ cname = optarg;
533
+ break;
534
+ case 'f':
535
+ {
536
+ const char *p;
537
+ p = optarg;
538
+ if (!strcmp(optarg, "lto")) {
539
+ use_lto = TRUE;
540
+ } else if (strstart(p, "no-", &p)) {
541
+ use_lto = TRUE;
542
+ for(i = 0; i < countof(feature_list); i++) {
543
+ if (!strcmp(p, feature_list[i].option_name)) {
544
+ feature_bitmap &= ~((uint64_t)1 << i);
545
+ break;
546
+ }
547
+ }
548
+ if (i == countof(feature_list))
549
+ goto bad_feature;
550
+ } else
551
+ #ifdef CONFIG_BIGNUM
552
+ if (!strcmp(optarg, "bignum")) {
553
+ bignum_ext = TRUE;
554
+ } else
555
+ #endif
556
+ {
557
+ bad_feature:
558
+ fprintf(stderr, "unsupported feature: %s\n", optarg);
559
+ exit(1);
560
+ }
561
+ }
562
+ break;
563
+ case 'm':
564
+ module = 1;
565
+ break;
566
+ case 'M':
567
+ {
568
+ char *p;
569
+ char path[1024];
570
+ char cname[1024];
571
+ pstrcpy(path, sizeof(path), optarg);
572
+ p = strchr(path, ',');
573
+ if (p) {
574
+ *p = '\0';
575
+ pstrcpy(cname, sizeof(cname), p + 1);
576
+ } else {
577
+ get_c_name(cname, sizeof(cname), path);
578
+ }
579
+ namelist_add(&cmodule_list, path, cname, 0);
580
+ }
581
+ break;
582
+ case 'D':
583
+ namelist_add(&dynamic_module_list, optarg, NULL, 0);
584
+ break;
585
+ case 'x':
586
+ byte_swap = TRUE;
587
+ break;
588
+ case 'v':
589
+ verbose++;
590
+ break;
591
+ case 'p':
592
+ c_ident_prefix = optarg;
593
+ break;
594
+ case 'S':
595
+ stack_size = (size_t)strtod(optarg, NULL);
596
+ break;
597
+ default:
598
+ break;
599
+ }
600
+ }
601
+
602
+ if (optind >= argc)
603
+ help();
604
+
605
+ if (!out_filename) {
606
+ if (output_type == OUTPUT_EXECUTABLE) {
607
+ out_filename = "a.out";
608
+ } else {
609
+ out_filename = "out.c";
610
+ }
611
+ }
612
+
613
+ if (output_type == OUTPUT_EXECUTABLE) {
614
+ #if defined(_WIN32) || defined(__ANDROID__)
615
+ /* XXX: find a /tmp directory ? */
616
+ snprintf(cfilename, sizeof(cfilename), "out%d.c", getpid());
617
+ #else
618
+ snprintf(cfilename, sizeof(cfilename), "/tmp/out%d.c", getpid());
619
+ #endif
620
+ } else {
621
+ pstrcpy(cfilename, sizeof(cfilename), out_filename);
622
+ }
623
+
624
+ fo = fopen(cfilename, "w");
625
+ if (!fo) {
626
+ perror(cfilename);
627
+ exit(1);
628
+ }
629
+ outfile = fo;
630
+
631
+ rt = JS_NewRuntime();
632
+ ctx = JS_NewContext(rt);
633
+ #ifdef CONFIG_BIGNUM
634
+ if (bignum_ext) {
635
+ JS_AddIntrinsicBigFloat(ctx);
636
+ JS_AddIntrinsicBigDecimal(ctx);
637
+ JS_AddIntrinsicOperators(ctx);
638
+ JS_EnableBignumExt(ctx, TRUE);
639
+ }
640
+ #endif
641
+
642
+ /* loader for ES6 modules */
643
+ JS_SetModuleLoaderFunc(rt, NULL, jsc_module_loader, NULL);
644
+
645
+ fprintf(fo, "/* File generated automatically by the QuickJS compiler. */\n"
646
+ "\n"
647
+ );
648
+
649
+ if (output_type != OUTPUT_C) {
650
+ fprintf(fo, "#include \"quickjs-libc.h\"\n"
651
+ "\n"
652
+ );
653
+ } else {
654
+ fprintf(fo, "#include <inttypes.h>\n"
655
+ "\n"
656
+ );
657
+ }
658
+
659
+ for(i = optind; i < argc; i++) {
660
+ const char *filename = argv[i];
661
+ compile_file(ctx, fo, filename, cname, module);
662
+ cname = NULL;
663
+ }
664
+
665
+ for(i = 0; i < dynamic_module_list.count; i++) {
666
+ if (!jsc_module_loader(ctx, dynamic_module_list.array[i].name, NULL)) {
667
+ fprintf(stderr, "Could not load dynamic module '%s'\n",
668
+ dynamic_module_list.array[i].name);
669
+ exit(1);
670
+ }
671
+ }
672
+
673
+ if (output_type != OUTPUT_C) {
674
+ fprintf(fo,
675
+ "static JSContext *JS_NewCustomContext(JSRuntime *rt)\n"
676
+ "{\n"
677
+ " JSContext *ctx = JS_NewContextRaw(rt);\n"
678
+ " if (!ctx)\n"
679
+ " return NULL;\n");
680
+ /* add the basic objects */
681
+ fprintf(fo, " JS_AddIntrinsicBaseObjects(ctx);\n");
682
+ for(i = 0; i < countof(feature_list); i++) {
683
+ if ((feature_bitmap & ((uint64_t)1 << i)) &&
684
+ feature_list[i].init_name) {
685
+ fprintf(fo, " JS_AddIntrinsic%s(ctx);\n",
686
+ feature_list[i].init_name);
687
+ }
688
+ }
689
+ #ifdef CONFIG_BIGNUM
690
+ if (bignum_ext) {
691
+ fprintf(fo,
692
+ " JS_AddIntrinsicBigFloat(ctx);\n"
693
+ " JS_AddIntrinsicBigDecimal(ctx);\n"
694
+ " JS_AddIntrinsicOperators(ctx);\n"
695
+ " JS_EnableBignumExt(ctx, 1);\n");
696
+ }
697
+ #endif
698
+ /* add the precompiled modules (XXX: could modify the module
699
+ loader instead) */
700
+ for(i = 0; i < init_module_list.count; i++) {
701
+ namelist_entry_t *e = &init_module_list.array[i];
702
+ /* initialize the static C modules */
703
+
704
+ fprintf(fo,
705
+ " {\n"
706
+ " extern JSModuleDef *js_init_module_%s(JSContext *ctx, const char *name);\n"
707
+ " js_init_module_%s(ctx, \"%s\");\n"
708
+ " }\n",
709
+ e->short_name, e->short_name, e->name);
710
+ }
711
+ for(i = 0; i < cname_list.count; i++) {
712
+ namelist_entry_t *e = &cname_list.array[i];
713
+ if (e->flags) {
714
+ fprintf(fo, " js_std_eval_binary(ctx, %s, %s_size, 1);\n",
715
+ e->name, e->name);
716
+ }
717
+ }
718
+ fprintf(fo,
719
+ " return ctx;\n"
720
+ "}\n\n");
721
+
722
+ fputs(main_c_template1, fo);
723
+
724
+ if (stack_size != 0) {
725
+ fprintf(fo, " JS_SetMaxStackSize(rt, %u);\n",
726
+ (unsigned int)stack_size);
727
+ }
728
+
729
+ /* add the module loader if necessary */
730
+ if (feature_bitmap & (1 << FE_MODULE_LOADER)) {
731
+ fprintf(fo, " JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);\n");
732
+ }
733
+
734
+ fprintf(fo,
735
+ " ctx = JS_NewCustomContext(rt);\n"
736
+ " js_std_add_helpers(ctx, argc, argv);\n");
737
+
738
+ for(i = 0; i < cname_list.count; i++) {
739
+ namelist_entry_t *e = &cname_list.array[i];
740
+ if (!e->flags) {
741
+ fprintf(fo, " js_std_eval_binary(ctx, %s, %s_size, 0);\n",
742
+ e->name, e->name);
743
+ }
744
+ }
745
+ fputs(main_c_template2, fo);
746
+ }
747
+
748
+ JS_FreeContext(ctx);
749
+ JS_FreeRuntime(rt);
750
+
751
+ fclose(fo);
752
+
753
+ if (output_type == OUTPUT_EXECUTABLE) {
754
+ return output_executable(out_filename, cfilename, use_lto, verbose,
755
+ argv[0]);
756
+ }
757
+ namelist_free(&cname_list);
758
+ namelist_free(&cmodule_list);
759
+ namelist_free(&init_module_list);
760
+ return 0;
761
+ }