/[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 169 by siliconforks, Mon Sep 15 16:22:41 2008 UTC revision 345 by siliconforks, Fri Oct 24 16:16:00 2008 UTC
# Line 26  Line 26 
26  #include <string.h>  #include <string.h>
27    
28  #include <jsapi.h>  #include <jsapi.h>
29    #include <jsarena.h>
30  #include <jsatom.h>  #include <jsatom.h>
31    #include <jsexn.h>
32  #include <jsfun.h>  #include <jsfun.h>
33  #include <jsinterp.h>  #include <jsinterp.h>
34    #include <jsiter.h>
35  #include <jsparse.h>  #include <jsparse.h>
36  #include <jsregexp.h>  #include <jsregexp.h>
37  #include <jsscope.h>  #include <jsscope.h>
38  #include <jsstr.h>  #include <jsstr.h>
39    
40    #include "encoding.h"
41    #include "global.h"
42    #include "highlight.h"
43  #include "resource-manager.h"  #include "resource-manager.h"
44  #include "util.h"  #include "util.h"
45    
46    struct IfDirective {
47      const jschar * condition_start;
48      const jschar * condition_end;
49      uint16_t start_line;
50      uint16_t end_line;
51      struct IfDirective * next;
52    };
53    
54    static bool * exclusive_directives = NULL;
55    
56  static JSRuntime * runtime = NULL;  static JSRuntime * runtime = NULL;
57  static JSContext * context = NULL;  static JSContext * context = NULL;
58  static JSObject * global = NULL;  static JSObject * global = NULL;
59    static JSVersion js_version = JSVERSION_ECMA_3;
60    
61  /*  /*
62  JSParseNode objects store line numbers starting from 1.  JSParseNode objects store line numbers starting from 1.
# Line 47  Line 64 
64  */  */
65  static const char * file_id = NULL;  static const char * file_id = NULL;
66  static char * lines = NULL;  static char * lines = NULL;
67    static uint16_t num_lines = 0;
68    
69    void jscoverage_set_js_version(const char * version) {
70      js_version = atoi(version);
71    }
72    
73  void jscoverage_init(void) {  void jscoverage_init(void) {
74    runtime = JS_NewRuntime(8L * 1024L * 1024L);    runtime = JS_NewRuntime(8L * 1024L * 1024L);
# Line 59  Line 81 
81      fatal("cannot create context");      fatal("cannot create context");
82    }    }
83    
84      JS_SetVersion(context, js_version);
85    
86    global = JS_NewObject(context, NULL, NULL, NULL);    global = JS_NewObject(context, NULL, NULL, NULL);
87    if (global == NULL) {    if (global == NULL) {
88      fatal("cannot create global object");      fatal("cannot create global object");
# Line 74  Line 98 
98    JS_DestroyRuntime(runtime);    JS_DestroyRuntime(runtime);
99  }  }
100    
101    static void print_javascript(const jschar * characters, size_t num_characters, Stream * f) {
102      for (size_t i = 0; i < num_characters; i++) {
103        jschar c = characters[i];
104        /*
105        XXX does not handle no-break space, other unicode "space separator"
106        */
107        switch (c) {
108        case 0x9:
109        case 0xB:
110        case 0xC:
111          Stream_write_char(f, c);
112          break;
113        default:
114          if (32 <= c && c <= 126) {
115            Stream_write_char(f, c);
116          }
117          else {
118            Stream_printf(f, "\\u%04x", c);
119          }
120          break;
121        }
122      }
123    }
124    
125  static void print_string(JSString * s, Stream * f) {  static void print_string(JSString * s, Stream * f) {
126    for (size_t i = 0; i < s->length; i++) {    size_t length = JSSTRING_LENGTH(s);
127      char c = s->chars[i];    jschar * characters = JSSTRING_CHARS(s);
128      Stream_write_char(f, c);    for (size_t i = 0; i < length; i++) {
129        jschar c = characters[i];
130        if (32 <= c && c <= 126) {
131          switch (c) {
132          case '"':
133            Stream_write_string(f, "\\\"");
134            break;
135    /*
136          case '\'':
137            Stream_write_string(f, "\\'");
138            break;
139    */
140          case '\\':
141            Stream_write_string(f, "\\\\");
142            break;
143          default:
144            Stream_write_char(f, c);
145            break;
146          }
147        }
148        else {
149          switch (c) {
150          case 0x8:
151            Stream_write_string(f, "\\b");
152            break;
153          case 0x9:
154            Stream_write_string(f, "\\t");
155            break;
156          case 0xa:
157            Stream_write_string(f, "\\n");
158            break;
159          /* IE doesn't support this */
160          /*
161          case 0xb:
162            Stream_write_string(f, "\\v");
163            break;
164          */
165          case 0xc:
166            Stream_write_string(f, "\\f");
167            break;
168          case 0xd:
169            Stream_write_string(f, "\\r");
170            break;
171          default:
172            Stream_printf(f, "\\u%04x", c);
173            break;
174          }
175        }
176    }    }
177  }  }
178    
# Line 87  Line 182 
182    print_string(s, f);    print_string(s, f);
183  }  }
184    
185  static void print_string_jsval(jsval value, Stream * f) {  static void print_regex(jsval value, Stream * f) {
186    assert(JSVAL_IS_STRING(value));    assert(JSVAL_IS_STRING(value));
187    JSString * s = JSVAL_TO_STRING(value);    JSString * s = JSVAL_TO_STRING(value);
188    print_string(s, f);    size_t length = JSSTRING_LENGTH(s);
189      jschar * characters = JSSTRING_CHARS(s);
190      for (size_t i = 0; i < length; i++) {
191        jschar c = characters[i];
192        if (32 <= c && c <= 126) {
193          Stream_write_char(f, c);
194        }
195        else {
196          Stream_printf(f, "\\u%04x", c);
197        }
198      }
199  }  }
200    
201  static void print_quoted_string_atom(JSAtom * atom, Stream * f) {  static void print_quoted_string_atom(JSAtom * atom, Stream * f) {
202    assert(ATOM_IS_STRING(atom));    assert(ATOM_IS_STRING(atom));
203    JSString * s = ATOM_TO_STRING(atom);    JSString * s = ATOM_TO_STRING(atom);
204    JSString * quoted = js_QuoteString(context, s, '"');    Stream_write_char(f, '"');
205    print_string(quoted, f);    print_string(s, f);
206      Stream_write_char(f, '"');
207  }  }
208    
209  static const char * get_op(uint8 op) {  static const char * get_op(uint8 op) {
210    switch(op) {    switch(op) {
211      case JSOP_OR:
212        return "||";
213      case JSOP_AND:
214        return "&&";
215    case JSOP_BITOR:    case JSOP_BITOR:
216      return "|";      return "|";
217    case JSOP_BITXOR:    case JSOP_BITXOR:
# Line 112  Line 222 
222      return "==";      return "==";
223    case JSOP_NE:    case JSOP_NE:
224      return "!=";      return "!=";
225    case JSOP_NEW_EQ:    case JSOP_STRICTEQ:
226      return "===";      return "===";
227    case JSOP_NEW_NE:    case JSOP_STRICTNE:
228      return "!==";      return "!==";
229    case JSOP_LT:    case JSOP_LT:
230      return "<";      return "<";
# Line 146  Line 256 
256  }  }
257    
258  static void instrument_expression(JSParseNode * node, Stream * f);  static void instrument_expression(JSParseNode * node, Stream * f);
259  static void instrument_statement(JSParseNode * node, Stream * f, int indent);  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
260    static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
261    
262  enum FunctionType {  enum FunctionType {
263    FUNCTION_NORMAL,    FUNCTION_NORMAL,
# Line 154  Line 265 
265  };  };
266    
267  static void instrument_function(JSParseNode * node, Stream * f, int indent, enum FunctionType type) {  static void instrument_function(JSParseNode * node, Stream * f, int indent, enum FunctionType type) {
268      assert(node->pn_type == TOK_FUNCTION);
269    assert(node->pn_arity == PN_FUNC);    assert(node->pn_arity == PN_FUNC);
270    assert(ATOM_IS_OBJECT(node->pn_funAtom));    JSObject * object = node->pn_funpob->object;
   JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom);  
271    assert(JS_ObjectIsFunction(context, object));    assert(JS_ObjectIsFunction(context, object));
272    JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);    JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
273    assert(function);    assert(function);
274    assert(object == function->object);    assert(object == &function->object);
275    Stream_printf(f, "%*s", indent, "");    Stream_printf(f, "%*s", indent, "");
276    if (type == FUNCTION_NORMAL) {    if (type == FUNCTION_NORMAL) {
277      Stream_write_string(f, "function");      Stream_write_string(f, "function");
# Line 172  Line 283 
283      print_string_atom(function->atom, f);      print_string_atom(function->atom, f);
284    }    }
285    
286    /* function parameters */    /*
287      function parameters - see JS_DecompileFunction in jsapi.cpp, which calls
288      js_DecompileFunction in jsopcode.cpp
289      */
290    Stream_write_string(f, "(");    Stream_write_string(f, "(");
291    JSAtom ** params = xnew(JSAtom *, function->nargs);    JSArenaPool pool;
292    for (int i = 0; i < function->nargs; i++) {    JS_INIT_ARENA_POOL(&pool, "instrument_function", 256, 1, &context->scriptStackQuota);
293      /* initialize to NULL for sanity check */    jsuword * local_names = NULL;
294      params[i] = NULL;    if (JS_GET_LOCAL_NAME_COUNT(function)) {
295    }      local_names = js_GetLocalNameArray(context, function, &pool);
296    JSScope * scope = OBJ_SCOPE(object);      if (local_names == NULL) {
297    for (JSScopeProperty * scope_property = SCOPE_LAST_PROP(scope); scope_property != NULL; scope_property = scope_property->parent) {        fatal("out of memory");
     if (scope_property->getter != js_GetArgument) {  
       continue;  
298      }      }
     assert(scope_property->flags & SPROP_HAS_SHORTID);  
     assert((uint16) scope_property->shortid < function->nargs);  
     assert(JSID_IS_ATOM(scope_property->id));  
     params[(uint16) scope_property->shortid] = JSID_TO_ATOM(scope_property->id);  
299    }    }
300    for (int i = 0; i < function->nargs; i++) {    for (int i = 0; i < function->nargs; i++) {
     assert(params[i] != NULL);  
301      if (i > 0) {      if (i > 0) {
302        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
303      }      }
304      if (ATOM_IS_STRING(params[i])) {      JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]);
305        print_string_atom(params[i], f);      print_string_atom(param, f);
     }  
306    }    }
307      JS_FinishArenaPool(&pool);
308    Stream_write_string(f, ") {\n");    Stream_write_string(f, ") {\n");
   free(params);  
309    
310    /* function body */    /* function body */
311    instrument_statement(node->pn_body, f, indent + 2);    if (function->flags & JSFUN_EXPR_CLOSURE) {
312        /* expression closure */
313        output_statement(node->pn_body, f, indent + 2, false);
314      }
315      else {
316        instrument_statement(node->pn_body, f, indent + 2, false);
317      }
318    
319    Stream_write_string(f, "}\n");    Stream_write_string(f, "}\n");
320  }  }
# Line 219  Line 331 
331    Stream_write_char(f, ')');    Stream_write_char(f, ')');
332  }  }
333    
334    static void instrument_declarations(JSParseNode * list, Stream * f) {
335      assert(list->pn_arity == PN_LIST);
336      for (JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
337        switch (p->pn_type) {
338        case TOK_NAME:
339          assert(p->pn_arity == PN_NAME);
340          if (p != list->pn_head) {
341            Stream_write_string(f, ", ");
342          }
343          print_string_atom(p->pn_atom, f);
344          if (p->pn_expr != NULL) {
345            Stream_write_string(f, " = ");
346            instrument_expression(p->pn_expr, f);
347          }
348          break;
349        case TOK_ASSIGN:
350        case TOK_RB:
351        case TOK_RC:
352          /* destructuring */
353          instrument_expression(p, f);
354          break;
355        default:
356          abort();
357          break;
358        }
359      }
360    }
361    
362    static void output_for_in(JSParseNode * node, Stream * f) {
363      assert(node->pn_type == TOK_FOR);
364      assert(node->pn_arity == PN_BINARY);
365      Stream_write_string(f, "for ");
366      if (node->pn_iflags & JSITER_FOREACH) {
367        Stream_write_string(f, "each ");
368      }
369      Stream_write_char(f, '(');
370      instrument_expression(node->pn_left, f);
371      Stream_write_char(f, ')');
372    }
373    
374  /*  /*
375  See <Expressions> in jsparse.h.  See <Expressions> in jsparse.h.
376  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
# Line 277  Line 429 
429      instrument_expression(node->pn_kid3, f);      instrument_expression(node->pn_kid3, f);
430      break;      break;
431    case TOK_OR:    case TOK_OR:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " || ");  
     instrument_expression(node->pn_right, f);  
     break;  
432    case TOK_AND:    case TOK_AND:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " && ");  
     instrument_expression(node->pn_right, f);  
     break;  
433    case TOK_BITOR:    case TOK_BITOR:
434    case TOK_BITXOR:    case TOK_BITXOR:
435    case TOK_BITAND:    case TOK_BITAND:
# Line 398  Line 542 
542      assert(ATOM_IS_STRING(node->pn_atom));      assert(ATOM_IS_STRING(node->pn_atom));
543      {      {
544        JSString * s = ATOM_TO_STRING(node->pn_atom);        JSString * s = ATOM_TO_STRING(node->pn_atom);
545        /* XXX - semantics changed in 1.7 */        bool is_keyword = (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF);
546        if (! ATOM_KEYWORD(node->pn_atom) && js_IsIdentifier(s)) {        if (! is_keyword && js_IsIdentifier(s)) {
547          Stream_write_char(f, '.');          Stream_write_char(f, '.');
548          print_string_atom(node->pn_atom, f);          print_string_atom(node->pn_atom, f);
549        }        }
# Line 446  Line 590 
590        /* check whether this is a getter or setter */        /* check whether this is a getter or setter */
591        switch (p->pn_op) {        switch (p->pn_op) {
592        case JSOP_GETTER:        case JSOP_GETTER:
         Stream_write_string(f, "get ");  
         instrument_expression(p->pn_left, f);  
         instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);  
         break;  
593        case JSOP_SETTER:        case JSOP_SETTER:
594          Stream_write_string(f, "set ");          if (p->pn_op == JSOP_GETTER) {
595              Stream_write_string(f, "get ");
596            }
597            else {
598              Stream_write_string(f, "set ");
599            }
600          instrument_expression(p->pn_left, f);          instrument_expression(p->pn_left, f);
601            if (p->pn_right->pn_type != TOK_FUNCTION) {
602              fatal("parse error: expected function");
603            }
604          instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);          instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);
605          break;          break;
606        default:        default:
# Line 475  Line 623 
623    case TOK_STRING:    case TOK_STRING:
624      print_quoted_string_atom(node->pn_atom, f);      print_quoted_string_atom(node->pn_atom, f);
625      break;      break;
626    case TOK_OBJECT:    case TOK_REGEXP:
627      switch (node->pn_op) {      assert(node->pn_op == JSOP_REGEXP);
628      case JSOP_OBJECT:      {
629        /* I assume this is JSOP_REGEXP */        JSObject * object = node->pn_pob->object;
630        abort();        jsval result;
631        break;        js_regexp_toString(context, object, &result);
632      case JSOP_REGEXP:        print_regex(result, f);
       assert(ATOM_IS_OBJECT(node->pn_atom));  
       {  
         JSObject * object = ATOM_TO_OBJECT(node->pn_atom);  
         jsval result;  
         js_regexp_toString(context, object, 0, NULL, &result);  
         print_string_jsval(result, f);  
       }  
       break;  
     default:  
       abort();  
       break;  
633      }      }
634      break;      break;
635    case TOK_NUMBER:    case TOK_NUMBER:
# Line 538  Line 675 
675      Stream_write_string(f, " in ");      Stream_write_string(f, " in ");
676      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
677      break;      break;
678      case TOK_LEXICALSCOPE:
679        assert(node->pn_arity == PN_NAME);
680        assert(node->pn_expr->pn_type == TOK_LET);
681        assert(node->pn_expr->pn_arity == PN_BINARY);
682        Stream_write_string(f, "let(");
683        assert(node->pn_expr->pn_left->pn_type == TOK_LP);
684        assert(node->pn_expr->pn_left->pn_arity == PN_LIST);
685        instrument_declarations(node->pn_expr->pn_left, f);
686        Stream_write_string(f, ") ");
687        instrument_expression(node->pn_expr->pn_right, f);
688        break;
689      case TOK_YIELD:
690        assert(node->pn_arity == PN_UNARY);
691        Stream_write_string(f, "yield ");
692        instrument_expression(node->pn_kid, f);
693        break;
694      case TOK_ARRAYCOMP:
695        assert(node->pn_arity == PN_LIST);
696        {
697          JSParseNode * block_node;
698          switch (node->pn_count) {
699          case 1:
700            block_node = node->pn_head;
701            break;
702          case 2:
703            block_node = node->pn_head->pn_next;
704            break;
705          default:
706            abort();
707            break;
708          }
709          assert(block_node->pn_type == TOK_LEXICALSCOPE);
710          assert(block_node->pn_arity == PN_NAME);
711          JSParseNode * for_node = block_node->pn_expr;
712          assert(for_node->pn_type == TOK_FOR);
713          assert(for_node->pn_arity == PN_BINARY);
714          JSParseNode * push_node;
715          JSParseNode * if_node = NULL;
716          switch (for_node->pn_right->pn_type) {
717          case TOK_ARRAYPUSH:
718            push_node = for_node->pn_right;
719            assert(push_node->pn_arity == PN_UNARY);
720            break;
721          case TOK_IF:
722            if_node = for_node->pn_right;
723            assert(if_node->pn_arity == PN_TERNARY);
724            push_node = if_node->pn_kid2;
725            break;
726          default:
727            abort();
728            break;
729          }
730          Stream_write_char(f, '[');
731          instrument_expression(push_node->pn_kid, f);
732          Stream_write_char(f, ' ');
733          output_for_in(for_node, f);
734          if (if_node) {
735            Stream_write_string(f, " if (");
736            instrument_expression(if_node->pn_kid1, f);
737            Stream_write_char(f, ')');
738          }
739          Stream_write_char(f, ']');
740        }
741        break;
742      case TOK_VAR:
743        assert(node->pn_arity == PN_LIST);
744        Stream_write_string(f, "var ");
745        instrument_declarations(node, f);
746        break;
747      case TOK_LET:
748        assert(node->pn_arity == PN_LIST);
749        Stream_write_string(f, "let ");
750        instrument_declarations(node, f);
751        break;
752    default:    default:
753      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);
754    }    }
755  }  }
756    
757  static void instrument_var_statement(JSParseNode * node, Stream * f, int indent) {  static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
   assert(node->pn_arity == PN_LIST);  
   Stream_printf(f, "%*s", indent, "");  
   Stream_write_string(f, "var ");  
   for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) {  
     assert(p->pn_type == TOK_NAME);  
     assert(p->pn_arity == PN_NAME);  
     if (p != node->pn_head) {  
       Stream_write_string(f, ", ");  
     }  
     print_string_atom(p->pn_atom, f);  
     if (p->pn_expr != NULL) {  
       Stream_write_string(f, " = ");  
       instrument_expression(p->pn_expr, f);  
     }  
   }  
 }  
   
 static void output_statement(JSParseNode * node, Stream * f, int indent) {  
758    switch (node->pn_type) {    switch (node->pn_type) {
759    case TOK_FUNCTION:    case TOK_FUNCTION:
760      instrument_function(node, f, indent, FUNCTION_NORMAL);      instrument_function(node, f, indent, FUNCTION_NORMAL);
# Line 572  Line 765 
765      Stream_write_string(f, "{\n");      Stream_write_string(f, "{\n");
766  */  */
767      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) {
768        instrument_statement(p, f, indent);        instrument_statement(p, f, indent, false);
769      }      }
770  /*  /*
771      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 580  Line 773 
773  */  */
774      break;      break;
775    case TOK_IF:    case TOK_IF:
776      {
777      assert(node->pn_arity == PN_TERNARY);      assert(node->pn_arity == PN_TERNARY);
778    
779        uint16_t line = node->pn_pos.begin.lineno;
780        if (! is_jscoverage_if) {
781          if (line > num_lines) {
782            fatal("%s: script contains more than 65,535 lines", file_id);
783          }
784          if (line >= 2 && exclusive_directives[line - 2]) {
785            is_jscoverage_if = true;
786          }
787        }
788    
789      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
790      Stream_write_string(f, "if (");      Stream_write_string(f, "if (");
791      instrument_expression(node->pn_kid1, f);      instrument_expression(node->pn_kid1, f);
792      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
793      instrument_statement(node->pn_kid2, f, indent + 2);      if (is_jscoverage_if && node->pn_kid3) {
794          uint16_t else_start = node->pn_kid3->pn_pos.begin.lineno;
795          uint16_t else_end = node->pn_kid3->pn_pos.end.lineno + 1;
796          Stream_printf(f, "%*s", indent + 2, "");
797          Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, else_start, else_end);
798        }
799        instrument_statement(node->pn_kid2, f, indent + 2, false);
800      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
801      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
802      if (node->pn_kid3) {  
803        if (node->pn_kid3 || is_jscoverage_if) {
804        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
805        Stream_write_string(f, "else {\n");        Stream_write_string(f, "else {\n");
806        instrument_statement(node->pn_kid3, f, indent + 2);  
807          if (is_jscoverage_if) {
808            uint16_t if_start = node->pn_kid2->pn_pos.begin.lineno + 1;
809            uint16_t if_end = node->pn_kid2->pn_pos.end.lineno + 1;
810            Stream_printf(f, "%*s", indent + 2, "");
811            Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_start, if_end);
812          }
813    
814          if (node->pn_kid3) {
815            instrument_statement(node->pn_kid3, f, indent + 2, is_jscoverage_if);
816          }
817    
818        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
819        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
820      }      }
821    
822      break;      break;
823      }
824    case TOK_SWITCH:    case TOK_SWITCH:
825      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
826      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 617  Line 842 
842          abort();          abort();
843          break;          break;
844        }        }
845        instrument_statement(p->pn_right, f, indent + 2);        instrument_statement(p->pn_right, f, indent + 2, false);
846      }      }
847      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
848      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
# Line 632  Line 857 
857      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
858      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
859      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
860      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
861      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
862      break;      break;
863    case TOK_DO:    case TOK_DO:
864      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
865      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
866      Stream_write_string(f, "do {\n");      Stream_write_string(f, "do {\n");
867      instrument_statement(node->pn_left, f, indent + 2);      instrument_statement(node->pn_left, f, indent + 2, false);
868      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
869      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
870      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
# Line 649  Line 874 
874    case TOK_FOR:    case TOK_FOR:
875      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
876      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
     Stream_write_string(f, "for (");  
877      switch (node->pn_left->pn_type) {      switch (node->pn_left->pn_type) {
878      case TOK_IN:      case TOK_IN:
879        /* for/in */        /* for/in */
880        assert(node->pn_left->pn_arity == PN_BINARY);        assert(node->pn_left->pn_arity == PN_BINARY);
881        switch (node->pn_left->pn_left->pn_type) {        output_for_in(node, f);
       case TOK_VAR:  
         instrument_var_statement(node->pn_left->pn_left, f, 0);  
         break;  
       case TOK_NAME:  
         instrument_expression(node->pn_left->pn_left, f);  
         break;  
       default:  
         /* this is undocumented: for (x.value in y) */  
         instrument_expression(node->pn_left->pn_left, f);  
         break;  
 /*  
       default:  
         fprintf(stderr, "unexpected node type: %d\n", node->pn_left->pn_left->pn_type);  
         abort();  
         break;  
 */  
       }  
       Stream_write_string(f, " in ");  
       instrument_expression(node->pn_left->pn_right, f);  
882        break;        break;
883      case TOK_RESERVED:      case TOK_RESERVED:
884        /* for (;;) */        /* for (;;) */
885        assert(node->pn_left->pn_arity == PN_TERNARY);        assert(node->pn_left->pn_arity == PN_TERNARY);
886          Stream_write_string(f, "for (");
887        if (node->pn_left->pn_kid1) {        if (node->pn_left->pn_kid1) {
888          if (node->pn_left->pn_kid1->pn_type == TOK_VAR) {          instrument_expression(node->pn_left->pn_kid1, f);
           instrument_var_statement(node->pn_left->pn_kid1, f, 0);  
         }  
         else {  
           instrument_expression(node->pn_left->pn_kid1, f);  
         }  
889        }        }
890        Stream_write_string(f, ";");        Stream_write_string(f, ";");
891        if (node->pn_left->pn_kid2) {        if (node->pn_left->pn_kid2) {
# Line 696  Line 897 
897          Stream_write_char(f, ' ');          Stream_write_char(f, ' ');
898          instrument_expression(node->pn_left->pn_kid3, f);          instrument_expression(node->pn_left->pn_kid3, f);
899        }        }
900          Stream_write_char(f, ')');
901        break;        break;
902      default:      default:
903        abort();        abort();
904        break;        break;
905      }      }
906      Stream_write_string(f, ") {\n");      Stream_write_string(f, " {\n");
907      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
908      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
909      break;      break;
910    case TOK_THROW:    case TOK_THROW:
# Line 715  Line 917 
917    case TOK_TRY:    case TOK_TRY:
918      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
919      Stream_write_string(f, "try {\n");      Stream_write_string(f, "try {\n");
920      instrument_statement(node->pn_kid1, f, indent + 2);      instrument_statement(node->pn_kid1, f, indent + 2, false);
921      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
922      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
923      {      if (node->pn_kid2) {
924        for (JSParseNode * catch = node->pn_kid2; catch != NULL; catch = catch->pn_kid2) {        assert(node->pn_kid2->pn_type == TOK_RESERVED);
925          for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) {
926            assert(scope->pn_type == TOK_LEXICALSCOPE);
927            JSParseNode * catch = scope->pn_expr;
928          assert(catch->pn_type == TOK_CATCH);          assert(catch->pn_type == TOK_CATCH);
929          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
930          Stream_write_string(f, "catch (");          Stream_write_string(f, "catch (");
931            /* this may not be a name - destructuring assignment */
932            /*
933          assert(catch->pn_kid1->pn_arity == PN_NAME);          assert(catch->pn_kid1->pn_arity == PN_NAME);
934          print_string_atom(catch->pn_kid1->pn_atom, f);          print_string_atom(catch->pn_kid1->pn_atom, f);
935          if (catch->pn_kid1->pn_expr) {          */
936            instrument_expression(catch->pn_kid1, f);
937            if (catch->pn_kid2) {
938            Stream_write_string(f, " if ");            Stream_write_string(f, " if ");
939            instrument_expression(catch->pn_kid1->pn_expr, f);            instrument_expression(catch->pn_kid2, f);
940          }          }
941          Stream_write_string(f, ") {\n");          Stream_write_string(f, ") {\n");
942          instrument_statement(catch->pn_kid3, f, indent + 2);          instrument_statement(catch->pn_kid3, f, indent + 2, false);
943          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
944          Stream_write_string(f, "}\n");          Stream_write_string(f, "}\n");
945        }        }
# Line 738  Line 947 
947      if (node->pn_kid3) {      if (node->pn_kid3) {
948        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
949        Stream_write_string(f, "finally {\n");        Stream_write_string(f, "finally {\n");
950        instrument_statement(node->pn_kid3, f, indent + 2);        instrument_statement(node->pn_kid3, f, indent + 2, false);
951        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
952        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
953      }      }
# Line 764  Line 973 
973      Stream_write_string(f, "with (");      Stream_write_string(f, "with (");
974      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
975      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
976      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
977      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
978      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
979      break;      break;
980    case TOK_VAR:    case TOK_VAR:
981      instrument_var_statement(node, f, indent);      Stream_printf(f, "%*s", indent, "");
982        instrument_expression(node, f);
983      Stream_write_string(f, ";\n");      Stream_write_string(f, ";\n");
984      break;      break;
985    case TOK_RETURN:    case TOK_RETURN:
# Line 802  Line 1012 
1012      /*      /*
1013      ... use output_statement instead of instrument_statement.      ... use output_statement instead of instrument_statement.
1014      */      */
1015      output_statement(node->pn_expr, f, indent);      output_statement(node->pn_expr, f, indent, false);
1016        break;
1017      case TOK_LEXICALSCOPE:
1018        /* let statement */
1019        assert(node->pn_arity == PN_NAME);
1020        switch (node->pn_expr->pn_type) {
1021        case TOK_LET:
1022          /* let statement */
1023          assert(node->pn_expr->pn_arity == PN_BINARY);
1024          instrument_statement(node->pn_expr, f, indent, false);
1025          break;
1026        case TOK_LC:
1027          /* block */
1028          Stream_printf(f, "%*s", indent, "");
1029          Stream_write_string(f, "{\n");
1030          instrument_statement(node->pn_expr, f, indent + 2, false);
1031          Stream_printf(f, "%*s", indent, "");
1032          Stream_write_string(f, "}\n");
1033          break;
1034        case TOK_FOR:
1035          instrument_statement(node->pn_expr, f, indent, false);
1036          break;
1037        default:
1038          abort();
1039          break;
1040        }
1041        break;
1042      case TOK_LET:
1043        switch (node->pn_arity) {
1044        case PN_BINARY:
1045          /* let statement */
1046          Stream_printf(f, "%*s", indent, "");
1047          Stream_write_string(f, "let (");
1048          assert(node->pn_left->pn_type == TOK_LP);
1049          assert(node->pn_left->pn_arity == PN_LIST);
1050          instrument_declarations(node->pn_left, f);
1051          Stream_write_string(f, ") {\n");
1052          instrument_statement(node->pn_right, f, indent + 2, false);
1053          Stream_printf(f, "%*s", indent, "");
1054          Stream_write_string(f, "}\n");
1055          break;
1056        case PN_LIST:
1057          /* let definition */
1058          Stream_printf(f, "%*s", indent, "");
1059          instrument_expression(node, f);
1060          Stream_write_string(f, ";\n");
1061          break;
1062        default:
1063          abort();
1064          break;
1065        }
1066      break;      break;
1067    default:    default:
1068      fatal("unsupported node type in file %s: %d", file_id, node->pn_type);      fatal("unsupported node type in file %s: %d", file_id, node->pn_type);
# Line 814  Line 1074 
1074  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
1075  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
1076  */  */
1077  static void instrument_statement(JSParseNode * node, Stream * f, int indent) {  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
1078    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC && node->pn_type != TOK_LEXICALSCOPE) {
1079      int line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
1080        if (line > num_lines) {
1081          fatal("%s: script contains more than 65,535 lines", file_id);
1082        }
1083    
1084      /* the root node has line number 0 */      /* the root node has line number 0 */
1085      if (line != 0) {      if (line != 0) {
1086        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
# Line 824  Line 1088 
1088        lines[line - 1] = 1;        lines[line - 1] = 1;
1089      }      }
1090    }    }
1091    output_statement(node, f, indent);    output_statement(node, f, indent, is_jscoverage_if);
1092  }  }
1093    
1094  void jscoverage_instrument_js(const char * id, Stream * input, Stream * output) {  static bool characters_start_with(const jschar * characters, size_t line_start, size_t line_end, const char * prefix) {
1095    file_id = id;    const jschar * characters_end = characters + line_end;
1096      const jschar * cp = characters + line_start;
1097    /* scan the javascript */    const char * bp = prefix;
1098    size_t input_length = input->length;    for (;;) {
1099    jschar * base = js_InflateString(context, (char *) input->data, &input_length);      if (*bp == '\0') {
1100    if (base == NULL) {        return true;
1101      fatal("out of memory");      }
1102        else if (cp == characters_end) {
1103          return false;
1104        }
1105        else if (*cp != *bp) {
1106          return false;
1107        }
1108        bp++;
1109        cp++;
1110    }    }
1111    JSTokenStream * token_stream = js_NewTokenStream(context, base, input_length, NULL, 1, NULL);  }
1112    if (token_stream == NULL) {  
1113      fatal("cannot create token stream from file: %s", file_id);  static bool characters_are_white_space(const jschar * characters, size_t line_start, size_t line_end) {
1114      /* XXX - other Unicode space */
1115      const jschar * end = characters + line_end;
1116      for (const jschar * p = characters + line_start; p < end; p++) {
1117        jschar c = *p;
1118        if (c == 0x9 || c == 0xB || c == 0xC || c == 0x20 || c == 0xA0) {
1119          continue;
1120        }
1121        else {
1122          return false;
1123        }
1124    }    }
1125      return true;
1126    }
1127    
1128    static void error_reporter(JSContext * context, const char * message, JSErrorReport * report) {
1129      fprintf(stderr, "jscoverage: parse error: line %u: %s\n", report->lineno, message);
1130    }
1131    
1132    void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) {
1133      file_id = id;
1134    
1135    /* parse the javascript */    /* parse the javascript */
1136    JSParseNode * node = js_ParseTokenStream(context, global, token_stream);    JSParseContext parse_context;
1137      if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) {
1138        fatal("cannot create token stream from file: %s", file_id);
1139      }
1140      JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);
1141      JSParseNode * node = js_ParseScript(context, global, &parse_context);
1142    if (node == NULL) {    if (node == NULL) {
1143        js_ReportUncaughtException(context);
1144      fatal("parse error in file: %s", file_id);      fatal("parse error in file: %s", file_id);
1145    }    }
1146    int num_lines = node->pn_pos.end.lineno;    JS_SetErrorReporter(context, old_error_reporter);
1147      num_lines = node->pn_pos.end.lineno;
1148    lines = xmalloc(num_lines);    lines = xmalloc(num_lines);
1149    for (int i = 0; i < num_lines; i++) {    for (unsigned int i = 0; i < num_lines; i++) {
1150      lines[i] = 0;      lines[i] = 0;
1151    }    }
1152    
1153      /* search code for conditionals */
1154      exclusive_directives = xnew(bool, num_lines);
1155      for (unsigned int i = 0; i < num_lines; i++) {
1156        exclusive_directives[i] = false;
1157      }
1158    
1159      bool has_conditionals = false;
1160      struct IfDirective * if_directives = NULL;
1161      size_t line_number = 0;
1162      size_t i = 0;
1163      while (i < num_characters) {
1164        if (line_number == UINT16_MAX) {
1165          fatal("%s: script has more than 65,535 lines", file_id);
1166        }
1167        line_number++;
1168        size_t line_start = i;
1169        jschar c;
1170        bool done = false;
1171        while (! done && i < num_characters) {
1172          c = characters[i];
1173          switch (c) {
1174          case '\r':
1175          case '\n':
1176          case 0x2028:
1177          case 0x2029:
1178            done = true;
1179            break;
1180          default:
1181            i++;
1182          }
1183        }
1184        size_t line_end = i;
1185        if (i < num_characters) {
1186          i++;
1187          if (c == '\r' && i < num_characters && characters[i] == '\n') {
1188            i++;
1189          }
1190        }
1191    
1192        if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) {
1193          has_conditionals = true;
1194    
1195          if (characters_are_white_space(characters, line_start + 16, line_end)) {
1196            exclusive_directives[line_number - 1] = true;
1197          }
1198          else {
1199            struct IfDirective * if_directive = xnew(struct IfDirective, 1);
1200            if_directive->condition_start = characters + line_start + 16;
1201            if_directive->condition_end = characters + line_end;
1202            if_directive->start_line = line_number;
1203            if_directive->end_line = 0;
1204            if_directive->next = if_directives;
1205            if_directives = if_directive;
1206          }
1207        }
1208        else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) {
1209          for (struct IfDirective * p = if_directives; p != NULL; p = p->next) {
1210            if (p->end_line == 0) {
1211              p->end_line = line_number;
1212              break;
1213            }
1214          }
1215        }
1216      }
1217    
1218    /*    /*
1219    An instrumented JavaScript file has 3 sections:    An instrumented JavaScript file has 4 sections:
1220    1. initialization    1. initialization
1221    2. instrumented source code    2. instrumented source code
1222    3. original source code    3. conditionals
1223      4. original source code
1224    */    */
1225    
1226    Stream * instrumented = Stream_new(0);    Stream * instrumented = Stream_new(0);
1227    instrument_statement(node, instrumented, 0);    instrument_statement(node, instrumented, 0, false);
1228      js_FinishParseContext(context, &parse_context);
1229    
1230    /* write line number info to the output */    /* write line number info to the output */
1231    Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");    Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");
# Line 876  Line 1241 
1241    Stream_write_string(output, "}\n");    Stream_write_string(output, "}\n");
1242    free(lines);    free(lines);
1243    lines = NULL;    lines = NULL;
1244      free(exclusive_directives);
1245      exclusive_directives = NULL;
1246    
1247      /* conditionals */
1248      if (has_conditionals) {
1249        Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);
1250      }
1251    
1252    /* copy the instrumented source code to the output */    /* copy the instrumented source code to the output */
1253    Stream_write(output, instrumented->data, instrumented->length);    Stream_write(output, instrumented->data, instrumented->length);
   Stream_write_char(output, '\n');  
1254    
1255    /* conditionals */    /* conditionals */
1256    bool has_conditionals = false;    for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) {
1257    size_t line_number = 0;      Stream_write_string(output, "if (!(");
1258    size_t i = 0;      print_javascript(if_directive->condition_start, if_directive->condition_end - if_directive->condition_start, output);
1259    while (i < input_length) {      Stream_write_string(output, ")) {\n");
1260      line_number++;      Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line);
1261      size_t line_start = i;      Stream_write_string(output, "}\n");
1262      while (i < input_length && base[i] != '\r' && base[i] != '\n') {    }
1263        i++;  
1264      }    /* free */
1265      size_t line_end = i;    while (if_directives != NULL) {
1266      if (i < input_length) {      struct IfDirective * if_directive = if_directives;
1267        if (base[i] == '\r') {      if_directives = if_directives->next;
1268          line_end = i;      free(if_directive);
         i++;  
         if (i < input_length && base[i] == '\n') {  
           i++;  
         }  
       }  
       else if (base[i] == '\n') {  
         line_end = i;  
         i++;  
       }  
       else {  
         abort();  
       }  
     }  
     char * line = js_DeflateString(context, base + line_start, line_end - line_start);  
     if (str_starts_with(line, "//#JSCOVERAGE_IF")) {  
       if (! has_conditionals) {  
         has_conditionals = true;  
         Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);  
       }  
       Stream_printf(output, "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);  
1269    }    }
1270    
1271    /* copy the original source to the output */    /* copy the original source to the output */
1272    i = 0;    Stream_printf(output, "_$jscoverage['%s'].source = ", file_id);
1273    while (i < input_length) {    jscoverage_write_source(id, characters, num_characters, output);
1274      Stream_write_string(output, "// ");    Stream_printf(output, ";\n");
     size_t line_start = i;  
     while (i < input_length && base[i] != '\r' && base[i] != '\n') {  
       i++;  
     }  
1275    
1276      size_t line_end = i;    Stream_delete(instrumented);
1277      if (i < input_length) {  
1278        if (base[i] == '\r') {    file_id = NULL;
1279          line_end = i;  }
1280    
1281    void jscoverage_write_source(const char * id, const jschar * characters, size_t num_characters, Stream * output) {
1282      Stream_write_string(output, "[");
1283      if (jscoverage_highlight) {
1284        Stream * highlighted_stream = Stream_new(num_characters);
1285        jscoverage_highlight_js(context, id, characters, num_characters, highlighted_stream);
1286        size_t i = 0;
1287        while (i < highlighted_stream->length) {
1288          if (i > 0) {
1289            Stream_write_char(output, ',');
1290          }
1291    
1292          Stream_write_char(output, '"');
1293          bool done = false;
1294          while (! done) {
1295            char c = highlighted_stream->data[i];
1296            switch (c) {
1297            case 0x8:
1298              /* backspace */
1299              Stream_write_string(output, "\\b");
1300              break;
1301            case 0x9:
1302              /* horizontal tab */
1303              Stream_write_string(output, "\\t");
1304              break;
1305            case 0xa:
1306              /* line feed (new line) */
1307              done = true;
1308              break;
1309            /* IE doesn't support this */
1310            /*
1311            case 0xb:
1312              Stream_write_string(output, "\\v");
1313              break;
1314            */
1315            case 0xc:
1316              /* form feed */
1317              Stream_write_string(output, "\\f");
1318              break;
1319            case 0xd:
1320              /* carriage return */
1321              done = true;
1322              if (i + 1 < highlighted_stream->length && highlighted_stream->data[i + 1] == '\n') {
1323                i++;
1324              }
1325              break;
1326            case '"':
1327              Stream_write_string(output, "\\\"");
1328              break;
1329            case '\\':
1330              Stream_write_string(output, "\\\\");
1331              break;
1332            default:
1333              Stream_write_char(output, c);
1334              break;
1335            }
1336          i++;          i++;
1337          if (i < input_length && base[i] == '\n') {          if (i >= highlighted_stream->length) {
1338            i++;            done = true;
1339          }          }
1340        }        }
1341        else if (base[i] == '\n') {        Stream_write_char(output, '"');
1342          line_end = i;      }
1343        Stream_delete(highlighted_stream);
1344      }
1345      else {
1346        size_t i = 0;
1347        while (i < num_characters) {
1348          if (i > 0) {
1349            Stream_write_char(output, ',');
1350          }
1351    
1352          Stream_write_char(output, '"');
1353          bool done = false;
1354          while (! done) {
1355            jschar c = characters[i];
1356            switch (c) {
1357            case 0x8:
1358              /* backspace */
1359              Stream_write_string(output, "\\b");
1360              break;
1361            case 0x9:
1362              /* horizontal tab */
1363              Stream_write_string(output, "\\t");
1364              break;
1365            case 0xa:
1366              /* line feed (new line) */
1367              done = true;
1368              break;
1369            /* IE doesn't support this */
1370            /*
1371            case 0xb:
1372              Stream_write_string(output, "\\v");
1373              break;
1374            */
1375            case 0xc:
1376              /* form feed */
1377              Stream_write_string(output, "\\f");
1378              break;
1379            case 0xd:
1380              /* carriage return */
1381              done = true;
1382              if (i + 1 < num_characters && characters[i + 1] == '\n') {
1383                i++;
1384              }
1385              break;
1386            case '"':
1387              Stream_write_string(output, "\\\"");
1388              break;
1389            case '\\':
1390              Stream_write_string(output, "\\\\");
1391              break;
1392            case '&':
1393              Stream_write_string(output, "&amp;");
1394              break;
1395            case '<':
1396              Stream_write_string(output, "&lt;");
1397              break;
1398            case '>':
1399              Stream_write_string(output, "&gt;");
1400              break;
1401            case 0x2028:
1402            case 0x2029:
1403              done = true;
1404              break;
1405            default:
1406              if (32 <= c && c <= 126) {
1407                Stream_write_char(output, c);
1408              }
1409              else {
1410                Stream_printf(output, "&#%d;", c);
1411              }
1412              break;
1413            }
1414          i++;          i++;
1415            if (i >= num_characters) {
1416              done = true;
1417            }
1418        }        }
1419        else {        Stream_write_char(output, '"');
         abort();  
       }  
1420      }      }
   
     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);  
1421    }    }
1422      Stream_write_string(output, "]");
   Stream_delete(instrumented);  
   
   JS_free(context, base);  
   
   file_id = NULL;  
1423  }  }
1424    
1425  void jscoverage_copy_resources(const char * destination_directory) {  void jscoverage_copy_resources(const char * destination_directory) {
# Line 970  Line 1428 
1428    copy_resource("jscoverage.js", destination_directory);    copy_resource("jscoverage.js", destination_directory);
1429    copy_resource("jscoverage-ie.css", destination_directory);    copy_resource("jscoverage-ie.css", destination_directory);
1430    copy_resource("jscoverage-throbber.gif", destination_directory);    copy_resource("jscoverage-throbber.gif", destination_directory);
1431    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);  
1432  }  }
1433    
1434  /*  /*
# Line 1007  Line 1463 
1463    JS_HashTableDestroy(coverage->coverage_table);    JS_HashTableDestroy(coverage->coverage_table);
1464    struct FileCoverageList * p = coverage->coverage_list;    struct FileCoverageList * p = coverage->coverage_list;
1465    while (p != NULL) {    while (p != NULL) {
1466      free(p->file_coverage->lines);      free(p->file_coverage->coverage_lines);
1467      free(p->file_coverage->source);      if (p->file_coverage->source_lines != NULL) {
1468          for (uint32 i = 0; i < p->file_coverage->num_source_lines; i++) {
1469            free(p->file_coverage->source_lines[i]);
1470          }
1471          free(p->file_coverage->source_lines);
1472        }
1473      free(p->file_coverage->id);      free(p->file_coverage->id);
1474      free(p->file_coverage);      free(p->file_coverage);
1475      struct FileCoverageList * q = p;      struct FileCoverageList * q = p;
# Line 1037  Line 1498 
1498  }  }
1499    
1500  int jscoverage_parse_json(Coverage * coverage, const uint8_t * json, size_t length) {  int jscoverage_parse_json(Coverage * coverage, const uint8_t * json, size_t length) {
1501      int result = 0;
1502    
1503    jschar * base = js_InflateString(context, (char *) json, &length);    jschar * base = js_InflateString(context, (char *) json, &length);
1504    if (base == NULL) {    if (base == NULL) {
1505      fatal("out of memory");      fatal("out of memory");
# Line 1049  Line 1512 
1512    
1513    JS_free(context, base);    JS_free(context, base);
1514    
1515    JSTokenStream * token_stream = js_NewTokenStream(context, parenthesized_json, length + 2, NULL, 1, NULL);    JSParseContext parse_context;
1516    if (token_stream == NULL) {    if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) {
1517      fatal("cannot create token stream");      free(parenthesized_json);
1518        return -1;
1519    }    }
1520      JSParseNode * root = js_ParseScript(context, global, &parse_context);
   JSParseNode * root = js_ParseTokenStream(context, global, token_stream);  
1521    free(parenthesized_json);    free(parenthesized_json);
1522    if (root == NULL) {    if (root == NULL) {
1523      return -1;      result = -1;
1524        goto done;
1525    }    }
1526    
1527    /* root node must be TOK_LC */    /* root node must be TOK_LC */
1528    if (root->pn_type != TOK_LC) {    if (root->pn_type != TOK_LC) {
1529      return -1;      result = -1;
1530        goto done;
1531    }    }
1532    JSParseNode * semi = root->pn_u.list.head;    JSParseNode * semi = root->pn_u.list.head;
1533    
1534    /* the list must be TOK_SEMI and it must contain only one element */    /* the list must be TOK_SEMI and it must contain only one element */
1535    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {
1536      return -1;      result = -1;
1537        goto done;
1538    }    }
1539    JSParseNode * parenthesized = semi->pn_kid;    JSParseNode * parenthesized = semi->pn_kid;
1540    
1541    /* this must be a parenthesized expression */    /* this must be a parenthesized expression */
1542    if (parenthesized->pn_type != TOK_RP) {    if (parenthesized->pn_type != TOK_RP) {
1543      return -1;      result = -1;
1544        goto done;
1545    }    }
1546    JSParseNode * object = parenthesized->pn_kid;    JSParseNode * object = parenthesized->pn_kid;
1547    
1548    /* this must be an object literal */    /* this must be an object literal */
1549    if (object->pn_type != TOK_RC) {    if (object->pn_type != TOK_RC) {
1550      return -1;      result = -1;
1551        goto done;
1552    }    }
1553    
1554    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {
1555      /* every element of this list must be TOK_COLON */      /* every element of this list must be TOK_COLON */
1556      if (p->pn_type != TOK_COLON) {      if (p->pn_type != TOK_COLON) {
1557        return -1;        result = -1;
1558          goto done;
1559      }      }
1560    
1561      /* the key must be a string representing the file */      /* the key must be a string representing the file */
1562      JSParseNode * key = p->pn_left;      JSParseNode * key = p->pn_left;
1563      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {
1564        return -1;        result = -1;
1565          goto done;
1566      }      }
1567      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));
1568    
1569      /* the value must be an object literal OR an array */      /* the value must be an object literal OR an array */
1570      JSParseNode * value = p->pn_right;      JSParseNode * value = p->pn_right;
1571      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {
1572        return -1;        result = -1;
1573          goto done;
1574      }      }
1575    
1576      JSParseNode * array = NULL;      JSParseNode * array = NULL;
# Line 1111  Line 1582 
1582      else if (value->pn_type == TOK_RC) {      else if (value->pn_type == TOK_RC) {
1583        /* an object literal */        /* an object literal */
1584        if (value->pn_count != 2) {        if (value->pn_count != 2) {
1585          return -1;          result = -1;
1586            goto done;
1587        }        }
1588        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {
1589          if (element->pn_type != TOK_COLON) {          if (element->pn_type != TOK_COLON) {
1590            return -1;            result = -1;
1591              goto done;
1592          }          }
1593          JSParseNode * left = element->pn_left;          JSParseNode * left = element->pn_left;
1594          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {
1595            return -1;            result = -1;
1596              goto done;
1597          }          }
1598          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));
1599          if (strcmp(s, "coverage") == 0) {          if (strcmp(s, "coverage") == 0) {
1600            array = element->pn_right;            array = element->pn_right;
1601            if (array->pn_type != TOK_RB) {            if (array->pn_type != TOK_RB) {
1602              return -1;              result = -1;
1603                goto done;
1604            }            }
1605          }          }
1606          else if (strcmp(s, "source") == 0) {          else if (strcmp(s, "source") == 0) {
1607            source = element->pn_right;            source = element->pn_right;
1608            if (source->pn_type != TOK_STRING || ! ATOM_IS_STRING(source->pn_atom)) {            if (source->pn_type != TOK_RB) {
1609              return -1;              result = -1;
1610                goto done;
1611            }            }
1612          }          }
1613          else {          else {
1614            return -1;            result = -1;
1615              goto done;
1616          }          }
1617        }        }
1618      }      }
1619      else {      else {
1620        return -1;        result = -1;
1621          goto done;
1622      }      }
1623    
1624      if (array == NULL) {      if (array == NULL) {
1625        return -1;        result = -1;
1626          goto done;
1627      }      }
1628    
1629      /* look up the file in the coverage table */      /* look up the file in the coverage table */
# Line 1154  Line 1633 
1633        char * id = xstrdup(id_bytes);        char * id = xstrdup(id_bytes);
1634        file_coverage = xmalloc(sizeof(FileCoverage));        file_coverage = xmalloc(sizeof(FileCoverage));
1635        file_coverage->id = id;        file_coverage->id = id;
1636        file_coverage->num_lines = array->pn_count - 1;        file_coverage->num_coverage_lines = array->pn_count;
1637        file_coverage->lines = xnew(int, array->pn_count);        file_coverage->coverage_lines = xnew(int, array->pn_count);
1638        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)));  
       }  
1639    
1640        /* set coverage for all lines */        /* set coverage for all lines */
1641        uint32 i = 0;        uint32 i = 0;
1642        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++) {
1643          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1644            file_coverage->lines[i] = (int) element->pn_dval;            file_coverage->coverage_lines[i] = (int) element->pn_dval;
1645          }          }
1646          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1647            file_coverage->lines[i] = -1;            file_coverage->coverage_lines[i] = -1;
1648          }          }
1649          else {          else {
1650            return -1;            result = -1;
1651              goto done;
1652          }          }
1653        }        }
1654        assert(i == array->pn_count);        assert(i == array->pn_count);
# Line 1188  Line 1663 
1663      else {      else {
1664        /* sanity check */        /* sanity check */
1665        assert(strcmp(file_coverage->id, id_bytes) == 0);        assert(strcmp(file_coverage->id, id_bytes) == 0);
1666        if (file_coverage->num_lines != array->pn_count - 1) {        if (file_coverage->num_coverage_lines != array->pn_count) {
1667          return -2;          result = -2;
1668            goto done;
1669        }        }
1670    
1671        /* merge the coverage */        /* merge the coverage */
1672        uint32 i = 0;        uint32 i = 0;
1673        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++) {
1674          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1675            if (file_coverage->lines[i] == -1) {            if (file_coverage->coverage_lines[i] == -1) {
1676              return -2;              result = -2;
1677                goto done;
1678            }            }
1679            file_coverage->lines[i] += (int) element->pn_dval;            file_coverage->coverage_lines[i] += (int) element->pn_dval;
1680          }          }
1681          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1682            if (file_coverage->lines[i] != -1) {            if (file_coverage->coverage_lines[i] != -1) {
1683              return -2;              result = -2;
1684                goto done;
1685            }            }
1686          }          }
1687          else {          else {
1688            return -1;            result = -1;
1689              goto done;
1690          }          }
1691        }        }
1692        assert(i == array->pn_count);        assert(i == array->pn_count);
1693        }
1694    
1695        /* if this JSON file has source, use it */      /* if this JSON file has source, use it */
1696        if (file_coverage->source == NULL && source != NULL) {      if (file_coverage->source_lines == NULL && source != NULL) {
1697          file_coverage->source = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(source->pn_atom)));        file_coverage->num_source_lines = source->pn_count;
1698          file_coverage->source_lines = xnew(char *, source->pn_count);
1699          uint32 i = 0;
1700          for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {
1701            if (element->pn_type != TOK_STRING) {
1702              result = -1;
1703              goto done;
1704            }
1705            file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));
1706        }        }
1707          assert(i == source->pn_count);
1708      }      }
1709    }    }
1710    
1711    return 0;  done:
1712      js_FinishParseContext(context, &parse_context);
1713      return result;
1714  }  }

Legend:
Removed from v.169  
changed lines
  Added in v.345

  ViewVC Help
Powered by ViewVC 1.1.24