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

Contents of /trunk/instrument-js.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 155 - (show annotations)
Mon Sep 1 20:30:28 2008 UTC (10 years, 3 months ago) by siliconforks
Original Path: trunk/instrument-js.c
File MIME type: text/plain
File size: 34315 byte(s)
Add support for getters and setters.

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 <jsatom.h>
30 #include <jsfun.h>
31 #include <jsinterp.h>
32 #include <jsparse.h>
33 #include <jsregexp.h>
34 #include <jsscope.h>
35 #include <jsstr.h>
36
37 #include "resource-manager.h"
38 #include "util.h"
39
40 static JSRuntime * runtime = NULL;
41 static JSContext * context = NULL;
42 static JSObject * global = NULL;
43
44 /*
45 JSParseNode objects store line numbers starting from 1.
46 The lines array stores line numbers starting from 0.
47 */
48 static const char * file_id = NULL;
49 static char * lines = NULL;
50
51 void jscoverage_init(void) {
52 runtime = JS_NewRuntime(8L * 1024L * 1024L);
53 if (runtime == NULL) {
54 fatal("cannot create runtime");
55 }
56
57 context = JS_NewContext(runtime, 8192);
58 if (context == NULL) {
59 fatal("cannot create context");
60 }
61
62 global = JS_NewObject(context, NULL, NULL, NULL);
63 if (global == NULL) {
64 fatal("cannot create global object");
65 }
66
67 if (! JS_InitStandardClasses(context, global)) {
68 fatal("cannot initialize standard classes");
69 }
70 }
71
72 void jscoverage_cleanup(void) {
73 JS_DestroyContext(context);
74 JS_DestroyRuntime(runtime);
75 }
76
77 static void print_string(JSString * s, Stream * f) {
78 for (size_t i = 0; i < s->length; i++) {
79 char c = s->chars[i];
80 Stream_write_char(f, c);
81 }
82 }
83
84 static void print_string_atom(JSAtom * atom, Stream * f) {
85 assert(ATOM_IS_STRING(atom));
86 JSString * s = ATOM_TO_STRING(atom);
87 print_string(s, f);
88 }
89
90 static void print_string_jsval(jsval value, Stream * f) {
91 assert(JSVAL_IS_STRING(value));
92 JSString * s = JSVAL_TO_STRING(value);
93 print_string(s, f);
94 }
95
96 static void print_quoted_string_atom(JSAtom * atom, Stream * f) {
97 assert(ATOM_IS_STRING(atom));
98 JSString * s = ATOM_TO_STRING(atom);
99 JSString * quoted = js_QuoteString(context, s, '"');
100 print_string(quoted, f);
101 }
102
103 static const char * get_op(uint8 op) {
104 switch(op) {
105 case JSOP_BITOR:
106 return "|";
107 case JSOP_BITXOR:
108 return "^";
109 case JSOP_BITAND:
110 return "&";
111 case JSOP_EQ:
112 return "==";
113 case JSOP_NE:
114 return "!=";
115 case JSOP_NEW_EQ:
116 return "===";
117 case JSOP_NEW_NE:
118 return "!==";
119 case JSOP_LT:
120 return "<";
121 case JSOP_LE:
122 return "<=";
123 case JSOP_GT:
124 return ">";
125 case JSOP_GE:
126 return ">=";
127 case JSOP_LSH:
128 return "<<";
129 case JSOP_RSH:
130 return ">>";
131 case JSOP_URSH:
132 return ">>>";
133 case JSOP_ADD:
134 return "+";
135 case JSOP_SUB:
136 return "-";
137 case JSOP_MUL:
138 return "*";
139 case JSOP_DIV:
140 return "/";
141 case JSOP_MOD:
142 return "%";
143 default:
144 abort();
145 }
146 }
147
148 static void instrument_expression(JSParseNode * node, Stream * f);
149 static void instrument_statement(JSParseNode * node, Stream * f, int indent);
150
151 enum FunctionType {
152 FUNCTION_NORMAL,
153 FUNCTION_GETTER_OR_SETTER
154 };
155
156 static void instrument_function(JSParseNode * node, Stream * f, int indent, enum FunctionType type) {
157 assert(node->pn_arity == PN_FUNC);
158 assert(ATOM_IS_OBJECT(node->pn_funAtom));
159 JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom);
160 assert(JS_ObjectIsFunction(context, object));
161 JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
162 assert(function);
163 assert(object == function->object);
164 Stream_printf(f, "%*s", indent, "");
165 if (type == FUNCTION_NORMAL) {
166 Stream_write_string(f, "function");
167 }
168
169 /* function name */
170 if (function->atom) {
171 Stream_write_char(f, ' ');
172 print_string_atom(function->atom, f);
173 }
174
175 /* function parameters */
176 Stream_write_string(f, "(");
177 JSAtom ** params = xnew(JSAtom *, function->nargs);
178 for (int i = 0; i < function->nargs; i++) {
179 /* initialize to NULL for sanity check */
180 params[i] = NULL;
181 }
182 JSScope * scope = OBJ_SCOPE(object);
183 for (JSScopeProperty * scope_property = SCOPE_LAST_PROP(scope); scope_property != NULL; scope_property = scope_property->parent) {
184 if (scope_property->getter != js_GetArgument) {
185 continue;
186 }
187 assert(scope_property->flags & SPROP_HAS_SHORTID);
188 assert((uint16) scope_property->shortid < function->nargs);
189 assert(JSID_IS_ATOM(scope_property->id));
190 params[(uint16) scope_property->shortid] = JSID_TO_ATOM(scope_property->id);
191 }
192 for (int i = 0; i < function->nargs; i++) {
193 assert(params[i] != NULL);
194 if (i > 0) {
195 Stream_write_string(f, ", ");
196 }
197 if (ATOM_IS_STRING(params[i])) {
198 print_string_atom(params[i], f);
199 }
200 }
201 Stream_write_string(f, ") {\n");
202 free(params);
203
204 /* function body */
205 instrument_statement(node->pn_body, f, indent + 2);
206
207 Stream_write_string(f, "}\n");
208 }
209
210 static void instrument_function_call(JSParseNode * node, Stream * f) {
211 instrument_expression(node->pn_head, f);
212 Stream_write_char(f, '(');
213 for (struct JSParseNode * p = node->pn_head->pn_next; p != NULL; p = p->pn_next) {
214 if (p != node->pn_head->pn_next) {
215 Stream_write_string(f, ", ");
216 }
217 instrument_expression(p, f);
218 }
219 Stream_write_char(f, ')');
220 }
221
222 /*
223 See <Expressions> in jsparse.h.
224 TOK_FUNCTION is handled as a statement and as an expression.
225 TOK_DBLDOT is not handled (XML op).
226 TOK_DEFSHARP and TOK_USESHARP are not handled.
227 TOK_ANYNAME is not handled (XML op).
228 TOK_AT is not handled (XML op).
229 TOK_DBLCOLON is not handled.
230 TOK_XML* are not handled.
231 There seem to be some undocumented expressions:
232 TOK_INSTANCEOF binary
233 TOK_IN binary
234 */
235 static void instrument_expression(JSParseNode * node, Stream * f) {
236 switch (node->pn_type) {
237 case TOK_FUNCTION:
238 instrument_function(node, f, 0, FUNCTION_NORMAL);
239 break;
240 case TOK_COMMA:
241 for (struct JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) {
242 if (p != node->pn_head) {
243 Stream_write_string(f, ", ");
244 }
245 instrument_expression(p, f);
246 }
247 break;
248 case TOK_ASSIGN:
249 instrument_expression(node->pn_left, f);
250 Stream_write_char(f, ' ');
251 switch (node->pn_op) {
252 case JSOP_ADD:
253 case JSOP_SUB:
254 case JSOP_MUL:
255 case JSOP_MOD:
256 case JSOP_LSH:
257 case JSOP_RSH:
258 case JSOP_URSH:
259 case JSOP_BITAND:
260 case JSOP_BITOR:
261 case JSOP_BITXOR:
262 case JSOP_DIV:
263 Stream_printf(f, "%s", get_op(node->pn_op));
264 break;
265 default:
266 /* do nothing - it must be a simple assignment */
267 break;
268 }
269 Stream_write_string(f, "= ");
270 instrument_expression(node->pn_right, f);
271 break;
272 case TOK_HOOK:
273 instrument_expression(node->pn_kid1, f);
274 Stream_write_string(f, "? ");
275 instrument_expression(node->pn_kid2, f);
276 Stream_write_string(f, ": ");
277 instrument_expression(node->pn_kid3, f);
278 break;
279 case TOK_OR:
280 instrument_expression(node->pn_left, f);
281 Stream_write_string(f, " || ");
282 instrument_expression(node->pn_right, f);
283 break;
284 case TOK_AND:
285 instrument_expression(node->pn_left, f);
286 Stream_write_string(f, " && ");
287 instrument_expression(node->pn_right, f);
288 break;
289 case TOK_BITOR:
290 case TOK_BITXOR:
291 case TOK_BITAND:
292 case TOK_EQOP:
293 case TOK_RELOP:
294 case TOK_SHOP:
295 case TOK_PLUS:
296 case TOK_MINUS:
297 case TOK_STAR:
298 case TOK_DIVOP:
299 switch (node->pn_arity) {
300 case PN_BINARY:
301 instrument_expression(node->pn_left, f);
302 Stream_printf(f, " %s ", get_op(node->pn_op));
303 instrument_expression(node->pn_right, f);
304 break;
305 case PN_LIST:
306 for (struct JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) {
307 if (p != node->pn_head) {
308 Stream_printf(f, " %s ", get_op(node->pn_op));
309 }
310 instrument_expression(p, f);
311 }
312 break;
313 default:
314 abort();
315 }
316 break;
317 case TOK_UNARYOP:
318 switch (node->pn_op) {
319 case JSOP_NEG:
320 Stream_write_char(f, '-');
321 instrument_expression(node->pn_kid, f);
322 break;
323 case JSOP_POS:
324 Stream_write_char(f, '+');
325 instrument_expression(node->pn_kid, f);
326 break;
327 case JSOP_NOT:
328 Stream_write_char(f, '!');
329 instrument_expression(node->pn_kid, f);
330 break;
331 case JSOP_BITNOT:
332 Stream_write_char(f, '~');
333 instrument_expression(node->pn_kid, f);
334 break;
335 case JSOP_TYPEOF:
336 Stream_write_string(f, "typeof ");
337 instrument_expression(node->pn_kid, f);
338 break;
339 case JSOP_VOID:
340 Stream_write_string(f, "void ");
341 instrument_expression(node->pn_kid, f);
342 break;
343 default:
344 abort();
345 break;
346 }
347 break;
348 case TOK_INC:
349 case TOK_DEC:
350 /*
351 This is not documented, but node->pn_op tells whether it is pre- or post-increment.
352 */
353 switch (node->pn_op) {
354 case JSOP_INCNAME:
355 case JSOP_INCPROP:
356 case JSOP_INCELEM:
357 Stream_write_string(f, "++");
358 instrument_expression(node->pn_kid, f);
359 break;
360 case JSOP_DECNAME:
361 case JSOP_DECPROP:
362 case JSOP_DECELEM:
363 Stream_write_string(f, "--");
364 instrument_expression(node->pn_kid, f);
365 break;
366 case JSOP_NAMEINC:
367 case JSOP_PROPINC:
368 case JSOP_ELEMINC:
369 instrument_expression(node->pn_kid, f);
370 Stream_write_string(f, "++");
371 break;
372 case JSOP_NAMEDEC:
373 case JSOP_PROPDEC:
374 case JSOP_ELEMDEC:
375 instrument_expression(node->pn_kid, f);
376 Stream_write_string(f, "--");
377 break;
378 default:
379 abort();
380 break;
381 }
382 break;
383 case TOK_NEW:
384 Stream_write_string(f, "new ");
385 instrument_function_call(node, f);
386 break;
387 case TOK_DELETE:
388 Stream_write_string(f, "delete ");
389 instrument_expression(node->pn_kid, f);
390 break;
391 case TOK_DOT:
392 /*
393 This may have originally been x['foo-bar']. Because the string 'foo-bar'
394 contains illegal characters, we have to use the subscript syntax instead of
395 the dot syntax.
396 */
397 instrument_expression(node->pn_expr, f);
398 assert(ATOM_IS_STRING(node->pn_atom));
399 {
400 JSString * s = ATOM_TO_STRING(node->pn_atom);
401 /* XXX - semantics changed in 1.7 */
402 if (! ATOM_KEYWORD(node->pn_atom) && js_IsIdentifier(s)) {
403 Stream_write_char(f, '.');
404 print_string_atom(node->pn_atom, f);
405 }
406 else {
407 Stream_write_char(f, '[');
408 print_quoted_string_atom(node->pn_atom, f);
409 Stream_write_char(f, ']');
410 }
411 }
412 break;
413 case TOK_LB:
414 instrument_expression(node->pn_left, f);
415 Stream_write_char(f, '[');
416 instrument_expression(node->pn_right, f);
417 Stream_write_char(f, ']');
418 break;
419 case TOK_LP:
420 instrument_function_call(node, f);
421 break;
422 case TOK_RB:
423 Stream_write_char(f, '[');
424 for (struct JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) {
425 if (p != node->pn_head) {
426 Stream_write_string(f, ", ");
427 }
428 /* TOK_COMMA is a special case: a hole in the array */
429 if (p->pn_type != TOK_COMMA) {
430 instrument_expression(p, f);
431 }
432 }
433 if (node->pn_extra == PNX_ENDCOMMA) {
434 Stream_write_char(f, ',');
435 }
436 Stream_write_char(f, ']');
437 break;
438 case TOK_RC:
439 Stream_write_char(f, '{');
440 for (struct JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) {
441 assert(p->pn_type == TOK_COLON);
442 if (p != node->pn_head) {
443 Stream_write_string(f, ", ");
444 }
445
446 /* check whether this is a getter or setter */
447 switch (p->pn_op) {
448 case JSOP_GETTER:
449 Stream_write_string(f, "get ");
450 instrument_expression(p->pn_left, f);
451 instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);
452 break;
453 case JSOP_SETTER:
454 Stream_write_string(f, "set ");
455 instrument_expression(p->pn_left, f);
456 instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);
457 break;
458 default:
459 instrument_expression(p->pn_left, f);
460 Stream_write_string(f, ": ");
461 instrument_expression(p->pn_right, f);
462 break;
463 }
464 }
465 Stream_write_char(f, '}');
466 break;
467 case TOK_RP:
468 Stream_write_char(f, '(');
469 instrument_expression(node->pn_kid, f);
470 Stream_write_char(f, ')');
471 break;
472 case TOK_NAME:
473 print_string_atom(node->pn_atom, f);
474 break;
475 case TOK_STRING:
476 print_quoted_string_atom(node->pn_atom, f);
477 break;
478 case TOK_OBJECT:
479 switch (node->pn_op) {
480 case JSOP_OBJECT:
481 /* I assume this is JSOP_REGEXP */
482 abort();
483 break;
484 case JSOP_REGEXP:
485 assert(ATOM_IS_OBJECT(node->pn_atom));
486 {
487 JSObject * object = ATOM_TO_OBJECT(node->pn_atom);
488 jsval result;
489 js_regexp_toString(context, object, 0, NULL, &result);
490 print_string_jsval(result, f);
491 }
492 break;
493 default:
494 abort();
495 break;
496 }
497 break;
498 case TOK_NUMBER:
499 /*
500 A 64-bit IEEE 754 floating point number has a 52-bit fraction.
501 2^(-52) = 2.22 x 10^(-16)
502 Thus there are 16 significant digits.
503 To keep the output simple, special-case zero.
504 */
505 if (node->pn_dval == 0.0) {
506 Stream_write_string(f, "0");
507 }
508 else {
509 Stream_printf(f, "%.15g", node->pn_dval);
510 }
511 break;
512 case TOK_PRIMARY:
513 switch (node->pn_op) {
514 case JSOP_TRUE:
515 Stream_write_string(f, "true");
516 break;
517 case JSOP_FALSE:
518 Stream_write_string(f, "false");
519 break;
520 case JSOP_NULL:
521 Stream_write_string(f, "null");
522 break;
523 case JSOP_THIS:
524 Stream_write_string(f, "this");
525 break;
526 /* jsscan.h mentions `super' ??? */
527 default:
528 abort();
529 }
530 break;
531 case TOK_INSTANCEOF:
532 instrument_expression(node->pn_left, f);
533 Stream_write_string(f, " instanceof ");
534 instrument_expression(node->pn_right, f);
535 break;
536 case TOK_IN:
537 instrument_expression(node->pn_left, f);
538 Stream_write_string(f, " in ");
539 instrument_expression(node->pn_right, f);
540 break;
541 default:
542 fatal("unsupported node type in file %s: %d", file_id, node->pn_type);
543 }
544 }
545
546 static void instrument_var_statement(JSParseNode * node, Stream * f, int indent) {
547 assert(node->pn_arity == PN_LIST);
548 Stream_printf(f, "%*s", indent, "");
549 Stream_write_string(f, "var ");
550 for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) {
551 assert(p->pn_type == TOK_NAME);
552 assert(p->pn_arity == PN_NAME);
553 if (p != node->pn_head) {
554 Stream_write_string(f, ", ");
555 }
556 print_string_atom(p->pn_atom, f);
557 if (p->pn_expr != NULL) {
558 Stream_write_string(f, " = ");
559 instrument_expression(p->pn_expr, f);
560 }
561 }
562 }
563
564 static void output_statement(JSParseNode * node, Stream * f, int indent) {
565 switch (node->pn_type) {
566 case TOK_FUNCTION:
567 instrument_function(node, f, indent, FUNCTION_NORMAL);
568 break;
569 case TOK_LC:
570 assert(node->pn_arity == PN_LIST);
571 /*
572 Stream_write_string(f, "{\n");
573 */
574 for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) {
575 instrument_statement(p, f, indent);
576 }
577 /*
578 Stream_printf(f, "%*s", indent, "");
579 Stream_write_string(f, "}\n");
580 */
581 break;
582 case TOK_IF:
583 assert(node->pn_arity == PN_TERNARY);
584 Stream_printf(f, "%*s", indent, "");
585 Stream_write_string(f, "if (");
586 instrument_expression(node->pn_kid1, f);
587 Stream_write_string(f, ") {\n");
588 instrument_statement(node->pn_kid2, f, indent + 2);
589 Stream_printf(f, "%*s", indent, "");
590 Stream_write_string(f, "}\n");
591 if (node->pn_kid3) {
592 Stream_printf(f, "%*s", indent, "");
593 Stream_write_string(f, "else {\n");
594 instrument_statement(node->pn_kid3, f, indent + 2);
595 Stream_printf(f, "%*s", indent, "");
596 Stream_write_string(f, "}\n");
597 }
598 break;
599 case TOK_SWITCH:
600 assert(node->pn_arity == PN_BINARY);
601 Stream_printf(f, "%*s", indent, "");
602 Stream_write_string(f, "switch (");
603 instrument_expression(node->pn_left, f);
604 Stream_write_string(f, ") {\n");
605 for (struct JSParseNode * p = node->pn_right->pn_head; p != NULL; p = p->pn_next) {
606 Stream_printf(f, "%*s", indent, "");
607 switch (p->pn_type) {
608 case TOK_CASE:
609 Stream_write_string(f, "case ");
610 instrument_expression(p->pn_left, f);
611 Stream_write_string(f, ":\n");
612 break;
613 case TOK_DEFAULT:
614 Stream_write_string(f, "default:\n");
615 break;
616 default:
617 abort();
618 break;
619 }
620 instrument_statement(p->pn_right, f, indent + 2);
621 }
622 Stream_printf(f, "%*s", indent, "");
623 Stream_write_string(f, "}\n");
624 break;
625 case TOK_CASE:
626 case TOK_DEFAULT:
627 abort();
628 break;
629 case TOK_WHILE:
630 assert(node->pn_arity == PN_BINARY);
631 Stream_printf(f, "%*s", indent, "");
632 Stream_write_string(f, "while (");
633 instrument_expression(node->pn_left, f);
634 Stream_write_string(f, ") {\n");
635 instrument_statement(node->pn_right, f, indent + 2);
636 Stream_write_string(f, "}\n");
637 break;
638 case TOK_DO:
639 assert(node->pn_arity == PN_BINARY);
640 Stream_printf(f, "%*s", indent, "");
641 Stream_write_string(f, "do {\n");
642 instrument_statement(node->pn_left, f, indent + 2);
643 Stream_write_string(f, "}\n");
644 Stream_printf(f, "%*s", indent, "");
645 Stream_write_string(f, "while (");
646 instrument_expression(node->pn_right, f);
647 Stream_write_string(f, ");\n");
648 break;
649 case TOK_FOR:
650 assert(node->pn_arity == PN_BINARY);
651 Stream_printf(f, "%*s", indent, "");
652 Stream_write_string(f, "for (");
653 switch (node->pn_left->pn_type) {
654 case TOK_IN:
655 /* for/in */
656 assert(node->pn_left->pn_arity == PN_BINARY);
657 switch (node->pn_left->pn_left->pn_type) {
658 case TOK_VAR:
659 instrument_var_statement(node->pn_left->pn_left, f, 0);
660 break;
661 case TOK_NAME:
662 instrument_expression(node->pn_left->pn_left, f);
663 break;
664 default:
665 /* this is undocumented: for (x.value in y) */
666 instrument_expression(node->pn_left->pn_left, f);
667 break;
668 /*
669 default:
670 fprintf(stderr, "unexpected node type: %d\n", node->pn_left->pn_left->pn_type);
671 abort();
672 break;
673 */
674 }
675 Stream_write_string(f, " in ");
676 instrument_expression(node->pn_left->pn_right, f);
677 break;
678 case TOK_RESERVED:
679 /* for (;;) */
680 assert(node->pn_left->pn_arity == PN_TERNARY);
681 if (node->pn_left->pn_kid1) {
682 if (node->pn_left->pn_kid1->pn_type == TOK_VAR) {
683 instrument_var_statement(node->pn_left->pn_kid1, f, 0);
684 }
685 else {
686 instrument_expression(node->pn_left->pn_kid1, f);
687 }
688 }
689 Stream_write_string(f, ";");
690 if (node->pn_left->pn_kid2) {
691 Stream_write_char(f, ' ');
692 instrument_expression(node->pn_left->pn_kid2, f);
693 }
694 Stream_write_string(f, ";");
695 if (node->pn_left->pn_kid3) {
696 Stream_write_char(f, ' ');
697 instrument_expression(node->pn_left->pn_kid3, f);
698 }
699 break;
700 default:
701 abort();
702 break;
703 }
704 Stream_write_string(f, ") {\n");
705 instrument_statement(node->pn_right, f, indent + 2);
706 Stream_write_string(f, "}\n");
707 break;
708 case TOK_THROW:
709 assert(node->pn_arity == PN_UNARY);
710 Stream_printf(f, "%*s", indent, "");
711 Stream_write_string(f, "throw ");
712 instrument_expression(node->pn_u.unary.kid, f);
713 Stream_write_string(f, ";\n");
714 break;
715 case TOK_TRY:
716 Stream_printf(f, "%*s", indent, "");
717 Stream_write_string(f, "try {\n");
718 instrument_statement(node->pn_kid1, f, indent + 2);
719 Stream_printf(f, "%*s", indent, "");
720 Stream_write_string(f, "}\n");
721 {
722 for (JSParseNode * catch = node->pn_kid2; catch != NULL; catch = catch->pn_kid2) {
723 assert(catch->pn_type == TOK_CATCH);
724 Stream_printf(f, "%*s", indent, "");
725 Stream_write_string(f, "catch (");
726 assert(catch->pn_kid1->pn_arity == PN_NAME);
727 print_string_atom(catch->pn_kid1->pn_atom, f);
728 if (catch->pn_kid1->pn_expr) {
729 Stream_write_string(f, " if ");
730 instrument_expression(catch->pn_kid1->pn_expr, f);
731 }
732 Stream_write_string(f, ") {\n");
733 instrument_statement(catch->pn_kid3, f, indent + 2);
734 Stream_printf(f, "%*s", indent, "");
735 Stream_write_string(f, "}\n");
736 }
737 }
738 if (node->pn_kid3) {
739 Stream_printf(f, "%*s", indent, "");
740 Stream_write_string(f, "finally {\n");
741 instrument_statement(node->pn_kid3, f, indent + 2);
742 Stream_printf(f, "%*s", indent, "");
743 Stream_write_string(f, "}\n");
744 }
745 break;
746 case TOK_CATCH:
747 abort();
748 break;
749 case TOK_BREAK:
750 case TOK_CONTINUE:
751 assert(node->pn_arity == PN_NAME || node->pn_arity == PN_NULLARY);
752 Stream_printf(f, "%*s", indent, "");
753 Stream_write_string(f, node->pn_type == TOK_BREAK? "break": "continue");
754 JSAtom * atom = node->pn_u.name.atom;
755 if (atom != NULL) {
756 Stream_write_char(f, ' ');
757 print_string_atom(node->pn_atom, f);
758 }
759 Stream_write_string(f, ";\n");
760 break;
761 case TOK_WITH:
762 assert(node->pn_arity == PN_BINARY);
763 Stream_printf(f, "%*s", indent, "");
764 Stream_write_string(f, "with (");
765 instrument_expression(node->pn_left, f);
766 Stream_write_string(f, ") {\n");
767 instrument_statement(node->pn_right, f, indent + 2);
768 Stream_printf(f, "%*s", indent, "");
769 Stream_write_string(f, "}\n");
770 break;
771 case TOK_VAR:
772 instrument_var_statement(node, f, indent);
773 Stream_write_string(f, ";\n");
774 break;
775 case TOK_RETURN:
776 assert(node->pn_arity == PN_UNARY);
777 Stream_printf(f, "%*s", indent, "");
778 Stream_write_string(f, "return");
779 if (node->pn_kid != NULL) {
780 Stream_write_char(f, ' ');
781 instrument_expression(node->pn_kid, f);
782 }
783 Stream_write_string(f, ";\n");
784 break;
785 case TOK_SEMI:
786 assert(node->pn_arity == PN_UNARY);
787 Stream_printf(f, "%*s", indent, "");
788 if (node->pn_kid != NULL) {
789 instrument_expression(node->pn_kid, f);
790 }
791 Stream_write_string(f, ";\n");
792 break;
793 case TOK_COLON:
794 assert(node->pn_arity == PN_NAME);
795 /*
796 This one is tricky: can't output instrumentation between the label and the
797 statement it's supposed to label ...
798 */
799 Stream_printf(f, "%*s", indent < 2? 0: indent - 2, "");
800 print_string_atom(node->pn_atom, f);
801 Stream_write_string(f, ":\n");
802 /*
803 ... use output_statement instead of instrument_statement.
804 */
805 output_statement(node->pn_expr, f, indent);
806 break;
807 default:
808 fatal("unsupported node type in file %s: %d", file_id, node->pn_type);
809 }
810 }
811
812 /*
813 See <Statements> in jsparse.h.
814 TOK_FUNCTION is handled as a statement and as an expression.
815 TOK_EXPORT, TOK_IMPORT are not handled.
816 */
817 static void instrument_statement(JSParseNode * node, Stream * f, int indent) {
818 if (node->pn_type != TOK_LC) {
819 int line = node->pn_pos.begin.lineno;
820 /* the root node has line number 0 */
821 if (line != 0) {
822 Stream_printf(f, "%*s", indent, "");
823 Stream_printf(f, "_$jscoverage['%s'][%d]++;\n", file_id, line);
824 lines[line - 1] = 1;
825 }
826 }
827 output_statement(node, f, indent);
828 }
829
830 void jscoverage_instrument_js(const char * id, Stream * input, Stream * output) {
831 file_id = id;
832
833 /* scan the javascript */
834 size_t input_length = input->length;
835 jschar * base = js_InflateString(context, (char *) input->data, &input_length);
836 if (base == NULL) {
837 fatal("out of memory");
838 }
839 JSTokenStream * token_stream = js_NewTokenStream(context, base, input_length, NULL, 1, NULL);
840 if (token_stream == NULL) {
841 fatal("cannot create token stream from file: %s", file_id);
842 }
843
844 /* parse the javascript */
845 JSParseNode * node = js_ParseTokenStream(context, global, token_stream);
846 if (node == NULL) {
847 fatal("parse error in file: %s", file_id);
848 }
849 int num_lines = node->pn_pos.end.lineno;
850 lines = xmalloc(num_lines);
851 for (int i = 0; i < num_lines; i++) {
852 lines[i] = 0;
853 }
854
855 /*
856 An instrumented JavaScript file has 3 sections:
857 1. initialization
858 2. instrumented source code
859 3. original source code
860 */
861
862 Stream * instrumented = Stream_new(0);
863 instrument_statement(node, instrumented, 0);
864
865 /* write line number info to the output */
866 Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");
867 Stream_write_string(output, "if (! top._$jscoverage) {\n top._$jscoverage = {};\n}\n");
868 Stream_write_string(output, "var _$jscoverage = top._$jscoverage;\n");
869 Stream_printf(output, "if (! _$jscoverage['%s']) {\n", file_id);
870 Stream_printf(output, " _$jscoverage['%s'] = [];\n", file_id);
871 for (int i = 0; i < num_lines; i++) {
872 if (lines[i]) {
873 Stream_printf(output, " _$jscoverage['%s'][%d] = 0;\n", file_id, i + 1);
874 }
875 }
876 Stream_write_string(output, "}\n");
877 free(lines);
878 lines = NULL;
879
880 /* copy the instrumented source code to the output */
881 Stream_write(output, instrumented->data, instrumented->length);
882 Stream_write_char(output, '\n');
883
884 /* copy the original source to the output */
885 size_t i = 0;
886 while (i < input_length) {
887 Stream_write_string(output, "// ");
888 size_t line_start = i;
889 while (i < input_length && base[i] != '\r' && base[i] != '\n') {
890 i++;
891 }
892
893 size_t line_end = i;
894 if (i < input_length) {
895 if (base[i] == '\r') {
896 line_end = i;
897 i++;
898 if (i < input_length && base[i] == '\n') {
899 i++;
900 }
901 }
902 else if (base[i] == '\n') {
903 line_end = i;
904 i++;
905 }
906 else {
907 abort();
908 }
909 }
910
911 char * line = js_DeflateString(context, base + line_start, line_end - line_start);
912 Stream_write_string(output, line);
913 Stream_write_char(output, '\n');
914 JS_free(context, line);
915 }
916
917 Stream_delete(instrumented);
918
919 JS_free(context, base);
920
921 file_id = NULL;
922 }
923
924 void jscoverage_copy_resources(const char * destination_directory) {
925 copy_resource("jscoverage.html", destination_directory);
926 copy_resource("jscoverage.css", destination_directory);
927 copy_resource("jscoverage.js", destination_directory);
928 copy_resource("jscoverage-throbber.gif", destination_directory);
929 copy_resource("jscoverage-sh_main.js", destination_directory);
930 copy_resource("jscoverage-sh_javascript.js", destination_directory);
931 copy_resource("jscoverage-sh_nedit.css", destination_directory);
932 }
933
934 /*
935 coverage reports
936 */
937
938 struct FileCoverageList {
939 FileCoverage * file_coverage;
940 struct FileCoverageList * next;
941 };
942
943 struct Coverage {
944 JSHashTable * coverage_table;
945 struct FileCoverageList * coverage_list;
946 };
947
948 static int compare_strings(const void * p1, const void * p2) {
949 return strcmp(p1, p2) == 0;
950 }
951
952 Coverage * Coverage_new(void) {
953 Coverage * result = xmalloc(sizeof(Coverage));
954 result->coverage_table = JS_NewHashTable(1024, JS_HashString, compare_strings, NULL, NULL, NULL);
955 if (result->coverage_table == NULL) {
956 fatal("cannot create hash table");
957 }
958 result->coverage_list = NULL;
959 return result;
960 }
961
962 void Coverage_delete(Coverage * coverage) {
963 JS_HashTableDestroy(coverage->coverage_table);
964 struct FileCoverageList * p = coverage->coverage_list;
965 while (p != NULL) {
966 free(p->file_coverage->lines);
967 free(p->file_coverage->source);
968 free(p->file_coverage->id);
969 free(p->file_coverage);
970 struct FileCoverageList * q = p;
971 p = p->next;
972 free(q);
973 }
974 free(coverage);
975 }
976
977 struct EnumeratorArg {
978 CoverageForeachFunction f;
979 void * p;
980 };
981
982 static intN enumerator(JSHashEntry * entry, intN i, void * arg) {
983 struct EnumeratorArg * enumerator_arg = arg;
984 enumerator_arg->f(entry->value, i, enumerator_arg->p);
985 return 0;
986 }
987
988 void Coverage_foreach_file(Coverage * coverage, CoverageForeachFunction f, void * p) {
989 struct EnumeratorArg enumerator_arg;
990 enumerator_arg.f = f;
991 enumerator_arg.p = p;
992 JS_HashTableEnumerateEntries(coverage->coverage_table, enumerator, &enumerator_arg);
993 }
994
995 int jscoverage_parse_json(Coverage * coverage, const uint8_t * json, size_t length) {
996 jschar * base = js_InflateString(context, (char *) json, &length);
997 if (base == NULL) {
998 fatal("out of memory");
999 }
1000
1001 jschar * parenthesized_json = xnew(jschar, addst(length, 2));
1002 parenthesized_json[0] = '(';
1003 memcpy(parenthesized_json + 1, base, mulst(length, sizeof(jschar)));
1004 parenthesized_json[length + 1] = ')';
1005
1006 JS_free(context, base);
1007
1008 JSTokenStream * token_stream = js_NewTokenStream(context, parenthesized_json, length + 2, NULL, 1, NULL);
1009 if (token_stream == NULL) {
1010 fatal("cannot create token stream");
1011 }
1012
1013 JSParseNode * root = js_ParseTokenStream(context, global, token_stream);
1014 free(parenthesized_json);
1015 if (root == NULL) {
1016 return -1;
1017 }
1018
1019 /* root node must be TOK_LC */
1020 if (root->pn_type != TOK_LC) {
1021 return -1;
1022 }
1023 JSParseNode * semi = root->pn_u.list.head;
1024
1025 /* the list must be TOK_SEMI and it must contain only one element */
1026 if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {
1027 return -1;
1028 }
1029 JSParseNode * parenthesized = semi->pn_kid;
1030
1031 /* this must be a parenthesized expression */
1032 if (parenthesized->pn_type != TOK_RP) {
1033 return -1;
1034 }
1035 JSParseNode * object = parenthesized->pn_kid;
1036
1037 /* this must be an object literal */
1038 if (object->pn_type != TOK_RC) {
1039 return -1;
1040 }
1041
1042 for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {
1043 /* every element of this list must be TOK_COLON */
1044 if (p->pn_type != TOK_COLON) {
1045 return -1;
1046 }
1047
1048 /* the key must be a string representing the file */
1049 JSParseNode * key = p->pn_left;
1050 if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {
1051 return -1;
1052 }
1053 char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));
1054
1055 /* the value must be an object literal OR an array */
1056 JSParseNode * value = p->pn_right;
1057 if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {
1058 return -1;
1059 }
1060
1061 JSParseNode * array = NULL;
1062 JSParseNode * source = NULL;
1063 if (value->pn_type == TOK_RB) {
1064 /* an array */
1065 array = value;
1066 }
1067 else if (value->pn_type == TOK_RC) {
1068 /* an object literal */
1069 if (value->pn_count != 2) {
1070 return -1;
1071 }
1072 for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {
1073 if (element->pn_type != TOK_COLON) {
1074 return -1;
1075 }
1076 JSParseNode * left = element->pn_left;
1077 if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {
1078 return -1;
1079 }
1080 const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));
1081 if (strcmp(s, "coverage") == 0) {
1082 array = element->pn_right;
1083 if (array->pn_type != TOK_RB) {
1084 return -1;
1085 }
1086 }
1087 else if (strcmp(s, "source") == 0) {
1088 source = element->pn_right;
1089 if (source->pn_type != TOK_STRING || ! ATOM_IS_STRING(source->pn_atom)) {
1090 return -1;
1091 }
1092 }
1093 else {
1094 return -1;
1095 }
1096 }
1097 }
1098 else {
1099 return -1;
1100 }
1101
1102 if (array == NULL) {
1103 return -1;
1104 }
1105
1106 /* look up the file in the coverage table */
1107 FileCoverage * file_coverage = JS_HashTableLookup(coverage->coverage_table, id_bytes);
1108 if (file_coverage == NULL) {
1109 /* not there: create a new one */
1110 char * id = xstrdup(id_bytes);
1111 file_coverage = xmalloc(sizeof(FileCoverage));
1112 file_coverage->id = id;
1113 file_coverage->num_lines = array->pn_count - 1;
1114 file_coverage->lines = xnew(int, array->pn_count);
1115 if (source == NULL) {
1116 file_coverage->source = NULL;
1117 }
1118 else {
1119 file_coverage->source = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(source->pn_atom)));
1120 }
1121
1122 /* set coverage for all lines */
1123 uint32 i = 0;
1124 for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {
1125 if (element->pn_type == TOK_NUMBER) {
1126 file_coverage->lines[i] = (int) element->pn_dval;
1127 }
1128 else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1129 file_coverage->lines[i] = -1;
1130 }
1131 else {
1132 return -1;
1133 }
1134 }
1135 assert(i == array->pn_count);
1136
1137 /* add to the hash table */
1138 JS_HashTableAdd(coverage->coverage_table, id, file_coverage);
1139 struct FileCoverageList * coverage_list = xmalloc(sizeof(struct FileCoverageList));
1140 coverage_list->file_coverage = file_coverage;
1141 coverage_list->next = coverage->coverage_list;
1142 coverage->coverage_list = coverage_list;
1143 }
1144 else {
1145 /* sanity check */
1146 assert(strcmp(file_coverage->id, id_bytes) == 0);
1147 if (file_coverage->num_lines != array->pn_count - 1) {
1148 return -2;
1149 }
1150
1151 /* merge the coverage */
1152 uint32 i = 0;
1153 for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {
1154 if (element->pn_type == TOK_NUMBER) {
1155 if (file_coverage->lines[i] == -1) {
1156 return -2;
1157 }
1158 file_coverage->lines[i] += (int) element->pn_dval;
1159 }
1160 else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1161 if (file_coverage->lines[i] != -1) {
1162 return -2;
1163 }
1164 }
1165 else {
1166 return -1;
1167 }
1168 }
1169 assert(i == array->pn_count);
1170
1171 /* if this JSON file has source, use it */
1172 if (file_coverage->source == NULL && source != NULL) {
1173 file_coverage->source = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(source->pn_atom)));
1174 }
1175 }
1176 }
1177
1178 return 0;
1179 }

  ViewVC Help
Powered by ViewVC 1.1.24