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

Annotation of /trunk/instrument-js.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.24