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

Annotation of /trunk/instrument-js.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.24