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

Diff of /trunk/instrument-js.c

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

revision 116 by siliconforks, Sat May 31 21:42:36 2008 UTC revision 361 by siliconforks, Sun Oct 26 05:28:03 2008 UTC
# Line 26  Line 26 
26  #include <string.h>  #include <string.h>
27    
28  #include <jsapi.h>  #include <jsapi.h>
29    #include <jsarena.h>
30  #include <jsatom.h>  #include <jsatom.h>
31    #include <jsemit.h>
32    #include <jsexn.h>
33  #include <jsfun.h>  #include <jsfun.h>
34  #include <jsinterp.h>  #include <jsinterp.h>
35    #include <jsiter.h>
36  #include <jsparse.h>  #include <jsparse.h>
37  #include <jsregexp.h>  #include <jsregexp.h>
38  #include <jsscope.h>  #include <jsscope.h>
39  #include <jsstr.h>  #include <jsstr.h>
40    
41    #include "encoding.h"
42    #include "global.h"
43    #include "highlight.h"
44  #include "resource-manager.h"  #include "resource-manager.h"
45  #include "util.h"  #include "util.h"
46    
47    struct IfDirective {
48      const jschar * condition_start;
49      const jschar * condition_end;
50      uint16_t start_line;
51      uint16_t end_line;
52      struct IfDirective * next;
53    };
54    
55    bool jscoverage_mozilla = false;
56    
57    static bool * exclusive_directives = NULL;
58    
59  static JSRuntime * runtime = NULL;  static JSRuntime * runtime = NULL;
60  static JSContext * context = NULL;  static JSContext * context = NULL;
61  static JSObject * global = NULL;  static JSObject * global = NULL;
62    static JSVersion js_version = JSVERSION_ECMA_3;
63    
64  /*  /*
65  JSParseNode objects store line numbers starting from 1.  JSParseNode objects store line numbers starting from 1.
# Line 47  Line 67 
67  */  */
68  static const char * file_id = NULL;  static const char * file_id = NULL;
69  static char * lines = NULL;  static char * lines = NULL;
70    static uint16_t num_lines = 0;
71    
72    void jscoverage_set_js_version(const char * version) {
73      js_version = atoi(version);
74    }
75    
76  void jscoverage_init(void) {  void jscoverage_init(void) {
77    runtime = JS_NewRuntime(8L * 1024L * 1024L);    runtime = JS_NewRuntime(8L * 1024L * 1024L);
# Line 59  Line 84 
84      fatal("cannot create context");      fatal("cannot create context");
85    }    }
86    
87      JS_SetVersion(context, js_version);
88    
89    global = JS_NewObject(context, NULL, NULL, NULL);    global = JS_NewObject(context, NULL, NULL, NULL);
90    if (global == NULL) {    if (global == NULL) {
91      fatal("cannot create global object");      fatal("cannot create global object");
# Line 74  Line 101 
101    JS_DestroyRuntime(runtime);    JS_DestroyRuntime(runtime);
102  }  }
103    
104    static void print_javascript(const jschar * characters, size_t num_characters, Stream * f) {
105      for (size_t i = 0; i < num_characters; i++) {
106        jschar c = characters[i];
107        /*
108        XXX does not handle no-break space, other unicode "space separator"
109        */
110        switch (c) {
111        case 0x9:
112        case 0xB:
113        case 0xC:
114          Stream_write_char(f, c);
115          break;
116        default:
117          if (32 <= c && c <= 126) {
118            Stream_write_char(f, c);
119          }
120          else {
121            Stream_printf(f, "\\u%04x", c);
122          }
123          break;
124        }
125      }
126    }
127    
128  static void print_string(JSString * s, Stream * f) {  static void print_string(JSString * s, Stream * f) {
129    for (size_t i = 0; i < s->length; i++) {    size_t length = JSSTRING_LENGTH(s);
130      char c = s->chars[i];    jschar * characters = JSSTRING_CHARS(s);
131      Stream_write_char(f, c);    for (size_t i = 0; i < length; i++) {
132        jschar c = characters[i];
133        if (32 <= c && c <= 126) {
134          switch (c) {
135          case '"':
136            Stream_write_string(f, "\\\"");
137            break;
138    /*
139          case '\'':
140            Stream_write_string(f, "\\'");
141            break;
142    */
143          case '\\':
144            Stream_write_string(f, "\\\\");
145            break;
146          default:
147            Stream_write_char(f, c);
148            break;
149          }
150        }
151        else {
152          switch (c) {
153          case 0x8:
154            Stream_write_string(f, "\\b");
155            break;
156          case 0x9:
157            Stream_write_string(f, "\\t");
158            break;
159          case 0xa:
160            Stream_write_string(f, "\\n");
161            break;
162          /* IE doesn't support this */
163          /*
164          case 0xb:
165            Stream_write_string(f, "\\v");
166            break;
167          */
168          case 0xc:
169            Stream_write_string(f, "\\f");
170            break;
171          case 0xd:
172            Stream_write_string(f, "\\r");
173            break;
174          default:
175            Stream_printf(f, "\\u%04x", c);
176            break;
177          }
178        }
179    }    }
180  }  }
181    
# Line 87  Line 185 
185    print_string(s, f);    print_string(s, f);
186  }  }
187    
188  static void print_string_jsval(jsval value, Stream * f) {  static void print_regex(jsval value, Stream * f) {
189    assert(JSVAL_IS_STRING(value));    assert(JSVAL_IS_STRING(value));
190    JSString * s = JSVAL_TO_STRING(value);    JSString * s = JSVAL_TO_STRING(value);
191    print_string(s, f);    size_t length = JSSTRING_LENGTH(s);
192      jschar * characters = JSSTRING_CHARS(s);
193      for (size_t i = 0; i < length; i++) {
194        jschar c = characters[i];
195        if (32 <= c && c <= 126) {
196          Stream_write_char(f, c);
197        }
198        else {
199          Stream_printf(f, "\\u%04x", c);
200        }
201      }
202  }  }
203    
204  static void print_quoted_string_atom(JSAtom * atom, Stream * f) {  static void print_quoted_string_atom(JSAtom * atom, Stream * f) {
205    assert(ATOM_IS_STRING(atom));    assert(ATOM_IS_STRING(atom));
206    JSString * s = ATOM_TO_STRING(atom);    JSString * s = ATOM_TO_STRING(atom);
207    JSString * quoted = js_QuoteString(context, s, '"');    Stream_write_char(f, '"');
208    print_string(quoted, f);    print_string(s, f);
209      Stream_write_char(f, '"');
210  }  }
211    
212  static const char * get_op(uint8 op) {  static const char * get_op(uint8 op) {
213    switch(op) {    switch(op) {
214      case JSOP_OR:
215        return "||";
216      case JSOP_AND:
217        return "&&";
218    case JSOP_BITOR:    case JSOP_BITOR:
219      return "|";      return "|";
220    case JSOP_BITXOR:    case JSOP_BITXOR:
# Line 112  Line 225 
225      return "==";      return "==";
226    case JSOP_NE:    case JSOP_NE:
227      return "!=";      return "!=";
228    case JSOP_NEW_EQ:    case JSOP_STRICTEQ:
229      return "===";      return "===";
230    case JSOP_NEW_NE:    case JSOP_STRICTNE:
231      return "!==";      return "!==";
232    case JSOP_LT:    case JSOP_LT:
233      return "<";      return "<";
# Line 146  Line 259 
259  }  }
260    
261  static void instrument_expression(JSParseNode * node, Stream * f);  static void instrument_expression(JSParseNode * node, Stream * f);
262  static void instrument_statement(JSParseNode * node, Stream * f, int indent);  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
263    static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
264    
265  static void instrument_function(JSParseNode * node, Stream * f, int indent) {  enum FunctionType {
266      assert(node->pn_arity == PN_FUNC);    FUNCTION_NORMAL,
267      assert(ATOM_IS_OBJECT(node->pn_funAtom));    FUNCTION_GETTER_OR_SETTER
268      JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom);  };
269      assert(JS_ObjectIsFunction(context, object));  
270      JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);  static void output_for_in(JSParseNode * node, Stream * f) {
271      assert(function);    assert(node->pn_type == TOK_FOR);
272      assert(object == function->object);    assert(node->pn_arity == PN_BINARY);
273      Stream_printf(f, "%*s", indent, "");    Stream_write_string(f, "for ");
274      if (node->pn_iflags & JSITER_FOREACH) {
275        Stream_write_string(f, "each ");
276      }
277      Stream_write_char(f, '(');
278      instrument_expression(node->pn_left, f);
279      Stream_write_char(f, ')');
280    }
281    
282    static void output_array_comprehension_or_generator_expression(JSParseNode * node, Stream * f) {
283      assert(node->pn_type == TOK_LEXICALSCOPE);
284      assert(node->pn_arity == PN_NAME);
285      JSParseNode * for_node = node->pn_expr;
286      assert(for_node->pn_type == TOK_FOR);
287      assert(for_node->pn_arity == PN_BINARY);
288      JSParseNode * p = for_node;
289      while (p->pn_type == TOK_FOR) {
290        p = p->pn_right;
291      }
292      JSParseNode * if_node = NULL;
293      if (p->pn_type == TOK_IF) {
294        if_node = p;
295        assert(if_node->pn_arity == PN_TERNARY);
296        p = if_node->pn_kid2;
297      }
298      assert(p->pn_arity == PN_UNARY);
299      p = p->pn_kid;
300      if (p->pn_type == TOK_YIELD) {
301        /* for generator expressions */
302        p = p->pn_kid;
303      }
304    
305      instrument_expression(p, f);
306      p = for_node;
307      while (p->pn_type == TOK_FOR) {
308        Stream_write_char(f, ' ');
309        output_for_in(p, f);
310        p = p->pn_right;
311      }
312      if (if_node) {
313        Stream_write_string(f, " if (");
314        instrument_expression(if_node->pn_kid1, f);
315        Stream_write_char(f, ')');
316      }
317    }
318    
319    static void instrument_function(JSParseNode * node, Stream * f, int indent, enum FunctionType type) {
320      assert(node->pn_type == TOK_FUNCTION);
321      assert(node->pn_arity == PN_FUNC);
322      JSObject * object = node->pn_funpob->object;
323      assert(JS_ObjectIsFunction(context, object));
324      JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
325      assert(function);
326      assert(object == &function->object);
327      Stream_printf(f, "%*s", indent, "");
328      if (type == FUNCTION_NORMAL) {
329      Stream_write_string(f, "function");      Stream_write_string(f, "function");
330      }
331    
332      /* function name */    /* function name */
333      if (function->atom) {    if (function->atom) {
334        Stream_write_char(f, ' ');      Stream_write_char(f, ' ');
335        print_string_atom(function->atom, f);      print_string_atom(function->atom, f);
336      }    }
337    
338      /* function parameters */    /*
339      Stream_write_string(f, "(");    function parameters - see JS_DecompileFunction in jsapi.cpp, which calls
340      JSAtom ** params = xnew(JSAtom *, function->nargs);    js_DecompileFunction in jsopcode.cpp
341      for (int i = 0; i < function->nargs; i++) {    */
342        /* initialize to NULL for sanity check */    Stream_write_string(f, "(");
343        params[i] = NULL;    JSArenaPool pool;
344      }    JS_INIT_ARENA_POOL(&pool, "instrument_function", 256, 1, &context->scriptStackQuota);
345      JSScope * scope = OBJ_SCOPE(object);    jsuword * local_names = NULL;
346      for (JSScopeProperty * scope_property = SCOPE_LAST_PROP(scope); scope_property != NULL; scope_property = scope_property->parent) {    if (JS_GET_LOCAL_NAME_COUNT(function)) {
347        if (scope_property->getter != js_GetArgument) {      local_names = js_GetLocalNameArray(context, function, &pool);
348          continue;      if (local_names == NULL) {
349        }        fatal("out of memory");
       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);  
350      }      }
351      for (int i = 0; i < function->nargs; i++) {    }
352        assert(params[i] != NULL);    for (int i = 0; i < function->nargs; i++) {
353        if (i > 0) {      if (i > 0) {
354          Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
       }  
       if (ATOM_IS_STRING(params[i])) {  
         print_string_atom(params[i], f);  
       }  
355      }      }
356      Stream_write_string(f, ") {\n");      JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]);
357      free(params);      if (param == NULL) {
358          fatal("unsupported parameter type for function: %s", file_id);
359        }
360        print_string_atom(param, f);
361      }
362      JS_FinishArenaPool(&pool);
363      Stream_write_string(f, ") {\n");
364    
365      /* function body */    /* function body */
366      instrument_statement(node->pn_body, f, indent + 2);    if (function->flags & JSFUN_EXPR_CLOSURE) {
367        /* expression closure */
368        output_statement(node->pn_body, f, indent + 2, false);
369      }
370      else {
371        instrument_statement(node->pn_body, f, indent + 2, false);
372      }
373    
374      Stream_write_string(f, "}\n");    Stream_write_string(f, "}\n");
375  }  }
376    
377  static void instrument_function_call(JSParseNode * node, Stream * f) {  static void instrument_function_call(JSParseNode * node, Stream * f) {
378    instrument_expression(node->pn_head, f);    JSParseNode * function_node = node->pn_head;
379      if (function_node->pn_type == TOK_FUNCTION) {
380        JSObject * object = function_node->pn_funpob->object;
381        assert(JS_ObjectIsFunction(context, object));
382        JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
383        assert(function);
384        assert(object == &function->object);
385    
386        if (function_node->pn_flags & TCF_GENEXP_LAMBDA) {
387          /* it's a generator expression */
388          Stream_write_char(f, '(');
389          output_array_comprehension_or_generator_expression(function_node->pn_body, f);
390          Stream_write_char(f, ')');
391          return;
392        }
393        else {
394          Stream_write_char(f, '(');
395          instrument_expression(function_node, f);
396          Stream_write_char(f, ')');
397        }
398      }
399      else {
400        instrument_expression(function_node, f);
401      }
402    Stream_write_char(f, '(');    Stream_write_char(f, '(');
403    for (struct JSParseNode * p = node->pn_head->pn_next; p != NULL; p = p->pn_next) {    for (struct JSParseNode * p = function_node->pn_next; p != NULL; p = p->pn_next) {
404      if (p != node->pn_head->pn_next) {      if (p != node->pn_head->pn_next) {
405        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
406      }      }
# Line 212  Line 409 
409    Stream_write_char(f, ')');    Stream_write_char(f, ')');
410  }  }
411    
412    static void instrument_declarations(JSParseNode * list, Stream * f) {
413      assert(list->pn_arity == PN_LIST);
414      for (JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
415        switch (p->pn_type) {
416        case TOK_NAME:
417          assert(p->pn_arity == PN_NAME);
418          if (p != list->pn_head) {
419            Stream_write_string(f, ", ");
420          }
421          print_string_atom(p->pn_atom, f);
422          if (p->pn_expr != NULL) {
423            Stream_write_string(f, " = ");
424            instrument_expression(p->pn_expr, f);
425          }
426          break;
427        case TOK_ASSIGN:
428        case TOK_RB:
429        case TOK_RC:
430          /* destructuring */
431          instrument_expression(p, f);
432          break;
433        default:
434          abort();
435          break;
436        }
437      }
438    }
439    
440  /*  /*
441  See <Expressions> in jsparse.h.  See <Expressions> in jsparse.h.
442  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
# Line 228  Line 453 
453  static void instrument_expression(JSParseNode * node, Stream * f) {  static void instrument_expression(JSParseNode * node, Stream * f) {
454    switch (node->pn_type) {    switch (node->pn_type) {
455    case TOK_FUNCTION:    case TOK_FUNCTION:
456      instrument_function(node, f, 0);      instrument_function(node, f, 0, FUNCTION_NORMAL);
457      break;      break;
458    case TOK_COMMA:    case TOK_COMMA:
459      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) {
# Line 270  Line 495 
495      instrument_expression(node->pn_kid3, f);      instrument_expression(node->pn_kid3, f);
496      break;      break;
497    case TOK_OR:    case TOK_OR:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " || ");  
     instrument_expression(node->pn_right, f);  
     break;  
498    case TOK_AND:    case TOK_AND:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " && ");  
     instrument_expression(node->pn_right, f);  
     break;  
499    case TOK_BITOR:    case TOK_BITOR:
500    case TOK_BITXOR:    case TOK_BITXOR:
501    case TOK_BITAND:    case TOK_BITAND:
# Line 334  Line 551 
551        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
552        break;        break;
553      default:      default:
554        abort();        fatal("%s: unknown operator (%d) in file", file_id, node->pn_op);
555        break;        break;
556      }      }
557      break;      break;
# Line 391  Line 608 
608      assert(ATOM_IS_STRING(node->pn_atom));      assert(ATOM_IS_STRING(node->pn_atom));
609      {      {
610        JSString * s = ATOM_TO_STRING(node->pn_atom);        JSString * s = ATOM_TO_STRING(node->pn_atom);
611        /* XXX - semantics changed in 1.7 */        bool must_quote;
612        if (! ATOM_KEYWORD(node->pn_atom) && js_IsIdentifier(s)) {        if (JSSTRING_LENGTH(s) == 0) {
613          Stream_write_char(f, '.');          must_quote = true;
614          print_string_atom(node->pn_atom, f);        }
615          else if (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF) {
616            must_quote = true;
617          }
618          else if (! js_IsIdentifier(s)) {
619            must_quote = true;
620        }        }
621        else {        else {
622            must_quote = false;
623          }
624          if (must_quote) {
625          Stream_write_char(f, '[');          Stream_write_char(f, '[');
626          print_quoted_string_atom(node->pn_atom, f);          print_quoted_string_atom(node->pn_atom, f);
627          Stream_write_char(f, ']');          Stream_write_char(f, ']');
628        }        }
629          else {
630            Stream_write_char(f, '.');
631            print_string_atom(node->pn_atom, f);
632          }
633      }      }
634      break;      break;
635    case TOK_LB:    case TOK_LB:
# Line 431  Line 660 
660    case TOK_RC:    case TOK_RC:
661      Stream_write_char(f, '{');      Stream_write_char(f, '{');
662      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) {
663        assert(p->pn_type == TOK_COLON);        if (p->pn_type != TOK_COLON) {
664            fatal("unsupported node type in file %s: %d", file_id, p->pn_type);
665          }
666        if (p != node->pn_head) {        if (p != node->pn_head) {
667          Stream_write_string(f, ", ");          Stream_write_string(f, ", ");
668        }        }
669        instrument_expression(p->pn_left, f);  
670        Stream_write_string(f, ": ");        /* check whether this is a getter or setter */
671        instrument_expression(p->pn_right, f);        switch (p->pn_op) {
672          case JSOP_GETTER:
673          case JSOP_SETTER:
674            if (p->pn_op == JSOP_GETTER) {
675              Stream_write_string(f, "get ");
676            }
677            else {
678              Stream_write_string(f, "set ");
679            }
680            instrument_expression(p->pn_left, f);
681            if (p->pn_right->pn_type != TOK_FUNCTION) {
682              fatal("parse error: expected function");
683            }
684            instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);
685            break;
686          default:
687            instrument_expression(p->pn_left, f);
688            Stream_write_string(f, ": ");
689            instrument_expression(p->pn_right, f);
690            break;
691          }
692      }      }
693      Stream_write_char(f, '}');      Stream_write_char(f, '}');
694      break;      break;
# Line 452  Line 703 
703    case TOK_STRING:    case TOK_STRING:
704      print_quoted_string_atom(node->pn_atom, f);      print_quoted_string_atom(node->pn_atom, f);
705      break;      break;
706    case TOK_OBJECT:    case TOK_REGEXP:
707      switch (node->pn_op) {      assert(node->pn_op == JSOP_REGEXP);
708      case JSOP_OBJECT:      {
709        /* I assume this is JSOP_REGEXP */        JSObject * object = node->pn_pob->object;
710        abort();        jsval result;
711        break;        js_regexp_toString(context, object, &result);
712      case JSOP_REGEXP:        print_regex(result, f);
       assert(ATOM_IS_OBJECT(node->pn_atom));  
       {  
         JSObject * object = ATOM_TO_OBJECT(node->pn_atom);  
         jsval result;  
         js_regexp_toString(context, object, 0, NULL, &result);  
         print_string_jsval(result, f);  
       }  
       break;  
     default:  
       abort();  
       break;  
713      }      }
714      break;      break;
715    case TOK_NUMBER:    case TOK_NUMBER:
# Line 515  Line 755 
755      Stream_write_string(f, " in ");      Stream_write_string(f, " in ");
756      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
757      break;      break;
758    default:    case TOK_LEXICALSCOPE:
759      fatal("unsupported node type in file %s: %d", file_id, node->pn_type);      assert(node->pn_arity == PN_NAME);
760    }      assert(node->pn_expr->pn_type == TOK_LET);
761  }      assert(node->pn_expr->pn_arity == PN_BINARY);
762        Stream_write_string(f, "let(");
763  static void instrument_var_statement(JSParseNode * node, Stream * f, int indent) {      assert(node->pn_expr->pn_left->pn_type == TOK_LP);
764    assert(node->pn_arity == PN_LIST);      assert(node->pn_expr->pn_left->pn_arity == PN_LIST);
765    Stream_printf(f, "%*s", indent, "");      instrument_declarations(node->pn_expr->pn_left, f);
766    Stream_write_string(f, "var ");      Stream_write_string(f, ") ");
767    for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) {      instrument_expression(node->pn_expr->pn_right, f);
768      assert(p->pn_type == TOK_NAME);      break;
769      assert(p->pn_arity == PN_NAME);    case TOK_YIELD:
770      if (p != node->pn_head) {      assert(node->pn_arity == PN_UNARY);
771        Stream_write_string(f, ", ");      Stream_write_string(f, "yield");
772        if (node->pn_kid != NULL) {
773          Stream_write_char(f, ' ');
774          instrument_expression(node->pn_kid, f);
775      }      }
776      print_string_atom(p->pn_atom, f);      break;
777      if (p->pn_expr != NULL) {    case TOK_ARRAYCOMP:
778        Stream_write_string(f, " = ");      assert(node->pn_arity == PN_LIST);
779        instrument_expression(p->pn_expr, f);      {
780          JSParseNode * block_node;
781          switch (node->pn_count) {
782          case 1:
783            block_node = node->pn_head;
784            break;
785          case 2:
786            block_node = node->pn_head->pn_next;
787            break;
788          default:
789            abort();
790            break;
791          }
792          Stream_write_char(f, '[');
793          output_array_comprehension_or_generator_expression(block_node, f);
794          Stream_write_char(f, ']');
795      }      }
796        break;
797      case TOK_VAR:
798        assert(node->pn_arity == PN_LIST);
799        Stream_write_string(f, "var ");
800        instrument_declarations(node, f);
801        break;
802      case TOK_LET:
803        assert(node->pn_arity == PN_LIST);
804        Stream_write_string(f, "let ");
805        instrument_declarations(node, f);
806        break;
807      default:
808        fatal("unsupported node type in file %s: %d", file_id, node->pn_type);
809    }    }
810  }  }
811    
812  static void output_statement(JSParseNode * node, Stream * f, int indent) {  static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
813    switch (node->pn_type) {    switch (node->pn_type) {
814    case TOK_FUNCTION:    case TOK_FUNCTION:
815      instrument_function(node, f, indent);      instrument_function(node, f, indent, FUNCTION_NORMAL);
816      break;      break;
817    case TOK_LC:    case TOK_LC:
818      assert(node->pn_arity == PN_LIST);      assert(node->pn_arity == PN_LIST);
# Line 549  Line 820 
820      Stream_write_string(f, "{\n");      Stream_write_string(f, "{\n");
821  */  */
822      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) {
823        instrument_statement(p, f, indent);        instrument_statement(p, f, indent, false);
824      }      }
825  /*  /*
826      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 557  Line 828 
828  */  */
829      break;      break;
830    case TOK_IF:    case TOK_IF:
831      {
832      assert(node->pn_arity == PN_TERNARY);      assert(node->pn_arity == PN_TERNARY);
833    
834        uint16_t line = node->pn_pos.begin.lineno;
835        if (! is_jscoverage_if) {
836          if (line > num_lines) {
837            fatal("%s: script contains more than 65,535 lines", file_id);
838          }
839          if (line >= 2 && exclusive_directives[line - 2]) {
840            is_jscoverage_if = true;
841          }
842        }
843    
844      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
845      Stream_write_string(f, "if (");      Stream_write_string(f, "if (");
846      instrument_expression(node->pn_kid1, f);      instrument_expression(node->pn_kid1, f);
847      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
848      instrument_statement(node->pn_kid2, f, indent + 2);      if (is_jscoverage_if && node->pn_kid3) {
849          uint16_t else_start = node->pn_kid3->pn_pos.begin.lineno;
850          uint16_t else_end = node->pn_kid3->pn_pos.end.lineno + 1;
851          Stream_printf(f, "%*s", indent + 2, "");
852          Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, else_start, else_end);
853        }
854        instrument_statement(node->pn_kid2, f, indent + 2, false);
855      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
856      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
857      if (node->pn_kid3) {  
858        if (node->pn_kid3 || is_jscoverage_if) {
859        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
860        Stream_write_string(f, "else {\n");        Stream_write_string(f, "else {\n");
861        instrument_statement(node->pn_kid3, f, indent + 2);  
862          if (is_jscoverage_if) {
863            uint16_t if_start = node->pn_kid2->pn_pos.begin.lineno + 1;
864            uint16_t if_end = node->pn_kid2->pn_pos.end.lineno + 1;
865            Stream_printf(f, "%*s", indent + 2, "");
866            Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_start, if_end);
867          }
868    
869          if (node->pn_kid3) {
870            instrument_statement(node->pn_kid3, f, indent + 2, is_jscoverage_if);
871          }
872    
873        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
874        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
875      }      }
876    
877      break;      break;
878      }
879    case TOK_SWITCH:    case TOK_SWITCH:
880      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
881      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
882      Stream_write_string(f, "switch (");      Stream_write_string(f, "switch (");
883      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
884      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
885      for (struct JSParseNode * p = node->pn_right->pn_head; p != NULL; p = p->pn_next) {      {
886        Stream_printf(f, "%*s", indent, "");        JSParseNode * list = node->pn_right;
887        switch (p->pn_type) {        if (list->pn_type == TOK_LEXICALSCOPE) {
888        case TOK_CASE:          list = list->pn_expr;
889          Stream_write_string(f, "case ");        }
890          instrument_expression(p->pn_left, f);        for (struct JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
891          Stream_write_string(f, ":\n");          Stream_printf(f, "%*s", indent, "");
892          break;          switch (p->pn_type) {
893        case TOK_DEFAULT:          case TOK_CASE:
894          Stream_write_string(f, "default:\n");            Stream_write_string(f, "case ");
895          break;            instrument_expression(p->pn_left, f);
896        default:            Stream_write_string(f, ":\n");
897          abort();            break;
898          break;          case TOK_DEFAULT:
899              Stream_write_string(f, "default:\n");
900              break;
901            default:
902              abort();
903              break;
904            }
905            instrument_statement(p->pn_right, f, indent + 2, false);
906        }        }
       instrument_statement(p->pn_right, f, indent + 2);  
907      }      }
908      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
909      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
# Line 609  Line 918 
918      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
919      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
920      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
921      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
922      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
923      break;      break;
924    case TOK_DO:    case TOK_DO:
925      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
926      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
927      Stream_write_string(f, "do {\n");      Stream_write_string(f, "do {\n");
928      instrument_statement(node->pn_left, f, indent + 2);      instrument_statement(node->pn_left, f, indent + 2, false);
929      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
930      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
931      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
# Line 626  Line 935 
935    case TOK_FOR:    case TOK_FOR:
936      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
937      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
     Stream_write_string(f, "for (");  
938      switch (node->pn_left->pn_type) {      switch (node->pn_left->pn_type) {
939      case TOK_IN:      case TOK_IN:
940        /* for/in */        /* for/in */
941        assert(node->pn_left->pn_arity == PN_BINARY);        assert(node->pn_left->pn_arity == PN_BINARY);
942        switch (node->pn_left->pn_left->pn_type) {        output_for_in(node, f);
       case TOK_VAR:  
         instrument_var_statement(node->pn_left->pn_left, f, 0);  
         break;  
       case TOK_NAME:  
         instrument_expression(node->pn_left->pn_left, f);  
         break;  
       default:  
         /* this is undocumented: for (x.value in y) */  
         instrument_expression(node->pn_left->pn_left, f);  
         break;  
 /*  
       default:  
         fprintf(stderr, "unexpected node type: %d\n", node->pn_left->pn_left->pn_type);  
         abort();  
         break;  
 */  
       }  
       Stream_write_string(f, " in ");  
       instrument_expression(node->pn_left->pn_right, f);  
943        break;        break;
944      case TOK_RESERVED:      case TOK_RESERVED:
945        /* for (;;) */        /* for (;;) */
946        assert(node->pn_left->pn_arity == PN_TERNARY);        assert(node->pn_left->pn_arity == PN_TERNARY);
947          Stream_write_string(f, "for (");
948        if (node->pn_left->pn_kid1) {        if (node->pn_left->pn_kid1) {
949          if (node->pn_left->pn_kid1->pn_type == TOK_VAR) {          instrument_expression(node->pn_left->pn_kid1, f);
           instrument_var_statement(node->pn_left->pn_kid1, f, 0);  
         }  
         else {  
           instrument_expression(node->pn_left->pn_kid1, f);  
         }  
950        }        }
951        Stream_write_string(f, ";");        Stream_write_string(f, ";");
952        if (node->pn_left->pn_kid2) {        if (node->pn_left->pn_kid2) {
# Line 673  Line 958 
958          Stream_write_char(f, ' ');          Stream_write_char(f, ' ');
959          instrument_expression(node->pn_left->pn_kid3, f);          instrument_expression(node->pn_left->pn_kid3, f);
960        }        }
961          Stream_write_char(f, ')');
962        break;        break;
963      default:      default:
964        abort();        abort();
965        break;        break;
966      }      }
967      Stream_write_string(f, ") {\n");      Stream_write_string(f, " {\n");
968      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
969      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
970      break;      break;
971    case TOK_THROW:    case TOK_THROW:
# Line 692  Line 978 
978    case TOK_TRY:    case TOK_TRY:
979      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
980      Stream_write_string(f, "try {\n");      Stream_write_string(f, "try {\n");
981      instrument_statement(node->pn_kid1, f, indent + 2);      instrument_statement(node->pn_kid1, f, indent + 2, false);
982      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
983      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
984      {      if (node->pn_kid2) {
985        for (JSParseNode * catch = node->pn_kid2; catch != NULL; catch = catch->pn_kid2) {        assert(node->pn_kid2->pn_type == TOK_RESERVED);
986          for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) {
987            assert(scope->pn_type == TOK_LEXICALSCOPE);
988            JSParseNode * catch = scope->pn_expr;
989          assert(catch->pn_type == TOK_CATCH);          assert(catch->pn_type == TOK_CATCH);
990          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
991          Stream_write_string(f, "catch (");          Stream_write_string(f, "catch (");
992            /* this may not be a name - destructuring assignment */
993            /*
994          assert(catch->pn_kid1->pn_arity == PN_NAME);          assert(catch->pn_kid1->pn_arity == PN_NAME);
995          print_string_atom(catch->pn_kid1->pn_atom, f);          print_string_atom(catch->pn_kid1->pn_atom, f);
996          if (catch->pn_kid1->pn_expr) {          */
997            instrument_expression(catch->pn_kid1, f);
998            if (catch->pn_kid2) {
999            Stream_write_string(f, " if ");            Stream_write_string(f, " if ");
1000            instrument_expression(catch->pn_kid1->pn_expr, f);            instrument_expression(catch->pn_kid2, f);
1001          }          }
1002          Stream_write_string(f, ") {\n");          Stream_write_string(f, ") {\n");
1003          instrument_statement(catch->pn_kid3, f, indent + 2);          instrument_statement(catch->pn_kid3, f, indent + 2, false);
1004          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
1005          Stream_write_string(f, "}\n");          Stream_write_string(f, "}\n");
1006        }        }
# Line 715  Line 1008 
1008      if (node->pn_kid3) {      if (node->pn_kid3) {
1009        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
1010        Stream_write_string(f, "finally {\n");        Stream_write_string(f, "finally {\n");
1011        instrument_statement(node->pn_kid3, f, indent + 2);        instrument_statement(node->pn_kid3, f, indent + 2, false);
1012        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
1013        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
1014      }      }
# Line 741  Line 1034 
1034      Stream_write_string(f, "with (");      Stream_write_string(f, "with (");
1035      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
1036      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
1037      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
1038      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
1039      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
1040      break;      break;
1041    case TOK_VAR:    case TOK_VAR:
1042      instrument_var_statement(node, f, indent);      Stream_printf(f, "%*s", indent, "");
1043        instrument_expression(node, f);
1044      Stream_write_string(f, ";\n");      Stream_write_string(f, ";\n");
1045      break;      break;
1046    case TOK_RETURN:    case TOK_RETURN:
# Line 779  Line 1073 
1073      /*      /*
1074      ... use output_statement instead of instrument_statement.      ... use output_statement instead of instrument_statement.
1075      */      */
1076      output_statement(node->pn_expr, f, indent);      output_statement(node->pn_expr, f, indent, false);
1077        break;
1078      case TOK_LEXICALSCOPE:
1079        /* let statement */
1080        assert(node->pn_arity == PN_NAME);
1081        switch (node->pn_expr->pn_type) {
1082        case TOK_LET:
1083          /* let statement */
1084          assert(node->pn_expr->pn_arity == PN_BINARY);
1085          instrument_statement(node->pn_expr, f, indent, false);
1086          break;
1087        case TOK_LC:
1088          /* block */
1089          Stream_printf(f, "%*s", indent, "");
1090          Stream_write_string(f, "{\n");
1091          instrument_statement(node->pn_expr, f, indent + 2, false);
1092          Stream_printf(f, "%*s", indent, "");
1093          Stream_write_string(f, "}\n");
1094          break;
1095        case TOK_FOR:
1096          instrument_statement(node->pn_expr, f, indent, false);
1097          break;
1098        default:
1099          abort();
1100          break;
1101        }
1102        break;
1103      case TOK_LET:
1104        switch (node->pn_arity) {
1105        case PN_BINARY:
1106          /* let statement */
1107          Stream_printf(f, "%*s", indent, "");
1108          Stream_write_string(f, "let (");
1109          assert(node->pn_left->pn_type == TOK_LP);
1110          assert(node->pn_left->pn_arity == PN_LIST);
1111          instrument_declarations(node->pn_left, f);
1112          Stream_write_string(f, ") {\n");
1113          instrument_statement(node->pn_right, f, indent + 2, false);
1114          Stream_printf(f, "%*s", indent, "");
1115          Stream_write_string(f, "}\n");
1116          break;
1117        case PN_LIST:
1118          /* let definition */
1119          Stream_printf(f, "%*s", indent, "");
1120          instrument_expression(node, f);
1121          Stream_write_string(f, ";\n");
1122          break;
1123        default:
1124          abort();
1125          break;
1126        }
1127        break;
1128      case TOK_DEBUGGER:
1129        Stream_printf(f, "%*s", indent, "");
1130        Stream_write_string(f, "debugger;\n");
1131      break;      break;
1132    default:    default:
1133      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 791  Line 1139 
1139  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
1140  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
1141  */  */
1142  static void instrument_statement(JSParseNode * node, Stream * f, int indent) {  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
1143    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC && node->pn_type != TOK_LEXICALSCOPE) {
1144      int line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
1145        if (line > num_lines) {
1146          fatal("%s: script contains more than 65,535 lines", file_id);
1147        }
1148    
1149      /* the root node has line number 0 */      /* the root node has line number 0 */
1150      if (line != 0) {      if (line != 0) {
1151        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
# Line 801  Line 1153 
1153        lines[line - 1] = 1;        lines[line - 1] = 1;
1154      }      }
1155    }    }
1156    output_statement(node, f, indent);    output_statement(node, f, indent, is_jscoverage_if);
1157  }  }
1158    
1159  void jscoverage_instrument_js(const char * id, Stream * input, Stream * output) {  static bool characters_start_with(const jschar * characters, size_t line_start, size_t line_end, const char * prefix) {
1160    file_id = id;    const jschar * characters_end = characters + line_end;
1161      const jschar * cp = characters + line_start;
1162    /* scan the javascript */    const char * bp = prefix;
1163    size_t input_length = input->length;    for (;;) {
1164    jschar * base = js_InflateString(context, (char *) input->data, &input_length);      if (*bp == '\0') {
1165    if (base == NULL) {        return true;
1166      fatal("out of memory");      }
1167        else if (cp == characters_end) {
1168          return false;
1169        }
1170        else if (*cp != *bp) {
1171          return false;
1172        }
1173        bp++;
1174        cp++;
1175    }    }
1176    JSTokenStream * token_stream = js_NewTokenStream(context, base, input_length, NULL, 1, NULL);  }
1177    if (token_stream == NULL) {  
1178      fatal("cannot create token stream from file: %s", file_id);  static bool characters_are_white_space(const jschar * characters, size_t line_start, size_t line_end) {
1179      /* XXX - other Unicode space */
1180      const jschar * end = characters + line_end;
1181      for (const jschar * p = characters + line_start; p < end; p++) {
1182        jschar c = *p;
1183        if (c == 0x9 || c == 0xB || c == 0xC || c == 0x20 || c == 0xA0) {
1184          continue;
1185        }
1186        else {
1187          return false;
1188        }
1189    }    }
1190      return true;
1191    }
1192    
1193    static void error_reporter(JSContext * context, const char * message, JSErrorReport * report) {
1194      fprintf(stderr, "jscoverage: parse error: line %u: %s\n", report->lineno, message);
1195    }
1196    
1197    void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) {
1198      file_id = id;
1199    
1200    /* parse the javascript */    /* parse the javascript */
1201    JSParseNode * node = js_ParseTokenStream(context, global, token_stream);    JSParseContext parse_context;
1202      if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) {
1203        fatal("cannot create token stream from file: %s", file_id);
1204      }
1205      JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);
1206      JSParseNode * node = js_ParseScript(context, global, &parse_context);
1207    if (node == NULL) {    if (node == NULL) {
1208        js_ReportUncaughtException(context);
1209      fatal("parse error in file: %s", file_id);      fatal("parse error in file: %s", file_id);
1210    }    }
1211    int num_lines = node->pn_pos.end.lineno;    JS_SetErrorReporter(context, old_error_reporter);
1212      num_lines = node->pn_pos.end.lineno;
1213    lines = xmalloc(num_lines);    lines = xmalloc(num_lines);
1214    for (int i = 0; i < num_lines; i++) {    for (unsigned int i = 0; i < num_lines; i++) {
1215      lines[i] = 0;      lines[i] = 0;
1216    }    }
1217    
1218      /* search code for conditionals */
1219      exclusive_directives = xnew(bool, num_lines);
1220      for (unsigned int i = 0; i < num_lines; i++) {
1221        exclusive_directives[i] = false;
1222      }
1223    
1224      bool has_conditionals = false;
1225      struct IfDirective * if_directives = NULL;
1226      size_t line_number = 0;
1227      size_t i = 0;
1228      while (i < num_characters) {
1229        if (line_number == UINT16_MAX) {
1230          fatal("%s: script has more than 65,535 lines", file_id);
1231        }
1232        line_number++;
1233        size_t line_start = i;
1234        jschar c;
1235        bool done = false;
1236        while (! done && i < num_characters) {
1237          c = characters[i];
1238          switch (c) {
1239          case '\r':
1240          case '\n':
1241          case 0x2028:
1242          case 0x2029:
1243            done = true;
1244            break;
1245          default:
1246            i++;
1247          }
1248        }
1249        size_t line_end = i;
1250        if (i < num_characters) {
1251          i++;
1252          if (c == '\r' && i < num_characters && characters[i] == '\n') {
1253            i++;
1254          }
1255        }
1256    
1257        if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) {
1258          has_conditionals = true;
1259    
1260          if (characters_are_white_space(characters, line_start + 16, line_end)) {
1261            exclusive_directives[line_number - 1] = true;
1262          }
1263          else {
1264            struct IfDirective * if_directive = xnew(struct IfDirective, 1);
1265            if_directive->condition_start = characters + line_start + 16;
1266            if_directive->condition_end = characters + line_end;
1267            if_directive->start_line = line_number;
1268            if_directive->end_line = 0;
1269            if_directive->next = if_directives;
1270            if_directives = if_directive;
1271          }
1272        }
1273        else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) {
1274          for (struct IfDirective * p = if_directives; p != NULL; p = p->next) {
1275            if (p->end_line == 0) {
1276              p->end_line = line_number;
1277              break;
1278            }
1279          }
1280        }
1281      }
1282    
1283    /*    /*
1284    An instrumented JavaScript file has 3 sections:    An instrumented JavaScript file has 4 sections:
1285    1. initialization    1. initialization
1286    2. instrumented source code    2. instrumented source code
1287    3. original source code    3. conditionals
1288      4. original source code
1289    */    */
1290    
1291    Stream * instrumented = Stream_new(0);    Stream * instrumented = Stream_new(0);
1292    instrument_statement(node, instrumented, 0);    instrument_statement(node, instrumented, 0, false);
1293      js_FinishParseContext(context, &parse_context);
1294    
1295    /* write line number info to the output */    /* write line number info to the output */
1296    Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");    Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");
1297    Stream_write_string(output, "if (! top._$jscoverage) {\n  top._$jscoverage = {};\n}\n");    if (jscoverage_mozilla) {
1298    Stream_write_string(output, "var _$jscoverage = top._$jscoverage;\n");      Stream_write_string(output, "try {\n");
1299        Stream_write_string(output, "  Components.utils.import('resource://gre/modules/jscoverage.jsm');\n");
1300        Stream_printf(output, "  dump('%s: successfully imported jscoverage module\\n');\n", id);
1301        Stream_write_string(output, "}\n");
1302        Stream_write_string(output, "catch (e) {\n");
1303        Stream_write_string(output, "  _$jscoverage = {};\n");
1304        Stream_printf(output, "  dump('%s: failed to import jscoverage module - coverage not available for this file\\n');\n", id);
1305        Stream_write_string(output, "}\n");
1306      }
1307      else {
1308        Stream_write_string(output, "if (! top._$jscoverage) {\n  top._$jscoverage = {};\n}\n");
1309        Stream_write_string(output, "var _$jscoverage = top._$jscoverage;\n");
1310      }
1311    Stream_printf(output, "if (! _$jscoverage['%s']) {\n", file_id);    Stream_printf(output, "if (! _$jscoverage['%s']) {\n", file_id);
1312    Stream_printf(output, "  _$jscoverage['%s'] = [];\n", file_id);    Stream_printf(output, "  _$jscoverage['%s'] = [];\n", file_id);
1313    for (int i = 0; i < num_lines; i++) {    for (int i = 0; i < num_lines; i++) {
# Line 853  Line 1318 
1318    Stream_write_string(output, "}\n");    Stream_write_string(output, "}\n");
1319    free(lines);    free(lines);
1320    lines = NULL;    lines = NULL;
1321      free(exclusive_directives);
1322      exclusive_directives = NULL;
1323    
1324      /* conditionals */
1325      if (has_conditionals) {
1326        Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);
1327      }
1328    
1329    /* copy the instrumented source code to the output */    /* copy the instrumented source code to the output */
1330    Stream_write(output, instrumented->data, instrumented->length);    Stream_write(output, instrumented->data, instrumented->length);
1331    Stream_write_char(output, '\n');  
1332      /* conditionals */
1333      for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) {
1334        Stream_write_string(output, "if (!(");
1335        print_javascript(if_directive->condition_start, if_directive->condition_end - if_directive->condition_start, output);
1336        Stream_write_string(output, ")) {\n");
1337        Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line);
1338        Stream_write_string(output, "}\n");
1339      }
1340    
1341      /* free */
1342      while (if_directives != NULL) {
1343        struct IfDirective * if_directive = if_directives;
1344        if_directives = if_directives->next;
1345        free(if_directive);
1346      }
1347    
1348    /* copy the original source to the output */    /* copy the original source to the output */
1349    size_t i = 0;    Stream_printf(output, "_$jscoverage['%s'].source = ", file_id);
1350    while (i < input_length) {    jscoverage_write_source(id, characters, num_characters, output);
1351      Stream_write_string(output, "// ");    Stream_printf(output, ";\n");
     size_t line_start = i;  
     while (i < input_length && base[i] != '\r' && base[i] != '\n') {  
       i++;  
     }  
1352    
1353      size_t line_end = i;    Stream_delete(instrumented);
1354      if (i < input_length) {  
1355        if (base[i] == '\r') {    file_id = NULL;
1356          line_end = i;  }
1357    
1358    void jscoverage_write_source(const char * id, const jschar * characters, size_t num_characters, Stream * output) {
1359      Stream_write_string(output, "[");
1360      if (jscoverage_highlight) {
1361        Stream * highlighted_stream = Stream_new(num_characters);
1362        jscoverage_highlight_js(context, id, characters, num_characters, highlighted_stream);
1363        size_t i = 0;
1364        while (i < highlighted_stream->length) {
1365          if (i > 0) {
1366            Stream_write_char(output, ',');
1367          }
1368    
1369          Stream_write_char(output, '"');
1370          bool done = false;
1371          while (! done) {
1372            char c = highlighted_stream->data[i];
1373            switch (c) {
1374            case 0x8:
1375              /* backspace */
1376              Stream_write_string(output, "\\b");
1377              break;
1378            case 0x9:
1379              /* horizontal tab */
1380              Stream_write_string(output, "\\t");
1381              break;
1382            case 0xa:
1383              /* line feed (new line) */
1384              done = true;
1385              break;
1386            /* IE doesn't support this */
1387            /*
1388            case 0xb:
1389              Stream_write_string(output, "\\v");
1390              break;
1391            */
1392            case 0xc:
1393              /* form feed */
1394              Stream_write_string(output, "\\f");
1395              break;
1396            case 0xd:
1397              /* carriage return */
1398              done = true;
1399              if (i + 1 < highlighted_stream->length && highlighted_stream->data[i + 1] == '\n') {
1400                i++;
1401              }
1402              break;
1403            case '"':
1404              Stream_write_string(output, "\\\"");
1405              break;
1406            case '\\':
1407              Stream_write_string(output, "\\\\");
1408              break;
1409            default:
1410              Stream_write_char(output, c);
1411              break;
1412            }
1413          i++;          i++;
1414          if (i < input_length && base[i] == '\n') {          if (i >= highlighted_stream->length) {
1415            i++;            done = true;
1416          }          }
1417        }        }
1418        else if (base[i] == '\n') {        Stream_write_char(output, '"');
1419          line_end = i;      }
1420          i++;      Stream_delete(highlighted_stream);
1421      }
1422      else {
1423        size_t i = 0;
1424        while (i < num_characters) {
1425          if (i > 0) {
1426            Stream_write_char(output, ',');
1427        }        }
1428        else {  
1429          abort();        Stream_write_char(output, '"');
1430          bool done = false;
1431          while (! done) {
1432            jschar c = characters[i];
1433            switch (c) {
1434            case 0x8:
1435              /* backspace */
1436              Stream_write_string(output, "\\b");
1437              break;
1438            case 0x9:
1439              /* horizontal tab */
1440              Stream_write_string(output, "\\t");
1441              break;
1442            case 0xa:
1443              /* line feed (new line) */
1444              done = true;
1445              break;
1446            /* IE doesn't support this */
1447            /*
1448            case 0xb:
1449              Stream_write_string(output, "\\v");
1450              break;
1451            */
1452            case 0xc:
1453              /* form feed */
1454              Stream_write_string(output, "\\f");
1455              break;
1456            case 0xd:
1457              /* carriage return */
1458              done = true;
1459              if (i + 1 < num_characters && characters[i + 1] == '\n') {
1460                i++;
1461              }
1462              break;
1463            case '"':
1464              Stream_write_string(output, "\\\"");
1465              break;
1466            case '\\':
1467              Stream_write_string(output, "\\\\");
1468              break;
1469            case '&':
1470              Stream_write_string(output, "&amp;");
1471              break;
1472            case '<':
1473              Stream_write_string(output, "&lt;");
1474              break;
1475            case '>':
1476              Stream_write_string(output, "&gt;");
1477              break;
1478            case 0x2028:
1479            case 0x2029:
1480              done = true;
1481              break;
1482            default:
1483              if (32 <= c && c <= 126) {
1484                Stream_write_char(output, c);
1485              }
1486              else {
1487                Stream_printf(output, "&#%d;", c);
1488              }
1489              break;
1490            }
1491            i++;
1492            if (i >= num_characters) {
1493              done = true;
1494            }
1495        }        }
1496          Stream_write_char(output, '"');
1497      }      }
   
     char * line = js_DeflateString(context, base + line_start, line_end - line_start);  
     Stream_write_string(output, line);  
     Stream_write_char(output, '\n');  
     JS_free(context, line);  
1498    }    }
1499      Stream_write_string(output, "]");
   Stream_delete(instrumented);  
   
   JS_free(context, base);  
   
   file_id = NULL;  
1500  }  }
1501    
1502  void jscoverage_copy_resources(const char * destination_directory) {  void jscoverage_copy_resources(const char * destination_directory) {
1503    copy_resource("jscoverage.html", destination_directory);    copy_resource("jscoverage.html", destination_directory);
1504    copy_resource("jscoverage.css", destination_directory);    copy_resource("jscoverage.css", destination_directory);
1505    copy_resource("jscoverage.js", destination_directory);    copy_resource("jscoverage.js", destination_directory);
1506      copy_resource("jscoverage-ie.css", destination_directory);
1507    copy_resource("jscoverage-throbber.gif", destination_directory);    copy_resource("jscoverage-throbber.gif", destination_directory);
1508    copy_resource("jscoverage-sh_main.js", destination_directory);    copy_resource("jscoverage-highlight.css", destination_directory);
   copy_resource("jscoverage-sh_javascript.js", destination_directory);  
   copy_resource("jscoverage-sh_nedit.css", destination_directory);  
1509  }  }
1510    
1511  /*  /*
# Line 940  Line 1540 
1540    JS_HashTableDestroy(coverage->coverage_table);    JS_HashTableDestroy(coverage->coverage_table);
1541    struct FileCoverageList * p = coverage->coverage_list;    struct FileCoverageList * p = coverage->coverage_list;
1542    while (p != NULL) {    while (p != NULL) {
1543      free(p->file_coverage->lines);      free(p->file_coverage->coverage_lines);
1544      free(p->file_coverage->source);      if (p->file_coverage->source_lines != NULL) {
1545          for (uint32 i = 0; i < p->file_coverage->num_source_lines; i++) {
1546            free(p->file_coverage->source_lines[i]);
1547          }
1548          free(p->file_coverage->source_lines);
1549        }
1550      free(p->file_coverage->id);      free(p->file_coverage->id);
1551      free(p->file_coverage);      free(p->file_coverage);
1552      struct FileCoverageList * q = p;      struct FileCoverageList * q = p;
# Line 970  Line 1575 
1575  }  }
1576    
1577  int jscoverage_parse_json(Coverage * coverage, const uint8_t * json, size_t length) {  int jscoverage_parse_json(Coverage * coverage, const uint8_t * json, size_t length) {
1578      int result = 0;
1579    
1580    jschar * base = js_InflateString(context, (char *) json, &length);    jschar * base = js_InflateString(context, (char *) json, &length);
1581    if (base == NULL) {    if (base == NULL) {
1582      fatal("out of memory");      fatal("out of memory");
# Line 982  Line 1589 
1589    
1590    JS_free(context, base);    JS_free(context, base);
1591    
1592    JSTokenStream * token_stream = js_NewTokenStream(context, parenthesized_json, length + 2, NULL, 1, NULL);    JSParseContext parse_context;
1593    if (token_stream == NULL) {    if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) {
1594      fatal("cannot create token stream");      free(parenthesized_json);
1595        return -1;
1596    }    }
1597      JSParseNode * root = js_ParseScript(context, global, &parse_context);
   JSParseNode * root = js_ParseTokenStream(context, global, token_stream);  
1598    free(parenthesized_json);    free(parenthesized_json);
1599    if (root == NULL) {    if (root == NULL) {
1600      return -1;      result = -1;
1601        goto done;
1602    }    }
1603    
1604    /* root node must be TOK_LC */    /* root node must be TOK_LC */
1605    if (root->pn_type != TOK_LC) {    if (root->pn_type != TOK_LC) {
1606      return -1;      result = -1;
1607        goto done;
1608    }    }
1609    JSParseNode * semi = root->pn_u.list.head;    JSParseNode * semi = root->pn_u.list.head;
1610    
1611    /* the list must be TOK_SEMI and it must contain only one element */    /* the list must be TOK_SEMI and it must contain only one element */
1612    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {
1613      return -1;      result = -1;
1614        goto done;
1615    }    }
1616    JSParseNode * parenthesized = semi->pn_kid;    JSParseNode * parenthesized = semi->pn_kid;
1617    
1618    /* this must be a parenthesized expression */    /* this must be a parenthesized expression */
1619    if (parenthesized->pn_type != TOK_RP) {    if (parenthesized->pn_type != TOK_RP) {
1620      return -1;      result = -1;
1621        goto done;
1622    }    }
1623    JSParseNode * object = parenthesized->pn_kid;    JSParseNode * object = parenthesized->pn_kid;
1624    
1625    /* this must be an object literal */    /* this must be an object literal */
1626    if (object->pn_type != TOK_RC) {    if (object->pn_type != TOK_RC) {
1627      return -1;      result = -1;
1628        goto done;
1629    }    }
1630    
1631    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {
1632      /* every element of this list must be TOK_COLON */      /* every element of this list must be TOK_COLON */
1633      if (p->pn_type != TOK_COLON) {      if (p->pn_type != TOK_COLON) {
1634        return -1;        result = -1;
1635          goto done;
1636      }      }
1637    
1638      /* the key must be a string representing the file */      /* the key must be a string representing the file */
1639      JSParseNode * key = p->pn_left;      JSParseNode * key = p->pn_left;
1640      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {
1641        return -1;        result = -1;
1642          goto done;
1643      }      }
1644      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));
1645    
1646      /* the value must be an object literal OR an array */      /* the value must be an object literal OR an array */
1647      JSParseNode * value = p->pn_right;      JSParseNode * value = p->pn_right;
1648      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {
1649        return -1;        result = -1;
1650          goto done;
1651      }      }
1652    
1653      JSParseNode * array = NULL;      JSParseNode * array = NULL;
# Line 1044  Line 1659 
1659      else if (value->pn_type == TOK_RC) {      else if (value->pn_type == TOK_RC) {
1660        /* an object literal */        /* an object literal */
1661        if (value->pn_count != 2) {        if (value->pn_count != 2) {
1662          return -1;          result = -1;
1663            goto done;
1664        }        }
1665        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {
1666          if (element->pn_type != TOK_COLON) {          if (element->pn_type != TOK_COLON) {
1667            return -1;            result = -1;
1668              goto done;
1669          }          }
1670          JSParseNode * left = element->pn_left;          JSParseNode * left = element->pn_left;
1671          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {
1672            return -1;            result = -1;
1673              goto done;
1674          }          }
1675          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));
1676          if (strcmp(s, "coverage") == 0) {          if (strcmp(s, "coverage") == 0) {
1677            array = element->pn_right;            array = element->pn_right;
1678            if (array->pn_type != TOK_RB) {            if (array->pn_type != TOK_RB) {
1679              return -1;              result = -1;
1680                goto done;
1681            }            }
1682          }          }
1683          else if (strcmp(s, "source") == 0) {          else if (strcmp(s, "source") == 0) {
1684            source = element->pn_right;            source = element->pn_right;
1685            if (source->pn_type != TOK_STRING || ! ATOM_IS_STRING(source->pn_atom)) {            if (source->pn_type != TOK_RB) {
1686              return -1;              result = -1;
1687                goto done;
1688            }            }
1689          }          }
1690          else {          else {
1691            return -1;            result = -1;
1692              goto done;
1693          }          }
1694        }        }
1695      }      }
1696      else {      else {
1697        return -1;        result = -1;
1698          goto done;
1699      }      }
1700    
1701      if (array == NULL) {      if (array == NULL) {
1702        return -1;        result = -1;
1703          goto done;
1704      }      }
1705    
1706      /* look up the file in the coverage table */      /* look up the file in the coverage table */
# Line 1087  Line 1710 
1710        char * id = xstrdup(id_bytes);        char * id = xstrdup(id_bytes);
1711        file_coverage = xmalloc(sizeof(FileCoverage));        file_coverage = xmalloc(sizeof(FileCoverage));
1712        file_coverage->id = id;        file_coverage->id = id;
1713        file_coverage->num_lines = array->pn_count - 1;        file_coverage->num_coverage_lines = array->pn_count;
1714        file_coverage->lines = xnew(int, array->pn_count);        file_coverage->coverage_lines = xnew(int, array->pn_count);
1715        if (source == NULL) {        file_coverage->source_lines = NULL;
         file_coverage->source = NULL;  
       }  
       else {  
         file_coverage->source = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(source->pn_atom)));  
       }  
1716    
1717        /* set coverage for all lines */        /* set coverage for all lines */
1718        uint32 i = 0;        uint32 i = 0;
1719        for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {        for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {
1720          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1721            file_coverage->lines[i] = (int) element->pn_dval;            file_coverage->coverage_lines[i] = (int) element->pn_dval;
1722          }          }
1723          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1724            file_coverage->lines[i] = -1;            file_coverage->coverage_lines[i] = -1;
1725          }          }
1726          else {          else {
1727            return -1;            result = -1;
1728              goto done;
1729          }          }
1730        }        }
1731        assert(i == array->pn_count);        assert(i == array->pn_count);
# Line 1121  Line 1740 
1740      else {      else {
1741        /* sanity check */        /* sanity check */
1742        assert(strcmp(file_coverage->id, id_bytes) == 0);        assert(strcmp(file_coverage->id, id_bytes) == 0);
1743        if (file_coverage->num_lines != array->pn_count - 1) {        if (file_coverage->num_coverage_lines != array->pn_count) {
1744          return -2;          result = -2;
1745            goto done;
1746        }        }
1747    
1748        /* merge the coverage */        /* merge the coverage */
1749        uint32 i = 0;        uint32 i = 0;
1750        for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {        for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {
1751          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1752            if (file_coverage->lines[i] == -1) {            if (file_coverage->coverage_lines[i] == -1) {
1753              return -2;              result = -2;
1754                goto done;
1755            }            }
1756            file_coverage->lines[i] += (int) element->pn_dval;            file_coverage->coverage_lines[i] += (int) element->pn_dval;
1757          }          }
1758          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1759            if (file_coverage->lines[i] != -1) {            if (file_coverage->coverage_lines[i] != -1) {
1760              return -2;              result = -2;
1761                goto done;
1762            }            }
1763          }          }
1764          else {          else {
1765            return -1;            result = -1;
1766              goto done;
1767          }          }
1768        }        }
1769        assert(i == array->pn_count);        assert(i == array->pn_count);
1770        }
1771    
1772        /* if this JSON file has source, use it */      /* if this JSON file has source, use it */
1773        if (file_coverage->source == NULL && source != NULL) {      if (file_coverage->source_lines == NULL && source != NULL) {
1774          file_coverage->source = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(source->pn_atom)));        file_coverage->num_source_lines = source->pn_count;
1775          file_coverage->source_lines = xnew(char *, source->pn_count);
1776          uint32 i = 0;
1777          for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {
1778            if (element->pn_type != TOK_STRING) {
1779              result = -1;
1780              goto done;
1781            }
1782            file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));
1783        }        }
1784          assert(i == source->pn_count);
1785      }      }
1786    }    }
1787    
1788    return 0;  done:
1789      js_FinishParseContext(context, &parse_context);
1790      return result;
1791  }  }

Legend:
Removed from v.116  
changed lines
  Added in v.361

  ViewVC Help
Powered by ViewVC 1.1.24