/[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 95 by siliconforks, Wed May 7 22:50:00 2008 UTC revision 340 by siliconforks, Fri Oct 24 16:14:20 2008 UTC
# 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 24  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 <jsexn.h>
32  #include <jsfun.h>  #include <jsfun.h>
33  #include <jsinterp.h>  #include <jsinterp.h>
34    #include <jsiter.h>
35  #include <jsparse.h>  #include <jsparse.h>
36  #include <jsregexp.h>  #include <jsregexp.h>
37  #include <jsscope.h>  #include <jsscope.h>
38  #include <jsstr.h>  #include <jsstr.h>
39    
40    #include "encoding.h"
41    #include "global.h"
42    #include "highlight.h"
43    #include "resource-manager.h"
44  #include "util.h"  #include "util.h"
45    
46    struct IfDirective {
47      const jschar * condition_start;
48      const jschar * condition_end;
49      uint16_t start_line;
50      uint16_t end_line;
51      struct IfDirective * next;
52    };
53    
54    static bool * exclusive_directives = NULL;
55    
56  static JSRuntime * runtime = NULL;  static JSRuntime * runtime = NULL;
57  static JSContext * context = NULL;  static JSContext * context = NULL;
58  static JSObject * global = NULL;  static JSObject * global = NULL;
59    static JSVersion js_version = JSVERSION_ECMA_3;
60    
61  /*  /*
62  JSParseNode objects store line numbers starting from 1.  JSParseNode objects store line numbers starting from 1.
# Line 44  Line 64 
64  */  */
65  static const char * file_id = NULL;  static const char * file_id = NULL;
66  static char * lines = NULL;  static char * lines = NULL;
67    static uint16_t num_lines = 0;
68    
69    void jscoverage_set_js_version(const char * version) {
70      js_version = atoi(version);
71    }
72    
73  void jscoverage_init(void) {  void jscoverage_init(void) {
74    runtime = JS_NewRuntime(8L * 1024L * 1024L);    runtime = JS_NewRuntime(8L * 1024L * 1024L);
# Line 56  Line 81 
81      fatal("cannot create context");      fatal("cannot create context");
82    }    }
83    
84      JS_SetVersion(context, js_version);
85    
86    global = JS_NewObject(context, NULL, NULL, NULL);    global = JS_NewObject(context, NULL, NULL, NULL);
87    if (global == NULL) {    if (global == NULL) {
88      fatal("cannot create global object");      fatal("cannot create global object");
# Line 71  Line 98 
98    JS_DestroyRuntime(runtime);    JS_DestroyRuntime(runtime);
99  }  }
100    
101    static void print_javascript(const jschar * characters, size_t num_characters, Stream * f) {
102      for (size_t i = 0; i < num_characters; i++) {
103        jschar c = characters[i];
104        /*
105        XXX does not handle no-break space, other unicode "space separator"
106        */
107        switch (c) {
108        case 0x9:
109        case 0xB:
110        case 0xC:
111          Stream_write_char(f, c);
112          break;
113        default:
114          if (32 <= c && c <= 126) {
115            Stream_write_char(f, c);
116          }
117          else {
118            Stream_printf(f, "\\u%04x", c);
119          }
120          break;
121        }
122      }
123    }
124    
125  static void print_string(JSString * s, Stream * f) {  static void print_string(JSString * s, Stream * f) {
126    for (int i = 0; i < s->length; i++) {    size_t length = JSSTRING_LENGTH(s);
127      char c = s->chars[i];    jschar * characters = JSSTRING_CHARS(s);
128      Stream_write_char(f, c);    for (size_t i = 0; i < length; i++) {
129        jschar c = characters[i];
130        if (32 <= c && c <= 126) {
131          switch (c) {
132          case '"':
133            Stream_write_string(f, "\\\"");
134            break;
135    /*
136          case '\'':
137            Stream_write_string(f, "\\'");
138            break;
139    */
140          case '\\':
141            Stream_write_string(f, "\\\\");
142            break;
143          default:
144            Stream_write_char(f, c);
145            break;
146          }
147        }
148        else {
149          switch (c) {
150          case 0x8:
151            Stream_write_string(f, "\\b");
152            break;
153          case 0x9:
154            Stream_write_string(f, "\\t");
155            break;
156          case 0xa:
157            Stream_write_string(f, "\\n");
158            break;
159          /* IE doesn't support this */
160          /*
161          case 0xb:
162            Stream_write_string(f, "\\v");
163            break;
164          */
165          case 0xc:
166            Stream_write_string(f, "\\f");
167            break;
168          case 0xd:
169            Stream_write_string(f, "\\r");
170            break;
171          default:
172            Stream_printf(f, "\\u%04x", c);
173            break;
174          }
175        }
176    }    }
177  }  }
178    
# Line 84  Line 182 
182    print_string(s, f);    print_string(s, f);
183  }  }
184    
185  static void print_string_jsval(jsval value, Stream * f) {  static void print_regex(jsval value, Stream * f) {
186    assert(JSVAL_IS_STRING(value));    assert(JSVAL_IS_STRING(value));
187    JSString * s = JSVAL_TO_STRING(value);    JSString * s = JSVAL_TO_STRING(value);
188    print_string(s, f);    size_t length = JSSTRING_LENGTH(s);
189      jschar * characters = JSSTRING_CHARS(s);
190      for (size_t i = 0; i < length; i++) {
191        jschar c = characters[i];
192        if (32 <= c && c <= 126) {
193          Stream_write_char(f, c);
194        }
195        else {
196          Stream_printf(f, "\\u%04x", c);
197        }
198      }
199  }  }
200    
201  static void print_quoted_string_atom(JSAtom * atom, Stream * f) {  static void print_quoted_string_atom(JSAtom * atom, Stream * f) {
202    assert(ATOM_IS_STRING(atom));    assert(ATOM_IS_STRING(atom));
203    JSString * s = ATOM_TO_STRING(atom);    JSString * s = ATOM_TO_STRING(atom);
204    JSString * quoted = js_QuoteString(context, s, '"');    Stream_write_char(f, '"');
205    print_string(quoted, f);    print_string(s, f);
206      Stream_write_char(f, '"');
207  }  }
208    
209  static const char * get_op(uint8 op) {  static const char * get_op(uint8 op) {
210    switch(op) {    switch(op) {
211      case JSOP_OR:
212        return "||";
213      case JSOP_AND:
214        return "&&";
215    case JSOP_BITOR:    case JSOP_BITOR:
216      return "|";      return "|";
217    case JSOP_BITXOR:    case JSOP_BITXOR:
# Line 109  Line 222 
222      return "==";      return "==";
223    case JSOP_NE:    case JSOP_NE:
224      return "!=";      return "!=";
225    case JSOP_NEW_EQ:    case JSOP_STRICTEQ:
226      return "===";      return "===";
227    case JSOP_NEW_NE:    case JSOP_STRICTNE:
228      return "!==";      return "!==";
229    case JSOP_LT:    case JSOP_LT:
230      return "<";      return "<";
# Line 143  Line 256 
256  }  }
257    
258  static void instrument_expression(JSParseNode * node, Stream * f);  static void instrument_expression(JSParseNode * node, Stream * f);
259  static void instrument_statement(JSParseNode * node, Stream * f, int indent);  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
260    
261  static void instrument_function(JSParseNode * node, Stream * f, int indent) {  enum FunctionType {
262      assert(node->pn_arity == PN_FUNC);    FUNCTION_NORMAL,
263      assert(ATOM_IS_OBJECT(node->pn_funAtom));    FUNCTION_GETTER_OR_SETTER
264      JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom);  };
265      assert(JS_ObjectIsFunction(context, object));  
266      JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);  static void instrument_function(JSParseNode * node, Stream * f, int indent, enum FunctionType type) {
267      assert(function);    assert(node->pn_type == TOK_FUNCTION);
268      assert(object == function->object);    assert(node->pn_arity == PN_FUNC);
269      Stream_printf(f, "%*s", indent, "");    JSObject * object = node->pn_funpob->object;
270      assert(JS_ObjectIsFunction(context, object));
271      JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
272      assert(function);
273      assert(object == &function->object);
274      Stream_printf(f, "%*s", indent, "");
275      if (type == FUNCTION_NORMAL) {
276      Stream_write_string(f, "function");      Stream_write_string(f, "function");
277      }
278    
279      /* function name */    /* function name */
280      if (function->atom) {    if (function->atom) {
281        Stream_write_char(f, ' ');      Stream_write_char(f, ' ');
282        print_string_atom(function->atom, f);      print_string_atom(function->atom, f);
283      }    }
284    
285      /* function parameters */    /*
286      Stream_write_string(f, "(");    function parameters - see JS_DecompileFunction in jsapi.cpp, which calls
287      JSAtom ** params = xmalloc(function->nargs * sizeof(JSAtom *));    js_DecompileFunction in jsopcode.cpp
288      for (int i = 0; i < function->nargs; i++) {    */
289        /* initialize to NULL for sanity check */    Stream_write_string(f, "(");
290        params[i] = NULL;    JSArenaPool pool;
291      }    JS_INIT_ARENA_POOL(&pool, "instrument_function", 256, 1, &context->scriptStackQuota);
292      JSScope * scope = OBJ_SCOPE(object);    jsuword * local_names = NULL;
293      for (JSScopeProperty * scope_property = SCOPE_LAST_PROP(scope); scope_property != NULL; scope_property = scope_property->parent) {    if (JS_GET_LOCAL_NAME_COUNT(function)) {
294        if (scope_property->getter != js_GetArgument) {      local_names = js_GetLocalNameArray(context, function, &pool);
295          continue;      if (local_names == NULL) {
296        }        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);  
297      }      }
298      for (int i = 0; i < function->nargs; i++) {    }
299        assert(params[i] != NULL);    for (int i = 0; i < function->nargs; i++) {
300        if (i > 0) {      if (i > 0) {
301          Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
       }  
       if (ATOM_IS_STRING(params[i])) {  
         print_string_atom(params[i], f);  
       }  
302      }      }
303      Stream_write_string(f, ") {\n");      JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]);
304      free(params);      print_string_atom(param, f);
305      }
306      JS_FinishArenaPool(&pool);
307      Stream_write_string(f, ") {\n");
308    
309      /* function body */    /* function body */
310      instrument_statement(node->pn_body, f, indent + 2);    instrument_statement(node->pn_body, f, indent + 2, false);
311    
312      Stream_write_string(f, "}\n");    Stream_write_string(f, "}\n");
313  }  }
314    
315  static void instrument_function_call(JSParseNode * node, Stream * f) {  static void instrument_function_call(JSParseNode * node, Stream * f) {
# Line 209  Line 324 
324    Stream_write_char(f, ')');    Stream_write_char(f, ')');
325  }  }
326    
327    static void instrument_declarations(JSParseNode * list, Stream * f) {
328      assert(list->pn_arity == PN_LIST);
329      for (JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
330        assert(p->pn_type == TOK_NAME);
331        assert(p->pn_arity == PN_NAME);
332        if (p != list->pn_head) {
333          Stream_write_string(f, ", ");
334        }
335        print_string_atom(p->pn_atom, f);
336        if (p->pn_expr != NULL) {
337          Stream_write_string(f, " = ");
338          instrument_expression(p->pn_expr, f);
339        }
340      }
341    }
342    
343  /*  /*
344  See <Expressions> in jsparse.h.  See <Expressions> in jsparse.h.
345  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
# Line 225  Line 356 
356  static void instrument_expression(JSParseNode * node, Stream * f) {  static void instrument_expression(JSParseNode * node, Stream * f) {
357    switch (node->pn_type) {    switch (node->pn_type) {
358    case TOK_FUNCTION:    case TOK_FUNCTION:
359      instrument_function(node, f, 0);      instrument_function(node, f, 0, FUNCTION_NORMAL);
360      break;      break;
361    case TOK_COMMA:    case TOK_COMMA:
362      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 267  Line 398 
398      instrument_expression(node->pn_kid3, f);      instrument_expression(node->pn_kid3, f);
399      break;      break;
400    case TOK_OR:    case TOK_OR:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " || ");  
     instrument_expression(node->pn_right, f);  
     break;  
401    case TOK_AND:    case TOK_AND:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " && ");  
     instrument_expression(node->pn_right, f);  
     break;  
402    case TOK_BITOR:    case TOK_BITOR:
403    case TOK_BITXOR:    case TOK_BITXOR:
404    case TOK_BITAND:    case TOK_BITAND:
# Line 388  Line 511 
511      assert(ATOM_IS_STRING(node->pn_atom));      assert(ATOM_IS_STRING(node->pn_atom));
512      {      {
513        JSString * s = ATOM_TO_STRING(node->pn_atom);        JSString * s = ATOM_TO_STRING(node->pn_atom);
514        /* XXX - semantics changed in 1.7 */        bool is_keyword = (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF);
515        if (! ATOM_KEYWORD(node->pn_atom) && js_IsIdentifier(s)) {        if (! is_keyword && js_IsIdentifier(s)) {
516          Stream_write_char(f, '.');          Stream_write_char(f, '.');
517          print_string_atom(node->pn_atom, f);          print_string_atom(node->pn_atom, f);
518        }        }
# Line 432  Line 555 
555        if (p != node->pn_head) {        if (p != node->pn_head) {
556          Stream_write_string(f, ", ");          Stream_write_string(f, ", ");
557        }        }
558        instrument_expression(p->pn_left, f);  
559        Stream_write_string(f, ": ");        /* check whether this is a getter or setter */
560        instrument_expression(p->pn_right, f);        switch (p->pn_op) {
561          case JSOP_GETTER:
562          case JSOP_SETTER:
563            if (p->pn_op == JSOP_GETTER) {
564              Stream_write_string(f, "get ");
565            }
566            else {
567              Stream_write_string(f, "set ");
568            }
569            instrument_expression(p->pn_left, f);
570            if (p->pn_right->pn_type != TOK_FUNCTION) {
571              fatal("parse error: expected function");
572            }
573            instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);
574            break;
575          default:
576            instrument_expression(p->pn_left, f);
577            Stream_write_string(f, ": ");
578            instrument_expression(p->pn_right, f);
579            break;
580          }
581      }      }
582      Stream_write_char(f, '}');      Stream_write_char(f, '}');
583      break;      break;
# Line 449  Line 592 
592    case TOK_STRING:    case TOK_STRING:
593      print_quoted_string_atom(node->pn_atom, f);      print_quoted_string_atom(node->pn_atom, f);
594      break;      break;
595    case TOK_OBJECT:    case TOK_REGEXP:
596      switch (node->pn_op) {      assert(node->pn_op == JSOP_REGEXP);
597      case JSOP_OBJECT:      {
598        /* I assume this is JSOP_REGEXP */        JSObject * object = node->pn_pob->object;
599        abort();        jsval result;
600        break;        js_regexp_toString(context, object, &result);
601      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;  
602      }      }
603      break;      break;
604    case TOK_NUMBER:    case TOK_NUMBER:
# Line 512  Line 644 
644      Stream_write_string(f, " in ");      Stream_write_string(f, " in ");
645      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
646      break;      break;
647      case TOK_LEXICALSCOPE:
648        assert(node->pn_arity == PN_NAME);
649        assert(node->pn_expr->pn_type == TOK_LET);
650        assert(node->pn_expr->pn_arity == PN_BINARY);
651        Stream_write_string(f, "let(");
652        assert(node->pn_expr->pn_left->pn_type == TOK_LP);
653        assert(node->pn_expr->pn_left->pn_arity == PN_LIST);
654        instrument_declarations(node->pn_expr->pn_left, f);
655        Stream_write_string(f, ") ");
656        instrument_expression(node->pn_expr->pn_right, f);
657        break;
658    default:    default:
659      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);
660    }    }
# Line 521  Line 664 
664    assert(node->pn_arity == PN_LIST);    assert(node->pn_arity == PN_LIST);
665    Stream_printf(f, "%*s", indent, "");    Stream_printf(f, "%*s", indent, "");
666    Stream_write_string(f, "var ");    Stream_write_string(f, "var ");
667    for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) {    instrument_declarations(node, f);
     assert(p->pn_type == TOK_NAME);  
     assert(p->pn_arity == PN_NAME);  
     if (p != node->pn_head) {  
       Stream_write_string(f, ", ");  
     }  
     print_string_atom(p->pn_atom, f);  
     if (p->pn_expr != NULL) {  
       Stream_write_string(f, " = ");  
       instrument_expression(p->pn_expr, f);  
     }  
   }  
668  }  }
669    
670  static void output_statement(JSParseNode * node, Stream * f, int indent) {  static void instrument_let_definition(JSParseNode * node, Stream * f, int indent) {
671      assert(node->pn_arity == PN_LIST);
672      Stream_printf(f, "%*s", indent, "");
673      Stream_write_string(f, "let ");
674      instrument_declarations(node, f);
675    }
676    
677    static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
678    switch (node->pn_type) {    switch (node->pn_type) {
679    case TOK_FUNCTION:    case TOK_FUNCTION:
680      instrument_function(node, f, indent);      instrument_function(node, f, indent, FUNCTION_NORMAL);
681      break;      break;
682    case TOK_LC:    case TOK_LC:
683      assert(node->pn_arity == PN_LIST);      assert(node->pn_arity == PN_LIST);
# Line 546  Line 685 
685      Stream_write_string(f, "{\n");      Stream_write_string(f, "{\n");
686  */  */
687      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) {
688        instrument_statement(p, f, indent);        instrument_statement(p, f, indent, false);
689      }      }
690  /*  /*
691      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 554  Line 693 
693  */  */
694      break;      break;
695    case TOK_IF:    case TOK_IF:
696      {
697      assert(node->pn_arity == PN_TERNARY);      assert(node->pn_arity == PN_TERNARY);
698    
699        uint16_t line = node->pn_pos.begin.lineno;
700        if (! is_jscoverage_if) {
701          if (line > num_lines) {
702            fatal("%s: script contains more than 65,535 lines", file_id);
703          }
704          if (line >= 2 && exclusive_directives[line - 2]) {
705            is_jscoverage_if = true;
706          }
707        }
708    
709      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
710      Stream_write_string(f, "if (");      Stream_write_string(f, "if (");
711      instrument_expression(node->pn_kid1, f);      instrument_expression(node->pn_kid1, f);
712      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
713      instrument_statement(node->pn_kid2, f, indent + 2);      if (is_jscoverage_if && node->pn_kid3) {
714          uint16_t else_start = node->pn_kid3->pn_pos.begin.lineno;
715          uint16_t else_end = node->pn_kid3->pn_pos.end.lineno + 1;
716          Stream_printf(f, "%*s", indent + 2, "");
717          Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, else_start, else_end);
718        }
719        instrument_statement(node->pn_kid2, f, indent + 2, false);
720      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
721      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
722      if (node->pn_kid3) {  
723        if (node->pn_kid3 || is_jscoverage_if) {
724        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
725        Stream_write_string(f, "else {\n");        Stream_write_string(f, "else {\n");
726        instrument_statement(node->pn_kid3, f, indent + 2);  
727          if (is_jscoverage_if) {
728            uint16_t if_start = node->pn_kid2->pn_pos.begin.lineno + 1;
729            uint16_t if_end = node->pn_kid2->pn_pos.end.lineno + 1;
730            Stream_printf(f, "%*s", indent + 2, "");
731            Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_start, if_end);
732          }
733    
734          if (node->pn_kid3) {
735            instrument_statement(node->pn_kid3, f, indent + 2, is_jscoverage_if);
736          }
737    
738        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
739        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
740      }      }
741    
742      break;      break;
743      }
744    case TOK_SWITCH:    case TOK_SWITCH:
745      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
746      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 591  Line 762 
762          abort();          abort();
763          break;          break;
764        }        }
765        instrument_statement(p->pn_right, f, indent + 2);        instrument_statement(p->pn_right, f, indent + 2, false);
766      }      }
767      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
768      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
# Line 606  Line 777 
777      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
778      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
779      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
780      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
781      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
782      break;      break;
783    case TOK_DO:    case TOK_DO:
784      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
785      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
786      Stream_write_string(f, "do {\n");      Stream_write_string(f, "do {\n");
787      instrument_statement(node->pn_left, f, indent + 2);      instrument_statement(node->pn_left, f, indent + 2, false);
788      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
789      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
790      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
# Line 623  Line 794 
794    case TOK_FOR:    case TOK_FOR:
795      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
796      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
797      Stream_write_string(f, "for (");      Stream_write_string(f, "for ");
798        if (node->pn_iflags & JSITER_FOREACH) {
799          Stream_write_string(f, "each ");
800        }
801        Stream_write_char(f, '(');
802      switch (node->pn_left->pn_type) {      switch (node->pn_left->pn_type) {
803      case TOK_IN:      case TOK_IN:
804        /* for/in */        /* for/in */
# Line 632  Line 807 
807        case TOK_VAR:        case TOK_VAR:
808          instrument_var_statement(node->pn_left->pn_left, f, 0);          instrument_var_statement(node->pn_left->pn_left, f, 0);
809          break;          break;
810          case TOK_LET:
811            instrument_let_definition(node->pn_left->pn_left, f, 0);
812            break;
813        case TOK_NAME:        case TOK_NAME:
814          instrument_expression(node->pn_left->pn_left, f);          instrument_expression(node->pn_left->pn_left, f);
815          break;          break;
# Line 653  Line 831 
831        /* for (;;) */        /* for (;;) */
832        assert(node->pn_left->pn_arity == PN_TERNARY);        assert(node->pn_left->pn_arity == PN_TERNARY);
833        if (node->pn_left->pn_kid1) {        if (node->pn_left->pn_kid1) {
834          if (node->pn_left->pn_kid1->pn_type == TOK_VAR) {          switch (node->pn_left->pn_kid1->pn_type) {
835            case TOK_VAR:
836            instrument_var_statement(node->pn_left->pn_kid1, f, 0);            instrument_var_statement(node->pn_left->pn_kid1, f, 0);
837          }            break;
838          else {          case TOK_LET:
839              instrument_let_definition(node->pn_left->pn_kid1, f, 0);
840              break;
841            default:
842            instrument_expression(node->pn_left->pn_kid1, f);            instrument_expression(node->pn_left->pn_kid1, f);
843              break;
844          }          }
845        }        }
846        Stream_write_string(f, ";");        Stream_write_string(f, ";");
# Line 676  Line 859 
859        break;        break;
860      }      }
861      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
862      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
863      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
864      break;      break;
865    case TOK_THROW:    case TOK_THROW:
# Line 689  Line 872 
872    case TOK_TRY:    case TOK_TRY:
873      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
874      Stream_write_string(f, "try {\n");      Stream_write_string(f, "try {\n");
875      instrument_statement(node->pn_kid1, f, indent + 2);      instrument_statement(node->pn_kid1, f, indent + 2, false);
876      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
877      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
878      {      if (node->pn_kid2) {
879        for (JSParseNode * catch = node->pn_kid2; catch != NULL; catch = catch->pn_kid2) {        assert(node->pn_kid2->pn_type == TOK_RESERVED);
880          for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) {
881            assert(scope->pn_type == TOK_LEXICALSCOPE);
882            JSParseNode * catch = scope->pn_expr;
883          assert(catch->pn_type == TOK_CATCH);          assert(catch->pn_type == TOK_CATCH);
884          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
885          Stream_write_string(f, "catch (");          Stream_write_string(f, "catch (");
886          assert(catch->pn_kid1->pn_arity == PN_NAME);          assert(catch->pn_kid1->pn_arity == PN_NAME);
887          print_string_atom(catch->pn_kid1->pn_atom, f);          print_string_atom(catch->pn_kid1->pn_atom, f);
888          if (catch->pn_kid1->pn_expr) {          if (catch->pn_kid2) {
889            Stream_write_string(f, " if ");            Stream_write_string(f, " if ");
890            instrument_expression(catch->pn_kid1->pn_expr, f);            instrument_expression(catch->pn_kid2, f);
891          }          }
892          Stream_write_string(f, ") {\n");          Stream_write_string(f, ") {\n");
893          instrument_statement(catch->pn_kid3, f, indent + 2);          instrument_statement(catch->pn_kid3, f, indent + 2, false);
894          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
895          Stream_write_string(f, "}\n");          Stream_write_string(f, "}\n");
896        }        }
# Line 712  Line 898 
898      if (node->pn_kid3) {      if (node->pn_kid3) {
899        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
900        Stream_write_string(f, "finally {\n");        Stream_write_string(f, "finally {\n");
901        instrument_statement(node->pn_kid3, f, indent + 2);        instrument_statement(node->pn_kid3, f, indent + 2, false);
902        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
903        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
904      }      }
# Line 738  Line 924 
924      Stream_write_string(f, "with (");      Stream_write_string(f, "with (");
925      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
926      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
927      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
928      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
929      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
930      break;      break;
# Line 776  Line 962 
962      /*      /*
963      ... use output_statement instead of instrument_statement.      ... use output_statement instead of instrument_statement.
964      */      */
965      output_statement(node->pn_expr, f, indent);      output_statement(node->pn_expr, f, indent, false);
966        break;
967      case TOK_LEXICALSCOPE:
968        /* let statement */
969        assert(node->pn_arity == PN_NAME);
970        switch (node->pn_expr->pn_type) {
971        case TOK_LET:
972          /* let statement */
973          assert(node->pn_expr->pn_arity == PN_BINARY);
974          instrument_statement(node->pn_expr, f, indent, false);
975          break;
976        case TOK_LC:
977          /* block */
978          Stream_printf(f, "%*s", indent, "");
979          Stream_write_string(f, "{\n");
980          instrument_statement(node->pn_expr, f, indent + 2, false);
981          Stream_printf(f, "%*s", indent, "");
982          Stream_write_string(f, "}\n");
983          break;
984        case TOK_FOR:
985          instrument_statement(node->pn_expr, f, indent, false);
986          break;
987        default:
988          abort();
989          break;
990        }
991        break;
992      case TOK_LET:
993        switch (node->pn_arity) {
994        case PN_BINARY:
995          /* let statement */
996          Stream_printf(f, "%*s", indent, "");
997          Stream_write_string(f, "let (");
998          assert(node->pn_left->pn_type == TOK_LP);
999          assert(node->pn_left->pn_arity == PN_LIST);
1000          instrument_declarations(node->pn_left, f);
1001          Stream_write_string(f, ") {\n");
1002          instrument_statement(node->pn_right, f, indent + 2, false);
1003          Stream_printf(f, "%*s", indent, "");
1004          Stream_write_string(f, "}\n");
1005          break;
1006        case PN_LIST:
1007          /* let definition */
1008          instrument_let_definition(node, f, indent);
1009          Stream_write_string(f, ";\n");
1010          break;
1011        default:
1012          abort();
1013          break;
1014        }
1015      break;      break;
1016    default:    default:
1017      fatal("unsupported node type in file %s: %d", file_id, node->pn_type);      fatal("unsupported node type in file %s: %d", file_id, node->pn_type);
# Line 788  Line 1023 
1023  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
1024  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
1025  */  */
1026  static void instrument_statement(JSParseNode * node, Stream * f, int indent) {  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
1027    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC && node->pn_type != TOK_LEXICALSCOPE) {
1028      int line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
1029        if (line > num_lines) {
1030          fatal("%s: script contains more than 65,535 lines", file_id);
1031        }
1032    
1033      /* the root node has line number 0 */      /* the root node has line number 0 */
1034      if (line != 0) {      if (line != 0) {
1035        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
# Line 798  Line 1037 
1037        lines[line - 1] = 1;        lines[line - 1] = 1;
1038      }      }
1039    }    }
1040    output_statement(node, f, indent);    output_statement(node, f, indent, is_jscoverage_if);
1041  }  }
1042    
1043  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) {
1044    file_id = id;    const jschar * characters_end = characters + line_end;
1045      const jschar * cp = characters + line_start;
1046    /* scan the javascript */    const char * bp = prefix;
1047    size_t input_length = input->length;    for (;;) {
1048    jschar * base = js_InflateString(context, input->data, &input_length);      if (*bp == '\0') {
1049    if (base == NULL) {        return true;
1050      fatal("out of memory");      }
1051        else if (cp == characters_end) {
1052          return false;
1053        }
1054        else if (*cp != *bp) {
1055          return false;
1056        }
1057        bp++;
1058        cp++;
1059    }    }
1060    JSTokenStream * token_stream = js_NewTokenStream(context, base, input_length, NULL, 1, NULL);  }
1061    if (token_stream == NULL) {  
1062      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) {
1063      /* XXX - other Unicode space */
1064      const jschar * end = characters + line_end;
1065      for (const jschar * p = characters + line_start; p < end; p++) {
1066        jschar c = *p;
1067        if (c == 0x9 || c == 0xB || c == 0xC || c == 0x20 || c == 0xA0) {
1068          continue;
1069        }
1070        else {
1071          return false;
1072        }
1073    }    }
1074      return true;
1075    }
1076    
1077    static void error_reporter(JSContext * context, const char * message, JSErrorReport * report) {
1078      fprintf(stderr, "jscoverage: parse error: line %u: %s\n", report->lineno, message);
1079    }
1080    
1081    void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) {
1082      file_id = id;
1083    
1084    /* parse the javascript */    /* parse the javascript */
1085    JSParseNode * node = js_ParseTokenStream(context, global, token_stream);    JSParseContext parse_context;
1086      if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) {
1087        fatal("cannot create token stream from file: %s", file_id);
1088      }
1089      JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);
1090      JSParseNode * node = js_ParseScript(context, global, &parse_context);
1091    if (node == NULL) {    if (node == NULL) {
1092        js_ReportUncaughtException(context);
1093      fatal("parse error in file: %s", file_id);      fatal("parse error in file: %s", file_id);
1094    }    }
1095    int num_lines = node->pn_pos.end.lineno;    JS_SetErrorReporter(context, old_error_reporter);
1096      num_lines = node->pn_pos.end.lineno;
1097    lines = xmalloc(num_lines);    lines = xmalloc(num_lines);
1098    for (int i = 0; i < num_lines; i++) {    for (unsigned int i = 0; i < num_lines; i++) {
1099      lines[i] = 0;      lines[i] = 0;
1100    }    }
1101    
1102      /* search code for conditionals */
1103      exclusive_directives = xnew(bool, num_lines);
1104      for (unsigned int i = 0; i < num_lines; i++) {
1105        exclusive_directives[i] = false;
1106      }
1107    
1108      bool has_conditionals = false;
1109      struct IfDirective * if_directives = NULL;
1110      size_t line_number = 0;
1111      size_t i = 0;
1112      while (i < num_characters) {
1113        if (line_number == UINT16_MAX) {
1114          fatal("%s: script has more than 65,535 lines", file_id);
1115        }
1116        line_number++;
1117        size_t line_start = i;
1118        jschar c;
1119        bool done = false;
1120        while (! done && i < num_characters) {
1121          c = characters[i];
1122          switch (c) {
1123          case '\r':
1124          case '\n':
1125          case 0x2028:
1126          case 0x2029:
1127            done = true;
1128            break;
1129          default:
1130            i++;
1131          }
1132        }
1133        size_t line_end = i;
1134        if (i < num_characters) {
1135          i++;
1136          if (c == '\r' && i < num_characters && characters[i] == '\n') {
1137            i++;
1138          }
1139        }
1140    
1141        if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) {
1142          has_conditionals = true;
1143    
1144          if (characters_are_white_space(characters, line_start + 16, line_end)) {
1145            exclusive_directives[line_number - 1] = true;
1146          }
1147          else {
1148            struct IfDirective * if_directive = xnew(struct IfDirective, 1);
1149            if_directive->condition_start = characters + line_start + 16;
1150            if_directive->condition_end = characters + line_end;
1151            if_directive->start_line = line_number;
1152            if_directive->end_line = 0;
1153            if_directive->next = if_directives;
1154            if_directives = if_directive;
1155          }
1156        }
1157        else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) {
1158          for (struct IfDirective * p = if_directives; p != NULL; p = p->next) {
1159            if (p->end_line == 0) {
1160              p->end_line = line_number;
1161              break;
1162            }
1163          }
1164        }
1165      }
1166    
1167    /*    /*
1168    An instrumented JavaScript file has 3 sections:    An instrumented JavaScript file has 4 sections:
1169    1. initialization    1. initialization
1170    2. instrumented source code    2. instrumented source code
1171    3. original source code (TODO)    3. conditionals
1172      4. original source code
1173    */    */
1174    
1175    Stream * instrumented = Stream_new(0);    Stream * instrumented = Stream_new(0);
1176    instrument_statement(node, instrumented, 0);    instrument_statement(node, instrumented, 0, false);
1177      js_FinishParseContext(context, &parse_context);
1178    
1179    /* write line number info to the output */    /* write line number info to the output */
1180    Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");    Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");
# Line 850  Line 1190 
1190    Stream_write_string(output, "}\n");    Stream_write_string(output, "}\n");
1191    free(lines);    free(lines);
1192    lines = NULL;    lines = NULL;
1193      free(exclusive_directives);
1194      exclusive_directives = NULL;
1195    
1196      /* conditionals */
1197      if (has_conditionals) {
1198        Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);
1199      }
1200    
1201    /* copy the instrumented source code to the output */    /* copy the instrumented source code to the output */
1202    Stream_write(output, instrumented->data, instrumented->length);    Stream_write(output, instrumented->data, instrumented->length);
1203    Stream_write_char(output, '\n');  
1204      /* conditionals */
1205      for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) {
1206        Stream_write_string(output, "if (!(");
1207        print_javascript(if_directive->condition_start, if_directive->condition_end - if_directive->condition_start, output);
1208        Stream_write_string(output, ")) {\n");
1209        Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line);
1210        Stream_write_string(output, "}\n");
1211      }
1212    
1213      /* free */
1214      while (if_directives != NULL) {
1215        struct IfDirective * if_directive = if_directives;
1216        if_directives = if_directives->next;
1217        free(if_directive);
1218      }
1219    
1220    /* copy the original source to the output */    /* copy the original source to the output */
1221    size_t i = 0;    Stream_printf(output, "_$jscoverage['%s'].source = ", file_id);
1222    while (i < input_length) {    jscoverage_write_source(id, characters, num_characters, output);
1223      Stream_write_string(output, "// ");    Stream_printf(output, ";\n");
     size_t line_start = i;  
     while (i < input_length && base[i] != '\r' && base[i] != '\n') {  
       i++;  
     }  
1224    
1225      size_t line_end = i;    Stream_delete(instrumented);
1226      if (i < input_length) {  
1227        if (base[i] == '\r') {    file_id = NULL;
1228          line_end = i;  }
1229    
1230    void jscoverage_write_source(const char * id, const jschar * characters, size_t num_characters, Stream * output) {
1231      Stream_write_string(output, "[");
1232      if (jscoverage_highlight) {
1233        Stream * highlighted_stream = Stream_new(num_characters);
1234        jscoverage_highlight_js(context, id, characters, num_characters, highlighted_stream);
1235        size_t i = 0;
1236        while (i < highlighted_stream->length) {
1237          if (i > 0) {
1238            Stream_write_char(output, ',');
1239          }
1240    
1241          Stream_write_char(output, '"');
1242          bool done = false;
1243          while (! done) {
1244            char c = highlighted_stream->data[i];
1245            switch (c) {
1246            case 0x8:
1247              /* backspace */
1248              Stream_write_string(output, "\\b");
1249              break;
1250            case 0x9:
1251              /* horizontal tab */
1252              Stream_write_string(output, "\\t");
1253              break;
1254            case 0xa:
1255              /* line feed (new line) */
1256              done = true;
1257              break;
1258            /* IE doesn't support this */
1259            /*
1260            case 0xb:
1261              Stream_write_string(output, "\\v");
1262              break;
1263            */
1264            case 0xc:
1265              /* form feed */
1266              Stream_write_string(output, "\\f");
1267              break;
1268            case 0xd:
1269              /* carriage return */
1270              done = true;
1271              if (i + 1 < highlighted_stream->length && highlighted_stream->data[i + 1] == '\n') {
1272                i++;
1273              }
1274              break;
1275            case '"':
1276              Stream_write_string(output, "\\\"");
1277              break;
1278            case '\\':
1279              Stream_write_string(output, "\\\\");
1280              break;
1281            default:
1282              Stream_write_char(output, c);
1283              break;
1284            }
1285          i++;          i++;
1286          if (i < input_length && base[i] == '\n') {          if (i >= highlighted_stream->length) {
1287            i++;            done = true;
1288          }          }
1289        }        }
1290        else if (base[i] == '\n') {        Stream_write_char(output, '"');
1291          line_end = i;      }
1292        Stream_delete(highlighted_stream);
1293      }
1294      else {
1295        size_t i = 0;
1296        while (i < num_characters) {
1297          if (i > 0) {
1298            Stream_write_char(output, ',');
1299          }
1300    
1301          Stream_write_char(output, '"');
1302          bool done = false;
1303          while (! done) {
1304            jschar c = characters[i];
1305            switch (c) {
1306            case 0x8:
1307              /* backspace */
1308              Stream_write_string(output, "\\b");
1309              break;
1310            case 0x9:
1311              /* horizontal tab */
1312              Stream_write_string(output, "\\t");
1313              break;
1314            case 0xa:
1315              /* line feed (new line) */
1316              done = true;
1317              break;
1318            /* IE doesn't support this */
1319            /*
1320            case 0xb:
1321              Stream_write_string(output, "\\v");
1322              break;
1323            */
1324            case 0xc:
1325              /* form feed */
1326              Stream_write_string(output, "\\f");
1327              break;
1328            case 0xd:
1329              /* carriage return */
1330              done = true;
1331              if (i + 1 < num_characters && characters[i + 1] == '\n') {
1332                i++;
1333              }
1334              break;
1335            case '"':
1336              Stream_write_string(output, "\\\"");
1337              break;
1338            case '\\':
1339              Stream_write_string(output, "\\\\");
1340              break;
1341            case '&':
1342              Stream_write_string(output, "&amp;");
1343              break;
1344            case '<':
1345              Stream_write_string(output, "&lt;");
1346              break;
1347            case '>':
1348              Stream_write_string(output, "&gt;");
1349              break;
1350            case 0x2028:
1351            case 0x2029:
1352              done = true;
1353              break;
1354            default:
1355              if (32 <= c && c <= 126) {
1356                Stream_write_char(output, c);
1357              }
1358              else {
1359                Stream_printf(output, "&#%d;", c);
1360              }
1361              break;
1362            }
1363          i++;          i++;
1364            if (i >= num_characters) {
1365              done = true;
1366            }
1367        }        }
1368        else {        Stream_write_char(output, '"');
1369          abort();      }
1370      }
1371      Stream_write_string(output, "]");
1372    }
1373    
1374    void jscoverage_copy_resources(const char * destination_directory) {
1375      copy_resource("jscoverage.html", destination_directory);
1376      copy_resource("jscoverage.css", destination_directory);
1377      copy_resource("jscoverage.js", destination_directory);
1378      copy_resource("jscoverage-ie.css", destination_directory);
1379      copy_resource("jscoverage-throbber.gif", destination_directory);
1380      copy_resource("jscoverage-highlight.css", destination_directory);
1381    }
1382    
1383    /*
1384    coverage reports
1385    */
1386    
1387    struct FileCoverageList {
1388      FileCoverage * file_coverage;
1389      struct FileCoverageList * next;
1390    };
1391    
1392    struct Coverage {
1393      JSHashTable * coverage_table;
1394      struct FileCoverageList * coverage_list;
1395    };
1396    
1397    static int compare_strings(const void * p1, const void * p2) {
1398      return strcmp(p1, p2) == 0;
1399    }
1400    
1401    Coverage * Coverage_new(void) {
1402      Coverage * result = xmalloc(sizeof(Coverage));
1403      result->coverage_table = JS_NewHashTable(1024, JS_HashString, compare_strings, NULL, NULL, NULL);
1404      if (result->coverage_table == NULL) {
1405        fatal("cannot create hash table");
1406      }
1407      result->coverage_list = NULL;
1408      return result;
1409    }
1410    
1411    void Coverage_delete(Coverage * coverage) {
1412      JS_HashTableDestroy(coverage->coverage_table);
1413      struct FileCoverageList * p = coverage->coverage_list;
1414      while (p != NULL) {
1415        free(p->file_coverage->coverage_lines);
1416        if (p->file_coverage->source_lines != NULL) {
1417          for (uint32 i = 0; i < p->file_coverage->num_source_lines; i++) {
1418            free(p->file_coverage->source_lines[i]);
1419        }        }
1420          free(p->file_coverage->source_lines);
1421      }      }
1422        free(p->file_coverage->id);
1423        free(p->file_coverage);
1424        struct FileCoverageList * q = p;
1425        p = p->next;
1426        free(q);
1427      }
1428      free(coverage);
1429    }
1430    
1431    struct EnumeratorArg {
1432      CoverageForeachFunction f;
1433      void * p;
1434    };
1435    
1436    static intN enumerator(JSHashEntry * entry, intN i, void * arg) {
1437      struct EnumeratorArg * enumerator_arg = arg;
1438      enumerator_arg->f(entry->value, i, enumerator_arg->p);
1439      return 0;
1440    }
1441    
1442      char * line = js_DeflateString(context, base + line_start, line_end - line_start);  void Coverage_foreach_file(Coverage * coverage, CoverageForeachFunction f, void * p) {
1443      Stream_write_string(output, line);    struct EnumeratorArg enumerator_arg;
1444      Stream_write_char(output, '\n');    enumerator_arg.f = f;
1445      JS_free(context, line);    enumerator_arg.p = p;
1446      JS_HashTableEnumerateEntries(coverage->coverage_table, enumerator, &enumerator_arg);
1447    }
1448    
1449    int jscoverage_parse_json(Coverage * coverage, const uint8_t * json, size_t length) {
1450      int result = 0;
1451    
1452      jschar * base = js_InflateString(context, (char *) json, &length);
1453      if (base == NULL) {
1454        fatal("out of memory");
1455    }    }
1456    
1457    Stream_delete(instrumented);    jschar * parenthesized_json = xnew(jschar, addst(length, 2));
1458      parenthesized_json[0] = '(';
1459      memcpy(parenthesized_json + 1, base, mulst(length, sizeof(jschar)));
1460      parenthesized_json[length + 1] = ')';
1461    
1462    JS_free(context, base);    JS_free(context, base);
1463    
1464    file_id = NULL;    JSParseContext parse_context;
1465      if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) {
1466        free(parenthesized_json);
1467        return -1;
1468      }
1469      JSParseNode * root = js_ParseScript(context, global, &parse_context);
1470      free(parenthesized_json);
1471      if (root == NULL) {
1472        result = -1;
1473        goto done;
1474      }
1475    
1476      /* root node must be TOK_LC */
1477      if (root->pn_type != TOK_LC) {
1478        result = -1;
1479        goto done;
1480      }
1481      JSParseNode * semi = root->pn_u.list.head;
1482    
1483      /* the list must be TOK_SEMI and it must contain only one element */
1484      if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {
1485        result = -1;
1486        goto done;
1487      }
1488      JSParseNode * parenthesized = semi->pn_kid;
1489    
1490      /* this must be a parenthesized expression */
1491      if (parenthesized->pn_type != TOK_RP) {
1492        result = -1;
1493        goto done;
1494      }
1495      JSParseNode * object = parenthesized->pn_kid;
1496    
1497      /* this must be an object literal */
1498      if (object->pn_type != TOK_RC) {
1499        result = -1;
1500        goto done;
1501      }
1502    
1503      for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {
1504        /* every element of this list must be TOK_COLON */
1505        if (p->pn_type != TOK_COLON) {
1506          result = -1;
1507          goto done;
1508        }
1509    
1510        /* the key must be a string representing the file */
1511        JSParseNode * key = p->pn_left;
1512        if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {
1513          result = -1;
1514          goto done;
1515        }
1516        char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));
1517    
1518        /* the value must be an object literal OR an array */
1519        JSParseNode * value = p->pn_right;
1520        if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {
1521          result = -1;
1522          goto done;
1523        }
1524    
1525        JSParseNode * array = NULL;
1526        JSParseNode * source = NULL;
1527        if (value->pn_type == TOK_RB) {
1528          /* an array */
1529          array = value;
1530        }
1531        else if (value->pn_type == TOK_RC) {
1532          /* an object literal */
1533          if (value->pn_count != 2) {
1534            result = -1;
1535            goto done;
1536          }
1537          for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {
1538            if (element->pn_type != TOK_COLON) {
1539              result = -1;
1540              goto done;
1541            }
1542            JSParseNode * left = element->pn_left;
1543            if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {
1544              result = -1;
1545              goto done;
1546            }
1547            const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));
1548            if (strcmp(s, "coverage") == 0) {
1549              array = element->pn_right;
1550              if (array->pn_type != TOK_RB) {
1551                result = -1;
1552                goto done;
1553              }
1554            }
1555            else if (strcmp(s, "source") == 0) {
1556              source = element->pn_right;
1557              if (source->pn_type != TOK_RB) {
1558                result = -1;
1559                goto done;
1560              }
1561            }
1562            else {
1563              result = -1;
1564              goto done;
1565            }
1566          }
1567        }
1568        else {
1569          result = -1;
1570          goto done;
1571        }
1572    
1573        if (array == NULL) {
1574          result = -1;
1575          goto done;
1576        }
1577    
1578        /* look up the file in the coverage table */
1579        FileCoverage * file_coverage = JS_HashTableLookup(coverage->coverage_table, id_bytes);
1580        if (file_coverage == NULL) {
1581          /* not there: create a new one */
1582          char * id = xstrdup(id_bytes);
1583          file_coverage = xmalloc(sizeof(FileCoverage));
1584          file_coverage->id = id;
1585          file_coverage->num_coverage_lines = array->pn_count;
1586          file_coverage->coverage_lines = xnew(int, array->pn_count);
1587          file_coverage->source_lines = NULL;
1588    
1589          /* set coverage for all lines */
1590          uint32 i = 0;
1591          for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {
1592            if (element->pn_type == TOK_NUMBER) {
1593              file_coverage->coverage_lines[i] = (int) element->pn_dval;
1594            }
1595            else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1596              file_coverage->coverage_lines[i] = -1;
1597            }
1598            else {
1599              result = -1;
1600              goto done;
1601            }
1602          }
1603          assert(i == array->pn_count);
1604    
1605          /* add to the hash table */
1606          JS_HashTableAdd(coverage->coverage_table, id, file_coverage);
1607          struct FileCoverageList * coverage_list = xmalloc(sizeof(struct FileCoverageList));
1608          coverage_list->file_coverage = file_coverage;
1609          coverage_list->next = coverage->coverage_list;
1610          coverage->coverage_list = coverage_list;
1611        }
1612        else {
1613          /* sanity check */
1614          assert(strcmp(file_coverage->id, id_bytes) == 0);
1615          if (file_coverage->num_coverage_lines != array->pn_count) {
1616            result = -2;
1617            goto done;
1618          }
1619    
1620          /* merge the coverage */
1621          uint32 i = 0;
1622          for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {
1623            if (element->pn_type == TOK_NUMBER) {
1624              if (file_coverage->coverage_lines[i] == -1) {
1625                result = -2;
1626                goto done;
1627              }
1628              file_coverage->coverage_lines[i] += (int) element->pn_dval;
1629            }
1630            else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1631              if (file_coverage->coverage_lines[i] != -1) {
1632                result = -2;
1633                goto done;
1634              }
1635            }
1636            else {
1637              result = -1;
1638              goto done;
1639            }
1640          }
1641          assert(i == array->pn_count);
1642        }
1643    
1644        /* if this JSON file has source, use it */
1645        if (file_coverage->source_lines == NULL && source != NULL) {
1646          file_coverage->num_source_lines = source->pn_count;
1647          file_coverage->source_lines = xnew(char *, source->pn_count);
1648          uint32 i = 0;
1649          for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {
1650            if (element->pn_type != TOK_STRING) {
1651              result = -1;
1652              goto done;
1653            }
1654            file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));
1655          }
1656          assert(i == source->pn_count);
1657        }
1658      }
1659    
1660    done:
1661      js_FinishParseContext(context, &parse_context);
1662      return result;
1663  }  }

Legend:
Removed from v.95  
changed lines
  Added in v.340

  ViewVC Help
Powered by ViewVC 1.1.24