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

Annotation of /trunk/instrument-js.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.24