/[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 95 by siliconforks, Wed May 7 22:50:00 2008 UTC revision 317 by siliconforks, Tue Oct 14 01:28:46 2008 UTC
# Line 17  Line 17 
17      51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.      51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */  */
19    
20    #include <config.h>
21    
22  #include "instrument-js.h"  #include "instrument-js.h"
23    
24  #include <assert.h>  #include <assert.h>
# Line 25  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 32  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"
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 44  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 71  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 (int 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 84  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 143  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  static void instrument_function(JSParseNode * node, Stream * f, int indent) {  enum FunctionType {
249      assert(node->pn_arity == PN_FUNC);    FUNCTION_NORMAL,
250      assert(ATOM_IS_OBJECT(node->pn_funAtom));    FUNCTION_GETTER_OR_SETTER
251      JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom);  };
252      assert(JS_ObjectIsFunction(context, object));  
253      JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);  static void instrument_function(JSParseNode * node, Stream * f, int indent, enum FunctionType type) {
254      assert(function);    assert(node->pn_arity == PN_FUNC);
255      assert(object == function->object);    assert(ATOM_IS_OBJECT(node->pn_funAtom));
256      Stream_printf(f, "%*s", indent, "");    JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom);
257      assert(JS_ObjectIsFunction(context, object));
258      JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
259      assert(function);
260      assert(object == function->object);
261      Stream_printf(f, "%*s", indent, "");
262      if (type == FUNCTION_NORMAL) {
263      Stream_write_string(f, "function");      Stream_write_string(f, "function");
264      }
265    
266      /* function name */    /* function name */
267      if (function->atom) {    if (function->atom) {
268        Stream_write_char(f, ' ');      Stream_write_char(f, ' ');
269        print_string_atom(function->atom, f);      print_string_atom(function->atom, f);
270      }    }
271    
272      /* function parameters */    /* function parameters */
273      Stream_write_string(f, "(");    Stream_write_string(f, "(");
274      JSAtom ** params = xmalloc(function->nargs * sizeof(JSAtom *));    JSAtom ** params = xnew(JSAtom *, function->nargs);
275      for (int i = 0; i < function->nargs; i++) {    for (int i = 0; i < function->nargs; i++) {
276        /* initialize to NULL for sanity check */      /* initialize to NULL for sanity check */
277        params[i] = NULL;      params[i] = NULL;
278      }    }
279      JSScope * scope = OBJ_SCOPE(object);    JSScope * scope = OBJ_SCOPE(object);
280      for (JSScopeProperty * scope_property = SCOPE_LAST_PROP(scope); scope_property != NULL; scope_property = scope_property->parent) {    for (JSScopeProperty * scope_property = SCOPE_LAST_PROP(scope); scope_property != NULL; scope_property = scope_property->parent) {
281        if (scope_property->getter != js_GetArgument) {      if (scope_property->getter != js_GetArgument) {
282          continue;        continue;
283        }      }
284        assert(scope_property->flags & SPROP_HAS_SHORTID);      assert(scope_property->flags & SPROP_HAS_SHORTID);
285        assert((uint16) scope_property->shortid < function->nargs);      assert((uint16) scope_property->shortid < function->nargs);
286        assert(JSID_IS_ATOM(scope_property->id));      assert(JSID_IS_ATOM(scope_property->id));
287        params[(uint16) scope_property->shortid] = JSID_TO_ATOM(scope_property->id);      params[(uint16) scope_property->shortid] = JSID_TO_ATOM(scope_property->id);
288      }
289      for (int i = 0; i < function->nargs; i++) {
290        assert(params[i] != NULL);
291        if (i > 0) {
292          Stream_write_string(f, ", ");
293      }      }
294      for (int i = 0; i < function->nargs; i++) {      if (ATOM_IS_STRING(params[i])) {
295        assert(params[i] != NULL);        print_string_atom(params[i], f);
       if (i > 0) {  
         Stream_write_string(f, ", ");  
       }  
       if (ATOM_IS_STRING(params[i])) {  
         print_string_atom(params[i], f);  
       }  
296      }      }
297      Stream_write_string(f, ") {\n");    }
298      free(params);    Stream_write_string(f, ") {\n");
299      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  }  }
306    
307  static void instrument_function_call(JSParseNode * node, Stream * f) {  static void instrument_function_call(JSParseNode * node, Stream * f) {
# Line 225  Line 332 
332  static void instrument_expression(JSParseNode * node, Stream * f) {  static void instrument_expression(JSParseNode * node, Stream * f) {
333    switch (node->pn_type) {    switch (node->pn_type) {
334    case TOK_FUNCTION:    case TOK_FUNCTION:
335      instrument_function(node, f, 0);      instrument_function(node, f, 0, FUNCTION_NORMAL);
336      break;      break;
337    case TOK_COMMA:    case TOK_COMMA:
338      for (struct JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) {      for (struct JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) {
# Line 432  Line 539 
539        if (p != node->pn_head) {        if (p != node->pn_head) {
540          Stream_write_string(f, ", ");          Stream_write_string(f, ", ");
541        }        }
542        instrument_expression(p->pn_left, f);  
543        Stream_write_string(f, ": ");        /* check whether this is a getter or setter */
544        instrument_expression(p->pn_right, f);        switch (p->pn_op) {
545          case JSOP_GETTER:
546          case JSOP_SETTER:
547            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);
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);
558            break;
559          default:
560            instrument_expression(p->pn_left, f);
561            Stream_write_string(f, ": ");
562            instrument_expression(p->pn_right, f);
563            break;
564          }
565      }      }
566      Stream_write_char(f, '}');      Stream_write_char(f, '}');
567      break;      break;
# Line 461  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 535  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);      instrument_function(node, f, indent, FUNCTION_NORMAL);
669      break;      break;
670    case TOK_LC:    case TOK_LC:
671      assert(node->pn_arity == PN_LIST);      assert(node->pn_arity == PN_LIST);
# Line 546  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 554  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 591  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 606  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 676  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 689  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 704  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 712  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 738  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 776  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 788  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 798  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  void jscoverage_instrument_js(const char * id, Stream * input, Stream * output) {  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, 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, 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 (TODO)    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 850  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 */
1122      if (has_conditionals) {
1123        Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);
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    size_t 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, '"');
1294          abort();      }
1295      }
1296      Stream_write_string(output, "]");
1297    }
1298    
1299    void jscoverage_copy_resources(const char * destination_directory) {
1300      copy_resource("jscoverage.html", destination_directory);
1301      copy_resource("jscoverage.css", destination_directory);
1302      copy_resource("jscoverage.js", destination_directory);
1303      copy_resource("jscoverage-ie.css", destination_directory);
1304      copy_resource("jscoverage-throbber.gif", destination_directory);
1305      copy_resource("jscoverage-highlight.css", destination_directory);
1306    }
1307    
1308    /*
1309    coverage reports
1310    */
1311    
1312    struct FileCoverageList {
1313      FileCoverage * file_coverage;
1314      struct FileCoverageList * next;
1315    };
1316    
1317    struct Coverage {
1318      JSHashTable * coverage_table;
1319      struct FileCoverageList * coverage_list;
1320    };
1321    
1322    static int compare_strings(const void * p1, const void * p2) {
1323      return strcmp(p1, p2) == 0;
1324    }
1325    
1326    Coverage * Coverage_new(void) {
1327      Coverage * result = xmalloc(sizeof(Coverage));
1328      result->coverage_table = JS_NewHashTable(1024, JS_HashString, compare_strings, NULL, NULL, NULL);
1329      if (result->coverage_table == NULL) {
1330        fatal("cannot create hash table");
1331      }
1332      result->coverage_list = NULL;
1333      return result;
1334    }
1335    
1336    void Coverage_delete(Coverage * coverage) {
1337      JS_HashTableDestroy(coverage->coverage_table);
1338      struct FileCoverageList * p = coverage->coverage_list;
1339      while (p != NULL) {
1340        free(p->file_coverage->coverage_lines);
1341        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);
1348        free(p->file_coverage);
1349        struct FileCoverageList * q = p;
1350        p = p->next;
1351        free(q);
1352      }
1353      free(coverage);
1354    }
1355    
1356    struct EnumeratorArg {
1357      CoverageForeachFunction f;
1358      void * p;
1359    };
1360    
1361    static intN enumerator(JSHashEntry * entry, intN i, void * arg) {
1362      struct EnumeratorArg * enumerator_arg = arg;
1363      enumerator_arg->f(entry->value, i, enumerator_arg->p);
1364      return 0;
1365    }
1366    
1367    void Coverage_foreach_file(Coverage * coverage, CoverageForeachFunction f, void * p) {
1368      struct EnumeratorArg enumerator_arg;
1369      enumerator_arg.f = f;
1370      enumerator_arg.p = p;
1371      JS_HashTableEnumerateEntries(coverage->coverage_table, enumerator, &enumerator_arg);
1372    }
1373    
1374      char * line = js_DeflateString(context, base + line_start, line_end - line_start);  int jscoverage_parse_json(Coverage * coverage, const uint8_t * json, size_t length) {
1375      Stream_write_string(output, line);    jschar * base = js_InflateString(context, (char *) json, &length);
1376      Stream_write_char(output, '\n');    if (base == NULL) {
1377      JS_free(context, line);      fatal("out of memory");
1378    }    }
1379    
1380    Stream_delete(instrumented);    jschar * parenthesized_json = xnew(jschar, addst(length, 2));
1381      parenthesized_json[0] = '(';
1382      memcpy(parenthesized_json + 1, base, mulst(length, sizeof(jschar)));
1383      parenthesized_json[length + 1] = ')';
1384    
1385    JS_free(context, base);    JS_free(context, base);
1386    
1387    file_id = NULL;    JSTokenStream * token_stream = js_NewTokenStream(context, parenthesized_json, length + 2, NULL, 1, NULL);
1388      if (token_stream == NULL) {
1389        fatal("cannot create token stream");
1390      }
1391    
1392      JSParseNode * root = js_ParseTokenStream(context, global, token_stream);
1393      free(parenthesized_json);
1394      if (root == NULL) {
1395        return -1;
1396      }
1397    
1398      /* root node must be TOK_LC */
1399      if (root->pn_type != TOK_LC) {
1400        return -1;
1401      }
1402      JSParseNode * semi = root->pn_u.list.head;
1403    
1404      /* the list must be TOK_SEMI and it must contain only one element */
1405      if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {
1406        return -1;
1407      }
1408      JSParseNode * parenthesized = semi->pn_kid;
1409    
1410      /* this must be a parenthesized expression */
1411      if (parenthesized->pn_type != TOK_RP) {
1412        return -1;
1413      }
1414      JSParseNode * object = parenthesized->pn_kid;
1415    
1416      /* this must be an object literal */
1417      if (object->pn_type != TOK_RC) {
1418        return -1;
1419      }
1420    
1421      for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {
1422        /* every element of this list must be TOK_COLON */
1423        if (p->pn_type != TOK_COLON) {
1424          return -1;
1425        }
1426    
1427        /* the key must be a string representing the file */
1428        JSParseNode * key = p->pn_left;
1429        if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {
1430          return -1;
1431        }
1432        char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));
1433    
1434        /* the value must be an object literal OR an array */
1435        JSParseNode * value = p->pn_right;
1436        if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {
1437          return -1;
1438        }
1439    
1440        JSParseNode * array = NULL;
1441        JSParseNode * source = NULL;
1442        if (value->pn_type == TOK_RB) {
1443          /* an array */
1444          array = value;
1445        }
1446        else if (value->pn_type == TOK_RC) {
1447          /* an object literal */
1448          if (value->pn_count != 2) {
1449            return -1;
1450          }
1451          for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {
1452            if (element->pn_type != TOK_COLON) {
1453              return -1;
1454            }
1455            JSParseNode * left = element->pn_left;
1456            if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {
1457              return -1;
1458            }
1459            const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));
1460            if (strcmp(s, "coverage") == 0) {
1461              array = element->pn_right;
1462              if (array->pn_type != TOK_RB) {
1463                return -1;
1464              }
1465            }
1466            else if (strcmp(s, "source") == 0) {
1467              source = element->pn_right;
1468              if (source->pn_type != TOK_RB) {
1469                return -1;
1470              }
1471            }
1472            else {
1473              return -1;
1474            }
1475          }
1476        }
1477        else {
1478          return -1;
1479        }
1480    
1481        if (array == NULL) {
1482          return -1;
1483        }
1484    
1485        /* look up the file in the coverage table */
1486        FileCoverage * file_coverage = JS_HashTableLookup(coverage->coverage_table, id_bytes);
1487        if (file_coverage == NULL) {
1488          /* not there: create a new one */
1489          char * id = xstrdup(id_bytes);
1490          file_coverage = xmalloc(sizeof(FileCoverage));
1491          file_coverage->id = id;
1492          file_coverage->num_coverage_lines = array->pn_count;
1493          file_coverage->coverage_lines = xnew(int, array->pn_count);
1494          file_coverage->source_lines = NULL;
1495    
1496          /* set coverage for all lines */
1497          uint32 i = 0;
1498          for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {
1499            if (element->pn_type == TOK_NUMBER) {
1500              file_coverage->coverage_lines[i] = (int) element->pn_dval;
1501            }
1502            else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1503              file_coverage->coverage_lines[i] = -1;
1504            }
1505            else {
1506              return -1;
1507            }
1508          }
1509          assert(i == array->pn_count);
1510    
1511          /* add to the hash table */
1512          JS_HashTableAdd(coverage->coverage_table, id, file_coverage);
1513          struct FileCoverageList * coverage_list = xmalloc(sizeof(struct FileCoverageList));
1514          coverage_list->file_coverage = file_coverage;
1515          coverage_list->next = coverage->coverage_list;
1516          coverage->coverage_list = coverage_list;
1517        }
1518        else {
1519          /* sanity check */
1520          assert(strcmp(file_coverage->id, id_bytes) == 0);
1521          if (file_coverage->num_coverage_lines != array->pn_count) {
1522            return -2;
1523          }
1524    
1525          /* merge the coverage */
1526          uint32 i = 0;
1527          for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {
1528            if (element->pn_type == TOK_NUMBER) {
1529              if (file_coverage->coverage_lines[i] == -1) {
1530                return -2;
1531              }
1532              file_coverage->coverage_lines[i] += (int) element->pn_dval;
1533            }
1534            else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1535              if (file_coverage->coverage_lines[i] != -1) {
1536                return -2;
1537              }
1538            }
1539            else {
1540              return -1;
1541            }
1542          }
1543          assert(i == array->pn_count);
1544        }
1545    
1546        /* if this JSON file has source, use it */
1547        if (file_coverage->source_lines == NULL && source != NULL) {
1548          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    
1561      return 0;
1562  }  }

Legend:
Removed from v.95  
changed lines
  Added in v.317

  ViewVC Help
Powered by ViewVC 1.1.24