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

Annotation of /trunk/instrument-js.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 336 - (hide annotations)
Thu Oct 23 20:08:29 2008 UTC (11 years, 1 month ago) by siliconforks
File MIME type: text/plain
File size: 45141 byte(s)
Update to work with SpiderMonkey 1.8.

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

  ViewVC Help
Powered by ViewVC 1.1.24