/[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 356 by siliconforks, Fri Oct 24 21:48:02 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);      if (param == NULL) {
319          fatal("unsupported parameter type for function: %s", file_id);
320      }      }
321        print_string_atom(param, f);
322    }    }
323      JS_FinishArenaPool(&pool);
324    Stream_write_string(f, ") {\n");    Stream_write_string(f, ") {\n");
   free(params);  
325    
326    /* function body */    /* function body */
327    instrument_statement(node->pn_body, f, indent + 2);    if (function->flags & JSFUN_EXPR_CLOSURE) {
328        /* expression closure */
329        output_statement(node->pn_body, f, indent + 2, false);
330      }
331      else {
332        instrument_statement(node->pn_body, f, indent + 2, false);
333      }
334    
335    Stream_write_string(f, "}\n");    Stream_write_string(f, "}\n");
336  }  }
337    
338  static void instrument_function_call(JSParseNode * node, Stream * f) {  static void instrument_function_call(JSParseNode * node, Stream * f) {
339    instrument_expression(node->pn_head, f);    JSParseNode * function_node = node->pn_head;
340      if (function_node->pn_type == TOK_FUNCTION) {
341        JSObject * object = function_node->pn_funpob->object;
342        assert(JS_ObjectIsFunction(context, object));
343        JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
344        assert(function);
345        assert(object == &function->object);
346    
347        if (function_node->pn_flags & TCF_GENEXP_LAMBDA) {
348          /* it's a generator expression */
349          JSParseNode * lexical_scope_node = function_node->pn_body;
350          assert(lexical_scope_node->pn_type == TOK_LEXICALSCOPE);
351          assert(lexical_scope_node->pn_arity == PN_NAME);
352          JSParseNode * for_node = lexical_scope_node->pn_body;
353          assert(for_node->pn_type == TOK_FOR);
354          assert(for_node->pn_arity == PN_BINARY);
355          JSParseNode * if_node = NULL;
356          JSParseNode * semi_node;
357          switch (for_node->pn_right->pn_type) {
358          case TOK_SEMI:
359            semi_node = for_node->pn_right;
360            break;
361          case TOK_IF:
362            if_node = for_node->pn_right;
363            assert(if_node->pn_arity == PN_TERNARY);
364            semi_node = if_node->pn_kid2;
365            assert(semi_node->pn_type == TOK_SEMI);
366            break;
367          default:
368            abort();
369            break;
370          }
371          assert(semi_node->pn_arity == PN_UNARY);
372          JSParseNode * yield_node = semi_node->pn_kid;
373          assert(yield_node->pn_type == TOK_YIELD);
374          Stream_write_char(f, '(');
375          instrument_expression(yield_node->pn_kid, f);
376          Stream_write_char(f, ' ');
377          output_for_in(for_node, f);
378          if (if_node) {
379            Stream_write_string(f, " if (");
380            instrument_expression(if_node->pn_kid1, f);
381            Stream_write_char(f, ')');
382          }
383          Stream_write_char(f, ')');
384          return;
385        }
386        else {
387          Stream_write_char(f, '(');
388          instrument_expression(function_node, f);
389          Stream_write_char(f, ')');
390        }
391      }
392      else {
393        instrument_expression(function_node, f);
394      }
395    Stream_write_char(f, '(');    Stream_write_char(f, '(');
396    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) {
397      if (p != node->pn_head->pn_next) {      if (p != node->pn_head->pn_next) {
398        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
399      }      }
# Line 219  Line 402 
402    Stream_write_char(f, ')');    Stream_write_char(f, ')');
403  }  }
404    
405    static void instrument_declarations(JSParseNode * list, Stream * f) {
406      assert(list->pn_arity == PN_LIST);
407      for (JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
408        switch (p->pn_type) {
409        case TOK_NAME:
410          assert(p->pn_arity == PN_NAME);
411          if (p != list->pn_head) {
412            Stream_write_string(f, ", ");
413          }
414          print_string_atom(p->pn_atom, f);
415          if (p->pn_expr != NULL) {
416            Stream_write_string(f, " = ");
417            instrument_expression(p->pn_expr, f);
418          }
419          break;
420        case TOK_ASSIGN:
421        case TOK_RB:
422        case TOK_RC:
423          /* destructuring */
424          instrument_expression(p, f);
425          break;
426        default:
427          abort();
428          break;
429        }
430      }
431    }
432    
433  /*  /*
434  See <Expressions> in jsparse.h.  See <Expressions> in jsparse.h.
435  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 488 
488      instrument_expression(node->pn_kid3, f);      instrument_expression(node->pn_kid3, f);
489      break;      break;
490    case TOK_OR:    case TOK_OR:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " || ");  
     instrument_expression(node->pn_right, f);  
     break;  
491    case TOK_AND:    case TOK_AND:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " && ");  
     instrument_expression(node->pn_right, f);  
     break;  
492    case TOK_BITOR:    case TOK_BITOR:
493    case TOK_BITXOR:    case TOK_BITXOR:
494    case TOK_BITAND:    case TOK_BITAND:
# Line 341  Line 544 
544        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
545        break;        break;
546      default:      default:
547        abort();        fatal("%s: unknown operator (%d) in file", file_id, node->pn_op);
548        break;        break;
549      }      }
550      break;      break;
# Line 398  Line 601 
601      assert(ATOM_IS_STRING(node->pn_atom));      assert(ATOM_IS_STRING(node->pn_atom));
602      {      {
603        JSString * s = ATOM_TO_STRING(node->pn_atom);        JSString * s = ATOM_TO_STRING(node->pn_atom);
604        /* XXX - semantics changed in 1.7 */        bool must_quote;
605        if (! ATOM_KEYWORD(node->pn_atom) && js_IsIdentifier(s)) {        if (JSSTRING_LENGTH(s) == 0) {
606          Stream_write_char(f, '.');          must_quote = true;
607          print_string_atom(node->pn_atom, f);        }
608          else if (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF) {
609            must_quote = true;
610          }
611          else if (! js_IsIdentifier(s)) {
612            must_quote = true;
613        }        }
614        else {        else {
615            must_quote = false;
616          }
617          if (must_quote) {
618          Stream_write_char(f, '[');          Stream_write_char(f, '[');
619          print_quoted_string_atom(node->pn_atom, f);          print_quoted_string_atom(node->pn_atom, f);
620          Stream_write_char(f, ']');          Stream_write_char(f, ']');
621        }        }
622          else {
623            Stream_write_char(f, '.');
624            print_string_atom(node->pn_atom, f);
625          }
626      }      }
627      break;      break;
628    case TOK_LB:    case TOK_LB:
# Line 438  Line 653 
653    case TOK_RC:    case TOK_RC:
654      Stream_write_char(f, '{');      Stream_write_char(f, '{');
655      for (struct JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) {      for (struct JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) {
656        assert(p->pn_type == TOK_COLON);        if (p->pn_type != TOK_COLON) {
657            fatal("unsupported node type in file %s: %d", file_id, p->pn_type);
658          }
659        if (p != node->pn_head) {        if (p != node->pn_head) {
660          Stream_write_string(f, ", ");          Stream_write_string(f, ", ");
661        }        }
# Line 446  Line 663 
663        /* check whether this is a getter or setter */        /* check whether this is a getter or setter */
664        switch (p->pn_op) {        switch (p->pn_op) {
665        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;  
666        case JSOP_SETTER:        case JSOP_SETTER:
667          Stream_write_string(f, "set ");          if (p->pn_op == JSOP_GETTER) {
668              Stream_write_string(f, "get ");
669            }
670            else {
671              Stream_write_string(f, "set ");
672            }
673          instrument_expression(p->pn_left, f);          instrument_expression(p->pn_left, f);
674            if (p->pn_right->pn_type != TOK_FUNCTION) {
675              fatal("parse error: expected function");
676            }
677          instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);          instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);
678          break;          break;
679        default:        default:
# Line 475  Line 696 
696    case TOK_STRING:    case TOK_STRING:
697      print_quoted_string_atom(node->pn_atom, f);      print_quoted_string_atom(node->pn_atom, f);
698      break;      break;
699    case TOK_OBJECT:    case TOK_REGEXP:
700      switch (node->pn_op) {      assert(node->pn_op == JSOP_REGEXP);
701      case JSOP_OBJECT:      {
702        /* I assume this is JSOP_REGEXP */        JSObject * object = node->pn_pob->object;
703        abort();        jsval result;
704        break;        js_regexp_toString(context, object, &result);
705      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;  
706      }      }
707      break;      break;
708    case TOK_NUMBER:    case TOK_NUMBER:
# Line 538  Line 748 
748      Stream_write_string(f, " in ");      Stream_write_string(f, " in ");
749      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
750      break;      break;
751    default:    case TOK_LEXICALSCOPE:
752      fatal("unsupported node type in file %s: %d", file_id, node->pn_type);      assert(node->pn_arity == PN_NAME);
753    }      assert(node->pn_expr->pn_type == TOK_LET);
754  }      assert(node->pn_expr->pn_arity == PN_BINARY);
755        Stream_write_string(f, "let(");
756  static void instrument_var_statement(JSParseNode * node, Stream * f, int indent) {      assert(node->pn_expr->pn_left->pn_type == TOK_LP);
757    assert(node->pn_arity == PN_LIST);      assert(node->pn_expr->pn_left->pn_arity == PN_LIST);
758    Stream_printf(f, "%*s", indent, "");      instrument_declarations(node->pn_expr->pn_left, f);
759    Stream_write_string(f, "var ");      Stream_write_string(f, ") ");
760    for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) {      instrument_expression(node->pn_expr->pn_right, f);
761      assert(p->pn_type == TOK_NAME);      break;
762      assert(p->pn_arity == PN_NAME);    case TOK_YIELD:
763      if (p != node->pn_head) {      assert(node->pn_arity == PN_UNARY);
764        Stream_write_string(f, ", ");      Stream_write_string(f, "yield");
765        if (node->pn_kid != NULL) {
766          Stream_write_char(f, ' ');
767          instrument_expression(node->pn_kid, f);
768      }      }
769      print_string_atom(p->pn_atom, f);      break;
770      if (p->pn_expr != NULL) {    case TOK_ARRAYCOMP:
771        Stream_write_string(f, " = ");      assert(node->pn_arity == PN_LIST);
772        instrument_expression(p->pn_expr, f);      {
773          JSParseNode * block_node;
774          switch (node->pn_count) {
775          case 1:
776            block_node = node->pn_head;
777            break;
778          case 2:
779            block_node = node->pn_head->pn_next;
780            break;
781          default:
782            abort();
783            break;
784          }
785          assert(block_node->pn_type == TOK_LEXICALSCOPE);
786          assert(block_node->pn_arity == PN_NAME);
787          JSParseNode * for_node = block_node->pn_expr;
788          assert(for_node->pn_type == TOK_FOR);
789          assert(for_node->pn_arity == PN_BINARY);
790          JSParseNode * p = for_node;
791          while (p->pn_type == TOK_FOR) {
792            p = p->pn_right;
793          }
794          JSParseNode * if_node = NULL;
795          JSParseNode * push_node;
796          switch (p->pn_type) {
797          case TOK_ARRAYPUSH:
798            push_node = p;
799            assert(push_node->pn_arity == PN_UNARY);
800            break;
801          case TOK_IF:
802            if_node = p;
803            assert(if_node->pn_arity == PN_TERNARY);
804            push_node = if_node->pn_kid2;
805            assert(push_node->pn_arity == PN_UNARY);
806            break;
807          default:
808            abort();
809            break;
810          }
811          Stream_write_char(f, '[');
812          instrument_expression(push_node->pn_kid, f);
813          p = for_node;
814          while (p->pn_type == TOK_FOR) {
815            Stream_write_char(f, ' ');
816            output_for_in(p, f);
817            p = p->pn_right;
818          }
819          if (if_node) {
820            Stream_write_string(f, " if (");
821            instrument_expression(if_node->pn_kid1, f);
822            Stream_write_char(f, ')');
823          }
824          Stream_write_char(f, ']');
825      }      }
826        break;
827      case TOK_VAR:
828        assert(node->pn_arity == PN_LIST);
829        Stream_write_string(f, "var ");
830        instrument_declarations(node, f);
831        break;
832      case TOK_LET:
833        assert(node->pn_arity == PN_LIST);
834        Stream_write_string(f, "let ");
835        instrument_declarations(node, f);
836        break;
837      default:
838        fatal("unsupported node type in file %s: %d", file_id, node->pn_type);
839    }    }
840  }  }
841    
842  static void output_statement(JSParseNode * node, Stream * f, int indent) {  static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
843    switch (node->pn_type) {    switch (node->pn_type) {
844    case TOK_FUNCTION:    case TOK_FUNCTION:
845      instrument_function(node, f, indent, FUNCTION_NORMAL);      instrument_function(node, f, indent, FUNCTION_NORMAL);
# Line 572  Line 850 
850      Stream_write_string(f, "{\n");      Stream_write_string(f, "{\n");
851  */  */
852      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) {
853        instrument_statement(p, f, indent);        instrument_statement(p, f, indent, false);
854      }      }
855  /*  /*
856      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 580  Line 858 
858  */  */
859      break;      break;
860    case TOK_IF:    case TOK_IF:
861      {
862      assert(node->pn_arity == PN_TERNARY);      assert(node->pn_arity == PN_TERNARY);
863    
864        uint16_t line = node->pn_pos.begin.lineno;
865        if (! is_jscoverage_if) {
866          if (line > num_lines) {
867            fatal("%s: script contains more than 65,535 lines", file_id);
868          }
869          if (line >= 2 && exclusive_directives[line - 2]) {
870            is_jscoverage_if = true;
871          }
872        }
873    
874      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
875      Stream_write_string(f, "if (");      Stream_write_string(f, "if (");
876      instrument_expression(node->pn_kid1, f);      instrument_expression(node->pn_kid1, f);
877      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
878      instrument_statement(node->pn_kid2, f, indent + 2);      if (is_jscoverage_if && node->pn_kid3) {
879          uint16_t else_start = node->pn_kid3->pn_pos.begin.lineno;
880          uint16_t else_end = node->pn_kid3->pn_pos.end.lineno + 1;
881          Stream_printf(f, "%*s", indent + 2, "");
882          Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, else_start, else_end);
883        }
884        instrument_statement(node->pn_kid2, f, indent + 2, false);
885      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
886      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
887      if (node->pn_kid3) {  
888        if (node->pn_kid3 || is_jscoverage_if) {
889        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
890        Stream_write_string(f, "else {\n");        Stream_write_string(f, "else {\n");
891        instrument_statement(node->pn_kid3, f, indent + 2);  
892          if (is_jscoverage_if) {
893            uint16_t if_start = node->pn_kid2->pn_pos.begin.lineno + 1;
894            uint16_t if_end = node->pn_kid2->pn_pos.end.lineno + 1;
895            Stream_printf(f, "%*s", indent + 2, "");
896            Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_start, if_end);
897          }
898    
899          if (node->pn_kid3) {
900            instrument_statement(node->pn_kid3, f, indent + 2, is_jscoverage_if);
901          }
902    
903        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
904        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
905      }      }
906    
907      break;      break;
908      }
909    case TOK_SWITCH:    case TOK_SWITCH:
910      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
911      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
912      Stream_write_string(f, "switch (");      Stream_write_string(f, "switch (");
913      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
914      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
915      for (struct JSParseNode * p = node->pn_right->pn_head; p != NULL; p = p->pn_next) {      {
916        Stream_printf(f, "%*s", indent, "");        JSParseNode * list = node->pn_right;
917        switch (p->pn_type) {        if (list->pn_type == TOK_LEXICALSCOPE) {
918        case TOK_CASE:          list = list->pn_expr;
919          Stream_write_string(f, "case ");        }
920          instrument_expression(p->pn_left, f);        for (struct JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
921          Stream_write_string(f, ":\n");          Stream_printf(f, "%*s", indent, "");
922          break;          switch (p->pn_type) {
923        case TOK_DEFAULT:          case TOK_CASE:
924          Stream_write_string(f, "default:\n");            Stream_write_string(f, "case ");
925          break;            instrument_expression(p->pn_left, f);
926        default:            Stream_write_string(f, ":\n");
927          abort();            break;
928          break;          case TOK_DEFAULT:
929              Stream_write_string(f, "default:\n");
930              break;
931            default:
932              abort();
933              break;
934            }
935            instrument_statement(p->pn_right, f, indent + 2, false);
936        }        }
       instrument_statement(p->pn_right, f, indent + 2);  
937      }      }
938      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
939      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
# Line 632  Line 948 
948      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
949      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
950      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
951      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
952      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
953      break;      break;
954    case TOK_DO:    case TOK_DO:
955      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
956      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
957      Stream_write_string(f, "do {\n");      Stream_write_string(f, "do {\n");
958      instrument_statement(node->pn_left, f, indent + 2);      instrument_statement(node->pn_left, f, indent + 2, false);
959      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
960      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
961      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
# Line 649  Line 965 
965    case TOK_FOR:    case TOK_FOR:
966      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
967      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
     Stream_write_string(f, "for (");  
968      switch (node->pn_left->pn_type) {      switch (node->pn_left->pn_type) {
969      case TOK_IN:      case TOK_IN:
970        /* for/in */        /* for/in */
971        assert(node->pn_left->pn_arity == PN_BINARY);        assert(node->pn_left->pn_arity == PN_BINARY);
972        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);  
973        break;        break;
974      case TOK_RESERVED:      case TOK_RESERVED:
975        /* for (;;) */        /* for (;;) */
976        assert(node->pn_left->pn_arity == PN_TERNARY);        assert(node->pn_left->pn_arity == PN_TERNARY);
977          Stream_write_string(f, "for (");
978        if (node->pn_left->pn_kid1) {        if (node->pn_left->pn_kid1) {
979          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);  
         }  
980        }        }
981        Stream_write_string(f, ";");        Stream_write_string(f, ";");
982        if (node->pn_left->pn_kid2) {        if (node->pn_left->pn_kid2) {
# Line 696  Line 988 
988          Stream_write_char(f, ' ');          Stream_write_char(f, ' ');
989          instrument_expression(node->pn_left->pn_kid3, f);          instrument_expression(node->pn_left->pn_kid3, f);
990        }        }
991          Stream_write_char(f, ')');
992        break;        break;
993      default:      default:
994        abort();        abort();
995        break;        break;
996      }      }
997      Stream_write_string(f, ") {\n");      Stream_write_string(f, " {\n");
998      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
999      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
1000      break;      break;
1001    case TOK_THROW:    case TOK_THROW:
# Line 715  Line 1008 
1008    case TOK_TRY:    case TOK_TRY:
1009      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
1010      Stream_write_string(f, "try {\n");      Stream_write_string(f, "try {\n");
1011      instrument_statement(node->pn_kid1, f, indent + 2);      instrument_statement(node->pn_kid1, f, indent + 2, false);
1012      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
1013      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
1014      {      if (node->pn_kid2) {
1015        for (JSParseNode * catch = node->pn_kid2; catch != NULL; catch = catch->pn_kid2) {        assert(node->pn_kid2->pn_type == TOK_RESERVED);
1016          for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) {
1017            assert(scope->pn_type == TOK_LEXICALSCOPE);
1018            JSParseNode * catch = scope->pn_expr;
1019          assert(catch->pn_type == TOK_CATCH);          assert(catch->pn_type == TOK_CATCH);
1020          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
1021          Stream_write_string(f, "catch (");          Stream_write_string(f, "catch (");
1022            /* this may not be a name - destructuring assignment */
1023            /*
1024          assert(catch->pn_kid1->pn_arity == PN_NAME);          assert(catch->pn_kid1->pn_arity == PN_NAME);
1025          print_string_atom(catch->pn_kid1->pn_atom, f);          print_string_atom(catch->pn_kid1->pn_atom, f);
1026          if (catch->pn_kid1->pn_expr) {          */
1027            instrument_expression(catch->pn_kid1, f);
1028            if (catch->pn_kid2) {
1029            Stream_write_string(f, " if ");            Stream_write_string(f, " if ");
1030            instrument_expression(catch->pn_kid1->pn_expr, f);            instrument_expression(catch->pn_kid2, f);
1031          }          }
1032          Stream_write_string(f, ") {\n");          Stream_write_string(f, ") {\n");
1033          instrument_statement(catch->pn_kid3, f, indent + 2);          instrument_statement(catch->pn_kid3, f, indent + 2, false);
1034          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
1035          Stream_write_string(f, "}\n");          Stream_write_string(f, "}\n");
1036        }        }
# Line 738  Line 1038 
1038      if (node->pn_kid3) {      if (node->pn_kid3) {
1039        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
1040        Stream_write_string(f, "finally {\n");        Stream_write_string(f, "finally {\n");
1041        instrument_statement(node->pn_kid3, f, indent + 2);        instrument_statement(node->pn_kid3, f, indent + 2, false);
1042        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
1043        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
1044      }      }
# Line 764  Line 1064 
1064      Stream_write_string(f, "with (");      Stream_write_string(f, "with (");
1065      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
1066      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
1067      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
1068      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
1069      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
1070      break;      break;
1071    case TOK_VAR:    case TOK_VAR:
1072      instrument_var_statement(node, f, indent);      Stream_printf(f, "%*s", indent, "");
1073        instrument_expression(node, f);
1074      Stream_write_string(f, ";\n");      Stream_write_string(f, ";\n");
1075      break;      break;
1076    case TOK_RETURN:    case TOK_RETURN:
# Line 802  Line 1103 
1103      /*      /*
1104      ... use output_statement instead of instrument_statement.      ... use output_statement instead of instrument_statement.
1105      */      */
1106      output_statement(node->pn_expr, f, indent);      output_statement(node->pn_expr, f, indent, false);
1107        break;
1108      case TOK_LEXICALSCOPE:
1109        /* let statement */
1110        assert(node->pn_arity == PN_NAME);
1111        switch (node->pn_expr->pn_type) {
1112        case TOK_LET:
1113          /* let statement */
1114          assert(node->pn_expr->pn_arity == PN_BINARY);
1115          instrument_statement(node->pn_expr, f, indent, false);
1116          break;
1117        case TOK_LC:
1118          /* block */
1119          Stream_printf(f, "%*s", indent, "");
1120          Stream_write_string(f, "{\n");
1121          instrument_statement(node->pn_expr, f, indent + 2, false);
1122          Stream_printf(f, "%*s", indent, "");
1123          Stream_write_string(f, "}\n");
1124          break;
1125        case TOK_FOR:
1126          instrument_statement(node->pn_expr, f, indent, false);
1127          break;
1128        default:
1129          abort();
1130          break;
1131        }
1132        break;
1133      case TOK_LET:
1134        switch (node->pn_arity) {
1135        case PN_BINARY:
1136          /* let statement */
1137          Stream_printf(f, "%*s", indent, "");
1138          Stream_write_string(f, "let (");
1139          assert(node->pn_left->pn_type == TOK_LP);
1140          assert(node->pn_left->pn_arity == PN_LIST);
1141          instrument_declarations(node->pn_left, f);
1142          Stream_write_string(f, ") {\n");
1143          instrument_statement(node->pn_right, f, indent + 2, false);
1144          Stream_printf(f, "%*s", indent, "");
1145          Stream_write_string(f, "}\n");
1146          break;
1147        case PN_LIST:
1148          /* let definition */
1149          Stream_printf(f, "%*s", indent, "");
1150          instrument_expression(node, f);
1151          Stream_write_string(f, ";\n");
1152          break;
1153        default:
1154          abort();
1155          break;
1156        }
1157        break;
1158      case TOK_DEBUGGER:
1159        Stream_printf(f, "%*s", indent, "");
1160        Stream_write_string(f, "debugger;\n");
1161      break;      break;
1162    default:    default:
1163      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 1169 
1169  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
1170  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
1171  */  */
1172  static void instrument_statement(JSParseNode * node, Stream * f, int indent) {  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
1173    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC && node->pn_type != TOK_LEXICALSCOPE) {
1174      int line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
1175        if (line > num_lines) {
1176          fatal("%s: script contains more than 65,535 lines", file_id);
1177        }
1178    
1179      /* the root node has line number 0 */      /* the root node has line number 0 */
1180      if (line != 0) {      if (line != 0) {
1181        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
# Line 824  Line 1183 
1183        lines[line - 1] = 1;        lines[line - 1] = 1;
1184      }      }
1185    }    }
1186    output_statement(node, f, indent);    output_statement(node, f, indent, is_jscoverage_if);
1187  }  }
1188    
1189  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) {
1190    file_id = id;    const jschar * characters_end = characters + line_end;
1191      const jschar * cp = characters + line_start;
1192    /* scan the javascript */    const char * bp = prefix;
1193    size_t input_length = input->length;    for (;;) {
1194    jschar * base = js_InflateString(context, (char *) input->data, &input_length);      if (*bp == '\0') {
1195    if (base == NULL) {        return true;
1196      fatal("out of memory");      }
1197        else if (cp == characters_end) {
1198          return false;
1199        }
1200        else if (*cp != *bp) {
1201          return false;
1202        }
1203        bp++;
1204        cp++;
1205    }    }
1206    JSTokenStream * token_stream = js_NewTokenStream(context, base, input_length, NULL, 1, NULL);  }
1207    if (token_stream == NULL) {  
1208      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) {
1209      /* XXX - other Unicode space */
1210      const jschar * end = characters + line_end;
1211      for (const jschar * p = characters + line_start; p < end; p++) {
1212        jschar c = *p;
1213        if (c == 0x9 || c == 0xB || c == 0xC || c == 0x20 || c == 0xA0) {
1214          continue;
1215        }
1216        else {
1217          return false;
1218        }
1219    }    }
1220      return true;
1221    }
1222    
1223    static void error_reporter(JSContext * context, const char * message, JSErrorReport * report) {
1224      fprintf(stderr, "jscoverage: parse error: line %u: %s\n", report->lineno, message);
1225    }
1226    
1227    void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) {
1228      file_id = id;
1229    
1230    /* parse the javascript */    /* parse the javascript */
1231    JSParseNode * node = js_ParseTokenStream(context, global, token_stream);    JSParseContext parse_context;
1232      if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) {
1233        fatal("cannot create token stream from file: %s", file_id);
1234      }
1235      JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);
1236      JSParseNode * node = js_ParseScript(context, global, &parse_context);
1237    if (node == NULL) {    if (node == NULL) {
1238        js_ReportUncaughtException(context);
1239      fatal("parse error in file: %s", file_id);      fatal("parse error in file: %s", file_id);
1240    }    }
1241    int num_lines = node->pn_pos.end.lineno;    JS_SetErrorReporter(context, old_error_reporter);
1242      num_lines = node->pn_pos.end.lineno;
1243    lines = xmalloc(num_lines);    lines = xmalloc(num_lines);
1244    for (int i = 0; i < num_lines; i++) {    for (unsigned int i = 0; i < num_lines; i++) {
1245      lines[i] = 0;      lines[i] = 0;
1246    }    }
1247    
1248      /* search code for conditionals */
1249      exclusive_directives = xnew(bool, num_lines);
1250      for (unsigned int i = 0; i < num_lines; i++) {
1251        exclusive_directives[i] = false;
1252      }
1253    
1254      bool has_conditionals = false;
1255      struct IfDirective * if_directives = NULL;
1256      size_t line_number = 0;
1257      size_t i = 0;
1258      while (i < num_characters) {
1259        if (line_number == UINT16_MAX) {
1260          fatal("%s: script has more than 65,535 lines", file_id);
1261        }
1262        line_number++;
1263        size_t line_start = i;
1264        jschar c;
1265        bool done = false;
1266        while (! done && i < num_characters) {
1267          c = characters[i];
1268          switch (c) {
1269          case '\r':
1270          case '\n':
1271          case 0x2028:
1272          case 0x2029:
1273            done = true;
1274            break;
1275          default:
1276            i++;
1277          }
1278        }
1279        size_t line_end = i;
1280        if (i < num_characters) {
1281          i++;
1282          if (c == '\r' && i < num_characters && characters[i] == '\n') {
1283            i++;
1284          }
1285        }
1286    
1287        if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) {
1288          has_conditionals = true;
1289    
1290          if (characters_are_white_space(characters, line_start + 16, line_end)) {
1291            exclusive_directives[line_number - 1] = true;
1292          }
1293          else {
1294            struct IfDirective * if_directive = xnew(struct IfDirective, 1);
1295            if_directive->condition_start = characters + line_start + 16;
1296            if_directive->condition_end = characters + line_end;
1297            if_directive->start_line = line_number;
1298            if_directive->end_line = 0;
1299            if_directive->next = if_directives;
1300            if_directives = if_directive;
1301          }
1302        }
1303        else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) {
1304          for (struct IfDirective * p = if_directives; p != NULL; p = p->next) {
1305            if (p->end_line == 0) {
1306              p->end_line = line_number;
1307              break;
1308            }
1309          }
1310        }
1311      }
1312    
1313    /*    /*
1314    An instrumented JavaScript file has 3 sections:    An instrumented JavaScript file has 4 sections:
1315    1. initialization    1. initialization
1316    2. instrumented source code    2. instrumented source code
1317    3. original source code    3. conditionals
1318      4. original source code
1319    */    */
1320    
1321    Stream * instrumented = Stream_new(0);    Stream * instrumented = Stream_new(0);
1322    instrument_statement(node, instrumented, 0);    instrument_statement(node, instrumented, 0, false);
1323      js_FinishParseContext(context, &parse_context);
1324    
1325    /* write line number info to the output */    /* write line number info to the output */
1326    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 1336 
1336    Stream_write_string(output, "}\n");    Stream_write_string(output, "}\n");
1337    free(lines);    free(lines);
1338    lines = NULL;    lines = NULL;
1339      free(exclusive_directives);
1340      exclusive_directives = NULL;
1341    
1342      /* conditionals */
1343      if (has_conditionals) {
1344        Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);
1345      }
1346    
1347    /* copy the instrumented source code to the output */    /* copy the instrumented source code to the output */
1348    Stream_write(output, instrumented->data, instrumented->length);    Stream_write(output, instrumented->data, instrumented->length);
   Stream_write_char(output, '\n');  
1349    
1350    /* conditionals */    /* conditionals */
1351    bool has_conditionals = false;    for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) {
1352    size_t line_number = 0;      Stream_write_string(output, "if (!(");
1353    size_t i = 0;      print_javascript(if_directive->condition_start, if_directive->condition_end - if_directive->condition_start, output);
1354    while (i < input_length) {      Stream_write_string(output, ")) {\n");
1355      line_number++;      Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line);
1356      size_t line_start = i;      Stream_write_string(output, "}\n");
1357      while (i < input_length && base[i] != '\r' && base[i] != '\n') {    }
1358        i++;  
1359      }    /* free */
1360      size_t line_end = i;    while (if_directives != NULL) {
1361      if (i < input_length) {      struct IfDirective * if_directive = if_directives;
1362        if (base[i] == '\r') {      if_directives = if_directives->next;
1363          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);  
1364    }    }
1365    
1366    /* copy the original source to the output */    /* copy the original source to the output */
1367    i = 0;    Stream_printf(output, "_$jscoverage['%s'].source = ", file_id);
1368    while (i < input_length) {    jscoverage_write_source(id, characters, num_characters, output);
1369      Stream_write_string(output, "// ");    Stream_printf(output, ";\n");
     size_t line_start = i;  
     while (i < input_length && base[i] != '\r' && base[i] != '\n') {  
       i++;  
     }  
1370    
1371      size_t line_end = i;    Stream_delete(instrumented);
1372      if (i < input_length) {  
1373        if (base[i] == '\r') {    file_id = NULL;
1374          line_end = i;  }
1375    
1376    void jscoverage_write_source(const char * id, const jschar * characters, size_t num_characters, Stream * output) {
1377      Stream_write_string(output, "[");
1378      if (jscoverage_highlight) {
1379        Stream * highlighted_stream = Stream_new(num_characters);
1380        jscoverage_highlight_js(context, id, characters, num_characters, highlighted_stream);
1381        size_t i = 0;
1382        while (i < highlighted_stream->length) {
1383          if (i > 0) {
1384            Stream_write_char(output, ',');
1385          }
1386    
1387          Stream_write_char(output, '"');
1388          bool done = false;
1389          while (! done) {
1390            char c = highlighted_stream->data[i];
1391            switch (c) {
1392            case 0x8:
1393              /* backspace */
1394              Stream_write_string(output, "\\b");
1395              break;
1396            case 0x9:
1397              /* horizontal tab */
1398              Stream_write_string(output, "\\t");
1399              break;
1400            case 0xa:
1401              /* line feed (new line) */
1402              done = true;
1403              break;
1404            /* IE doesn't support this */
1405            /*
1406            case 0xb:
1407              Stream_write_string(output, "\\v");
1408              break;
1409            */
1410            case 0xc:
1411              /* form feed */
1412              Stream_write_string(output, "\\f");
1413              break;
1414            case 0xd:
1415              /* carriage return */
1416              done = true;
1417              if (i + 1 < highlighted_stream->length && highlighted_stream->data[i + 1] == '\n') {
1418                i++;
1419              }
1420              break;
1421            case '"':
1422              Stream_write_string(output, "\\\"");
1423              break;
1424            case '\\':
1425              Stream_write_string(output, "\\\\");
1426              break;
1427            default:
1428              Stream_write_char(output, c);
1429              break;
1430            }
1431          i++;          i++;
1432          if (i < input_length && base[i] == '\n') {          if (i >= highlighted_stream->length) {
1433            i++;            done = true;
1434          }          }
1435        }        }
1436        else if (base[i] == '\n') {        Stream_write_char(output, '"');
1437          line_end = i;      }
1438        Stream_delete(highlighted_stream);
1439      }
1440      else {
1441        size_t i = 0;
1442        while (i < num_characters) {
1443          if (i > 0) {
1444            Stream_write_char(output, ',');
1445          }
1446    
1447          Stream_write_char(output, '"');
1448          bool done = false;
1449          while (! done) {
1450            jschar c = characters[i];
1451            switch (c) {
1452            case 0x8:
1453              /* backspace */
1454              Stream_write_string(output, "\\b");
1455              break;
1456            case 0x9:
1457              /* horizontal tab */
1458              Stream_write_string(output, "\\t");
1459              break;
1460            case 0xa:
1461              /* line feed (new line) */
1462              done = true;
1463              break;
1464            /* IE doesn't support this */
1465            /*
1466            case 0xb:
1467              Stream_write_string(output, "\\v");
1468              break;
1469            */
1470            case 0xc:
1471              /* form feed */
1472              Stream_write_string(output, "\\f");
1473              break;
1474            case 0xd:
1475              /* carriage return */
1476              done = true;
1477              if (i + 1 < num_characters && characters[i + 1] == '\n') {
1478                i++;
1479              }
1480              break;
1481            case '"':
1482              Stream_write_string(output, "\\\"");
1483              break;
1484            case '\\':
1485              Stream_write_string(output, "\\\\");
1486              break;
1487            case '&':
1488              Stream_write_string(output, "&amp;");
1489              break;
1490            case '<':
1491              Stream_write_string(output, "&lt;");
1492              break;
1493            case '>':
1494              Stream_write_string(output, "&gt;");
1495              break;
1496            case 0x2028:
1497            case 0x2029:
1498              done = true;
1499              break;
1500            default:
1501              if (32 <= c && c <= 126) {
1502                Stream_write_char(output, c);
1503              }
1504              else {
1505                Stream_printf(output, "&#%d;", c);
1506              }
1507              break;
1508            }
1509          i++;          i++;
1510            if (i >= num_characters) {
1511              done = true;
1512            }
1513        }        }
1514        else {        Stream_write_char(output, '"');
         abort();  
       }  
1515      }      }
   
     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);  
1516    }    }
1517      Stream_write_string(output, "]");
   Stream_delete(instrumented);  
   
   JS_free(context, base);  
   
   file_id = NULL;  
1518  }  }
1519    
1520  void jscoverage_copy_resources(const char * destination_directory) {  void jscoverage_copy_resources(const char * destination_directory) {
# Line 970  Line 1523 
1523    copy_resource("jscoverage.js", destination_directory);    copy_resource("jscoverage.js", destination_directory);
1524    copy_resource("jscoverage-ie.css", destination_directory);    copy_resource("jscoverage-ie.css", destination_directory);
1525    copy_resource("jscoverage-throbber.gif", destination_directory);    copy_resource("jscoverage-throbber.gif", destination_directory);
1526    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);  
1527  }  }
1528    
1529  /*  /*
# Line 1007  Line 1558 
1558    JS_HashTableDestroy(coverage->coverage_table);    JS_HashTableDestroy(coverage->coverage_table);
1559    struct FileCoverageList * p = coverage->coverage_list;    struct FileCoverageList * p = coverage->coverage_list;
1560    while (p != NULL) {    while (p != NULL) {
1561      free(p->file_coverage->lines);      free(p->file_coverage->coverage_lines);
1562      free(p->file_coverage->source);      if (p->file_coverage->source_lines != NULL) {
1563          for (uint32 i = 0; i < p->file_coverage->num_source_lines; i++) {
1564            free(p->file_coverage->source_lines[i]);
1565          }
1566          free(p->file_coverage->source_lines);
1567        }
1568      free(p->file_coverage->id);      free(p->file_coverage->id);
1569      free(p->file_coverage);      free(p->file_coverage);
1570      struct FileCoverageList * q = p;      struct FileCoverageList * q = p;
# Line 1037  Line 1593 
1593  }  }
1594    
1595  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) {
1596      int result = 0;
1597    
1598    jschar * base = js_InflateString(context, (char *) json, &length);    jschar * base = js_InflateString(context, (char *) json, &length);
1599    if (base == NULL) {    if (base == NULL) {
1600      fatal("out of memory");      fatal("out of memory");
# Line 1049  Line 1607 
1607    
1608    JS_free(context, base);    JS_free(context, base);
1609    
1610    JSTokenStream * token_stream = js_NewTokenStream(context, parenthesized_json, length + 2, NULL, 1, NULL);    JSParseContext parse_context;
1611    if (token_stream == NULL) {    if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) {
1612      fatal("cannot create token stream");      free(parenthesized_json);
1613        return -1;
1614    }    }
1615      JSParseNode * root = js_ParseScript(context, global, &parse_context);
   JSParseNode * root = js_ParseTokenStream(context, global, token_stream);  
1616    free(parenthesized_json);    free(parenthesized_json);
1617    if (root == NULL) {    if (root == NULL) {
1618      return -1;      result = -1;
1619        goto done;
1620    }    }
1621    
1622    /* root node must be TOK_LC */    /* root node must be TOK_LC */
1623    if (root->pn_type != TOK_LC) {    if (root->pn_type != TOK_LC) {
1624      return -1;      result = -1;
1625        goto done;
1626    }    }
1627    JSParseNode * semi = root->pn_u.list.head;    JSParseNode * semi = root->pn_u.list.head;
1628    
1629    /* 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 */
1630    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {
1631      return -1;      result = -1;
1632        goto done;
1633    }    }
1634    JSParseNode * parenthesized = semi->pn_kid;    JSParseNode * parenthesized = semi->pn_kid;
1635    
1636    /* this must be a parenthesized expression */    /* this must be a parenthesized expression */
1637    if (parenthesized->pn_type != TOK_RP) {    if (parenthesized->pn_type != TOK_RP) {
1638      return -1;      result = -1;
1639        goto done;
1640    }    }
1641    JSParseNode * object = parenthesized->pn_kid;    JSParseNode * object = parenthesized->pn_kid;
1642    
1643    /* this must be an object literal */    /* this must be an object literal */
1644    if (object->pn_type != TOK_RC) {    if (object->pn_type != TOK_RC) {
1645      return -1;      result = -1;
1646        goto done;
1647    }    }
1648    
1649    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {
1650      /* every element of this list must be TOK_COLON */      /* every element of this list must be TOK_COLON */
1651      if (p->pn_type != TOK_COLON) {      if (p->pn_type != TOK_COLON) {
1652        return -1;        result = -1;
1653          goto done;
1654      }      }
1655    
1656      /* the key must be a string representing the file */      /* the key must be a string representing the file */
1657      JSParseNode * key = p->pn_left;      JSParseNode * key = p->pn_left;
1658      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {
1659        return -1;        result = -1;
1660          goto done;
1661      }      }
1662      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));
1663    
1664      /* the value must be an object literal OR an array */      /* the value must be an object literal OR an array */
1665      JSParseNode * value = p->pn_right;      JSParseNode * value = p->pn_right;
1666      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {
1667        return -1;        result = -1;
1668          goto done;
1669      }      }
1670    
1671      JSParseNode * array = NULL;      JSParseNode * array = NULL;
# Line 1111  Line 1677 
1677      else if (value->pn_type == TOK_RC) {      else if (value->pn_type == TOK_RC) {
1678        /* an object literal */        /* an object literal */
1679        if (value->pn_count != 2) {        if (value->pn_count != 2) {
1680          return -1;          result = -1;
1681            goto done;
1682        }        }
1683        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {
1684          if (element->pn_type != TOK_COLON) {          if (element->pn_type != TOK_COLON) {
1685            return -1;            result = -1;
1686              goto done;
1687          }          }
1688          JSParseNode * left = element->pn_left;          JSParseNode * left = element->pn_left;
1689          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {
1690            return -1;            result = -1;
1691              goto done;
1692          }          }
1693          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));
1694          if (strcmp(s, "coverage") == 0) {          if (strcmp(s, "coverage") == 0) {
1695            array = element->pn_right;            array = element->pn_right;
1696            if (array->pn_type != TOK_RB) {            if (array->pn_type != TOK_RB) {
1697              return -1;              result = -1;
1698                goto done;
1699            }            }
1700          }          }
1701          else if (strcmp(s, "source") == 0) {          else if (strcmp(s, "source") == 0) {
1702            source = element->pn_right;            source = element->pn_right;
1703            if (source->pn_type != TOK_STRING || ! ATOM_IS_STRING(source->pn_atom)) {            if (source->pn_type != TOK_RB) {
1704              return -1;              result = -1;
1705                goto done;
1706            }            }
1707          }          }
1708          else {          else {
1709            return -1;            result = -1;
1710              goto done;
1711          }          }
1712        }        }
1713      }      }
1714      else {      else {
1715        return -1;        result = -1;
1716          goto done;
1717      }      }
1718    
1719      if (array == NULL) {      if (array == NULL) {
1720        return -1;        result = -1;
1721          goto done;
1722      }      }
1723    
1724      /* look up the file in the coverage table */      /* look up the file in the coverage table */
# Line 1154  Line 1728 
1728        char * id = xstrdup(id_bytes);        char * id = xstrdup(id_bytes);
1729        file_coverage = xmalloc(sizeof(FileCoverage));        file_coverage = xmalloc(sizeof(FileCoverage));
1730        file_coverage->id = id;        file_coverage->id = id;
1731        file_coverage->num_lines = array->pn_count - 1;        file_coverage->num_coverage_lines = array->pn_count;
1732        file_coverage->lines = xnew(int, array->pn_count);        file_coverage->coverage_lines = xnew(int, array->pn_count);
1733        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)));  
       }  
1734    
1735        /* set coverage for all lines */        /* set coverage for all lines */
1736        uint32 i = 0;        uint32 i = 0;
1737        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++) {
1738          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1739            file_coverage->lines[i] = (int) element->pn_dval;            file_coverage->coverage_lines[i] = (int) element->pn_dval;
1740          }          }
1741          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1742            file_coverage->lines[i] = -1;            file_coverage->coverage_lines[i] = -1;
1743          }          }
1744          else {          else {
1745            return -1;            result = -1;
1746              goto done;
1747          }          }
1748        }        }
1749        assert(i == array->pn_count);        assert(i == array->pn_count);
# Line 1188  Line 1758 
1758      else {      else {
1759        /* sanity check */        /* sanity check */
1760        assert(strcmp(file_coverage->id, id_bytes) == 0);        assert(strcmp(file_coverage->id, id_bytes) == 0);
1761        if (file_coverage->num_lines != array->pn_count - 1) {        if (file_coverage->num_coverage_lines != array->pn_count) {
1762          return -2;          result = -2;
1763            goto done;
1764        }        }
1765    
1766        /* merge the coverage */        /* merge the coverage */
1767        uint32 i = 0;        uint32 i = 0;
1768        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++) {
1769          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1770            if (file_coverage->lines[i] == -1) {            if (file_coverage->coverage_lines[i] == -1) {
1771              return -2;              result = -2;
1772                goto done;
1773            }            }
1774            file_coverage->lines[i] += (int) element->pn_dval;            file_coverage->coverage_lines[i] += (int) element->pn_dval;
1775          }          }
1776          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1777            if (file_coverage->lines[i] != -1) {            if (file_coverage->coverage_lines[i] != -1) {
1778              return -2;              result = -2;
1779                goto done;
1780            }            }
1781          }          }
1782          else {          else {
1783            return -1;            result = -1;
1784              goto done;
1785          }          }
1786        }        }
1787        assert(i == array->pn_count);        assert(i == array->pn_count);
1788        }
1789    
1790        /* if this JSON file has source, use it */      /* if this JSON file has source, use it */
1791        if (file_coverage->source == NULL && source != NULL) {      if (file_coverage->source_lines == NULL && source != NULL) {
1792          file_coverage->source = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(source->pn_atom)));        file_coverage->num_source_lines = source->pn_count;
1793          file_coverage->source_lines = xnew(char *, source->pn_count);
1794          uint32 i = 0;
1795          for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {
1796            if (element->pn_type != TOK_STRING) {
1797              result = -1;
1798              goto done;
1799            }
1800            file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));
1801        }        }
1802          assert(i == source->pn_count);
1803      }      }
1804    }    }
1805    
1806    return 0;  done:
1807      js_FinishParseContext(context, &parse_context);
1808      return result;
1809  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.24