/[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 159 by siliconforks, Sat Sep 13 04:47:17 2008 UTC revision 240 by siliconforks, Fri Oct 3 23:51:32 2008 UTC
# Line 34  Line 34 
34  #include <jsscope.h>  #include <jsscope.h>
35  #include <jsstr.h>  #include <jsstr.h>
36    
37    #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    
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 47  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 75  Line 89 
89  }  }
90    
91  static void print_string(JSString * s, Stream * f) {  static void print_string(JSString * s, Stream * f) {
92    for (size_t i = 0; i < s->length; i++) {    size_t length = JSSTRING_LENGTH(s);
93      char c = s->chars[i];    jschar * characters = JSSTRING_CHARS(s);
94      Stream_write_char(f, c);    for (size_t i = 0; i < length; i++) {
95        jschar c = characters[i];
96        if (32 <= c && c <= 126) {
97          switch (c) {
98          case '"':
99            Stream_write_string(f, "\\\"");
100            break;
101    /*
102          case '\'':
103            Stream_write_string(f, "\\'");
104            break;
105    */
106          case '\\':
107            Stream_write_string(f, "\\\\");
108            break;
109          default:
110            Stream_write_char(f, c);
111            break;
112          }
113        }
114        else {
115          switch (c) {
116          case 0x8:
117            Stream_write_string(f, "\\b");
118            break;
119          case 0x9:
120            Stream_write_string(f, "\\t");
121            break;
122          case 0xa:
123            Stream_write_string(f, "\\n");
124            break;
125          case 0xb:
126            Stream_write_string(f, "\\v");
127            break;
128          case 0xc:
129            Stream_write_string(f, "\\f");
130            break;
131          case 0xd:
132            Stream_write_string(f, "\\r");
133            break;
134          default:
135            Stream_printf(f, "\\u%04x", c);
136            break;
137          }
138        }
139    }    }
140  }  }
141    
# Line 87  Line 145 
145    print_string(s, f);    print_string(s, f);
146  }  }
147    
148  static void print_string_jsval(jsval value, Stream * f) {  static void print_regex(jsval value, Stream * f) {
149    assert(JSVAL_IS_STRING(value));    assert(JSVAL_IS_STRING(value));
150    JSString * s = JSVAL_TO_STRING(value);    JSString * s = JSVAL_TO_STRING(value);
151    print_string(s, f);    size_t length = JSSTRING_LENGTH(s);
152      jschar * characters = JSSTRING_CHARS(s);
153      for (size_t i = 0; i < length; i++) {
154        jschar c = characters[i];
155        if (32 <= c && c <= 126) {
156          Stream_write_char(f, c);
157        }
158        else {
159          Stream_printf(f, "\\u%04x", c);
160        }
161      }
162  }  }
163    
164  static void print_quoted_string_atom(JSAtom * atom, Stream * f) {  static void print_quoted_string_atom(JSAtom * atom, Stream * f) {
165    assert(ATOM_IS_STRING(atom));    assert(ATOM_IS_STRING(atom));
166    JSString * s = ATOM_TO_STRING(atom);    JSString * s = ATOM_TO_STRING(atom);
167    JSString * quoted = js_QuoteString(context, s, '"');    Stream_write_char(f, '"');
168    print_string(quoted, f);    print_string(s, f);
169      Stream_write_char(f, '"');
170  }  }
171    
172  static const char * get_op(uint8 op) {  static const char * get_op(uint8 op) {
# Line 146  Line 215 
215  }  }
216    
217  static void instrument_expression(JSParseNode * node, Stream * f);  static void instrument_expression(JSParseNode * node, Stream * f);
218  static void instrument_statement(JSParseNode * node, Stream * f, int indent);  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
219    
220  enum FunctionType {  enum FunctionType {
221    FUNCTION_NORMAL,    FUNCTION_NORMAL,
# Line 202  Line 271 
271    free(params);    free(params);
272    
273    /* function body */    /* function body */
274    instrument_statement(node->pn_body, f, indent + 2);    instrument_statement(node->pn_body, f, indent + 2, false);
275    
276    Stream_write_string(f, "}\n");    Stream_write_string(f, "}\n");
277  }  }
# Line 487  Line 556 
556          JSObject * object = ATOM_TO_OBJECT(node->pn_atom);          JSObject * object = ATOM_TO_OBJECT(node->pn_atom);
557          jsval result;          jsval result;
558          js_regexp_toString(context, object, 0, NULL, &result);          js_regexp_toString(context, object, 0, NULL, &result);
559          print_string_jsval(result, f);          print_regex(result, f);
560        }        }
561        break;        break;
562      default:      default:
# Line 561  Line 630 
630    }    }
631  }  }
632    
633  static void output_statement(JSParseNode * node, Stream * f, int indent) {  static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
634    switch (node->pn_type) {    switch (node->pn_type) {
635    case TOK_FUNCTION:    case TOK_FUNCTION:
636      instrument_function(node, f, indent, FUNCTION_NORMAL);      instrument_function(node, f, indent, FUNCTION_NORMAL);
# Line 572  Line 641 
641      Stream_write_string(f, "{\n");      Stream_write_string(f, "{\n");
642  */  */
643      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) {
644        instrument_statement(p, f, indent);        instrument_statement(p, f, indent, false);
645      }      }
646  /*  /*
647      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 580  Line 649 
649  */  */
650      break;      break;
651    case TOK_IF:    case TOK_IF:
652      {
653      assert(node->pn_arity == PN_TERNARY);      assert(node->pn_arity == PN_TERNARY);
654    
655        uint16_t line = node->pn_pos.begin.lineno;
656        if (! is_jscoverage_if) {
657          if (line > num_lines) {
658            fatal("%s: script contains more than 65,535 lines", file_id);
659          }
660          if (line >= 2 && exclusive_directives[line - 2]) {
661            is_jscoverage_if = true;
662          }
663        }
664    
665      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
666      Stream_write_string(f, "if (");      Stream_write_string(f, "if (");
667      instrument_expression(node->pn_kid1, f);      instrument_expression(node->pn_kid1, f);
668      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
669      instrument_statement(node->pn_kid2, f, indent + 2);      if (is_jscoverage_if && node->pn_kid3) {
670          uint16_t else_start = node->pn_kid3->pn_pos.begin.lineno;
671          uint16_t else_end = node->pn_kid3->pn_pos.end.lineno + 1;
672          Stream_printf(f, "%*s", indent + 2, "");
673          Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, else_start, else_end);
674        }
675        instrument_statement(node->pn_kid2, f, indent + 2, false);
676      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
677      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
678      if (node->pn_kid3) {  
679        if (node->pn_kid3 || is_jscoverage_if) {
680        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
681        Stream_write_string(f, "else {\n");        Stream_write_string(f, "else {\n");
682        instrument_statement(node->pn_kid3, f, indent + 2);  
683          if (is_jscoverage_if) {
684            uint16_t if_start = node->pn_kid2->pn_pos.begin.lineno + 1;
685            uint16_t if_end = node->pn_kid2->pn_pos.end.lineno + 1;
686            Stream_printf(f, "%*s", indent + 2, "");
687            Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_start, if_end);
688          }
689    
690          if (node->pn_kid3) {
691            instrument_statement(node->pn_kid3, f, indent + 2, is_jscoverage_if);
692          }
693    
694        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
695        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
696      }      }
697    
698      break;      break;
699      }
700    case TOK_SWITCH:    case TOK_SWITCH:
701      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
702      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 617  Line 718 
718          abort();          abort();
719          break;          break;
720        }        }
721        instrument_statement(p->pn_right, f, indent + 2);        instrument_statement(p->pn_right, f, indent + 2, false);
722      }      }
723      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
724      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
# Line 632  Line 733 
733      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
734      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
735      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
736      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
737      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
738      break;      break;
739    case TOK_DO:    case TOK_DO:
740      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
741      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
742      Stream_write_string(f, "do {\n");      Stream_write_string(f, "do {\n");
743      instrument_statement(node->pn_left, f, indent + 2);      instrument_statement(node->pn_left, f, indent + 2, false);
744      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
745      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
746      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
# Line 702  Line 803 
803        break;        break;
804      }      }
805      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
806      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
807      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
808      break;      break;
809    case TOK_THROW:    case TOK_THROW:
# Line 715  Line 816 
816    case TOK_TRY:    case TOK_TRY:
817      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
818      Stream_write_string(f, "try {\n");      Stream_write_string(f, "try {\n");
819      instrument_statement(node->pn_kid1, f, indent + 2);      instrument_statement(node->pn_kid1, f, indent + 2, false);
820      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
821      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
822      {      {
# Line 730  Line 831 
831            instrument_expression(catch->pn_kid1->pn_expr, f);            instrument_expression(catch->pn_kid1->pn_expr, f);
832          }          }
833          Stream_write_string(f, ") {\n");          Stream_write_string(f, ") {\n");
834          instrument_statement(catch->pn_kid3, f, indent + 2);          instrument_statement(catch->pn_kid3, f, indent + 2, false);
835          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
836          Stream_write_string(f, "}\n");          Stream_write_string(f, "}\n");
837        }        }
# Line 738  Line 839 
839      if (node->pn_kid3) {      if (node->pn_kid3) {
840        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
841        Stream_write_string(f, "finally {\n");        Stream_write_string(f, "finally {\n");
842        instrument_statement(node->pn_kid3, f, indent + 2);        instrument_statement(node->pn_kid3, f, indent + 2, false);
843        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
844        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
845      }      }
# Line 764  Line 865 
865      Stream_write_string(f, "with (");      Stream_write_string(f, "with (");
866      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
867      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
868      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
869      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
870      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
871      break;      break;
# Line 802  Line 903 
903      /*      /*
904      ... use output_statement instead of instrument_statement.      ... use output_statement instead of instrument_statement.
905      */      */
906      output_statement(node->pn_expr, f, indent);      output_statement(node->pn_expr, f, indent, false);
907      break;      break;
908    default:    default:
909      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 915 
915  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
916  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
917  */  */
918  static void instrument_statement(JSParseNode * node, Stream * f, int indent) {  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
919    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC) {
920      int line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
921        if (line > num_lines) {
922          fatal("%s: script contains more than 65,535 lines", file_id);
923        }
924    
925      /* the root node has line number 0 */      /* the root node has line number 0 */
926      if (line != 0) {      if (line != 0) {
927        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
# Line 824  Line 929 
929        lines[line - 1] = 1;        lines[line - 1] = 1;
930      }      }
931    }    }
932    output_statement(node, f, indent);    output_statement(node, f, indent, is_jscoverage_if);
933  }  }
934    
935  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) {
936      const jschar * characters_end = characters + line_end;
937      const jschar * cp = characters + line_start;
938      const char * bp = prefix;
939      for (;;) {
940        if (*bp == '\0') {
941          return true;
942        }
943        else if (cp == characters_end) {
944          return false;
945        }
946        else if (*cp != *bp) {
947          return false;
948        }
949        bp++;
950        cp++;
951      }
952    }
953    
954    static bool characters_are_white_space(const jschar * characters, size_t line_start, size_t line_end) {
955      /* XXX - other Unicode space */
956      const jschar * end = characters + line_end;
957      for (const jschar * p = characters + line_start; p < end; p++) {
958        jschar c = *p;
959        if (c == 0x9 || c == 0xB || c == 0xC || c == 0x20 || c == 0xA0) {
960          continue;
961        }
962        else {
963          return false;
964        }
965      }
966      return true;
967    }
968    
969    void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) {
970    file_id = id;    file_id = id;
971    
972    /* scan the javascript */    /* scan the javascript */
973    size_t input_length = input->length;    JSTokenStream * token_stream = js_NewTokenStream(context, characters, num_characters, NULL, 1, NULL);
   jschar * base = js_InflateString(context, (char *) input->data, &input_length);  
   if (base == NULL) {  
     fatal("out of memory");  
   }  
   JSTokenStream * token_stream = js_NewTokenStream(context, base, input_length, NULL, 1, NULL);  
974    if (token_stream == NULL) {    if (token_stream == NULL) {
975      fatal("cannot create token stream from file: %s", file_id);      fatal("cannot create token stream from file: %s", file_id);
976    }    }
# Line 846  Line 980 
980    if (node == NULL) {    if (node == NULL) {
981      fatal("parse error in file: %s", file_id);      fatal("parse error in file: %s", file_id);
982    }    }
983    int num_lines = node->pn_pos.end.lineno;    num_lines = node->pn_pos.end.lineno;
984    lines = xmalloc(num_lines);    lines = xmalloc(num_lines);
985    for (int i = 0; i < num_lines; i++) {    for (unsigned int i = 0; i < num_lines; i++) {
986      lines[i] = 0;      lines[i] = 0;
987    }    }
988    
989      /* search code for conditionals */
990      exclusive_directives = xnew(bool, num_lines);
991      for (unsigned int i = 0; i < num_lines; i++) {
992        exclusive_directives[i] = false;
993      }
994    
995      bool has_conditionals = false;
996      struct IfDirective * if_directives = NULL;
997      size_t line_number = 0;
998      size_t i = 0;
999      while (i < num_characters) {
1000        if (line_number == UINT16_MAX) {
1001          fatal("%s: script has more than 65,535 lines", file_id);
1002        }
1003        line_number++;
1004        size_t line_start = i;
1005        jschar c;
1006        bool done = false;
1007        while (! done && i < num_characters) {
1008          c = characters[i];
1009          switch (c) {
1010          case '\r':
1011          case '\n':
1012          case 0x2028:
1013          case 0x2029:
1014            done = true;
1015            break;
1016          default:
1017            i++;
1018          }
1019        }
1020        size_t line_end = i;
1021        if (i < num_characters) {
1022          i++;
1023          if (c == '\r' && i < num_characters && characters[i] == '\n') {
1024            i++;
1025          }
1026        }
1027    
1028        if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) {
1029          has_conditionals = true;
1030    
1031          if (characters_are_white_space(characters, line_start + 16, line_end)) {
1032            exclusive_directives[line_number - 1] = true;
1033          }
1034          else {
1035            struct IfDirective * if_directive = xnew(struct IfDirective, 1);
1036            if_directive->condition_start = characters + line_start + 16;
1037            if_directive->condition_end = characters + line_end;
1038            if_directive->start_line = line_number;
1039            if_directive->end_line = 0;
1040            if_directive->next = if_directives;
1041            if_directives = if_directive;
1042          }
1043        }
1044        else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) {
1045          for (struct IfDirective * p = if_directives; p != NULL; p = p->next) {
1046            if (p->end_line == 0) {
1047              p->end_line = line_number;
1048              break;
1049            }
1050          }
1051        }
1052      }
1053    
1054    /*    /*
1055    An instrumented JavaScript file has 3 sections:    An instrumented JavaScript file has 4 sections:
1056    1. initialization    1. initialization
1057    2. instrumented source code    2. instrumented source code
1058    3. original source code    3. conditionals
1059      4. original source code
1060    */    */
1061    
1062    Stream * instrumented = Stream_new(0);    Stream * instrumented = Stream_new(0);
1063    instrument_statement(node, instrumented, 0);    instrument_statement(node, instrumented, 0, false);
1064    
1065    /* write line number info to the output */    /* write line number info to the output */
1066    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 1076 
1076    Stream_write_string(output, "}\n");    Stream_write_string(output, "}\n");
1077    free(lines);    free(lines);
1078    lines = NULL;    lines = NULL;
1079      free(exclusive_directives);
1080      exclusive_directives = NULL;
1081    
1082      /* conditionals */
1083      if (has_conditionals) {
1084        Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);
1085      }
1086    
1087    /* copy the instrumented source code to the output */    /* copy the instrumented source code to the output */
1088    Stream_write(output, instrumented->data, instrumented->length);    Stream_write(output, instrumented->data, instrumented->length);
   Stream_write_char(output, '\n');  
1089    
1090    /* conditionals */    /* conditionals */
1091    bool has_conditionals = false;    for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) {
1092    size_t line_number = 0;      Stream_write_string(output, "if (!(");
1093    size_t i = 0;      for (const jschar * p = if_directive->condition_start; p < if_directive->condition_end; p++) {
1094    while (i < input_length) {        jschar c = *p;
1095      line_number++;        if (c == '\t' || (32 <= c && c <= 126)) {
1096      size_t line_start = i;          Stream_write_char(output, c);
     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++;  
1097        }        }
1098        else {        else {
1099          abort();          Stream_printf(output, "\\u%04x", c);
1100        }        }
1101      }      }
1102      char * line = js_DeflateString(context, base + line_start, line_end - line_start);      Stream_write_string(output, ")) {\n");
1103      if (str_starts_with(line, "//#JSCOVERAGE_IF")) {      Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line);
1104        if (! has_conditionals) {      Stream_write_string(output, "}\n");
1105          has_conditionals = true;    }
1106          Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);  
1107        }    /* free */
1108        Stream_printf(output, "if (!%s) {\n", line + 16);    while (if_directives != NULL) {
1109        Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = ", file_id, line_number);      struct IfDirective * if_directive = if_directives;
1110      }      if_directives = if_directives->next;
1111      else if (str_starts_with(line, "//#JSCOVERAGE_ENDIF")) {      free(if_directive);
       Stream_printf(output, "%d;\n", line_number);  
       Stream_printf(output, "}\n");  
     }  
     JS_free(context, line);  
1112    }    }
1113    
1114    /* copy the original source to the output */    /* copy the original source to the output */
1115    i = 0;    Stream_printf(output, "_$jscoverage['%s'].source = ", file_id);
1116    while (i < input_length) {    jscoverage_write_source(id, characters, num_characters, output);
1117      Stream_write_string(output, "// ");    Stream_printf(output, ";\n");
     size_t line_start = i;  
     while (i < input_length && base[i] != '\r' && base[i] != '\n') {  
       i++;  
     }  
1118    
1119      size_t line_end = i;    Stream_delete(instrumented);
1120      if (i < input_length) {  
1121        if (base[i] == '\r') {    file_id = NULL;
1122          line_end = i;  }
1123    
1124    void jscoverage_write_source(const char * id, const jschar * characters, size_t num_characters, Stream * output) {
1125      Stream_write_string(output, "[");
1126      if (jscoverage_highlight) {
1127        Stream * highlighted_stream = Stream_new(num_characters);
1128        jscoverage_highlight_js(context, id, characters, num_characters, highlighted_stream);
1129        size_t i = 0;
1130        while (i < highlighted_stream->length) {
1131          if (i > 0) {
1132            Stream_write_char(output, ',');
1133          }
1134    
1135          Stream_write_char(output, '"');
1136          bool done = false;
1137          while (! done) {
1138            char c = highlighted_stream->data[i];
1139            switch (c) {
1140            case 0x8:
1141              /* backspace */
1142              Stream_write_string(output, "\\b");
1143              break;
1144            case 0x9:
1145              /* horizontal tab */
1146              Stream_write_string(output, "\\t");
1147              break;
1148            case 0xa:
1149              /* line feed (new line) */
1150              done = true;
1151              break;
1152            case 0xb:
1153              /* vertical tab */
1154              Stream_write_string(output, "\\v");
1155              break;
1156            case 0xc:
1157              /* form feed */
1158              Stream_write_string(output, "\\f");
1159              break;
1160            case 0xd:
1161              /* carriage return */
1162              done = true;
1163              if (i + 1 < highlighted_stream->length && highlighted_stream->data[i + 1] == '\n') {
1164                i++;
1165              }
1166              break;
1167            case '"':
1168              Stream_write_string(output, "\\\"");
1169              break;
1170            case '\\':
1171              Stream_write_string(output, "\\\\");
1172              break;
1173            default:
1174              Stream_write_char(output, c);
1175              break;
1176            }
1177          i++;          i++;
1178          if (i < input_length && base[i] == '\n') {          if (i >= highlighted_stream->length) {
1179            i++;            done = true;
1180          }          }
1181        }        }
1182        else if (base[i] == '\n') {        Stream_write_char(output, '"');
1183          line_end = i;      }
1184        Stream_delete(highlighted_stream);
1185      }
1186      else {
1187        size_t i = 0;
1188        while (i < num_characters) {
1189          if (i > 0) {
1190            Stream_write_char(output, ',');
1191          }
1192    
1193          Stream_write_char(output, '"');
1194          bool done = false;
1195          while (! done) {
1196            jschar c = characters[i];
1197            switch (c) {
1198            case 0x8:
1199              /* backspace */
1200              Stream_write_string(output, "\\b");
1201              break;
1202            case 0x9:
1203              /* horizontal tab */
1204              Stream_write_string(output, "\\t");
1205              break;
1206            case 0xa:
1207              /* line feed (new line) */
1208              done = true;
1209              break;
1210            case 0xb:
1211              /* vertical tab */
1212              Stream_write_string(output, "\\v");
1213              break;
1214            case 0xc:
1215              /* form feed */
1216              Stream_write_string(output, "\\f");
1217              break;
1218            case 0xd:
1219              /* carriage return */
1220              done = true;
1221              if (i + 1 < num_characters && characters[i + 1] == '\n') {
1222                i++;
1223              }
1224              break;
1225            case '"':
1226              Stream_write_string(output, "\\\"");
1227              break;
1228            case '\\':
1229              Stream_write_string(output, "\\\\");
1230              break;
1231            case '&':
1232              Stream_write_string(output, "&amp;");
1233              break;
1234            case '<':
1235              Stream_write_string(output, "&lt;");
1236              break;
1237            case '>':
1238              Stream_write_string(output, "&gt;");
1239              break;
1240            case 0x2028:
1241            case 0x2029:
1242              done = true;
1243              break;
1244            default:
1245              if (32 <= c && c <= 126) {
1246                Stream_write_char(output, c);
1247              }
1248              else {
1249                Stream_printf(output, "&#%d;", c);
1250              }
1251              break;
1252            }
1253          i++;          i++;
1254            if (i >= num_characters) {
1255              done = true;
1256            }
1257        }        }
1258        else {        Stream_write_char(output, '"');
         abort();  
       }  
1259      }      }
   
     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);  
1260    }    }
1261      Stream_write_string(output, "]");
   Stream_delete(instrumented);  
   
   JS_free(context, base);  
   
   file_id = NULL;  
1262  }  }
1263    
1264  void jscoverage_copy_resources(const char * destination_directory) {  void jscoverage_copy_resources(const char * destination_directory) {
1265    copy_resource("jscoverage.html", destination_directory);    copy_resource("jscoverage.html", destination_directory);
1266    copy_resource("jscoverage.css", destination_directory);    copy_resource("jscoverage.css", destination_directory);
1267    copy_resource("jscoverage.js", destination_directory);    copy_resource("jscoverage.js", destination_directory);
1268      copy_resource("jscoverage-ie.css", destination_directory);
1269    copy_resource("jscoverage-throbber.gif", destination_directory);    copy_resource("jscoverage-throbber.gif", destination_directory);
1270    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);  
1271  }  }
1272    
1273  /*  /*
# Line 1006  Line 1302 
1302    JS_HashTableDestroy(coverage->coverage_table);    JS_HashTableDestroy(coverage->coverage_table);
1303    struct FileCoverageList * p = coverage->coverage_list;    struct FileCoverageList * p = coverage->coverage_list;
1304    while (p != NULL) {    while (p != NULL) {
1305      free(p->file_coverage->lines);      free(p->file_coverage->coverage_lines);
1306      free(p->file_coverage->source);      if (p->file_coverage->source_lines != NULL) {
1307          for (uint32 i = 0; i < p->file_coverage->num_source_lines; i++) {
1308            free(p->file_coverage->source_lines[i]);
1309          }
1310          free(p->file_coverage->source_lines);
1311        }
1312      free(p->file_coverage->id);      free(p->file_coverage->id);
1313      free(p->file_coverage);      free(p->file_coverage);
1314      struct FileCoverageList * q = p;      struct FileCoverageList * q = p;
# Line 1129  Line 1430 
1430          }          }
1431          else if (strcmp(s, "source") == 0) {          else if (strcmp(s, "source") == 0) {
1432            source = element->pn_right;            source = element->pn_right;
1433            if (source->pn_type != TOK_STRING || ! ATOM_IS_STRING(source->pn_atom)) {            if (source->pn_type != TOK_RB) {
1434              return -1;              return -1;
1435            }            }
1436          }          }
# Line 1153  Line 1454 
1454        char * id = xstrdup(id_bytes);        char * id = xstrdup(id_bytes);
1455        file_coverage = xmalloc(sizeof(FileCoverage));        file_coverage = xmalloc(sizeof(FileCoverage));
1456        file_coverage->id = id;        file_coverage->id = id;
1457        file_coverage->num_lines = array->pn_count - 1;        file_coverage->num_coverage_lines = array->pn_count;
1458        file_coverage->lines = xnew(int, array->pn_count);        file_coverage->coverage_lines = xnew(int, array->pn_count);
1459        if (source == NULL) {        if (source == NULL) {
1460          file_coverage->source = NULL;          file_coverage->source_lines = NULL;
1461        }        }
1462        else {        else {
1463          file_coverage->source = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(source->pn_atom)));          file_coverage->num_source_lines = source->pn_count;
1464            file_coverage->source_lines = xnew(char *, source->pn_count);
1465            uint32 i = 0;
1466            for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {
1467              if (element->pn_type != TOK_STRING) {
1468                return -1;
1469              }
1470              file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));
1471            }
1472            assert(i == source->pn_count);
1473        }        }
1474    
1475        /* set coverage for all lines */        /* set coverage for all lines */
1476        uint32 i = 0;        uint32 i = 0;
1477        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++) {
1478          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1479            file_coverage->lines[i] = (int) element->pn_dval;            file_coverage->coverage_lines[i] = (int) element->pn_dval;
1480          }          }
1481          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1482            file_coverage->lines[i] = -1;            file_coverage->coverage_lines[i] = -1;
1483          }          }
1484          else {          else {
1485            return -1;            return -1;
# Line 1187  Line 1497 
1497      else {      else {
1498        /* sanity check */        /* sanity check */
1499        assert(strcmp(file_coverage->id, id_bytes) == 0);        assert(strcmp(file_coverage->id, id_bytes) == 0);
1500        if (file_coverage->num_lines != array->pn_count - 1) {        if (file_coverage->num_coverage_lines != array->pn_count) {
1501          return -2;          return -2;
1502        }        }
1503    
# Line 1195  Line 1505 
1505        uint32 i = 0;        uint32 i = 0;
1506        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++) {
1507          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1508            if (file_coverage->lines[i] == -1) {            if (file_coverage->coverage_lines[i] == -1) {
1509              return -2;              return -2;
1510            }            }
1511            file_coverage->lines[i] += (int) element->pn_dval;            file_coverage->coverage_lines[i] += (int) element->pn_dval;
1512          }          }
1513          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1514            if (file_coverage->lines[i] != -1) {            if (file_coverage->coverage_lines[i] != -1) {
1515              return -2;              return -2;
1516            }            }
1517          }          }
# Line 1212  Line 1522 
1522        assert(i == array->pn_count);        assert(i == array->pn_count);
1523    
1524        /* if this JSON file has source, use it */        /* if this JSON file has source, use it */
1525        if (file_coverage->source == NULL && source != NULL) {        if (file_coverage->source_lines == NULL && source != NULL) {
1526          file_coverage->source = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(source->pn_atom)));          file_coverage->num_source_lines = source->pn_count;
1527            file_coverage->source_lines = xnew(char *, source->pn_count);
1528            uint32 i = 0;
1529            for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {
1530              if (element->pn_type != TOK_STRING) {
1531                return -1;
1532              }
1533              file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));
1534            }
1535            assert(i == source->pn_count);
1536        }        }
1537      }      }
1538    }    }

Legend:
Removed from v.159  
changed lines
  Added in v.240

  ViewVC Help
Powered by ViewVC 1.1.24