/[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 157 by siliconforks, Sat Sep 13 03:56:45 2008 UTC revision 336 by siliconforks, Thu Oct 23 20:08:29 2008 UTC
# Line 26  Line 26 
26  #include <string.h>  #include <string.h>
27    
28  #include <jsapi.h>  #include <jsapi.h>
29    #include <jsarena.h>
30  #include <jsatom.h>  #include <jsatom.h>
31    #include <jsexn.h>
32  #include <jsfun.h>  #include <jsfun.h>
33  #include <jsinterp.h>  #include <jsinterp.h>
34  #include <jsparse.h>  #include <jsparse.h>
# Line 34  Line 36 
36  #include <jsscope.h>  #include <jsscope.h>
37  #include <jsstr.h>  #include <jsstr.h>
38    
39    #include "encoding.h"
40    #include "global.h"
41    #include "highlight.h"
42  #include "resource-manager.h"  #include "resource-manager.h"
43  #include "util.h"  #include "util.h"
44    
45    struct IfDirective {
46      const jschar * condition_start;
47      const jschar * condition_end;
48      uint16_t start_line;
49      uint16_t end_line;
50      struct IfDirective * next;
51    };
52    
53    static bool * exclusive_directives = NULL;
54    
55  static JSRuntime * runtime = NULL;  static JSRuntime * runtime = NULL;
56  static JSContext * context = NULL;  static JSContext * context = NULL;
57  static JSObject * global = NULL;  static JSObject * global = NULL;
# Line 47  Line 62 
62  */  */
63  static const char * file_id = NULL;  static const char * file_id = NULL;
64  static char * lines = NULL;  static char * lines = NULL;
65    static uint16_t num_lines = 0;
66    
67  void jscoverage_init(void) {  void jscoverage_init(void) {
68    runtime = JS_NewRuntime(8L * 1024L * 1024L);    runtime = JS_NewRuntime(8L * 1024L * 1024L);
# Line 74  Line 90 
90    JS_DestroyRuntime(runtime);    JS_DestroyRuntime(runtime);
91  }  }
92    
93    static void print_javascript(const jschar * characters, size_t num_characters, Stream * f) {
94      for (size_t i = 0; i < num_characters; i++) {
95        jschar c = characters[i];
96        /*
97        XXX does not handle no-break space, other unicode "space separator"
98        */
99        switch (c) {
100        case 0x9:
101        case 0xB:
102        case 0xC:
103          Stream_write_char(f, c);
104          break;
105        default:
106          if (32 <= c && c <= 126) {
107            Stream_write_char(f, c);
108          }
109          else {
110            Stream_printf(f, "\\u%04x", c);
111          }
112          break;
113        }
114      }
115    }
116    
117  static void print_string(JSString * s, Stream * f) {  static void print_string(JSString * s, Stream * f) {
118    for (size_t i = 0; i < s->length; i++) {    size_t length = JSSTRING_LENGTH(s);
119      char c = s->chars[i];    jschar * characters = JSSTRING_CHARS(s);
120      Stream_write_char(f, c);    for (size_t i = 0; i < length; i++) {
121        jschar c = characters[i];
122        if (32 <= c && c <= 126) {
123          switch (c) {
124          case '"':
125            Stream_write_string(f, "\\\"");
126            break;
127    /*
128          case '\'':
129            Stream_write_string(f, "\\'");
130            break;
131    */
132          case '\\':
133            Stream_write_string(f, "\\\\");
134            break;
135          default:
136            Stream_write_char(f, c);
137            break;
138          }
139        }
140        else {
141          switch (c) {
142          case 0x8:
143            Stream_write_string(f, "\\b");
144            break;
145          case 0x9:
146            Stream_write_string(f, "\\t");
147            break;
148          case 0xa:
149            Stream_write_string(f, "\\n");
150            break;
151          /* IE doesn't support this */
152          /*
153          case 0xb:
154            Stream_write_string(f, "\\v");
155            break;
156          */
157          case 0xc:
158            Stream_write_string(f, "\\f");
159            break;
160          case 0xd:
161            Stream_write_string(f, "\\r");
162            break;
163          default:
164            Stream_printf(f, "\\u%04x", c);
165            break;
166          }
167        }
168    }    }
169  }  }
170    
# Line 87  Line 174 
174    print_string(s, f);    print_string(s, f);
175  }  }
176    
177  static void print_string_jsval(jsval value, Stream * f) {  static void print_regex(jsval value, Stream * f) {
178    assert(JSVAL_IS_STRING(value));    assert(JSVAL_IS_STRING(value));
179    JSString * s = JSVAL_TO_STRING(value);    JSString * s = JSVAL_TO_STRING(value);
180    print_string(s, f);    size_t length = JSSTRING_LENGTH(s);
181      jschar * characters = JSSTRING_CHARS(s);
182      for (size_t i = 0; i < length; i++) {
183        jschar c = characters[i];
184        if (32 <= c && c <= 126) {
185          Stream_write_char(f, c);
186        }
187        else {
188          Stream_printf(f, "\\u%04x", c);
189        }
190      }
191  }  }
192    
193  static void print_quoted_string_atom(JSAtom * atom, Stream * f) {  static void print_quoted_string_atom(JSAtom * atom, Stream * f) {
194    assert(ATOM_IS_STRING(atom));    assert(ATOM_IS_STRING(atom));
195    JSString * s = ATOM_TO_STRING(atom);    JSString * s = ATOM_TO_STRING(atom);
196    JSString * quoted = js_QuoteString(context, s, '"');    Stream_write_char(f, '"');
197    print_string(quoted, f);    print_string(s, f);
198      Stream_write_char(f, '"');
199  }  }
200    
201  static const char * get_op(uint8 op) {  static const char * get_op(uint8 op) {
202    switch(op) {    switch(op) {
203      case JSOP_OR:
204        return "||";
205      case JSOP_AND:
206        return "&&";
207    case JSOP_BITOR:    case JSOP_BITOR:
208      return "|";      return "|";
209    case JSOP_BITXOR:    case JSOP_BITXOR:
# Line 112  Line 214 
214      return "==";      return "==";
215    case JSOP_NE:    case JSOP_NE:
216      return "!=";      return "!=";
217    case JSOP_NEW_EQ:    case JSOP_STRICTEQ:
218      return "===";      return "===";
219    case JSOP_NEW_NE:    case JSOP_STRICTNE:
220      return "!==";      return "!==";
221    case JSOP_LT:    case JSOP_LT:
222      return "<";      return "<";
# Line 146  Line 248 
248  }  }
249    
250  static void instrument_expression(JSParseNode * node, Stream * f);  static void instrument_expression(JSParseNode * node, Stream * f);
251  static void instrument_statement(JSParseNode * node, Stream * f, int indent);  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
252    
253  enum FunctionType {  enum FunctionType {
254    FUNCTION_NORMAL,    FUNCTION_NORMAL,
# Line 154  Line 256 
256  };  };
257    
258  static void instrument_function(JSParseNode * node, Stream * f, int indent, enum FunctionType type) {  static void instrument_function(JSParseNode * node, Stream * f, int indent, enum FunctionType type) {
259      assert(node->pn_type == TOK_FUNCTION);
260    assert(node->pn_arity == PN_FUNC);    assert(node->pn_arity == PN_FUNC);
261    assert(ATOM_IS_OBJECT(node->pn_funAtom));    JSObject * object = node->pn_funpob->object;
   JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom);  
262    assert(JS_ObjectIsFunction(context, object));    assert(JS_ObjectIsFunction(context, object));
263    JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);    JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
264    assert(function);    assert(function);
265    assert(object == function->object);    assert(object == &function->object);
266    Stream_printf(f, "%*s", indent, "");    Stream_printf(f, "%*s", indent, "");
267    if (type == FUNCTION_NORMAL) {    if (type == FUNCTION_NORMAL) {
268      Stream_write_string(f, "function");      Stream_write_string(f, "function");
# Line 172  Line 274 
274      print_string_atom(function->atom, f);      print_string_atom(function->atom, f);
275    }    }
276    
277    /* function parameters */    /*
278      function parameters - see JS_DecompileFunction in jsapi.cpp, which calls
279      js_DecompileFunction in jsopcode.cpp
280      */
281    Stream_write_string(f, "(");    Stream_write_string(f, "(");
282    JSAtom ** params = xnew(JSAtom *, function->nargs);    JSArenaPool pool;
283    for (int i = 0; i < function->nargs; i++) {    JS_INIT_ARENA_POOL(&pool, "instrument_function", 256, 1, &context->scriptStackQuota);
284      /* initialize to NULL for sanity check */    jsuword * local_names = NULL;
285      params[i] = NULL;    if (JS_GET_LOCAL_NAME_COUNT(function)) {
286    }      local_names = js_GetLocalNameArray(context, function, &pool);
287    JSScope * scope = OBJ_SCOPE(object);      if (local_names == NULL) {
288    for (JSScopeProperty * scope_property = SCOPE_LAST_PROP(scope); scope_property != NULL; scope_property = scope_property->parent) {        fatal("out of memory");
     if (scope_property->getter != js_GetArgument) {  
       continue;  
289      }      }
     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);  
290    }    }
291    for (int i = 0; i < function->nargs; i++) {    for (int i = 0; i < function->nargs; i++) {
     assert(params[i] != NULL);  
292      if (i > 0) {      if (i > 0) {
293        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
294      }      }
295      if (ATOM_IS_STRING(params[i])) {      JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]);
296        print_string_atom(params[i], f);      print_string_atom(param, f);
     }  
297    }    }
298      JS_FinishArenaPool(&pool);
299    Stream_write_string(f, ") {\n");    Stream_write_string(f, ") {\n");
   free(params);  
300    
301    /* function body */    /* function body */
302    instrument_statement(node->pn_body, f, indent + 2);    instrument_statement(node->pn_body, f, indent + 2, false);
303    
304    Stream_write_string(f, "}\n");    Stream_write_string(f, "}\n");
305  }  }
# Line 277  Line 374 
374      instrument_expression(node->pn_kid3, f);      instrument_expression(node->pn_kid3, f);
375      break;      break;
376    case TOK_OR:    case TOK_OR:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " || ");  
     instrument_expression(node->pn_right, f);  
     break;  
377    case TOK_AND:    case TOK_AND:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " && ");  
     instrument_expression(node->pn_right, f);  
     break;  
378    case TOK_BITOR:    case TOK_BITOR:
379    case TOK_BITXOR:    case TOK_BITXOR:
380    case TOK_BITAND:    case TOK_BITAND:
# Line 398  Line 487 
487      assert(ATOM_IS_STRING(node->pn_atom));      assert(ATOM_IS_STRING(node->pn_atom));
488      {      {
489        JSString * s = ATOM_TO_STRING(node->pn_atom);        JSString * s = ATOM_TO_STRING(node->pn_atom);
490        /* XXX - semantics changed in 1.7 */        bool is_keyword = (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF);
491        if (! ATOM_KEYWORD(node->pn_atom) && js_IsIdentifier(s)) {        if (! is_keyword && js_IsIdentifier(s)) {
492          Stream_write_char(f, '.');          Stream_write_char(f, '.');
493          print_string_atom(node->pn_atom, f);          print_string_atom(node->pn_atom, f);
494        }        }
# Line 446  Line 535 
535        /* check whether this is a getter or setter */        /* check whether this is a getter or setter */
536        switch (p->pn_op) {        switch (p->pn_op) {
537        case JSOP_GETTER:        case JSOP_GETTER:
         Stream_write_string(f, "get ");  
         instrument_expression(p->pn_left, f);  
         instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);  
         break;  
538        case JSOP_SETTER:        case JSOP_SETTER:
539          Stream_write_string(f, "set ");          if (p->pn_op == JSOP_GETTER) {
540              Stream_write_string(f, "get ");
541            }
542            else {
543              Stream_write_string(f, "set ");
544            }
545          instrument_expression(p->pn_left, f);          instrument_expression(p->pn_left, f);
546            if (p->pn_right->pn_type != TOK_FUNCTION) {
547              fatal("parse error: expected function");
548            }
549          instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);          instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);
550          break;          break;
551        default:        default:
# Line 475  Line 568 
568    case TOK_STRING:    case TOK_STRING:
569      print_quoted_string_atom(node->pn_atom, f);      print_quoted_string_atom(node->pn_atom, f);
570      break;      break;
571    case TOK_OBJECT:    case TOK_REGEXP:
572      switch (node->pn_op) {      assert(node->pn_op == JSOP_REGEXP);
573      case JSOP_OBJECT:      {
574        /* I assume this is JSOP_REGEXP */        JSObject * object = node->pn_pob->object;
575        abort();        jsval result;
576        break;        js_regexp_toString(context, object, &result);
577      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;  
578      }      }
579      break;      break;
580    case TOK_NUMBER:    case TOK_NUMBER:
# Line 561  Line 643 
643    }    }
644  }  }
645    
646  static void output_statement(JSParseNode * node, Stream * f, int indent) {  static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
647    switch (node->pn_type) {    switch (node->pn_type) {
648    case TOK_FUNCTION:    case TOK_FUNCTION:
649      instrument_function(node, f, indent, FUNCTION_NORMAL);      instrument_function(node, f, indent, FUNCTION_NORMAL);
# Line 572  Line 654 
654      Stream_write_string(f, "{\n");      Stream_write_string(f, "{\n");
655  */  */
656      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) {
657        instrument_statement(p, f, indent);        instrument_statement(p, f, indent, false);
658      }      }
659  /*  /*
660      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 580  Line 662 
662  */  */
663      break;      break;
664    case TOK_IF:    case TOK_IF:
665      {
666      assert(node->pn_arity == PN_TERNARY);      assert(node->pn_arity == PN_TERNARY);
667    
668        uint16_t line = node->pn_pos.begin.lineno;
669        if (! is_jscoverage_if) {
670          if (line > num_lines) {
671            fatal("%s: script contains more than 65,535 lines", file_id);
672          }
673          if (line >= 2 && exclusive_directives[line - 2]) {
674            is_jscoverage_if = true;
675          }
676        }
677    
678      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
679      Stream_write_string(f, "if (");      Stream_write_string(f, "if (");
680      instrument_expression(node->pn_kid1, f);      instrument_expression(node->pn_kid1, f);
681      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
682      instrument_statement(node->pn_kid2, f, indent + 2);      if (is_jscoverage_if && node->pn_kid3) {
683          uint16_t else_start = node->pn_kid3->pn_pos.begin.lineno;
684          uint16_t else_end = node->pn_kid3->pn_pos.end.lineno + 1;
685          Stream_printf(f, "%*s", indent + 2, "");
686          Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, else_start, else_end);
687        }
688        instrument_statement(node->pn_kid2, f, indent + 2, false);
689      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
690      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
691      if (node->pn_kid3) {  
692        if (node->pn_kid3 || is_jscoverage_if) {
693        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
694        Stream_write_string(f, "else {\n");        Stream_write_string(f, "else {\n");
695        instrument_statement(node->pn_kid3, f, indent + 2);  
696          if (is_jscoverage_if) {
697            uint16_t if_start = node->pn_kid2->pn_pos.begin.lineno + 1;
698            uint16_t if_end = node->pn_kid2->pn_pos.end.lineno + 1;
699            Stream_printf(f, "%*s", indent + 2, "");
700            Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_start, if_end);
701          }
702    
703          if (node->pn_kid3) {
704            instrument_statement(node->pn_kid3, f, indent + 2, is_jscoverage_if);
705          }
706    
707        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
708        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
709      }      }
710    
711      break;      break;
712      }
713    case TOK_SWITCH:    case TOK_SWITCH:
714      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
715      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 617  Line 731 
731          abort();          abort();
732          break;          break;
733        }        }
734        instrument_statement(p->pn_right, f, indent + 2);        instrument_statement(p->pn_right, f, indent + 2, false);
735      }      }
736      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
737      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
# Line 632  Line 746 
746      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
747      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
748      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
749      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
750      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
751      break;      break;
752    case TOK_DO:    case TOK_DO:
753      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
754      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
755      Stream_write_string(f, "do {\n");      Stream_write_string(f, "do {\n");
756      instrument_statement(node->pn_left, f, indent + 2);      instrument_statement(node->pn_left, f, indent + 2, false);
757      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
758      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
759      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
# Line 702  Line 816 
816        break;        break;
817      }      }
818      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
819      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
820      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
821      break;      break;
822    case TOK_THROW:    case TOK_THROW:
# Line 715  Line 829 
829    case TOK_TRY:    case TOK_TRY:
830      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
831      Stream_write_string(f, "try {\n");      Stream_write_string(f, "try {\n");
832      instrument_statement(node->pn_kid1, f, indent + 2);      instrument_statement(node->pn_kid1, f, indent + 2, false);
833      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
834      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
835      {      if (node->pn_kid2) {
836        for (JSParseNode * catch = node->pn_kid2; catch != NULL; catch = catch->pn_kid2) {        assert(node->pn_kid2->pn_type == TOK_RESERVED);
837          for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) {
838            assert(scope->pn_type == TOK_LEXICALSCOPE);
839            JSParseNode * catch = scope->pn_expr;
840          assert(catch->pn_type == TOK_CATCH);          assert(catch->pn_type == TOK_CATCH);
841          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
842          Stream_write_string(f, "catch (");          Stream_write_string(f, "catch (");
843          assert(catch->pn_kid1->pn_arity == PN_NAME);          assert(catch->pn_kid1->pn_arity == PN_NAME);
844          print_string_atom(catch->pn_kid1->pn_atom, f);          print_string_atom(catch->pn_kid1->pn_atom, f);
845          if (catch->pn_kid1->pn_expr) {          if (catch->pn_kid2) {
846            Stream_write_string(f, " if ");            Stream_write_string(f, " if ");
847            instrument_expression(catch->pn_kid1->pn_expr, f);            instrument_expression(catch->pn_kid2, f);
848          }          }
849          Stream_write_string(f, ") {\n");          Stream_write_string(f, ") {\n");
850          instrument_statement(catch->pn_kid3, f, indent + 2);          instrument_statement(catch->pn_kid3, f, indent + 2, false);
851          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
852          Stream_write_string(f, "}\n");          Stream_write_string(f, "}\n");
853        }        }
# Line 738  Line 855 
855      if (node->pn_kid3) {      if (node->pn_kid3) {
856        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
857        Stream_write_string(f, "finally {\n");        Stream_write_string(f, "finally {\n");
858        instrument_statement(node->pn_kid3, f, indent + 2);        instrument_statement(node->pn_kid3, f, indent + 2, false);
859        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
860        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
861      }      }
# Line 764  Line 881 
881      Stream_write_string(f, "with (");      Stream_write_string(f, "with (");
882      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
883      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
884      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
885      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
886      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
887      break;      break;
# Line 802  Line 919 
919      /*      /*
920      ... use output_statement instead of instrument_statement.      ... use output_statement instead of instrument_statement.
921      */      */
922      output_statement(node->pn_expr, f, indent);      output_statement(node->pn_expr, f, indent, false);
923      break;      break;
924    default:    default:
925      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 814  Line 931 
931  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
932  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
933  */  */
934  static void instrument_statement(JSParseNode * node, Stream * f, int indent) {  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
935    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC) {
936      int line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
937        if (line > num_lines) {
938          fatal("%s: script contains more than 65,535 lines", file_id);
939        }
940    
941      /* the root node has line number 0 */      /* the root node has line number 0 */
942      if (line != 0) {      if (line != 0) {
943        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
# Line 824  Line 945 
945        lines[line - 1] = 1;        lines[line - 1] = 1;
946      }      }
947    }    }
948    output_statement(node, f, indent);    output_statement(node, f, indent, is_jscoverage_if);
949  }  }
950    
951  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) {
952    file_id = id;    const jschar * characters_end = characters + line_end;
953      const jschar * cp = characters + line_start;
954    /* scan the javascript */    const char * bp = prefix;
955    size_t input_length = input->length;    for (;;) {
956    jschar * base = js_InflateString(context, (char *) input->data, &input_length);      if (*bp == '\0') {
957    if (base == NULL) {        return true;
958      fatal("out of memory");      }
959        else if (cp == characters_end) {
960          return false;
961        }
962        else if (*cp != *bp) {
963          return false;
964        }
965        bp++;
966        cp++;
967    }    }
968    JSTokenStream * token_stream = js_NewTokenStream(context, base, input_length, NULL, 1, NULL);  }
969    if (token_stream == NULL) {  
970      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) {
971      /* XXX - other Unicode space */
972      const jschar * end = characters + line_end;
973      for (const jschar * p = characters + line_start; p < end; p++) {
974        jschar c = *p;
975        if (c == 0x9 || c == 0xB || c == 0xC || c == 0x20 || c == 0xA0) {
976          continue;
977        }
978        else {
979          return false;
980        }
981    }    }
982      return true;
983    }
984    
985    static void error_reporter(JSContext * context, const char * message, JSErrorReport * report) {
986      fprintf(stderr, "jscoverage: parse error: line %u: %s\n", report->lineno, message);
987    }
988    
989    void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) {
990      file_id = id;
991    
992    /* parse the javascript */    /* parse the javascript */
993    JSParseNode * node = js_ParseTokenStream(context, global, token_stream);    JSParseContext parse_context;
994      if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) {
995        fatal("cannot create token stream from file: %s", file_id);
996      }
997      JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);
998      JSParseNode * node = js_ParseScript(context, global, &parse_context);
999    if (node == NULL) {    if (node == NULL) {
1000        js_ReportUncaughtException(context);
1001      fatal("parse error in file: %s", file_id);      fatal("parse error in file: %s", file_id);
1002    }    }
1003    int num_lines = node->pn_pos.end.lineno;    JS_SetErrorReporter(context, old_error_reporter);
1004      num_lines = node->pn_pos.end.lineno;
1005    lines = xmalloc(num_lines);    lines = xmalloc(num_lines);
1006    for (int i = 0; i < num_lines; i++) {    for (unsigned int i = 0; i < num_lines; i++) {
1007      lines[i] = 0;      lines[i] = 0;
1008    }    }
1009    
1010      /* search code for conditionals */
1011      exclusive_directives = xnew(bool, num_lines);
1012      for (unsigned int i = 0; i < num_lines; i++) {
1013        exclusive_directives[i] = false;
1014      }
1015    
1016      bool has_conditionals = false;
1017      struct IfDirective * if_directives = NULL;
1018      size_t line_number = 0;
1019      size_t i = 0;
1020      while (i < num_characters) {
1021        if (line_number == UINT16_MAX) {
1022          fatal("%s: script has more than 65,535 lines", file_id);
1023        }
1024        line_number++;
1025        size_t line_start = i;
1026        jschar c;
1027        bool done = false;
1028        while (! done && i < num_characters) {
1029          c = characters[i];
1030          switch (c) {
1031          case '\r':
1032          case '\n':
1033          case 0x2028:
1034          case 0x2029:
1035            done = true;
1036            break;
1037          default:
1038            i++;
1039          }
1040        }
1041        size_t line_end = i;
1042        if (i < num_characters) {
1043          i++;
1044          if (c == '\r' && i < num_characters && characters[i] == '\n') {
1045            i++;
1046          }
1047        }
1048    
1049        if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) {
1050          has_conditionals = true;
1051    
1052          if (characters_are_white_space(characters, line_start + 16, line_end)) {
1053            exclusive_directives[line_number - 1] = true;
1054          }
1055          else {
1056            struct IfDirective * if_directive = xnew(struct IfDirective, 1);
1057            if_directive->condition_start = characters + line_start + 16;
1058            if_directive->condition_end = characters + line_end;
1059            if_directive->start_line = line_number;
1060            if_directive->end_line = 0;
1061            if_directive->next = if_directives;
1062            if_directives = if_directive;
1063          }
1064        }
1065        else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) {
1066          for (struct IfDirective * p = if_directives; p != NULL; p = p->next) {
1067            if (p->end_line == 0) {
1068              p->end_line = line_number;
1069              break;
1070            }
1071          }
1072        }
1073      }
1074    
1075    /*    /*
1076    An instrumented JavaScript file has 3 sections:    An instrumented JavaScript file has 4 sections:
1077    1. initialization    1. initialization
1078    2. instrumented source code    2. instrumented source code
1079    3. original source code    3. conditionals
1080      4. original source code
1081    */    */
1082    
1083    Stream * instrumented = Stream_new(0);    Stream * instrumented = Stream_new(0);
1084    instrument_statement(node, instrumented, 0);    instrument_statement(node, instrumented, 0, false);
1085      js_FinishParseContext(context, &parse_context);
1086    
1087    /* write line number info to the output */    /* write line number info to the output */
1088    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 876  Line 1098 
1098    Stream_write_string(output, "}\n");    Stream_write_string(output, "}\n");
1099    free(lines);    free(lines);
1100    lines = NULL;    lines = NULL;
1101      free(exclusive_directives);
1102      exclusive_directives = NULL;
1103    
1104    /* conditionals */    /* conditionals */
1105    bool has_conditionals = false;    if (has_conditionals) {
1106    size_t line_number = 0;      Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);
   size_t i = 0;  
   while (i < input_length) {  
     line_number++;  
     size_t line_start = i;  
     while (i < input_length && base[i] != '\r' && base[i] != '\n') {  
       i++;  
     }  
     size_t line_end = i;  
     if (i < input_length) {  
       if (base[i] == '\r') {  
         line_end = i;  
         i++;  
         if (i < input_length && base[i] == '\n') {  
           i++;  
         }  
       }  
       else if (base[i] == '\n') {  
         line_end = i;  
         i++;  
       }  
       else {  
         abort();  
       }  
     }  
     char * line = js_DeflateString(context, base + line_start, line_end - line_start);  
     if (str_starts_with(line, "//#JSCOVERAGE_IF")) {  
       if (! has_conditionals) {  
         has_conditionals = true;  
         Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);  
       }  
       Stream_printf(output, "_$jscoverage['%s'].conditionals[%d] = {condition: function () {return %s;}, ", file_id, line_number, line + 16);  
     }  
     else if (str_starts_with(line, "//#JSCOVERAGE_ENDIF")) {  
       Stream_printf(output, "end: %d};\n", line_number);  
     }  
     JS_free(context, line);  
1107    }    }
1108    
1109    /* copy the instrumented source code to the output */    /* copy the instrumented source code to the output */
1110    Stream_write(output, instrumented->data, instrumented->length);    Stream_write(output, instrumented->data, instrumented->length);
1111    Stream_write_char(output, '\n');  
1112      /* conditionals */
1113      for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) {
1114        Stream_write_string(output, "if (!(");
1115        print_javascript(if_directive->condition_start, if_directive->condition_end - if_directive->condition_start, output);
1116        Stream_write_string(output, ")) {\n");
1117        Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line);
1118        Stream_write_string(output, "}\n");
1119      }
1120    
1121      /* free */
1122      while (if_directives != NULL) {
1123        struct IfDirective * if_directive = if_directives;
1124        if_directives = if_directives->next;
1125        free(if_directive);
1126      }
1127    
1128    /* copy the original source to the output */    /* copy the original source to the output */
1129    i = 0;    Stream_printf(output, "_$jscoverage['%s'].source = ", file_id);
1130    while (i < input_length) {    jscoverage_write_source(id, characters, num_characters, output);
1131      Stream_write_string(output, "// ");    Stream_printf(output, ";\n");
     size_t line_start = i;  
     while (i < input_length && base[i] != '\r' && base[i] != '\n') {  
       i++;  
     }  
1132    
1133      size_t line_end = i;    Stream_delete(instrumented);
1134      if (i < input_length) {  
1135        if (base[i] == '\r') {    file_id = NULL;
1136          line_end = i;  }
1137    
1138    void jscoverage_write_source(const char * id, const jschar * characters, size_t num_characters, Stream * output) {
1139      Stream_write_string(output, "[");
1140      if (jscoverage_highlight) {
1141        Stream * highlighted_stream = Stream_new(num_characters);
1142        jscoverage_highlight_js(context, id, characters, num_characters, highlighted_stream);
1143        size_t i = 0;
1144        while (i < highlighted_stream->length) {
1145          if (i > 0) {
1146            Stream_write_char(output, ',');
1147          }
1148    
1149          Stream_write_char(output, '"');
1150          bool done = false;
1151          while (! done) {
1152            char c = highlighted_stream->data[i];
1153            switch (c) {
1154            case 0x8:
1155              /* backspace */
1156              Stream_write_string(output, "\\b");
1157              break;
1158            case 0x9:
1159              /* horizontal tab */
1160              Stream_write_string(output, "\\t");
1161              break;
1162            case 0xa:
1163              /* line feed (new line) */
1164              done = true;
1165              break;
1166            /* IE doesn't support this */
1167            /*
1168            case 0xb:
1169              Stream_write_string(output, "\\v");
1170              break;
1171            */
1172            case 0xc:
1173              /* form feed */
1174              Stream_write_string(output, "\\f");
1175              break;
1176            case 0xd:
1177              /* carriage return */
1178              done = true;
1179              if (i + 1 < highlighted_stream->length && highlighted_stream->data[i + 1] == '\n') {
1180                i++;
1181              }
1182              break;
1183            case '"':
1184              Stream_write_string(output, "\\\"");
1185              break;
1186            case '\\':
1187              Stream_write_string(output, "\\\\");
1188              break;
1189            default:
1190              Stream_write_char(output, c);
1191              break;
1192            }
1193          i++;          i++;
1194          if (i < input_length && base[i] == '\n') {          if (i >= highlighted_stream->length) {
1195            i++;            done = true;
1196          }          }
1197        }        }
1198        else if (base[i] == '\n') {        Stream_write_char(output, '"');
1199          line_end = i;      }
1200        Stream_delete(highlighted_stream);
1201      }
1202      else {
1203        size_t i = 0;
1204        while (i < num_characters) {
1205          if (i > 0) {
1206            Stream_write_char(output, ',');
1207          }
1208    
1209          Stream_write_char(output, '"');
1210          bool done = false;
1211          while (! done) {
1212            jschar c = characters[i];
1213            switch (c) {
1214            case 0x8:
1215              /* backspace */
1216              Stream_write_string(output, "\\b");
1217              break;
1218            case 0x9:
1219              /* horizontal tab */
1220              Stream_write_string(output, "\\t");
1221              break;
1222            case 0xa:
1223              /* line feed (new line) */
1224              done = true;
1225              break;
1226            /* IE doesn't support this */
1227            /*
1228            case 0xb:
1229              Stream_write_string(output, "\\v");
1230              break;
1231            */
1232            case 0xc:
1233              /* form feed */
1234              Stream_write_string(output, "\\f");
1235              break;
1236            case 0xd:
1237              /* carriage return */
1238              done = true;
1239              if (i + 1 < num_characters && characters[i + 1] == '\n') {
1240                i++;
1241              }
1242              break;
1243            case '"':
1244              Stream_write_string(output, "\\\"");
1245              break;
1246            case '\\':
1247              Stream_write_string(output, "\\\\");
1248              break;
1249            case '&':
1250              Stream_write_string(output, "&amp;");
1251              break;
1252            case '<':
1253              Stream_write_string(output, "&lt;");
1254              break;
1255            case '>':
1256              Stream_write_string(output, "&gt;");
1257              break;
1258            case 0x2028:
1259            case 0x2029:
1260              done = true;
1261              break;
1262            default:
1263              if (32 <= c && c <= 126) {
1264                Stream_write_char(output, c);
1265              }
1266              else {
1267                Stream_printf(output, "&#%d;", c);
1268              }
1269              break;
1270            }
1271          i++;          i++;
1272            if (i >= num_characters) {
1273              done = true;
1274            }
1275        }        }
1276        else {        Stream_write_char(output, '"');
         abort();  
       }  
1277      }      }
   
     char * line = js_DeflateString(context, base + line_start, line_end - line_start);  
     Stream_write_string(output, line);  
     Stream_write_char(output, '\n');  
     JS_free(context, line);  
1278    }    }
1279      Stream_write_string(output, "]");
   Stream_delete(instrumented);  
   
   JS_free(context, base);  
   
   file_id = NULL;  
1280  }  }
1281    
1282  void jscoverage_copy_resources(const char * destination_directory) {  void jscoverage_copy_resources(const char * destination_directory) {
1283    copy_resource("jscoverage.html", destination_directory);    copy_resource("jscoverage.html", destination_directory);
1284    copy_resource("jscoverage.css", destination_directory);    copy_resource("jscoverage.css", destination_directory);
1285    copy_resource("jscoverage.js", destination_directory);    copy_resource("jscoverage.js", destination_directory);
1286      copy_resource("jscoverage-ie.css", destination_directory);
1287    copy_resource("jscoverage-throbber.gif", destination_directory);    copy_resource("jscoverage-throbber.gif", destination_directory);
1288    copy_resource("jscoverage-sh_main.js", destination_directory);    copy_resource("jscoverage-highlight.css", destination_directory);
   copy_resource("jscoverage-sh_javascript.js", destination_directory);  
   copy_resource("jscoverage-sh_nedit.css", destination_directory);  
1289  }  }
1290    
1291  /*  /*
# Line 1004  Line 1320 
1320    JS_HashTableDestroy(coverage->coverage_table);    JS_HashTableDestroy(coverage->coverage_table);
1321    struct FileCoverageList * p = coverage->coverage_list;    struct FileCoverageList * p = coverage->coverage_list;
1322    while (p != NULL) {    while (p != NULL) {
1323      free(p->file_coverage->lines);      free(p->file_coverage->coverage_lines);
1324      free(p->file_coverage->source);      if (p->file_coverage->source_lines != NULL) {
1325          for (uint32 i = 0; i < p->file_coverage->num_source_lines; i++) {
1326            free(p->file_coverage->source_lines[i]);
1327          }
1328          free(p->file_coverage->source_lines);
1329        }
1330      free(p->file_coverage->id);      free(p->file_coverage->id);
1331      free(p->file_coverage);      free(p->file_coverage);
1332      struct FileCoverageList * q = p;      struct FileCoverageList * q = p;
# Line 1034  Line 1355 
1355  }  }
1356    
1357  int jscoverage_parse_json(Coverage * coverage, const uint8_t * json, size_t length) {  int jscoverage_parse_json(Coverage * coverage, const uint8_t * json, size_t length) {
1358      int result = 0;
1359    
1360    jschar * base = js_InflateString(context, (char *) json, &length);    jschar * base = js_InflateString(context, (char *) json, &length);
1361    if (base == NULL) {    if (base == NULL) {
1362      fatal("out of memory");      fatal("out of memory");
# Line 1046  Line 1369 
1369    
1370    JS_free(context, base);    JS_free(context, base);
1371    
1372    JSTokenStream * token_stream = js_NewTokenStream(context, parenthesized_json, length + 2, NULL, 1, NULL);    JSParseContext parse_context;
1373    if (token_stream == NULL) {    if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) {
1374      fatal("cannot create token stream");      free(parenthesized_json);
1375        return -1;
1376    }    }
1377      JSParseNode * root = js_ParseScript(context, global, &parse_context);
   JSParseNode * root = js_ParseTokenStream(context, global, token_stream);  
1378    free(parenthesized_json);    free(parenthesized_json);
1379    if (root == NULL) {    if (root == NULL) {
1380      return -1;      result = -1;
1381        goto done;
1382    }    }
1383    
1384    /* root node must be TOK_LC */    /* root node must be TOK_LC */
1385    if (root->pn_type != TOK_LC) {    if (root->pn_type != TOK_LC) {
1386      return -1;      result = -1;
1387        goto done;
1388    }    }
1389    JSParseNode * semi = root->pn_u.list.head;    JSParseNode * semi = root->pn_u.list.head;
1390    
1391    /* the list must be TOK_SEMI and it must contain only one element */    /* the list must be TOK_SEMI and it must contain only one element */
1392    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {
1393      return -1;      result = -1;
1394        goto done;
1395    }    }
1396    JSParseNode * parenthesized = semi->pn_kid;    JSParseNode * parenthesized = semi->pn_kid;
1397    
1398    /* this must be a parenthesized expression */    /* this must be a parenthesized expression */
1399    if (parenthesized->pn_type != TOK_RP) {    if (parenthesized->pn_type != TOK_RP) {
1400      return -1;      result = -1;
1401        goto done;
1402    }    }
1403    JSParseNode * object = parenthesized->pn_kid;    JSParseNode * object = parenthesized->pn_kid;
1404    
1405    /* this must be an object literal */    /* this must be an object literal */
1406    if (object->pn_type != TOK_RC) {    if (object->pn_type != TOK_RC) {
1407      return -1;      result = -1;
1408        goto done;
1409    }    }
1410    
1411    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {
1412      /* every element of this list must be TOK_COLON */      /* every element of this list must be TOK_COLON */
1413      if (p->pn_type != TOK_COLON) {      if (p->pn_type != TOK_COLON) {
1414        return -1;        result = -1;
1415          goto done;
1416      }      }
1417    
1418      /* the key must be a string representing the file */      /* the key must be a string representing the file */
1419      JSParseNode * key = p->pn_left;      JSParseNode * key = p->pn_left;
1420      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {
1421        return -1;        result = -1;
1422          goto done;
1423      }      }
1424      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));
1425    
1426      /* the value must be an object literal OR an array */      /* the value must be an object literal OR an array */
1427      JSParseNode * value = p->pn_right;      JSParseNode * value = p->pn_right;
1428      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {
1429        return -1;        result = -1;
1430          goto done;
1431      }      }
1432    
1433      JSParseNode * array = NULL;      JSParseNode * array = NULL;
# Line 1108  Line 1439 
1439      else if (value->pn_type == TOK_RC) {      else if (value->pn_type == TOK_RC) {
1440        /* an object literal */        /* an object literal */
1441        if (value->pn_count != 2) {        if (value->pn_count != 2) {
1442          return -1;          result = -1;
1443            goto done;
1444        }        }
1445        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {
1446          if (element->pn_type != TOK_COLON) {          if (element->pn_type != TOK_COLON) {
1447            return -1;            result = -1;
1448              goto done;
1449          }          }
1450          JSParseNode * left = element->pn_left;          JSParseNode * left = element->pn_left;
1451          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {
1452            return -1;            result = -1;
1453              goto done;
1454          }          }
1455          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));
1456          if (strcmp(s, "coverage") == 0) {          if (strcmp(s, "coverage") == 0) {
1457            array = element->pn_right;            array = element->pn_right;
1458            if (array->pn_type != TOK_RB) {            if (array->pn_type != TOK_RB) {
1459              return -1;              result = -1;
1460                goto done;
1461            }            }
1462          }          }
1463          else if (strcmp(s, "source") == 0) {          else if (strcmp(s, "source") == 0) {
1464            source = element->pn_right;            source = element->pn_right;
1465            if (source->pn_type != TOK_STRING || ! ATOM_IS_STRING(source->pn_atom)) {            if (source->pn_type != TOK_RB) {
1466              return -1;              result = -1;
1467                goto done;
1468            }            }
1469          }          }
1470          else {          else {
1471            return -1;            result = -1;
1472              goto done;
1473          }          }
1474        }        }
1475      }      }
1476      else {      else {
1477        return -1;        result = -1;
1478          goto done;
1479      }      }
1480    
1481      if (array == NULL) {      if (array == NULL) {
1482        return -1;        result = -1;
1483          goto done;
1484      }      }
1485    
1486      /* look up the file in the coverage table */      /* look up the file in the coverage table */
# Line 1151  Line 1490 
1490        char * id = xstrdup(id_bytes);        char * id = xstrdup(id_bytes);
1491        file_coverage = xmalloc(sizeof(FileCoverage));        file_coverage = xmalloc(sizeof(FileCoverage));
1492        file_coverage->id = id;        file_coverage->id = id;
1493        file_coverage->num_lines = array->pn_count - 1;        file_coverage->num_coverage_lines = array->pn_count;
1494        file_coverage->lines = xnew(int, array->pn_count);        file_coverage->coverage_lines = xnew(int, array->pn_count);
1495        if (source == NULL) {        file_coverage->source_lines = NULL;
         file_coverage->source = NULL;  
       }  
       else {  
         file_coverage->source = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(source->pn_atom)));  
       }  
1496    
1497        /* set coverage for all lines */        /* set coverage for all lines */
1498        uint32 i = 0;        uint32 i = 0;
1499        for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {        for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {
1500          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1501            file_coverage->lines[i] = (int) element->pn_dval;            file_coverage->coverage_lines[i] = (int) element->pn_dval;
1502          }          }
1503          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1504            file_coverage->lines[i] = -1;            file_coverage->coverage_lines[i] = -1;
1505          }          }
1506          else {          else {
1507            return -1;            result = -1;
1508              goto done;
1509          }          }
1510        }        }
1511        assert(i == array->pn_count);        assert(i == array->pn_count);
# Line 1185  Line 1520 
1520      else {      else {
1521        /* sanity check */        /* sanity check */
1522        assert(strcmp(file_coverage->id, id_bytes) == 0);        assert(strcmp(file_coverage->id, id_bytes) == 0);
1523        if (file_coverage->num_lines != array->pn_count - 1) {        if (file_coverage->num_coverage_lines != array->pn_count) {
1524          return -2;          result = -2;
1525            goto done;
1526        }        }
1527    
1528        /* merge the coverage */        /* merge the coverage */
1529        uint32 i = 0;        uint32 i = 0;
1530        for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {        for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {
1531          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1532            if (file_coverage->lines[i] == -1) {            if (file_coverage->coverage_lines[i] == -1) {
1533              return -2;              result = -2;
1534                goto done;
1535            }            }
1536            file_coverage->lines[i] += (int) element->pn_dval;            file_coverage->coverage_lines[i] += (int) element->pn_dval;
1537          }          }
1538          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1539            if (file_coverage->lines[i] != -1) {            if (file_coverage->coverage_lines[i] != -1) {
1540              return -2;              result = -2;
1541                goto done;
1542            }            }
1543          }          }
1544          else {          else {
1545            return -1;            result = -1;
1546              goto done;
1547          }          }
1548        }        }
1549        assert(i == array->pn_count);        assert(i == array->pn_count);
1550        }
1551    
1552        /* if this JSON file has source, use it */      /* if this JSON file has source, use it */
1553        if (file_coverage->source == NULL && source != NULL) {      if (file_coverage->source_lines == NULL && source != NULL) {
1554          file_coverage->source = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(source->pn_atom)));        file_coverage->num_source_lines = source->pn_count;
1555          file_coverage->source_lines = xnew(char *, source->pn_count);
1556          uint32 i = 0;
1557          for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {
1558            if (element->pn_type != TOK_STRING) {
1559              result = -1;
1560              goto done;
1561            }
1562            file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));
1563        }        }
1564          assert(i == source->pn_count);
1565      }      }
1566    }    }
1567    
1568    return 0;  done:
1569      js_FinishParseContext(context, &parse_context);
1570      return result;
1571  }  }

Legend:
Removed from v.157  
changed lines
  Added in v.336

  ViewVC Help
Powered by ViewVC 1.1.24