/[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 214 by siliconforks, Fri Oct 3 02:26:04 2008 UTC revision 317 by siliconforks, Tue Oct 14 01:28:46 2008 UTC
# Line 27  Line 27 
27    
28  #include <jsapi.h>  #include <jsapi.h>
29  #include <jsatom.h>  #include <jsatom.h>
30    #include <jsexn.h>
31  #include <jsfun.h>  #include <jsfun.h>
32  #include <jsinterp.h>  #include <jsinterp.h>
33  #include <jsparse.h>  #include <jsparse.h>
# Line 40  Line 41 
41  #include "resource-manager.h"  #include "resource-manager.h"
42  #include "util.h"  #include "util.h"
43    
44    struct IfDirective {
45      const jschar * condition_start;
46      const jschar * condition_end;
47      uint16_t start_line;
48      uint16_t end_line;
49      struct IfDirective * next;
50    };
51    
52    static bool * exclusive_directives = NULL;
53    
54  static JSRuntime * runtime = NULL;  static JSRuntime * runtime = NULL;
55  static JSContext * context = NULL;  static JSContext * context = NULL;
56  static JSObject * global = NULL;  static JSObject * global = NULL;
# Line 78  Line 89 
89    JS_DestroyRuntime(runtime);    JS_DestroyRuntime(runtime);
90  }  }
91    
92    static void print_javascript(const jschar * characters, size_t num_characters, Stream * f) {
93      for (size_t i = 0; i < num_characters; i++) {
94        jschar c = characters[i];
95        /*
96        XXX does not handle no-break space, other unicode "space separator"
97        */
98        switch (c) {
99        case 0x9:
100        case 0xB:
101        case 0xC:
102          Stream_write_char(f, c);
103          break;
104        default:
105          if (32 <= c && c <= 126) {
106            Stream_write_char(f, c);
107          }
108          else {
109            Stream_printf(f, "\\u%04x", c);
110          }
111          break;
112        }
113      }
114    }
115    
116  static void print_string(JSString * s, Stream * f) {  static void print_string(JSString * s, Stream * f) {
117    size_t length = JSSTRING_LENGTH(s);    size_t length = JSSTRING_LENGTH(s);
118    jschar * characters = JSSTRING_CHARS(s);    jschar * characters = JSSTRING_CHARS(s);
# Line 112  Line 147 
147        case 0xa:        case 0xa:
148          Stream_write_string(f, "\\n");          Stream_write_string(f, "\\n");
149          break;          break;
150          /* IE doesn't support this */
151          /*
152        case 0xb:        case 0xb:
153          Stream_write_string(f, "\\v");          Stream_write_string(f, "\\v");
154          break;          break;
155          */
156        case 0xc:        case 0xc:
157          Stream_write_string(f, "\\f");          Stream_write_string(f, "\\f");
158          break;          break;
# Line 205  Line 243 
243  }  }
244    
245  static void instrument_expression(JSParseNode * node, Stream * f);  static void instrument_expression(JSParseNode * node, Stream * f);
246  static void instrument_statement(JSParseNode * node, Stream * f, int indent);  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
247    
248  enum FunctionType {  enum FunctionType {
249    FUNCTION_NORMAL,    FUNCTION_NORMAL,
# Line 261  Line 299 
299    free(params);    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 505  Line 543 
543        /* check whether this is a getter or setter */        /* check whether this is a getter or setter */
544        switch (p->pn_op) {        switch (p->pn_op) {
545        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;  
546        case JSOP_SETTER:        case JSOP_SETTER:
547          Stream_write_string(f, "set ");          if (p->pn_op == JSOP_GETTER) {
548              Stream_write_string(f, "get ");
549            }
550            else {
551              Stream_write_string(f, "set ");
552            }
553          instrument_expression(p->pn_left, f);          instrument_expression(p->pn_left, f);
554            if (p->pn_right->pn_type != TOK_FUNCTION) {
555              fatal("parse error: expected function");
556            }
557          instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);          instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);
558          break;          break;
559        default:        default:
# Line 620  Line 662 
662    }    }
663  }  }
664    
665  static void output_statement(JSParseNode * node, Stream * f, int indent) {  static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
666    switch (node->pn_type) {    switch (node->pn_type) {
667    case TOK_FUNCTION:    case TOK_FUNCTION:
668      instrument_function(node, f, indent, FUNCTION_NORMAL);      instrument_function(node, f, indent, FUNCTION_NORMAL);
# Line 631  Line 673 
673      Stream_write_string(f, "{\n");      Stream_write_string(f, "{\n");
674  */  */
675      for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) {      for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) {
676        instrument_statement(p, f, indent);        instrument_statement(p, f, indent, false);
677      }      }
678  /*  /*
679      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 639  Line 681 
681  */  */
682      break;      break;
683    case TOK_IF:    case TOK_IF:
684      {
685      assert(node->pn_arity == PN_TERNARY);      assert(node->pn_arity == PN_TERNARY);
686    
687        uint16_t line = node->pn_pos.begin.lineno;
688        if (! is_jscoverage_if) {
689          if (line > num_lines) {
690            fatal("%s: script contains more than 65,535 lines", file_id);
691          }
692          if (line >= 2 && exclusive_directives[line - 2]) {
693            is_jscoverage_if = true;
694          }
695        }
696    
697      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
698      Stream_write_string(f, "if (");      Stream_write_string(f, "if (");
699      instrument_expression(node->pn_kid1, f);      instrument_expression(node->pn_kid1, f);
700      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
701      instrument_statement(node->pn_kid2, f, indent + 2);      if (is_jscoverage_if && node->pn_kid3) {
702          uint16_t else_start = node->pn_kid3->pn_pos.begin.lineno;
703          uint16_t else_end = node->pn_kid3->pn_pos.end.lineno + 1;
704          Stream_printf(f, "%*s", indent + 2, "");
705          Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, else_start, else_end);
706        }
707        instrument_statement(node->pn_kid2, f, indent + 2, false);
708      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
709      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
710      if (node->pn_kid3) {  
711        if (node->pn_kid3 || is_jscoverage_if) {
712        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
713        Stream_write_string(f, "else {\n");        Stream_write_string(f, "else {\n");
714        instrument_statement(node->pn_kid3, f, indent + 2);  
715          if (is_jscoverage_if) {
716            uint16_t if_start = node->pn_kid2->pn_pos.begin.lineno + 1;
717            uint16_t if_end = node->pn_kid2->pn_pos.end.lineno + 1;
718            Stream_printf(f, "%*s", indent + 2, "");
719            Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_start, if_end);
720          }
721    
722          if (node->pn_kid3) {
723            instrument_statement(node->pn_kid3, f, indent + 2, is_jscoverage_if);
724          }
725    
726        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
727        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
728      }      }
729    
730      break;      break;
731      }
732    case TOK_SWITCH:    case TOK_SWITCH:
733      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
734      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 676  Line 750 
750          abort();          abort();
751          break;          break;
752        }        }
753        instrument_statement(p->pn_right, f, indent + 2);        instrument_statement(p->pn_right, f, indent + 2, false);
754      }      }
755      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
756      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
# Line 691  Line 765 
765      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
766      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
767      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
768      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
769      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
770      break;      break;
771    case TOK_DO:    case TOK_DO:
772      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
773      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
774      Stream_write_string(f, "do {\n");      Stream_write_string(f, "do {\n");
775      instrument_statement(node->pn_left, f, indent + 2);      instrument_statement(node->pn_left, f, indent + 2, false);
776      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
777      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
778      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
# Line 761  Line 835 
835        break;        break;
836      }      }
837      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
838      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
839      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
840      break;      break;
841    case TOK_THROW:    case TOK_THROW:
# Line 774  Line 848 
848    case TOK_TRY:    case TOK_TRY:
849      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
850      Stream_write_string(f, "try {\n");      Stream_write_string(f, "try {\n");
851      instrument_statement(node->pn_kid1, f, indent + 2);      instrument_statement(node->pn_kid1, f, indent + 2, false);
852      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
853      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
854      {      {
# Line 789  Line 863 
863            instrument_expression(catch->pn_kid1->pn_expr, f);            instrument_expression(catch->pn_kid1->pn_expr, f);
864          }          }
865          Stream_write_string(f, ") {\n");          Stream_write_string(f, ") {\n");
866          instrument_statement(catch->pn_kid3, f, indent + 2);          instrument_statement(catch->pn_kid3, f, indent + 2, false);
867          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
868          Stream_write_string(f, "}\n");          Stream_write_string(f, "}\n");
869        }        }
# Line 797  Line 871 
871      if (node->pn_kid3) {      if (node->pn_kid3) {
872        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
873        Stream_write_string(f, "finally {\n");        Stream_write_string(f, "finally {\n");
874        instrument_statement(node->pn_kid3, f, indent + 2);        instrument_statement(node->pn_kid3, f, indent + 2, false);
875        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
876        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
877      }      }
# Line 823  Line 897 
897      Stream_write_string(f, "with (");      Stream_write_string(f, "with (");
898      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
899      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
900      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
901      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
902      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
903      break;      break;
# Line 861  Line 935 
935      /*      /*
936      ... use output_statement instead of instrument_statement.      ... use output_statement instead of instrument_statement.
937      */      */
938      output_statement(node->pn_expr, f, indent);      output_statement(node->pn_expr, f, indent, false);
939      break;      break;
940    default:    default:
941      fatal("unsupported node type in file %s: %d", file_id, node->pn_type);      fatal("unsupported node type in file %s: %d", file_id, node->pn_type);
# Line 873  Line 947 
947  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
948  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
949  */  */
950  static void instrument_statement(JSParseNode * node, Stream * f, int indent) {  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
951    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC) {
952      uint16_t line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
953      if (line > num_lines) {      if (line > num_lines) {
# Line 887  Line 961 
961        lines[line - 1] = 1;        lines[line - 1] = 1;
962      }      }
963    }    }
964    output_statement(node, f, indent);    output_statement(node, f, indent, is_jscoverage_if);
965  }  }
966    
967  static bool characters_start_with(const jschar * characters, size_t line_start, size_t line_end, const char * prefix) {  static bool characters_start_with(const jschar * characters, size_t line_start, size_t line_end, const char * prefix) {
# Line 909  Line 983 
983    }    }
984  }  }
985    
986    static bool characters_are_white_space(const jschar * characters, size_t line_start, size_t line_end) {
987      /* XXX - other Unicode space */
988      const jschar * end = characters + line_end;
989      for (const jschar * p = characters + line_start; p < end; p++) {
990        jschar c = *p;
991        if (c == 0x9 || c == 0xB || c == 0xC || c == 0x20 || c == 0xA0) {
992          continue;
993        }
994        else {
995          return false;
996        }
997      }
998      return true;
999    }
1000    
1001    static void error_reporter(JSContext * context, const char * message, JSErrorReport * report) {
1002      fprintf(stderr, "jscoverage: parse error: line %u: %s\n", report->lineno, message);
1003    }
1004    
1005  void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) {  void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) {
1006    file_id = id;    file_id = id;
1007    
# Line 919  Line 1012 
1012    }    }
1013    
1014    /* parse the javascript */    /* parse the javascript */
1015      JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);
1016    JSParseNode * node = js_ParseTokenStream(context, global, token_stream);    JSParseNode * node = js_ParseTokenStream(context, global, token_stream);
1017    if (node == NULL) {    if (node == NULL) {
1018        js_ReportUncaughtException(context);
1019      fatal("parse error in file: %s", file_id);      fatal("parse error in file: %s", file_id);
1020    }    }
1021      JS_SetErrorReporter(context, old_error_reporter);
1022    num_lines = node->pn_pos.end.lineno;    num_lines = node->pn_pos.end.lineno;
1023    lines = xmalloc(num_lines);    lines = xmalloc(num_lines);
1024    for (int i = 0; i < num_lines; i++) {    for (unsigned int i = 0; i < num_lines; i++) {
1025      lines[i] = 0;      lines[i] = 0;
1026    }    }
1027    
1028    /*    /* search code for conditionals */
1029    An instrumented JavaScript file has 3 sections:    exclusive_directives = xnew(bool, num_lines);
1030    1. initialization    for (unsigned int i = 0; i < num_lines; i++) {
1031    2. instrumented source code      exclusive_directives[i] = false;
   3. original source code  
   */  
   
   Stream * instrumented = Stream_new(0);  
   instrument_statement(node, instrumented, 0);  
   
   /* write line number info to the output */  
   Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");  
   Stream_write_string(output, "if (! top._$jscoverage) {\n  top._$jscoverage = {};\n}\n");  
   Stream_write_string(output, "var _$jscoverage = top._$jscoverage;\n");  
   Stream_printf(output, "if (! _$jscoverage['%s']) {\n", file_id);  
   Stream_printf(output, "  _$jscoverage['%s'] = [];\n", file_id);  
   for (int i = 0; i < num_lines; i++) {  
     if (lines[i]) {  
       Stream_printf(output, "  _$jscoverage['%s'][%d] = 0;\n", file_id, i + 1);  
     }  
1032    }    }
   Stream_write_string(output, "}\n");  
   free(lines);  
   lines = NULL;  
   
   /* copy the instrumented source code to the output */  
   Stream_write(output, instrumented->data, instrumented->length);  
1033    
   /* conditionals */  
1034    bool has_conditionals = false;    bool has_conditionals = false;
1035      struct IfDirective * if_directives = NULL;
1036    size_t line_number = 0;    size_t line_number = 0;
1037    size_t i = 0;    size_t i = 0;
1038    while (i < num_characters) {    while (i < num_characters) {
1039        if (line_number == UINT16_MAX) {
1040          fatal("%s: script has more than 65,535 lines", file_id);
1041        }
1042      line_number++;      line_number++;
1043      size_t line_start = i;      size_t line_start = i;
1044      jschar c;      jschar c;
# Line 977  Line 1054 
1054          break;          break;
1055        default:        default:
1056          i++;          i++;
         break;  
1057        }        }
1058      }      }
1059      size_t line_end = i;      size_t line_end = i;
# Line 987  Line 1063 
1063          i++;          i++;
1064        }        }
1065      }      }
1066    
1067      if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) {      if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) {
1068        if (! has_conditionals) {        has_conditionals = true;
1069          has_conditionals = true;  
1070          Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);        if (characters_are_white_space(characters, line_start + 16, line_end)) {
1071        }          exclusive_directives[line_number - 1] = true;
1072        Stream_write_string(output, "if (!(");        }
1073        for (size_t j = line_start + 16; j < line_end; j++) {        else {
1074          jschar c = characters[j];          struct IfDirective * if_directive = xnew(struct IfDirective, 1);
1075          if (c == '\t' || (32 <= c && c <= 126)) {          if_directive->condition_start = characters + line_start + 16;
1076            Stream_write_char(output, c);          if_directive->condition_end = characters + line_end;
1077          }          if_directive->start_line = line_number;
1078          else {          if_directive->end_line = 0;
1079            Stream_printf(output, "\\u%04x", c);          if_directive->next = if_directives;
1080          }          if_directives = if_directive;
1081        }        }
       Stream_write_string(output, ")) {\n");  
       Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = ", file_id, line_number);  
1082      }      }
1083      else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) {      else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) {
1084        Stream_printf(output, "%d;\n", line_number);        for (struct IfDirective * p = if_directives; p != NULL; p = p->next) {
1085        Stream_printf(output, "}\n");          if (p->end_line == 0) {
1086              p->end_line = line_number;
1087              break;
1088            }
1089          }
1090      }      }
1091    }    }
1092    
1093      /*
1094      An instrumented JavaScript file has 4 sections:
1095      1. initialization
1096      2. instrumented source code
1097      3. conditionals
1098      4. original source code
1099      */
1100    
1101      Stream * instrumented = Stream_new(0);
1102      instrument_statement(node, instrumented, 0, false);
1103    
1104      /* write line number info to the output */
1105      Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");
1106      Stream_write_string(output, "if (! top._$jscoverage) {\n  top._$jscoverage = {};\n}\n");
1107      Stream_write_string(output, "var _$jscoverage = top._$jscoverage;\n");
1108      Stream_printf(output, "if (! _$jscoverage['%s']) {\n", file_id);
1109      Stream_printf(output, "  _$jscoverage['%s'] = [];\n", file_id);
1110      for (int i = 0; i < num_lines; i++) {
1111        if (lines[i]) {
1112          Stream_printf(output, "  _$jscoverage['%s'][%d] = 0;\n", file_id, i + 1);
1113        }
1114      }
1115      Stream_write_string(output, "}\n");
1116      free(lines);
1117      lines = NULL;
1118      free(exclusive_directives);
1119      exclusive_directives = NULL;
1120    
1121      /* conditionals */
1122      if (has_conditionals) {
1123        Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);
1124      }
1125    
1126      /* copy the instrumented source code to the output */
1127      Stream_write(output, instrumented->data, instrumented->length);
1128    
1129      /* conditionals */
1130      for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) {
1131        Stream_write_string(output, "if (!(");
1132        print_javascript(if_directive->condition_start, if_directive->condition_end - if_directive->condition_start, output);
1133        Stream_write_string(output, ")) {\n");
1134        Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line);
1135        Stream_write_string(output, "}\n");
1136      }
1137    
1138      /* free */
1139      while (if_directives != NULL) {
1140        struct IfDirective * if_directive = if_directives;
1141        if_directives = if_directives->next;
1142        free(if_directive);
1143      }
1144    
1145    /* copy the original source to the output */    /* copy the original source to the output */
1146    Stream_printf(output, "_$jscoverage['%s'].source = ", file_id);    Stream_printf(output, "_$jscoverage['%s'].source = ", file_id);
1147    jscoverage_write_source(id, characters, num_characters, output);    jscoverage_write_source(id, characters, num_characters, output);
# Line 1049  Line 1180 
1180            /* line feed (new line) */            /* line feed (new line) */
1181            done = true;            done = true;
1182            break;            break;
1183            /* IE doesn't support this */
1184            /*
1185          case 0xb:          case 0xb:
           /* vertical tab */  
1186            Stream_write_string(output, "\\v");            Stream_write_string(output, "\\v");
1187            break;            break;
1188            */
1189          case 0xc:          case 0xc:
1190            /* form feed */            /* form feed */
1191            Stream_write_string(output, "\\f");            Stream_write_string(output, "\\f");
# Line 1107  Line 1240 
1240            /* line feed (new line) */            /* line feed (new line) */
1241            done = true;            done = true;
1242            break;            break;
1243            /* IE doesn't support this */
1244            /*
1245          case 0xb:          case 0xb:
           /* vertical tab */  
1246            Stream_write_string(output, "\\v");            Stream_write_string(output, "\\v");
1247            break;            break;
1248            */
1249          case 0xc:          case 0xc:
1250            /* form feed */            /* form feed */
1251            Stream_write_string(output, "\\f");            Stream_write_string(output, "\\f");
# Line 1356  Line 1491 
1491        file_coverage->id = id;        file_coverage->id = id;
1492        file_coverage->num_coverage_lines = array->pn_count;        file_coverage->num_coverage_lines = array->pn_count;
1493        file_coverage->coverage_lines = xnew(int, array->pn_count);        file_coverage->coverage_lines = xnew(int, array->pn_count);
1494        if (source == NULL) {        file_coverage->source_lines = NULL;
         file_coverage->source_lines = NULL;  
       }  
       else {  
         file_coverage->num_source_lines = source->pn_count;  
         file_coverage->source_lines = xnew(char *, source->pn_count);  
         uint32 i = 0;  
         for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {  
           if (element->pn_type != TOK_STRING) {  
             return -1;  
           }  
           file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));  
         }  
         assert(i == source->pn_count);  
       }  
1495    
1496        /* set coverage for all lines */        /* set coverage for all lines */
1497        uint32 i = 0;        uint32 i = 0;
# Line 1420  Line 1541 
1541          }          }
1542        }        }
1543        assert(i == array->pn_count);        assert(i == array->pn_count);
1544        }
1545    
1546        /* if this JSON file has source, use it */      /* if this JSON file has source, use it */
1547        if (file_coverage->source_lines == NULL && source != NULL) {      if (file_coverage->source_lines == NULL && source != NULL) {
1548          file_coverage->num_source_lines = source->pn_count;        file_coverage->num_source_lines = source->pn_count;
1549          file_coverage->source_lines = xnew(char *, source->pn_count);        file_coverage->source_lines = xnew(char *, source->pn_count);
1550          uint32 i = 0;        uint32 i = 0;
1551          for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {        for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {
1552            if (element->pn_type != TOK_STRING) {          if (element->pn_type != TOK_STRING) {
1553              return -1;            return -1;
           }  
           file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));  
1554          }          }
1555          assert(i == source->pn_count);          file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));
1556        }        }
1557          assert(i == source->pn_count);
1558      }      }
1559    }    }
1560    

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

  ViewVC Help
Powered by ViewVC 1.1.24