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

Annotation of /trunk/instrument-js.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:mergeinfo

  ViewVC Help
Powered by ViewVC 1.1.24