Debian Codegraph @

quickjs/run-test262.c

raw symbols SCIP
1 /*
2 * ECMA Test 262 Runner for QuickJS
3 *
4 * Copyright (c) 2017-2021 Fabrice Bellard
5 * Copyright (c) 2017-2021 Charlie Gordon
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <inttypes.h>
29 #include <string.h>
30 #include <assert.h>
31 #include <ctype.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <time.h>
35 #include <dirent.h>
36 #include <ftw.h>
37
38 #include "cutils.h"
39 #include "list.h"
40 #include "quickjs-libc.h"
41
42 /* enable test262 thread support to test SharedArrayBuffer and Atomics */
43 #define CONFIG_AGENT
44
45 #define CMD_NAME "run-test262"
46
47 typedef struct namelist_t {
48 char **array;
49 int count;
50 int size;
51 unsigned int sorted : 1;
52 } namelist_t;
53
54 namelist_t test_list;
55 namelist_t exclude_list;
56 namelist_t exclude_dir_list;
57
58 FILE *outfile;
59 enum test_mode_t {
60 TEST_DEFAULT_NOSTRICT, /* run tests as nostrict unless test is flagged as strictonly */
61 TEST_DEFAULT_STRICT, /* run tests as strict unless test is flagged as nostrict */
62 TEST_NOSTRICT, /* run tests as nostrict, skip strictonly tests */
63 TEST_STRICT, /* run tests as strict, skip nostrict tests */
64 TEST_ALL, /* run tests in both strict and nostrict, unless restricted by spec */
65 } test_mode = TEST_DEFAULT_NOSTRICT;
66 int skip_async;
67 int skip_module;
68 int new_style;
69 int dump_memory;
70 int stats_count;
71 JSMemoryUsage stats_all, stats_avg, stats_min, stats_max;
72 char *stats_min_filename;
73 char *stats_max_filename;
74 int verbose;
75 char *harness_dir;
76 char *harness_exclude;
77 char *harness_features;
78 char *harness_skip_features;
79 char *error_filename;
80 char *error_file;
81 FILE *error_out;
82 char *report_filename;
83 int update_errors;
84 int test_count, test_failed, test_index, test_skipped, test_excluded;
85 int new_errors, changed_errors, fixed_errors;
86 int async_done;
87
88 void warning(const char *, ...) __attribute__((__format__(__printf__, 1, 2)));
89 void fatal(int, const char *, ...) __attribute__((__format__(__printf__, 2, 3)));
90
91 void warning(const char *fmt, ...)
92 {
93 va_list ap;
94
95 fflush(stdout);
96 fprintf(stderr, "%s: ", CMD_NAME);
97 va_start(ap, fmt);
98 vfprintf(stderr, fmt, ap);
99 va_end(ap);
100 fputc('\n', stderr);
101 }
102
103 void fatal(int errcode, const char *fmt, ...)
104 {
105 va_list ap;
106
107 fflush(stdout);
108 fprintf(stderr, "%s: ", CMD_NAME);
109 va_start(ap, fmt);
110 vfprintf(stderr, fmt, ap);
111 va_end(ap);
112 fputc('\n', stderr);
113
114 exit(errcode);
115 }
116
117 void perror_exit(int errcode, const char *s)
118 {
119 fflush(stdout);
120 fprintf(stderr, "%s: ", CMD_NAME);
121 perror(s);
122 exit(errcode);
123 }
124
125 char *strdup_len(const char *str, int len)
126 {
127 char *p = malloc(len + 1);
128 memcpy(p, str, len);
129 p[len] = '\0';
130 return p;
131 }
132
133 static inline int str_equal(const char *a, const char *b) {
134 return !strcmp(a, b);
135 }
136
137 char *str_append(char **pp, const char *sep, const char *str) {
138 char *res, *p;
139 size_t len = 0;
140 p = *pp;
141 if (p) {
142 len = strlen(p) + strlen(sep);
143 }
144 res = malloc(len + strlen(str) + 1);
145 if (p) {
146 strcpy(res, p);
147 strcat(res, sep);
148 }
149 strcpy(res + len, str);
150 free(p);
151 return *pp = res;
152 }
153
154 char *str_strip(char *p)
155 {
156 size_t len = strlen(p);
157 while (len > 0 && isspace((unsigned char)p[len - 1]))
158 p[--len] = '\0';
159 while (isspace((unsigned char)*p))
160 p++;
161 return p;
162 }
163
164 int has_prefix(const char *str, const char *prefix)
165 {
166 return !strncmp(str, prefix, strlen(prefix));
167 }
168
169 char *skip_prefix(const char *str, const char *prefix)
170 {
171 int i;
172 for (i = 0;; i++) {
173 if (prefix[i] == '\0') { /* skip the prefix */
174 str += i;
175 break;
176 }
177 if (str[i] != prefix[i])
178 break;
179 }
180 return (char *)str;
181 }
182
183 char *get_basename(const char *filename)
184 {
185 char *p;
186
187 p = strrchr(filename, '/');
188 if (!p)
189 return NULL;
190 return strdup_len(filename, p - filename);
191 }
192
193 char *compose_path(const char *path, const char *name)
194 {
195 int path_len, name_len;
196 char *d, *q;
197
198 if (!path || path[0] == '\0' || *name == '/') {
199 d = strdup(name);
200 } else {
201 path_len = strlen(path);
202 name_len = strlen(name);
203 d = malloc(path_len + 1 + name_len + 1);
204 if (d) {
205 q = d;
206 memcpy(q, path, path_len);
207 q += path_len;
208 if (path[path_len - 1] != '/')
209 *q++ = '/';
210 memcpy(q, name, name_len + 1);
211 }
212 }
213 return d;
214 }
215
216 int namelist_cmp(const char *a, const char *b)
217 {
218 /* compare strings in modified lexicographical order */
219 for (;;) {
220 int ca = (unsigned char)*a++;
221 int cb = (unsigned char)*b++;
222 if (isdigit(ca) && isdigit(cb)) {
223 int na = ca - '0';
224 int nb = cb - '0';
225 while (isdigit(ca = (unsigned char)*a++))
226 na = na * 10 + ca - '0';
227 while (isdigit(cb = (unsigned char)*b++))
228 nb = nb * 10 + cb - '0';
229 if (na < nb)
230 return -1;
231 if (na > nb)
232 return +1;
233 }
234 if (ca < cb)
235 return -1;
236 if (ca > cb)
237 return +1;
238 if (ca == '\0')
239 return 0;
240 }
241 }
242
243 int namelist_cmp_indirect(const void *a, const void *b)
244 {
245 return namelist_cmp(*(const char **)a, *(const char **)b);
246 }
247
248 void namelist_sort(namelist_t *lp)
249 {
250 int i, count;
251 if (lp->count > 1) {
252 qsort(lp->array, lp->count, sizeof(*lp->array), namelist_cmp_indirect);
253 /* remove duplicates */
254 for (count = i = 1; i < lp->count; i++) {
255 if (namelist_cmp(lp->array[count - 1], lp->array[i]) == 0) {
256 free(lp->array[i]);
257 } else {
258 lp->array[count++] = lp->array[i];
259 }
260 }
261 lp->count = count;
262 }
263 lp->sorted = 1;
264 }
265
266 int namelist_find(namelist_t *lp, const char *name)
267 {
268 int a, b, m, cmp;
269
270 if (!lp->sorted) {
271 namelist_sort(lp);
272 }
273 for (a = 0, b = lp->count; a < b;) {
274 m = a + (b - a) / 2;
275 cmp = namelist_cmp(lp->array[m], name);
276 if (cmp < 0)
277 a = m + 1;
278 else if (cmp > 0)
279 b = m;
280 else
281 return m;
282 }
283 return -1;
284 }
285
286 void namelist_add(namelist_t *lp, const char *base, const char *name)
287 {
288 char *s;
289
290 s = compose_path(base, name);
291 if (!s)
292 goto fail;
293 if (lp->count == lp->size) {
294 size_t newsize = lp->size + (lp->size >> 1) + 4;
295 char **a = realloc(lp->array, sizeof(lp->array[0]) * newsize);
296 if (!a)
297 goto fail;
298 lp->array = a;
299 lp->size = newsize;
300 }
301 lp->array[lp->count] = s;
302 lp->count++;
303 return;
304 fail:
305 fatal(1, "allocation failure\n");
306 }
307
308 void namelist_load(namelist_t *lp, const char *filename)
309 {
310 char buf[1024];
311 char *base_name;
312 FILE *f;
313
314 f = fopen(filename, "rb");
315 if (!f) {
316 perror_exit(1, filename);
317 }
318 base_name = get_basename(filename);
319
320 while (fgets(buf, sizeof(buf), f) != NULL) {
321 char *p = str_strip(buf);
322 if (*p == '#' || *p == ';' || *p == '\0')
323 continue; /* line comment */
324
325 namelist_add(lp, base_name, p);
326 }
327 free(base_name);
328 fclose(f);
329 }
330
331 void namelist_add_from_error_file(namelist_t *lp, const char *file)
332 {
333 const char *p, *p0;
334 char *pp;
335
336 for (p = file; (p = strstr(p, ".js:")) != NULL; p++) {
337 for (p0 = p; p0 > file && p0[-1] != '\n'; p0--)
338 continue;
339 pp = strdup_len(p0, p + 3 - p0);
340 namelist_add(lp, NULL, pp);
341 free(pp);
342 }
343 }
344
345 void namelist_free(namelist_t *lp)
346 {
347 while (lp->count > 0) {
348 free(lp->array[--lp->count]);
349 }
350 free(lp->array);
351 lp->array = NULL;
352 lp->size = 0;
353 }
354
355 static int add_test_file(const char *filename, const struct stat *ptr, int flag)
356 {
357 namelist_t *lp = &test_list;
358 if (has_suffix(filename, ".js") && !has_suffix(filename, "_FIXTURE.js"))
359 namelist_add(lp, NULL, filename);
360 return 0;
361 }
362
363 /* find js files from the directory tree and sort the list */
364 static void enumerate_tests(const char *path)
365 {
366 namelist_t *lp = &test_list;
367 int start = lp->count;
368 ftw(path, add_test_file, 100);
369 qsort(lp->array + start, lp->count - start, sizeof(*lp->array),
370 namelist_cmp_indirect);
371 }
372
373 static JSValue js_print(JSContext *ctx, JSValueConst this_val,
374 int argc, JSValueConst *argv)
375 {
376 int i;
377 const char *str;
378
379 if (outfile) {
380 for (i = 0; i < argc; i++) {
381 if (i != 0)
382 fputc(' ', outfile);
383 str = JS_ToCString(ctx, argv[i]);
384 if (!str)
385 return JS_EXCEPTION;
386 if (!strcmp(str, "Test262:AsyncTestComplete")) {
387 async_done++;
388 } else if (strstart(str, "Test262:AsyncTestFailure", NULL)) {
389 async_done = 2; /* force an error */
390 }
391 fputs(str, outfile);
392 JS_FreeCString(ctx, str);
393 }
394 fputc('\n', outfile);
395 }
396 return JS_UNDEFINED;
397 }
398
399 static JSValue js_detachArrayBuffer(JSContext *ctx, JSValue this_val,
400 int argc, JSValue *argv)
401 {
402 JS_DetachArrayBuffer(ctx, argv[0]);
403 return JS_UNDEFINED;
404 }
405
406 static JSValue js_evalScript(JSContext *ctx, JSValue this_val,
407 int argc, JSValue *argv)
408 {
409 const char *str;
410 size_t len;
411 JSValue ret;
412 str = JS_ToCStringLen(ctx, &len, argv[0]);
413 if (!str)
414 return JS_EXCEPTION;
415 ret = JS_Eval(ctx, str, len, "<evalScript>", JS_EVAL_TYPE_GLOBAL);
416 JS_FreeCString(ctx, str);
417 return ret;
418 }
419
420 #ifdef CONFIG_AGENT
421
422 #include <pthread.h>
423
424 typedef struct {
425 struct list_head link;
426 pthread_t tid;
427 char *script;
428 JSValue broadcast_func;
429 BOOL broadcast_pending;
430 JSValue broadcast_sab; /* in the main context */
431 uint8_t *broadcast_sab_buf;
432 size_t broadcast_sab_size;
433 int32_t broadcast_val;
434 } Test262Agent;
435
436 typedef struct {
437 struct list_head link;
438 char *str;
439 } AgentReport;
440
441 static JSValue add_helpers1(JSContext *ctx);
442 static void add_helpers(JSContext *ctx);
443
444 static pthread_mutex_t agent_mutex = PTHREAD_MUTEX_INITIALIZER;
445 static pthread_cond_t agent_cond = PTHREAD_COND_INITIALIZER;
446 /* list of Test262Agent.link */
447 static struct list_head agent_list = LIST_HEAD_INIT(agent_list);
448
449 static pthread_mutex_t report_mutex = PTHREAD_MUTEX_INITIALIZER;
450 /* list of AgentReport.link */
451 static struct list_head report_list = LIST_HEAD_INIT(report_list);
452
453 static void *agent_start(void *arg)
454 {
455 Test262Agent *agent = arg;
456 JSRuntime *rt;
457 JSContext *ctx;
458 JSValue ret_val;
459 int ret;
460
461 rt = JS_NewRuntime();
462 if (rt == NULL) {
463 fatal(1, "JS_NewRuntime failure");
464 }
465 ctx = JS_NewContext(rt);
466 if (ctx == NULL) {
467 JS_FreeRuntime(rt);
468 fatal(1, "JS_NewContext failure");
469 }
470 JS_SetContextOpaque(ctx, agent);
471 JS_SetRuntimeInfo(rt, "agent");
472 JS_SetCanBlock(rt, TRUE);
473
474 add_helpers(ctx);
475 ret_val = JS_Eval(ctx, agent->script, strlen(agent->script),
476 "<evalScript>", JS_EVAL_TYPE_GLOBAL);
477 free(agent->script);
478 agent->script = NULL;
479 if (JS_IsException(ret_val))
480 js_std_dump_error(ctx);
481 JS_FreeValue(ctx, ret_val);
482
483 for(;;) {
484 JSContext *ctx1;
485 ret = JS_ExecutePendingJob(JS_GetRuntime(ctx), &ctx1);
486 if (ret < 0) {
487 js_std_dump_error(ctx);
488 break;
489 } else if (ret == 0) {
490 if (JS_IsUndefined(agent->broadcast_func)) {
491 break;
492 } else {
493 JSValue args[2];
494
495 pthread_mutex_lock(&agent_mutex);
496 while (!agent->broadcast_pending) {
497 pthread_cond_wait(&agent_cond, &agent_mutex);
498 }
499
500 agent->broadcast_pending = FALSE;
501 pthread_cond_signal(&agent_cond);
502
503 pthread_mutex_unlock(&agent_mutex);
504
505 args[0] = JS_NewArrayBuffer(ctx, agent->broadcast_sab_buf,
506 agent->broadcast_sab_size,
507 NULL, NULL, TRUE);
508 args[1] = JS_NewInt32(ctx, agent->broadcast_val);
509 ret_val = JS_Call(ctx, agent->broadcast_func, JS_UNDEFINED,
510 2, (JSValueConst *)args);
511 JS_FreeValue(ctx, args[0]);
512 JS_FreeValue(ctx, args[1]);
513 if (JS_IsException(ret_val))
514 js_std_dump_error(ctx);
515 JS_FreeValue(ctx, ret_val);
516 JS_FreeValue(ctx, agent->broadcast_func);
517 agent->broadcast_func = JS_UNDEFINED;
518 }
519 }
520 }
521 JS_FreeValue(ctx, agent->broadcast_func);
522
523 JS_FreeContext(ctx);
524 JS_FreeRuntime(rt);
525 return NULL;
526 }
527
528 static JSValue js_agent_start(JSContext *ctx, JSValue this_val,
529 int argc, JSValue *argv)
530 {
531 const char *script;
532 Test262Agent *agent;
533
534 if (JS_GetContextOpaque(ctx) != NULL)
535 return JS_ThrowTypeError(ctx, "cannot be called inside an agent");
536
537 script = JS_ToCString(ctx, argv[0]);
538 if (!script)
539 return JS_EXCEPTION;
540 agent = malloc(sizeof(*agent));
541 memset(agent, 0, sizeof(*agent));
542 agent->broadcast_func = JS_UNDEFINED;
543 agent->broadcast_sab = JS_UNDEFINED;
544 agent->script = strdup(script);
545 JS_FreeCString(ctx, script);
546 list_add_tail(&agent->link, &agent_list);
547 pthread_create(&agent->tid, NULL, agent_start, agent);
548 return JS_UNDEFINED;
549 }
550
551 static void js_agent_free(JSContext *ctx)
552 {
553 struct list_head *el, *el1;
554 Test262Agent *agent;
555
556 list_for_each_safe(el, el1, &agent_list) {
557 agent = list_entry(el, Test262Agent, link);
558 pthread_join(agent->tid, NULL);
559 JS_FreeValue(ctx, agent->broadcast_sab);
560 list_del(&agent->link);
561 free(agent);
562 }
563 }
564
565 static JSValue js_agent_leaving(JSContext *ctx, JSValue this_val,
566 int argc, JSValue *argv)
567 {
568 Test262Agent *agent = JS_GetContextOpaque(ctx);
569 if (!agent)
570 return JS_ThrowTypeError(ctx, "must be called inside an agent");
571 /* nothing to do */
572 return JS_UNDEFINED;
573 }
574
575 static BOOL is_broadcast_pending(void)
576 {
577 struct list_head *el;
578 Test262Agent *agent;
579 list_for_each(el, &agent_list) {
580 agent = list_entry(el, Test262Agent, link);
581 if (agent->broadcast_pending)
582 return TRUE;
583 }
584 return FALSE;
585 }
586
587 static JSValue js_agent_broadcast(JSContext *ctx, JSValue this_val,
588 int argc, JSValue *argv)
589 {
590 JSValueConst sab = argv[0];
591 struct list_head *el;
592 Test262Agent *agent;
593 uint8_t *buf;
594 size_t buf_size;
595 int32_t val;
596
597 if (JS_GetContextOpaque(ctx) != NULL)
598 return JS_ThrowTypeError(ctx, "cannot be called inside an agent");
599
600 buf = JS_GetArrayBuffer(ctx, &buf_size, sab);
601 if (!buf)
602 return JS_EXCEPTION;
603 if (JS_ToInt32(ctx, &val, argv[1]))
604 return JS_EXCEPTION;
605
606 /* broadcast the values and wait until all agents have started
607 calling their callbacks */
608 pthread_mutex_lock(&agent_mutex);
609 list_for_each(el, &agent_list) {
610 agent = list_entry(el, Test262Agent, link);
611 agent->broadcast_pending = TRUE;
612 /* the shared array buffer is used by the thread, so increment
613 its refcount */
614 agent->broadcast_sab = JS_DupValue(ctx, sab);
615 agent->broadcast_sab_buf = buf;
616 agent->broadcast_sab_size = buf_size;
617 agent->broadcast_val = val;
618 }
619 pthread_cond_broadcast(&agent_cond);
620
621 while (is_broadcast_pending()) {
622 pthread_cond_wait(&agent_cond, &agent_mutex);
623 }
624 pthread_mutex_unlock(&agent_mutex);
625 return JS_UNDEFINED;
626 }
627
628 static JSValue js_agent_receiveBroadcast(JSContext *ctx, JSValue this_val,
629 int argc, JSValue *argv)
630 {
631 Test262Agent *agent = JS_GetContextOpaque(ctx);
632 if (!agent)
633 return JS_ThrowTypeError(ctx, "must be called inside an agent");
634 if (!JS_IsFunction(ctx, argv[0]))
635 return JS_ThrowTypeError(ctx, "expecting function");
636 JS_FreeValue(ctx, agent->broadcast_func);
637 agent->broadcast_func = JS_DupValue(ctx, argv[0]);
638 return JS_UNDEFINED;
639 }
640
641 static JSValue js_agent_sleep(JSContext *ctx, JSValue this_val,
642 int argc, JSValue *argv)
643 {
644 uint32_t duration;
645 if (JS_ToUint32(ctx, &duration, argv[0]))
646 return JS_EXCEPTION;
647 usleep(duration * 1000);
648 return JS_UNDEFINED;
649 }
650
651 static int64_t get_clock_ms(void)
652 {
653 struct timespec ts;
654 clock_gettime(CLOCK_MONOTONIC, &ts);
655 return (uint64_t)ts.tv_sec * 1000 + (ts.tv_nsec / 1000000);
656 }
657
658 static JSValue js_agent_monotonicNow(JSContext *ctx, JSValue this_val,
659 int argc, JSValue *argv)
660 {
661 return JS_NewInt64(ctx, get_clock_ms());
662 }
663
664 static JSValue js_agent_getReport(JSContext *ctx, JSValue this_val,
665 int argc, JSValue *argv)
666 {
667 AgentReport *rep;
668 JSValue ret;
669
670 pthread_mutex_lock(&report_mutex);
671 if (list_empty(&report_list)) {
672 rep = NULL;
673 } else {
674 rep = list_entry(report_list.next, AgentReport, link);
675 list_del(&rep->link);
676 }
677 pthread_mutex_unlock(&report_mutex);
678 if (rep) {
679 ret = JS_NewString(ctx, rep->str);
680 free(rep->str);
681 free(rep);
682 } else {
683 ret = JS_NULL;
684 }
685 return ret;
686 }
687
688 static JSValue js_agent_report(JSContext *ctx, JSValue this_val,
689 int argc, JSValue *argv)
690 {
691 const char *str;
692 AgentReport *rep;
693
694 str = JS_ToCString(ctx, argv[0]);
695 if (!str)
696 return JS_EXCEPTION;
697 rep = malloc(sizeof(*rep));
698 rep->str = strdup(str);
699 JS_FreeCString(ctx, str);
700
701 pthread_mutex_lock(&report_mutex);
702 list_add_tail(&rep->link, &report_list);
703 pthread_mutex_unlock(&report_mutex);
704 return JS_UNDEFINED;
705 }
706
707 static const JSCFunctionListEntry js_agent_funcs[] = {
708 /* only in main */
709 JS_CFUNC_DEF("start", 1, js_agent_start ),
710 JS_CFUNC_DEF("getReport", 0, js_agent_getReport ),
711 JS_CFUNC_DEF("broadcast", 2, js_agent_broadcast ),
712 /* only in agent */
713 JS_CFUNC_DEF("report", 1, js_agent_report ),
714 JS_CFUNC_DEF("leaving", 0, js_agent_leaving ),
715 JS_CFUNC_DEF("receiveBroadcast", 1, js_agent_receiveBroadcast ),
716 /* in both */
717 JS_CFUNC_DEF("sleep", 1, js_agent_sleep ),
718 JS_CFUNC_DEF("monotonicNow", 0, js_agent_monotonicNow ),
719 };
720
721 static JSValue js_new_agent(JSContext *ctx)
722 {
723 JSValue agent;
724 agent = JS_NewObject(ctx);
725 JS_SetPropertyFunctionList(ctx, agent, js_agent_funcs,
726 countof(js_agent_funcs));
727 return agent;
728 }
729 #endif
730
731 static JSValue js_createRealm(JSContext *ctx, JSValue this_val,
732 int argc, JSValue *argv)
733 {
734 JSContext *ctx1;
735 JSValue ret;
736
737 ctx1 = JS_NewContext(JS_GetRuntime(ctx));
738 if (!ctx1)
739 return JS_ThrowOutOfMemory(ctx);
740 ret = add_helpers1(ctx1);
741 /* ctx1 has a refcount so it stays alive */
742 JS_FreeContext(ctx1);
743 return ret;
744 }
745
746 static JSValue js_IsHTMLDDA(JSContext *ctx, JSValue this_val,
747 int argc, JSValue *argv)
748 {
749 return JS_NULL;
750 }
751
752 static JSValue add_helpers1(JSContext *ctx)
753 {
754 JSValue global_obj;
755 JSValue obj262, obj;
756
757 global_obj = JS_GetGlobalObject(ctx);
758
759 JS_SetPropertyStr(ctx, global_obj, "print",
760 JS_NewCFunction(ctx, js_print, "print", 1));
761
762 /* $262 special object used by the tests */
763 obj262 = JS_NewObject(ctx);
764 JS_SetPropertyStr(ctx, obj262, "detachArrayBuffer",
765 JS_NewCFunction(ctx, js_detachArrayBuffer,
766 "detachArrayBuffer", 1));
767 JS_SetPropertyStr(ctx, obj262, "evalScript",
768 JS_NewCFunction(ctx, js_evalScript,
769 "evalScript", 1));
770 JS_SetPropertyStr(ctx, obj262, "codePointRange",
771 JS_NewCFunction(ctx, js_string_codePointRange,
772 "codePointRange", 2));
773 #ifdef CONFIG_AGENT
774 JS_SetPropertyStr(ctx, obj262, "agent", js_new_agent(ctx));
775 #endif
776
777 JS_SetPropertyStr(ctx, obj262, "global",
778 JS_DupValue(ctx, global_obj));
779 JS_SetPropertyStr(ctx, obj262, "createRealm",
780 JS_NewCFunction(ctx, js_createRealm,
781 "createRealm", 0));
782 obj = JS_NewCFunction(ctx, js_IsHTMLDDA, "IsHTMLDDA", 0);
783 JS_SetIsHTMLDDA(ctx, obj);
784 JS_SetPropertyStr(ctx, obj262, "IsHTMLDDA", obj);
785
786 JS_SetPropertyStr(ctx, global_obj, "$262", JS_DupValue(ctx, obj262));
787
788 JS_FreeValue(ctx, global_obj);
789 return obj262;
790 }
791
792 static void add_helpers(JSContext *ctx)
793 {
794 JS_FreeValue(ctx, add_helpers1(ctx));
795 }
796
797 static char *load_file(const char *filename, size_t *lenp)
798 {
799 char *buf;
800 size_t buf_len;
801 buf = (char *)js_load_file(NULL, &buf_len, filename);
802 if (!buf)
803 perror_exit(1, filename);
804 if (lenp)
805 *lenp = buf_len;
806 return buf;
807 }
808
809 static JSModuleDef *js_module_loader_test(JSContext *ctx,
810 const char *module_name, void *opaque)
811 {
812 size_t buf_len;
813 uint8_t *buf;
814 JSModuleDef *m;
815 JSValue func_val;
816
817 buf = js_load_file(ctx, &buf_len, module_name);
818 if (!buf) {
819 JS_ThrowReferenceError(ctx, "could not load module filename '%s'",
820 module_name);
821 return NULL;
822 }
823
824 /* compile the module */
825 func_val = JS_Eval(ctx, (char *)buf, buf_len, module_name,
826 JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
827 js_free(ctx, buf);
828 if (JS_IsException(func_val))
829 return NULL;
830 /* the module is already referenced, so we must free it */
831 m = JS_VALUE_GET_PTR(func_val);
832 JS_FreeValue(ctx, func_val);
833 return m;
834 }
835
836 int is_line_sep(char c)
837 {
838 return (c == '\0' || c == '\n' || c == '\r');
839 }
840
841 char *find_line(const char *str, const char *line)
842 {
843 if (str) {
844 const char *p;
845 int len = strlen(line);
846 for (p = str; (p = strstr(p, line)) != NULL; p += len + 1) {
847 if ((p == str || is_line_sep(p[-1])) && is_line_sep(p[len]))
848 return (char *)p;
849 }
850 }
851 return NULL;
852 }
853
854 int is_word_sep(char c)
855 {
856 return (c == '\0' || isspace((unsigned char)c) || c == ',');
857 }
858
859 char *find_word(const char *str, const char *word)
860 {
861 const char *p;
862 int len = strlen(word);
863 if (str && len) {
864 for (p = str; (p = strstr(p, word)) != NULL; p += len) {
865 if ((p == str || is_word_sep(p[-1])) && is_word_sep(p[len]))
866 return (char *)p;
867 }
868 }
869 return NULL;
870 }
871
872 /* handle exclude directories */
873 void update_exclude_dirs(void)
874 {
875 namelist_t *lp = &test_list;
876 namelist_t *ep = &exclude_list;
877 namelist_t *dp = &exclude_dir_list;
878 char *name;
879 int i, j, count;
880
881 /* split directpries from exclude_list */
882 for (count = i = 0; i < ep->count; i++) {
883 name = ep->array[i];
884 if (has_suffix(name, "/")) {
885 namelist_add(dp, NULL, name);
886 free(name);
887 } else {
888 ep->array[count++] = name;
889 }
890 }
891 ep->count = count;
892
893 namelist_sort(dp);
894
895 /* filter out excluded directories */
896 for (count = i = 0; i < lp->count; i++) {
897 name = lp->array[i];
898 for (j = 0; j < dp->count; j++) {
899 if (has_prefix(name, dp->array[j])) {
900 test_excluded++;
901 free(name);
902 name = NULL;
903 break;
904 }
905 }
906 if (name) {
907 lp->array[count++] = name;
908 }
909 }
910 lp->count = count;
911 }
912
913 void load_config(const char *filename)
914 {
915 char buf[1024];
916 FILE *f;
917 char *base_name;
918 enum {
919 SECTION_NONE = 0,
920 SECTION_CONFIG,
921 SECTION_EXCLUDE,
922 SECTION_FEATURES,
923 SECTION_TESTS,
924 } section = SECTION_NONE;
925 int lineno = 0;
926
927 f = fopen(filename, "rb");
928 if (!f) {
929 perror_exit(1, filename);
930 }
931 base_name = get_basename(filename);
932
933 while (fgets(buf, sizeof(buf), f) != NULL) {
934 char *p, *q;
935 lineno++;
936 p = str_strip(buf);
937 if (*p == '#' || *p == ';' || *p == '\0')
938 continue; /* line comment */
939
940 if (*p == "[]"[0]) {
941 /* new section */
942 p++;
943 p[strcspn(p, "]")] = '\0';
944 if (str_equal(p, "config"))
945 section = SECTION_CONFIG;
946 else if (str_equal(p, "exclude"))
947 section = SECTION_EXCLUDE;
948 else if (str_equal(p, "features"))
949 section = SECTION_FEATURES;
950 else if (str_equal(p, "tests"))
951 section = SECTION_TESTS;
952 else
953 section = SECTION_NONE;
954 continue;
955 }
956 q = strchr(p, '=');
957 if (q) {
958 /* setting: name=value */
959 *q++ = '\0';
960 q = str_strip(q);
961 }
962 switch (section) {
963 case SECTION_CONFIG:
964 if (!q) {
965 printf("%s:%d: syntax error\n", filename, lineno);
966 continue;
967 }
968 if (str_equal(p, "style")) {
969 new_style = str_equal(q, "new");
970 continue;
971 }
972 if (str_equal(p, "testdir")) {
973 char *testdir = compose_path(base_name, q);
974 enumerate_tests(testdir);
975 free(testdir);
976 continue;
977 }
978 if (str_equal(p, "harnessdir")) {
979 harness_dir = compose_path(base_name, q);
980 continue;
981 }
982 if (str_equal(p, "harnessexclude")) {
983 str_append(&harness_exclude, " ", q);
984 continue;
985 }
986 if (str_equal(p, "features")) {
987 str_append(&harness_features, " ", q);
988 continue;
989 }
990 if (str_equal(p, "skip-features")) {
991 str_append(&harness_skip_features, " ", q);
992 continue;
993 }
994 if (str_equal(p, "mode")) {
995 if (str_equal(q, "default") || str_equal(q, "default-nostrict"))
996 test_mode = TEST_DEFAULT_NOSTRICT;
997 else if (str_equal(q, "default-strict"))
998 test_mode = TEST_DEFAULT_STRICT;
999 else if (str_equal(q, "nostrict"))
1000 test_mode = TEST_NOSTRICT;
1001 else if (str_equal(q, "strict"))
1002 test_mode = TEST_STRICT;
1003 else if (str_equal(q, "all") || str_equal(q, "both"))
1004 test_mode = TEST_ALL;
1005 else
1006 fatal(2, "unknown test mode: %s", q);
1007 continue;
1008 }
1009 if (str_equal(p, "strict")) {
1010 if (str_equal(q, "skip") || str_equal(q, "no"))
1011 test_mode = TEST_NOSTRICT;
1012 continue;
1013 }
1014 if (str_equal(p, "nostrict")) {
1015 if (str_equal(q, "skip") || str_equal(q, "no"))
1016 test_mode = TEST_STRICT;
1017 continue;
1018 }
1019 if (str_equal(p, "async")) {
1020 skip_async = !str_equal(q, "yes");
1021 continue;
1022 }
1023 if (str_equal(p, "module")) {
1024 skip_module = !str_equal(q, "yes");
1025 continue;
1026 }
1027 if (str_equal(p, "verbose")) {
1028 verbose = str_equal(q, "yes");
1029 continue;
1030 }
1031 if (str_equal(p, "errorfile")) {
1032 error_filename = compose_path(base_name, q);
1033 continue;
1034 }
1035 if (str_equal(p, "excludefile")) {
1036 char *path = compose_path(base_name, q);
1037 namelist_load(&exclude_list, path);
1038 free(path);
1039 continue;
1040 }
1041 if (str_equal(p, "reportfile")) {
1042 report_filename = compose_path(base_name, q);
1043 continue;
1044 }
1045 case SECTION_EXCLUDE:
1046 namelist_add(&exclude_list, base_name, p);
1047 break;
1048 case SECTION_FEATURES:
1049 if (!q || str_equal(q, "yes"))
1050 str_append(&harness_features, " ", p);
1051 else
1052 str_append(&harness_skip_features, " ", p);
1053 break;
1054 case SECTION_TESTS:
1055 namelist_add(&test_list, base_name, p);
1056 break;
1057 default:
1058 /* ignore settings in other sections */
1059 break;
1060 }
1061 }
1062 fclose(f);
1063 free(base_name);
1064 }
1065
1066 char *find_error(const char *filename, int *pline, int is_strict)
1067 {
1068 if (error_file) {
1069 size_t len = strlen(filename);
1070 const char *p, *q, *r;
1071 int line;
1072
1073 for (p = error_file; (p = strstr(p, filename)) != NULL; p += len) {
1074 if ((p == error_file || p[-1] == '\n' || p[-1] == '(') && p[len] == ':') {
1075 q = p + len;
1076 line = 1;
1077 if (*q == ':') {
1078 line = strtol(q + 1, (char**)&q, 10);
1079 if (*q == ':')
1080 q++;
1081 }
1082 while (*q == ' ') {
1083 q++;
1084 }
1085 /* check strict mode indicator */
1086 if (!strstart(q, "strict mode: ", &q) != !is_strict)
1087 continue;
1088 r = q = skip_prefix(q, "unexpected error: ");
1089 r += strcspn(r, "\n");
1090 while (r[0] == '\n' && r[1] && strncmp(r + 1, filename, 8)) {
1091 r++;
1092 r += strcspn(r, "\n");
1093 }
1094 if (pline)
1095 *pline = line;
1096 return strdup_len(q, r - q);
1097 }
1098 }
1099 }
1100 return NULL;
1101 }
1102
1103 int skip_comments(const char *str, int line, int *pline)
1104 {
1105 const char *p;
1106 int c;
1107
1108 p = str;
1109 while ((c = (unsigned char)*p++) != '\0') {
1110 if (isspace(c)) {
1111 if (c == '\n')
1112 line++;
1113 continue;
1114 }
1115 if (c == '/' && *p == '/') {
1116 while (*++p && *p != '\n')
1117 continue;
1118 continue;
1119 }
1120 if (c == '/' && *p == '*') {
1121 for (p += 1; *p; p++) {
1122 if (*p == '\n') {
1123 line++;
1124 continue;
1125 }
1126 if (*p == '*' && p[1] == '/') {
1127 p += 2;
1128 break;
1129 }
1130 }
1131 continue;
1132 }
1133 break;
1134 }
1135 if (pline)
1136 *pline = line;
1137
1138 return p - str;
1139 }
1140
1141 int longest_match(const char *str, const char *find, int pos, int *ppos, int line, int *pline)
1142 {
1143 int len, maxlen;
1144
1145 maxlen = 0;
1146
1147 if (*find) {
1148 const char *p;
1149 for (p = str + pos; *p; p++) {
1150 if (*p == *find) {
1151 for (len = 1; p[len] && p[len] == find[len]; len++)
1152 continue;
1153 if (len > maxlen) {
1154 maxlen = len;
1155 if (ppos)
1156 *ppos = p - str;
1157 if (pline)
1158 *pline = line;
1159 if (!find[len])
1160 break;
1161 }
1162 }
1163 if (*p == '\n')
1164 line++;
1165 }
1166 }
1167 return maxlen;
1168 }
1169
1170 static int eval_buf(JSContext *ctx, const char *buf, size_t buf_len,
1171 const char *filename, int is_test, int is_negative,
1172 const char *error_type, FILE *outfile, int eval_flags,
1173 int is_async)
1174 {
1175 JSValue res_val, exception_val;
1176 int ret, error_line, pos, pos_line;
1177 BOOL is_error, has_error_line, ret_promise;
1178 const char *error_name;
1179
1180 pos = skip_comments(buf, 1, &pos_line);
1181 error_line = pos_line;
1182 has_error_line = FALSE;
1183 exception_val = JS_UNDEFINED;
1184 error_name = NULL;
1185
1186 /* a module evaluation returns a promise */
1187 ret_promise = ((eval_flags & JS_EVAL_TYPE_MODULE) != 0);
1188 async_done = 0; /* counter of "Test262:AsyncTestComplete" messages */
1189
1190 res_val = JS_Eval(ctx, buf, buf_len, filename, eval_flags);
1191
1192 if ((is_async || ret_promise) && !JS_IsException(res_val)) {
1193 JSValue promise = JS_UNDEFINED;
1194 if (ret_promise) {
1195 promise = res_val;
1196 } else {
1197 JS_FreeValue(ctx, res_val);
1198 }
1199 for(;;) {
1200 JSContext *ctx1;
1201 ret = JS_ExecutePendingJob(JS_GetRuntime(ctx), &ctx1);
1202 if (ret < 0) {
1203 res_val = JS_EXCEPTION;
1204 break;
1205 } else if (ret == 0) {
1206 if (is_async) {
1207 /* test if the test called $DONE() once */
1208 if (async_done != 1) {
1209 res_val = JS_ThrowTypeError(ctx, "$DONE() not called");
1210 } else {
1211 res_val = JS_UNDEFINED;
1212 }
1213 } else {
1214 /* check that the returned promise is fulfilled */
1215 JSPromiseStateEnum state = JS_PromiseState(ctx, promise);
1216 if (state == JS_PROMISE_FULFILLED)
1217 res_val = JS_UNDEFINED;
1218 else if (state == JS_PROMISE_REJECTED)
1219 res_val = JS_Throw(ctx, JS_PromiseResult(ctx, promise));
1220 else
1221 res_val = JS_ThrowTypeError(ctx, "promise is pending");
1222 }
1223 break;
1224 }
1225 }
1226 JS_FreeValue(ctx, promise);
1227 }
1228
1229 if (JS_IsException(res_val)) {
1230 exception_val = JS_GetException(ctx);
1231 is_error = JS_IsError(ctx, exception_val);
1232 /* XXX: should get the filename and line number */
1233 if (outfile) {
1234 if (!is_error)
1235 fprintf(outfile, "%sThrow: ", (eval_flags & JS_EVAL_FLAG_STRICT) ?
1236 "strict mode: " : "");
1237 js_print(ctx, JS_NULL, 1, &exception_val);
1238 }
1239 if (is_error) {
1240 JSValue name, stack;
1241 const char *stack_str;
1242
1243 name = JS_GetPropertyStr(ctx, exception_val, "name");
1244 error_name = JS_ToCString(ctx, name);
1245 stack = JS_GetPropertyStr(ctx, exception_val, "stack");
1246 if (!JS_IsUndefined(stack)) {
1247 stack_str = JS_ToCString(ctx, stack);
1248 if (stack_str) {
1249 const char *p;
1250 int len;
1251
1252 if (outfile)
1253 fprintf(outfile, "%s", stack_str);
1254
1255 len = strlen(filename);
1256 p = strstr(stack_str, filename);
1257 if (p != NULL && p[len] == ':') {
1258 error_line = atoi(p + len + 1);
1259 has_error_line = TRUE;
1260 }
1261 JS_FreeCString(ctx, stack_str);
1262 }
1263 }
1264 JS_FreeValue(ctx, stack);
1265 JS_FreeValue(ctx, name);
1266 }
1267 if (is_negative) {
1268 ret = 0;
1269 if (error_type) {
1270 char *error_class;
1271 const char *msg;
1272
1273 msg = JS_ToCString(ctx, exception_val);
1274 error_class = strdup_len(msg, strcspn(msg, ":"));
1275 if (!str_equal(error_class, error_type))
1276 ret = -1;
1277 free(error_class);
1278 JS_FreeCString(ctx, msg);
1279 }
1280 } else {
1281 ret = -1;
1282 }
1283 } else {
1284 if (is_negative)
1285 ret = -1;
1286 else
1287 ret = 0;
1288 }
1289
1290 if (verbose && is_test) {
1291 JSValue msg_val = JS_UNDEFINED;
1292 const char *msg = NULL;
1293 int s_line;
1294 char *s = find_error(filename, &s_line, eval_flags & JS_EVAL_FLAG_STRICT);
1295 const char *strict_mode = (eval_flags & JS_EVAL_FLAG_STRICT) ? "strict mode: " : "";
1296
1297 if (!JS_IsUndefined(exception_val)) {
1298 msg_val = JS_ToString(ctx, exception_val);
1299 msg = JS_ToCString(ctx, msg_val);
1300 }
1301 if (is_negative) { // expect error
1302 if (ret == 0) {
1303 if (msg && s &&
1304 (str_equal(s, "expected error") ||
1305 strstart(s, "unexpected error type:", NULL) ||
1306 str_equal(s, msg))) { // did not have error yet
1307 if (!has_error_line) {
1308 longest_match(buf, msg, pos, &pos, pos_line, &error_line);
1309 }
1310 printf("%s:%d: %sOK, now has error %s\n",
1311 filename, error_line, strict_mode, msg);
1312 fixed_errors++;
1313 }
1314 } else {
1315 if (!s) { // not yet reported
1316 if (msg) {
1317 fprintf(error_out, "%s:%d: %sunexpected error type: %s\n",
1318 filename, error_line, strict_mode, msg);
1319 } else {
1320 fprintf(error_out, "%s:%d: %sexpected error\n",
1321 filename, error_line, strict_mode);
1322 }
1323 new_errors++;
1324 }
1325 }
1326 } else { // should not have error
1327 if (msg) {
1328 if (!s || !str_equal(s, msg)) {
1329 if (!has_error_line) {
1330 char *p = skip_prefix(msg, "Test262 Error: ");
1331 if (strstr(p, "Test case returned non-true value!")) {
1332 longest_match(buf, "runTestCase", pos, &pos, pos_line, &error_line);
1333 } else {
1334 longest_match(buf, p, pos, &pos, pos_line, &error_line);
1335 }
1336 }
1337 fprintf(error_out, "%s:%d: %s%s%s\n", filename, error_line, strict_mode,
1338 error_file ? "unexpected error: " : "", msg);
1339
1340 if (s && (!str_equal(s, msg) || error_line != s_line)) {
1341 printf("%s:%d: %sprevious error: %s\n", filename, s_line, strict_mode, s);
1342 changed_errors++;
1343 } else {
1344 new_errors++;
1345 }
1346 }
1347 } else {
1348 if (s) {
1349 printf("%s:%d: %sOK, fixed error: %s\n", filename, s_line, strict_mode, s);
1350 fixed_errors++;
1351 }
1352 }
1353 }
1354 JS_FreeValue(ctx, msg_val);
1355 JS_FreeCString(ctx, msg);
1356 free(s);
1357 }
1358 JS_FreeCString(ctx, error_name);
1359 JS_FreeValue(ctx, exception_val);
1360 JS_FreeValue(ctx, res_val);
1361 return ret;
1362 }
1363
1364 static int eval_file(JSContext *ctx, const char *base, const char *p,
1365 int eval_flags)
1366 {
1367 char *buf;
1368 size_t buf_len;
1369 char *filename = compose_path(base, p);
1370
1371 buf = load_file(filename, &buf_len);
1372 if (!buf) {
1373 warning("cannot load %s", filename);
1374 goto fail;
1375 }
1376 if (eval_buf(ctx, buf, buf_len, filename, FALSE, FALSE, NULL, stderr,
1377 eval_flags, FALSE)) {
1378 warning("error evaluating %s", filename);
1379 goto fail;
1380 }
1381 free(buf);
1382 free(filename);
1383 return 0;
1384
1385 fail:
1386 free(buf);
1387 free(filename);
1388 return 1;
1389 }
1390
1391 char *extract_desc(const char *buf, char style)
1392 {
1393 const char *p, *desc_start;
1394 char *desc;
1395 int len;
1396
1397 p = buf;
1398 while (*p != '\0') {
1399 if (p[0] == '/' && p[1] == '*' && p[2] == style && p[3] != '/') {
1400 p += 3;
1401 desc_start = p;
1402 while (*p != '\0' && (p[0] != '*' || p[1] != '/'))
1403 p++;
1404 if (*p == '\0') {
1405 warning("Expecting end of desc comment");
1406 return NULL;
1407 }
1408 len = p - desc_start;
1409 desc = malloc(len + 1);
1410 memcpy(desc, desc_start, len);
1411 desc[len] = '\0';
1412 return desc;
1413 } else {
1414 p++;
1415 }
1416 }
1417 return NULL;
1418 }
1419
1420 static char *find_tag(char *desc, const char *tag, int *state)
1421 {
1422 char *p;
1423 p = strstr(desc, tag);
1424 if (p) {
1425 p += strlen(tag);
1426 *state = 0;
1427 }
1428 return p;
1429 }
1430
1431 static char *get_option(char **pp, int *state)
1432 {
1433 char *p, *p0, *option = NULL;
1434 if (*pp) {
1435 for (p = *pp;; p++) {
1436 switch (*p) {
1437 case '[':
1438 *state += 1;
1439 continue;
1440 case ']':
1441 *state -= 1;
1442 if (*state > 0)
1443 continue;
1444 p = NULL;
1445 break;
1446 case ' ':
1447 case '\t':
1448 case '\r':
1449 case ',':
1450 case '-':
1451 continue;
1452 case '\n':
1453 if (*state > 0 || p[1] == ' ')
1454 continue;
1455 p = NULL;
1456 break;
1457 case '\0':
1458 p = NULL;
1459 break;
1460 default:
1461 p0 = p;
1462 p += strcspn(p0, " \t\r\n,]");
1463 option = strdup_len(p0, p - p0);
1464 break;
1465 }
1466 break;
1467 }
1468 *pp = p;
1469 }
1470 return option;
1471 }
1472
1473 void update_stats(JSRuntime *rt, const char *filename) {
1474 JSMemoryUsage stats;
1475 JS_ComputeMemoryUsage(rt, &stats);
1476 if (stats_count++ == 0) {
1477 stats_avg = stats_all = stats_min = stats_max = stats;
1478 stats_min_filename = strdup(filename);
1479 stats_max_filename = strdup(filename);
1480 } else {
1481 if (stats_max.malloc_size < stats.malloc_size) {
1482 stats_max = stats;
1483 free(stats_max_filename);
1484 stats_max_filename = strdup(filename);
1485 }
1486 if (stats_min.malloc_size > stats.malloc_size) {
1487 stats_min = stats;
1488 free(stats_min_filename);
1489 stats_min_filename = strdup(filename);
1490 }
1491
1492 #define update(f) stats_avg.f = (stats_all.f += stats.f) / stats_count
1493 update(malloc_count);
1494 update(malloc_size);
1495 update(memory_used_count);
1496 update(memory_used_size);
1497 update(atom_count);
1498 update(atom_size);
1499 update(str_count);
1500 update(str_size);
1501 update(obj_count);
1502 update(obj_size);
1503 update(prop_count);
1504 update(prop_size);
1505 update(shape_count);
1506 update(shape_size);
1507 update(js_func_count);
1508 update(js_func_size);
1509 update(js_func_code_size);
1510 update(js_func_pc2line_count);
1511 update(js_func_pc2line_size);
1512 update(c_func_count);
1513 update(array_count);
1514 update(fast_array_count);
1515 update(fast_array_elements);
1516 }
1517 #undef update
1518 }
1519
1520 int run_test_buf(const char *filename, const char *harness, namelist_t *ip,
1521 char *buf, size_t buf_len, const char* error_type,
1522 int eval_flags, BOOL is_negative, BOOL is_async,
1523 BOOL can_block)
1524 {
1525 JSRuntime *rt;
1526 JSContext *ctx;
1527 int i, ret;
1528
1529 rt = JS_NewRuntime();
1530 if (rt == NULL) {
1531 fatal(1, "JS_NewRuntime failure");
1532 }
1533 ctx = JS_NewContext(rt);
1534 if (ctx == NULL) {
1535 JS_FreeRuntime(rt);
1536 fatal(1, "JS_NewContext failure");
1537 }
1538 JS_SetRuntimeInfo(rt, filename);
1539
1540 JS_SetCanBlock(rt, can_block);
1541
1542 /* loader for ES6 modules */
1543 JS_SetModuleLoaderFunc(rt, NULL, js_module_loader_test, NULL);
1544
1545 add_helpers(ctx);
1546
1547 for (i = 0; i < ip->count; i++) {
1548 if (eval_file(ctx, harness, ip->array[i],
1549 JS_EVAL_TYPE_GLOBAL | JS_EVAL_FLAG_STRIP)) {
1550 fatal(1, "error including %s for %s", ip->array[i], filename);
1551 }
1552 }
1553
1554 ret = eval_buf(ctx, buf, buf_len, filename, TRUE, is_negative,
1555 error_type, outfile, eval_flags, is_async);
1556 ret = (ret != 0);
1557
1558 if (dump_memory) {
1559 update_stats(rt, filename);
1560 }
1561 #ifdef CONFIG_AGENT
1562 js_agent_free(ctx);
1563 #endif
1564 JS_FreeContext(ctx);
1565 JS_FreeRuntime(rt);
1566
1567 test_count++;
1568 if (ret) {
1569 test_failed++;
1570 if (outfile) {
1571 /* do not output a failure number to minimize diff */
1572 fprintf(outfile, " FAILED\n");
1573 }
1574 }
1575 return ret;
1576 }
1577
1578 int run_test(const char *filename, int index)
1579 {
1580 char harnessbuf[1024];
1581 char *harness;
1582 char *buf;
1583 size_t buf_len;
1584 char *desc, *p;
1585 char *error_type;
1586 int ret, eval_flags, use_strict, use_nostrict;
1587 BOOL is_negative, is_nostrict, is_onlystrict, is_async, is_module, skip;
1588 BOOL can_block;
1589 namelist_t include_list = { 0 }, *ip = &include_list;
1590
1591 is_nostrict = is_onlystrict = is_negative = is_async = is_module = skip = FALSE;
1592 can_block = TRUE;
1593 error_type = NULL;
1594 buf = load_file(filename, &buf_len);
1595
1596 harness = harness_dir;
1597
1598 if (new_style) {
1599 if (!harness) {
1600 p = strstr(filename, "test/");
1601 if (p) {
1602 snprintf(harnessbuf, sizeof(harnessbuf), "%.*s%s",
1603 (int)(p - filename), filename, "harness");
1604 } else {
1605 pstrcpy(harnessbuf, sizeof(harnessbuf), "");
1606 }
1607 harness = harnessbuf;
1608 }
1609 namelist_add(ip, NULL, "sta.js");
1610 namelist_add(ip, NULL, "assert.js");
1611 /* extract the YAML frontmatter */
1612 desc = extract_desc(buf, '-');
1613 if (desc) {
1614 char *ifile, *option;
1615 int state;
1616 p = find_tag(desc, "includes:", &state);
1617 if (p) {
1618 while ((ifile = get_option(&p, &state)) != NULL) {
1619 // skip unsupported harness files
1620 if (find_word(harness_exclude, ifile)) {
1621 skip |= 1;
1622 } else {
1623 namelist_add(ip, NULL, ifile);
1624 }
1625 free(ifile);
1626 }
1627 }
1628 p = find_tag(desc, "flags:", &state);
1629 if (p) {
1630 while ((option = get_option(&p, &state)) != NULL) {
1631 if (str_equal(option, "noStrict") ||
1632 str_equal(option, "raw")) {
1633 is_nostrict = TRUE;
1634 skip |= (test_mode == TEST_STRICT);
1635 }
1636 else if (str_equal(option, "onlyStrict")) {
1637 is_onlystrict = TRUE;
1638 skip |= (test_mode == TEST_NOSTRICT);
1639 }
1640 else if (str_equal(option, "async")) {
1641 is_async = TRUE;
1642 skip |= skip_async;
1643 }
1644 else if (str_equal(option, "module")) {
1645 is_module = TRUE;
1646 skip |= skip_module;
1647 }
1648 else if (str_equal(option, "CanBlockIsFalse")) {
1649 can_block = FALSE;
1650 }
1651 free(option);
1652 }
1653 }
1654 p = find_tag(desc, "negative:", &state);
1655 if (p) {
1656 /* XXX: should extract the phase */
1657 char *q = find_tag(p, "type:", &state);
1658 if (q) {
1659 while (isspace(*q))
1660 q++;
1661 error_type = strdup_len(q, strcspn(q, " \n"));
1662 }
1663 is_negative = TRUE;
1664 }
1665 p = find_tag(desc, "features:", &state);
1666 if (p) {
1667 while ((option = get_option(&p, &state)) != NULL) {
1668 if (find_word(harness_features, option)) {
1669 /* feature is enabled */
1670 } else if (find_word(harness_skip_features, option)) {
1671 /* skip disabled feature */
1672 skip |= 1;
1673 } else {
1674 /* feature is not listed: skip and warn */
1675 printf("%s:%d: unknown feature: %s\n", filename, 1, option);
1676 skip |= 1;
1677 }
1678 free(option);
1679 }
1680 }
1681 free(desc);
1682 }
1683 if (is_async)
1684 namelist_add(ip, NULL, "doneprintHandle.js");
1685 } else {
1686 char *ifile;
1687
1688 if (!harness) {
1689 p = strstr(filename, "test/");
1690 if (p) {
1691 snprintf(harnessbuf, sizeof(harnessbuf), "%.*s%s",
1692 (int)(p - filename), filename, "test/harness");
1693 } else {
1694 pstrcpy(harnessbuf, sizeof(harnessbuf), "");
1695 }
1696 harness = harnessbuf;
1697 }
1698
1699 namelist_add(ip, NULL, "sta.js");
1700
1701 /* include extra harness files */
1702 for (p = buf; (p = strstr(p, "$INCLUDE(\"")) != NULL; p++) {
1703 p += 10;
1704 ifile = strdup_len(p, strcspn(p, "\""));
1705 // skip unsupported harness files
1706 if (find_word(harness_exclude, ifile)) {
1707 skip |= 1;
1708 } else {
1709 namelist_add(ip, NULL, ifile);
1710 }
1711 free(ifile);
1712 }
1713
1714 /* locate the old style configuration comment */
1715 desc = extract_desc(buf, '*');
1716 if (desc) {
1717 if (strstr(desc, "@noStrict")) {
1718 is_nostrict = TRUE;
1719 skip |= (test_mode == TEST_STRICT);
1720 }
1721 if (strstr(desc, "@onlyStrict")) {
1722 is_onlystrict = TRUE;
1723 skip |= (test_mode == TEST_NOSTRICT);
1724 }
1725 if (strstr(desc, "@negative")) {
1726 /* XXX: should extract the regex to check error type */
1727 is_negative = TRUE;
1728 }
1729 free(desc);
1730 }
1731 }
1732
1733 if (outfile && index >= 0) {
1734 fprintf(outfile, "%d: %s%s%s%s%s%s%s\n", index, filename,
1735 is_nostrict ? " @noStrict" : "",
1736 is_onlystrict ? " @onlyStrict" : "",
1737 is_async ? " async" : "",
1738 is_module ? " module" : "",
1739 is_negative ? " @negative" : "",
1740 skip ? " SKIPPED" : "");
1741 fflush(outfile);
1742 }
1743
1744 use_strict = use_nostrict = 0;
1745 /* XXX: should remove 'test_mode' or simplify it just to force
1746 strict or non strict mode for single file tests */
1747 switch (test_mode) {
1748 case TEST_DEFAULT_NOSTRICT:
1749 if (is_onlystrict)
1750 use_strict = 1;
1751 else
1752 use_nostrict = 1;
1753 break;
1754 case TEST_DEFAULT_STRICT:
1755 if (is_nostrict)
1756 use_nostrict = 1;
1757 else
1758 use_strict = 1;
1759 break;
1760 case TEST_NOSTRICT:
1761 if (!is_onlystrict)
1762 use_nostrict = 1;
1763 break;
1764 case TEST_STRICT:
1765 if (!is_nostrict)
1766 use_strict = 1;
1767 break;
1768 case TEST_ALL:
1769 if (is_module) {
1770 use_nostrict = 1;
1771 } else {
1772 if (!is_nostrict)
1773 use_strict = 1;
1774 if (!is_onlystrict)
1775 use_nostrict = 1;
1776 }
1777 break;
1778 }
1779
1780 if (skip || use_strict + use_nostrict == 0) {
1781 test_skipped++;
1782 ret = -2;
1783 } else {
1784 clock_t clocks;
1785
1786 if (is_module) {
1787 eval_flags = JS_EVAL_TYPE_MODULE;
1788 } else {
1789 eval_flags = JS_EVAL_TYPE_GLOBAL;
1790 }
1791 clocks = clock();
1792 ret = 0;
1793 if (use_nostrict) {
1794 ret = run_test_buf(filename, harness, ip, buf, buf_len,
1795 error_type, eval_flags, is_negative, is_async,
1796 can_block);
1797 }
1798 if (use_strict) {
1799 ret |= run_test_buf(filename, harness, ip, buf, buf_len,
1800 error_type, eval_flags | JS_EVAL_FLAG_STRICT,
1801 is_negative, is_async, can_block);
1802 }
1803 clocks = clock() - clocks;
1804 if (outfile && index >= 0 && clocks >= CLOCKS_PER_SEC / 10) {
1805 /* output timings for tests that take more than 100 ms */
1806 fprintf(outfile, " time: %d ms\n", (int)(clocks * 1000LL / CLOCKS_PER_SEC));
1807 }
1808 }
1809 namelist_free(&include_list);
1810 free(error_type);
1811 free(buf);
1812
1813 return ret;
1814 }
1815
1816 /* run a test when called by test262-harness+eshost */
1817 int run_test262_harness_test(const char *filename, BOOL is_module)
1818 {
1819 JSRuntime *rt;
1820 JSContext *ctx;
1821 char *buf;
1822 size_t buf_len;
1823 int eval_flags, ret_code, ret;
1824 JSValue res_val;
1825 BOOL can_block;
1826
1827 outfile = stdout; /* for js_print */
1828
1829 rt = JS_NewRuntime();
1830 if (rt == NULL) {
1831 fatal(1, "JS_NewRuntime failure");
1832 }
1833 ctx = JS_NewContext(rt);
1834 if (ctx == NULL) {
1835 JS_FreeRuntime(rt);
1836 fatal(1, "JS_NewContext failure");
1837 }
1838 JS_SetRuntimeInfo(rt, filename);
1839
1840 can_block = TRUE;
1841 JS_SetCanBlock(rt, can_block);
1842
1843 /* loader for ES6 modules */
1844 JS_SetModuleLoaderFunc(rt, NULL, js_module_loader_test, NULL);
1845
1846 add_helpers(ctx);
1847
1848 buf = load_file(filename, &buf_len);
1849
1850 if (is_module) {
1851 eval_flags = JS_EVAL_TYPE_MODULE;
1852 } else {
1853 eval_flags = JS_EVAL_TYPE_GLOBAL;
1854 }
1855 res_val = JS_Eval(ctx, buf, buf_len, filename, eval_flags);
1856 ret_code = 0;
1857 if (JS_IsException(res_val)) {
1858 js_std_dump_error(ctx);
1859 ret_code = 1;
1860 } else {
1861 JSValue promise = JS_UNDEFINED;
1862 if (is_module) {
1863 promise = res_val;
1864 } else {
1865 JS_FreeValue(ctx, res_val);
1866 }
1867 for(;;) {
1868 JSContext *ctx1;
1869 ret = JS_ExecutePendingJob(JS_GetRuntime(ctx), &ctx1);
1870 if (ret < 0) {
1871 js_std_dump_error(ctx1);
1872 ret_code = 1;
1873 } else if (ret == 0) {
1874 break;
1875 }
1876 }
1877 /* dump the error if the module returned an error. */
1878 if (is_module) {
1879 JSPromiseStateEnum state = JS_PromiseState(ctx, promise);
1880 if (state == JS_PROMISE_REJECTED) {
1881 JS_Throw(ctx, JS_PromiseResult(ctx, promise));
1882 js_std_dump_error(ctx);
1883 ret_code = 1;
1884 }
1885 }
1886 JS_FreeValue(ctx, promise);
1887 }
1888 free(buf);
1889 #ifdef CONFIG_AGENT
1890 js_agent_free(ctx);
1891 #endif
1892 JS_FreeContext(ctx);
1893 JS_FreeRuntime(rt);
1894 return ret_code;
1895 }
1896
1897 clock_t last_clock;
1898
1899 void show_progress(int force) {
1900 clock_t t = clock();
1901 if (force || !last_clock || (t - last_clock) > CLOCKS_PER_SEC / 20) {
1902 last_clock = t;
1903 /* output progress indicator: erase end of line and return to col 0 */
1904 fprintf(stderr, "%d/%d/%d\033[K\r",
1905 test_failed, test_count, test_skipped);
1906 fflush(stderr);
1907 }
1908 }
1909
1910 static int slow_test_threshold;
1911
1912 void run_test_dir_list(namelist_t *lp, int start_index, int stop_index)
1913 {
1914 int i;
1915
1916 namelist_sort(lp);
1917 for (i = 0; i < lp->count; i++) {
1918 const char *p = lp->array[i];
1919 if (namelist_find(&exclude_list, p) >= 0) {
1920 test_excluded++;
1921 } else if (test_index < start_index) {
1922 test_skipped++;
1923 } else if (stop_index >= 0 && test_index > stop_index) {
1924 test_skipped++;
1925 } else {
1926 int ti;
1927 if (slow_test_threshold != 0) {
1928 ti = get_clock_ms();
1929 } else {
1930 ti = 0;
1931 }
1932 run_test(p, test_index);
1933 if (slow_test_threshold != 0) {
1934 ti = get_clock_ms() - ti;
1935 if (ti >= slow_test_threshold)
1936 fprintf(stderr, "\n%s (%d ms)\n", p, ti);
1937 }
1938 show_progress(FALSE);
1939 }
1940 test_index++;
1941 }
1942 show_progress(TRUE);
1943 }
1944
1945 void help(void)
1946 {
1947 printf("run-test262 version " CONFIG_VERSION "\n"
1948 "usage: run-test262 [options] {-f file ... | [dir_list] [index range]}\n"
1949 "-h help\n"
1950 "-a run tests in strict and nostrict modes\n"
1951 "-m print memory usage summary\n"
1952 "-n use new style harness\n"
1953 "-N run test prepared by test262-harness+eshost\n"
1954 "-s run tests in strict mode, skip @nostrict tests\n"
1955 "-E only run tests from the error file\n"
1956 "-u update error file\n"
1957 "-v verbose: output error messages\n"
1958 "-T duration display tests taking more than 'duration' ms\n"
1959 "-c file read configuration from 'file'\n"
1960 "-d dir run all test files in directory tree 'dir'\n"
1961 "-e file load the known errors from 'file'\n"
1962 "-f file execute single test from 'file'\n"
1963 "-r file set the report file name (default=none)\n"
1964 "-x file exclude tests listed in 'file'\n");
1965 exit(1);
1966 }
1967
1968 char *get_opt_arg(const char *option, char *arg)
1969 {
1970 if (!arg) {
1971 fatal(2, "missing argument for option %s", option);
1972 }
1973 return arg;
1974 }
1975
1976 int main(int argc, char **argv)
1977 {
1978 int optind, start_index, stop_index;
1979 BOOL is_dir_list;
1980 BOOL only_check_errors = FALSE;
1981 const char *filename;
1982 BOOL is_test262_harness = FALSE;
1983 BOOL is_module = FALSE;
1984
1985 #if !defined(_WIN32)
1986 /* Date tests assume California local time */
1987 setenv("TZ", "America/Los_Angeles", 1);
1988 #endif
1989
1990 /* cannot use getopt because we want to pass the command line to
1991 the script */
1992 optind = 1;
1993 is_dir_list = TRUE;
1994 while (optind < argc) {
1995 char *arg = argv[optind];
1996 if (*arg != '-')
1997 break;
1998 optind++;
1999 if (str_equal(arg, "-h")) {
2000 help();
2001 } else if (str_equal(arg, "-m")) {
2002 dump_memory++;
2003 } else if (str_equal(arg, "-n")) {
2004 new_style++;
2005 } else if (str_equal(arg, "-s")) {
2006 test_mode = TEST_STRICT;
2007 } else if (str_equal(arg, "-a")) {
2008 test_mode = TEST_ALL;
2009 } else if (str_equal(arg, "-u")) {
2010 update_errors++;
2011 } else if (str_equal(arg, "-v")) {
2012 verbose++;
2013 } else if (str_equal(arg, "-c")) {
2014 load_config(get_opt_arg(arg, argv[optind++]));
2015 } else if (str_equal(arg, "-d")) {
2016 enumerate_tests(get_opt_arg(arg, argv[optind++]));
2017 } else if (str_equal(arg, "-e")) {
2018 error_filename = get_opt_arg(arg, argv[optind++]);
2019 } else if (str_equal(arg, "-x")) {
2020 namelist_load(&exclude_list, get_opt_arg(arg, argv[optind++]));
2021 } else if (str_equal(arg, "-f")) {
2022 is_dir_list = FALSE;
2023 } else if (str_equal(arg, "-r")) {
2024 report_filename = get_opt_arg(arg, argv[optind++]);
2025 } else if (str_equal(arg, "-E")) {
2026 only_check_errors = TRUE;
2027 } else if (str_equal(arg, "-T")) {
2028 slow_test_threshold = atoi(get_opt_arg(arg, argv[optind++]));
2029 } else if (str_equal(arg, "-N")) {
2030 is_test262_harness = TRUE;
2031 } else if (str_equal(arg, "--module")) {
2032 is_module = TRUE;
2033 } else {
2034 fatal(1, "unknown option: %s", arg);
2035 break;
2036 }
2037 }
2038
2039 if (optind >= argc && !test_list.count)
2040 help();
2041
2042 if (is_test262_harness) {
2043 return run_test262_harness_test(argv[optind], is_module);
2044 }
2045
2046 error_out = stdout;
2047 if (error_filename) {
2048 error_file = load_file(error_filename, NULL);
2049 if (only_check_errors && error_file) {
2050 namelist_free(&test_list);
2051 namelist_add_from_error_file(&test_list, error_file);
2052 }
2053 if (update_errors) {
2054 free(error_file);
2055 error_file = NULL;
2056 error_out = fopen(error_filename, "w");
2057 if (!error_out) {
2058 perror_exit(1, error_filename);
2059 }
2060 }
2061 }
2062
2063 update_exclude_dirs();
2064
2065 if (is_dir_list) {
2066 if (optind < argc && !isdigit(argv[optind][0])) {
2067 filename = argv[optind++];
2068 namelist_load(&test_list, filename);
2069 }
2070 start_index = 0;
2071 stop_index = -1;
2072 if (optind < argc) {
2073 start_index = atoi(argv[optind++]);
2074 if (optind < argc) {
2075 stop_index = atoi(argv[optind++]);
2076 }
2077 }
2078 if (!report_filename || str_equal(report_filename, "none")) {
2079 outfile = NULL;
2080 } else if (str_equal(report_filename, "-")) {
2081 outfile = stdout;
2082 } else {
2083 outfile = fopen(report_filename, "wb");
2084 if (!outfile) {
2085 perror_exit(1, report_filename);
2086 }
2087 }
2088 run_test_dir_list(&test_list, start_index, stop_index);
2089
2090 if (outfile && outfile != stdout) {
2091 fclose(outfile);
2092 outfile = NULL;
2093 }
2094 } else {
2095 outfile = stdout;
2096 while (optind < argc) {
2097 run_test(argv[optind++], -1);
2098 }
2099 }
2100
2101 if (dump_memory) {
2102 if (dump_memory > 1 && stats_count > 1) {
2103 printf("\nMininum memory statistics for %s:\n\n", stats_min_filename);
2104 JS_DumpMemoryUsage(stdout, &stats_min, NULL);
2105 printf("\nMaximum memory statistics for %s:\n\n", stats_max_filename);
2106 JS_DumpMemoryUsage(stdout, &stats_max, NULL);
2107 }
2108 printf("\nAverage memory statistics for %d tests:\n\n", stats_count);
2109 JS_DumpMemoryUsage(stdout, &stats_avg, NULL);
2110 printf("\n");
2111 }
2112
2113 if (is_dir_list) {
2114 fprintf(stderr, "Result: %d/%d error%s",
2115 test_failed, test_count, test_count != 1 ? "s" : "");
2116 if (test_excluded)
2117 fprintf(stderr, ", %d excluded", test_excluded);
2118 if (test_skipped)
2119 fprintf(stderr, ", %d skipped", test_skipped);
2120 if (error_file) {
2121 if (new_errors)
2122 fprintf(stderr, ", %d new", new_errors);
2123 if (changed_errors)
2124 fprintf(stderr, ", %d changed", changed_errors);
2125 if (fixed_errors)
2126 fprintf(stderr, ", %d fixed", fixed_errors);
2127 }
2128 fprintf(stderr, "\n");
2129 }
2130
2131 if (error_out && error_out != stdout) {
2132 fclose(error_out);
2133 error_out = NULL;
2134 }
2135
2136 namelist_free(&test_list);
2137 namelist_free(&exclude_list);
2138 namelist_free(&exclude_dir_list);
2139 free(harness_dir);
2140 free(harness_features);
2141 free(harness_exclude);
2142 free(error_file);
2143
2144 return 0;
2145 }