/[jscoverage]/trunk/instrument-js.cpp
ViewVC logotype

Contents of /trunk/instrument-js.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 341 - (show annotations)
Fri Oct 24 16:14:43 2008 UTC (10 years, 8 months ago) by siliconforks
Original Path: trunk/instrument-js.c
File MIME type: text/plain
File size: 48362 byte(s)
Add support for destructuring assignment in JS 1.7.
1 /*
2 instrument-js.c - JavaScript instrumentation routines
3 Copyright (C) 2007, 2008 siliconforks.com
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <config.h>
21
22 #include "instrument-js.h"
23
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <jsapi.h>
29 #include <jsarena.h>
30 #include <jsatom.h>
31 #include <jsexn.h>
32 #include <jsfun.h>
33 #include <jsinterp.h>
34 #include <jsiter.h>
35 #include <jsparse.h>
36 #include <jsregexp.h>
37 #include <jsscope.h>
38 #include <jsstr.h>
39
40 #include "encoding.h"
41 #include "global.h"
42 #include "highlight.h"
43 #include "resource-manager.h"
44 #include "util.h"
45
46 struct IfDirective {
47 const jschar * condition_start;
48 const jschar * condition_end;
49 uint16_t start_line;
50 uint16_t end_line;
51 struct IfDirective * next;
52 };
53
54 static bool * exclusive_directives = NULL;
55
56 static JSRuntime * runtime = NULL;
57 static JSContext * context = NULL;
58 static JSObject * global = NULL;
59 static JSVersion js_version = JSVERSION_ECMA_3;
60
61 /*
62 JSParseNode objects store line numbers starting from 1.
63 The lines array stores line numbers starting from 0.
64 */
65 static const char * file_id = NULL;
66 static char * lines = NULL;
67 static uint16_t num_lines = 0;
68
69 void jscoverage_set_js_version(const char * version) {
70 js_version = atoi(version);
71 }
72
73 void jscoverage_init(void) {
74 runtime = JS_NewRuntime(8L * 1024L * 1024L);
75 if (runtime == NULL) {
76 fatal("cannot create runtime");
77 }
78
79 context = JS_NewContext(runtime, 8192);
80 if (context == NULL) {
81 fatal("cannot create context");
82 }
83
84 JS_SetVersion(context, js_version);
85
86 global = JS_NewObject(context, NULL, NULL, NULL);
87 if (global == NULL) {
88 fatal("cannot create global object");
89 }
90
91 if (! JS_InitStandardClasses(context, global)) {
92 fatal("cannot initialize standard classes");
93 }
94 }
95
96 void jscoverage_cleanup(void) {
97 JS_DestroyContext(context);
98 JS_DestroyRuntime(runtime);
99 }
100
101 static void print_javascript(const jschar * characters, size_t num_characters, Stream * f) {
102 for (size_t i = 0; i < num_characters; i++) {
103 jschar c = characters[i];
104 /*
105 XXX does not handle no-break space, other unicode "space separator"
106 */
107 switch (c) {
108 case 0x9:
109 case 0xB:
110 case 0xC:
111 Stream_write_char(f, c);
112 break;
113 default:
114 if (32 <= c && c <= 126) {
115 Stream_write_char(f, c);
116 }
117 else {
118 Stream_printf(f, "\\u%04x", c);
119 }
120 break;
121 }
122 }
123 }
124
125 static void print_string(JSString * s, Stream * f) {
126 size_t length = JSSTRING_LENGTH(s);
127 jschar * characters = JSSTRING_CHARS(s);
128 for (size_t i = 0; i < length; i++) {
129 jschar c = characters[i];
130 if (32 <= c && c <= 126) {
131 switch (c) {
132 case '"':
133 Stream_write_string(f, "\\\"");
134 break;
135 /*
136 case '\'':
137 Stream_write_string(f, "\\'");
138 break;
139 */
140 case '\\':
141 Stream_write_string(f, "\\\\");
142 break;
143 default:
144 Stream_write_char(f, c);
145 break;
146 }
147 }
148 else {
149 switch (c) {
150 case 0x8:
151 Stream_write_string(f, "\\b");
152 break;
153 case 0x9:
154 Stream_write_string(f, "\\t");
155 break;
156 case 0xa:
157 Stream_write_string(f, "\\n");
158 break;
159 /* IE doesn't support this */
160 /*
161 case 0xb:
162 Stream_write_string(f, "\\v");
163 break;
164 */
165 case 0xc:
166 Stream_write_string(f, "\\f");
167 break;
168 case 0xd:
169 Stream_write_string(f, "\\r");
170 break;
171 default:
172 Stream_printf(f, "\\u%04x", c);
173 break;
174 }
175 }
176 }
177 }
178
179 static void print_string_atom(JSAtom * atom, Stream * f) {
180 assert(ATOM_IS_STRING(atom));
181 JSString * s = ATOM_TO_STRING(atom);
182 print_string(s, f);
183 }
184
185 static void print_regex(jsval value, Stream * f) {
186 assert(JSVAL_IS_STRING(value));
187 JSString * s = JSVAL_TO_STRING(value);
188 size_t length = JSSTRING_LENGTH(s);
189 jschar * characters = JSSTRING_CHARS(s);
190 for (size_t i = 0; i < length; i++) {
191 jschar c = characters[i];
192 if (32 <= c && c <= 126) {
193 Stream_write_char(f, c);
194 }
195 else {
196 Stream_printf(f, "\\u%04x", c);
197 }
198 }
199 }
200
201 static void print_quoted_string_atom(JSAtom * atom, Stream * f) {
202 assert(ATOM_IS_STRING(atom));
203 JSString * s = ATOM_TO_STRING(atom);
204 Stream_write_char(f, '"');
205 print_string(s, f);
206 Stream_write_char(f, '"');
207 }
208
209 static const char * get_op(uint8 op) {
210 switch(op) {
211 case JSOP_OR:
212 return "||";
213 case JSOP_AND:
214 return "&&";
215 case JSOP_BITOR:
216 return "|";
217 case JSOP_BITXOR:
218 return "^";
219 case JSOP_BITAND:
220 return "&";
221 case JSOP_EQ:
222 return "==";
223 case JSOP_NE:
224 return "!=";
225 case JSOP_STRICTEQ:
226 return "===";
227 case JSOP_STRICTNE:
228 return "!==";
229 case JSOP_LT:
230 return "<";
231 case JSOP_LE:
232 return "<=";
233 case JSOP_GT:
234 return ">";
235 case JSOP_GE:
236 return ">=";
237 case JSOP_LSH:
238 return "<<";
239 case JSOP_RSH:
240 return ">>";
241 case JSOP_URSH:
242 return ">>>";
243 case JSOP_ADD:
244 return "+";
245 case JSOP_SUB:
246 return "-";
247 case JSOP_MUL:
248 return "*";
249 case JSOP_DIV:
250 return "/";
251 case JSOP_MOD:
252 return "%";
253 default:
254 abort();
255 }
256 }
257
258 static void instrument_expression(JSParseNode * node, Stream * f);
259 static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
260
261 enum FunctionType {
262 FUNCTION_NORMAL,
263 FUNCTION_GETTER_OR_SETTER
264 };
265
266 static void instrument_function(JSParseNode * node, Stream * f, int indent, enum FunctionType type) {
267 assert(node->pn_type == TOK_FUNCTION);
268 assert(node->pn_arity == PN_FUNC);
269 JSObject * object = node->pn_funpob->object;
270 assert(JS_ObjectIsFunction(context, object));
271 JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
272 assert(function);
273 assert(object == &function->object);
274 Stream_printf(f, "%*s", indent, "");
275 if (type == FUNCTION_NORMAL) {
276 Stream_write_string(f, "function");
277 }
278
279 /* function name */
280 if (function->atom) {
281 Stream_write_char(f, ' ');
282 print_string_atom(function->atom, f);
283 }
284
285 /*
286 function parameters - see JS_DecompileFunction in jsapi.cpp, which calls
287 js_DecompileFunction in jsopcode.cpp
288 */
289 Stream_write_string(f, "(");
290 JSArenaPool pool;
291 JS_INIT_ARENA_POOL(&pool, "instrument_function", 256, 1, &context->scriptStackQuota);
292 jsuword * local_names = NULL;
293 if (JS_GET_LOCAL_NAME_COUNT(function)) {
294 local_names = js_GetLocalNameArray(context, function, &pool);
295 if (local_names == NULL) {
296 fatal("out of memory");
297 }
298 }
299 for (int i = 0; i < function->nargs; i++) {
300 if (i > 0) {
301 Stream_write_string(f, ", ");
302 }
303 JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]);
304 print_string_atom(param, f);
305 }
306 JS_FinishArenaPool(&pool);
307 Stream_write_string(f, ") {\n");
308
309 /* function body */
310 instrument_statement(node->pn_body, f, indent + 2, false);
311
312 Stream_write_string(f, "}\n");
313 }
314
315 static void instrument_function_call(JSParseNode * node, Stream * f) {
316 instrument_expression(node->pn_head, f);
317 Stream_write_char(f, '(');
318 for (struct JSParseNode * p = node->pn_head->pn_next; p != NULL; p = p->pn_next) {
319 if (p != node->pn_head->pn_next) {
320 Stream_write_string(f, ", ");
321 }
322 instrument_expression(p, f);
323 }
324 Stream_write_char(f, ')');
325 }
326
327 static void instrument_declarations(JSParseNode * list, Stream * f) {
328 assert(list->pn_arity == PN_LIST);
329 for (JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
330 switch (p->pn_type) {
331 case TOK_NAME:
332 assert(p->pn_arity == PN_NAME);
333 if (p != list->pn_head) {
334 Stream_write_string(f, ", ");
335 }
336 print_string_atom(p->pn_atom, f);
337 if (p->pn_expr != NULL) {
338 Stream_write_string(f, " = ");
339 instrument_expression(p->pn_expr, f);
340 }
341 break;
342 case TOK_ASSIGN:
343 case TOK_RB:
344 case TOK_RC:
345 /* destructuring */
346 instrument_expression(p, f);
347 break;
348 default:
349 abort();
350 break;
351 }
352 }
353 }
354
355 /*
356 See <Expressions> in jsparse.h.
357 TOK_FUNCTION is handled as a statement and as an expression.
358 TOK_DBLDOT is not handled (XML op).
359 TOK_DEFSHARP and TOK_USESHARP are not handled.
360 TOK_ANYNAME is not handled (XML op).
361 TOK_AT is not handled (XML op).
362 TOK_DBLCOLON is not handled.
363 TOK_XML* are not handled.
364 There seem to be some undocumented expressions:
365 TOK_INSTANCEOF binary
366 TOK_IN binary
367 */
368 static void instrument_expression(JSParseNode * node, Stream * f) {
369 switch (node->pn_type) {
370 case TOK_FUNCTION:
371 instrument_function(node, f, 0, FUNCTION_NORMAL);
372 break;
373 case TOK_COMMA:
374 for (struct JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) {
375 if (p != node->pn_head) {
376 Stream_write_string(f, ", ");
377 }
378 instrument_expression(p, f);
379 }
380 break;
381 case TOK_ASSIGN:
382 instrument_expression(node->pn_left, f);
383 Stream_write_char(f, ' ');
384 switch (node->pn_op) {
385 case JSOP_ADD:
386 case JSOP_SUB:
387 case JSOP_MUL:
388 case JSOP_MOD:
389 case JSOP_LSH:
390 case JSOP_RSH:
391 case JSOP_URSH:
392 case JSOP_BITAND:
393 case JSOP_BITOR:
394 case JSOP_BITXOR:
395 case JSOP_DIV:
396 Stream_printf(f, "%s", get_op(node->pn_op));
397 break;
398 default:
399 /* do nothing - it must be a simple assignment */
400 break;
401 }
402 Stream_write_string(f, "= ");
403 instrument_expression(node->pn_right, f);
404 break;
405 case TOK_HOOK:
406 instrument_expression(node->pn_kid1, f);
407 Stream_write_string(f, "? ");
408 instrument_expression(node->pn_kid2, f);
409 Stream_write_string(f, ": ");
410 instrument_expression(node->pn_kid3, f);
411 break;
412 case TOK_OR:
413 case TOK_AND:
414 case TOK_BITOR:
415 case TOK_BITXOR:
416 case TOK_BITAND:
417 case TOK_EQOP:
418 case TOK_RELOP:
419 case TOK_SHOP:
420 case TOK_PLUS:
421 case TOK_MINUS:
422 case TOK_STAR:
423 case TOK_DIVOP:
424 switch (node->pn_arity) {
425 case PN_BINARY:
426 instrument_expression(node->pn_left, f);
427 Stream_printf(f, " %s ", get_op(node->pn_op));
428 instrument_expression(node->pn_right, f);
429 break;
430 case PN_LIST:
431 for (struct JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) {
432 if (p != node->pn_head) {
433 Stream_printf(f, " %s ", get_op(node->pn_op));
434 }
435 instrument_expression(p, f);
436 }
437 break;
438 default:
439 abort();
440 }
441 break;
442 case TOK_UNARYOP:
443 switch (node->pn_op) {
444 case JSOP_NEG:
445 Stream_write_char(f, '-');
446 instrument_expression(node->pn_kid, f);
447 break;
448 case JSOP_POS:
449 Stream_write_char(f, '+');
450 instrument_expression(node->pn_kid, f);
451 break;
452 case JSOP_NOT:
453 Stream_write_char(f, '!');
454 instrument_expression(node->pn_kid, f);
455 break;
456 case JSOP_BITNOT:
457 Stream_write_char(f, '~');
458 instrument_expression(node->pn_kid, f);
459 break;
460 case JSOP_TYPEOF:
461 Stream_write_string(f, "typeof ");
462 instrument_expression(node->pn_kid, f);
463 break;
464 case JSOP_VOID:
465 Stream_write_string(f, "void ");
466 instrument_expression(node->pn_kid, f);
467 break;
468 default:
469 abort();
470 break;
471 }
472 break;
473 case TOK_INC:
474 case TOK_DEC:
475 /*
476 This is not documented, but node->pn_op tells whether it is pre- or post-increment.
477 */
478 switch (node->pn_op) {
479 case JSOP_INCNAME:
480 case JSOP_INCPROP:
481 case JSOP_INCELEM:
482 Stream_write_string(f, "++");
483 instrument_expression(node->pn_kid, f);
484 break;
485 case JSOP_DECNAME:
486 case JSOP_DECPROP:
487 case JSOP_DECELEM:
488 Stream_write_string(f, "--");
489 instrument_expression(node->pn_kid, f);
490 break;
491 case JSOP_NAMEINC:
492 case JSOP_PROPINC:
493 case JSOP_ELEMINC:
494 instrument_expression(node->pn_kid, f);
495 Stream_write_string(f, "++");
496 break;
497 case JSOP_NAMEDEC:
498 case JSOP_PROPDEC:
499 case JSOP_ELEMDEC:
500 instrument_expression(node->pn_kid, f);
501 Stream_write_string(f, "--");
502 break;
503 default:
504 abort();
505 break;
506 }
507 break;
508 case TOK_NEW:
509 Stream_write_string(f, "new ");
510 instrument_function_call(node, f);
511 break;
512 case TOK_DELETE:
513 Stream_write_string(f, "delete ");
514 instrument_expression(node->pn_kid, f);
515 break;
516 case TOK_DOT:
517 /*
518 This may have originally been x['foo-bar']. Because the string 'foo-bar'
519 contains illegal characters, we have to use the subscript syntax instead of
520 the dot syntax.
521 */
522 instrument_expression(node->pn_expr, f);
523 assert(ATOM_IS_STRING(node->pn_atom));
524 {
525 JSString * s = ATOM_TO_STRING(node->pn_atom);
526 bool is_keyword = (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF);
527 if (! is_keyword && js_IsIdentifier(s)) {
528 Stream_write_char(f, '.');
529 print_string_atom(node->pn_atom, f);
530 }
531 else {
532 Stream_write_char(f, '[');
533 print_quoted_string_atom(node->pn_atom, f);
534 Stream_write_char(f, ']');
535 }
536 }
537 break;
538 case TOK_LB:
539 instrument_expression(node->pn_left, f);
540 Stream_write_char(f, '[');
541 instrument_expression(node->pn_right, f);
542 Stream_write_char(f, ']');
543 break;
544 case TOK_LP:
545 instrument_function_call(node, f);
546 break;
547 case TOK_RB:
548 Stream_write_char(f, '[');
549 for (struct JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) {
550 if (p != node->pn_head) {
551 Stream_write_string(f, ", ");
552 }
553 /* TOK_COMMA is a special case: a hole in the array */
554 if (p->pn_type != TOK_COMMA) {
555 instrument_expression(p, f);
556 }
557 }
558 if (node->pn_extra == PNX_ENDCOMMA) {
559 Stream_write_char(f, ',');
560 }
561 Stream_write_char(f, ']');
562 break;
563 case TOK_RC:
564 Stream_write_char(f, '{');
565 for (struct JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) {
566 assert(p->pn_type == TOK_COLON);
567 if (p != node->pn_head) {
568 Stream_write_string(f, ", ");
569 }
570
571 /* check whether this is a getter or setter */
572 switch (p->pn_op) {
573 case JSOP_GETTER:
574 case JSOP_SETTER:
575 if (p->pn_op == JSOP_GETTER) {
576 Stream_write_string(f, "get ");
577 }
578 else {
579 Stream_write_string(f, "set ");
580 }
581 instrument_expression(p->pn_left, f);
582 if (p->pn_right->pn_type != TOK_FUNCTION) {
583 fatal("parse error: expected function");
584 }
585 instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);
586 break;
587 default:
588 instrument_expression(p->pn_left, f);
589 Stream_write_string(f, ": ");
590 instrument_expression(p->pn_right, f);
591 break;
592 }
593 }
594 Stream_write_char(f, '}');
595 break;
596 case TOK_RP:
597 Stream_write_char(f, '(');
598 instrument_expression(node->pn_kid, f);
599 Stream_write_char(f, ')');
600 break;
601 case TOK_NAME:
602 print_string_atom(node->pn_atom, f);
603 break;
604 case TOK_STRING:
605 print_quoted_string_atom(node->pn_atom, f);
606 break;
607 case TOK_REGEXP:
608 assert(node->pn_op == JSOP_REGEXP);
609 {
610 JSObject * object = node->pn_pob->object;
611 jsval result;
612 js_regexp_toString(context, object, &result);
613 print_regex(result, f);
614 }
615 break;
616 case TOK_NUMBER:
617 /*
618 A 64-bit IEEE 754 floating point number has a 52-bit fraction.
619 2^(-52) = 2.22 x 10^(-16)
620 Thus there are 16 significant digits.
621 To keep the output simple, special-case zero.
622 */
623 if (node->pn_dval == 0.0) {
624 Stream_write_string(f, "0");
625 }
626 else {
627 Stream_printf(f, "%.15g", node->pn_dval);
628 }
629 break;
630 case TOK_PRIMARY:
631 switch (node->pn_op) {
632 case JSOP_TRUE:
633 Stream_write_string(f, "true");
634 break;
635 case JSOP_FALSE:
636 Stream_write_string(f, "false");
637 break;
638 case JSOP_NULL:
639 Stream_write_string(f, "null");
640 break;
641 case JSOP_THIS:
642 Stream_write_string(f, "this");
643 break;
644 /* jsscan.h mentions `super' ??? */
645 default:
646 abort();
647 }
648 break;
649 case TOK_INSTANCEOF:
650 instrument_expression(node->pn_left, f);
651 Stream_write_string(f, " instanceof ");
652 instrument_expression(node->pn_right, f);
653 break;
654 case TOK_IN:
655 instrument_expression(node->pn_left, f);
656 Stream_write_string(f, " in ");
657 instrument_expression(node->pn_right, f);
658 break;
659 case TOK_LEXICALSCOPE:
660 assert(node->pn_arity == PN_NAME);
661 assert(node->pn_expr->pn_type == TOK_LET);
662 assert(node->pn_expr->pn_arity == PN_BINARY);
663 Stream_write_string(f, "let(");
664 assert(node->pn_expr->pn_left->pn_type == TOK_LP);
665 assert(node->pn_expr->pn_left->pn_arity == PN_LIST);
666 instrument_declarations(node->pn_expr->pn_left, f);
667 Stream_write_string(f, ") ");
668 instrument_expression(node->pn_expr->pn_right, f);
669 break;
670 default:
671 fatal("unsupported node type in file %s: %d", file_id, node->pn_type);
672 }
673 }
674
675 static void instrument_var_statement(JSParseNode * node, Stream * f, int indent) {
676 assert(node->pn_arity == PN_LIST);
677 Stream_printf(f, "%*s", indent, "");
678 Stream_write_string(f, "var ");
679 instrument_declarations(node, f);
680 }
681
682 static void instrument_let_definition(JSParseNode * node, Stream * f, int indent) {
683 assert(node->pn_arity == PN_LIST);
684 Stream_printf(f, "%*s", indent, "");
685 Stream_write_string(f, "let ");
686 instrument_declarations(node, f);
687 }
688
689 static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
690 switch (node->pn_type) {
691 case TOK_FUNCTION:
692 instrument_function(node, f, indent, FUNCTION_NORMAL);
693 break;
694 case TOK_LC:
695 assert(node->pn_arity == PN_LIST);
696 /*
697 Stream_write_string(f, "{\n");
698 */
699 for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) {
700 instrument_statement(p, f, indent, false);
701 }
702 /*
703 Stream_printf(f, "%*s", indent, "");
704 Stream_write_string(f, "}\n");
705 */
706 break;
707 case TOK_IF:
708 {
709 assert(node->pn_arity == PN_TERNARY);
710
711 uint16_t line = node->pn_pos.begin.lineno;
712 if (! is_jscoverage_if) {
713 if (line > num_lines) {
714 fatal("%s: script contains more than 65,535 lines", file_id);
715 }
716 if (line >= 2 && exclusive_directives[line - 2]) {
717 is_jscoverage_if = true;
718 }
719 }
720
721 Stream_printf(f, "%*s", indent, "");
722 Stream_write_string(f, "if (");
723 instrument_expression(node->pn_kid1, f);
724 Stream_write_string(f, ") {\n");
725 if (is_jscoverage_if && node->pn_kid3) {
726 uint16_t else_start = node->pn_kid3->pn_pos.begin.lineno;
727 uint16_t else_end = node->pn_kid3->pn_pos.end.lineno + 1;
728 Stream_printf(f, "%*s", indent + 2, "");
729 Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, else_start, else_end);
730 }
731 instrument_statement(node->pn_kid2, f, indent + 2, false);
732 Stream_printf(f, "%*s", indent, "");
733 Stream_write_string(f, "}\n");
734
735 if (node->pn_kid3 || is_jscoverage_if) {
736 Stream_printf(f, "%*s", indent, "");
737 Stream_write_string(f, "else {\n");
738
739 if (is_jscoverage_if) {
740 uint16_t if_start = node->pn_kid2->pn_pos.begin.lineno + 1;
741 uint16_t if_end = node->pn_kid2->pn_pos.end.lineno + 1;
742 Stream_printf(f, "%*s", indent + 2, "");
743 Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_start, if_end);
744 }
745
746 if (node->pn_kid3) {
747 instrument_statement(node->pn_kid3, f, indent + 2, is_jscoverage_if);
748 }
749
750 Stream_printf(f, "%*s", indent, "");
751 Stream_write_string(f, "}\n");
752 }
753
754 break;
755 }
756 case TOK_SWITCH:
757 assert(node->pn_arity == PN_BINARY);
758 Stream_printf(f, "%*s", indent, "");
759 Stream_write_string(f, "switch (");
760 instrument_expression(node->pn_left, f);
761 Stream_write_string(f, ") {\n");
762 for (struct JSParseNode * p = node->pn_right->pn_head; p != NULL; p = p->pn_next) {
763 Stream_printf(f, "%*s", indent, "");
764 switch (p->pn_type) {
765 case TOK_CASE:
766 Stream_write_string(f, "case ");
767 instrument_expression(p->pn_left, f);
768 Stream_write_string(f, ":\n");
769 break;
770 case TOK_DEFAULT:
771 Stream_write_string(f, "default:\n");
772 break;
773 default:
774 abort();
775 break;
776 }
777 instrument_statement(p->pn_right, f, indent + 2, false);
778 }
779 Stream_printf(f, "%*s", indent, "");
780 Stream_write_string(f, "}\n");
781 break;
782 case TOK_CASE:
783 case TOK_DEFAULT:
784 abort();
785 break;
786 case TOK_WHILE:
787 assert(node->pn_arity == PN_BINARY);
788 Stream_printf(f, "%*s", indent, "");
789 Stream_write_string(f, "while (");
790 instrument_expression(node->pn_left, f);
791 Stream_write_string(f, ") {\n");
792 instrument_statement(node->pn_right, f, indent + 2, false);
793 Stream_write_string(f, "}\n");
794 break;
795 case TOK_DO:
796 assert(node->pn_arity == PN_BINARY);
797 Stream_printf(f, "%*s", indent, "");
798 Stream_write_string(f, "do {\n");
799 instrument_statement(node->pn_left, f, indent + 2, false);
800 Stream_write_string(f, "}\n");
801 Stream_printf(f, "%*s", indent, "");
802 Stream_write_string(f, "while (");
803 instrument_expression(node->pn_right, f);
804 Stream_write_string(f, ");\n");
805 break;
806 case TOK_FOR:
807 assert(node->pn_arity == PN_BINARY);
808 Stream_printf(f, "%*s", indent, "");
809 Stream_write_string(f, "for ");
810 if (node->pn_iflags & JSITER_FOREACH) {
811 Stream_write_string(f, "each ");
812 }
813 Stream_write_char(f, '(');
814 switch (node->pn_left->pn_type) {
815 case TOK_IN:
816 /* for/in */
817 assert(node->pn_left->pn_arity == PN_BINARY);
818 switch (node->pn_left->pn_left->pn_type) {
819 case TOK_VAR:
820 instrument_var_statement(node->pn_left->pn_left, f, 0);
821 break;
822 case TOK_LET:
823 instrument_let_definition(node->pn_left->pn_left, f, 0);
824 break;
825 case TOK_NAME:
826 instrument_expression(node->pn_left->pn_left, f);
827 break;
828 default:
829 /* this is undocumented: for (x.value in y) */
830 instrument_expression(node->pn_left->pn_left, f);
831 break;
832 /*
833 default:
834 fprintf(stderr, "unexpected node type: %d\n", node->pn_left->pn_left->pn_type);
835 abort();
836 break;
837 */
838 }
839 Stream_write_string(f, " in ");
840 instrument_expression(node->pn_left->pn_right, f);
841 break;
842 case TOK_RESERVED:
843 /* for (;;) */
844 assert(node->pn_left->pn_arity == PN_TERNARY);
845 if (node->pn_left->pn_kid1) {
846 switch (node->pn_left->pn_kid1->pn_type) {
847 case TOK_VAR:
848 instrument_var_statement(node->pn_left->pn_kid1, f, 0);
849 break;
850 case TOK_LET:
851 instrument_let_definition(node->pn_left->pn_kid1, f, 0);
852 break;
853 default:
854 instrument_expression(node->pn_left->pn_kid1, f);
855 break;
856 }
857 }
858 Stream_write_string(f, ";");
859 if (node->pn_left->pn_kid2) {
860 Stream_write_char(f, ' ');
861 instrument_expression(node->pn_left->pn_kid2, f);
862 }
863 Stream_write_string(f, ";");
864 if (node->pn_left->pn_kid3) {
865 Stream_write_char(f, ' ');
866 instrument_expression(node->pn_left->pn_kid3, f);
867 }
868 break;
869 default:
870 abort();
871 break;
872 }
873 Stream_write_string(f, ") {\n");
874 instrument_statement(node->pn_right, f, indent + 2, false);
875 Stream_write_string(f, "}\n");
876 break;
877 case TOK_THROW:
878 assert(node->pn_arity == PN_UNARY);
879 Stream_printf(f, "%*s", indent, "");
880 Stream_write_string(f, "throw ");
881 instrument_expression(node->pn_u.unary.kid, f);
882 Stream_write_string(f, ";\n");
883 break;
884 case TOK_TRY:
885 Stream_printf(f, "%*s", indent, "");
886 Stream_write_string(f, "try {\n");
887 instrument_statement(node->pn_kid1, f, indent + 2, false);
888 Stream_printf(f, "%*s", indent, "");
889 Stream_write_string(f, "}\n");
890 if (node->pn_kid2) {
891 assert(node->pn_kid2->pn_type == TOK_RESERVED);
892 for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) {
893 assert(scope->pn_type == TOK_LEXICALSCOPE);
894 JSParseNode * catch = scope->pn_expr;
895 assert(catch->pn_type == TOK_CATCH);
896 Stream_printf(f, "%*s", indent, "");
897 Stream_write_string(f, "catch (");
898 /* this may not be a name - destructuring assignment */
899 /*
900 assert(catch->pn_kid1->pn_arity == PN_NAME);
901 print_string_atom(catch->pn_kid1->pn_atom, f);
902 */
903 instrument_expression(catch->pn_kid1, f);
904 if (catch->pn_kid2) {
905 Stream_write_string(f, " if ");
906 instrument_expression(catch->pn_kid2, f);
907 }
908 Stream_write_string(f, ") {\n");
909 instrument_statement(catch->pn_kid3, f, indent + 2, false);
910 Stream_printf(f, "%*s", indent, "");
911 Stream_write_string(f, "}\n");
912 }
913 }
914 if (node->pn_kid3) {
915 Stream_printf(f, "%*s", indent, "");
916 Stream_write_string(f, "finally {\n");
917 instrument_statement(node->pn_kid3, f, indent + 2, false);
918 Stream_printf(f, "%*s", indent, "");
919 Stream_write_string(f, "}\n");
920 }
921 break;
922 case TOK_CATCH:
923 abort();
924 break;
925 case TOK_BREAK:
926 case TOK_CONTINUE:
927 assert(node->pn_arity == PN_NAME || node->pn_arity == PN_NULLARY);
928 Stream_printf(f, "%*s", indent, "");
929 Stream_write_string(f, node->pn_type == TOK_BREAK? "break": "continue");
930 JSAtom * atom = node->pn_u.name.atom;
931 if (atom != NULL) {
932 Stream_write_char(f, ' ');
933 print_string_atom(node->pn_atom, f);
934 }
935 Stream_write_string(f, ";\n");
936 break;
937 case TOK_WITH:
938 assert(node->pn_arity == PN_BINARY);
939 Stream_printf(f, "%*s", indent, "");
940 Stream_write_string(f, "with (");
941 instrument_expression(node->pn_left, f);
942 Stream_write_string(f, ") {\n");
943 instrument_statement(node->pn_right, f, indent + 2, false);
944 Stream_printf(f, "%*s", indent, "");
945 Stream_write_string(f, "}\n");
946 break;
947 case TOK_VAR:
948 instrument_var_statement(node, f, indent);
949 Stream_write_string(f, ";\n");
950 break;
951 case TOK_RETURN:
952 assert(node->pn_arity == PN_UNARY);
953 Stream_printf(f, "%*s", indent, "");
954 Stream_write_string(f, "return");
955 if (node->pn_kid != NULL) {
956 Stream_write_char(f, ' ');
957 instrument_expression(node->pn_kid, f);
958 }
959 Stream_write_string(f, ";\n");
960 break;
961 case TOK_SEMI:
962 assert(node->pn_arity == PN_UNARY);
963 Stream_printf(f, "%*s", indent, "");
964 if (node->pn_kid != NULL) {
965 instrument_expression(node->pn_kid, f);
966 }
967 Stream_write_string(f, ";\n");
968 break;
969 case TOK_COLON:
970 assert(node->pn_arity == PN_NAME);
971 /*
972 This one is tricky: can't output instrumentation between the label and the
973 statement it's supposed to label ...
974 */
975 Stream_printf(f, "%*s", indent < 2? 0: indent - 2, "");
976 print_string_atom(node->pn_atom, f);
977 Stream_write_string(f, ":\n");
978 /*
979 ... use output_statement instead of instrument_statement.
980 */
981 output_statement(node->pn_expr, f, indent, false);
982 break;
983 case TOK_LEXICALSCOPE:
984 /* let statement */
985 assert(node->pn_arity == PN_NAME);
986 switch (node->pn_expr->pn_type) {
987 case TOK_LET:
988 /* let statement */
989 assert(node->pn_expr->pn_arity == PN_BINARY);
990 instrument_statement(node->pn_expr, f, indent, false);
991 break;
992 case TOK_LC:
993 /* block */
994 Stream_printf(f, "%*s", indent, "");
995 Stream_write_string(f, "{\n");
996 instrument_statement(node->pn_expr, f, indent + 2, false);
997 Stream_printf(f, "%*s", indent, "");
998 Stream_write_string(f, "}\n");
999 break;
1000 case TOK_FOR:
1001 instrument_statement(node->pn_expr, f, indent, false);
1002 break;
1003 default:
1004 abort();
1005 break;
1006 }
1007 break;
1008 case TOK_LET:
1009 switch (node->pn_arity) {
1010 case PN_BINARY:
1011 /* let statement */
1012 Stream_printf(f, "%*s", indent, "");
1013 Stream_write_string(f, "let (");
1014 assert(node->pn_left->pn_type == TOK_LP);
1015 assert(node->pn_left->pn_arity == PN_LIST);
1016 instrument_declarations(node->pn_left, f);
1017 Stream_write_string(f, ") {\n");
1018 instrument_statement(node->pn_right, f, indent + 2, false);
1019 Stream_printf(f, "%*s", indent, "");
1020 Stream_write_string(f, "}\n");
1021 break;
1022 case PN_LIST:
1023 /* let definition */
1024 instrument_let_definition(node, f, indent);
1025 Stream_write_string(f, ";\n");
1026 break;
1027 default:
1028 abort();
1029 break;
1030 }
1031 break;
1032 default:
1033 fatal("unsupported node type in file %s: %d", file_id, node->pn_type);
1034 }
1035 }
1036
1037 /*
1038 See <Statements> in jsparse.h.
1039 TOK_FUNCTION is handled as a statement and as an expression.
1040 TOK_EXPORT, TOK_IMPORT are not handled.
1041 */
1042 static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
1043 if (node->pn_type != TOK_LC && node->pn_type != TOK_LEXICALSCOPE) {
1044 uint16_t line = node->pn_pos.begin.lineno;
1045 if (line > num_lines) {
1046 fatal("%s: script contains more than 65,535 lines", file_id);
1047 }
1048
1049 /* the root node has line number 0 */
1050 if (line != 0) {
1051 Stream_printf(f, "%*s", indent, "");
1052 Stream_printf(f, "_$jscoverage['%s'][%d]++;\n", file_id, line);
1053 lines[line - 1] = 1;
1054 }
1055 }
1056 output_statement(node, f, indent, is_jscoverage_if);
1057 }
1058
1059 static bool characters_start_with(const jschar * characters, size_t line_start, size_t line_end, const char * prefix) {
1060 const jschar * characters_end = characters + line_end;
1061 const jschar * cp = characters + line_start;
1062 const char * bp = prefix;
1063 for (;;) {
1064 if (*bp == '\0') {
1065 return true;
1066 }
1067 else if (cp == characters_end) {
1068 return false;
1069 }
1070 else if (*cp != *bp) {
1071 return false;
1072 }
1073 bp++;
1074 cp++;
1075 }
1076 }
1077
1078 static bool characters_are_white_space(const jschar * characters, size_t line_start, size_t line_end) {
1079 /* XXX - other Unicode space */
1080 const jschar * end = characters + line_end;
1081 for (const jschar * p = characters + line_start; p < end; p++) {
1082 jschar c = *p;
1083 if (c == 0x9 || c == 0xB || c == 0xC || c == 0x20 || c == 0xA0) {
1084 continue;
1085 }
1086 else {
1087 return false;
1088 }
1089 }
1090 return true;
1091 }
1092
1093 static void error_reporter(JSContext * context, const char * message, JSErrorReport * report) {
1094 fprintf(stderr, "jscoverage: parse error: line %u: %s\n", report->lineno, message);
1095 }
1096
1097 void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) {
1098 file_id = id;
1099
1100 /* parse the javascript */
1101 JSParseContext parse_context;
1102 if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) {
1103 fatal("cannot create token stream from file: %s", file_id);
1104 }
1105 JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);
1106 JSParseNode * node = js_ParseScript(context, global, &parse_context);
1107 if (node == NULL) {
1108 js_ReportUncaughtException(context);
1109 fatal("parse error in file: %s", file_id);
1110 }
1111 JS_SetErrorReporter(context, old_error_reporter);
1112 num_lines = node->pn_pos.end.lineno;
1113 lines = xmalloc(num_lines);
1114 for (unsigned int i = 0; i < num_lines; i++) {
1115 lines[i] = 0;
1116 }
1117
1118 /* search code for conditionals */
1119 exclusive_directives = xnew(bool, num_lines);
1120 for (unsigned int i = 0; i < num_lines; i++) {
1121 exclusive_directives[i] = false;
1122 }
1123
1124 bool has_conditionals = false;
1125 struct IfDirective * if_directives = NULL;
1126 size_t line_number = 0;
1127 size_t i = 0;
1128 while (i < num_characters) {
1129 if (line_number == UINT16_MAX) {
1130 fatal("%s: script has more than 65,535 lines", file_id);
1131 }
1132 line_number++;
1133 size_t line_start = i;
1134 jschar c;
1135 bool done = false;
1136 while (! done && i < num_characters) {
1137 c = characters[i];
1138 switch (c) {
1139 case '\r':
1140 case '\n':
1141 case 0x2028:
1142 case 0x2029:
1143 done = true;
1144 break;
1145 default:
1146 i++;
1147 }
1148 }
1149 size_t line_end = i;
1150 if (i < num_characters) {
1151 i++;
1152 if (c == '\r' && i < num_characters && characters[i] == '\n') {
1153 i++;
1154 }
1155 }
1156
1157 if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) {
1158 has_conditionals = true;
1159
1160 if (characters_are_white_space(characters, line_start + 16, line_end)) {
1161 exclusive_directives[line_number - 1] = true;
1162 }
1163 else {
1164 struct IfDirective * if_directive = xnew(struct IfDirective, 1);
1165 if_directive->condition_start = characters + line_start + 16;
1166 if_directive->condition_end = characters + line_end;
1167 if_directive->start_line = line_number;
1168 if_directive->end_line = 0;
1169 if_directive->next = if_directives;
1170 if_directives = if_directive;
1171 }
1172 }
1173 else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) {
1174 for (struct IfDirective * p = if_directives; p != NULL; p = p->next) {
1175 if (p->end_line == 0) {
1176 p->end_line = line_number;
1177 break;
1178 }
1179 }
1180 }
1181 }
1182
1183 /*
1184 An instrumented JavaScript file has 4 sections:
1185 1. initialization
1186 2. instrumented source code
1187 3. conditionals
1188 4. original source code
1189 */
1190
1191 Stream * instrumented = Stream_new(0);
1192 instrument_statement(node, instrumented, 0, false);
1193 js_FinishParseContext(context, &parse_context);
1194
1195 /* write line number info to the output */
1196 Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");
1197 Stream_write_string(output, "if (! top._$jscoverage) {\n top._$jscoverage = {};\n}\n");
1198 Stream_write_string(output, "var _$jscoverage = top._$jscoverage;\n");
1199 Stream_printf(output, "if (! _$jscoverage['%s']) {\n", file_id);
1200 Stream_printf(output, " _$jscoverage['%s'] = [];\n", file_id);
1201 for (int i = 0; i < num_lines; i++) {
1202 if (lines[i]) {
1203 Stream_printf(output, " _$jscoverage['%s'][%d] = 0;\n", file_id, i + 1);
1204 }
1205 }
1206 Stream_write_string(output, "}\n");
1207 free(lines);
1208 lines = NULL;
1209 free(exclusive_directives);
1210 exclusive_directives = NULL;
1211
1212 /* conditionals */
1213 if (has_conditionals) {
1214 Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);
1215 }
1216
1217 /* copy the instrumented source code to the output */
1218 Stream_write(output, instrumented->data, instrumented->length);
1219
1220 /* conditionals */
1221 for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) {
1222 Stream_write_string(output, "if (!(");
1223 print_javascript(if_directive->condition_start, if_directive->condition_end - if_directive->condition_start, output);
1224 Stream_write_string(output, ")) {\n");
1225 Stream_printf(output, " _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line);
1226 Stream_write_string(output, "}\n");
1227 }
1228
1229 /* free */
1230 while (if_directives != NULL) {
1231 struct IfDirective * if_directive = if_directives;
1232 if_directives = if_directives->next;
1233 free(if_directive);
1234 }
1235
1236 /* copy the original source to the output */
1237 Stream_printf(output, "_$jscoverage['%s'].source = ", file_id);
1238 jscoverage_write_source(id, characters, num_characters, output);
1239 Stream_printf(output, ";\n");
1240
1241 Stream_delete(instrumented);
1242
1243 file_id = NULL;
1244 }
1245
1246 void jscoverage_write_source(const char * id, const jschar * characters, size_t num_characters, Stream * output) {
1247 Stream_write_string(output, "[");
1248 if (jscoverage_highlight) {
1249 Stream * highlighted_stream = Stream_new(num_characters);
1250 jscoverage_highlight_js(context, id, characters, num_characters, highlighted_stream);
1251 size_t i = 0;
1252 while (i < highlighted_stream->length) {
1253 if (i > 0) {
1254 Stream_write_char(output, ',');
1255 }
1256
1257 Stream_write_char(output, '"');
1258 bool done = false;
1259 while (! done) {
1260 char c = highlighted_stream->data[i];
1261 switch (c) {
1262 case 0x8:
1263 /* backspace */
1264 Stream_write_string(output, "\\b");
1265 break;
1266 case 0x9:
1267 /* horizontal tab */
1268 Stream_write_string(output, "\\t");
1269 break;
1270 case 0xa:
1271 /* line feed (new line) */
1272 done = true;
1273 break;
1274 /* IE doesn't support this */
1275 /*
1276 case 0xb:
1277 Stream_write_string(output, "\\v");
1278 break;
1279 */
1280 case 0xc:
1281 /* form feed */
1282 Stream_write_string(output, "\\f");
1283 break;
1284 case 0xd:
1285 /* carriage return */
1286 done = true;
1287 if (i + 1 < highlighted_stream->length && highlighted_stream->data[i + 1] == '\n') {
1288 i++;
1289 }
1290 break;
1291 case '"':
1292 Stream_write_string(output, "\\\"");
1293 break;
1294 case '\\':
1295 Stream_write_string(output, "\\\\");
1296 break;
1297 default:
1298 Stream_write_char(output, c);
1299 break;
1300 }
1301 i++;
1302 if (i >= highlighted_stream->length) {
1303 done = true;
1304 }
1305 }
1306 Stream_write_char(output, '"');
1307 }
1308 Stream_delete(highlighted_stream);
1309 }
1310 else {
1311 size_t i = 0;
1312 while (i < num_characters) {
1313 if (i > 0) {
1314 Stream_write_char(output, ',');
1315 }
1316
1317 Stream_write_char(output, '"');
1318 bool done = false;
1319 while (! done) {
1320 jschar c = characters[i];
1321 switch (c) {
1322 case 0x8:
1323 /* backspace */
1324 Stream_write_string(output, "\\b");
1325 break;
1326 case 0x9:
1327 /* horizontal tab */
1328 Stream_write_string(output, "\\t");
1329 break;
1330 case 0xa:
1331 /* line feed (new line) */
1332 done = true;
1333 break;
1334 /* IE doesn't support this */
1335 /*
1336 case 0xb:
1337 Stream_write_string(output, "\\v");
1338 break;
1339 */
1340 case 0xc:
1341 /* form feed */
1342 Stream_write_string(output, "\\f");
1343 break;
1344 case 0xd:
1345 /* carriage return */
1346 done = true;
1347 if (i + 1 < num_characters && characters[i + 1] == '\n') {
1348 i++;
1349 }
1350 break;
1351 case '"':
1352 Stream_write_string(output, "\\\"");
1353 break;
1354 case '\\':
1355 Stream_write_string(output, "\\\\");
1356 break;
1357 case '&':
1358 Stream_write_string(output, "&amp;");
1359 break;
1360 case '<':
1361 Stream_write_string(output, "&lt;");
1362 break;
1363 case '>':
1364 Stream_write_string(output, "&gt;");
1365 break;
1366 case 0x2028:
1367 case 0x2029:
1368 done = true;
1369 break;
1370 default:
1371 if (32 <= c && c <= 126) {
1372 Stream_write_char(output, c);
1373 }
1374 else {
1375 Stream_printf(output, "&#%d;", c);
1376 }
1377 break;
1378 }
1379 i++;
1380 if (i >= num_characters) {
1381 done = true;
1382 }
1383 }
1384 Stream_write_char(output, '"');
1385 }
1386 }
1387 Stream_write_string(output, "]");
1388 }
1389
1390 void jscoverage_copy_resources(const char * destination_directory) {
1391 copy_resource("jscoverage.html", destination_directory);
1392 copy_resource("jscoverage.css", destination_directory);
1393 copy_resource("jscoverage.js", destination_directory);
1394 copy_resource("jscoverage-ie.css", destination_directory);
1395 copy_resource("jscoverage-throbber.gif", destination_directory);
1396 copy_resource("jscoverage-highlight.css", destination_directory);
1397 }
1398
1399 /*
1400 coverage reports
1401 */
1402
1403 struct FileCoverageList {
1404 FileCoverage * file_coverage;
1405 struct FileCoverageList * next;
1406 };
1407
1408 struct Coverage {
1409 JSHashTable * coverage_table;
1410 struct FileCoverageList * coverage_list;
1411 };
1412
1413 static int compare_strings(const void * p1, const void * p2) {
1414 return strcmp(p1, p2) == 0;
1415 }
1416
1417 Coverage * Coverage_new(void) {
1418 Coverage * result = xmalloc(sizeof(Coverage));
1419 result->coverage_table = JS_NewHashTable(1024, JS_HashString, compare_strings, NULL, NULL, NULL);
1420 if (result->coverage_table == NULL) {
1421 fatal("cannot create hash table");
1422 }
1423 result->coverage_list = NULL;
1424 return result;
1425 }
1426
1427 void Coverage_delete(Coverage * coverage) {
1428 JS_HashTableDestroy(coverage->coverage_table);
1429 struct FileCoverageList * p = coverage->coverage_list;
1430 while (p != NULL) {
1431 free(p->file_coverage->coverage_lines);
1432 if (p->file_coverage->source_lines != NULL) {
1433 for (uint32 i = 0; i < p->file_coverage->num_source_lines; i++) {
1434 free(p->file_coverage->source_lines[i]);
1435 }
1436 free(p->file_coverage->source_lines);
1437 }
1438 free(p->file_coverage->id);
1439 free(p->file_coverage);
1440 struct FileCoverageList * q = p;
1441 p = p->next;
1442 free(q);
1443 }
1444 free(coverage);
1445 }
1446
1447 struct EnumeratorArg {
1448 CoverageForeachFunction f;
1449 void * p;
1450 };
1451
1452 static intN enumerator(JSHashEntry * entry, intN i, void * arg) {
1453 struct EnumeratorArg * enumerator_arg = arg;
1454 enumerator_arg->f(entry->value, i, enumerator_arg->p);
1455 return 0;
1456 }
1457
1458 void Coverage_foreach_file(Coverage * coverage, CoverageForeachFunction f, void * p) {
1459 struct EnumeratorArg enumerator_arg;
1460 enumerator_arg.f = f;
1461 enumerator_arg.p = p;
1462 JS_HashTableEnumerateEntries(coverage->coverage_table, enumerator, &enumerator_arg);
1463 }
1464
1465 int jscoverage_parse_json(Coverage * coverage, const uint8_t * json, size_t length) {
1466 int result = 0;
1467
1468 jschar * base = js_InflateString(context, (char *) json, &length);
1469 if (base == NULL) {
1470 fatal("out of memory");
1471 }
1472
1473 jschar * parenthesized_json = xnew(jschar, addst(length, 2));
1474 parenthesized_json[0] = '(';
1475 memcpy(parenthesized_json + 1, base, mulst(length, sizeof(jschar)));
1476 parenthesized_json[length + 1] = ')';
1477
1478 JS_free(context, base);
1479
1480 JSParseContext parse_context;
1481 if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) {
1482 free(parenthesized_json);
1483 return -1;
1484 }
1485 JSParseNode * root = js_ParseScript(context, global, &parse_context);
1486 free(parenthesized_json);
1487 if (root == NULL) {
1488 result = -1;
1489 goto done;
1490 }
1491
1492 /* root node must be TOK_LC */
1493 if (root->pn_type != TOK_LC) {
1494 result = -1;
1495 goto done;
1496 }
1497 JSParseNode * semi = root->pn_u.list.head;
1498
1499 /* the list must be TOK_SEMI and it must contain only one element */
1500 if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {
1501 result = -1;
1502 goto done;
1503 }
1504 JSParseNode * parenthesized = semi->pn_kid;
1505
1506 /* this must be a parenthesized expression */
1507 if (parenthesized->pn_type != TOK_RP) {
1508 result = -1;
1509 goto done;
1510 }
1511 JSParseNode * object = parenthesized->pn_kid;
1512
1513 /* this must be an object literal */
1514 if (object->pn_type != TOK_RC) {
1515 result = -1;
1516 goto done;
1517 }
1518
1519 for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {
1520 /* every element of this list must be TOK_COLON */
1521 if (p->pn_type != TOK_COLON) {
1522 result = -1;
1523 goto done;
1524 }
1525
1526 /* the key must be a string representing the file */
1527 JSParseNode * key = p->pn_left;
1528 if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {
1529 result = -1;
1530 goto done;
1531 }
1532 char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));
1533
1534 /* the value must be an object literal OR an array */
1535 JSParseNode * value = p->pn_right;
1536 if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {
1537 result = -1;
1538 goto done;
1539 }
1540
1541 JSParseNode * array = NULL;
1542 JSParseNode * source = NULL;
1543 if (value->pn_type == TOK_RB) {
1544 /* an array */
1545 array = value;
1546 }
1547 else if (value->pn_type == TOK_RC) {
1548 /* an object literal */
1549 if (value->pn_count != 2) {
1550 result = -1;
1551 goto done;
1552 }
1553 for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {
1554 if (element->pn_type != TOK_COLON) {
1555 result = -1;
1556 goto done;
1557 }
1558 JSParseNode * left = element->pn_left;
1559 if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {
1560 result = -1;
1561 goto done;
1562 }
1563 const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));
1564 if (strcmp(s, "coverage") == 0) {
1565 array = element->pn_right;
1566 if (array->pn_type != TOK_RB) {
1567 result = -1;
1568 goto done;
1569 }
1570 }
1571 else if (strcmp(s, "source") == 0) {
1572 source = element->pn_right;
1573 if (source->pn_type != TOK_RB) {
1574 result = -1;
1575 goto done;
1576 }
1577 }
1578 else {
1579 result = -1;
1580 goto done;
1581 }
1582 }
1583 }
1584 else {
1585 result = -1;
1586 goto done;
1587 }
1588
1589 if (array == NULL) {
1590 result = -1;
1591 goto done;
1592 }
1593
1594 /* look up the file in the coverage table */
1595 FileCoverage * file_coverage = JS_HashTableLookup(coverage->coverage_table, id_bytes);
1596 if (file_coverage == NULL) {
1597 /* not there: create a new one */
1598 char * id = xstrdup(id_bytes);
1599 file_coverage = xmalloc(sizeof(FileCoverage));
1600 file_coverage->id = id;
1601 file_coverage->num_coverage_lines = array->pn_count;
1602 file_coverage->coverage_lines = xnew(int, array->pn_count);
1603 file_coverage->source_lines = NULL;
1604
1605 /* set coverage for all lines */
1606 uint32 i = 0;
1607 for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {
1608 if (element->pn_type == TOK_NUMBER) {
1609 file_coverage->coverage_lines[i] = (int) element->pn_dval;
1610 }
1611 else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1612 file_coverage->coverage_lines[i] = -1;
1613 }
1614 else {
1615 result = -1;
1616 goto done;
1617 }
1618 }
1619 assert(i == array->pn_count);
1620
1621 /* add to the hash table */
1622 JS_HashTableAdd(coverage->coverage_table, id, file_coverage);
1623 struct FileCoverageList * coverage_list = xmalloc(sizeof(struct FileCoverageList));
1624 coverage_list->file_coverage = file_coverage;
1625 coverage_list->next = coverage->coverage_list;
1626 coverage->coverage_list = coverage_list;
1627 }
1628 else {
1629 /* sanity check */
1630 assert(strcmp(file_coverage->id, id_bytes) == 0);
1631 if (file_coverage->num_coverage_lines != array->pn_count) {
1632 result = -2;
1633 goto done;
1634 }
1635
1636 /* merge the coverage */
1637 uint32 i = 0;
1638 for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {
1639 if (element->pn_type == TOK_NUMBER) {
1640 if (file_coverage->coverage_lines[i] == -1) {
1641 result = -2;
1642 goto done;
1643 }
1644 file_coverage->coverage_lines[i] += (int) element->pn_dval;
1645 }
1646 else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1647 if (file_coverage->coverage_lines[i] != -1) {
1648 result = -2;
1649 goto done;
1650 }
1651 }
1652 else {
1653 result = -1;
1654 goto done;
1655 }
1656 }
1657 assert(i == array->pn_count);
1658 }
1659
1660 /* if this JSON file has source, use it */
1661 if (file_coverage->source_lines == NULL && source != NULL) {
1662 file_coverage->num_source_lines = source->pn_count;
1663 file_coverage->source_lines = xnew(char *, source->pn_count);
1664 uint32 i = 0;
1665 for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {
1666 if (element->pn_type != TOK_STRING) {
1667 result = -1;
1668 goto done;
1669 }
1670 file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));
1671 }
1672 assert(i == source->pn_count);
1673 }
1674 }
1675
1676 done:
1677 js_FinishParseContext(context, &parse_context);
1678 return result;
1679 }

  ViewVC Help
Powered by ViewVC 1.1.24