/[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 176 by siliconforks, Sat Sep 20 23:29:42 2008 UTC revision 190 by siliconforks, Tue Sep 23 03:49:01 2008 UTC
# Line 35  Line 35 
35  #include <jsstr.h>  #include <jsstr.h>
36    
37  #include "encoding.h"  #include "encoding.h"
38    #include "global.h"
39    #include "highlight.h"
40  #include "resource-manager.h"  #include "resource-manager.h"
41  #include "util.h"  #include "util.h"
42    
# Line 883  Line 885 
885    output_statement(node, f, indent);    output_statement(node, f, indent);
886  }  }
887    
888  void jscoverage_instrument_js(const char * id, const char * encoding, Stream * input, Stream * output) {  static bool characters_start_with(const jschar * characters, size_t line_start, size_t line_end, const char * prefix) {
889      const jschar * characters_end = characters + line_end;
890      const jschar * cp = characters + line_start;
891      const char * bp = prefix;
892      for (;;) {
893        if (*bp == '\0') {
894          return true;
895        }
896        else if (cp == characters_end) {
897          return false;
898        }
899        else if (*cp != *bp) {
900          return false;
901        }
902        bp++;
903        cp++;
904      }
905    }
906    
907    void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) {
908    file_id = id;    file_id = id;
909    
910    /* scan the javascript */    /* scan the javascript */
911    size_t num_characters = input->length;    JSTokenStream * token_stream = js_NewTokenStream(context, characters, num_characters, NULL, 1, NULL);
   jschar * base = NULL;  
   int result = jscoverage_bytes_to_characters(encoding, input->data, input->length, &base, &num_characters);  
   if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) {  
     fatal("encoding %s not supported in file %s", encoding, id);  
   }  
   else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) {  
     fatal("error decoding %s in file %s", encoding, id);  
   }  
   JSTokenStream * token_stream = js_NewTokenStream(context, base, num_characters, NULL, 1, NULL);  
912    if (token_stream == NULL) {    if (token_stream == NULL) {
913      fatal("cannot create token stream from file: %s", file_id);      fatal("cannot create token stream from file: %s", file_id);
914    }    }
# Line 939  Line 951 
951    
952    /* copy the instrumented source code to the output */    /* copy the instrumented source code to the output */
953    Stream_write(output, instrumented->data, instrumented->length);    Stream_write(output, instrumented->data, instrumented->length);
   Stream_write_char(output, '\n');  
954    
955    /* conditionals */    /* conditionals */
956    bool has_conditionals = false;    bool has_conditionals = false;
# Line 948  Line 959 
959    while (i < num_characters) {    while (i < num_characters) {
960      line_number++;      line_number++;
961      size_t line_start = i;      size_t line_start = i;
962      while (i < num_characters && base[i] != '\r' && base[i] != '\n') {      jschar c;
963        i++;      bool done = false;
964        while (! done && i < num_characters) {
965          c = characters[i];
966          switch (c) {
967          case '\r':
968          case '\n':
969          case 0x2028:
970          case 0x2029:
971            done = true;
972            break;
973          default:
974            i++;
975            break;
976          }
977      }      }
978      size_t line_end = i;      size_t line_end = i;
979      if (i < num_characters) {      if (i < num_characters) {
980        if (base[i] == '\r') {        i++;
981          line_end = i;        if (c == '\r' && i < num_characters && characters[i] == '\n') {
         i++;  
         if (i < num_characters && base[i] == '\n') {  
           i++;  
         }  
       }  
       else if (base[i] == '\n') {  
         line_end = i;  
982          i++;          i++;
983        }        }
       else {  
         abort();  
       }  
984      }      }
985      char * line = js_DeflateString(context, base + line_start, line_end - line_start);      if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) {
     if (str_starts_with(line, "//#JSCOVERAGE_IF")) {  
986        if (! has_conditionals) {        if (! has_conditionals) {
987          has_conditionals = true;          has_conditionals = true;
988          Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);          Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);
989        }        }
990        Stream_printf(output, "if (!%s) {\n", line + 16);        Stream_write_string(output, "if (!(");
991          for (size_t j = line_start + 16; j < line_end; j++) {
992            jschar c = characters[j];
993            if (c == '\t' || (32 <= c && c <= 126)) {
994              Stream_write_char(output, c);
995            }
996            else {
997              Stream_printf(output, "\\u%04x", c);
998            }
999          }
1000          Stream_write_string(output, ")) {\n");
1001        Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = ", file_id, line_number);        Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = ", file_id, line_number);
1002      }      }
1003      else if (str_starts_with(line, "//#JSCOVERAGE_ENDIF")) {      else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) {
1004        Stream_printf(output, "%d;\n", line_number);        Stream_printf(output, "%d;\n", line_number);
1005        Stream_printf(output, "}\n");        Stream_printf(output, "}\n");
1006      }      }
     JS_free(context, line);  
1007    }    }
1008    
1009    /* copy the original source to the output */    /* copy the original source to the output */
1010    i = 0;    Stream_printf(output, "_$jscoverage['%s'].source = ", file_id);
1011    while (i < num_characters) {    jscoverage_write_source(id, characters, num_characters, output);
1012      Stream_write_string(output, "// ");    Stream_printf(output, ";\n");
     size_t line_start = i;  
     while (i < num_characters && base[i] != '\r' && base[i] != '\n') {  
       i++;  
     }  
1013    
1014      size_t line_end = i;    Stream_delete(instrumented);
1015      if (i < num_characters) {  
1016        if (base[i] == '\r') {    file_id = NULL;
1017          line_end = i;  }
1018    
1019    void jscoverage_write_source(const char * id, const jschar * characters, size_t num_characters, Stream * output) {
1020      Stream_write_string(output, "[");
1021      if (jscoverage_highlight) {
1022        Stream * highlighted_stream = Stream_new(num_characters);
1023        jscoverage_highlight_js(context, id, characters, num_characters, highlighted_stream);
1024        size_t i = 0;
1025        while (i < highlighted_stream->length) {
1026          if (i > 0) {
1027            Stream_write_char(output, ',');
1028          }
1029    
1030          Stream_write_char(output, '"');
1031          bool done = false;
1032          while (! done) {
1033            char c = highlighted_stream->data[i];
1034            switch (c) {
1035            case 0x8:
1036              /* backspace */
1037              Stream_write_string(output, "\\b");
1038              break;
1039            case 0x9:
1040              /* horizontal tab */
1041              Stream_write_string(output, "\\t");
1042              break;
1043            case 0xa:
1044              /* line feed (new line) */
1045              done = true;
1046              break;
1047            case 0xb:
1048              /* vertical tab */
1049              Stream_write_string(output, "\\v");
1050              break;
1051            case 0xc:
1052              /* form feed */
1053              Stream_write_string(output, "\\f");
1054              break;
1055            case 0xd:
1056              /* carriage return */
1057              done = true;
1058              if (i + 1 < highlighted_stream->length && highlighted_stream->data[i + 1] == '\n') {
1059                i++;
1060              }
1061              break;
1062            case '"':
1063              Stream_write_string(output, "\\\"");
1064              break;
1065            case '\\':
1066              Stream_write_string(output, "\\\\");
1067              break;
1068            default:
1069              Stream_write_char(output, c);
1070              break;
1071            }
1072          i++;          i++;
1073          if (i < num_characters && base[i] == '\n') {          if (i >= highlighted_stream->length) {
1074            i++;            done = true;
1075          }          }
1076        }        }
1077        else if (base[i] == '\n') {        Stream_write_char(output, '"');
1078          line_end = i;      }
1079        Stream_delete(highlighted_stream);
1080      }
1081      else {
1082        size_t i = 0;
1083        while (i < num_characters) {
1084          if (i > 0) {
1085            Stream_write_char(output, ',');
1086          }
1087    
1088          Stream_write_char(output, '"');
1089          bool done = false;
1090          while (! done) {
1091            jschar c = characters[i];
1092            switch (c) {
1093            case 0x8:
1094              /* backspace */
1095              Stream_write_string(output, "\\b");
1096              break;
1097            case 0x9:
1098              /* horizontal tab */
1099              Stream_write_string(output, "\\t");
1100              break;
1101            case 0xa:
1102              /* line feed (new line) */
1103              done = true;
1104              break;
1105            case 0xb:
1106              /* vertical tab */
1107              Stream_write_string(output, "\\v");
1108              break;
1109            case 0xc:
1110              /* form feed */
1111              Stream_write_string(output, "\\f");
1112              break;
1113            case 0xd:
1114              /* carriage return */
1115              done = true;
1116              if (i + 1 < num_characters && characters[i + 1] == '\n') {
1117                i++;
1118              }
1119              break;
1120            case '"':
1121              Stream_write_string(output, "\\\"");
1122              break;
1123            case '\\':
1124              Stream_write_string(output, "\\\\");
1125              break;
1126            case '&':
1127              Stream_write_string(output, "&amp;");
1128              break;
1129            case '<':
1130              Stream_write_string(output, "&lt;");
1131              break;
1132            case '>':
1133              Stream_write_string(output, "&gt;");
1134              break;
1135            case 0x2028:
1136            case 0x2029:
1137              done = true;
1138              break;
1139            default:
1140              if (32 <= c && c <= 126) {
1141                Stream_write_char(output, c);
1142              }
1143              else {
1144                Stream_printf(output, "&#%d;", c);
1145              }
1146              break;
1147            }
1148          i++;          i++;
1149            if (i >= num_characters) {
1150              done = true;
1151            }
1152        }        }
1153        else {        Stream_write_char(output, '"');
         abort();  
       }  
1154      }      }
   
     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);  
1155    }    }
1156      Stream_write_string(output, "]");
   Stream_delete(instrumented);  
   
   JS_free(context, base);  
   
   file_id = NULL;  
1157  }  }
1158    
1159  void jscoverage_copy_resources(const char * destination_directory) {  void jscoverage_copy_resources(const char * destination_directory) {
# Line 1030  Line 1162 
1162    copy_resource("jscoverage.js", destination_directory);    copy_resource("jscoverage.js", destination_directory);
1163    copy_resource("jscoverage-ie.css", destination_directory);    copy_resource("jscoverage-ie.css", destination_directory);
1164    copy_resource("jscoverage-throbber.gif", destination_directory);    copy_resource("jscoverage-throbber.gif", destination_directory);
1165    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);  
1166  }  }
1167    
1168  /*  /*
# Line 1067  Line 1197 
1197    JS_HashTableDestroy(coverage->coverage_table);    JS_HashTableDestroy(coverage->coverage_table);
1198    struct FileCoverageList * p = coverage->coverage_list;    struct FileCoverageList * p = coverage->coverage_list;
1199    while (p != NULL) {    while (p != NULL) {
1200      free(p->file_coverage->lines);      free(p->file_coverage->coverage_lines);
1201      free(p->file_coverage->source);      if (p->file_coverage->source_lines != NULL) {
1202          for (uint32 i = 0; i < p->file_coverage->num_source_lines; i++) {
1203            free(p->file_coverage->source_lines[i]);
1204          }
1205          free(p->file_coverage->source_lines);
1206        }
1207      free(p->file_coverage->id);      free(p->file_coverage->id);
1208      free(p->file_coverage);      free(p->file_coverage);
1209      struct FileCoverageList * q = p;      struct FileCoverageList * q = p;
# Line 1190  Line 1325 
1325          }          }
1326          else if (strcmp(s, "source") == 0) {          else if (strcmp(s, "source") == 0) {
1327            source = element->pn_right;            source = element->pn_right;
1328            if (source->pn_type != TOK_STRING || ! ATOM_IS_STRING(source->pn_atom)) {            if (source->pn_type != TOK_RB) {
1329              return -1;              return -1;
1330            }            }
1331          }          }
# Line 1214  Line 1349 
1349        char * id = xstrdup(id_bytes);        char * id = xstrdup(id_bytes);
1350        file_coverage = xmalloc(sizeof(FileCoverage));        file_coverage = xmalloc(sizeof(FileCoverage));
1351        file_coverage->id = id;        file_coverage->id = id;
1352        file_coverage->num_lines = array->pn_count - 1;        file_coverage->num_coverage_lines = array->pn_count;
1353        file_coverage->lines = xnew(int, array->pn_count);        file_coverage->coverage_lines = xnew(int, array->pn_count);
1354        if (source == NULL) {        if (source == NULL) {
1355          file_coverage->source = NULL;          file_coverage->source_lines = NULL;
1356        }        }
1357        else {        else {
1358          file_coverage->source = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(source->pn_atom)));          file_coverage->num_source_lines = source->pn_count;
1359            file_coverage->source_lines = xnew(char *, source->pn_count);
1360            uint32 i = 0;
1361            for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {
1362              if (element->pn_type != TOK_STRING) {
1363                return -1;
1364              }
1365              file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));
1366            }
1367            assert(i == source->pn_count);
1368        }        }
1369    
1370        /* set coverage for all lines */        /* set coverage for all lines */
1371        uint32 i = 0;        uint32 i = 0;
1372        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++) {
1373          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1374            file_coverage->lines[i] = (int) element->pn_dval;            file_coverage->coverage_lines[i] = (int) element->pn_dval;
1375          }          }
1376          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1377            file_coverage->lines[i] = -1;            file_coverage->coverage_lines[i] = -1;
1378          }          }
1379          else {          else {
1380            return -1;            return -1;
# Line 1248  Line 1392 
1392      else {      else {
1393        /* sanity check */        /* sanity check */
1394        assert(strcmp(file_coverage->id, id_bytes) == 0);        assert(strcmp(file_coverage->id, id_bytes) == 0);
1395        if (file_coverage->num_lines != array->pn_count - 1) {        if (file_coverage->num_coverage_lines != array->pn_count) {
1396          return -2;          return -2;
1397        }        }
1398    
# Line 1256  Line 1400 
1400        uint32 i = 0;        uint32 i = 0;
1401        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++) {
1402          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1403            if (file_coverage->lines[i] == -1) {            if (file_coverage->coverage_lines[i] == -1) {
1404              return -2;              return -2;
1405            }            }
1406            file_coverage->lines[i] += (int) element->pn_dval;            file_coverage->coverage_lines[i] += (int) element->pn_dval;
1407          }          }
1408          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1409            if (file_coverage->lines[i] != -1) {            if (file_coverage->coverage_lines[i] != -1) {
1410              return -2;              return -2;
1411            }            }
1412          }          }
# Line 1273  Line 1417 
1417        assert(i == array->pn_count);        assert(i == array->pn_count);
1418    
1419        /* if this JSON file has source, use it */        /* if this JSON file has source, use it */
1420        if (file_coverage->source == NULL && source != NULL) {        if (file_coverage->source_lines == NULL && source != NULL) {
1421          file_coverage->source = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(source->pn_atom)));          file_coverage->num_source_lines = source->pn_count;
1422            file_coverage->source_lines = xnew(char *, source->pn_count);
1423            uint32 i = 0;
1424            for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {
1425              if (element->pn_type != TOK_STRING) {
1426                return -1;
1427              }
1428              file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));
1429            }
1430            assert(i == source->pn_count);
1431        }        }
1432      }      }
1433    }    }

Legend:
Removed from v.176  
changed lines
  Added in v.190

  ViewVC Help
Powered by ViewVC 1.1.24