/[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 87 by siliconforks, Mon May 5 20:05:27 2008 UTC revision 348 by siliconforks, Fri Oct 24 16:17:00 2008 UTC
# Line 17  Line 17 
17      51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.      51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */  */
19    
20    #include <config.h>
21    
22  #include "instrument-js.h"  #include "instrument-js.h"
23    
24  #include <assert.h>  #include <assert.h>
# Line 24  Line 26 
26  #include <string.h>  #include <string.h>
27    
28  #include <jsapi.h>  #include <jsapi.h>
29    #include <jsarena.h>
30  #include <jsatom.h>  #include <jsatom.h>
31    #include <jsexn.h>
32  #include <jsfun.h>  #include <jsfun.h>
33  #include <jsinterp.h>  #include <jsinterp.h>
34    #include <jsiter.h>
35  #include <jsparse.h>  #include <jsparse.h>
36  #include <jsregexp.h>  #include <jsregexp.h>
37  #include <jsscope.h>  #include <jsscope.h>
38  #include <jsstr.h>  #include <jsstr.h>
39    
40    #include "encoding.h"
41    #include "global.h"
42    #include "highlight.h"
43    #include "resource-manager.h"
44  #include "util.h"  #include "util.h"
45    
46    struct IfDirective {
47      const jschar * condition_start;
48      const jschar * condition_end;
49      uint16_t start_line;
50      uint16_t end_line;
51      struct IfDirective * next;
52    };
53    
54    static bool * exclusive_directives = NULL;
55    
56  static JSRuntime * runtime = NULL;  static JSRuntime * runtime = NULL;
57  static JSContext * context = NULL;  static JSContext * context = NULL;
58  static JSObject * global = NULL;  static JSObject * global = NULL;
59    static JSVersion js_version = JSVERSION_ECMA_3;
60    
61  /*  /*
62  JSParseNode objects store line numbers starting from 1.  JSParseNode objects store line numbers starting from 1.
# Line 44  Line 64 
64  */  */
65  static const char * file_id = NULL;  static const char * file_id = NULL;
66  static char * lines = NULL;  static char * lines = NULL;
67    static uint16_t num_lines = 0;
68    
69    void jscoverage_set_js_version(const char * version) {
70      js_version = atoi(version);
71    }
72    
73  void jscoverage_init(void) {  void jscoverage_init(void) {
74    runtime = JS_NewRuntime(8L * 1024L * 1024L);    runtime = JS_NewRuntime(8L * 1024L * 1024L);
# Line 56  Line 81 
81      fatal("cannot create context");      fatal("cannot create context");
82    }    }
83    
84      JS_SetVersion(context, js_version);
85    
86    global = JS_NewObject(context, NULL, NULL, NULL);    global = JS_NewObject(context, NULL, NULL, NULL);
87    if (global == NULL) {    if (global == NULL) {
88      fatal("cannot create global object");      fatal("cannot create global object");
# Line 71  Line 98 
98    JS_DestroyRuntime(runtime);    JS_DestroyRuntime(runtime);
99  }  }
100    
101  static void print_string(JSString * s, FILE * f) {  static void print_javascript(const jschar * characters, size_t num_characters, Stream * f) {
102    for (int i = 0; i < s->length; i++) {    for (size_t i = 0; i < num_characters; i++) {
103      char c = s->chars[i];      jschar c = characters[i];
104      fputc(c, f);      /*
105        XXX does not handle no-break space, other unicode "space separator"
106        */
107        switch (c) {
108        case 0x9:
109        case 0xB:
110        case 0xC:
111          Stream_write_char(f, c);
112          break;
113        default:
114          if (32 <= c && c <= 126) {
115            Stream_write_char(f, c);
116          }
117          else {
118            Stream_printf(f, "\\u%04x", c);
119          }
120          break;
121        }
122      }
123    }
124    
125    static void print_string(JSString * s, Stream * f) {
126      size_t length = JSSTRING_LENGTH(s);
127      jschar * characters = JSSTRING_CHARS(s);
128      for (size_t i = 0; i < length; i++) {
129        jschar c = characters[i];
130        if (32 <= c && c <= 126) {
131          switch (c) {
132          case '"':
133            Stream_write_string(f, "\\\"");
134            break;
135    /*
136          case '\'':
137            Stream_write_string(f, "\\'");
138            break;
139    */
140          case '\\':
141            Stream_write_string(f, "\\\\");
142            break;
143          default:
144            Stream_write_char(f, c);
145            break;
146          }
147        }
148        else {
149          switch (c) {
150          case 0x8:
151            Stream_write_string(f, "\\b");
152            break;
153          case 0x9:
154            Stream_write_string(f, "\\t");
155            break;
156          case 0xa:
157            Stream_write_string(f, "\\n");
158            break;
159          /* IE doesn't support this */
160          /*
161          case 0xb:
162            Stream_write_string(f, "\\v");
163            break;
164          */
165          case 0xc:
166            Stream_write_string(f, "\\f");
167            break;
168          case 0xd:
169            Stream_write_string(f, "\\r");
170            break;
171          default:
172            Stream_printf(f, "\\u%04x", c);
173            break;
174          }
175        }
176    }    }
177  }  }
178    
179  static void print_string_atom(JSAtom * atom, FILE * f) {  static void print_string_atom(JSAtom * atom, Stream * f) {
180    assert(ATOM_IS_STRING(atom));    assert(ATOM_IS_STRING(atom));
181    JSString * s = ATOM_TO_STRING(atom);    JSString * s = ATOM_TO_STRING(atom);
182    print_string(s, f);    print_string(s, f);
183  }  }
184    
185  static void print_string_jsval(jsval value, FILE * f) {  static void print_regex(jsval value, Stream * f) {
186    assert(JSVAL_IS_STRING(value));    assert(JSVAL_IS_STRING(value));
187    JSString * s = JSVAL_TO_STRING(value);    JSString * s = JSVAL_TO_STRING(value);
188    print_string(s, f);    size_t length = JSSTRING_LENGTH(s);
189      jschar * characters = JSSTRING_CHARS(s);
190      for (size_t i = 0; i < length; i++) {
191        jschar c = characters[i];
192        if (32 <= c && c <= 126) {
193          Stream_write_char(f, c);
194        }
195        else {
196          Stream_printf(f, "\\u%04x", c);
197        }
198      }
199  }  }
200    
201  static void print_quoted_string_atom(JSAtom * atom, FILE * f) {  static void print_quoted_string_atom(JSAtom * atom, Stream * f) {
202    assert(ATOM_IS_STRING(atom));    assert(ATOM_IS_STRING(atom));
203    JSString * s = ATOM_TO_STRING(atom);    JSString * s = ATOM_TO_STRING(atom);
204    JSString * quoted = js_QuoteString(context, s, '"');    Stream_write_char(f, '"');
205    print_string(quoted, f);    print_string(s, f);
206      Stream_write_char(f, '"');
207  }  }
208    
209  static const char * get_op(uint8 op) {  static const char * get_op(uint8 op) {
210    switch(op) {    switch(op) {
211      case JSOP_OR:
212        return "||";
213      case JSOP_AND:
214        return "&&";
215    case JSOP_BITOR:    case JSOP_BITOR:
216      return "|";      return "|";
217    case JSOP_BITXOR:    case JSOP_BITXOR:
# Line 109  Line 222 
222      return "==";      return "==";
223    case JSOP_NE:    case JSOP_NE:
224      return "!=";      return "!=";
225    case JSOP_NEW_EQ:    case JSOP_STRICTEQ:
226      return "===";      return "===";
227    case JSOP_NEW_NE:    case JSOP_STRICTNE:
228      return "!==";      return "!==";
229    case JSOP_LT:    case JSOP_LT:
230      return "<";      return "<";
# Line 142  Line 255 
255    }    }
256  }  }
257    
258  static void instrument_expression(JSParseNode * node, FILE * f);  static void instrument_expression(JSParseNode * node, Stream * f);
259  static void instrument_statement(JSParseNode * node, FILE * f, int indent);  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
260    static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
261    
262    enum FunctionType {
263      FUNCTION_NORMAL,
264      FUNCTION_GETTER_OR_SETTER
265    };
266    
267    static void output_for_in(JSParseNode * node, Stream * f) {
268      assert(node->pn_type == TOK_FOR);
269      assert(node->pn_arity == PN_BINARY);
270      Stream_write_string(f, "for ");
271      if (node->pn_iflags & JSITER_FOREACH) {
272        Stream_write_string(f, "each ");
273      }
274      Stream_write_char(f, '(');
275      instrument_expression(node->pn_left, f);
276      Stream_write_char(f, ')');
277    }
278    
279    static void instrument_function(JSParseNode * node, Stream * f, int indent, enum FunctionType type) {
280      assert(node->pn_type == TOK_FUNCTION);
281      assert(node->pn_arity == PN_FUNC);
282      JSObject * object = node->pn_funpob->object;
283      assert(JS_ObjectIsFunction(context, object));
284      JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
285      assert(function);
286      assert(object == &function->object);
287      Stream_printf(f, "%*s", indent, "");
288      if (type == FUNCTION_NORMAL) {
289        Stream_write_string(f, "function");
290      }
291    
292      /* function name */
293      if (function->atom) {
294        Stream_write_char(f, ' ');
295        print_string_atom(function->atom, f);
296      }
297    
298  static void instrument_function(JSParseNode * node, FILE * f, int indent) {    /*
299      assert(node->pn_arity == PN_FUNC);    function parameters - see JS_DecompileFunction in jsapi.cpp, which calls
300      assert(ATOM_IS_OBJECT(node->pn_funAtom));    js_DecompileFunction in jsopcode.cpp
301      JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom);    */
302      assert(JS_ObjectIsFunction(context, object));    Stream_write_string(f, "(");
303      JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);    JSArenaPool pool;
304      assert(function);    JS_INIT_ARENA_POOL(&pool, "instrument_function", 256, 1, &context->scriptStackQuota);
305      assert(object == function->object);    jsuword * local_names = NULL;
306      fprintf(f, "%*s", indent, "");    if (JS_GET_LOCAL_NAME_COUNT(function)) {
307      fprintf(f, "function");      local_names = js_GetLocalNameArray(context, function, &pool);
308        if (local_names == NULL) {
309      /* function name */        fatal("out of memory");
     if (function->atom) {  
       fputc(' ', f);  
       print_string_atom(function->atom, f);  
     }  
   
     /* function parameters */  
     fprintf(f, "(");  
     JSAtom ** params = xmalloc(function->nargs * sizeof(JSAtom *));  
     for (int i = 0; i < function->nargs; i++) {  
       /* initialize to NULL for sanity check */  
       params[i] = NULL;  
     }  
     JSScope * scope = OBJ_SCOPE(object);  
     for (JSScopeProperty * scope_property = SCOPE_LAST_PROP(scope); scope_property != NULL; scope_property = scope_property->parent) {  
       if (scope_property->getter != js_GetArgument) {  
         continue;  
       }  
       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);  
310      }      }
311      for (int i = 0; i < function->nargs; i++) {    }
312        assert(params[i] != NULL);    for (int i = 0; i < function->nargs; i++) {
313        if (i > 0) {      if (i > 0) {
314          fprintf(f, ", ");        Stream_write_string(f, ", ");
       }  
       if (ATOM_IS_STRING(params[i])) {  
         print_string_atom(params[i], f);  
       }  
315      }      }
316      fprintf(f, ") {\n");      JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]);
317      free(params);      print_string_atom(param, f);
318      }
319      JS_FinishArenaPool(&pool);
320      Stream_write_string(f, ") {\n");
321    
322      /* function body */
323      if (function->flags & JSFUN_EXPR_CLOSURE) {
324        /* expression closure */
325        output_statement(node->pn_body, f, indent + 2, false);
326      }
327      else {
328        instrument_statement(node->pn_body, f, indent + 2, false);
329      }
330    
331      /* function body */    Stream_write_string(f, "}\n");
332      instrument_statement(node->pn_body, f, indent + 2);  }
333    
334      fprintf(f, "}\n");  static void instrument_function_call(JSParseNode * node, Stream * f) {
335      if (node->pn_head->pn_type == TOK_FUNCTION) {
336        /* it's a generator expression */
337        JSParseNode * function_node = node->pn_head;
338        JSParseNode * lexical_scope_node = function_node->pn_body;
339        assert(lexical_scope_node->pn_type == TOK_LEXICALSCOPE);
340        assert(lexical_scope_node->pn_arity == PN_NAME);
341        JSParseNode * for_node = lexical_scope_node->pn_body;
342        assert(for_node->pn_type == TOK_FOR);
343        assert(for_node->pn_arity == PN_BINARY);
344        JSParseNode * if_node = NULL;
345        JSParseNode * semi_node;
346        switch (for_node->pn_right->pn_type) {
347        case TOK_SEMI:
348          semi_node = for_node->pn_right;
349          break;
350        case TOK_IF:
351          if_node = for_node->pn_right;
352          assert(if_node->pn_arity == PN_TERNARY);
353          semi_node = if_node->pn_kid2;
354          assert(semi_node->pn_type == TOK_SEMI);
355          break;
356        default:
357          abort();
358          break;
359        }
360        assert(semi_node->pn_arity == PN_UNARY);
361        JSParseNode * yield_node = semi_node->pn_kid;
362        assert(yield_node->pn_type == TOK_YIELD);
363        Stream_write_char(f, '(');
364        instrument_expression(yield_node->pn_kid, f);
365        Stream_write_char(f, ' ');
366        output_for_in(for_node, f);
367        if (if_node) {
368          Stream_write_string(f, " if (");
369          instrument_expression(if_node->pn_kid1, f);
370          Stream_write_char(f, ')');
371        }
372        Stream_write_char(f, ')');
373      }
374      else {
375        instrument_expression(node->pn_head, f);
376        Stream_write_char(f, '(');
377        for (struct JSParseNode * p = node->pn_head->pn_next; p != NULL; p = p->pn_next) {
378          if (p != node->pn_head->pn_next) {
379            Stream_write_string(f, ", ");
380          }
381          instrument_expression(p, f);
382        }
383        Stream_write_char(f, ')');
384      }
385  }  }
386    
387  static void instrument_function_call(JSParseNode * node, FILE * f) {  static void instrument_declarations(JSParseNode * list, Stream * f) {
388    instrument_expression(node->pn_head, f);    assert(list->pn_arity == PN_LIST);
389    fputc('(', f);    for (JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
390    for (struct JSParseNode * p = node->pn_head->pn_next; p != NULL; p = p->pn_next) {      switch (p->pn_type) {
391      if (p != node->pn_head->pn_next) {      case TOK_NAME:
392        fprintf(f, ", ");        assert(p->pn_arity == PN_NAME);
393          if (p != list->pn_head) {
394            Stream_write_string(f, ", ");
395          }
396          print_string_atom(p->pn_atom, f);
397          if (p->pn_expr != NULL) {
398            Stream_write_string(f, " = ");
399            instrument_expression(p->pn_expr, f);
400          }
401          break;
402        case TOK_ASSIGN:
403        case TOK_RB:
404        case TOK_RC:
405          /* destructuring */
406          instrument_expression(p, f);
407          break;
408        default:
409          abort();
410          break;
411      }      }
     instrument_expression(p, f);  
412    }    }
   fputc(')', f);  
413  }  }
414    
415  /*  /*
# Line 222  Line 425 
425  TOK_INSTANCEOF  binary  TOK_INSTANCEOF  binary
426  TOK_IN          binary  TOK_IN          binary
427  */  */
428  static void instrument_expression(JSParseNode * node, FILE * f) {  static void instrument_expression(JSParseNode * node, Stream * f) {
429    switch (node->pn_type) {    switch (node->pn_type) {
430    case TOK_FUNCTION:    case TOK_FUNCTION:
431      instrument_function(node, f, 0);      instrument_function(node, f, 0, FUNCTION_NORMAL);
432      break;      break;
433    case TOK_COMMA:    case TOK_COMMA:
434      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) {
435        if (p != node->pn_head) {        if (p != node->pn_head) {
436          fprintf(f, ", ");          Stream_write_string(f, ", ");
437        }        }
438        instrument_expression(p, f);        instrument_expression(p, f);
439      }      }
440      break;      break;
441    case TOK_ASSIGN:    case TOK_ASSIGN:
442      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
443      fputc(' ', f);      Stream_write_char(f, ' ');
444      switch (node->pn_op) {      switch (node->pn_op) {
445      case JSOP_ADD:      case JSOP_ADD:
446      case JSOP_SUB:      case JSOP_SUB:
# Line 250  Line 453 
453      case JSOP_BITOR:      case JSOP_BITOR:
454      case JSOP_BITXOR:      case JSOP_BITXOR:
455      case JSOP_DIV:      case JSOP_DIV:
456        fprintf(f, "%s", get_op(node->pn_op));        Stream_printf(f, "%s", get_op(node->pn_op));
457        break;        break;
458      default:      default:
459        /* do nothing - it must be a simple assignment */        /* do nothing - it must be a simple assignment */
460        break;        break;
461      }      }
462      fprintf(f, "= ");      Stream_write_string(f, "= ");
463      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
464      break;      break;
465    case TOK_HOOK:    case TOK_HOOK:
466      instrument_expression(node->pn_kid1, f);      instrument_expression(node->pn_kid1, f);
467      fprintf(f, "? ");      Stream_write_string(f, "? ");
468      instrument_expression(node->pn_kid2, f);      instrument_expression(node->pn_kid2, f);
469      fprintf(f, ": ");      Stream_write_string(f, ": ");
470      instrument_expression(node->pn_kid3, f);      instrument_expression(node->pn_kid3, f);
471      break;      break;
472    case TOK_OR:    case TOK_OR:
     instrument_expression(node->pn_left, f);  
     fprintf(f, " || ");  
     instrument_expression(node->pn_right, f);  
     break;  
473    case TOK_AND:    case TOK_AND:
     instrument_expression(node->pn_left, f);  
     fprintf(f, " && ");  
     instrument_expression(node->pn_right, f);  
     break;  
474    case TOK_BITOR:    case TOK_BITOR:
475    case TOK_BITXOR:    case TOK_BITXOR:
476    case TOK_BITAND:    case TOK_BITAND:
# Line 289  Line 484 
484      switch (node->pn_arity) {      switch (node->pn_arity) {
485      case PN_BINARY:      case PN_BINARY:
486        instrument_expression(node->pn_left, f);        instrument_expression(node->pn_left, f);
487        fprintf(f, " %s ", get_op(node->pn_op));        Stream_printf(f, " %s ", get_op(node->pn_op));
488        instrument_expression(node->pn_right, f);        instrument_expression(node->pn_right, f);
489        break;        break;
490      case PN_LIST:      case PN_LIST:
491        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) {
492          if (p != node->pn_head) {          if (p != node->pn_head) {
493            fprintf(f, " %s ", get_op(node->pn_op));            Stream_printf(f, " %s ", get_op(node->pn_op));
494          }          }
495          instrument_expression(p, f);          instrument_expression(p, f);
496        }        }
# Line 307  Line 502 
502    case TOK_UNARYOP:    case TOK_UNARYOP:
503      switch (node->pn_op) {      switch (node->pn_op) {
504      case JSOP_NEG:      case JSOP_NEG:
505        fputc('-', f);        Stream_write_char(f, '-');
506        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
507        break;        break;
508      case JSOP_POS:      case JSOP_POS:
509        fputc('+', f);        Stream_write_char(f, '+');
510        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
511        break;        break;
512      case JSOP_NOT:      case JSOP_NOT:
513        fputc('!', f);        Stream_write_char(f, '!');
514        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
515        break;        break;
516      case JSOP_BITNOT:      case JSOP_BITNOT:
517        fputc('~', f);        Stream_write_char(f, '~');
518        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
519        break;        break;
520      case JSOP_TYPEOF:      case JSOP_TYPEOF:
521        fprintf(f, "typeof ");        Stream_write_string(f, "typeof ");
522        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
523        break;        break;
524      case JSOP_VOID:      case JSOP_VOID:
525        fprintf(f, "void ");        Stream_write_string(f, "void ");
526        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
527        break;        break;
528      default:      default:
# Line 344  Line 539 
539      case JSOP_INCNAME:      case JSOP_INCNAME:
540      case JSOP_INCPROP:      case JSOP_INCPROP:
541      case JSOP_INCELEM:      case JSOP_INCELEM:
542        fprintf(f, "++");        Stream_write_string(f, "++");
543        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
544        break;        break;
545      case JSOP_DECNAME:      case JSOP_DECNAME:
546      case JSOP_DECPROP:      case JSOP_DECPROP:
547      case JSOP_DECELEM:      case JSOP_DECELEM:
548        fprintf(f, "--");        Stream_write_string(f, "--");
549        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
550        break;        break;
551      case JSOP_NAMEINC:      case JSOP_NAMEINC:
552      case JSOP_PROPINC:      case JSOP_PROPINC:
553      case JSOP_ELEMINC:      case JSOP_ELEMINC:
554        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
555        fprintf(f, "++");        Stream_write_string(f, "++");
556        break;        break;
557      case JSOP_NAMEDEC:      case JSOP_NAMEDEC:
558      case JSOP_PROPDEC:      case JSOP_PROPDEC:
559      case JSOP_ELEMDEC:      case JSOP_ELEMDEC:
560        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
561        fprintf(f, "--");        Stream_write_string(f, "--");
562        break;        break;
563      default:      default:
564        abort();        abort();
# Line 371  Line 566 
566      }      }
567      break;      break;
568    case TOK_NEW:    case TOK_NEW:
569      fprintf(f, "new ");      Stream_write_string(f, "new ");
570      instrument_function_call(node, f);      instrument_function_call(node, f);
571      break;      break;
572    case TOK_DELETE:    case TOK_DELETE:
573      fprintf(f, "delete ");      Stream_write_string(f, "delete ");
574      instrument_expression(node->pn_kid, f);      instrument_expression(node->pn_kid, f);
575      break;      break;
576    case TOK_DOT:    case TOK_DOT:
# Line 388  Line 583 
583      assert(ATOM_IS_STRING(node->pn_atom));      assert(ATOM_IS_STRING(node->pn_atom));
584      {      {
585        JSString * s = ATOM_TO_STRING(node->pn_atom);        JSString * s = ATOM_TO_STRING(node->pn_atom);
586        /* XXX - semantics changed in 1.7 */        bool is_keyword = (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF);
587        if (! ATOM_KEYWORD(node->pn_atom) && js_IsIdentifier(s)) {        if (! is_keyword && js_IsIdentifier(s)) {
588          fputc('.', f);          Stream_write_char(f, '.');
589          print_string_atom(node->pn_atom, f);          print_string_atom(node->pn_atom, f);
590        }        }
591        else {        else {
592          fputc('[', f);          Stream_write_char(f, '[');
593          print_quoted_string_atom(node->pn_atom, f);          print_quoted_string_atom(node->pn_atom, f);
594          fputc(']', f);          Stream_write_char(f, ']');
595        }        }
596      }      }
597      break;      break;
598    case TOK_LB:    case TOK_LB:
599      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
600      fputc('[', f);      Stream_write_char(f, '[');
601      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
602      fputc(']', f);      Stream_write_char(f, ']');
603      break;      break;
604    case TOK_LP:    case TOK_LP:
605      instrument_function_call(node, f);      instrument_function_call(node, f);
606      break;      break;
607    case TOK_RB:    case TOK_RB:
608      fputc('[', f);      Stream_write_char(f, '[');
609      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) {
610        if (p != node->pn_head) {        if (p != node->pn_head) {
611          fprintf(f, ", ");          Stream_write_string(f, ", ");
612        }        }
613        /* TOK_COMMA is a special case: a hole in the array */        /* TOK_COMMA is a special case: a hole in the array */
614        if (p->pn_type != TOK_COMMA) {        if (p->pn_type != TOK_COMMA) {
# Line 421  Line 616 
616        }        }
617      }      }
618      if (node->pn_extra == PNX_ENDCOMMA) {      if (node->pn_extra == PNX_ENDCOMMA) {
619        fputc(',', f);        Stream_write_char(f, ',');
620      }      }
621      fputc(']', f);      Stream_write_char(f, ']');
622      break;      break;
623    case TOK_RC:    case TOK_RC:
624      fputc('{', f);      Stream_write_char(f, '{');
625      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) {
626        assert(p->pn_type == TOK_COLON);        assert(p->pn_type == TOK_COLON);
627        if (p != node->pn_head) {        if (p != node->pn_head) {
628          fprintf(f, ", ");          Stream_write_string(f, ", ");
629          }
630    
631          /* check whether this is a getter or setter */
632          switch (p->pn_op) {
633          case JSOP_GETTER:
634          case JSOP_SETTER:
635            if (p->pn_op == JSOP_GETTER) {
636              Stream_write_string(f, "get ");
637            }
638            else {
639              Stream_write_string(f, "set ");
640            }
641            instrument_expression(p->pn_left, f);
642            if (p->pn_right->pn_type != TOK_FUNCTION) {
643              fatal("parse error: expected function");
644            }
645            instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);
646            break;
647          default:
648            instrument_expression(p->pn_left, f);
649            Stream_write_string(f, ": ");
650            instrument_expression(p->pn_right, f);
651            break;
652        }        }
       instrument_expression(p->pn_left, f);  
       fprintf(f, ": ");  
       instrument_expression(p->pn_right, f);  
653      }      }
654      fputc('}', f);      Stream_write_char(f, '}');
655      break;      break;
656    case TOK_RP:    case TOK_RP:
657      fputc('(', f);      Stream_write_char(f, '(');
658      instrument_expression(node->pn_kid, f);      instrument_expression(node->pn_kid, f);
659      fputc(')', f);      Stream_write_char(f, ')');
660      break;      break;
661    case TOK_NAME:    case TOK_NAME:
662      print_string_atom(node->pn_atom, f);      print_string_atom(node->pn_atom, f);
# Line 449  Line 664 
664    case TOK_STRING:    case TOK_STRING:
665      print_quoted_string_atom(node->pn_atom, f);      print_quoted_string_atom(node->pn_atom, f);
666      break;      break;
667    case TOK_OBJECT:    case TOK_REGEXP:
668      switch (node->pn_op) {      assert(node->pn_op == JSOP_REGEXP);
669      case JSOP_OBJECT:      {
670        /* I assume this is JSOP_REGEXP */        JSObject * object = node->pn_pob->object;
671        abort();        jsval result;
672        break;        js_regexp_toString(context, object, &result);
673      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;  
674      }      }
675      break;      break;
676    case TOK_NUMBER:    case TOK_NUMBER:
# Line 477  Line 681 
681      To keep the output simple, special-case zero.      To keep the output simple, special-case zero.
682      */      */
683      if (node->pn_dval == 0.0) {      if (node->pn_dval == 0.0) {
684        fprintf(f, "0");        Stream_write_string(f, "0");
685      }      }
686      else {      else {
687        fprintf(f, "%.15g", node->pn_dval);        Stream_printf(f, "%.15g", node->pn_dval);
688      }      }
689      break;      break;
690    case TOK_PRIMARY:    case TOK_PRIMARY:
691      switch (node->pn_op) {      switch (node->pn_op) {
692      case JSOP_TRUE:      case JSOP_TRUE:
693        fprintf(f, "true");        Stream_write_string(f, "true");
694        break;        break;
695      case JSOP_FALSE:      case JSOP_FALSE:
696        fprintf(f, "false");        Stream_write_string(f, "false");
697        break;        break;
698      case JSOP_NULL:      case JSOP_NULL:
699        fprintf(f, "null");        Stream_write_string(f, "null");
700        break;        break;
701      case JSOP_THIS:      case JSOP_THIS:
702        fprintf(f, "this");        Stream_write_string(f, "this");
703        break;        break;
704      /* jsscan.h mentions `super' ??? */      /* jsscan.h mentions `super' ??? */
705      default:      default:
# Line 504  Line 708 
708      break;      break;
709    case TOK_INSTANCEOF:    case TOK_INSTANCEOF:
710      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
711      fprintf(f, " instanceof ");      Stream_write_string(f, " instanceof ");
712      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
713      break;      break;
714    case TOK_IN:    case TOK_IN:
715      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
716      fprintf(f, " in ");      Stream_write_string(f, " in ");
717      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
718      break;      break;
719      case TOK_LEXICALSCOPE:
720        assert(node->pn_arity == PN_NAME);
721        assert(node->pn_expr->pn_type == TOK_LET);
722        assert(node->pn_expr->pn_arity == PN_BINARY);
723        Stream_write_string(f, "let(");
724        assert(node->pn_expr->pn_left->pn_type == TOK_LP);
725        assert(node->pn_expr->pn_left->pn_arity == PN_LIST);
726        instrument_declarations(node->pn_expr->pn_left, f);
727        Stream_write_string(f, ") ");
728        instrument_expression(node->pn_expr->pn_right, f);
729        break;
730      case TOK_YIELD:
731        assert(node->pn_arity == PN_UNARY);
732        Stream_write_string(f, "yield ");
733        instrument_expression(node->pn_kid, f);
734        break;
735      case TOK_ARRAYCOMP:
736        assert(node->pn_arity == PN_LIST);
737        {
738          JSParseNode * block_node;
739          switch (node->pn_count) {
740          case 1:
741            block_node = node->pn_head;
742            break;
743          case 2:
744            block_node = node->pn_head->pn_next;
745            break;
746          default:
747            abort();
748            break;
749          }
750          assert(block_node->pn_type == TOK_LEXICALSCOPE);
751          assert(block_node->pn_arity == PN_NAME);
752          JSParseNode * for_node = block_node->pn_expr;
753          assert(for_node->pn_type == TOK_FOR);
754          assert(for_node->pn_arity == PN_BINARY);
755          JSParseNode * if_node = NULL;
756          JSParseNode * push_node;
757          switch (for_node->pn_right->pn_type) {
758          case TOK_ARRAYPUSH:
759            push_node = for_node->pn_right;
760            assert(push_node->pn_arity == PN_UNARY);
761            break;
762          case TOK_IF:
763            if_node = for_node->pn_right;
764            assert(if_node->pn_arity == PN_TERNARY);
765            push_node = if_node->pn_kid2;
766            break;
767          default:
768            abort();
769            break;
770          }
771          Stream_write_char(f, '[');
772          instrument_expression(push_node->pn_kid, f);
773          Stream_write_char(f, ' ');
774          output_for_in(for_node, f);
775          if (if_node) {
776            Stream_write_string(f, " if (");
777            instrument_expression(if_node->pn_kid1, f);
778            Stream_write_char(f, ')');
779          }
780          Stream_write_char(f, ']');
781        }
782        break;
783      case TOK_VAR:
784        assert(node->pn_arity == PN_LIST);
785        Stream_write_string(f, "var ");
786        instrument_declarations(node, f);
787        break;
788      case TOK_LET:
789        assert(node->pn_arity == PN_LIST);
790        Stream_write_string(f, "let ");
791        instrument_declarations(node, f);
792        break;
793    default:    default:
794      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);
795    }    }
796  }  }
797    
798  static void instrument_var_statement(JSParseNode * node, FILE * f, int indent) {  static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
   assert(node->pn_arity == PN_LIST);  
   fprintf(f, "%*s", indent, "");  
   fprintf(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) {  
       fprintf(f, ", ");  
     }  
     print_string_atom(p->pn_atom, f);  
     if (p->pn_expr != NULL) {  
       fprintf(f, " = ");  
       instrument_expression(p->pn_expr, f);  
     }  
   }  
 }  
   
 static void output_statement(JSParseNode * node, FILE * f, int indent) {  
799    switch (node->pn_type) {    switch (node->pn_type) {
800    case TOK_FUNCTION:    case TOK_FUNCTION:
801      instrument_function(node, f, indent);      instrument_function(node, f, indent, FUNCTION_NORMAL);
802      break;      break;
803    case TOK_LC:    case TOK_LC:
804      assert(node->pn_arity == PN_LIST);      assert(node->pn_arity == PN_LIST);
805  /*  /*
806      fprintf(f, "{\n");      Stream_write_string(f, "{\n");
807  */  */
808      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) {
809        instrument_statement(p, f, indent);        instrument_statement(p, f, indent, false);
810      }      }
811  /*  /*
812      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
813      fprintf(f, "}\n");      Stream_write_string(f, "}\n");
814  */  */
815      break;      break;
816    case TOK_IF:    case TOK_IF:
817      {
818      assert(node->pn_arity == PN_TERNARY);      assert(node->pn_arity == PN_TERNARY);
819      fprintf(f, "%*s", indent, "");  
820      fprintf(f, "if (");      uint16_t line = node->pn_pos.begin.lineno;
821        if (! is_jscoverage_if) {
822          if (line > num_lines) {
823            fatal("%s: script contains more than 65,535 lines", file_id);
824          }
825          if (line >= 2 && exclusive_directives[line - 2]) {
826            is_jscoverage_if = true;
827          }
828        }
829    
830        Stream_printf(f, "%*s", indent, "");
831        Stream_write_string(f, "if (");
832      instrument_expression(node->pn_kid1, f);      instrument_expression(node->pn_kid1, f);
833      fprintf(f, ") {\n");      Stream_write_string(f, ") {\n");
834      instrument_statement(node->pn_kid2, f, indent + 2);      if (is_jscoverage_if && node->pn_kid3) {
835      fprintf(f, "%*s", indent, "");        uint16_t else_start = node->pn_kid3->pn_pos.begin.lineno;
836      fprintf(f, "}\n");        uint16_t else_end = node->pn_kid3->pn_pos.end.lineno + 1;
837      if (node->pn_kid3) {        Stream_printf(f, "%*s", indent + 2, "");
838        fprintf(f, "%*s", indent, "");        Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, else_start, else_end);
839        fprintf(f, "else {\n");      }
840        instrument_statement(node->pn_kid3, f, indent + 2);      instrument_statement(node->pn_kid2, f, indent + 2, false);
841        fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
842        fprintf(f, "}\n");      Stream_write_string(f, "}\n");
843    
844        if (node->pn_kid3 || is_jscoverage_if) {
845          Stream_printf(f, "%*s", indent, "");
846          Stream_write_string(f, "else {\n");
847    
848          if (is_jscoverage_if) {
849            uint16_t if_start = node->pn_kid2->pn_pos.begin.lineno + 1;
850            uint16_t if_end = node->pn_kid2->pn_pos.end.lineno + 1;
851            Stream_printf(f, "%*s", indent + 2, "");
852            Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_start, if_end);
853          }
854    
855          if (node->pn_kid3) {
856            instrument_statement(node->pn_kid3, f, indent + 2, is_jscoverage_if);
857          }
858    
859          Stream_printf(f, "%*s", indent, "");
860          Stream_write_string(f, "}\n");
861      }      }
862    
863      break;      break;
864      }
865    case TOK_SWITCH:    case TOK_SWITCH:
866      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
867      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
868      fprintf(f, "switch (");      Stream_write_string(f, "switch (");
869      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
870      fprintf(f, ") {\n");      Stream_write_string(f, ") {\n");
871      for (struct JSParseNode * p = node->pn_right->pn_head; p != NULL; p = p->pn_next) {      for (struct JSParseNode * p = node->pn_right->pn_head; p != NULL; p = p->pn_next) {
872        fprintf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
873        switch (p->pn_type) {        switch (p->pn_type) {
874        case TOK_CASE:        case TOK_CASE:
875          fprintf(f, "case ");          Stream_write_string(f, "case ");
876          instrument_expression(p->pn_left, f);          instrument_expression(p->pn_left, f);
877          fprintf(f, ":\n");          Stream_write_string(f, ":\n");
878          break;          break;
879        case TOK_DEFAULT:        case TOK_DEFAULT:
880          fprintf(f, "default:\n");          Stream_write_string(f, "default:\n");
881          break;          break;
882        default:        default:
883          abort();          abort();
884          break;          break;
885        }        }
886        instrument_statement(p->pn_right, f, indent + 2);        instrument_statement(p->pn_right, f, indent + 2, false);
887      }      }
888      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
889      fprintf(f, "}\n");      Stream_write_string(f, "}\n");
890      break;      break;
891    case TOK_CASE:    case TOK_CASE:
892    case TOK_DEFAULT:    case TOK_DEFAULT:
# Line 602  Line 894 
894      break;      break;
895    case TOK_WHILE:    case TOK_WHILE:
896      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
897      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
898      fprintf(f, "while (");      Stream_write_string(f, "while (");
899      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
900      fprintf(f, ") {\n");      Stream_write_string(f, ") {\n");
901      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
902      fprintf(f, "}\n");      Stream_write_string(f, "}\n");
903      break;      break;
904    case TOK_DO:    case TOK_DO:
905      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
906      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
907      fprintf(f, "do {\n");      Stream_write_string(f, "do {\n");
908      instrument_statement(node->pn_left, f, indent + 2);      instrument_statement(node->pn_left, f, indent + 2, false);
909      fprintf(f, "}\n");      Stream_write_string(f, "}\n");
910      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
911      fprintf(f, "while (");      Stream_write_string(f, "while (");
912      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
913      fprintf(f, ");\n");      Stream_write_string(f, ");\n");
914      break;      break;
915    case TOK_FOR:    case TOK_FOR:
916      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
917      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
     fprintf(f, "for (");  
918      switch (node->pn_left->pn_type) {      switch (node->pn_left->pn_type) {
919      case TOK_IN:      case TOK_IN:
920        /* for/in */        /* for/in */
921        assert(node->pn_left->pn_arity == PN_BINARY);        assert(node->pn_left->pn_arity == PN_BINARY);
922        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;  
 */  
       }  
       fprintf(f, " in ");  
       instrument_expression(node->pn_left->pn_right, f);  
923        break;        break;
924      case TOK_RESERVED:      case TOK_RESERVED:
925        /* for (;;) */        /* for (;;) */
926        assert(node->pn_left->pn_arity == PN_TERNARY);        assert(node->pn_left->pn_arity == PN_TERNARY);
927          Stream_write_string(f, "for (");
928        if (node->pn_left->pn_kid1) {        if (node->pn_left->pn_kid1) {
929          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);  
         }  
930        }        }
931        fprintf(f, ";");        Stream_write_string(f, ";");
932        if (node->pn_left->pn_kid2) {        if (node->pn_left->pn_kid2) {
933          fputc(' ', f);          Stream_write_char(f, ' ');
934          instrument_expression(node->pn_left->pn_kid2, f);          instrument_expression(node->pn_left->pn_kid2, f);
935        }        }
936        fprintf(f, ";");        Stream_write_string(f, ";");
937        if (node->pn_left->pn_kid3) {        if (node->pn_left->pn_kid3) {
938          fputc(' ', f);          Stream_write_char(f, ' ');
939          instrument_expression(node->pn_left->pn_kid3, f);          instrument_expression(node->pn_left->pn_kid3, f);
940        }        }
941          Stream_write_char(f, ')');
942        break;        break;
943      default:      default:
944        abort();        abort();
945        break;        break;
946      }      }
947      fprintf(f, ") {\n");      Stream_write_string(f, " {\n");
948      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
949      fprintf(f, "}\n");      Stream_write_string(f, "}\n");
950      break;      break;
951    case TOK_THROW:    case TOK_THROW:
952      assert(node->pn_arity == PN_UNARY);      assert(node->pn_arity == PN_UNARY);
953      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
954      fprintf(f, "throw ");      Stream_write_string(f, "throw ");
955      instrument_expression(node->pn_u.unary.kid, f);      instrument_expression(node->pn_u.unary.kid, f);
956      fprintf(f, ";\n");      Stream_write_string(f, ";\n");
957      break;      break;
958    case TOK_TRY:    case TOK_TRY:
959      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
960      fprintf(f, "try {\n");      Stream_write_string(f, "try {\n");
961      instrument_statement(node->pn_kid1, f, indent + 2);      instrument_statement(node->pn_kid1, f, indent + 2, false);
962      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
963      fprintf(f, "}\n");      Stream_write_string(f, "}\n");
964      {      if (node->pn_kid2) {
965        for (JSParseNode * catch = node->pn_kid2; catch != NULL; catch = catch->pn_kid2) {        assert(node->pn_kid2->pn_type == TOK_RESERVED);
966          for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) {
967            assert(scope->pn_type == TOK_LEXICALSCOPE);
968            JSParseNode * catch = scope->pn_expr;
969          assert(catch->pn_type == TOK_CATCH);          assert(catch->pn_type == TOK_CATCH);
970          fprintf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
971          fprintf(f, "catch (");          Stream_write_string(f, "catch (");
972            /* this may not be a name - destructuring assignment */
973            /*
974          assert(catch->pn_kid1->pn_arity == PN_NAME);          assert(catch->pn_kid1->pn_arity == PN_NAME);
975          print_string_atom(catch->pn_kid1->pn_atom, f);          print_string_atom(catch->pn_kid1->pn_atom, f);
976          if (catch->pn_kid1->pn_expr) {          */
977            fprintf(f, " if ");          instrument_expression(catch->pn_kid1, f);
978            instrument_expression(catch->pn_kid1->pn_expr, f);          if (catch->pn_kid2) {
979          }            Stream_write_string(f, " if ");
980          fprintf(f, ") {\n");            instrument_expression(catch->pn_kid2, f);
981          instrument_statement(catch->pn_kid3, f, indent + 2);          }
982          fprintf(f, "%*s", indent, "");          Stream_write_string(f, ") {\n");
983          fprintf(f, "}\n");          instrument_statement(catch->pn_kid3, f, indent + 2, false);
984            Stream_printf(f, "%*s", indent, "");
985            Stream_write_string(f, "}\n");
986        }        }
987      }      }
988      if (node->pn_kid3) {      if (node->pn_kid3) {
989        fprintf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
990        fprintf(f, "finally {\n");        Stream_write_string(f, "finally {\n");
991        instrument_statement(node->pn_kid3, f, indent + 2);        instrument_statement(node->pn_kid3, f, indent + 2, false);
992        fprintf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
993        fprintf(f, "}\n");        Stream_write_string(f, "}\n");
994      }      }
995      break;      break;
996    case TOK_CATCH:    case TOK_CATCH:
# Line 723  Line 999 
999    case TOK_BREAK:    case TOK_BREAK:
1000    case TOK_CONTINUE:    case TOK_CONTINUE:
1001      assert(node->pn_arity == PN_NAME || node->pn_arity == PN_NULLARY);      assert(node->pn_arity == PN_NAME || node->pn_arity == PN_NULLARY);
1002      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
1003      fputs(node->pn_type == TOK_BREAK? "break": "continue", f);      Stream_write_string(f, node->pn_type == TOK_BREAK? "break": "continue");
1004      JSAtom * atom = node->pn_u.name.atom;      JSAtom * atom = node->pn_u.name.atom;
1005      if (atom != NULL) {      if (atom != NULL) {
1006        fputc(' ', f);        Stream_write_char(f, ' ');
1007        print_string_atom(node->pn_atom, f);        print_string_atom(node->pn_atom, f);
1008      }      }
1009      fprintf(f, ";\n");      Stream_write_string(f, ";\n");
1010      break;      break;
1011    case TOK_WITH:    case TOK_WITH:
1012      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
1013      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
1014      fprintf(f, "with (");      Stream_write_string(f, "with (");
1015      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
1016      fprintf(f, ") {\n");      Stream_write_string(f, ") {\n");
1017      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
1018      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
1019      fprintf(f, "}\n");      Stream_write_string(f, "}\n");
1020      break;      break;
1021    case TOK_VAR:    case TOK_VAR:
1022      instrument_var_statement(node, f, indent);      Stream_printf(f, "%*s", indent, "");
1023      fprintf(f, ";\n");      instrument_expression(node, f);
1024        Stream_write_string(f, ";\n");
1025      break;      break;
1026    case TOK_RETURN:    case TOK_RETURN:
1027      assert(node->pn_arity == PN_UNARY);      assert(node->pn_arity == PN_UNARY);
1028      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
1029      fprintf(f, "return");      Stream_write_string(f, "return");
1030      if (node->pn_kid != NULL) {      if (node->pn_kid != NULL) {
1031        fprintf(f, " ");        Stream_write_char(f, ' ');
1032        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
1033      }      }
1034      fprintf(f, ";\n");      Stream_write_string(f, ";\n");
1035      break;      break;
1036    case TOK_SEMI:    case TOK_SEMI:
1037      assert(node->pn_arity == PN_UNARY);      assert(node->pn_arity == PN_UNARY);
1038      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
1039      if (node->pn_kid != NULL) {      if (node->pn_kid != NULL) {
1040        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
1041      }      }
1042      fprintf(f, ";\n");      Stream_write_string(f, ";\n");
1043      break;      break;
1044    case TOK_COLON:    case TOK_COLON:
1045      assert(node->pn_arity == PN_NAME);      assert(node->pn_arity == PN_NAME);
# Line 770  Line 1047 
1047      This one is tricky: can't output instrumentation between the label and the      This one is tricky: can't output instrumentation between the label and the
1048      statement it's supposed to label ...      statement it's supposed to label ...
1049      */      */
1050      fprintf(f, "%*s", indent < 2? 0: indent - 2, "");      Stream_printf(f, "%*s", indent < 2? 0: indent - 2, "");
1051      print_string_atom(node->pn_atom, f);      print_string_atom(node->pn_atom, f);
1052      fprintf(f, ":\n");      Stream_write_string(f, ":\n");
1053      /*      /*
1054      ... use output_statement instead of instrument_statement.      ... use output_statement instead of instrument_statement.
1055      */      */
1056      output_statement(node->pn_expr, f, indent);      output_statement(node->pn_expr, f, indent, false);
1057        break;
1058      case TOK_LEXICALSCOPE:
1059        /* let statement */
1060        assert(node->pn_arity == PN_NAME);
1061        switch (node->pn_expr->pn_type) {
1062        case TOK_LET:
1063          /* let statement */
1064          assert(node->pn_expr->pn_arity == PN_BINARY);
1065          instrument_statement(node->pn_expr, f, indent, false);
1066          break;
1067        case TOK_LC:
1068          /* block */
1069          Stream_printf(f, "%*s", indent, "");
1070          Stream_write_string(f, "{\n");
1071          instrument_statement(node->pn_expr, f, indent + 2, false);
1072          Stream_printf(f, "%*s", indent, "");
1073          Stream_write_string(f, "}\n");
1074          break;
1075        case TOK_FOR:
1076          instrument_statement(node->pn_expr, f, indent, false);
1077          break;
1078        default:
1079          abort();
1080          break;
1081        }
1082        break;
1083      case TOK_LET:
1084        switch (node->pn_arity) {
1085        case PN_BINARY:
1086          /* let statement */
1087          Stream_printf(f, "%*s", indent, "");
1088          Stream_write_string(f, "let (");
1089          assert(node->pn_left->pn_type == TOK_LP);
1090          assert(node->pn_left->pn_arity == PN_LIST);
1091          instrument_declarations(node->pn_left, f);
1092          Stream_write_string(f, ") {\n");
1093          instrument_statement(node->pn_right, f, indent + 2, false);
1094          Stream_printf(f, "%*s", indent, "");
1095          Stream_write_string(f, "}\n");
1096          break;
1097        case PN_LIST:
1098          /* let definition */
1099          Stream_printf(f, "%*s", indent, "");
1100          instrument_expression(node, f);
1101          Stream_write_string(f, ";\n");
1102          break;
1103        default:
1104          abort();
1105          break;
1106        }
1107        break;
1108      case TOK_DEBUGGER:
1109        Stream_printf(f, "%*s", indent, "");
1110        Stream_write_string(f, "debugger;\n");
1111      break;      break;
1112    default:    default:
1113      fatal("unsupported node type in file %s: %d", file_id, node->pn_type);      fatal("unsupported node type in file %s: %d", file_id, node->pn_type);
# Line 788  Line 1119 
1119  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
1120  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
1121  */  */
1122  static void instrument_statement(JSParseNode * node, FILE * f, int indent) {  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
1123    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC && node->pn_type != TOK_LEXICALSCOPE) {
1124      int line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
1125        if (line > num_lines) {
1126          fatal("%s: script contains more than 65,535 lines", file_id);
1127        }
1128    
1129      /* the root node has line number 0 */      /* the root node has line number 0 */
1130      if (line != 0) {      if (line != 0) {
1131        fprintf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
1132        fprintf(f, "_$jscoverage['%s'][%d]++;\n", file_id, line);        Stream_printf(f, "_$jscoverage['%s'][%d]++;\n", file_id, line);
1133        lines[line - 1] = 1;        lines[line - 1] = 1;
1134      }      }
1135    }    }
1136    output_statement(node, f, indent);    output_statement(node, f, indent, is_jscoverage_if);
1137  }  }
1138    
1139  static void instrument_js_stream(const char * id, int line, FILE * input, FILE * output, const char * temporary_file_name) {  static bool characters_start_with(const jschar * characters, size_t line_start, size_t line_end, const char * prefix) {
1140    file_id = id;    const jschar * characters_end = characters + line_end;
1141      const jschar * cp = characters + line_start;
1142      const char * bp = prefix;
1143      for (;;) {
1144        if (*bp == '\0') {
1145          return true;
1146        }
1147        else if (cp == characters_end) {
1148          return false;
1149        }
1150        else if (*cp != *bp) {
1151          return false;
1152        }
1153        bp++;
1154        cp++;
1155      }
1156    }
1157    
1158    /* scan the javascript */  static bool characters_are_white_space(const jschar * characters, size_t line_start, size_t line_end) {
1159    JSTokenStream * token_stream = js_NewFileTokenStream(context, NULL, input);    /* XXX - other Unicode space */
1160    if (token_stream == NULL) {    const jschar * end = characters + line_end;
1161      fatal("cannot create token stream from file: %s", file_id);    for (const jschar * p = characters + line_start; p < end; p++) {
1162        jschar c = *p;
1163        if (c == 0x9 || c == 0xB || c == 0xC || c == 0x20 || c == 0xA0) {
1164          continue;
1165        }
1166        else {
1167          return false;
1168        }
1169    }    }
1170      return true;
1171    }
1172    
1173    static void error_reporter(JSContext * context, const char * message, JSErrorReport * report) {
1174      fprintf(stderr, "jscoverage: parse error: line %u: %s\n", report->lineno, message);
1175    }
1176    
1177    void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) {
1178      file_id = id;
1179    
1180    /* parse the javascript */    /* parse the javascript */
1181    JSParseNode * node = js_ParseTokenStream(context, global, token_stream);    JSParseContext parse_context;
1182      if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) {
1183        fatal("cannot create token stream from file: %s", file_id);
1184      }
1185      JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);
1186      JSParseNode * node = js_ParseScript(context, global, &parse_context);
1187    if (node == NULL) {    if (node == NULL) {
1188        js_ReportUncaughtException(context);
1189      fatal("parse error in file: %s", file_id);      fatal("parse error in file: %s", file_id);
1190    }    }
1191    int num_lines = node->pn_pos.end.lineno;    JS_SetErrorReporter(context, old_error_reporter);
1192      num_lines = node->pn_pos.end.lineno;
1193    lines = xmalloc(num_lines);    lines = xmalloc(num_lines);
1194    for (int i = 0; i < num_lines; i++) {    for (unsigned int i = 0; i < num_lines; i++) {
1195      lines[i] = 0;      lines[i] = 0;
1196    }    }
1197    
1198      /* search code for conditionals */
1199      exclusive_directives = xnew(bool, num_lines);
1200      for (unsigned int i = 0; i < num_lines; i++) {
1201        exclusive_directives[i] = false;
1202      }
1203    
1204      bool has_conditionals = false;
1205      struct IfDirective * if_directives = NULL;
1206      size_t line_number = 0;
1207      size_t i = 0;
1208      while (i < num_characters) {
1209        if (line_number == UINT16_MAX) {
1210          fatal("%s: script has more than 65,535 lines", file_id);
1211        }
1212        line_number++;
1213        size_t line_start = i;
1214        jschar c;
1215        bool done = false;
1216        while (! done && i < num_characters) {
1217          c = characters[i];
1218          switch (c) {
1219          case '\r':
1220          case '\n':
1221          case 0x2028:
1222          case 0x2029:
1223            done = true;
1224            break;
1225          default:
1226            i++;
1227          }
1228        }
1229        size_t line_end = i;
1230        if (i < num_characters) {
1231          i++;
1232          if (c == '\r' && i < num_characters && characters[i] == '\n') {
1233            i++;
1234          }
1235        }
1236    
1237        if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) {
1238          has_conditionals = true;
1239    
1240          if (characters_are_white_space(characters, line_start + 16, line_end)) {
1241            exclusive_directives[line_number - 1] = true;
1242          }
1243          else {
1244            struct IfDirective * if_directive = xnew(struct IfDirective, 1);
1245            if_directive->condition_start = characters + line_start + 16;
1246            if_directive->condition_end = characters + line_end;
1247            if_directive->start_line = line_number;
1248            if_directive->end_line = 0;
1249            if_directive->next = if_directives;
1250            if_directives = if_directive;
1251          }
1252        }
1253        else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) {
1254          for (struct IfDirective * p = if_directives; p != NULL; p = p->next) {
1255            if (p->end_line == 0) {
1256              p->end_line = line_number;
1257              break;
1258            }
1259          }
1260        }
1261      }
1262    
1263    /*    /*
1264    Create a temporary file - we can't write directly to the output because we    An instrumented JavaScript file has 4 sections:
1265    need to know the line number info first.    1. initialization
1266      2. instrumented source code
1267      3. conditionals
1268      4. original source code
1269    */    */
   FILE * temporary = fopen(temporary_file_name, "w+");  
   if (temporary == NULL) {  
     fatal("cannot create temporary file for script: %s", file_id);  
   }  
1270    
1271    /* write instrumented javascript to the temporary */    Stream * instrumented = Stream_new(0);
1272    instrument_statement(node, temporary, 0);    instrument_statement(node, instrumented, 0, false);
1273      js_FinishParseContext(context, &parse_context);
1274    
1275    /* write line number info to the output */    /* write line number info to the output */
1276    fprintf(output, "/* automatically generated by JSCoverage - do not edit */\n");    Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");
1277    fprintf(output, "if (! top._$jscoverage) {\n  top._$jscoverage = {};\n}\n");    Stream_write_string(output, "if (! top._$jscoverage) {\n  top._$jscoverage = {};\n}\n");
1278    fprintf(output, "var _$jscoverage = top._$jscoverage;\n");    Stream_write_string(output, "var _$jscoverage = top._$jscoverage;\n");
1279    fprintf(output, "if (! _$jscoverage['%s']) {\n", file_id);    Stream_printf(output, "if (! _$jscoverage['%s']) {\n", file_id);
1280    fprintf(output, "  _$jscoverage['%s'] = [];\n", file_id);    Stream_printf(output, "  _$jscoverage['%s'] = [];\n", file_id);
1281    for (int i = 0; i < num_lines; i++) {    for (int i = 0; i < num_lines; i++) {
1282      if (lines[i]) {      if (lines[i]) {
1283        fprintf(output, "  _$jscoverage['%s'][%d] = 0;\n", file_id, i + 1);        Stream_printf(output, "  _$jscoverage['%s'][%d] = 0;\n", file_id, i + 1);
1284      }      }
1285    }    }
1286    fprintf(output, "}\n");    Stream_write_string(output, "}\n");
1287      free(lines);
1288    lines = NULL;    lines = NULL;
1289      free(exclusive_directives);
1290      exclusive_directives = NULL;
1291    
1292    /* copy the temporary to the output */    /* conditionals */
1293    fseek(temporary, 0, SEEK_SET);    if (has_conditionals) {
1294    copy_stream(temporary, output);      Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);
1295      }
1296    
1297      /* copy the instrumented source code to the output */
1298      Stream_write(output, instrumented->data, instrumented->length);
1299    
1300    fclose(temporary);    /* conditionals */
1301      for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) {
1302        Stream_write_string(output, "if (!(");
1303        print_javascript(if_directive->condition_start, if_directive->condition_end - if_directive->condition_start, output);
1304        Stream_write_string(output, ")) {\n");
1305        Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line);
1306        Stream_write_string(output, "}\n");
1307      }
1308    
1309      /* free */
1310      while (if_directives != NULL) {
1311        struct IfDirective * if_directive = if_directives;
1312        if_directives = if_directives->next;
1313        free(if_directive);
1314      }
1315    
1316      /* copy the original source to the output */
1317      Stream_printf(output, "_$jscoverage['%s'].source = ", file_id);
1318      jscoverage_write_source(id, characters, num_characters, output);
1319      Stream_printf(output, ";\n");
1320    
1321      Stream_delete(instrumented);
1322    
1323    file_id = NULL;    file_id = NULL;
1324  }  }
1325    
1326  void jscoverage_instrument_js(const char * id, FILE * input, FILE * output, const char * temporary_file_name) {  void jscoverage_write_source(const char * id, const jschar * characters, size_t num_characters, Stream * output) {
1327    instrument_js_stream(id, 0, input, output, temporary_file_name);    Stream_write_string(output, "[");
1328      if (jscoverage_highlight) {
1329        Stream * highlighted_stream = Stream_new(num_characters);
1330        jscoverage_highlight_js(context, id, characters, num_characters, highlighted_stream);
1331        size_t i = 0;
1332        while (i < highlighted_stream->length) {
1333          if (i > 0) {
1334            Stream_write_char(output, ',');
1335          }
1336    
1337          Stream_write_char(output, '"');
1338          bool done = false;
1339          while (! done) {
1340            char c = highlighted_stream->data[i];
1341            switch (c) {
1342            case 0x8:
1343              /* backspace */
1344              Stream_write_string(output, "\\b");
1345              break;
1346            case 0x9:
1347              /* horizontal tab */
1348              Stream_write_string(output, "\\t");
1349              break;
1350            case 0xa:
1351              /* line feed (new line) */
1352              done = true;
1353              break;
1354            /* IE doesn't support this */
1355            /*
1356            case 0xb:
1357              Stream_write_string(output, "\\v");
1358              break;
1359            */
1360            case 0xc:
1361              /* form feed */
1362              Stream_write_string(output, "\\f");
1363              break;
1364            case 0xd:
1365              /* carriage return */
1366              done = true;
1367              if (i + 1 < highlighted_stream->length && highlighted_stream->data[i + 1] == '\n') {
1368                i++;
1369              }
1370              break;
1371            case '"':
1372              Stream_write_string(output, "\\\"");
1373              break;
1374            case '\\':
1375              Stream_write_string(output, "\\\\");
1376              break;
1377            default:
1378              Stream_write_char(output, c);
1379              break;
1380            }
1381            i++;
1382            if (i >= highlighted_stream->length) {
1383              done = true;
1384            }
1385          }
1386          Stream_write_char(output, '"');
1387        }
1388        Stream_delete(highlighted_stream);
1389      }
1390      else {
1391        size_t i = 0;
1392        while (i < num_characters) {
1393          if (i > 0) {
1394            Stream_write_char(output, ',');
1395          }
1396    
1397          Stream_write_char(output, '"');
1398          bool done = false;
1399          while (! done) {
1400            jschar c = characters[i];
1401            switch (c) {
1402            case 0x8:
1403              /* backspace */
1404              Stream_write_string(output, "\\b");
1405              break;
1406            case 0x9:
1407              /* horizontal tab */
1408              Stream_write_string(output, "\\t");
1409              break;
1410            case 0xa:
1411              /* line feed (new line) */
1412              done = true;
1413              break;
1414            /* IE doesn't support this */
1415            /*
1416            case 0xb:
1417              Stream_write_string(output, "\\v");
1418              break;
1419            */
1420            case 0xc:
1421              /* form feed */
1422              Stream_write_string(output, "\\f");
1423              break;
1424            case 0xd:
1425              /* carriage return */
1426              done = true;
1427              if (i + 1 < num_characters && characters[i + 1] == '\n') {
1428                i++;
1429              }
1430              break;
1431            case '"':
1432              Stream_write_string(output, "\\\"");
1433              break;
1434            case '\\':
1435              Stream_write_string(output, "\\\\");
1436              break;
1437            case '&':
1438              Stream_write_string(output, "&amp;");
1439              break;
1440            case '<':
1441              Stream_write_string(output, "&lt;");
1442              break;
1443            case '>':
1444              Stream_write_string(output, "&gt;");
1445              break;
1446            case 0x2028:
1447            case 0x2029:
1448              done = true;
1449              break;
1450            default:
1451              if (32 <= c && c <= 126) {
1452                Stream_write_char(output, c);
1453              }
1454              else {
1455                Stream_printf(output, "&#%d;", c);
1456              }
1457              break;
1458            }
1459            i++;
1460            if (i >= num_characters) {
1461              done = true;
1462            }
1463          }
1464          Stream_write_char(output, '"');
1465        }
1466      }
1467      Stream_write_string(output, "]");
1468    }
1469    
1470    void jscoverage_copy_resources(const char * destination_directory) {
1471      copy_resource("jscoverage.html", destination_directory);
1472      copy_resource("jscoverage.css", destination_directory);
1473      copy_resource("jscoverage.js", destination_directory);
1474      copy_resource("jscoverage-ie.css", destination_directory);
1475      copy_resource("jscoverage-throbber.gif", destination_directory);
1476      copy_resource("jscoverage-highlight.css", destination_directory);
1477    }
1478    
1479    /*
1480    coverage reports
1481    */
1482    
1483    struct FileCoverageList {
1484      FileCoverage * file_coverage;
1485      struct FileCoverageList * next;
1486    };
1487    
1488    struct Coverage {
1489      JSHashTable * coverage_table;
1490      struct FileCoverageList * coverage_list;
1491    };
1492    
1493    static int compare_strings(const void * p1, const void * p2) {
1494      return strcmp(p1, p2) == 0;
1495    }
1496    
1497    Coverage * Coverage_new(void) {
1498      Coverage * result = xmalloc(sizeof(Coverage));
1499      result->coverage_table = JS_NewHashTable(1024, JS_HashString, compare_strings, NULL, NULL, NULL);
1500      if (result->coverage_table == NULL) {
1501        fatal("cannot create hash table");
1502      }
1503      result->coverage_list = NULL;
1504      return result;
1505    }
1506    
1507    void Coverage_delete(Coverage * coverage) {
1508      JS_HashTableDestroy(coverage->coverage_table);
1509      struct FileCoverageList * p = coverage->coverage_list;
1510      while (p != NULL) {
1511        free(p->file_coverage->coverage_lines);
1512        if (p->file_coverage->source_lines != NULL) {
1513          for (uint32 i = 0; i < p->file_coverage->num_source_lines; i++) {
1514            free(p->file_coverage->source_lines[i]);
1515          }
1516          free(p->file_coverage->source_lines);
1517        }
1518        free(p->file_coverage->id);
1519        free(p->file_coverage);
1520        struct FileCoverageList * q = p;
1521        p = p->next;
1522        free(q);
1523      }
1524      free(coverage);
1525    }
1526    
1527    struct EnumeratorArg {
1528      CoverageForeachFunction f;
1529      void * p;
1530    };
1531    
1532    static intN enumerator(JSHashEntry * entry, intN i, void * arg) {
1533      struct EnumeratorArg * enumerator_arg = arg;
1534      enumerator_arg->f(entry->value, i, enumerator_arg->p);
1535      return 0;
1536    }
1537    
1538    void Coverage_foreach_file(Coverage * coverage, CoverageForeachFunction f, void * p) {
1539      struct EnumeratorArg enumerator_arg;
1540      enumerator_arg.f = f;
1541      enumerator_arg.p = p;
1542      JS_HashTableEnumerateEntries(coverage->coverage_table, enumerator, &enumerator_arg);
1543    }
1544    
1545    int jscoverage_parse_json(Coverage * coverage, const uint8_t * json, size_t length) {
1546      int result = 0;
1547    
1548      jschar * base = js_InflateString(context, (char *) json, &length);
1549      if (base == NULL) {
1550        fatal("out of memory");
1551      }
1552    
1553      jschar * parenthesized_json = xnew(jschar, addst(length, 2));
1554      parenthesized_json[0] = '(';
1555      memcpy(parenthesized_json + 1, base, mulst(length, sizeof(jschar)));
1556      parenthesized_json[length + 1] = ')';
1557    
1558      JS_free(context, base);
1559    
1560      JSParseContext parse_context;
1561      if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) {
1562        free(parenthesized_json);
1563        return -1;
1564      }
1565      JSParseNode * root = js_ParseScript(context, global, &parse_context);
1566      free(parenthesized_json);
1567      if (root == NULL) {
1568        result = -1;
1569        goto done;
1570      }
1571    
1572      /* root node must be TOK_LC */
1573      if (root->pn_type != TOK_LC) {
1574        result = -1;
1575        goto done;
1576      }
1577      JSParseNode * semi = root->pn_u.list.head;
1578    
1579      /* the list must be TOK_SEMI and it must contain only one element */
1580      if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {
1581        result = -1;
1582        goto done;
1583      }
1584      JSParseNode * parenthesized = semi->pn_kid;
1585    
1586      /* this must be a parenthesized expression */
1587      if (parenthesized->pn_type != TOK_RP) {
1588        result = -1;
1589        goto done;
1590      }
1591      JSParseNode * object = parenthesized->pn_kid;
1592    
1593      /* this must be an object literal */
1594      if (object->pn_type != TOK_RC) {
1595        result = -1;
1596        goto done;
1597      }
1598    
1599      for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {
1600        /* every element of this list must be TOK_COLON */
1601        if (p->pn_type != TOK_COLON) {
1602          result = -1;
1603          goto done;
1604        }
1605    
1606        /* the key must be a string representing the file */
1607        JSParseNode * key = p->pn_left;
1608        if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {
1609          result = -1;
1610          goto done;
1611        }
1612        char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));
1613    
1614        /* the value must be an object literal OR an array */
1615        JSParseNode * value = p->pn_right;
1616        if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {
1617          result = -1;
1618          goto done;
1619        }
1620    
1621        JSParseNode * array = NULL;
1622        JSParseNode * source = NULL;
1623        if (value->pn_type == TOK_RB) {
1624          /* an array */
1625          array = value;
1626        }
1627        else if (value->pn_type == TOK_RC) {
1628          /* an object literal */
1629          if (value->pn_count != 2) {
1630            result = -1;
1631            goto done;
1632          }
1633          for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {
1634            if (element->pn_type != TOK_COLON) {
1635              result = -1;
1636              goto done;
1637            }
1638            JSParseNode * left = element->pn_left;
1639            if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {
1640              result = -1;
1641              goto done;
1642            }
1643            const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));
1644            if (strcmp(s, "coverage") == 0) {
1645              array = element->pn_right;
1646              if (array->pn_type != TOK_RB) {
1647                result = -1;
1648                goto done;
1649              }
1650            }
1651            else if (strcmp(s, "source") == 0) {
1652              source = element->pn_right;
1653              if (source->pn_type != TOK_RB) {
1654                result = -1;
1655                goto done;
1656              }
1657            }
1658            else {
1659              result = -1;
1660              goto done;
1661            }
1662          }
1663        }
1664        else {
1665          result = -1;
1666          goto done;
1667        }
1668    
1669        if (array == NULL) {
1670          result = -1;
1671          goto done;
1672        }
1673    
1674        /* look up the file in the coverage table */
1675        FileCoverage * file_coverage = JS_HashTableLookup(coverage->coverage_table, id_bytes);
1676        if (file_coverage == NULL) {
1677          /* not there: create a new one */
1678          char * id = xstrdup(id_bytes);
1679          file_coverage = xmalloc(sizeof(FileCoverage));
1680          file_coverage->id = id;
1681          file_coverage->num_coverage_lines = array->pn_count;
1682          file_coverage->coverage_lines = xnew(int, array->pn_count);
1683          file_coverage->source_lines = NULL;
1684    
1685          /* set coverage for all lines */
1686          uint32 i = 0;
1687          for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {
1688            if (element->pn_type == TOK_NUMBER) {
1689              file_coverage->coverage_lines[i] = (int) element->pn_dval;
1690            }
1691            else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1692              file_coverage->coverage_lines[i] = -1;
1693            }
1694            else {
1695              result = -1;
1696              goto done;
1697            }
1698          }
1699          assert(i == array->pn_count);
1700    
1701          /* add to the hash table */
1702          JS_HashTableAdd(coverage->coverage_table, id, file_coverage);
1703          struct FileCoverageList * coverage_list = xmalloc(sizeof(struct FileCoverageList));
1704          coverage_list->file_coverage = file_coverage;
1705          coverage_list->next = coverage->coverage_list;
1706          coverage->coverage_list = coverage_list;
1707        }
1708        else {
1709          /* sanity check */
1710          assert(strcmp(file_coverage->id, id_bytes) == 0);
1711          if (file_coverage->num_coverage_lines != array->pn_count) {
1712            result = -2;
1713            goto done;
1714          }
1715    
1716          /* merge the coverage */
1717          uint32 i = 0;
1718          for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {
1719            if (element->pn_type == TOK_NUMBER) {
1720              if (file_coverage->coverage_lines[i] == -1) {
1721                result = -2;
1722                goto done;
1723              }
1724              file_coverage->coverage_lines[i] += (int) element->pn_dval;
1725            }
1726            else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1727              if (file_coverage->coverage_lines[i] != -1) {
1728                result = -2;
1729                goto done;
1730              }
1731            }
1732            else {
1733              result = -1;
1734              goto done;
1735            }
1736          }
1737          assert(i == array->pn_count);
1738        }
1739    
1740        /* if this JSON file has source, use it */
1741        if (file_coverage->source_lines == NULL && source != NULL) {
1742          file_coverage->num_source_lines = source->pn_count;
1743          file_coverage->source_lines = xnew(char *, source->pn_count);
1744          uint32 i = 0;
1745          for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {
1746            if (element->pn_type != TOK_STRING) {
1747              result = -1;
1748              goto done;
1749            }
1750            file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));
1751          }
1752          assert(i == source->pn_count);
1753        }
1754      }
1755    
1756    done:
1757      js_FinishParseContext(context, &parse_context);
1758      return result;
1759  }  }

Legend:
Removed from v.87  
changed lines
  Added in v.348

  ViewVC Help
Powered by ViewVC 1.1.24