/[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 176 by siliconforks, Sat Sep 20 23:29:42 2008 UTC revision 284 by siliconforks, Sat Oct 11 22:40:31 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 35  Line 36 
36  #include <jsstr.h>  #include <jsstr.h>
37    
38  #include "encoding.h"  #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 48  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 75  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    size_t length = JSSTRING_LENGTH(s);    size_t length = JSSTRING_LENGTH(s);
118    jschar * characters = JSSTRING_CHARS(s);    jschar * characters = JSSTRING_CHARS(s);
# Line 202  Line 240 
240  }  }
241    
242  static void instrument_expression(JSParseNode * node, Stream * f);  static void instrument_expression(JSParseNode * node, Stream * f);
243  static void instrument_statement(JSParseNode * node, Stream * f, int indent);  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
244    
245  enum FunctionType {  enum FunctionType {
246    FUNCTION_NORMAL,    FUNCTION_NORMAL,
# Line 258  Line 296 
296    free(params);    free(params);
297    
298    /* function body */    /* function body */
299    instrument_statement(node->pn_body, f, indent + 2);    instrument_statement(node->pn_body, f, indent + 2, false);
300    
301    Stream_write_string(f, "}\n");    Stream_write_string(f, "}\n");
302  }  }
# Line 617  Line 655 
655    }    }
656  }  }
657    
658  static void output_statement(JSParseNode * node, Stream * f, int indent) {  static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
659    switch (node->pn_type) {    switch (node->pn_type) {
660    case TOK_FUNCTION:    case TOK_FUNCTION:
661      instrument_function(node, f, indent, FUNCTION_NORMAL);      instrument_function(node, f, indent, FUNCTION_NORMAL);
# Line 628  Line 666 
666      Stream_write_string(f, "{\n");      Stream_write_string(f, "{\n");
667  */  */
668      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) {
669        instrument_statement(p, f, indent);        instrument_statement(p, f, indent, false);
670      }      }
671  /*  /*
672      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 636  Line 674 
674  */  */
675      break;      break;
676    case TOK_IF:    case TOK_IF:
677      {
678      assert(node->pn_arity == PN_TERNARY);      assert(node->pn_arity == PN_TERNARY);
679    
680        uint16_t line = node->pn_pos.begin.lineno;
681        if (! is_jscoverage_if) {
682          if (line > num_lines) {
683            fatal("%s: script contains more than 65,535 lines", file_id);
684          }
685          if (line >= 2 && exclusive_directives[line - 2]) {
686            is_jscoverage_if = true;
687          }
688        }
689    
690      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
691      Stream_write_string(f, "if (");      Stream_write_string(f, "if (");
692      instrument_expression(node->pn_kid1, f);      instrument_expression(node->pn_kid1, f);
693      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
694      instrument_statement(node->pn_kid2, f, indent + 2);      if (is_jscoverage_if && node->pn_kid3) {
695          uint16_t else_start = node->pn_kid3->pn_pos.begin.lineno;
696          uint16_t else_end = node->pn_kid3->pn_pos.end.lineno + 1;
697          Stream_printf(f, "%*s", indent + 2, "");
698          Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, else_start, else_end);
699        }
700        instrument_statement(node->pn_kid2, f, indent + 2, false);
701      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
702      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
703      if (node->pn_kid3) {  
704        if (node->pn_kid3 || is_jscoverage_if) {
705        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
706        Stream_write_string(f, "else {\n");        Stream_write_string(f, "else {\n");
707        instrument_statement(node->pn_kid3, f, indent + 2);  
708          if (is_jscoverage_if) {
709            uint16_t if_start = node->pn_kid2->pn_pos.begin.lineno + 1;
710            uint16_t if_end = node->pn_kid2->pn_pos.end.lineno + 1;
711            Stream_printf(f, "%*s", indent + 2, "");
712            Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_start, if_end);
713          }
714    
715          if (node->pn_kid3) {
716            instrument_statement(node->pn_kid3, f, indent + 2, is_jscoverage_if);
717          }
718    
719        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
720        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
721      }      }
722    
723      break;      break;
724      }
725    case TOK_SWITCH:    case TOK_SWITCH:
726      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
727      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 673  Line 743 
743          abort();          abort();
744          break;          break;
745        }        }
746        instrument_statement(p->pn_right, f, indent + 2);        instrument_statement(p->pn_right, f, indent + 2, false);
747      }      }
748      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
749      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
# Line 688  Line 758 
758      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
759      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
760      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
761      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
762      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
763      break;      break;
764    case TOK_DO:    case TOK_DO:
765      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
766      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
767      Stream_write_string(f, "do {\n");      Stream_write_string(f, "do {\n");
768      instrument_statement(node->pn_left, f, indent + 2);      instrument_statement(node->pn_left, f, indent + 2, false);
769      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
770      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
771      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
# Line 758  Line 828 
828        break;        break;
829      }      }
830      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
831      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
832      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
833      break;      break;
834    case TOK_THROW:    case TOK_THROW:
# Line 771  Line 841 
841    case TOK_TRY:    case TOK_TRY:
842      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
843      Stream_write_string(f, "try {\n");      Stream_write_string(f, "try {\n");
844      instrument_statement(node->pn_kid1, f, indent + 2);      instrument_statement(node->pn_kid1, f, indent + 2, false);
845      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
846      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
847      {      {
# Line 786  Line 856 
856            instrument_expression(catch->pn_kid1->pn_expr, f);            instrument_expression(catch->pn_kid1->pn_expr, f);
857          }          }
858          Stream_write_string(f, ") {\n");          Stream_write_string(f, ") {\n");
859          instrument_statement(catch->pn_kid3, f, indent + 2);          instrument_statement(catch->pn_kid3, f, indent + 2, false);
860          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
861          Stream_write_string(f, "}\n");          Stream_write_string(f, "}\n");
862        }        }
# Line 794  Line 864 
864      if (node->pn_kid3) {      if (node->pn_kid3) {
865        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
866        Stream_write_string(f, "finally {\n");        Stream_write_string(f, "finally {\n");
867        instrument_statement(node->pn_kid3, f, indent + 2);        instrument_statement(node->pn_kid3, f, indent + 2, false);
868        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
869        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
870      }      }
# Line 820  Line 890 
890      Stream_write_string(f, "with (");      Stream_write_string(f, "with (");
891      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
892      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
893      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
894      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
895      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
896      break;      break;
# Line 858  Line 928 
928      /*      /*
929      ... use output_statement instead of instrument_statement.      ... use output_statement instead of instrument_statement.
930      */      */
931      output_statement(node->pn_expr, f, indent);      output_statement(node->pn_expr, f, indent, false);
932      break;      break;
933    default:    default:
934      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 870  Line 940 
940  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
941  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
942  */  */
943  static void instrument_statement(JSParseNode * node, Stream * f, int indent) {  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
944    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC) {
945      int line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
946        if (line > num_lines) {
947          fatal("%s: script contains more than 65,535 lines", file_id);
948        }
949    
950      /* the root node has line number 0 */      /* the root node has line number 0 */
951      if (line != 0) {      if (line != 0) {
952        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
# Line 880  Line 954 
954        lines[line - 1] = 1;        lines[line - 1] = 1;
955      }      }
956    }    }
957    output_statement(node, f, indent);    output_statement(node, f, indent, is_jscoverage_if);
958  }  }
959    
960  void jscoverage_instrument_js(const char * id, const char * encoding, Stream * input, Stream * output) {  static bool characters_start_with(const jschar * characters, size_t line_start, size_t line_end, const char * prefix) {
961      const jschar * characters_end = characters + line_end;
962      const jschar * cp = characters + line_start;
963      const char * bp = prefix;
964      for (;;) {
965        if (*bp == '\0') {
966          return true;
967        }
968        else if (cp == characters_end) {
969          return false;
970        }
971        else if (*cp != *bp) {
972          return false;
973        }
974        bp++;
975        cp++;
976      }
977    }
978    
979    static bool characters_are_white_space(const jschar * characters, size_t line_start, size_t line_end) {
980      /* XXX - other Unicode space */
981      const jschar * end = characters + line_end;
982      for (const jschar * p = characters + line_start; p < end; p++) {
983        jschar c = *p;
984        if (c == 0x9 || c == 0xB || c == 0xC || c == 0x20 || c == 0xA0) {
985          continue;
986        }
987        else {
988          return false;
989        }
990      }
991      return true;
992    }
993    
994    static void error_reporter(JSContext * context, const char * message, JSErrorReport * report) {
995      fprintf(stderr, "jscoverage: parse error: line %u: %s\n", report->lineno, message);
996    }
997    
998    void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) {
999    file_id = id;    file_id = id;
1000    
1001    /* scan the javascript */    /* scan the javascript */
1002    size_t num_characters = input->length;    JSTokenStream * token_stream = js_NewTokenStream(context, characters, num_characters, NULL, 1, NULL);
   jschar * base = NULL;  
   int result = jscoverage_bytes_to_characters(encoding, input->data, input->length, &base, &num_characters);  
   if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) {  
     fatal("encoding %s not supported in file %s", encoding, id);  
   }  
   else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) {  
     fatal("error decoding %s in file %s", encoding, id);  
   }  
   JSTokenStream * token_stream = js_NewTokenStream(context, base, num_characters, NULL, 1, NULL);  
1003    if (token_stream == NULL) {    if (token_stream == NULL) {
1004      fatal("cannot create token stream from file: %s", file_id);      fatal("cannot create token stream from file: %s", file_id);
1005    }    }
1006    
1007    /* parse the javascript */    /* parse the javascript */
1008      JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);
1009    JSParseNode * node = js_ParseTokenStream(context, global, token_stream);    JSParseNode * node = js_ParseTokenStream(context, global, token_stream);
1010    if (node == NULL) {    if (node == NULL) {
1011        js_ReportUncaughtException(context);
1012      fatal("parse error in file: %s", file_id);      fatal("parse error in file: %s", file_id);
1013    }    }
1014    int num_lines = node->pn_pos.end.lineno;    JS_SetErrorReporter(context, old_error_reporter);
1015      num_lines = node->pn_pos.end.lineno;
1016    lines = xmalloc(num_lines);    lines = xmalloc(num_lines);
1017    for (int i = 0; i < num_lines; i++) {    for (unsigned int i = 0; i < num_lines; i++) {
1018      lines[i] = 0;      lines[i] = 0;
1019    }    }
1020    
1021      /* search code for conditionals */
1022      exclusive_directives = xnew(bool, num_lines);
1023      for (unsigned int i = 0; i < num_lines; i++) {
1024        exclusive_directives[i] = false;
1025      }
1026    
1027      bool has_conditionals = false;
1028      struct IfDirective * if_directives = NULL;
1029      size_t line_number = 0;
1030      size_t i = 0;
1031      while (i < num_characters) {
1032        if (line_number == UINT16_MAX) {
1033          fatal("%s: script has more than 65,535 lines", file_id);
1034        }
1035        line_number++;
1036        size_t line_start = i;
1037        jschar c;
1038        bool done = false;
1039        while (! done && i < num_characters) {
1040          c = characters[i];
1041          switch (c) {
1042          case '\r':
1043          case '\n':
1044          case 0x2028:
1045          case 0x2029:
1046            done = true;
1047            break;
1048          default:
1049            i++;
1050          }
1051        }
1052        size_t line_end = i;
1053        if (i < num_characters) {
1054          i++;
1055          if (c == '\r' && i < num_characters && characters[i] == '\n') {
1056            i++;
1057          }
1058        }
1059    
1060        if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) {
1061          has_conditionals = true;
1062    
1063          if (characters_are_white_space(characters, line_start + 16, line_end)) {
1064            exclusive_directives[line_number - 1] = true;
1065          }
1066          else {
1067            struct IfDirective * if_directive = xnew(struct IfDirective, 1);
1068            if_directive->condition_start = characters + line_start + 16;
1069            if_directive->condition_end = characters + line_end;
1070            if_directive->start_line = line_number;
1071            if_directive->end_line = 0;
1072            if_directive->next = if_directives;
1073            if_directives = if_directive;
1074          }
1075        }
1076        else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) {
1077          for (struct IfDirective * p = if_directives; p != NULL; p = p->next) {
1078            if (p->end_line == 0) {
1079              p->end_line = line_number;
1080              break;
1081            }
1082          }
1083        }
1084      }
1085    
1086    /*    /*
1087    An instrumented JavaScript file has 3 sections:    An instrumented JavaScript file has 4 sections:
1088    1. initialization    1. initialization
1089    2. instrumented source code    2. instrumented source code
1090    3. original source code    3. conditionals
1091      4. original source code
1092    */    */
1093    
1094    Stream * instrumented = Stream_new(0);    Stream * instrumented = Stream_new(0);
1095    instrument_statement(node, instrumented, 0);    instrument_statement(node, instrumented, 0, false);
1096    
1097    /* write line number info to the output */    /* write line number info to the output */
1098    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 936  Line 1108 
1108    Stream_write_string(output, "}\n");    Stream_write_string(output, "}\n");
1109    free(lines);    free(lines);
1110    lines = NULL;    lines = NULL;
1111      free(exclusive_directives);
1112      exclusive_directives = NULL;
1113    
1114      /* conditionals */
1115      if (has_conditionals) {
1116        Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);
1117      }
1118    
1119    /* copy the instrumented source code to the output */    /* copy the instrumented source code to the output */
1120    Stream_write(output, instrumented->data, instrumented->length);    Stream_write(output, instrumented->data, instrumented->length);
   Stream_write_char(output, '\n');  
1121    
1122    /* conditionals */    /* conditionals */
1123    bool has_conditionals = false;    for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) {
1124    size_t line_number = 0;      Stream_write_string(output, "if (!(");
1125    size_t i = 0;      print_javascript(if_directive->condition_start, if_directive->condition_end - if_directive->condition_start, output);
1126    while (i < num_characters) {      Stream_write_string(output, ")) {\n");
1127      line_number++;      Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line);
1128      size_t line_start = i;      Stream_write_string(output, "}\n");
1129      while (i < num_characters && base[i] != '\r' && base[i] != '\n') {    }
1130        i++;  
1131      }    /* free */
1132      size_t line_end = i;    while (if_directives != NULL) {
1133      if (i < num_characters) {      struct IfDirective * if_directive = if_directives;
1134        if (base[i] == '\r') {      if_directives = if_directives->next;
1135          line_end = i;      free(if_directive);
         i++;  
         if (i < num_characters && 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, "if (!%s) {\n", line + 16);  
       Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = ", file_id, line_number);  
     }  
     else if (str_starts_with(line, "//#JSCOVERAGE_ENDIF")) {  
       Stream_printf(output, "%d;\n", line_number);  
       Stream_printf(output, "}\n");  
     }  
     JS_free(context, line);  
1136    }    }
1137    
1138    /* copy the original source to the output */    /* copy the original source to the output */
1139    i = 0;    Stream_printf(output, "_$jscoverage['%s'].source = ", file_id);
1140    while (i < num_characters) {    jscoverage_write_source(id, characters, num_characters, output);
1141      Stream_write_string(output, "// ");    Stream_printf(output, ";\n");
     size_t line_start = i;  
     while (i < num_characters && base[i] != '\r' && base[i] != '\n') {  
       i++;  
     }  
1142    
1143      size_t line_end = i;    Stream_delete(instrumented);
1144      if (i < num_characters) {  
1145        if (base[i] == '\r') {    file_id = NULL;
1146          line_end = i;  }
1147    
1148    void jscoverage_write_source(const char * id, const jschar * characters, size_t num_characters, Stream * output) {
1149      Stream_write_string(output, "[");
1150      if (jscoverage_highlight) {
1151        Stream * highlighted_stream = Stream_new(num_characters);
1152        jscoverage_highlight_js(context, id, characters, num_characters, highlighted_stream);
1153        size_t i = 0;
1154        while (i < highlighted_stream->length) {
1155          if (i > 0) {
1156            Stream_write_char(output, ',');
1157          }
1158    
1159          Stream_write_char(output, '"');
1160          bool done = false;
1161          while (! done) {
1162            char c = highlighted_stream->data[i];
1163            switch (c) {
1164            case 0x8:
1165              /* backspace */
1166              Stream_write_string(output, "\\b");
1167              break;
1168            case 0x9:
1169              /* horizontal tab */
1170              Stream_write_string(output, "\\t");
1171              break;
1172            case 0xa:
1173              /* line feed (new line) */
1174              done = true;
1175              break;
1176            case 0xb:
1177              /* vertical tab */
1178              Stream_write_string(output, "\\v");
1179              break;
1180            case 0xc:
1181              /* form feed */
1182              Stream_write_string(output, "\\f");
1183              break;
1184            case 0xd:
1185              /* carriage return */
1186              done = true;
1187              if (i + 1 < highlighted_stream->length && highlighted_stream->data[i + 1] == '\n') {
1188                i++;
1189              }
1190              break;
1191            case '"':
1192              Stream_write_string(output, "\\\"");
1193              break;
1194            case '\\':
1195              Stream_write_string(output, "\\\\");
1196              break;
1197            default:
1198              Stream_write_char(output, c);
1199              break;
1200            }
1201          i++;          i++;
1202          if (i < num_characters && base[i] == '\n') {          if (i >= highlighted_stream->length) {
1203            i++;            done = true;
1204          }          }
1205        }        }
1206        else if (base[i] == '\n') {        Stream_write_char(output, '"');
1207          line_end = i;      }
1208        Stream_delete(highlighted_stream);
1209      }
1210      else {
1211        size_t i = 0;
1212        while (i < num_characters) {
1213          if (i > 0) {
1214            Stream_write_char(output, ',');
1215          }
1216    
1217          Stream_write_char(output, '"');
1218          bool done = false;
1219          while (! done) {
1220            jschar c = characters[i];
1221            switch (c) {
1222            case 0x8:
1223              /* backspace */
1224              Stream_write_string(output, "\\b");
1225              break;
1226            case 0x9:
1227              /* horizontal tab */
1228              Stream_write_string(output, "\\t");
1229              break;
1230            case 0xa:
1231              /* line feed (new line) */
1232              done = true;
1233              break;
1234            case 0xb:
1235              /* vertical tab */
1236              Stream_write_string(output, "\\v");
1237              break;
1238            case 0xc:
1239              /* form feed */
1240              Stream_write_string(output, "\\f");
1241              break;
1242            case 0xd:
1243              /* carriage return */
1244              done = true;
1245              if (i + 1 < num_characters && characters[i + 1] == '\n') {
1246                i++;
1247              }
1248              break;
1249            case '"':
1250              Stream_write_string(output, "\\\"");
1251              break;
1252            case '\\':
1253              Stream_write_string(output, "\\\\");
1254              break;
1255            case '&':
1256              Stream_write_string(output, "&amp;");
1257              break;
1258            case '<':
1259              Stream_write_string(output, "&lt;");
1260              break;
1261            case '>':
1262              Stream_write_string(output, "&gt;");
1263              break;
1264            case 0x2028:
1265            case 0x2029:
1266              done = true;
1267              break;
1268            default:
1269              if (32 <= c && c <= 126) {
1270                Stream_write_char(output, c);
1271              }
1272              else {
1273                Stream_printf(output, "&#%d;", c);
1274              }
1275              break;
1276            }
1277          i++;          i++;
1278            if (i >= num_characters) {
1279              done = true;
1280            }
1281        }        }
1282        else {        Stream_write_char(output, '"');
         abort();  
       }  
1283      }      }
   
     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);  
1284    }    }
1285      Stream_write_string(output, "]");
   Stream_delete(instrumented);  
   
   JS_free(context, base);  
   
   file_id = NULL;  
1286  }  }
1287    
1288  void jscoverage_copy_resources(const char * destination_directory) {  void jscoverage_copy_resources(const char * destination_directory) {
# Line 1030  Line 1291 
1291    copy_resource("jscoverage.js", destination_directory);    copy_resource("jscoverage.js", destination_directory);
1292    copy_resource("jscoverage-ie.css", destination_directory);    copy_resource("jscoverage-ie.css", destination_directory);
1293    copy_resource("jscoverage-throbber.gif", destination_directory);    copy_resource("jscoverage-throbber.gif", destination_directory);
1294    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);  
1295  }  }
1296    
1297  /*  /*
# Line 1067  Line 1326 
1326    JS_HashTableDestroy(coverage->coverage_table);    JS_HashTableDestroy(coverage->coverage_table);
1327    struct FileCoverageList * p = coverage->coverage_list;    struct FileCoverageList * p = coverage->coverage_list;
1328    while (p != NULL) {    while (p != NULL) {
1329      free(p->file_coverage->lines);      free(p->file_coverage->coverage_lines);
1330      free(p->file_coverage->source);      if (p->file_coverage->source_lines != NULL) {
1331          for (uint32 i = 0; i < p->file_coverage->num_source_lines; i++) {
1332            free(p->file_coverage->source_lines[i]);
1333          }
1334          free(p->file_coverage->source_lines);
1335        }
1336      free(p->file_coverage->id);      free(p->file_coverage->id);
1337      free(p->file_coverage);      free(p->file_coverage);
1338      struct FileCoverageList * q = p;      struct FileCoverageList * q = p;
# Line 1190  Line 1454 
1454          }          }
1455          else if (strcmp(s, "source") == 0) {          else if (strcmp(s, "source") == 0) {
1456            source = element->pn_right;            source = element->pn_right;
1457            if (source->pn_type != TOK_STRING || ! ATOM_IS_STRING(source->pn_atom)) {            if (source->pn_type != TOK_RB) {
1458              return -1;              return -1;
1459            }            }
1460          }          }
# Line 1214  Line 1478 
1478        char * id = xstrdup(id_bytes);        char * id = xstrdup(id_bytes);
1479        file_coverage = xmalloc(sizeof(FileCoverage));        file_coverage = xmalloc(sizeof(FileCoverage));
1480        file_coverage->id = id;        file_coverage->id = id;
1481        file_coverage->num_lines = array->pn_count - 1;        file_coverage->num_coverage_lines = array->pn_count;
1482        file_coverage->lines = xnew(int, array->pn_count);        file_coverage->coverage_lines = xnew(int, array->pn_count);
1483        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)));  
       }  
1484    
1485        /* set coverage for all lines */        /* set coverage for all lines */
1486        uint32 i = 0;        uint32 i = 0;
1487        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++) {
1488          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1489            file_coverage->lines[i] = (int) element->pn_dval;            file_coverage->coverage_lines[i] = (int) element->pn_dval;
1490          }          }
1491          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1492            file_coverage->lines[i] = -1;            file_coverage->coverage_lines[i] = -1;
1493          }          }
1494          else {          else {
1495            return -1;            return -1;
# Line 1248  Line 1507 
1507      else {      else {
1508        /* sanity check */        /* sanity check */
1509        assert(strcmp(file_coverage->id, id_bytes) == 0);        assert(strcmp(file_coverage->id, id_bytes) == 0);
1510        if (file_coverage->num_lines != array->pn_count - 1) {        if (file_coverage->num_coverage_lines != array->pn_count) {
1511          return -2;          return -2;
1512        }        }
1513    
# Line 1256  Line 1515 
1515        uint32 i = 0;        uint32 i = 0;
1516        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++) {
1517          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1518            if (file_coverage->lines[i] == -1) {            if (file_coverage->coverage_lines[i] == -1) {
1519              return -2;              return -2;
1520            }            }
1521            file_coverage->lines[i] += (int) element->pn_dval;            file_coverage->coverage_lines[i] += (int) element->pn_dval;
1522          }          }
1523          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1524            if (file_coverage->lines[i] != -1) {            if (file_coverage->coverage_lines[i] != -1) {
1525              return -2;              return -2;
1526            }            }
1527          }          }
# Line 1271  Line 1530 
1530          }          }
1531        }        }
1532        assert(i == array->pn_count);        assert(i == array->pn_count);
1533        }
1534    
1535        /* if this JSON file has source, use it */      /* if this JSON file has source, use it */
1536        if (file_coverage->source == NULL && source != NULL) {      if (file_coverage->source_lines == NULL && source != NULL) {
1537          file_coverage->source = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(source->pn_atom)));        file_coverage->num_source_lines = source->pn_count;
1538          file_coverage->source_lines = xnew(char *, source->pn_count);
1539          uint32 i = 0;
1540          for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {
1541            if (element->pn_type != TOK_STRING) {
1542              return -1;
1543            }
1544            file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));
1545        }        }
1546          assert(i == source->pn_count);
1547      }      }
1548    }    }
1549    

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

  ViewVC Help
Powered by ViewVC 1.1.24