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

Annotation of /trunk/instrument-js.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.24