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

Contents of /trunk/instrument-js.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:mergeinfo

  ViewVC Help
Powered by ViewVC 1.1.24