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

Annotation of /trunk/instrument-js.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.24