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

Contents of /trunk/instrument-js.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.24