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

Annotation of /trunk/instrument-js.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:mergeinfo

  ViewVC Help
Powered by ViewVC 1.1.24