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

Legend:
Removed from v.70  
changed lines
  Added in v.317

  ViewVC Help
Powered by ViewVC 1.1.24