/[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 179 by siliconforks, Sun Sep 21 18:35:21 2008 UTC revision 214 by siliconforks, Fri Oct 3 02:26:04 2008 UTC
# Line 50  Line 50 
50  */  */
51  static const char * file_id = NULL;  static const char * file_id = NULL;
52  static char * lines = NULL;  static char * lines = NULL;
53    static uint16_t num_lines = 0;
54    
55  void jscoverage_init(void) {  void jscoverage_init(void) {
56    runtime = JS_NewRuntime(8L * 1024L * 1024L);    runtime = JS_NewRuntime(8L * 1024L * 1024L);
# Line 874  Line 875 
875  */  */
876  static void instrument_statement(JSParseNode * node, Stream * f, int indent) {  static void instrument_statement(JSParseNode * node, Stream * f, int indent) {
877    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC) {
878      int line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
879        if (line > num_lines) {
880          fatal("%s: script contains more than 65,535 lines", file_id);
881        }
882    
883      /* the root node has line number 0 */      /* the root node has line number 0 */
884      if (line != 0) {      if (line != 0) {
885        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
# Line 885  Line 890 
890    output_statement(node, f, indent);    output_statement(node, f, indent);
891  }  }
892    
893    static bool characters_start_with(const jschar * characters, size_t line_start, size_t line_end, const char * prefix) {
894      const jschar * characters_end = characters + line_end;
895      const jschar * cp = characters + line_start;
896      const char * bp = prefix;
897      for (;;) {
898        if (*bp == '\0') {
899          return true;
900        }
901        else if (cp == characters_end) {
902          return false;
903        }
904        else if (*cp != *bp) {
905          return false;
906        }
907        bp++;
908        cp++;
909      }
910    }
911    
912  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) {
913    file_id = id;    file_id = id;
914    
# Line 899  Line 923 
923    if (node == NULL) {    if (node == NULL) {
924      fatal("parse error in file: %s", file_id);      fatal("parse error in file: %s", file_id);
925    }    }
926    int num_lines = node->pn_pos.end.lineno;    num_lines = node->pn_pos.end.lineno;
927    lines = xmalloc(num_lines);    lines = xmalloc(num_lines);
928    for (int i = 0; i < num_lines; i++) {    for (int i = 0; i < num_lines; i++) {
929      lines[i] = 0;      lines[i] = 0;
# Line 940  Line 964 
964    while (i < num_characters) {    while (i < num_characters) {
965      line_number++;      line_number++;
966      size_t line_start = i;      size_t line_start = i;
967      /* FIXME */      jschar c;
968      while (i < num_characters && characters[i] != '\r' && characters[i] != '\n') {      bool done = false;
969        i++;      while (! done && i < num_characters) {
970          c = characters[i];
971          switch (c) {
972          case '\r':
973          case '\n':
974          case 0x2028:
975          case 0x2029:
976            done = true;
977            break;
978          default:
979            i++;
980            break;
981          }
982      }      }
983      size_t line_end = i;      size_t line_end = i;
984      if (i < num_characters) {      if (i < num_characters) {
985        if (characters[i] == '\r') {        i++;
986          line_end = i;        if (c == '\r' && i < num_characters && characters[i] == '\n') {
         i++;  
         if (i < num_characters && characters[i] == '\n') {  
           i++;  
         }  
       }  
       else if (characters[i] == '\n') {  
         line_end = i;  
987          i++;          i++;
988        }        }
       else {  
         abort();  
       }  
989      }      }
990      char * line = js_DeflateString(context, characters + line_start, line_end - line_start);      if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) {
     if (str_starts_with(line, "//#JSCOVERAGE_IF")) {  
991        if (! has_conditionals) {        if (! has_conditionals) {
992          has_conditionals = true;          has_conditionals = true;
993          Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);          Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);
994        }        }
995        Stream_printf(output, "if (!%s) {\n", line + 16);        Stream_write_string(output, "if (!(");
996          for (size_t j = line_start + 16; j < line_end; j++) {
997            jschar c = characters[j];
998            if (c == '\t' || (32 <= c && c <= 126)) {
999              Stream_write_char(output, c);
1000            }
1001            else {
1002              Stream_printf(output, "\\u%04x", c);
1003            }
1004          }
1005          Stream_write_string(output, ")) {\n");
1006        Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = ", file_id, line_number);        Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = ", file_id, line_number);
1007      }      }
1008      else if (str_starts_with(line, "//#JSCOVERAGE_ENDIF")) {      else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) {
1009        Stream_printf(output, "%d;\n", line_number);        Stream_printf(output, "%d;\n", line_number);
1010        Stream_printf(output, "}\n");        Stream_printf(output, "}\n");
1011      }      }
     JS_free(context, line);  
1012    }    }
1013    
1014    /* copy the original source to the output */    /* copy the original source to the output */

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

  ViewVC Help
Powered by ViewVC 1.1.24