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

Contents of /trunk/instrument-js.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.24