/[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 157 by siliconforks, Sat Sep 13 03:56:45 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 34  Line 35 
35  #include <jsscope.h>  #include <jsscope.h>
36  #include <jsstr.h>  #include <jsstr.h>
37    
38    #include "encoding.h"
39    #include "global.h"
40    #include "highlight.h"
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 47  Line 61 
61  */  */
62  static const char * file_id = NULL;  static const char * file_id = NULL;
63  static char * lines = NULL;  static char * lines = NULL;
64    static uint16_t num_lines = 0;
65    
66  void jscoverage_init(void) {  void jscoverage_init(void) {
67    runtime = JS_NewRuntime(8L * 1024L * 1024L);    runtime = JS_NewRuntime(8L * 1024L * 1024L);
# Line 74  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    for (size_t i = 0; i < s->length; i++) {    size_t length = JSSTRING_LENGTH(s);
118      char c = s->chars[i];    jschar * characters = JSSTRING_CHARS(s);
119      Stream_write_char(f, c);    for (size_t i = 0; i < length; i++) {
120        jschar c = characters[i];
121        if (32 <= c && c <= 126) {
122          switch (c) {
123          case '"':
124            Stream_write_string(f, "\\\"");
125            break;
126    /*
127          case '\'':
128            Stream_write_string(f, "\\'");
129            break;
130    */
131          case '\\':
132            Stream_write_string(f, "\\\\");
133            break;
134          default:
135            Stream_write_char(f, c);
136            break;
137          }
138        }
139        else {
140          switch (c) {
141          case 0x8:
142            Stream_write_string(f, "\\b");
143            break;
144          case 0x9:
145            Stream_write_string(f, "\\t");
146            break;
147          case 0xa:
148            Stream_write_string(f, "\\n");
149            break;
150          /* IE doesn't support this */
151          /*
152          case 0xb:
153            Stream_write_string(f, "\\v");
154            break;
155          */
156          case 0xc:
157            Stream_write_string(f, "\\f");
158            break;
159          case 0xd:
160            Stream_write_string(f, "\\r");
161            break;
162          default:
163            Stream_printf(f, "\\u%04x", c);
164            break;
165          }
166        }
167    }    }
168  }  }
169    
# Line 87  Line 173 
173    print_string(s, f);    print_string(s, f);
174  }  }
175    
176  static void print_string_jsval(jsval value, Stream * f) {  static void print_regex(jsval value, Stream * f) {
177    assert(JSVAL_IS_STRING(value));    assert(JSVAL_IS_STRING(value));
178    JSString * s = JSVAL_TO_STRING(value);    JSString * s = JSVAL_TO_STRING(value);
179    print_string(s, f);    size_t length = JSSTRING_LENGTH(s);
180      jschar * characters = JSSTRING_CHARS(s);
181      for (size_t i = 0; i < length; i++) {
182        jschar c = characters[i];
183        if (32 <= c && c <= 126) {
184          Stream_write_char(f, c);
185        }
186        else {
187          Stream_printf(f, "\\u%04x", c);
188        }
189      }
190  }  }
191    
192  static void print_quoted_string_atom(JSAtom * atom, Stream * f) {  static void print_quoted_string_atom(JSAtom * atom, Stream * f) {
193    assert(ATOM_IS_STRING(atom));    assert(ATOM_IS_STRING(atom));
194    JSString * s = ATOM_TO_STRING(atom);    JSString * s = ATOM_TO_STRING(atom);
195    JSString * quoted = js_QuoteString(context, s, '"');    Stream_write_char(f, '"');
196    print_string(quoted, f);    print_string(s, f);
197      Stream_write_char(f, '"');
198  }  }
199    
200  static const char * get_op(uint8 op) {  static const char * get_op(uint8 op) {
# Line 146  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 202  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 446  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 487  Line 588 
588          JSObject * object = ATOM_TO_OBJECT(node->pn_atom);          JSObject * object = ATOM_TO_OBJECT(node->pn_atom);
589          jsval result;          jsval result;
590          js_regexp_toString(context, object, 0, NULL, &result);          js_regexp_toString(context, object, 0, NULL, &result);
591          print_string_jsval(result, f);          print_regex(result, f);
592        }        }
593        break;        break;
594      default:      default:
# Line 561  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 572  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 580  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 617  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 632  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 702  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 715  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 730  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 738  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 764  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 802  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 814  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      int line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
953        if (line > num_lines) {
954          fatal("%s: script contains more than 65,535 lines", file_id);
955        }
956    
957      /* the root node has line number 0 */      /* the root node has line number 0 */
958      if (line != 0) {      if (line != 0) {
959        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
# Line 824  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) {
968      const jschar * characters_end = characters + line_end;
969      const jschar * cp = characters + line_start;
970      const char * bp = prefix;
971      for (;;) {
972        if (*bp == '\0') {
973          return true;
974        }
975        else if (cp == characters_end) {
976          return false;
977        }
978        else if (*cp != *bp) {
979          return false;
980        }
981        bp++;
982        cp++;
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, Stream * input, 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    
1008    /* scan the javascript */    /* scan the javascript */
1009    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);  
1010    if (token_stream == NULL) {    if (token_stream == NULL) {
1011      fatal("cannot create token stream from file: %s", file_id);      fatal("cannot create token stream from file: %s", file_id);
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    int num_lines = node->pn_pos.end.lineno;    JS_SetErrorReporter(context, old_error_reporter);
1022      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      exclusive_directives = xnew(bool, num_lines);
1030      for (unsigned int i = 0; i < num_lines; i++) {
1031        exclusive_directives[i] = false;
1032      }
1033    
1034      bool has_conditionals = false;
1035      struct IfDirective * if_directives = NULL;
1036      size_t line_number = 0;
1037      size_t i = 0;
1038      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++;
1043        size_t line_start = i;
1044        jschar c;
1045        bool done = false;
1046        while (! done && i < num_characters) {
1047          c = characters[i];
1048          switch (c) {
1049          case '\r':
1050          case '\n':
1051          case 0x2028:
1052          case 0x2029:
1053            done = true;
1054            break;
1055          default:
1056            i++;
1057          }
1058        }
1059        size_t line_end = i;
1060        if (i < num_characters) {
1061          i++;
1062          if (c == '\r' && i < num_characters && characters[i] == '\n') {
1063            i++;
1064          }
1065        }
1066    
1067        if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) {
1068          has_conditionals = true;
1069    
1070          if (characters_are_white_space(characters, line_start + 16, line_end)) {
1071            exclusive_directives[line_number - 1] = true;
1072          }
1073          else {
1074            struct IfDirective * if_directive = xnew(struct IfDirective, 1);
1075            if_directive->condition_start = characters + line_start + 16;
1076            if_directive->condition_end = characters + line_end;
1077            if_directive->start_line = line_number;
1078            if_directive->end_line = 0;
1079            if_directive->next = if_directives;
1080            if_directives = if_directive;
1081          }
1082        }
1083        else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) {
1084          for (struct IfDirective * p = if_directives; p != NULL; p = p->next) {
1085            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 3 sections:    An instrumented JavaScript file has 4 sections:
1095    1. initialization    1. initialization
1096    2. instrumented source code    2. instrumented source code
1097    3. original source code    3. conditionals
1098      4. original source code
1099    */    */
1100    
1101    Stream * instrumented = Stream_new(0);    Stream * instrumented = Stream_new(0);
1102    instrument_statement(node, instrumented, 0);    instrument_statement(node, instrumented, 0, false);
1103    
1104    /* write line number info to the output */    /* write line number info to the output */
1105    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 1115 
1115    Stream_write_string(output, "}\n");    Stream_write_string(output, "}\n");
1116    free(lines);    free(lines);
1117    lines = NULL;    lines = NULL;
1118      free(exclusive_directives);
1119      exclusive_directives = NULL;
1120    
1121    /* conditionals */    /* conditionals */
1122    bool has_conditionals = false;    if (has_conditionals) {
1123    size_t line_number = 0;      Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);
   size_t i = 0;  
   while (i < input_length) {  
     line_number++;  
     size_t line_start = i;  
     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++;  
       }  
       else {  
         abort();  
       }  
     }  
     char * line = js_DeflateString(context, base + line_start, line_end - line_start);  
     if (str_starts_with(line, "//#JSCOVERAGE_IF")) {  
       if (! has_conditionals) {  
         has_conditionals = true;  
         Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);  
       }  
       Stream_printf(output, "_$jscoverage['%s'].conditionals[%d] = {condition: function () {return %s;}, ", file_id, line_number, line + 16);  
     }  
     else if (str_starts_with(line, "//#JSCOVERAGE_ENDIF")) {  
       Stream_printf(output, "end: %d};\n", line_number);  
     }  
     JS_free(context, line);  
1124    }    }
1125    
1126    /* copy the instrumented source code to the output */    /* copy the instrumented source code to the output */
1127    Stream_write(output, instrumented->data, instrumented->length);    Stream_write(output, instrumented->data, instrumented->length);
1128    Stream_write_char(output, '\n');  
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    i = 0;    Stream_printf(output, "_$jscoverage['%s'].source = ", file_id);
1147    while (i < input_length) {    jscoverage_write_source(id, characters, num_characters, output);
1148      Stream_write_string(output, "// ");    Stream_printf(output, ";\n");
     size_t line_start = i;  
     while (i < input_length && base[i] != '\r' && base[i] != '\n') {  
       i++;  
     }  
1149    
1150      size_t line_end = i;    Stream_delete(instrumented);
1151      if (i < input_length) {  
1152        if (base[i] == '\r') {    file_id = NULL;
1153          line_end = i;  }
1154    
1155    void jscoverage_write_source(const char * id, const jschar * characters, size_t num_characters, Stream * output) {
1156      Stream_write_string(output, "[");
1157      if (jscoverage_highlight) {
1158        Stream * highlighted_stream = Stream_new(num_characters);
1159        jscoverage_highlight_js(context, id, characters, num_characters, highlighted_stream);
1160        size_t i = 0;
1161        while (i < highlighted_stream->length) {
1162          if (i > 0) {
1163            Stream_write_char(output, ',');
1164          }
1165    
1166          Stream_write_char(output, '"');
1167          bool done = false;
1168          while (! done) {
1169            char c = highlighted_stream->data[i];
1170            switch (c) {
1171            case 0x8:
1172              /* backspace */
1173              Stream_write_string(output, "\\b");
1174              break;
1175            case 0x9:
1176              /* horizontal tab */
1177              Stream_write_string(output, "\\t");
1178              break;
1179            case 0xa:
1180              /* line feed (new line) */
1181              done = true;
1182              break;
1183            /* IE doesn't support this */
1184            /*
1185            case 0xb:
1186              Stream_write_string(output, "\\v");
1187              break;
1188            */
1189            case 0xc:
1190              /* form feed */
1191              Stream_write_string(output, "\\f");
1192              break;
1193            case 0xd:
1194              /* carriage return */
1195              done = true;
1196              if (i + 1 < highlighted_stream->length && highlighted_stream->data[i + 1] == '\n') {
1197                i++;
1198              }
1199              break;
1200            case '"':
1201              Stream_write_string(output, "\\\"");
1202              break;
1203            case '\\':
1204              Stream_write_string(output, "\\\\");
1205              break;
1206            default:
1207              Stream_write_char(output, c);
1208              break;
1209            }
1210          i++;          i++;
1211          if (i < input_length && base[i] == '\n') {          if (i >= highlighted_stream->length) {
1212            i++;            done = true;
1213          }          }
1214        }        }
1215        else if (base[i] == '\n') {        Stream_write_char(output, '"');
1216          line_end = i;      }
1217        Stream_delete(highlighted_stream);
1218      }
1219      else {
1220        size_t i = 0;
1221        while (i < num_characters) {
1222          if (i > 0) {
1223            Stream_write_char(output, ',');
1224          }
1225    
1226          Stream_write_char(output, '"');
1227          bool done = false;
1228          while (! done) {
1229            jschar c = characters[i];
1230            switch (c) {
1231            case 0x8:
1232              /* backspace */
1233              Stream_write_string(output, "\\b");
1234              break;
1235            case 0x9:
1236              /* horizontal tab */
1237              Stream_write_string(output, "\\t");
1238              break;
1239            case 0xa:
1240              /* line feed (new line) */
1241              done = true;
1242              break;
1243            /* IE doesn't support this */
1244            /*
1245            case 0xb:
1246              Stream_write_string(output, "\\v");
1247              break;
1248            */
1249            case 0xc:
1250              /* form feed */
1251              Stream_write_string(output, "\\f");
1252              break;
1253            case 0xd:
1254              /* carriage return */
1255              done = true;
1256              if (i + 1 < num_characters && characters[i + 1] == '\n') {
1257                i++;
1258              }
1259              break;
1260            case '"':
1261              Stream_write_string(output, "\\\"");
1262              break;
1263            case '\\':
1264              Stream_write_string(output, "\\\\");
1265              break;
1266            case '&':
1267              Stream_write_string(output, "&amp;");
1268              break;
1269            case '<':
1270              Stream_write_string(output, "&lt;");
1271              break;
1272            case '>':
1273              Stream_write_string(output, "&gt;");
1274              break;
1275            case 0x2028:
1276            case 0x2029:
1277              done = true;
1278              break;
1279            default:
1280              if (32 <= c && c <= 126) {
1281                Stream_write_char(output, c);
1282              }
1283              else {
1284                Stream_printf(output, "&#%d;", c);
1285              }
1286              break;
1287            }
1288          i++;          i++;
1289            if (i >= num_characters) {
1290              done = true;
1291            }
1292        }        }
1293        else {        Stream_write_char(output, '"');
         abort();  
       }  
1294      }      }
   
     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);  
1295    }    }
1296      Stream_write_string(output, "]");
   Stream_delete(instrumented);  
   
   JS_free(context, base);  
   
   file_id = NULL;  
1297  }  }
1298    
1299  void jscoverage_copy_resources(const char * destination_directory) {  void jscoverage_copy_resources(const char * destination_directory) {
1300    copy_resource("jscoverage.html", destination_directory);    copy_resource("jscoverage.html", destination_directory);
1301    copy_resource("jscoverage.css", destination_directory);    copy_resource("jscoverage.css", destination_directory);
1302    copy_resource("jscoverage.js", destination_directory);    copy_resource("jscoverage.js", destination_directory);
1303      copy_resource("jscoverage-ie.css", destination_directory);
1304    copy_resource("jscoverage-throbber.gif", destination_directory);    copy_resource("jscoverage-throbber.gif", destination_directory);
1305    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);  
1306  }  }
1307    
1308  /*  /*
# Line 1004  Line 1337 
1337    JS_HashTableDestroy(coverage->coverage_table);    JS_HashTableDestroy(coverage->coverage_table);
1338    struct FileCoverageList * p = coverage->coverage_list;    struct FileCoverageList * p = coverage->coverage_list;
1339    while (p != NULL) {    while (p != NULL) {
1340      free(p->file_coverage->lines);      free(p->file_coverage->coverage_lines);
1341      free(p->file_coverage->source);      if (p->file_coverage->source_lines != NULL) {
1342          for (uint32 i = 0; i < p->file_coverage->num_source_lines; i++) {
1343            free(p->file_coverage->source_lines[i]);
1344          }
1345          free(p->file_coverage->source_lines);
1346        }
1347      free(p->file_coverage->id);      free(p->file_coverage->id);
1348      free(p->file_coverage);      free(p->file_coverage);
1349      struct FileCoverageList * q = p;      struct FileCoverageList * q = p;
# Line 1127  Line 1465 
1465          }          }
1466          else if (strcmp(s, "source") == 0) {          else if (strcmp(s, "source") == 0) {
1467            source = element->pn_right;            source = element->pn_right;
1468            if (source->pn_type != TOK_STRING || ! ATOM_IS_STRING(source->pn_atom)) {            if (source->pn_type != TOK_RB) {
1469              return -1;              return -1;
1470            }            }
1471          }          }
# Line 1151  Line 1489 
1489        char * id = xstrdup(id_bytes);        char * id = xstrdup(id_bytes);
1490        file_coverage = xmalloc(sizeof(FileCoverage));        file_coverage = xmalloc(sizeof(FileCoverage));
1491        file_coverage->id = id;        file_coverage->id = id;
1492        file_coverage->num_lines = array->pn_count - 1;        file_coverage->num_coverage_lines = array->pn_count;
1493        file_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 = NULL;  
       }  
       else {  
         file_coverage->source = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(source->pn_atom)));  
       }  
1495    
1496        /* set coverage for all lines */        /* set coverage for all lines */
1497        uint32 i = 0;        uint32 i = 0;
1498        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++) {
1499          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1500            file_coverage->lines[i] = (int) element->pn_dval;            file_coverage->coverage_lines[i] = (int) element->pn_dval;
1501          }          }
1502          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1503            file_coverage->lines[i] = -1;            file_coverage->coverage_lines[i] = -1;
1504          }          }
1505          else {          else {
1506            return -1;            return -1;
# Line 1185  Line 1518 
1518      else {      else {
1519        /* sanity check */        /* sanity check */
1520        assert(strcmp(file_coverage->id, id_bytes) == 0);        assert(strcmp(file_coverage->id, id_bytes) == 0);
1521        if (file_coverage->num_lines != array->pn_count - 1) {        if (file_coverage->num_coverage_lines != array->pn_count) {
1522          return -2;          return -2;
1523        }        }
1524    
# Line 1193  Line 1526 
1526        uint32 i = 0;        uint32 i = 0;
1527        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++) {
1528          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1529            if (file_coverage->lines[i] == -1) {            if (file_coverage->coverage_lines[i] == -1) {
1530              return -2;              return -2;
1531            }            }
1532            file_coverage->lines[i] += (int) element->pn_dval;            file_coverage->coverage_lines[i] += (int) element->pn_dval;
1533          }          }
1534          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1535            if (file_coverage->lines[i] != -1) {            if (file_coverage->coverage_lines[i] != -1) {
1536              return -2;              return -2;
1537            }            }
1538          }          }
# Line 1208  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 == NULL && source != NULL) {      if (file_coverage->source_lines == NULL && source != NULL) {
1548          file_coverage->source = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(source->pn_atom)));        file_coverage->num_source_lines = source->pn_count;
1549          file_coverage->source_lines = xnew(char *, source->pn_count);
1550          uint32 i = 0;
1551          for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {
1552            if (element->pn_type != TOK_STRING) {
1553              return -1;
1554            }
1555            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.157  
changed lines
  Added in v.317

  ViewVC Help
Powered by ViewVC 1.1.24