/[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 156 by siliconforks, Fri Sep 12 19:44:03 2008 UTC revision 176 by siliconforks, Sat Sep 20 23:29:42 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 "resource-manager.h"  #include "resource-manager.h"
39  #include "util.h"  #include "util.h"
40    
# Line 75  Line 76 
76  }  }
77    
78  static void print_string(JSString * s, Stream * f) {  static void print_string(JSString * s, Stream * f) {
79    for (size_t i = 0; i < s->length; i++) {    size_t length = JSSTRING_LENGTH(s);
80      char c = s->chars[i];    jschar * characters = JSSTRING_CHARS(s);
81      Stream_write_char(f, c);    for (size_t i = 0; i < length; i++) {
82        jschar c = characters[i];
83        if (32 <= c && c <= 126) {
84          switch (c) {
85          case '"':
86            Stream_write_string(f, "\\\"");
87            break;
88    /*
89          case '\'':
90            Stream_write_string(f, "\\'");
91            break;
92    */
93          case '\\':
94            Stream_write_string(f, "\\\\");
95            break;
96          default:
97            Stream_write_char(f, c);
98            break;
99          }
100        }
101        else {
102          switch (c) {
103          case 0x8:
104            Stream_write_string(f, "\\b");
105            break;
106          case 0x9:
107            Stream_write_string(f, "\\t");
108            break;
109          case 0xa:
110            Stream_write_string(f, "\\n");
111            break;
112          case 0xb:
113            Stream_write_string(f, "\\v");
114            break;
115          case 0xc:
116            Stream_write_string(f, "\\f");
117            break;
118          case 0xd:
119            Stream_write_string(f, "\\r");
120            break;
121          default:
122            Stream_printf(f, "\\u%04x", c);
123            break;
124          }
125        }
126    }    }
127  }  }
128    
# Line 87  Line 132 
132    print_string(s, f);    print_string(s, f);
133  }  }
134    
135  static void print_string_jsval(jsval value, Stream * f) {  static void print_regex(jsval value, Stream * f) {
136    assert(JSVAL_IS_STRING(value));    assert(JSVAL_IS_STRING(value));
137    JSString * s = JSVAL_TO_STRING(value);    JSString * s = JSVAL_TO_STRING(value);
138    print_string(s, f);    size_t length = JSSTRING_LENGTH(s);
139      jschar * characters = JSSTRING_CHARS(s);
140      for (size_t i = 0; i < length; i++) {
141        jschar c = characters[i];
142        if (32 <= c && c <= 126) {
143          Stream_write_char(f, c);
144        }
145        else {
146          Stream_printf(f, "\\u%04x", c);
147        }
148      }
149  }  }
150    
151  static void print_quoted_string_atom(JSAtom * atom, Stream * f) {  static void print_quoted_string_atom(JSAtom * atom, Stream * f) {
152    assert(ATOM_IS_STRING(atom));    assert(ATOM_IS_STRING(atom));
153    JSString * s = ATOM_TO_STRING(atom);    JSString * s = ATOM_TO_STRING(atom);
154    JSString * quoted = js_QuoteString(context, s, '"');    Stream_write_char(f, '"');
155    print_string(quoted, f);    print_string(s, f);
156      Stream_write_char(f, '"');
157  }  }
158    
159  static const char * get_op(uint8 op) {  static const char * get_op(uint8 op) {
# Line 487  Line 543 
543          JSObject * object = ATOM_TO_OBJECT(node->pn_atom);          JSObject * object = ATOM_TO_OBJECT(node->pn_atom);
544          jsval result;          jsval result;
545          js_regexp_toString(context, object, 0, NULL, &result);          js_regexp_toString(context, object, 0, NULL, &result);
546          print_string_jsval(result, f);          print_regex(result, f);
547        }        }
548        break;        break;
549      default:      default:
# Line 827  Line 883 
883    output_statement(node, f, indent);    output_statement(node, f, indent);
884  }  }
885    
886  void jscoverage_instrument_js(const char * id, Stream * input, Stream * output) {  void jscoverage_instrument_js(const char * id, const char * encoding, Stream * input, Stream * output) {
887    file_id = id;    file_id = id;
888    
889    /* scan the javascript */    /* scan the javascript */
890    size_t input_length = input->length;    size_t num_characters = input->length;
891    jschar * base = js_InflateString(context, (char *) input->data, &input_length);    jschar * base = NULL;
892    if (base == NULL) {    int result = jscoverage_bytes_to_characters(encoding, input->data, input->length, &base, &num_characters);
893      fatal("out of memory");    if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) {
894        fatal("encoding %s not supported in file %s", encoding, id);
895    }    }
896    JSTokenStream * token_stream = js_NewTokenStream(context, base, input_length, NULL, 1, NULL);    else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) {
897        fatal("error decoding %s in file %s", encoding, id);
898      }
899      JSTokenStream * token_stream = js_NewTokenStream(context, base, num_characters, NULL, 1, NULL);
900    if (token_stream == NULL) {    if (token_stream == NULL) {
901      fatal("cannot create token stream from file: %s", file_id);      fatal("cannot create token stream from file: %s", file_id);
902    }    }
# Line 881  Line 941 
941    Stream_write(output, instrumented->data, instrumented->length);    Stream_write(output, instrumented->data, instrumented->length);
942    Stream_write_char(output, '\n');    Stream_write_char(output, '\n');
943    
944    /* copy the original source to the output */    /* conditionals */
945      bool has_conditionals = false;
946      size_t line_number = 0;
947    size_t i = 0;    size_t i = 0;
948    while (i < input_length) {    while (i < num_characters) {
949        line_number++;
950        size_t line_start = i;
951        while (i < num_characters && base[i] != '\r' && base[i] != '\n') {
952          i++;
953        }
954        size_t line_end = i;
955        if (i < num_characters) {
956          if (base[i] == '\r') {
957            line_end = i;
958            i++;
959            if (i < num_characters && base[i] == '\n') {
960              i++;
961            }
962          }
963          else if (base[i] == '\n') {
964            line_end = i;
965            i++;
966          }
967          else {
968            abort();
969          }
970        }
971        char * line = js_DeflateString(context, base + line_start, line_end - line_start);
972        if (str_starts_with(line, "//#JSCOVERAGE_IF")) {
973          if (! has_conditionals) {
974            has_conditionals = true;
975            Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);
976          }
977          Stream_printf(output, "if (!%s) {\n", line + 16);
978          Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = ", file_id, line_number);
979        }
980        else if (str_starts_with(line, "//#JSCOVERAGE_ENDIF")) {
981          Stream_printf(output, "%d;\n", line_number);
982          Stream_printf(output, "}\n");
983        }
984        JS_free(context, line);
985      }
986    
987      /* copy the original source to the output */
988      i = 0;
989      while (i < num_characters) {
990      Stream_write_string(output, "// ");      Stream_write_string(output, "// ");
991      size_t line_start = i;      size_t line_start = i;
992      while (i < input_length && base[i] != '\r' && base[i] != '\n') {      while (i < num_characters && base[i] != '\r' && base[i] != '\n') {
993        i++;        i++;
994      }      }
995    
996      size_t line_end = i;      size_t line_end = i;
997      if (i < input_length) {      if (i < num_characters) {
998        if (base[i] == '\r') {        if (base[i] == '\r') {
999          line_end = i;          line_end = i;
1000          i++;          i++;
1001          if (i < input_length && base[i] == '\n') {          if (i < num_characters && base[i] == '\n') {
1002            i++;            i++;
1003          }          }
1004        }        }
# Line 925  Line 1028 
1028    copy_resource("jscoverage.html", destination_directory);    copy_resource("jscoverage.html", destination_directory);
1029    copy_resource("jscoverage.css", destination_directory);    copy_resource("jscoverage.css", destination_directory);
1030    copy_resource("jscoverage.js", destination_directory);    copy_resource("jscoverage.js", destination_directory);
1031      copy_resource("jscoverage-ie.css", destination_directory);
1032    copy_resource("jscoverage-throbber.gif", destination_directory);    copy_resource("jscoverage-throbber.gif", destination_directory);
1033    copy_resource("jscoverage-sh_main.js", destination_directory);    copy_resource("jscoverage-sh_main.js", destination_directory);
1034    copy_resource("jscoverage-sh_javascript.js", destination_directory);    copy_resource("jscoverage-sh_javascript.js", destination_directory);

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

  ViewVC Help
Powered by ViewVC 1.1.24