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

Diff of /trunk/instrument-js.cpp

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

revision 92 by siliconforks, Wed May 7 04:21:22 2008 UTC revision 341 by siliconforks, Fri Oct 24 16:14:43 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        switch (p->pn_type) {
331        case TOK_NAME:
332          assert(p->pn_arity == PN_NAME);
333          if (p != list->pn_head) {
334            Stream_write_string(f, ", ");
335          }
336          print_string_atom(p->pn_atom, f);
337          if (p->pn_expr != NULL) {
338            Stream_write_string(f, " = ");
339            instrument_expression(p->pn_expr, f);
340          }
341          break;
342        case TOK_ASSIGN:
343        case TOK_RB:
344        case TOK_RC:
345          /* destructuring */
346          instrument_expression(p, f);
347          break;
348        default:
349          abort();
350          break;
351        }
352      }
353    }
354    
355  /*  /*
356  See <Expressions> in jsparse.h.  See <Expressions> in jsparse.h.
357  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 368 
368  static void instrument_expression(JSParseNode * node, Stream * f) {  static void instrument_expression(JSParseNode * node, Stream * f) {
369    switch (node->pn_type) {    switch (node->pn_type) {
370    case TOK_FUNCTION:    case TOK_FUNCTION:
371      instrument_function(node, f, 0);      instrument_function(node, f, 0, FUNCTION_NORMAL);
372      break;      break;
373    case TOK_COMMA:    case TOK_COMMA:
374      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 410 
410      instrument_expression(node->pn_kid3, f);      instrument_expression(node->pn_kid3, f);
411      break;      break;
412    case TOK_OR:    case TOK_OR:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " || ");  
     instrument_expression(node->pn_right, f);  
     break;  
413    case TOK_AND:    case TOK_AND:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " && ");  
     instrument_expression(node->pn_right, f);  
     break;  
414    case TOK_BITOR:    case TOK_BITOR:
415    case TOK_BITXOR:    case TOK_BITXOR:
416    case TOK_BITAND:    case TOK_BITAND:
# Line 388  Line 523 
523      assert(ATOM_IS_STRING(node->pn_atom));      assert(ATOM_IS_STRING(node->pn_atom));
524      {      {
525        JSString * s = ATOM_TO_STRING(node->pn_atom);        JSString * s = ATOM_TO_STRING(node->pn_atom);
526        /* XXX - semantics changed in 1.7 */        bool is_keyword = (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF);
527        if (! ATOM_KEYWORD(node->pn_atom) && js_IsIdentifier(s)) {        if (! is_keyword && js_IsIdentifier(s)) {
528          Stream_write_char(f, '.');          Stream_write_char(f, '.');
529          print_string_atom(node->pn_atom, f);          print_string_atom(node->pn_atom, f);
530        }        }
# Line 432  Line 567 
567        if (p != node->pn_head) {        if (p != node->pn_head) {
568          Stream_write_string(f, ", ");          Stream_write_string(f, ", ");
569        }        }
570        instrument_expression(p->pn_left, f);  
571        Stream_write_string(f, ": ");        /* check whether this is a getter or setter */
572        instrument_expression(p->pn_right, f);        switch (p->pn_op) {
573          case JSOP_GETTER:
574          case JSOP_SETTER:
575            if (p->pn_op == JSOP_GETTER) {
576              Stream_write_string(f, "get ");
577            }
578            else {
579              Stream_write_string(f, "set ");
580            }
581            instrument_expression(p->pn_left, f);
582            if (p->pn_right->pn_type != TOK_FUNCTION) {
583              fatal("parse error: expected function");
584            }
585            instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);
586            break;
587          default:
588            instrument_expression(p->pn_left, f);
589            Stream_write_string(f, ": ");
590            instrument_expression(p->pn_right, f);
591            break;
592          }
593      }      }
594      Stream_write_char(f, '}');      Stream_write_char(f, '}');
595      break;      break;
# Line 449  Line 604 
604    case TOK_STRING:    case TOK_STRING:
605      print_quoted_string_atom(node->pn_atom, f);      print_quoted_string_atom(node->pn_atom, f);
606      break;      break;
607    case TOK_OBJECT:    case TOK_REGEXP:
608      switch (node->pn_op) {      assert(node->pn_op == JSOP_REGEXP);
609      case JSOP_OBJECT:      {
610        /* I assume this is JSOP_REGEXP */        JSObject * object = node->pn_pob->object;
611        abort();        jsval result;
612        break;        js_regexp_toString(context, object, &result);
613      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;  
614      }      }
615      break;      break;
616    case TOK_NUMBER:    case TOK_NUMBER:
# Line 512  Line 656 
656      Stream_write_string(f, " in ");      Stream_write_string(f, " in ");
657      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
658      break;      break;
659      case TOK_LEXICALSCOPE:
660        assert(node->pn_arity == PN_NAME);
661        assert(node->pn_expr->pn_type == TOK_LET);
662        assert(node->pn_expr->pn_arity == PN_BINARY);
663        Stream_write_string(f, "let(");
664        assert(node->pn_expr->pn_left->pn_type == TOK_LP);
665        assert(node->pn_expr->pn_left->pn_arity == PN_LIST);
666        instrument_declarations(node->pn_expr->pn_left, f);
667        Stream_write_string(f, ") ");
668        instrument_expression(node->pn_expr->pn_right, f);
669        break;
670    default:    default:
671      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);
672    }    }
# Line 521  Line 676 
676    assert(node->pn_arity == PN_LIST);    assert(node->pn_arity == PN_LIST);
677    Stream_printf(f, "%*s", indent, "");    Stream_printf(f, "%*s", indent, "");
678    Stream_write_string(f, "var ");    Stream_write_string(f, "var ");
679    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);  
     }  
   }  
680  }  }
681    
682  static void output_statement(JSParseNode * node, Stream * f, int indent) {  static void instrument_let_definition(JSParseNode * node, Stream * f, int indent) {
683      assert(node->pn_arity == PN_LIST);
684      Stream_printf(f, "%*s", indent, "");
685      Stream_write_string(f, "let ");
686      instrument_declarations(node, f);
687    }
688    
689    static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
690    switch (node->pn_type) {    switch (node->pn_type) {
691    case TOK_FUNCTION:    case TOK_FUNCTION:
692      instrument_function(node, f, indent);      instrument_function(node, f, indent, FUNCTION_NORMAL);
693      break;      break;
694    case TOK_LC:    case TOK_LC:
695      assert(node->pn_arity == PN_LIST);      assert(node->pn_arity == PN_LIST);
# Line 546  Line 697 
697      Stream_write_string(f, "{\n");      Stream_write_string(f, "{\n");
698  */  */
699      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) {
700        instrument_statement(p, f, indent);        instrument_statement(p, f, indent, false);
701      }      }
702  /*  /*
703      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 554  Line 705 
705  */  */
706      break;      break;
707    case TOK_IF:    case TOK_IF:
708      {
709      assert(node->pn_arity == PN_TERNARY);      assert(node->pn_arity == PN_TERNARY);
710    
711        uint16_t line = node->pn_pos.begin.lineno;
712        if (! is_jscoverage_if) {
713          if (line > num_lines) {
714            fatal("%s: script contains more than 65,535 lines", file_id);
715          }
716          if (line >= 2 && exclusive_directives[line - 2]) {
717            is_jscoverage_if = true;
718          }
719        }
720    
721      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
722      Stream_write_string(f, "if (");      Stream_write_string(f, "if (");
723      instrument_expression(node->pn_kid1, f);      instrument_expression(node->pn_kid1, f);
724      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
725      instrument_statement(node->pn_kid2, f, indent + 2);      if (is_jscoverage_if && node->pn_kid3) {
726          uint16_t else_start = node->pn_kid3->pn_pos.begin.lineno;
727          uint16_t else_end = node->pn_kid3->pn_pos.end.lineno + 1;
728          Stream_printf(f, "%*s", indent + 2, "");
729          Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, else_start, else_end);
730        }
731        instrument_statement(node->pn_kid2, f, indent + 2, false);
732      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
733      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
734      if (node->pn_kid3) {  
735        if (node->pn_kid3 || is_jscoverage_if) {
736        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
737        Stream_write_string(f, "else {\n");        Stream_write_string(f, "else {\n");
738        instrument_statement(node->pn_kid3, f, indent + 2);  
739          if (is_jscoverage_if) {
740            uint16_t if_start = node->pn_kid2->pn_pos.begin.lineno + 1;
741            uint16_t if_end = node->pn_kid2->pn_pos.end.lineno + 1;
742            Stream_printf(f, "%*s", indent + 2, "");
743            Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_start, if_end);
744          }
745    
746          if (node->pn_kid3) {
747            instrument_statement(node->pn_kid3, f, indent + 2, is_jscoverage_if);
748          }
749    
750        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
751        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
752      }      }
753    
754      break;      break;
755      }
756    case TOK_SWITCH:    case TOK_SWITCH:
757      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
758      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 591  Line 774 
774          abort();          abort();
775          break;          break;
776        }        }
777        instrument_statement(p->pn_right, f, indent + 2);        instrument_statement(p->pn_right, f, indent + 2, false);
778      }      }
779      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
780      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
# Line 606  Line 789 
789      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
790      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
791      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
792      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
793      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
794      break;      break;
795    case TOK_DO:    case TOK_DO:
796      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
797      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
798      Stream_write_string(f, "do {\n");      Stream_write_string(f, "do {\n");
799      instrument_statement(node->pn_left, f, indent + 2);      instrument_statement(node->pn_left, f, indent + 2, false);
800      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
801      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
802      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
# Line 623  Line 806 
806    case TOK_FOR:    case TOK_FOR:
807      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
808      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
809      Stream_write_string(f, "for (");      Stream_write_string(f, "for ");
810        if (node->pn_iflags & JSITER_FOREACH) {
811          Stream_write_string(f, "each ");
812        }
813        Stream_write_char(f, '(');
814      switch (node->pn_left->pn_type) {      switch (node->pn_left->pn_type) {
815      case TOK_IN:      case TOK_IN:
816        /* for/in */        /* for/in */
# Line 632  Line 819 
819        case TOK_VAR:        case TOK_VAR:
820          instrument_var_statement(node->pn_left->pn_left, f, 0);          instrument_var_statement(node->pn_left->pn_left, f, 0);
821          break;          break;
822          case TOK_LET:
823            instrument_let_definition(node->pn_left->pn_left, f, 0);
824            break;
825        case TOK_NAME:        case TOK_NAME:
826          instrument_expression(node->pn_left->pn_left, f);          instrument_expression(node->pn_left->pn_left, f);
827          break;          break;
# Line 653  Line 843 
843        /* for (;;) */        /* for (;;) */
844        assert(node->pn_left->pn_arity == PN_TERNARY);        assert(node->pn_left->pn_arity == PN_TERNARY);
845        if (node->pn_left->pn_kid1) {        if (node->pn_left->pn_kid1) {
846          if (node->pn_left->pn_kid1->pn_type == TOK_VAR) {          switch (node->pn_left->pn_kid1->pn_type) {
847            case TOK_VAR:
848            instrument_var_statement(node->pn_left->pn_kid1, f, 0);            instrument_var_statement(node->pn_left->pn_kid1, f, 0);
849          }            break;
850          else {          case TOK_LET:
851              instrument_let_definition(node->pn_left->pn_kid1, f, 0);
852              break;
853            default:
854            instrument_expression(node->pn_left->pn_kid1, f);            instrument_expression(node->pn_left->pn_kid1, f);
855              break;
856          }          }
857        }        }
858        Stream_write_string(f, ";");        Stream_write_string(f, ";");
# Line 676  Line 871 
871        break;        break;
872      }      }
873      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
874      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
875      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
876      break;      break;
877    case TOK_THROW:    case TOK_THROW:
# Line 689  Line 884 
884    case TOK_TRY:    case TOK_TRY:
885      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
886      Stream_write_string(f, "try {\n");      Stream_write_string(f, "try {\n");
887      instrument_statement(node->pn_kid1, f, indent + 2);      instrument_statement(node->pn_kid1, f, indent + 2, false);
888      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
889      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
890      {      if (node->pn_kid2) {
891        for (JSParseNode * catch = node->pn_kid2; catch != NULL; catch = catch->pn_kid2) {        assert(node->pn_kid2->pn_type == TOK_RESERVED);
892          for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) {
893            assert(scope->pn_type == TOK_LEXICALSCOPE);
894            JSParseNode * catch = scope->pn_expr;
895          assert(catch->pn_type == TOK_CATCH);          assert(catch->pn_type == TOK_CATCH);
896          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
897          Stream_write_string(f, "catch (");          Stream_write_string(f, "catch (");
898            /* this may not be a name - destructuring assignment */
899            /*
900          assert(catch->pn_kid1->pn_arity == PN_NAME);          assert(catch->pn_kid1->pn_arity == PN_NAME);
901          print_string_atom(catch->pn_kid1->pn_atom, f);          print_string_atom(catch->pn_kid1->pn_atom, f);
902          if (catch->pn_kid1->pn_expr) {          */
903            instrument_expression(catch->pn_kid1, f);
904            if (catch->pn_kid2) {
905            Stream_write_string(f, " if ");            Stream_write_string(f, " if ");
906            instrument_expression(catch->pn_kid1->pn_expr, f);            instrument_expression(catch->pn_kid2, f);
907          }          }
908          Stream_write_string(f, ") {\n");          Stream_write_string(f, ") {\n");
909          instrument_statement(catch->pn_kid3, f, indent + 2);          instrument_statement(catch->pn_kid3, f, indent + 2, false);
910          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
911          Stream_write_string(f, "}\n");          Stream_write_string(f, "}\n");
912        }        }
# Line 712  Line 914 
914      if (node->pn_kid3) {      if (node->pn_kid3) {
915        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
916        Stream_write_string(f, "finally {\n");        Stream_write_string(f, "finally {\n");
917        instrument_statement(node->pn_kid3, f, indent + 2);        instrument_statement(node->pn_kid3, f, indent + 2, false);
918        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
919        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
920      }      }
# Line 738  Line 940 
940      Stream_write_string(f, "with (");      Stream_write_string(f, "with (");
941      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
942      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
943      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
944      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
945      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
946      break;      break;
# Line 776  Line 978 
978      /*      /*
979      ... use output_statement instead of instrument_statement.      ... use output_statement instead of instrument_statement.
980      */      */
981      output_statement(node->pn_expr, f, indent);      output_statement(node->pn_expr, f, indent, false);
982        break;
983      case TOK_LEXICALSCOPE:
984        /* let statement */
985        assert(node->pn_arity == PN_NAME);
986        switch (node->pn_expr->pn_type) {
987        case TOK_LET:
988          /* let statement */
989          assert(node->pn_expr->pn_arity == PN_BINARY);
990          instrument_statement(node->pn_expr, f, indent, false);
991          break;
992        case TOK_LC:
993          /* block */
994          Stream_printf(f, "%*s", indent, "");
995          Stream_write_string(f, "{\n");
996          instrument_statement(node->pn_expr, f, indent + 2, false);
997          Stream_printf(f, "%*s", indent, "");
998          Stream_write_string(f, "}\n");
999          break;
1000        case TOK_FOR:
1001          instrument_statement(node->pn_expr, f, indent, false);
1002          break;
1003        default:
1004          abort();
1005          break;
1006        }
1007        break;
1008      case TOK_LET:
1009        switch (node->pn_arity) {
1010        case PN_BINARY:
1011          /* let statement */
1012          Stream_printf(f, "%*s", indent, "");
1013          Stream_write_string(f, "let (");
1014          assert(node->pn_left->pn_type == TOK_LP);
1015          assert(node->pn_left->pn_arity == PN_LIST);
1016          instrument_declarations(node->pn_left, f);
1017          Stream_write_string(f, ") {\n");
1018          instrument_statement(node->pn_right, f, indent + 2, false);
1019          Stream_printf(f, "%*s", indent, "");
1020          Stream_write_string(f, "}\n");
1021          break;
1022        case PN_LIST:
1023          /* let definition */
1024          instrument_let_definition(node, f, indent);
1025          Stream_write_string(f, ";\n");
1026          break;
1027        default:
1028          abort();
1029          break;
1030        }
1031      break;      break;
1032    default:    default:
1033      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 1039 
1039  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
1040  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
1041  */  */
1042  static void instrument_statement(JSParseNode * node, Stream * f, int indent) {  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
1043    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC && node->pn_type != TOK_LEXICALSCOPE) {
1044      int line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
1045        if (line > num_lines) {
1046          fatal("%s: script contains more than 65,535 lines", file_id);
1047        }
1048    
1049      /* the root node has line number 0 */      /* the root node has line number 0 */
1050      if (line != 0) {      if (line != 0) {
1051        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
# Line 798  Line 1053 
1053        lines[line - 1] = 1;        lines[line - 1] = 1;
1054      }      }
1055    }    }
1056    output_statement(node, f, indent);    output_statement(node, f, indent, is_jscoverage_if);
1057  }  }
1058    
1059  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) {
1060    file_id = id;    const jschar * characters_end = characters + line_end;
1061      const jschar * cp = characters + line_start;
1062    /* scan the javascript */    const char * bp = prefix;
1063    size_t input_length = input->length;    for (;;) {
1064    jschar * base = js_InflateString(context, input->data, &input_length);      if (*bp == '\0') {
1065    if (base == NULL) {        return true;
1066      fatal("out of memory");      }
1067        else if (cp == characters_end) {
1068          return false;
1069        }
1070        else if (*cp != *bp) {
1071          return false;
1072        }
1073        bp++;
1074        cp++;
1075    }    }
1076    JSTokenStream * token_stream = js_NewTokenStream(context, base, input_length, NULL, 1, NULL);  }
1077    if (token_stream == NULL) {  
1078      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) {
1079      /* XXX - other Unicode space */
1080      const jschar * end = characters + line_end;
1081      for (const jschar * p = characters + line_start; p < end; p++) {
1082        jschar c = *p;
1083        if (c == 0x9 || c == 0xB || c == 0xC || c == 0x20 || c == 0xA0) {
1084          continue;
1085        }
1086        else {
1087          return false;
1088        }
1089    }    }
1090      return true;
1091    }
1092    
1093    static void error_reporter(JSContext * context, const char * message, JSErrorReport * report) {
1094      fprintf(stderr, "jscoverage: parse error: line %u: %s\n", report->lineno, message);
1095    }
1096    
1097    void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) {
1098      file_id = id;
1099    
1100    /* parse the javascript */    /* parse the javascript */
1101    JSParseNode * node = js_ParseTokenStream(context, global, token_stream);    JSParseContext parse_context;
1102      if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) {
1103        fatal("cannot create token stream from file: %s", file_id);
1104      }
1105      JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);
1106      JSParseNode * node = js_ParseScript(context, global, &parse_context);
1107    if (node == NULL) {    if (node == NULL) {
1108        js_ReportUncaughtException(context);
1109      fatal("parse error in file: %s", file_id);      fatal("parse error in file: %s", file_id);
1110    }    }
1111    int num_lines = node->pn_pos.end.lineno;    JS_SetErrorReporter(context, old_error_reporter);
1112      num_lines = node->pn_pos.end.lineno;
1113    lines = xmalloc(num_lines);    lines = xmalloc(num_lines);
1114    for (int i = 0; i < num_lines; i++) {    for (unsigned int i = 0; i < num_lines; i++) {
1115      lines[i] = 0;      lines[i] = 0;
1116    }    }
1117    
1118      /* search code for conditionals */
1119      exclusive_directives = xnew(bool, num_lines);
1120      for (unsigned int i = 0; i < num_lines; i++) {
1121        exclusive_directives[i] = false;
1122      }
1123    
1124      bool has_conditionals = false;
1125      struct IfDirective * if_directives = NULL;
1126      size_t line_number = 0;
1127      size_t i = 0;
1128      while (i < num_characters) {
1129        if (line_number == UINT16_MAX) {
1130          fatal("%s: script has more than 65,535 lines", file_id);
1131        }
1132        line_number++;
1133        size_t line_start = i;
1134        jschar c;
1135        bool done = false;
1136        while (! done && i < num_characters) {
1137          c = characters[i];
1138          switch (c) {
1139          case '\r':
1140          case '\n':
1141          case 0x2028:
1142          case 0x2029:
1143            done = true;
1144            break;
1145          default:
1146            i++;
1147          }
1148        }
1149        size_t line_end = i;
1150        if (i < num_characters) {
1151          i++;
1152          if (c == '\r' && i < num_characters && characters[i] == '\n') {
1153            i++;
1154          }
1155        }
1156    
1157        if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) {
1158          has_conditionals = true;
1159    
1160          if (characters_are_white_space(characters, line_start + 16, line_end)) {
1161            exclusive_directives[line_number - 1] = true;
1162          }
1163          else {
1164            struct IfDirective * if_directive = xnew(struct IfDirective, 1);
1165            if_directive->condition_start = characters + line_start + 16;
1166            if_directive->condition_end = characters + line_end;
1167            if_directive->start_line = line_number;
1168            if_directive->end_line = 0;
1169            if_directive->next = if_directives;
1170            if_directives = if_directive;
1171          }
1172        }
1173        else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) {
1174          for (struct IfDirective * p = if_directives; p != NULL; p = p->next) {
1175            if (p->end_line == 0) {
1176              p->end_line = line_number;
1177              break;
1178            }
1179          }
1180        }
1181      }
1182    
1183    /*    /*
1184    An instrumented JavaScript file has 3 sections:    An instrumented JavaScript file has 4 sections:
1185    1. initialization    1. initialization
1186    2. instrumented source code    2. instrumented source code
1187    3. original source code (TODO)    3. conditionals
1188      4. original source code
1189    */    */
1190    
1191    Stream * instrumented = Stream_new(0);    Stream * instrumented = Stream_new(0);
1192    instrument_statement(node, instrumented, 0);    instrument_statement(node, instrumented, 0, false);
1193      js_FinishParseContext(context, &parse_context);
1194    
1195    /* write line number info to the output */    /* write line number info to the output */
1196    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 1206 
1206    Stream_write_string(output, "}\n");    Stream_write_string(output, "}\n");
1207    free(lines);    free(lines);
1208    lines = NULL;    lines = NULL;
1209      free(exclusive_directives);
1210      exclusive_directives = NULL;
1211    
1212      /* conditionals */
1213      if (has_conditionals) {
1214        Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);
1215      }
1216    
1217    /* copy the instrumented source code to the output */    /* copy the instrumented source code to the output */
1218    Stream_write(output, instrumented->data, instrumented->length);    Stream_write(output, instrumented->data, instrumented->length);
1219    
1220      /* conditionals */
1221      for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) {
1222        Stream_write_string(output, "if (!(");
1223        print_javascript(if_directive->condition_start, if_directive->condition_end - if_directive->condition_start, output);
1224        Stream_write_string(output, ")) {\n");
1225        Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line);
1226        Stream_write_string(output, "}\n");
1227      }
1228    
1229      /* free */
1230      while (if_directives != NULL) {
1231        struct IfDirective * if_directive = if_directives;
1232        if_directives = if_directives->next;
1233        free(if_directive);
1234      }
1235    
1236      /* copy the original source to the output */
1237      Stream_printf(output, "_$jscoverage['%s'].source = ", file_id);
1238      jscoverage_write_source(id, characters, num_characters, output);
1239      Stream_printf(output, ";\n");
1240    
1241    Stream_delete(instrumented);    Stream_delete(instrumented);
1242    
1243      file_id = NULL;
1244    }
1245    
1246    void jscoverage_write_source(const char * id, const jschar * characters, size_t num_characters, Stream * output) {
1247      Stream_write_string(output, "[");
1248      if (jscoverage_highlight) {
1249        Stream * highlighted_stream = Stream_new(num_characters);
1250        jscoverage_highlight_js(context, id, characters, num_characters, highlighted_stream);
1251        size_t i = 0;
1252        while (i < highlighted_stream->length) {
1253          if (i > 0) {
1254            Stream_write_char(output, ',');
1255          }
1256    
1257          Stream_write_char(output, '"');
1258          bool done = false;
1259          while (! done) {
1260            char c = highlighted_stream->data[i];
1261            switch (c) {
1262            case 0x8:
1263              /* backspace */
1264              Stream_write_string(output, "\\b");
1265              break;
1266            case 0x9:
1267              /* horizontal tab */
1268              Stream_write_string(output, "\\t");
1269              break;
1270            case 0xa:
1271              /* line feed (new line) */
1272              done = true;
1273              break;
1274            /* IE doesn't support this */
1275            /*
1276            case 0xb:
1277              Stream_write_string(output, "\\v");
1278              break;
1279            */
1280            case 0xc:
1281              /* form feed */
1282              Stream_write_string(output, "\\f");
1283              break;
1284            case 0xd:
1285              /* carriage return */
1286              done = true;
1287              if (i + 1 < highlighted_stream->length && highlighted_stream->data[i + 1] == '\n') {
1288                i++;
1289              }
1290              break;
1291            case '"':
1292              Stream_write_string(output, "\\\"");
1293              break;
1294            case '\\':
1295              Stream_write_string(output, "\\\\");
1296              break;
1297            default:
1298              Stream_write_char(output, c);
1299              break;
1300            }
1301            i++;
1302            if (i >= highlighted_stream->length) {
1303              done = true;
1304            }
1305          }
1306          Stream_write_char(output, '"');
1307        }
1308        Stream_delete(highlighted_stream);
1309      }
1310      else {
1311        size_t i = 0;
1312        while (i < num_characters) {
1313          if (i > 0) {
1314            Stream_write_char(output, ',');
1315          }
1316    
1317          Stream_write_char(output, '"');
1318          bool done = false;
1319          while (! done) {
1320            jschar c = characters[i];
1321            switch (c) {
1322            case 0x8:
1323              /* backspace */
1324              Stream_write_string(output, "\\b");
1325              break;
1326            case 0x9:
1327              /* horizontal tab */
1328              Stream_write_string(output, "\\t");
1329              break;
1330            case 0xa:
1331              /* line feed (new line) */
1332              done = true;
1333              break;
1334            /* IE doesn't support this */
1335            /*
1336            case 0xb:
1337              Stream_write_string(output, "\\v");
1338              break;
1339            */
1340            case 0xc:
1341              /* form feed */
1342              Stream_write_string(output, "\\f");
1343              break;
1344            case 0xd:
1345              /* carriage return */
1346              done = true;
1347              if (i + 1 < num_characters && characters[i + 1] == '\n') {
1348                i++;
1349              }
1350              break;
1351            case '"':
1352              Stream_write_string(output, "\\\"");
1353              break;
1354            case '\\':
1355              Stream_write_string(output, "\\\\");
1356              break;
1357            case '&':
1358              Stream_write_string(output, "&amp;");
1359              break;
1360            case '<':
1361              Stream_write_string(output, "&lt;");
1362              break;
1363            case '>':
1364              Stream_write_string(output, "&gt;");
1365              break;
1366            case 0x2028:
1367            case 0x2029:
1368              done = true;
1369              break;
1370            default:
1371              if (32 <= c && c <= 126) {
1372                Stream_write_char(output, c);
1373              }
1374              else {
1375                Stream_printf(output, "&#%d;", c);
1376              }
1377              break;
1378            }
1379            i++;
1380            if (i >= num_characters) {
1381              done = true;
1382            }
1383          }
1384          Stream_write_char(output, '"');
1385        }
1386      }
1387      Stream_write_string(output, "]");
1388    }
1389    
1390    void jscoverage_copy_resources(const char * destination_directory) {
1391      copy_resource("jscoverage.html", destination_directory);
1392      copy_resource("jscoverage.css", destination_directory);
1393      copy_resource("jscoverage.js", destination_directory);
1394      copy_resource("jscoverage-ie.css", destination_directory);
1395      copy_resource("jscoverage-throbber.gif", destination_directory);
1396      copy_resource("jscoverage-highlight.css", destination_directory);
1397    }
1398    
1399    /*
1400    coverage reports
1401    */
1402    
1403    struct FileCoverageList {
1404      FileCoverage * file_coverage;
1405      struct FileCoverageList * next;
1406    };
1407    
1408    struct Coverage {
1409      JSHashTable * coverage_table;
1410      struct FileCoverageList * coverage_list;
1411    };
1412    
1413    static int compare_strings(const void * p1, const void * p2) {
1414      return strcmp(p1, p2) == 0;
1415    }
1416    
1417    Coverage * Coverage_new(void) {
1418      Coverage * result = xmalloc(sizeof(Coverage));
1419      result->coverage_table = JS_NewHashTable(1024, JS_HashString, compare_strings, NULL, NULL, NULL);
1420      if (result->coverage_table == NULL) {
1421        fatal("cannot create hash table");
1422      }
1423      result->coverage_list = NULL;
1424      return result;
1425    }
1426    
1427    void Coverage_delete(Coverage * coverage) {
1428      JS_HashTableDestroy(coverage->coverage_table);
1429      struct FileCoverageList * p = coverage->coverage_list;
1430      while (p != NULL) {
1431        free(p->file_coverage->coverage_lines);
1432        if (p->file_coverage->source_lines != NULL) {
1433          for (uint32 i = 0; i < p->file_coverage->num_source_lines; i++) {
1434            free(p->file_coverage->source_lines[i]);
1435          }
1436          free(p->file_coverage->source_lines);
1437        }
1438        free(p->file_coverage->id);
1439        free(p->file_coverage);
1440        struct FileCoverageList * q = p;
1441        p = p->next;
1442        free(q);
1443      }
1444      free(coverage);
1445    }
1446    
1447    struct EnumeratorArg {
1448      CoverageForeachFunction f;
1449      void * p;
1450    };
1451    
1452    static intN enumerator(JSHashEntry * entry, intN i, void * arg) {
1453      struct EnumeratorArg * enumerator_arg = arg;
1454      enumerator_arg->f(entry->value, i, enumerator_arg->p);
1455      return 0;
1456    }
1457    
1458    void Coverage_foreach_file(Coverage * coverage, CoverageForeachFunction f, void * p) {
1459      struct EnumeratorArg enumerator_arg;
1460      enumerator_arg.f = f;
1461      enumerator_arg.p = p;
1462      JS_HashTableEnumerateEntries(coverage->coverage_table, enumerator, &enumerator_arg);
1463    }
1464    
1465    int jscoverage_parse_json(Coverage * coverage, const uint8_t * json, size_t length) {
1466      int result = 0;
1467    
1468      jschar * base = js_InflateString(context, (char *) json, &length);
1469      if (base == NULL) {
1470        fatal("out of memory");
1471      }
1472    
1473      jschar * parenthesized_json = xnew(jschar, addst(length, 2));
1474      parenthesized_json[0] = '(';
1475      memcpy(parenthesized_json + 1, base, mulst(length, sizeof(jschar)));
1476      parenthesized_json[length + 1] = ')';
1477    
1478    JS_free(context, base);    JS_free(context, base);
1479    
1480    file_id = NULL;    JSParseContext parse_context;
1481      if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) {
1482        free(parenthesized_json);
1483        return -1;
1484      }
1485      JSParseNode * root = js_ParseScript(context, global, &parse_context);
1486      free(parenthesized_json);
1487      if (root == NULL) {
1488        result = -1;
1489        goto done;
1490      }
1491    
1492      /* root node must be TOK_LC */
1493      if (root->pn_type != TOK_LC) {
1494        result = -1;
1495        goto done;
1496      }
1497      JSParseNode * semi = root->pn_u.list.head;
1498    
1499      /* the list must be TOK_SEMI and it must contain only one element */
1500      if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {
1501        result = -1;
1502        goto done;
1503      }
1504      JSParseNode * parenthesized = semi->pn_kid;
1505    
1506      /* this must be a parenthesized expression */
1507      if (parenthesized->pn_type != TOK_RP) {
1508        result = -1;
1509        goto done;
1510      }
1511      JSParseNode * object = parenthesized->pn_kid;
1512    
1513      /* this must be an object literal */
1514      if (object->pn_type != TOK_RC) {
1515        result = -1;
1516        goto done;
1517      }
1518    
1519      for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {
1520        /* every element of this list must be TOK_COLON */
1521        if (p->pn_type != TOK_COLON) {
1522          result = -1;
1523          goto done;
1524        }
1525    
1526        /* the key must be a string representing the file */
1527        JSParseNode * key = p->pn_left;
1528        if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {
1529          result = -1;
1530          goto done;
1531        }
1532        char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));
1533    
1534        /* the value must be an object literal OR an array */
1535        JSParseNode * value = p->pn_right;
1536        if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {
1537          result = -1;
1538          goto done;
1539        }
1540    
1541        JSParseNode * array = NULL;
1542        JSParseNode * source = NULL;
1543        if (value->pn_type == TOK_RB) {
1544          /* an array */
1545          array = value;
1546        }
1547        else if (value->pn_type == TOK_RC) {
1548          /* an object literal */
1549          if (value->pn_count != 2) {
1550            result = -1;
1551            goto done;
1552          }
1553          for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {
1554            if (element->pn_type != TOK_COLON) {
1555              result = -1;
1556              goto done;
1557            }
1558            JSParseNode * left = element->pn_left;
1559            if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {
1560              result = -1;
1561              goto done;
1562            }
1563            const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));
1564            if (strcmp(s, "coverage") == 0) {
1565              array = element->pn_right;
1566              if (array->pn_type != TOK_RB) {
1567                result = -1;
1568                goto done;
1569              }
1570            }
1571            else if (strcmp(s, "source") == 0) {
1572              source = element->pn_right;
1573              if (source->pn_type != TOK_RB) {
1574                result = -1;
1575                goto done;
1576              }
1577            }
1578            else {
1579              result = -1;
1580              goto done;
1581            }
1582          }
1583        }
1584        else {
1585          result = -1;
1586          goto done;
1587        }
1588    
1589        if (array == NULL) {
1590          result = -1;
1591          goto done;
1592        }
1593    
1594        /* look up the file in the coverage table */
1595        FileCoverage * file_coverage = JS_HashTableLookup(coverage->coverage_table, id_bytes);
1596        if (file_coverage == NULL) {
1597          /* not there: create a new one */
1598          char * id = xstrdup(id_bytes);
1599          file_coverage = xmalloc(sizeof(FileCoverage));
1600          file_coverage->id = id;
1601          file_coverage->num_coverage_lines = array->pn_count;
1602          file_coverage->coverage_lines = xnew(int, array->pn_count);
1603          file_coverage->source_lines = NULL;
1604    
1605          /* set coverage for all lines */
1606          uint32 i = 0;
1607          for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {
1608            if (element->pn_type == TOK_NUMBER) {
1609              file_coverage->coverage_lines[i] = (int) element->pn_dval;
1610            }
1611            else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1612              file_coverage->coverage_lines[i] = -1;
1613            }
1614            else {
1615              result = -1;
1616              goto done;
1617            }
1618          }
1619          assert(i == array->pn_count);
1620    
1621          /* add to the hash table */
1622          JS_HashTableAdd(coverage->coverage_table, id, file_coverage);
1623          struct FileCoverageList * coverage_list = xmalloc(sizeof(struct FileCoverageList));
1624          coverage_list->file_coverage = file_coverage;
1625          coverage_list->next = coverage->coverage_list;
1626          coverage->coverage_list = coverage_list;
1627        }
1628        else {
1629          /* sanity check */
1630          assert(strcmp(file_coverage->id, id_bytes) == 0);
1631          if (file_coverage->num_coverage_lines != array->pn_count) {
1632            result = -2;
1633            goto done;
1634          }
1635    
1636          /* merge the coverage */
1637          uint32 i = 0;
1638          for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {
1639            if (element->pn_type == TOK_NUMBER) {
1640              if (file_coverage->coverage_lines[i] == -1) {
1641                result = -2;
1642                goto done;
1643              }
1644              file_coverage->coverage_lines[i] += (int) element->pn_dval;
1645            }
1646            else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1647              if (file_coverage->coverage_lines[i] != -1) {
1648                result = -2;
1649                goto done;
1650              }
1651            }
1652            else {
1653              result = -1;
1654              goto done;
1655            }
1656          }
1657          assert(i == array->pn_count);
1658        }
1659    
1660        /* if this JSON file has source, use it */
1661        if (file_coverage->source_lines == NULL && source != NULL) {
1662          file_coverage->num_source_lines = source->pn_count;
1663          file_coverage->source_lines = xnew(char *, source->pn_count);
1664          uint32 i = 0;
1665          for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {
1666            if (element->pn_type != TOK_STRING) {
1667              result = -1;
1668              goto done;
1669            }
1670            file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));
1671          }
1672          assert(i == source->pn_count);
1673        }
1674      }
1675    
1676    done:
1677      js_FinishParseContext(context, &parse_context);
1678      return result;
1679  }  }

Legend:
Removed from v.92  
changed lines
  Added in v.341

  ViewVC Help
Powered by ViewVC 1.1.24