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

Annotation of /trunk/instrument-js.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.24