/[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 190 by siliconforks, Tue Sep 23 03:49:01 2008 UTC revision 246 by siliconforks, Sun Oct 5 18:05:42 2008 UTC
# Line 40  Line 40 
40  #include "resource-manager.h"  #include "resource-manager.h"
41  #include "util.h"  #include "util.h"
42    
43    struct IfDirective {
44      const jschar * condition_start;
45      const jschar * condition_end;
46      uint16_t start_line;
47      uint16_t end_line;
48      struct IfDirective * next;
49    };
50    
51    static bool * exclusive_directives = NULL;
52    
53  static JSRuntime * runtime = NULL;  static JSRuntime * runtime = NULL;
54  static JSContext * context = NULL;  static JSContext * context = NULL;
55  static JSObject * global = NULL;  static JSObject * global = NULL;
# Line 50  Line 60 
60  */  */
61  static const char * file_id = NULL;  static const char * file_id = NULL;
62  static char * lines = NULL;  static char * lines = NULL;
63    static uint16_t num_lines = 0;
64    
65  void jscoverage_init(void) {  void jscoverage_init(void) {
66    runtime = JS_NewRuntime(8L * 1024L * 1024L);    runtime = JS_NewRuntime(8L * 1024L * 1024L);
# Line 77  Line 88 
88    JS_DestroyRuntime(runtime);    JS_DestroyRuntime(runtime);
89  }  }
90    
91    static void print_javascript(const jschar * characters, size_t num_characters, Stream * f) {
92      for (size_t i = 0; i < num_characters; i++) {
93        jschar c = characters[i];
94        /*
95        XXX does not handle no-break space, other unicode "space separator"
96        */
97        switch (c) {
98        case 0x9:
99        case 0xB:
100        case 0xC:
101          Stream_write_char(f, c);
102          break;
103        default:
104          if (32 <= c && c <= 126) {
105            Stream_write_char(f, c);
106          }
107          else {
108            Stream_printf(f, "\\u%04x", c);
109          }
110          break;
111        }
112      }
113    }
114    
115  static void print_string(JSString * s, Stream * f) {  static void print_string(JSString * s, Stream * f) {
116    size_t length = JSSTRING_LENGTH(s);    size_t length = JSSTRING_LENGTH(s);
117    jschar * characters = JSSTRING_CHARS(s);    jschar * characters = JSSTRING_CHARS(s);
# Line 204  Line 239 
239  }  }
240    
241  static void instrument_expression(JSParseNode * node, Stream * f);  static void instrument_expression(JSParseNode * node, Stream * f);
242  static void instrument_statement(JSParseNode * node, Stream * f, int indent);  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
243    
244  enum FunctionType {  enum FunctionType {
245    FUNCTION_NORMAL,    FUNCTION_NORMAL,
# Line 260  Line 295 
295    free(params);    free(params);
296    
297    /* function body */    /* function body */
298    instrument_statement(node->pn_body, f, indent + 2);    instrument_statement(node->pn_body, f, indent + 2, false);
299    
300    Stream_write_string(f, "}\n");    Stream_write_string(f, "}\n");
301  }  }
# Line 619  Line 654 
654    }    }
655  }  }
656    
657  static void output_statement(JSParseNode * node, Stream * f, int indent) {  static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
658    switch (node->pn_type) {    switch (node->pn_type) {
659    case TOK_FUNCTION:    case TOK_FUNCTION:
660      instrument_function(node, f, indent, FUNCTION_NORMAL);      instrument_function(node, f, indent, FUNCTION_NORMAL);
# Line 630  Line 665 
665      Stream_write_string(f, "{\n");      Stream_write_string(f, "{\n");
666  */  */
667      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) {
668        instrument_statement(p, f, indent);        instrument_statement(p, f, indent, false);
669      }      }
670  /*  /*
671      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 638  Line 673 
673  */  */
674      break;      break;
675    case TOK_IF:    case TOK_IF:
676      {
677      assert(node->pn_arity == PN_TERNARY);      assert(node->pn_arity == PN_TERNARY);
678    
679        uint16_t line = node->pn_pos.begin.lineno;
680        if (! is_jscoverage_if) {
681          if (line > num_lines) {
682            fatal("%s: script contains more than 65,535 lines", file_id);
683          }
684          if (line >= 2 && exclusive_directives[line - 2]) {
685            is_jscoverage_if = true;
686          }
687        }
688    
689      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
690      Stream_write_string(f, "if (");      Stream_write_string(f, "if (");
691      instrument_expression(node->pn_kid1, f);      instrument_expression(node->pn_kid1, f);
692      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
693      instrument_statement(node->pn_kid2, f, indent + 2);      if (is_jscoverage_if && node->pn_kid3) {
694          uint16_t else_start = node->pn_kid3->pn_pos.begin.lineno;
695          uint16_t else_end = node->pn_kid3->pn_pos.end.lineno + 1;
696          Stream_printf(f, "%*s", indent + 2, "");
697          Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, else_start, else_end);
698        }
699        instrument_statement(node->pn_kid2, f, indent + 2, false);
700      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
701      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
702      if (node->pn_kid3) {  
703        if (node->pn_kid3 || is_jscoverage_if) {
704        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
705        Stream_write_string(f, "else {\n");        Stream_write_string(f, "else {\n");
706        instrument_statement(node->pn_kid3, f, indent + 2);  
707          if (is_jscoverage_if) {
708            uint16_t if_start = node->pn_kid2->pn_pos.begin.lineno + 1;
709            uint16_t if_end = node->pn_kid2->pn_pos.end.lineno + 1;
710            Stream_printf(f, "%*s", indent + 2, "");
711            Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_start, if_end);
712          }
713    
714          if (node->pn_kid3) {
715            instrument_statement(node->pn_kid3, f, indent + 2, is_jscoverage_if);
716          }
717    
718        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
719        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
720      }      }
721    
722      break;      break;
723      }
724    case TOK_SWITCH:    case TOK_SWITCH:
725      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
726      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 675  Line 742 
742          abort();          abort();
743          break;          break;
744        }        }
745        instrument_statement(p->pn_right, f, indent + 2);        instrument_statement(p->pn_right, f, indent + 2, false);
746      }      }
747      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
748      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
# Line 690  Line 757 
757      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
758      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
759      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
760      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
761      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
762      break;      break;
763    case TOK_DO:    case TOK_DO:
764      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
765      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
766      Stream_write_string(f, "do {\n");      Stream_write_string(f, "do {\n");
767      instrument_statement(node->pn_left, f, indent + 2);      instrument_statement(node->pn_left, f, indent + 2, false);
768      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
769      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
770      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
# Line 760  Line 827 
827        break;        break;
828      }      }
829      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
830      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
831      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
832      break;      break;
833    case TOK_THROW:    case TOK_THROW:
# Line 773  Line 840 
840    case TOK_TRY:    case TOK_TRY:
841      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
842      Stream_write_string(f, "try {\n");      Stream_write_string(f, "try {\n");
843      instrument_statement(node->pn_kid1, f, indent + 2);      instrument_statement(node->pn_kid1, f, indent + 2, false);
844      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
845      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
846      {      {
# Line 788  Line 855 
855            instrument_expression(catch->pn_kid1->pn_expr, f);            instrument_expression(catch->pn_kid1->pn_expr, f);
856          }          }
857          Stream_write_string(f, ") {\n");          Stream_write_string(f, ") {\n");
858          instrument_statement(catch->pn_kid3, f, indent + 2);          instrument_statement(catch->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 796  Line 863 
863      if (node->pn_kid3) {      if (node->pn_kid3) {
864        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
865        Stream_write_string(f, "finally {\n");        Stream_write_string(f, "finally {\n");
866        instrument_statement(node->pn_kid3, f, indent + 2);        instrument_statement(node->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 822  Line 889 
889      Stream_write_string(f, "with (");      Stream_write_string(f, "with (");
890      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
891      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
892      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
893      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
894      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
895      break;      break;
# Line 860  Line 927 
927      /*      /*
928      ... use output_statement instead of instrument_statement.      ... use output_statement instead of instrument_statement.
929      */      */
930      output_statement(node->pn_expr, f, indent);      output_statement(node->pn_expr, f, indent, false);
931      break;      break;
932    default:    default:
933      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 872  Line 939 
939  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
940  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
941  */  */
942  static void instrument_statement(JSParseNode * node, Stream * f, int indent) {  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
943    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC) {
944      int line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
945        if (line > num_lines) {
946          fatal("%s: script contains more than 65,535 lines", file_id);
947        }
948    
949      /* the root node has line number 0 */      /* the root node has line number 0 */
950      if (line != 0) {      if (line != 0) {
951        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
# Line 882  Line 953 
953        lines[line - 1] = 1;        lines[line - 1] = 1;
954      }      }
955    }    }
956    output_statement(node, f, indent);    output_statement(node, f, indent, is_jscoverage_if);
957  }  }
958    
959  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 904  Line 975 
975    }    }
976  }  }
977    
978    static bool characters_are_white_space(const jschar * characters, size_t line_start, size_t line_end) {
979      /* XXX - other Unicode space */
980      const jschar * end = characters + line_end;
981      for (const jschar * p = characters + line_start; p < end; p++) {
982        jschar c = *p;
983        if (c == 0x9 || c == 0xB || c == 0xC || c == 0x20 || c == 0xA0) {
984          continue;
985        }
986        else {
987          return false;
988        }
989      }
990      return true;
991    }
992    
993  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) {
994    file_id = id;    file_id = id;
995    
# Line 918  Line 1004 
1004    if (node == NULL) {    if (node == NULL) {
1005      fatal("parse error in file: %s", file_id);      fatal("parse error in file: %s", file_id);
1006    }    }
1007    int num_lines = node->pn_pos.end.lineno;    num_lines = node->pn_pos.end.lineno;
1008    lines = xmalloc(num_lines);    lines = xmalloc(num_lines);
1009    for (int i = 0; i < num_lines; i++) {    for (unsigned int i = 0; i < num_lines; i++) {
1010      lines[i] = 0;      lines[i] = 0;
1011    }    }
1012    
1013    /*    /* search code for conditionals */
1014    An instrumented JavaScript file has 3 sections:    exclusive_directives = xnew(bool, num_lines);
1015    1. initialization    for (unsigned int i = 0; i < num_lines; i++) {
1016    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);  
     }  
1017    }    }
   Stream_write_string(output, "}\n");  
   free(lines);  
   lines = NULL;  
   
   /* copy the instrumented source code to the output */  
   Stream_write(output, instrumented->data, instrumented->length);  
1018    
   /* conditionals */  
1019    bool has_conditionals = false;    bool has_conditionals = false;
1020      struct IfDirective * if_directives = NULL;
1021    size_t line_number = 0;    size_t line_number = 0;
1022    size_t i = 0;    size_t i = 0;
1023    while (i < num_characters) {    while (i < num_characters) {
1024        if (line_number == UINT16_MAX) {
1025          fatal("%s: script has more than 65,535 lines", file_id);
1026        }
1027      line_number++;      line_number++;
1028      size_t line_start = i;      size_t line_start = i;
1029      jschar c;      jschar c;
# Line 972  Line 1039 
1039          break;          break;
1040        default:        default:
1041          i++;          i++;
         break;  
1042        }        }
1043      }      }
1044      size_t line_end = i;      size_t line_end = i;
# Line 982  Line 1048 
1048          i++;          i++;
1049        }        }
1050      }      }
1051    
1052      if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) {      if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) {
1053        if (! has_conditionals) {        has_conditionals = true;
1054          has_conditionals = true;  
1055          Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);        if (characters_are_white_space(characters, line_start + 16, line_end)) {
1056        }          exclusive_directives[line_number - 1] = true;
1057        Stream_write_string(output, "if (!(");        }
1058        for (size_t j = line_start + 16; j < line_end; j++) {        else {
1059          jschar c = characters[j];          struct IfDirective * if_directive = xnew(struct IfDirective, 1);
1060          if (c == '\t' || (32 <= c && c <= 126)) {          if_directive->condition_start = characters + line_start + 16;
1061            Stream_write_char(output, c);          if_directive->condition_end = characters + line_end;
1062          }          if_directive->start_line = line_number;
1063          else {          if_directive->end_line = 0;
1064            Stream_printf(output, "\\u%04x", c);          if_directive->next = if_directives;
1065          }          if_directives = if_directive;
1066        }        }
       Stream_write_string(output, ")) {\n");  
       Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = ", file_id, line_number);  
1067      }      }
1068      else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) {      else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) {
1069        Stream_printf(output, "%d;\n", line_number);        for (struct IfDirective * p = if_directives; p != NULL; p = p->next) {
1070        Stream_printf(output, "}\n");          if (p->end_line == 0) {
1071              p->end_line = line_number;
1072              break;
1073            }
1074          }
1075      }      }
1076    }    }
1077    
1078      /*
1079      An instrumented JavaScript file has 4 sections:
1080      1. initialization
1081      2. instrumented source code
1082      3. conditionals
1083      4. original source code
1084      */
1085    
1086      Stream * instrumented = Stream_new(0);
1087      instrument_statement(node, instrumented, 0, false);
1088    
1089      /* write line number info to the output */
1090      Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");
1091      Stream_write_string(output, "if (! top._$jscoverage) {\n  top._$jscoverage = {};\n}\n");
1092      Stream_write_string(output, "var _$jscoverage = top._$jscoverage;\n");
1093      Stream_printf(output, "if (! _$jscoverage['%s']) {\n", file_id);
1094      Stream_printf(output, "  _$jscoverage['%s'] = [];\n", file_id);
1095      for (int i = 0; i < num_lines; i++) {
1096        if (lines[i]) {
1097          Stream_printf(output, "  _$jscoverage['%s'][%d] = 0;\n", file_id, i + 1);
1098        }
1099      }
1100      Stream_write_string(output, "}\n");
1101      free(lines);
1102      lines = NULL;
1103      free(exclusive_directives);
1104      exclusive_directives = NULL;
1105    
1106      /* conditionals */
1107      if (has_conditionals) {
1108        Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);
1109      }
1110    
1111      /* copy the instrumented source code to the output */
1112      Stream_write(output, instrumented->data, instrumented->length);
1113    
1114      /* conditionals */
1115      for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) {
1116        Stream_write_string(output, "if (!(");
1117        print_javascript(if_directive->condition_start, if_directive->condition_end - if_directive->condition_start, output);
1118        Stream_write_string(output, ")) {\n");
1119        Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line);
1120        Stream_write_string(output, "}\n");
1121      }
1122    
1123      /* free */
1124      while (if_directives != NULL) {
1125        struct IfDirective * if_directive = if_directives;
1126        if_directives = if_directives->next;
1127        free(if_directive);
1128      }
1129    
1130    /* copy the original source to the output */    /* copy the original source to the output */
1131    Stream_printf(output, "_$jscoverage['%s'].source = ", file_id);    Stream_printf(output, "_$jscoverage['%s'].source = ", file_id);
1132    jscoverage_write_source(id, characters, num_characters, output);    jscoverage_write_source(id, characters, num_characters, output);
# Line 1351  Line 1472 
1472        file_coverage->id = id;        file_coverage->id = id;
1473        file_coverage->num_coverage_lines = array->pn_count;        file_coverage->num_coverage_lines = array->pn_count;
1474        file_coverage->coverage_lines = xnew(int, array->pn_count);        file_coverage->coverage_lines = xnew(int, array->pn_count);
1475        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);  
       }  
1476    
1477        /* set coverage for all lines */        /* set coverage for all lines */
1478        uint32 i = 0;        uint32 i = 0;
# Line 1415  Line 1522 
1522          }          }
1523        }        }
1524        assert(i == array->pn_count);        assert(i == array->pn_count);
1525        }
1526    
1527        /* if this JSON file has source, use it */      /* if this JSON file has source, use it */
1528        if (file_coverage->source_lines == NULL && source != NULL) {      if (file_coverage->source_lines == NULL && source != NULL) {
1529          file_coverage->num_source_lines = source->pn_count;        file_coverage->num_source_lines = source->pn_count;
1530          file_coverage->source_lines = xnew(char *, source->pn_count);        file_coverage->source_lines = xnew(char *, source->pn_count);
1531          uint32 i = 0;        uint32 i = 0;
1532          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++) {
1533            if (element->pn_type != TOK_STRING) {          if (element->pn_type != TOK_STRING) {
1534              return -1;            return -1;
           }  
           file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));  
1535          }          }
1536          assert(i == source->pn_count);          file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));
1537        }        }
1538          assert(i == source->pn_count);
1539      }      }
1540    }    }
1541    

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

  ViewVC Help
Powered by ViewVC 1.1.24