/[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 157 by siliconforks, Sat Sep 13 03:56:45 2008 UTC revision 351 by siliconforks, Fri Oct 24 16:17:59 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 <jsemit.h>
32    #include <jsexn.h>
33  #include <jsfun.h>  #include <jsfun.h>
34  #include <jsinterp.h>  #include <jsinterp.h>
35    #include <jsiter.h>
36  #include <jsparse.h>  #include <jsparse.h>
37  #include <jsregexp.h>  #include <jsregexp.h>
38  #include <jsscope.h>  #include <jsscope.h>
39  #include <jsstr.h>  #include <jsstr.h>
40    
41    #include "encoding.h"
42    #include "global.h"
43    #include "highlight.h"
44  #include "resource-manager.h"  #include "resource-manager.h"
45  #include "util.h"  #include "util.h"
46    
47    struct IfDirective {
48      const jschar * condition_start;
49      const jschar * condition_end;
50      uint16_t start_line;
51      uint16_t end_line;
52      struct IfDirective * next;
53    };
54    
55    static bool * exclusive_directives = NULL;
56    
57  static JSRuntime * runtime = NULL;  static JSRuntime * runtime = NULL;
58  static JSContext * context = NULL;  static JSContext * context = NULL;
59  static JSObject * global = NULL;  static JSObject * global = NULL;
60    static JSVersion js_version = JSVERSION_ECMA_3;
61    
62  /*  /*
63  JSParseNode objects store line numbers starting from 1.  JSParseNode objects store line numbers starting from 1.
# Line 47  Line 65 
65  */  */
66  static const char * file_id = NULL;  static const char * file_id = NULL;
67  static char * lines = NULL;  static char * lines = NULL;
68    static uint16_t num_lines = 0;
69    
70    void jscoverage_set_js_version(const char * version) {
71      js_version = atoi(version);
72    }
73    
74  void jscoverage_init(void) {  void jscoverage_init(void) {
75    runtime = JS_NewRuntime(8L * 1024L * 1024L);    runtime = JS_NewRuntime(8L * 1024L * 1024L);
# Line 59  Line 82 
82      fatal("cannot create context");      fatal("cannot create context");
83    }    }
84    
85      JS_SetVersion(context, js_version);
86    
87    global = JS_NewObject(context, NULL, NULL, NULL);    global = JS_NewObject(context, NULL, NULL, NULL);
88    if (global == NULL) {    if (global == NULL) {
89      fatal("cannot create global object");      fatal("cannot create global object");
# Line 74  Line 99 
99    JS_DestroyRuntime(runtime);    JS_DestroyRuntime(runtime);
100  }  }
101    
102    static void print_javascript(const jschar * characters, size_t num_characters, Stream * f) {
103      for (size_t i = 0; i < num_characters; i++) {
104        jschar c = characters[i];
105        /*
106        XXX does not handle no-break space, other unicode "space separator"
107        */
108        switch (c) {
109        case 0x9:
110        case 0xB:
111        case 0xC:
112          Stream_write_char(f, c);
113          break;
114        default:
115          if (32 <= c && c <= 126) {
116            Stream_write_char(f, c);
117          }
118          else {
119            Stream_printf(f, "\\u%04x", c);
120          }
121          break;
122        }
123      }
124    }
125    
126  static void print_string(JSString * s, Stream * f) {  static void print_string(JSString * s, Stream * f) {
127    for (size_t i = 0; i < s->length; i++) {    size_t length = JSSTRING_LENGTH(s);
128      char c = s->chars[i];    jschar * characters = JSSTRING_CHARS(s);
129      Stream_write_char(f, c);    for (size_t i = 0; i < length; i++) {
130        jschar c = characters[i];
131        if (32 <= c && c <= 126) {
132          switch (c) {
133          case '"':
134            Stream_write_string(f, "\\\"");
135            break;
136    /*
137          case '\'':
138            Stream_write_string(f, "\\'");
139            break;
140    */
141          case '\\':
142            Stream_write_string(f, "\\\\");
143            break;
144          default:
145            Stream_write_char(f, c);
146            break;
147          }
148        }
149        else {
150          switch (c) {
151          case 0x8:
152            Stream_write_string(f, "\\b");
153            break;
154          case 0x9:
155            Stream_write_string(f, "\\t");
156            break;
157          case 0xa:
158            Stream_write_string(f, "\\n");
159            break;
160          /* IE doesn't support this */
161          /*
162          case 0xb:
163            Stream_write_string(f, "\\v");
164            break;
165          */
166          case 0xc:
167            Stream_write_string(f, "\\f");
168            break;
169          case 0xd:
170            Stream_write_string(f, "\\r");
171            break;
172          default:
173            Stream_printf(f, "\\u%04x", c);
174            break;
175          }
176        }
177    }    }
178  }  }
179    
# Line 87  Line 183 
183    print_string(s, f);    print_string(s, f);
184  }  }
185    
186  static void print_string_jsval(jsval value, Stream * f) {  static void print_regex(jsval value, Stream * f) {
187    assert(JSVAL_IS_STRING(value));    assert(JSVAL_IS_STRING(value));
188    JSString * s = JSVAL_TO_STRING(value);    JSString * s = JSVAL_TO_STRING(value);
189    print_string(s, f);    size_t length = JSSTRING_LENGTH(s);
190      jschar * characters = JSSTRING_CHARS(s);
191      for (size_t i = 0; i < length; i++) {
192        jschar c = characters[i];
193        if (32 <= c && c <= 126) {
194          Stream_write_char(f, c);
195        }
196        else {
197          Stream_printf(f, "\\u%04x", c);
198        }
199      }
200  }  }
201    
202  static void print_quoted_string_atom(JSAtom * atom, Stream * f) {  static void print_quoted_string_atom(JSAtom * atom, Stream * f) {
203    assert(ATOM_IS_STRING(atom));    assert(ATOM_IS_STRING(atom));
204    JSString * s = ATOM_TO_STRING(atom);    JSString * s = ATOM_TO_STRING(atom);
205    JSString * quoted = js_QuoteString(context, s, '"');    Stream_write_char(f, '"');
206    print_string(quoted, f);    print_string(s, f);
207      Stream_write_char(f, '"');
208  }  }
209    
210  static const char * get_op(uint8 op) {  static const char * get_op(uint8 op) {
211    switch(op) {    switch(op) {
212      case JSOP_OR:
213        return "||";
214      case JSOP_AND:
215        return "&&";
216    case JSOP_BITOR:    case JSOP_BITOR:
217      return "|";      return "|";
218    case JSOP_BITXOR:    case JSOP_BITXOR:
# Line 112  Line 223 
223      return "==";      return "==";
224    case JSOP_NE:    case JSOP_NE:
225      return "!=";      return "!=";
226    case JSOP_NEW_EQ:    case JSOP_STRICTEQ:
227      return "===";      return "===";
228    case JSOP_NEW_NE:    case JSOP_STRICTNE:
229      return "!==";      return "!==";
230    case JSOP_LT:    case JSOP_LT:
231      return "<";      return "<";
# Line 146  Line 257 
257  }  }
258    
259  static void instrument_expression(JSParseNode * node, Stream * f);  static void instrument_expression(JSParseNode * node, Stream * f);
260  static void instrument_statement(JSParseNode * node, Stream * f, int indent);  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
261    static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
262    
263  enum FunctionType {  enum FunctionType {
264    FUNCTION_NORMAL,    FUNCTION_NORMAL,
265    FUNCTION_GETTER_OR_SETTER    FUNCTION_GETTER_OR_SETTER
266  };  };
267    
268    static void output_for_in(JSParseNode * node, Stream * f) {
269      assert(node->pn_type == TOK_FOR);
270      assert(node->pn_arity == PN_BINARY);
271      Stream_write_string(f, "for ");
272      if (node->pn_iflags & JSITER_FOREACH) {
273        Stream_write_string(f, "each ");
274      }
275      Stream_write_char(f, '(');
276      instrument_expression(node->pn_left, f);
277      Stream_write_char(f, ')');
278    }
279    
280  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) {
281      assert(node->pn_type == TOK_FUNCTION);
282    assert(node->pn_arity == PN_FUNC);    assert(node->pn_arity == PN_FUNC);
283    assert(ATOM_IS_OBJECT(node->pn_funAtom));    JSObject * object = node->pn_funpob->object;
   JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom);  
284    assert(JS_ObjectIsFunction(context, object));    assert(JS_ObjectIsFunction(context, object));
285    JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);    JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
286    assert(function);    assert(function);
287    assert(object == function->object);    assert(object == &function->object);
288    Stream_printf(f, "%*s", indent, "");    Stream_printf(f, "%*s", indent, "");
289    if (type == FUNCTION_NORMAL) {    if (type == FUNCTION_NORMAL) {
290      Stream_write_string(f, "function");      Stream_write_string(f, "function");
# Line 172  Line 296 
296      print_string_atom(function->atom, f);      print_string_atom(function->atom, f);
297    }    }
298    
299    /* function parameters */    /*
300      function parameters - see JS_DecompileFunction in jsapi.cpp, which calls
301      js_DecompileFunction in jsopcode.cpp
302      */
303    Stream_write_string(f, "(");    Stream_write_string(f, "(");
304    JSAtom ** params = xnew(JSAtom *, function->nargs);    JSArenaPool pool;
305    for (int i = 0; i < function->nargs; i++) {    JS_INIT_ARENA_POOL(&pool, "instrument_function", 256, 1, &context->scriptStackQuota);
306      /* initialize to NULL for sanity check */    jsuword * local_names = NULL;
307      params[i] = NULL;    if (JS_GET_LOCAL_NAME_COUNT(function)) {
308    }      local_names = js_GetLocalNameArray(context, function, &pool);
309    JSScope * scope = OBJ_SCOPE(object);      if (local_names == NULL) {
310    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;  
311      }      }
     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);  
312    }    }
313    for (int i = 0; i < function->nargs; i++) {    for (int i = 0; i < function->nargs; i++) {
     assert(params[i] != NULL);  
314      if (i > 0) {      if (i > 0) {
315        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
316      }      }
317      if (ATOM_IS_STRING(params[i])) {      JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]);
318        print_string_atom(params[i], f);      print_string_atom(param, f);
     }  
319    }    }
320      JS_FinishArenaPool(&pool);
321    Stream_write_string(f, ") {\n");    Stream_write_string(f, ") {\n");
   free(params);  
322    
323    /* function body */    /* function body */
324    instrument_statement(node->pn_body, f, indent + 2);    if (function->flags & JSFUN_EXPR_CLOSURE) {
325        /* expression closure */
326        output_statement(node->pn_body, f, indent + 2, false);
327      }
328      else {
329        instrument_statement(node->pn_body, f, indent + 2, false);
330      }
331    
332    Stream_write_string(f, "}\n");    Stream_write_string(f, "}\n");
333  }  }
334    
335  static void instrument_function_call(JSParseNode * node, Stream * f) {  static void instrument_function_call(JSParseNode * node, Stream * f) {
336    instrument_expression(node->pn_head, f);    JSParseNode * function_node = node->pn_head;
337      if (function_node->pn_type == TOK_FUNCTION) {
338        JSObject * object = function_node->pn_funpob->object;
339        assert(JS_ObjectIsFunction(context, object));
340        JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
341        assert(function);
342        assert(object == &function->object);
343    
344        if (function_node->pn_flags & TCF_GENEXP_LAMBDA) {
345          /* it's a generator expression */
346          JSParseNode * lexical_scope_node = function_node->pn_body;
347          assert(lexical_scope_node->pn_type == TOK_LEXICALSCOPE);
348          assert(lexical_scope_node->pn_arity == PN_NAME);
349          JSParseNode * for_node = lexical_scope_node->pn_body;
350          assert(for_node->pn_type == TOK_FOR);
351          assert(for_node->pn_arity == PN_BINARY);
352          JSParseNode * if_node = NULL;
353          JSParseNode * semi_node;
354          switch (for_node->pn_right->pn_type) {
355          case TOK_SEMI:
356            semi_node = for_node->pn_right;
357            break;
358          case TOK_IF:
359            if_node = for_node->pn_right;
360            assert(if_node->pn_arity == PN_TERNARY);
361            semi_node = if_node->pn_kid2;
362            assert(semi_node->pn_type == TOK_SEMI);
363            break;
364          default:
365            abort();
366            break;
367          }
368          assert(semi_node->pn_arity == PN_UNARY);
369          JSParseNode * yield_node = semi_node->pn_kid;
370          assert(yield_node->pn_type == TOK_YIELD);
371          Stream_write_char(f, '(');
372          instrument_expression(yield_node->pn_kid, f);
373          Stream_write_char(f, ' ');
374          output_for_in(for_node, f);
375          if (if_node) {
376            Stream_write_string(f, " if (");
377            instrument_expression(if_node->pn_kid1, f);
378            Stream_write_char(f, ')');
379          }
380          Stream_write_char(f, ')');
381          return;
382        }
383        else {
384          Stream_write_char(f, '(');
385          instrument_expression(function_node, f);
386          Stream_write_char(f, ')');
387        }
388      }
389      else {
390        instrument_expression(function_node, f);
391      }
392    Stream_write_char(f, '(');    Stream_write_char(f, '(');
393    for (struct JSParseNode * p = node->pn_head->pn_next; p != NULL; p = p->pn_next) {    for (struct JSParseNode * p = function_node->pn_next; p != NULL; p = p->pn_next) {
394      if (p != node->pn_head->pn_next) {      if (p != node->pn_head->pn_next) {
395        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
396      }      }
# Line 219  Line 399 
399    Stream_write_char(f, ')');    Stream_write_char(f, ')');
400  }  }
401    
402    static void instrument_declarations(JSParseNode * list, Stream * f) {
403      assert(list->pn_arity == PN_LIST);
404      for (JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
405        switch (p->pn_type) {
406        case TOK_NAME:
407          assert(p->pn_arity == PN_NAME);
408          if (p != list->pn_head) {
409            Stream_write_string(f, ", ");
410          }
411          print_string_atom(p->pn_atom, f);
412          if (p->pn_expr != NULL) {
413            Stream_write_string(f, " = ");
414            instrument_expression(p->pn_expr, f);
415          }
416          break;
417        case TOK_ASSIGN:
418        case TOK_RB:
419        case TOK_RC:
420          /* destructuring */
421          instrument_expression(p, f);
422          break;
423        default:
424          abort();
425          break;
426        }
427      }
428    }
429    
430  /*  /*
431  See <Expressions> in jsparse.h.  See <Expressions> in jsparse.h.
432  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 485 
485      instrument_expression(node->pn_kid3, f);      instrument_expression(node->pn_kid3, f);
486      break;      break;
487    case TOK_OR:    case TOK_OR:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " || ");  
     instrument_expression(node->pn_right, f);  
     break;  
488    case TOK_AND:    case TOK_AND:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " && ");  
     instrument_expression(node->pn_right, f);  
     break;  
489    case TOK_BITOR:    case TOK_BITOR:
490    case TOK_BITXOR:    case TOK_BITXOR:
491    case TOK_BITAND:    case TOK_BITAND:
# Line 398  Line 598 
598      assert(ATOM_IS_STRING(node->pn_atom));      assert(ATOM_IS_STRING(node->pn_atom));
599      {      {
600        JSString * s = ATOM_TO_STRING(node->pn_atom);        JSString * s = ATOM_TO_STRING(node->pn_atom);
601        /* XXX - semantics changed in 1.7 */        bool must_quote;
602        if (! ATOM_KEYWORD(node->pn_atom) && js_IsIdentifier(s)) {        if (JSSTRING_LENGTH(s) == 0) {
603          Stream_write_char(f, '.');          must_quote = true;
604          print_string_atom(node->pn_atom, f);        }
605          else if (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF) {
606            must_quote = true;
607          }
608          else if (! js_IsIdentifier(s)) {
609            must_quote = true;
610        }        }
611        else {        else {
612            must_quote = false;
613          }
614          if (must_quote) {
615          Stream_write_char(f, '[');          Stream_write_char(f, '[');
616          print_quoted_string_atom(node->pn_atom, f);          print_quoted_string_atom(node->pn_atom, f);
617          Stream_write_char(f, ']');          Stream_write_char(f, ']');
618        }        }
619          else {
620            Stream_write_char(f, '.');
621            print_string_atom(node->pn_atom, f);
622          }
623      }      }
624      break;      break;
625    case TOK_LB:    case TOK_LB:
# Line 446  Line 658 
658        /* check whether this is a getter or setter */        /* check whether this is a getter or setter */
659        switch (p->pn_op) {        switch (p->pn_op) {
660        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;  
661        case JSOP_SETTER:        case JSOP_SETTER:
662          Stream_write_string(f, "set ");          if (p->pn_op == JSOP_GETTER) {
663              Stream_write_string(f, "get ");
664            }
665            else {
666              Stream_write_string(f, "set ");
667            }
668          instrument_expression(p->pn_left, f);          instrument_expression(p->pn_left, f);
669            if (p->pn_right->pn_type != TOK_FUNCTION) {
670              fatal("parse error: expected function");
671            }
672          instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);          instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);
673          break;          break;
674        default:        default:
# Line 475  Line 691 
691    case TOK_STRING:    case TOK_STRING:
692      print_quoted_string_atom(node->pn_atom, f);      print_quoted_string_atom(node->pn_atom, f);
693      break;      break;
694    case TOK_OBJECT:    case TOK_REGEXP:
695      switch (node->pn_op) {      assert(node->pn_op == JSOP_REGEXP);
696      case JSOP_OBJECT:      {
697        /* I assume this is JSOP_REGEXP */        JSObject * object = node->pn_pob->object;
698        abort();        jsval result;
699        break;        js_regexp_toString(context, object, &result);
700      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;  
701      }      }
702      break;      break;
703    case TOK_NUMBER:    case TOK_NUMBER:
# Line 538  Line 743 
743      Stream_write_string(f, " in ");      Stream_write_string(f, " in ");
744      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
745      break;      break;
746      case TOK_LEXICALSCOPE:
747        assert(node->pn_arity == PN_NAME);
748        assert(node->pn_expr->pn_type == TOK_LET);
749        assert(node->pn_expr->pn_arity == PN_BINARY);
750        Stream_write_string(f, "let(");
751        assert(node->pn_expr->pn_left->pn_type == TOK_LP);
752        assert(node->pn_expr->pn_left->pn_arity == PN_LIST);
753        instrument_declarations(node->pn_expr->pn_left, f);
754        Stream_write_string(f, ") ");
755        instrument_expression(node->pn_expr->pn_right, f);
756        break;
757      case TOK_YIELD:
758        assert(node->pn_arity == PN_UNARY);
759        Stream_write_string(f, "yield ");
760        instrument_expression(node->pn_kid, f);
761        break;
762      case TOK_ARRAYCOMP:
763        assert(node->pn_arity == PN_LIST);
764        {
765          JSParseNode * block_node;
766          switch (node->pn_count) {
767          case 1:
768            block_node = node->pn_head;
769            break;
770          case 2:
771            block_node = node->pn_head->pn_next;
772            break;
773          default:
774            abort();
775            break;
776          }
777          assert(block_node->pn_type == TOK_LEXICALSCOPE);
778          assert(block_node->pn_arity == PN_NAME);
779          JSParseNode * for_node = block_node->pn_expr;
780          assert(for_node->pn_type == TOK_FOR);
781          assert(for_node->pn_arity == PN_BINARY);
782          JSParseNode * if_node = NULL;
783          JSParseNode * push_node;
784          switch (for_node->pn_right->pn_type) {
785          case TOK_ARRAYPUSH:
786            push_node = for_node->pn_right;
787            assert(push_node->pn_arity == PN_UNARY);
788            break;
789          case TOK_IF:
790            if_node = for_node->pn_right;
791            assert(if_node->pn_arity == PN_TERNARY);
792            push_node = if_node->pn_kid2;
793            break;
794          default:
795            abort();
796            break;
797          }
798          Stream_write_char(f, '[');
799          instrument_expression(push_node->pn_kid, f);
800          Stream_write_char(f, ' ');
801          output_for_in(for_node, f);
802          if (if_node) {
803            Stream_write_string(f, " if (");
804            instrument_expression(if_node->pn_kid1, f);
805            Stream_write_char(f, ')');
806          }
807          Stream_write_char(f, ']');
808        }
809        break;
810      case TOK_VAR:
811        assert(node->pn_arity == PN_LIST);
812        Stream_write_string(f, "var ");
813        instrument_declarations(node, f);
814        break;
815      case TOK_LET:
816        assert(node->pn_arity == PN_LIST);
817        Stream_write_string(f, "let ");
818        instrument_declarations(node, f);
819        break;
820    default:    default:
821      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);
822    }    }
823  }  }
824    
825  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) {  
826    switch (node->pn_type) {    switch (node->pn_type) {
827    case TOK_FUNCTION:    case TOK_FUNCTION:
828      instrument_function(node, f, indent, FUNCTION_NORMAL);      instrument_function(node, f, indent, FUNCTION_NORMAL);
# Line 572  Line 833 
833      Stream_write_string(f, "{\n");      Stream_write_string(f, "{\n");
834  */  */
835      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) {
836        instrument_statement(p, f, indent);        instrument_statement(p, f, indent, false);
837      }      }
838  /*  /*
839      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 580  Line 841 
841  */  */
842      break;      break;
843    case TOK_IF:    case TOK_IF:
844      {
845      assert(node->pn_arity == PN_TERNARY);      assert(node->pn_arity == PN_TERNARY);
846    
847        uint16_t line = node->pn_pos.begin.lineno;
848        if (! is_jscoverage_if) {
849          if (line > num_lines) {
850            fatal("%s: script contains more than 65,535 lines", file_id);
851          }
852          if (line >= 2 && exclusive_directives[line - 2]) {
853            is_jscoverage_if = true;
854          }
855        }
856    
857      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
858      Stream_write_string(f, "if (");      Stream_write_string(f, "if (");
859      instrument_expression(node->pn_kid1, f);      instrument_expression(node->pn_kid1, f);
860      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
861      instrument_statement(node->pn_kid2, f, indent + 2);      if (is_jscoverage_if && node->pn_kid3) {
862          uint16_t else_start = node->pn_kid3->pn_pos.begin.lineno;
863          uint16_t else_end = node->pn_kid3->pn_pos.end.lineno + 1;
864          Stream_printf(f, "%*s", indent + 2, "");
865          Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, else_start, else_end);
866        }
867        instrument_statement(node->pn_kid2, f, indent + 2, false);
868      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
869      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
870      if (node->pn_kid3) {  
871        if (node->pn_kid3 || is_jscoverage_if) {
872        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
873        Stream_write_string(f, "else {\n");        Stream_write_string(f, "else {\n");
874        instrument_statement(node->pn_kid3, f, indent + 2);  
875          if (is_jscoverage_if) {
876            uint16_t if_start = node->pn_kid2->pn_pos.begin.lineno + 1;
877            uint16_t if_end = node->pn_kid2->pn_pos.end.lineno + 1;
878            Stream_printf(f, "%*s", indent + 2, "");
879            Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_start, if_end);
880          }
881    
882          if (node->pn_kid3) {
883            instrument_statement(node->pn_kid3, f, indent + 2, is_jscoverage_if);
884          }
885    
886        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
887        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
888      }      }
889    
890      break;      break;
891      }
892    case TOK_SWITCH:    case TOK_SWITCH:
893      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
894      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
895      Stream_write_string(f, "switch (");      Stream_write_string(f, "switch (");
896      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
897      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
898      for (struct JSParseNode * p = node->pn_right->pn_head; p != NULL; p = p->pn_next) {      {
899        Stream_printf(f, "%*s", indent, "");        JSParseNode * list = node->pn_right;
900        switch (p->pn_type) {        if (list->pn_type == TOK_LEXICALSCOPE) {
901        case TOK_CASE:          list = list->pn_expr;
902          Stream_write_string(f, "case ");        }
903          instrument_expression(p->pn_left, f);        for (struct JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
904          Stream_write_string(f, ":\n");          Stream_printf(f, "%*s", indent, "");
905          break;          switch (p->pn_type) {
906        case TOK_DEFAULT:          case TOK_CASE:
907          Stream_write_string(f, "default:\n");            Stream_write_string(f, "case ");
908          break;            instrument_expression(p->pn_left, f);
909        default:            Stream_write_string(f, ":\n");
910          abort();            break;
911          break;          case TOK_DEFAULT:
912              Stream_write_string(f, "default:\n");
913              break;
914            default:
915              abort();
916              break;
917            }
918            instrument_statement(p->pn_right, f, indent + 2, false);
919        }        }
       instrument_statement(p->pn_right, f, indent + 2);  
920      }      }
921      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
922      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
# Line 632  Line 931 
931      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
932      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
933      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
934      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
935      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
936      break;      break;
937    case TOK_DO:    case TOK_DO:
938      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
939      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
940      Stream_write_string(f, "do {\n");      Stream_write_string(f, "do {\n");
941      instrument_statement(node->pn_left, f, indent + 2);      instrument_statement(node->pn_left, f, indent + 2, false);
942      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
943      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
944      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
# Line 649  Line 948 
948    case TOK_FOR:    case TOK_FOR:
949      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
950      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
     Stream_write_string(f, "for (");  
951      switch (node->pn_left->pn_type) {      switch (node->pn_left->pn_type) {
952      case TOK_IN:      case TOK_IN:
953        /* for/in */        /* for/in */
954        assert(node->pn_left->pn_arity == PN_BINARY);        assert(node->pn_left->pn_arity == PN_BINARY);
955        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);  
956        break;        break;
957      case TOK_RESERVED:      case TOK_RESERVED:
958        /* for (;;) */        /* for (;;) */
959        assert(node->pn_left->pn_arity == PN_TERNARY);        assert(node->pn_left->pn_arity == PN_TERNARY);
960          Stream_write_string(f, "for (");
961        if (node->pn_left->pn_kid1) {        if (node->pn_left->pn_kid1) {
962          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);  
         }  
963        }        }
964        Stream_write_string(f, ";");        Stream_write_string(f, ";");
965        if (node->pn_left->pn_kid2) {        if (node->pn_left->pn_kid2) {
# Line 696  Line 971 
971          Stream_write_char(f, ' ');          Stream_write_char(f, ' ');
972          instrument_expression(node->pn_left->pn_kid3, f);          instrument_expression(node->pn_left->pn_kid3, f);
973        }        }
974          Stream_write_char(f, ')');
975        break;        break;
976      default:      default:
977        abort();        abort();
978        break;        break;
979      }      }
980      Stream_write_string(f, ") {\n");      Stream_write_string(f, " {\n");
981      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
982      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
983      break;      break;
984    case TOK_THROW:    case TOK_THROW:
# Line 715  Line 991 
991    case TOK_TRY:    case TOK_TRY:
992      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
993      Stream_write_string(f, "try {\n");      Stream_write_string(f, "try {\n");
994      instrument_statement(node->pn_kid1, f, indent + 2);      instrument_statement(node->pn_kid1, f, indent + 2, false);
995      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
996      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
997      {      if (node->pn_kid2) {
998        for (JSParseNode * catch = node->pn_kid2; catch != NULL; catch = catch->pn_kid2) {        assert(node->pn_kid2->pn_type == TOK_RESERVED);
999          for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) {
1000            assert(scope->pn_type == TOK_LEXICALSCOPE);
1001            JSParseNode * catch = scope->pn_expr;
1002          assert(catch->pn_type == TOK_CATCH);          assert(catch->pn_type == TOK_CATCH);
1003          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
1004          Stream_write_string(f, "catch (");          Stream_write_string(f, "catch (");
1005            /* this may not be a name - destructuring assignment */
1006            /*
1007          assert(catch->pn_kid1->pn_arity == PN_NAME);          assert(catch->pn_kid1->pn_arity == PN_NAME);
1008          print_string_atom(catch->pn_kid1->pn_atom, f);          print_string_atom(catch->pn_kid1->pn_atom, f);
1009          if (catch->pn_kid1->pn_expr) {          */
1010            instrument_expression(catch->pn_kid1, f);
1011            if (catch->pn_kid2) {
1012            Stream_write_string(f, " if ");            Stream_write_string(f, " if ");
1013            instrument_expression(catch->pn_kid1->pn_expr, f);            instrument_expression(catch->pn_kid2, f);
1014          }          }
1015          Stream_write_string(f, ") {\n");          Stream_write_string(f, ") {\n");
1016          instrument_statement(catch->pn_kid3, f, indent + 2);          instrument_statement(catch->pn_kid3, f, indent + 2, false);
1017          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
1018          Stream_write_string(f, "}\n");          Stream_write_string(f, "}\n");
1019        }        }
# Line 738  Line 1021 
1021      if (node->pn_kid3) {      if (node->pn_kid3) {
1022        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
1023        Stream_write_string(f, "finally {\n");        Stream_write_string(f, "finally {\n");
1024        instrument_statement(node->pn_kid3, f, indent + 2);        instrument_statement(node->pn_kid3, f, indent + 2, false);
1025        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
1026        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
1027      }      }
# Line 764  Line 1047 
1047      Stream_write_string(f, "with (");      Stream_write_string(f, "with (");
1048      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
1049      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
1050      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
1051      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
1052      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
1053      break;      break;
1054    case TOK_VAR:    case TOK_VAR:
1055      instrument_var_statement(node, f, indent);      Stream_printf(f, "%*s", indent, "");
1056        instrument_expression(node, f);
1057      Stream_write_string(f, ";\n");      Stream_write_string(f, ";\n");
1058      break;      break;
1059    case TOK_RETURN:    case TOK_RETURN:
# Line 802  Line 1086 
1086      /*      /*
1087      ... use output_statement instead of instrument_statement.      ... use output_statement instead of instrument_statement.
1088      */      */
1089      output_statement(node->pn_expr, f, indent);      output_statement(node->pn_expr, f, indent, false);
1090        break;
1091      case TOK_LEXICALSCOPE:
1092        /* let statement */
1093        assert(node->pn_arity == PN_NAME);
1094        switch (node->pn_expr->pn_type) {
1095        case TOK_LET:
1096          /* let statement */
1097          assert(node->pn_expr->pn_arity == PN_BINARY);
1098          instrument_statement(node->pn_expr, f, indent, false);
1099          break;
1100        case TOK_LC:
1101          /* block */
1102          Stream_printf(f, "%*s", indent, "");
1103          Stream_write_string(f, "{\n");
1104          instrument_statement(node->pn_expr, f, indent + 2, false);
1105          Stream_printf(f, "%*s", indent, "");
1106          Stream_write_string(f, "}\n");
1107          break;
1108        case TOK_FOR:
1109          instrument_statement(node->pn_expr, f, indent, false);
1110          break;
1111        default:
1112          abort();
1113          break;
1114        }
1115        break;
1116      case TOK_LET:
1117        switch (node->pn_arity) {
1118        case PN_BINARY:
1119          /* let statement */
1120          Stream_printf(f, "%*s", indent, "");
1121          Stream_write_string(f, "let (");
1122          assert(node->pn_left->pn_type == TOK_LP);
1123          assert(node->pn_left->pn_arity == PN_LIST);
1124          instrument_declarations(node->pn_left, f);
1125          Stream_write_string(f, ") {\n");
1126          instrument_statement(node->pn_right, f, indent + 2, false);
1127          Stream_printf(f, "%*s", indent, "");
1128          Stream_write_string(f, "}\n");
1129          break;
1130        case PN_LIST:
1131          /* let definition */
1132          Stream_printf(f, "%*s", indent, "");
1133          instrument_expression(node, f);
1134          Stream_write_string(f, ";\n");
1135          break;
1136        default:
1137          abort();
1138          break;
1139        }
1140        break;
1141      case TOK_DEBUGGER:
1142        Stream_printf(f, "%*s", indent, "");
1143        Stream_write_string(f, "debugger;\n");
1144      break;      break;
1145    default:    default:
1146      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 1152 
1152  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
1153  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
1154  */  */
1155  static void instrument_statement(JSParseNode * node, Stream * f, int indent) {  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
1156    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC && node->pn_type != TOK_LEXICALSCOPE) {
1157      int line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
1158        if (line > num_lines) {
1159          fatal("%s: script contains more than 65,535 lines", file_id);
1160        }
1161    
1162      /* the root node has line number 0 */      /* the root node has line number 0 */
1163      if (line != 0) {      if (line != 0) {
1164        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
# Line 824  Line 1166 
1166        lines[line - 1] = 1;        lines[line - 1] = 1;
1167      }      }
1168    }    }
1169    output_statement(node, f, indent);    output_statement(node, f, indent, is_jscoverage_if);
1170  }  }
1171    
1172  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) {
1173    file_id = id;    const jschar * characters_end = characters + line_end;
1174      const jschar * cp = characters + line_start;
1175    /* scan the javascript */    const char * bp = prefix;
1176    size_t input_length = input->length;    for (;;) {
1177    jschar * base = js_InflateString(context, (char *) input->data, &input_length);      if (*bp == '\0') {
1178    if (base == NULL) {        return true;
1179      fatal("out of memory");      }
1180        else if (cp == characters_end) {
1181          return false;
1182        }
1183        else if (*cp != *bp) {
1184          return false;
1185        }
1186        bp++;
1187        cp++;
1188    }    }
1189    JSTokenStream * token_stream = js_NewTokenStream(context, base, input_length, NULL, 1, NULL);  }
1190    if (token_stream == NULL) {  
1191      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) {
1192      /* XXX - other Unicode space */
1193      const jschar * end = characters + line_end;
1194      for (const jschar * p = characters + line_start; p < end; p++) {
1195        jschar c = *p;
1196        if (c == 0x9 || c == 0xB || c == 0xC || c == 0x20 || c == 0xA0) {
1197          continue;
1198        }
1199        else {
1200          return false;
1201        }
1202    }    }
1203      return true;
1204    }
1205    
1206    static void error_reporter(JSContext * context, const char * message, JSErrorReport * report) {
1207      fprintf(stderr, "jscoverage: parse error: line %u: %s\n", report->lineno, message);
1208    }
1209    
1210    void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) {
1211      file_id = id;
1212    
1213    /* parse the javascript */    /* parse the javascript */
1214    JSParseNode * node = js_ParseTokenStream(context, global, token_stream);    JSParseContext parse_context;
1215      if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) {
1216        fatal("cannot create token stream from file: %s", file_id);
1217      }
1218      JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);
1219      JSParseNode * node = js_ParseScript(context, global, &parse_context);
1220    if (node == NULL) {    if (node == NULL) {
1221        js_ReportUncaughtException(context);
1222      fatal("parse error in file: %s", file_id);      fatal("parse error in file: %s", file_id);
1223    }    }
1224    int num_lines = node->pn_pos.end.lineno;    JS_SetErrorReporter(context, old_error_reporter);
1225      num_lines = node->pn_pos.end.lineno;
1226    lines = xmalloc(num_lines);    lines = xmalloc(num_lines);
1227    for (int i = 0; i < num_lines; i++) {    for (unsigned int i = 0; i < num_lines; i++) {
1228      lines[i] = 0;      lines[i] = 0;
1229    }    }
1230    
1231      /* search code for conditionals */
1232      exclusive_directives = xnew(bool, num_lines);
1233      for (unsigned int i = 0; i < num_lines; i++) {
1234        exclusive_directives[i] = false;
1235      }
1236    
1237      bool has_conditionals = false;
1238      struct IfDirective * if_directives = NULL;
1239      size_t line_number = 0;
1240      size_t i = 0;
1241      while (i < num_characters) {
1242        if (line_number == UINT16_MAX) {
1243          fatal("%s: script has more than 65,535 lines", file_id);
1244        }
1245        line_number++;
1246        size_t line_start = i;
1247        jschar c;
1248        bool done = false;
1249        while (! done && i < num_characters) {
1250          c = characters[i];
1251          switch (c) {
1252          case '\r':
1253          case '\n':
1254          case 0x2028:
1255          case 0x2029:
1256            done = true;
1257            break;
1258          default:
1259            i++;
1260          }
1261        }
1262        size_t line_end = i;
1263        if (i < num_characters) {
1264          i++;
1265          if (c == '\r' && i < num_characters && characters[i] == '\n') {
1266            i++;
1267          }
1268        }
1269    
1270        if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) {
1271          has_conditionals = true;
1272    
1273          if (characters_are_white_space(characters, line_start + 16, line_end)) {
1274            exclusive_directives[line_number - 1] = true;
1275          }
1276          else {
1277            struct IfDirective * if_directive = xnew(struct IfDirective, 1);
1278            if_directive->condition_start = characters + line_start + 16;
1279            if_directive->condition_end = characters + line_end;
1280            if_directive->start_line = line_number;
1281            if_directive->end_line = 0;
1282            if_directive->next = if_directives;
1283            if_directives = if_directive;
1284          }
1285        }
1286        else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) {
1287          for (struct IfDirective * p = if_directives; p != NULL; p = p->next) {
1288            if (p->end_line == 0) {
1289              p->end_line = line_number;
1290              break;
1291            }
1292          }
1293        }
1294      }
1295    
1296    /*    /*
1297    An instrumented JavaScript file has 3 sections:    An instrumented JavaScript file has 4 sections:
1298    1. initialization    1. initialization
1299    2. instrumented source code    2. instrumented source code
1300    3. original source code    3. conditionals
1301      4. original source code
1302    */    */
1303    
1304    Stream * instrumented = Stream_new(0);    Stream * instrumented = Stream_new(0);
1305    instrument_statement(node, instrumented, 0);    instrument_statement(node, instrumented, 0, false);
1306      js_FinishParseContext(context, &parse_context);
1307    
1308    /* write line number info to the output */    /* write line number info to the output */
1309    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 1319 
1319    Stream_write_string(output, "}\n");    Stream_write_string(output, "}\n");
1320    free(lines);    free(lines);
1321    lines = NULL;    lines = NULL;
1322      free(exclusive_directives);
1323      exclusive_directives = NULL;
1324    
1325    /* conditionals */    /* conditionals */
1326    bool has_conditionals = false;    if (has_conditionals) {
1327    size_t line_number = 0;      Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);
   size_t i = 0;  
   while (i < input_length) {  
     line_number++;  
     size_t line_start = i;  
     while (i < input_length && base[i] != '\r' && base[i] != '\n') {  
       i++;  
     }  
     size_t line_end = i;  
     if (i < input_length) {  
       if (base[i] == '\r') {  
         line_end = i;  
         i++;  
         if (i < input_length && base[i] == '\n') {  
           i++;  
         }  
       }  
       else if (base[i] == '\n') {  
         line_end = i;  
         i++;  
       }  
       else {  
         abort();  
       }  
     }  
     char * line = js_DeflateString(context, base + line_start, line_end - line_start);  
     if (str_starts_with(line, "//#JSCOVERAGE_IF")) {  
       if (! has_conditionals) {  
         has_conditionals = true;  
         Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);  
       }  
       Stream_printf(output, "_$jscoverage['%s'].conditionals[%d] = {condition: function () {return %s;}, ", file_id, line_number, line + 16);  
     }  
     else if (str_starts_with(line, "//#JSCOVERAGE_ENDIF")) {  
       Stream_printf(output, "end: %d};\n", line_number);  
     }  
     JS_free(context, line);  
1328    }    }
1329    
1330    /* copy the instrumented source code to the output */    /* copy the instrumented source code to the output */
1331    Stream_write(output, instrumented->data, instrumented->length);    Stream_write(output, instrumented->data, instrumented->length);
1332    Stream_write_char(output, '\n');  
1333      /* conditionals */
1334      for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) {
1335        Stream_write_string(output, "if (!(");
1336        print_javascript(if_directive->condition_start, if_directive->condition_end - if_directive->condition_start, output);
1337        Stream_write_string(output, ")) {\n");
1338        Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line);
1339        Stream_write_string(output, "}\n");
1340      }
1341    
1342      /* free */
1343      while (if_directives != NULL) {
1344        struct IfDirective * if_directive = if_directives;
1345        if_directives = if_directives->next;
1346        free(if_directive);
1347      }
1348    
1349    /* copy the original source to the output */    /* copy the original source to the output */
1350    i = 0;    Stream_printf(output, "_$jscoverage['%s'].source = ", file_id);
1351    while (i < input_length) {    jscoverage_write_source(id, characters, num_characters, output);
1352      Stream_write_string(output, "// ");    Stream_printf(output, ";\n");
     size_t line_start = i;  
     while (i < input_length && base[i] != '\r' && base[i] != '\n') {  
       i++;  
     }  
1353    
1354      size_t line_end = i;    Stream_delete(instrumented);
1355      if (i < input_length) {  
1356        if (base[i] == '\r') {    file_id = NULL;
1357          line_end = i;  }
1358    
1359    void jscoverage_write_source(const char * id, const jschar * characters, size_t num_characters, Stream * output) {
1360      Stream_write_string(output, "[");
1361      if (jscoverage_highlight) {
1362        Stream * highlighted_stream = Stream_new(num_characters);
1363        jscoverage_highlight_js(context, id, characters, num_characters, highlighted_stream);
1364        size_t i = 0;
1365        while (i < highlighted_stream->length) {
1366          if (i > 0) {
1367            Stream_write_char(output, ',');
1368          }
1369    
1370          Stream_write_char(output, '"');
1371          bool done = false;
1372          while (! done) {
1373            char c = highlighted_stream->data[i];
1374            switch (c) {
1375            case 0x8:
1376              /* backspace */
1377              Stream_write_string(output, "\\b");
1378              break;
1379            case 0x9:
1380              /* horizontal tab */
1381              Stream_write_string(output, "\\t");
1382              break;
1383            case 0xa:
1384              /* line feed (new line) */
1385              done = true;
1386              break;
1387            /* IE doesn't support this */
1388            /*
1389            case 0xb:
1390              Stream_write_string(output, "\\v");
1391              break;
1392            */
1393            case 0xc:
1394              /* form feed */
1395              Stream_write_string(output, "\\f");
1396              break;
1397            case 0xd:
1398              /* carriage return */
1399              done = true;
1400              if (i + 1 < highlighted_stream->length && highlighted_stream->data[i + 1] == '\n') {
1401                i++;
1402              }
1403              break;
1404            case '"':
1405              Stream_write_string(output, "\\\"");
1406              break;
1407            case '\\':
1408              Stream_write_string(output, "\\\\");
1409              break;
1410            default:
1411              Stream_write_char(output, c);
1412              break;
1413            }
1414          i++;          i++;
1415          if (i < input_length && base[i] == '\n') {          if (i >= highlighted_stream->length) {
1416            i++;            done = true;
1417          }          }
1418        }        }
1419        else if (base[i] == '\n') {        Stream_write_char(output, '"');
1420          line_end = i;      }
1421        Stream_delete(highlighted_stream);
1422      }
1423      else {
1424        size_t i = 0;
1425        while (i < num_characters) {
1426          if (i > 0) {
1427            Stream_write_char(output, ',');
1428          }
1429    
1430          Stream_write_char(output, '"');
1431          bool done = false;
1432          while (! done) {
1433            jschar c = characters[i];
1434            switch (c) {
1435            case 0x8:
1436              /* backspace */
1437              Stream_write_string(output, "\\b");
1438              break;
1439            case 0x9:
1440              /* horizontal tab */
1441              Stream_write_string(output, "\\t");
1442              break;
1443            case 0xa:
1444              /* line feed (new line) */
1445              done = true;
1446              break;
1447            /* IE doesn't support this */
1448            /*
1449            case 0xb:
1450              Stream_write_string(output, "\\v");
1451              break;
1452            */
1453            case 0xc:
1454              /* form feed */
1455              Stream_write_string(output, "\\f");
1456              break;
1457            case 0xd:
1458              /* carriage return */
1459              done = true;
1460              if (i + 1 < num_characters && characters[i + 1] == '\n') {
1461                i++;
1462              }
1463              break;
1464            case '"':
1465              Stream_write_string(output, "\\\"");
1466              break;
1467            case '\\':
1468              Stream_write_string(output, "\\\\");
1469              break;
1470            case '&':
1471              Stream_write_string(output, "&amp;");
1472              break;
1473            case '<':
1474              Stream_write_string(output, "&lt;");
1475              break;
1476            case '>':
1477              Stream_write_string(output, "&gt;");
1478              break;
1479            case 0x2028:
1480            case 0x2029:
1481              done = true;
1482              break;
1483            default:
1484              if (32 <= c && c <= 126) {
1485                Stream_write_char(output, c);
1486              }
1487              else {
1488                Stream_printf(output, "&#%d;", c);
1489              }
1490              break;
1491            }
1492          i++;          i++;
1493            if (i >= num_characters) {
1494              done = true;
1495            }
1496        }        }
1497        else {        Stream_write_char(output, '"');
         abort();  
       }  
1498      }      }
   
     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);  
1499    }    }
1500      Stream_write_string(output, "]");
   Stream_delete(instrumented);  
   
   JS_free(context, base);  
   
   file_id = NULL;  
1501  }  }
1502    
1503  void jscoverage_copy_resources(const char * destination_directory) {  void jscoverage_copy_resources(const char * destination_directory) {
1504    copy_resource("jscoverage.html", destination_directory);    copy_resource("jscoverage.html", destination_directory);
1505    copy_resource("jscoverage.css", destination_directory);    copy_resource("jscoverage.css", destination_directory);
1506    copy_resource("jscoverage.js", destination_directory);    copy_resource("jscoverage.js", destination_directory);
1507      copy_resource("jscoverage-ie.css", destination_directory);
1508    copy_resource("jscoverage-throbber.gif", destination_directory);    copy_resource("jscoverage-throbber.gif", destination_directory);
1509    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);  
1510  }  }
1511    
1512  /*  /*
# Line 1004  Line 1541 
1541    JS_HashTableDestroy(coverage->coverage_table);    JS_HashTableDestroy(coverage->coverage_table);
1542    struct FileCoverageList * p = coverage->coverage_list;    struct FileCoverageList * p = coverage->coverage_list;
1543    while (p != NULL) {    while (p != NULL) {
1544      free(p->file_coverage->lines);      free(p->file_coverage->coverage_lines);
1545      free(p->file_coverage->source);      if (p->file_coverage->source_lines != NULL) {
1546          for (uint32 i = 0; i < p->file_coverage->num_source_lines; i++) {
1547            free(p->file_coverage->source_lines[i]);
1548          }
1549          free(p->file_coverage->source_lines);
1550        }
1551      free(p->file_coverage->id);      free(p->file_coverage->id);
1552      free(p->file_coverage);      free(p->file_coverage);
1553      struct FileCoverageList * q = p;      struct FileCoverageList * q = p;
# Line 1034  Line 1576 
1576  }  }
1577    
1578  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) {
1579      int result = 0;
1580    
1581    jschar * base = js_InflateString(context, (char *) json, &length);    jschar * base = js_InflateString(context, (char *) json, &length);
1582    if (base == NULL) {    if (base == NULL) {
1583      fatal("out of memory");      fatal("out of memory");
# Line 1046  Line 1590 
1590    
1591    JS_free(context, base);    JS_free(context, base);
1592    
1593    JSTokenStream * token_stream = js_NewTokenStream(context, parenthesized_json, length + 2, NULL, 1, NULL);    JSParseContext parse_context;
1594    if (token_stream == NULL) {    if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) {
1595      fatal("cannot create token stream");      free(parenthesized_json);
1596        return -1;
1597    }    }
1598      JSParseNode * root = js_ParseScript(context, global, &parse_context);
   JSParseNode * root = js_ParseTokenStream(context, global, token_stream);  
1599    free(parenthesized_json);    free(parenthesized_json);
1600    if (root == NULL) {    if (root == NULL) {
1601      return -1;      result = -1;
1602        goto done;
1603    }    }
1604    
1605    /* root node must be TOK_LC */    /* root node must be TOK_LC */
1606    if (root->pn_type != TOK_LC) {    if (root->pn_type != TOK_LC) {
1607      return -1;      result = -1;
1608        goto done;
1609    }    }
1610    JSParseNode * semi = root->pn_u.list.head;    JSParseNode * semi = root->pn_u.list.head;
1611    
1612    /* 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 */
1613    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {
1614      return -1;      result = -1;
1615        goto done;
1616    }    }
1617    JSParseNode * parenthesized = semi->pn_kid;    JSParseNode * parenthesized = semi->pn_kid;
1618    
1619    /* this must be a parenthesized expression */    /* this must be a parenthesized expression */
1620    if (parenthesized->pn_type != TOK_RP) {    if (parenthesized->pn_type != TOK_RP) {
1621      return -1;      result = -1;
1622        goto done;
1623    }    }
1624    JSParseNode * object = parenthesized->pn_kid;    JSParseNode * object = parenthesized->pn_kid;
1625    
1626    /* this must be an object literal */    /* this must be an object literal */
1627    if (object->pn_type != TOK_RC) {    if (object->pn_type != TOK_RC) {
1628      return -1;      result = -1;
1629        goto done;
1630    }    }
1631    
1632    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {
1633      /* every element of this list must be TOK_COLON */      /* every element of this list must be TOK_COLON */
1634      if (p->pn_type != TOK_COLON) {      if (p->pn_type != TOK_COLON) {
1635        return -1;        result = -1;
1636          goto done;
1637      }      }
1638    
1639      /* the key must be a string representing the file */      /* the key must be a string representing the file */
1640      JSParseNode * key = p->pn_left;      JSParseNode * key = p->pn_left;
1641      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {
1642        return -1;        result = -1;
1643          goto done;
1644      }      }
1645      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));
1646    
1647      /* the value must be an object literal OR an array */      /* the value must be an object literal OR an array */
1648      JSParseNode * value = p->pn_right;      JSParseNode * value = p->pn_right;
1649      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {
1650        return -1;        result = -1;
1651          goto done;
1652      }      }
1653    
1654      JSParseNode * array = NULL;      JSParseNode * array = NULL;
# Line 1108  Line 1660 
1660      else if (value->pn_type == TOK_RC) {      else if (value->pn_type == TOK_RC) {
1661        /* an object literal */        /* an object literal */
1662        if (value->pn_count != 2) {        if (value->pn_count != 2) {
1663          return -1;          result = -1;
1664            goto done;
1665        }        }
1666        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {
1667          if (element->pn_type != TOK_COLON) {          if (element->pn_type != TOK_COLON) {
1668            return -1;            result = -1;
1669              goto done;
1670          }          }
1671          JSParseNode * left = element->pn_left;          JSParseNode * left = element->pn_left;
1672          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {
1673            return -1;            result = -1;
1674              goto done;
1675          }          }
1676          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));
1677          if (strcmp(s, "coverage") == 0) {          if (strcmp(s, "coverage") == 0) {
1678            array = element->pn_right;            array = element->pn_right;
1679            if (array->pn_type != TOK_RB) {            if (array->pn_type != TOK_RB) {
1680              return -1;              result = -1;
1681                goto done;
1682            }            }
1683          }          }
1684          else if (strcmp(s, "source") == 0) {          else if (strcmp(s, "source") == 0) {
1685            source = element->pn_right;            source = element->pn_right;
1686            if (source->pn_type != TOK_STRING || ! ATOM_IS_STRING(source->pn_atom)) {            if (source->pn_type != TOK_RB) {
1687              return -1;              result = -1;
1688                goto done;
1689            }            }
1690          }          }
1691          else {          else {
1692            return -1;            result = -1;
1693              goto done;
1694          }          }
1695        }        }
1696      }      }
1697      else {      else {
1698        return -1;        result = -1;
1699          goto done;
1700      }      }
1701    
1702      if (array == NULL) {      if (array == NULL) {
1703        return -1;        result = -1;
1704          goto done;
1705      }      }
1706    
1707      /* look up the file in the coverage table */      /* look up the file in the coverage table */
# Line 1151  Line 1711 
1711        char * id = xstrdup(id_bytes);        char * id = xstrdup(id_bytes);
1712        file_coverage = xmalloc(sizeof(FileCoverage));        file_coverage = xmalloc(sizeof(FileCoverage));
1713        file_coverage->id = id;        file_coverage->id = id;
1714        file_coverage->num_lines = array->pn_count - 1;        file_coverage->num_coverage_lines = array->pn_count;
1715        file_coverage->lines = xnew(int, array->pn_count);        file_coverage->coverage_lines = xnew(int, array->pn_count);
1716        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)));  
       }  
1717    
1718        /* set coverage for all lines */        /* set coverage for all lines */
1719        uint32 i = 0;        uint32 i = 0;
1720        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++) {
1721          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1722            file_coverage->lines[i] = (int) element->pn_dval;            file_coverage->coverage_lines[i] = (int) element->pn_dval;
1723          }          }
1724          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1725            file_coverage->lines[i] = -1;            file_coverage->coverage_lines[i] = -1;
1726          }          }
1727          else {          else {
1728            return -1;            result = -1;
1729              goto done;
1730          }          }
1731        }        }
1732        assert(i == array->pn_count);        assert(i == array->pn_count);
# Line 1185  Line 1741 
1741      else {      else {
1742        /* sanity check */        /* sanity check */
1743        assert(strcmp(file_coverage->id, id_bytes) == 0);        assert(strcmp(file_coverage->id, id_bytes) == 0);
1744        if (file_coverage->num_lines != array->pn_count - 1) {        if (file_coverage->num_coverage_lines != array->pn_count) {
1745          return -2;          result = -2;
1746            goto done;
1747        }        }
1748    
1749        /* merge the coverage */        /* merge the coverage */
1750        uint32 i = 0;        uint32 i = 0;
1751        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++) {
1752          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1753            if (file_coverage->lines[i] == -1) {            if (file_coverage->coverage_lines[i] == -1) {
1754              return -2;              result = -2;
1755                goto done;
1756            }            }
1757            file_coverage->lines[i] += (int) element->pn_dval;            file_coverage->coverage_lines[i] += (int) element->pn_dval;
1758          }          }
1759          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1760            if (file_coverage->lines[i] != -1) {            if (file_coverage->coverage_lines[i] != -1) {
1761              return -2;              result = -2;
1762                goto done;
1763            }            }
1764          }          }
1765          else {          else {
1766            return -1;            result = -1;
1767              goto done;
1768          }          }
1769        }        }
1770        assert(i == array->pn_count);        assert(i == array->pn_count);
1771        }
1772    
1773        /* if this JSON file has source, use it */      /* if this JSON file has source, use it */
1774        if (file_coverage->source == NULL && source != NULL) {      if (file_coverage->source_lines == NULL && source != NULL) {
1775          file_coverage->source = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(source->pn_atom)));        file_coverage->num_source_lines = source->pn_count;
1776          file_coverage->source_lines = xnew(char *, source->pn_count);
1777          uint32 i = 0;
1778          for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {
1779            if (element->pn_type != TOK_STRING) {
1780              result = -1;
1781              goto done;
1782            }
1783            file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));
1784        }        }
1785          assert(i == source->pn_count);
1786      }      }
1787    }    }
1788    
1789    return 0;  done:
1790      js_FinishParseContext(context, &parse_context);
1791      return result;
1792  }  }

Legend:
Removed from v.157  
changed lines
  Added in v.351

  ViewVC Help
Powered by ViewVC 1.1.24