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

Diff of /trunk/instrument-js.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.84  
changed lines
  Added in v.246

  ViewVC Help
Powered by ViewVC 1.1.24