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

Annotation of /trunk/instrument-js.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.24