/[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 90 by siliconforks, Tue May 6 14:05:12 2008 UTC revision 353 by siliconforks, Fri Oct 24 16:18:36 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 <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"
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 44  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 56  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 71  Line 99 
99    JS_DestroyRuntime(runtime);    JS_DestroyRuntime(runtime);
100  }  }
101    
102  static void print_string(JSString * s, FILE * f) {  static void print_javascript(const jschar * characters, size_t num_characters, Stream * f) {
103    for (int i = 0; i < s->length; i++) {    for (size_t i = 0; i < num_characters; i++) {
104      char c = s->chars[i];      jschar c = characters[i];
105      fputc(c, f);      /*
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_atom(JSAtom * atom, FILE * f) {  static void print_string(JSString * s, Stream * f) {
127      size_t length = JSSTRING_LENGTH(s);
128      jschar * characters = JSSTRING_CHARS(s);
129      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    
180    static void print_string_atom(JSAtom * atom, Stream * f) {
181    assert(ATOM_IS_STRING(atom));    assert(ATOM_IS_STRING(atom));
182    JSString * s = ATOM_TO_STRING(atom);    JSString * s = ATOM_TO_STRING(atom);
183    print_string(s, f);    print_string(s, f);
184  }  }
185    
186  static void print_string_jsval(jsval value, FILE * 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, FILE * 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 109  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 142  Line 256 
256    }    }
257  }  }
258    
259  static void instrument_expression(JSParseNode * node, FILE * f);  static void instrument_expression(JSParseNode * node, Stream * f);
260  static void instrument_statement(JSParseNode * node, FILE * 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 {
264      FUNCTION_NORMAL,
265      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, FILE * f, int indent) {  static void instrument_function(JSParseNode * node, Stream * f, int indent, enum FunctionType type) {
281      assert(node->pn_arity == PN_FUNC);    assert(node->pn_type == TOK_FUNCTION);
282      assert(ATOM_IS_OBJECT(node->pn_funAtom));    assert(node->pn_arity == PN_FUNC);
283      JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom);    JSObject * object = node->pn_funpob->object;
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      fprintf(f, "%*s", indent, "");    Stream_printf(f, "%*s", indent, "");
289      fprintf(f, "function");    if (type == FUNCTION_NORMAL) {
290        Stream_write_string(f, "function");
291      /* function name */    }
292      if (function->atom) {  
293        fputc(' ', f);    /* function name */
294        print_string_atom(function->atom, f);    if (function->atom) {
295      }      Stream_write_char(f, ' ');
296        print_string_atom(function->atom, f);
297      /* function parameters */    }
298      fprintf(f, "(");  
299      JSAtom ** params = xmalloc(function->nargs * sizeof(JSAtom *));    /*
300      for (int i = 0; i < function->nargs; i++) {    function parameters - see JS_DecompileFunction in jsapi.cpp, which calls
301        /* initialize to NULL for sanity check */    js_DecompileFunction in jsopcode.cpp
302        params[i] = NULL;    */
303      }    Stream_write_string(f, "(");
304      JSScope * scope = OBJ_SCOPE(object);    JSArenaPool pool;
305      for (JSScopeProperty * scope_property = SCOPE_LAST_PROP(scope); scope_property != NULL; scope_property = scope_property->parent) {    JS_INIT_ARENA_POOL(&pool, "instrument_function", 256, 1, &context->scriptStackQuota);
306        if (scope_property->getter != js_GetArgument) {    jsuword * local_names = NULL;
307          continue;    if (JS_GET_LOCAL_NAME_COUNT(function)) {
308        }      local_names = js_GetLocalNameArray(context, function, &pool);
309        assert(scope_property->flags & SPROP_HAS_SHORTID);      if (local_names == NULL) {
310        assert((uint16) scope_property->shortid < function->nargs);        fatal("out of memory");
       assert(JSID_IS_ATOM(scope_property->id));  
       params[(uint16) scope_property->shortid] = JSID_TO_ATOM(scope_property->id);  
311      }      }
312      for (int i = 0; i < function->nargs; i++) {    }
313        assert(params[i] != NULL);    for (int i = 0; i < function->nargs; i++) {
314        if (i > 0) {      if (i > 0) {
315          fprintf(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      fprintf(f, ") {\n");      print_string_atom(param, f);
322      free(params);    }
323      JS_FinishArenaPool(&pool);
324      Stream_write_string(f, ") {\n");
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      fprintf(f, "}\n");    Stream_write_string(f, "}\n");
336  }  }
337    
338  static void instrument_function_call(JSParseNode * node, FILE * f) {  static void instrument_function_call(JSParseNode * node, Stream * f) {
339    instrument_expression(node->pn_head, f);    JSParseNode * function_node = node->pn_head;
340    fputc('(', f);    if (function_node->pn_type == TOK_FUNCTION) {
341    for (struct JSParseNode * p = node->pn_head->pn_next; p != NULL; p = p->pn_next) {      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, '(');
396      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        fprintf(f, ", ");        Stream_write_string(f, ", ");
399      }      }
400      instrument_expression(p, f);      instrument_expression(p, f);
401    }    }
402    fputc(')', 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  /*  /*
# Line 222  Line 443 
443  TOK_INSTANCEOF  binary  TOK_INSTANCEOF  binary
444  TOK_IN          binary  TOK_IN          binary
445  */  */
446  static void instrument_expression(JSParseNode * node, FILE * f) {  static void instrument_expression(JSParseNode * node, Stream * f) {
447    switch (node->pn_type) {    switch (node->pn_type) {
448    case TOK_FUNCTION:    case TOK_FUNCTION:
449      instrument_function(node, f, 0);      instrument_function(node, f, 0, FUNCTION_NORMAL);
450      break;      break;
451    case TOK_COMMA:    case TOK_COMMA:
452      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) {
453        if (p != node->pn_head) {        if (p != node->pn_head) {
454          fprintf(f, ", ");          Stream_write_string(f, ", ");
455        }        }
456        instrument_expression(p, f);        instrument_expression(p, f);
457      }      }
458      break;      break;
459    case TOK_ASSIGN:    case TOK_ASSIGN:
460      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
461      fputc(' ', f);      Stream_write_char(f, ' ');
462      switch (node->pn_op) {      switch (node->pn_op) {
463      case JSOP_ADD:      case JSOP_ADD:
464      case JSOP_SUB:      case JSOP_SUB:
# Line 250  Line 471 
471      case JSOP_BITOR:      case JSOP_BITOR:
472      case JSOP_BITXOR:      case JSOP_BITXOR:
473      case JSOP_DIV:      case JSOP_DIV:
474        fprintf(f, "%s", get_op(node->pn_op));        Stream_printf(f, "%s", get_op(node->pn_op));
475        break;        break;
476      default:      default:
477        /* do nothing - it must be a simple assignment */        /* do nothing - it must be a simple assignment */
478        break;        break;
479      }      }
480      fprintf(f, "= ");      Stream_write_string(f, "= ");
481      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
482      break;      break;
483    case TOK_HOOK:    case TOK_HOOK:
484      instrument_expression(node->pn_kid1, f);      instrument_expression(node->pn_kid1, f);
485      fprintf(f, "? ");      Stream_write_string(f, "? ");
486      instrument_expression(node->pn_kid2, f);      instrument_expression(node->pn_kid2, f);
487      fprintf(f, ": ");      Stream_write_string(f, ": ");
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);  
     fprintf(f, " || ");  
     instrument_expression(node->pn_right, f);  
     break;  
491    case TOK_AND:    case TOK_AND:
     instrument_expression(node->pn_left, f);  
     fprintf(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 289  Line 502 
502      switch (node->pn_arity) {      switch (node->pn_arity) {
503      case PN_BINARY:      case PN_BINARY:
504        instrument_expression(node->pn_left, f);        instrument_expression(node->pn_left, f);
505        fprintf(f, " %s ", get_op(node->pn_op));        Stream_printf(f, " %s ", get_op(node->pn_op));
506        instrument_expression(node->pn_right, f);        instrument_expression(node->pn_right, f);
507        break;        break;
508      case PN_LIST:      case PN_LIST:
509        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) {
510          if (p != node->pn_head) {          if (p != node->pn_head) {
511            fprintf(f, " %s ", get_op(node->pn_op));            Stream_printf(f, " %s ", get_op(node->pn_op));
512          }          }
513          instrument_expression(p, f);          instrument_expression(p, f);
514        }        }
# Line 307  Line 520 
520    case TOK_UNARYOP:    case TOK_UNARYOP:
521      switch (node->pn_op) {      switch (node->pn_op) {
522      case JSOP_NEG:      case JSOP_NEG:
523        fputc('-', f);        Stream_write_char(f, '-');
524        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
525        break;        break;
526      case JSOP_POS:      case JSOP_POS:
527        fputc('+', f);        Stream_write_char(f, '+');
528        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
529        break;        break;
530      case JSOP_NOT:      case JSOP_NOT:
531        fputc('!', f);        Stream_write_char(f, '!');
532        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
533        break;        break;
534      case JSOP_BITNOT:      case JSOP_BITNOT:
535        fputc('~', f);        Stream_write_char(f, '~');
536        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
537        break;        break;
538      case JSOP_TYPEOF:      case JSOP_TYPEOF:
539        fprintf(f, "typeof ");        Stream_write_string(f, "typeof ");
540        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
541        break;        break;
542      case JSOP_VOID:      case JSOP_VOID:
543        fprintf(f, "void ");        Stream_write_string(f, "void ");
544        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
545        break;        break;
546      default:      default:
# Line 344  Line 557 
557      case JSOP_INCNAME:      case JSOP_INCNAME:
558      case JSOP_INCPROP:      case JSOP_INCPROP:
559      case JSOP_INCELEM:      case JSOP_INCELEM:
560        fprintf(f, "++");        Stream_write_string(f, "++");
561        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
562        break;        break;
563      case JSOP_DECNAME:      case JSOP_DECNAME:
564      case JSOP_DECPROP:      case JSOP_DECPROP:
565      case JSOP_DECELEM:      case JSOP_DECELEM:
566        fprintf(f, "--");        Stream_write_string(f, "--");
567        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
568        break;        break;
569      case JSOP_NAMEINC:      case JSOP_NAMEINC:
570      case JSOP_PROPINC:      case JSOP_PROPINC:
571      case JSOP_ELEMINC:      case JSOP_ELEMINC:
572        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
573        fprintf(f, "++");        Stream_write_string(f, "++");
574        break;        break;
575      case JSOP_NAMEDEC:      case JSOP_NAMEDEC:
576      case JSOP_PROPDEC:      case JSOP_PROPDEC:
577      case JSOP_ELEMDEC:      case JSOP_ELEMDEC:
578        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
579        fprintf(f, "--");        Stream_write_string(f, "--");
580        break;        break;
581      default:      default:
582        abort();        abort();
# Line 371  Line 584 
584      }      }
585      break;      break;
586    case TOK_NEW:    case TOK_NEW:
587      fprintf(f, "new ");      Stream_write_string(f, "new ");
588      instrument_function_call(node, f);      instrument_function_call(node, f);
589      break;      break;
590    case TOK_DELETE:    case TOK_DELETE:
591      fprintf(f, "delete ");      Stream_write_string(f, "delete ");
592      instrument_expression(node->pn_kid, f);      instrument_expression(node->pn_kid, f);
593      break;      break;
594    case TOK_DOT:    case TOK_DOT:
# Line 388  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          fputc('.', 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          fputc('[', f);          must_quote = false;
616          }
617          if (must_quote) {
618            Stream_write_char(f, '[');
619          print_quoted_string_atom(node->pn_atom, f);          print_quoted_string_atom(node->pn_atom, f);
620          fputc(']', 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:
629      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
630      fputc('[', f);      Stream_write_char(f, '[');
631      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
632      fputc(']', f);      Stream_write_char(f, ']');
633      break;      break;
634    case TOK_LP:    case TOK_LP:
635      instrument_function_call(node, f);      instrument_function_call(node, f);
636      break;      break;
637    case TOK_RB:    case TOK_RB:
638      fputc('[', f);      Stream_write_char(f, '[');
639      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) {
640        if (p != node->pn_head) {        if (p != node->pn_head) {
641          fprintf(f, ", ");          Stream_write_string(f, ", ");
642        }        }
643        /* TOK_COMMA is a special case: a hole in the array */        /* TOK_COMMA is a special case: a hole in the array */
644        if (p->pn_type != TOK_COMMA) {        if (p->pn_type != TOK_COMMA) {
# Line 421  Line 646 
646        }        }
647      }      }
648      if (node->pn_extra == PNX_ENDCOMMA) {      if (node->pn_extra == PNX_ENDCOMMA) {
649        fputc(',', f);        Stream_write_char(f, ',');
650      }      }
651      fputc(']', f);      Stream_write_char(f, ']');
652      break;      break;
653    case TOK_RC:    case TOK_RC:
654      fputc('{', 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          fprintf(f, ", ");          Stream_write_string(f, ", ");
661          }
662    
663          /* check whether this is a getter or setter */
664          switch (p->pn_op) {
665          case JSOP_GETTER:
666          case JSOP_SETTER:
667            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);
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);
678            break;
679          default:
680            instrument_expression(p->pn_left, f);
681            Stream_write_string(f, ": ");
682            instrument_expression(p->pn_right, f);
683            break;
684        }        }
       instrument_expression(p->pn_left, f);  
       fprintf(f, ": ");  
       instrument_expression(p->pn_right, f);  
685      }      }
686      fputc('}', f);      Stream_write_char(f, '}');
687      break;      break;
688    case TOK_RP:    case TOK_RP:
689      fputc('(', f);      Stream_write_char(f, '(');
690      instrument_expression(node->pn_kid, f);      instrument_expression(node->pn_kid, f);
691      fputc(')', f);      Stream_write_char(f, ')');
692      break;      break;
693    case TOK_NAME:    case TOK_NAME:
694      print_string_atom(node->pn_atom, f);      print_string_atom(node->pn_atom, f);
# Line 449  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 477  Line 713 
713      To keep the output simple, special-case zero.      To keep the output simple, special-case zero.
714      */      */
715      if (node->pn_dval == 0.0) {      if (node->pn_dval == 0.0) {
716        fprintf(f, "0");        Stream_write_string(f, "0");
717      }      }
718      else {      else {
719        fprintf(f, "%.15g", node->pn_dval);        Stream_printf(f, "%.15g", node->pn_dval);
720      }      }
721      break;      break;
722    case TOK_PRIMARY:    case TOK_PRIMARY:
723      switch (node->pn_op) {      switch (node->pn_op) {
724      case JSOP_TRUE:      case JSOP_TRUE:
725        fprintf(f, "true");        Stream_write_string(f, "true");
726        break;        break;
727      case JSOP_FALSE:      case JSOP_FALSE:
728        fprintf(f, "false");        Stream_write_string(f, "false");
729        break;        break;
730      case JSOP_NULL:      case JSOP_NULL:
731        fprintf(f, "null");        Stream_write_string(f, "null");
732        break;        break;
733      case JSOP_THIS:      case JSOP_THIS:
734        fprintf(f, "this");        Stream_write_string(f, "this");
735        break;        break;
736      /* jsscan.h mentions `super' ??? */      /* jsscan.h mentions `super' ??? */
737      default:      default:
# Line 504  Line 740 
740      break;      break;
741    case TOK_INSTANCEOF:    case TOK_INSTANCEOF:
742      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
743      fprintf(f, " instanceof ");      Stream_write_string(f, " instanceof ");
744      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
745      break;      break;
746    case TOK_IN:    case TOK_IN:
747      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
748      fprintf(f, " in ");      Stream_write_string(f, " in ");
749      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
750      break;      break;
751      case TOK_LEXICALSCOPE:
752        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        assert(node->pn_expr->pn_left->pn_type == TOK_LP);
757        assert(node->pn_expr->pn_left->pn_arity == PN_LIST);
758        instrument_declarations(node->pn_expr->pn_left, f);
759        Stream_write_string(f, ") ");
760        instrument_expression(node->pn_expr->pn_right, f);
761        break;
762      case TOK_YIELD:
763        assert(node->pn_arity == PN_UNARY);
764        Stream_write_string(f, "yield ");
765        instrument_expression(node->pn_kid, f);
766        break;
767      case TOK_ARRAYCOMP:
768        assert(node->pn_arity == PN_LIST);
769        {
770          JSParseNode * block_node;
771          switch (node->pn_count) {
772          case 1:
773            block_node = node->pn_head;
774            break;
775          case 2:
776            block_node = node->pn_head->pn_next;
777            break;
778          default:
779            abort();
780            break;
781          }
782          assert(block_node->pn_type == TOK_LEXICALSCOPE);
783          assert(block_node->pn_arity == PN_NAME);
784          JSParseNode * for_node = block_node->pn_expr;
785          assert(for_node->pn_type == TOK_FOR);
786          assert(for_node->pn_arity == PN_BINARY);
787          JSParseNode * if_node = NULL;
788          JSParseNode * push_node;
789          switch (for_node->pn_right->pn_type) {
790          case TOK_ARRAYPUSH:
791            push_node = for_node->pn_right;
792            assert(push_node->pn_arity == PN_UNARY);
793            break;
794          case TOK_IF:
795            if_node = for_node->pn_right;
796            assert(if_node->pn_arity == PN_TERNARY);
797            push_node = if_node->pn_kid2;
798            break;
799          default:
800            abort();
801            break;
802          }
803          Stream_write_char(f, '[');
804          instrument_expression(push_node->pn_kid, f);
805          Stream_write_char(f, ' ');
806          output_for_in(for_node, f);
807          if (if_node) {
808            Stream_write_string(f, " if (");
809            instrument_expression(if_node->pn_kid1, f);
810            Stream_write_char(f, ')');
811          }
812          Stream_write_char(f, ']');
813        }
814        break;
815      case TOK_VAR:
816        assert(node->pn_arity == PN_LIST);
817        Stream_write_string(f, "var ");
818        instrument_declarations(node, f);
819        break;
820      case TOK_LET:
821        assert(node->pn_arity == PN_LIST);
822        Stream_write_string(f, "let ");
823        instrument_declarations(node, f);
824        break;
825    default:    default:
826      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);
827    }    }
828  }  }
829    
830  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) {  
831    switch (node->pn_type) {    switch (node->pn_type) {
832    case TOK_FUNCTION:    case TOK_FUNCTION:
833      instrument_function(node, f, indent);      instrument_function(node, f, indent, FUNCTION_NORMAL);
834      break;      break;
835    case TOK_LC:    case TOK_LC:
836      assert(node->pn_arity == PN_LIST);      assert(node->pn_arity == PN_LIST);
837  /*  /*
838      fprintf(f, "{\n");      Stream_write_string(f, "{\n");
839  */  */
840      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) {
841        instrument_statement(p, f, indent);        instrument_statement(p, f, indent, false);
842      }      }
843  /*  /*
844      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
845      fprintf(f, "}\n");      Stream_write_string(f, "}\n");
846  */  */
847      break;      break;
848    case TOK_IF:    case TOK_IF:
849      {
850      assert(node->pn_arity == PN_TERNARY);      assert(node->pn_arity == PN_TERNARY);
851      fprintf(f, "%*s", indent, "");  
852      fprintf(f, "if (");      uint16_t line = node->pn_pos.begin.lineno;
853        if (! is_jscoverage_if) {
854          if (line > num_lines) {
855            fatal("%s: script contains more than 65,535 lines", file_id);
856          }
857          if (line >= 2 && exclusive_directives[line - 2]) {
858            is_jscoverage_if = true;
859          }
860        }
861    
862        Stream_printf(f, "%*s", indent, "");
863        Stream_write_string(f, "if (");
864      instrument_expression(node->pn_kid1, f);      instrument_expression(node->pn_kid1, f);
865      fprintf(f, ") {\n");      Stream_write_string(f, ") {\n");
866      instrument_statement(node->pn_kid2, f, indent + 2);      if (is_jscoverage_if && node->pn_kid3) {
867      fprintf(f, "%*s", indent, "");        uint16_t else_start = node->pn_kid3->pn_pos.begin.lineno;
868      fprintf(f, "}\n");        uint16_t else_end = node->pn_kid3->pn_pos.end.lineno + 1;
869      if (node->pn_kid3) {        Stream_printf(f, "%*s", indent + 2, "");
870        fprintf(f, "%*s", indent, "");        Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, else_start, else_end);
871        fprintf(f, "else {\n");      }
872        instrument_statement(node->pn_kid3, f, indent + 2);      instrument_statement(node->pn_kid2, f, indent + 2, false);
873        fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
874        fprintf(f, "}\n");      Stream_write_string(f, "}\n");
875    
876        if (node->pn_kid3 || is_jscoverage_if) {
877          Stream_printf(f, "%*s", indent, "");
878          Stream_write_string(f, "else {\n");
879    
880          if (is_jscoverage_if) {
881            uint16_t if_start = node->pn_kid2->pn_pos.begin.lineno + 1;
882            uint16_t if_end = node->pn_kid2->pn_pos.end.lineno + 1;
883            Stream_printf(f, "%*s", indent + 2, "");
884            Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_start, if_end);
885          }
886    
887          if (node->pn_kid3) {
888            instrument_statement(node->pn_kid3, f, indent + 2, is_jscoverage_if);
889          }
890    
891          Stream_printf(f, "%*s", indent, "");
892          Stream_write_string(f, "}\n");
893      }      }
894    
895      break;      break;
896      }
897    case TOK_SWITCH:    case TOK_SWITCH:
898      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
899      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
900      fprintf(f, "switch (");      Stream_write_string(f, "switch (");
901      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
902      fprintf(f, ") {\n");      Stream_write_string(f, ") {\n");
903      for (struct JSParseNode * p = node->pn_right->pn_head; p != NULL; p = p->pn_next) {      {
904        fprintf(f, "%*s", indent, "");        JSParseNode * list = node->pn_right;
905        switch (p->pn_type) {        if (list->pn_type == TOK_LEXICALSCOPE) {
906        case TOK_CASE:          list = list->pn_expr;
907          fprintf(f, "case ");        }
908          instrument_expression(p->pn_left, f);        for (struct JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
909          fprintf(f, ":\n");          Stream_printf(f, "%*s", indent, "");
910          break;          switch (p->pn_type) {
911        case TOK_DEFAULT:          case TOK_CASE:
912          fprintf(f, "default:\n");            Stream_write_string(f, "case ");
913          break;            instrument_expression(p->pn_left, f);
914        default:            Stream_write_string(f, ":\n");
915          abort();            break;
916          break;          case TOK_DEFAULT:
917              Stream_write_string(f, "default:\n");
918              break;
919            default:
920              abort();
921              break;
922            }
923            instrument_statement(p->pn_right, f, indent + 2, false);
924        }        }
       instrument_statement(p->pn_right, f, indent + 2);  
925      }      }
926      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
927      fprintf(f, "}\n");      Stream_write_string(f, "}\n");
928      break;      break;
929    case TOK_CASE:    case TOK_CASE:
930    case TOK_DEFAULT:    case TOK_DEFAULT:
# Line 602  Line 932 
932      break;      break;
933    case TOK_WHILE:    case TOK_WHILE:
934      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
935      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
936      fprintf(f, "while (");      Stream_write_string(f, "while (");
937      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
938      fprintf(f, ") {\n");      Stream_write_string(f, ") {\n");
939      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
940      fprintf(f, "}\n");      Stream_write_string(f, "}\n");
941      break;      break;
942    case TOK_DO:    case TOK_DO:
943      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
944      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
945      fprintf(f, "do {\n");      Stream_write_string(f, "do {\n");
946      instrument_statement(node->pn_left, f, indent + 2);      instrument_statement(node->pn_left, f, indent + 2, false);
947      fprintf(f, "}\n");      Stream_write_string(f, "}\n");
948      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
949      fprintf(f, "while (");      Stream_write_string(f, "while (");
950      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
951      fprintf(f, ");\n");      Stream_write_string(f, ");\n");
952      break;      break;
953    case TOK_FOR:    case TOK_FOR:
954      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
955      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
     fprintf(f, "for (");  
956      switch (node->pn_left->pn_type) {      switch (node->pn_left->pn_type) {
957      case TOK_IN:      case TOK_IN:
958        /* for/in */        /* for/in */
959        assert(node->pn_left->pn_arity == PN_BINARY);        assert(node->pn_left->pn_arity == PN_BINARY);
960        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);  
961        break;        break;
962      case TOK_RESERVED:      case TOK_RESERVED:
963        /* for (;;) */        /* for (;;) */
964        assert(node->pn_left->pn_arity == PN_TERNARY);        assert(node->pn_left->pn_arity == PN_TERNARY);
965          Stream_write_string(f, "for (");
966        if (node->pn_left->pn_kid1) {        if (node->pn_left->pn_kid1) {
967          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);  
         }  
968        }        }
969        fprintf(f, ";");        Stream_write_string(f, ";");
970        if (node->pn_left->pn_kid2) {        if (node->pn_left->pn_kid2) {
971          fputc(' ', f);          Stream_write_char(f, ' ');
972          instrument_expression(node->pn_left->pn_kid2, f);          instrument_expression(node->pn_left->pn_kid2, f);
973        }        }
974        fprintf(f, ";");        Stream_write_string(f, ";");
975        if (node->pn_left->pn_kid3) {        if (node->pn_left->pn_kid3) {
976          fputc(' ', f);          Stream_write_char(f, ' ');
977          instrument_expression(node->pn_left->pn_kid3, f);          instrument_expression(node->pn_left->pn_kid3, f);
978        }        }
979          Stream_write_char(f, ')');
980        break;        break;
981      default:      default:
982        abort();        abort();
983        break;        break;
984      }      }
985      fprintf(f, ") {\n");      Stream_write_string(f, " {\n");
986      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
987      fprintf(f, "}\n");      Stream_write_string(f, "}\n");
988      break;      break;
989    case TOK_THROW:    case TOK_THROW:
990      assert(node->pn_arity == PN_UNARY);      assert(node->pn_arity == PN_UNARY);
991      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
992      fprintf(f, "throw ");      Stream_write_string(f, "throw ");
993      instrument_expression(node->pn_u.unary.kid, f);      instrument_expression(node->pn_u.unary.kid, f);
994      fprintf(f, ";\n");      Stream_write_string(f, ";\n");
995      break;      break;
996    case TOK_TRY:    case TOK_TRY:
997      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
998      fprintf(f, "try {\n");      Stream_write_string(f, "try {\n");
999      instrument_statement(node->pn_kid1, f, indent + 2);      instrument_statement(node->pn_kid1, f, indent + 2, false);
1000      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
1001      fprintf(f, "}\n");      Stream_write_string(f, "}\n");
1002      {      if (node->pn_kid2) {
1003        for (JSParseNode * catch = node->pn_kid2; catch != NULL; catch = catch->pn_kid2) {        assert(node->pn_kid2->pn_type == TOK_RESERVED);
1004          for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) {
1005            assert(scope->pn_type == TOK_LEXICALSCOPE);
1006            JSParseNode * catch = scope->pn_expr;
1007          assert(catch->pn_type == TOK_CATCH);          assert(catch->pn_type == TOK_CATCH);
1008          fprintf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
1009          fprintf(f, "catch (");          Stream_write_string(f, "catch (");
1010            /* this may not be a name - destructuring assignment */
1011            /*
1012          assert(catch->pn_kid1->pn_arity == PN_NAME);          assert(catch->pn_kid1->pn_arity == PN_NAME);
1013          print_string_atom(catch->pn_kid1->pn_atom, f);          print_string_atom(catch->pn_kid1->pn_atom, f);
1014          if (catch->pn_kid1->pn_expr) {          */
1015            fprintf(f, " if ");          instrument_expression(catch->pn_kid1, f);
1016            instrument_expression(catch->pn_kid1->pn_expr, f);          if (catch->pn_kid2) {
1017          }            Stream_write_string(f, " if ");
1018          fprintf(f, ") {\n");            instrument_expression(catch->pn_kid2, f);
1019          instrument_statement(catch->pn_kid3, f, indent + 2);          }
1020          fprintf(f, "%*s", indent, "");          Stream_write_string(f, ") {\n");
1021          fprintf(f, "}\n");          instrument_statement(catch->pn_kid3, f, indent + 2, false);
1022            Stream_printf(f, "%*s", indent, "");
1023            Stream_write_string(f, "}\n");
1024        }        }
1025      }      }
1026      if (node->pn_kid3) {      if (node->pn_kid3) {
1027        fprintf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
1028        fprintf(f, "finally {\n");        Stream_write_string(f, "finally {\n");
1029        instrument_statement(node->pn_kid3, f, indent + 2);        instrument_statement(node->pn_kid3, f, indent + 2, false);
1030        fprintf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
1031        fprintf(f, "}\n");        Stream_write_string(f, "}\n");
1032      }      }
1033      break;      break;
1034    case TOK_CATCH:    case TOK_CATCH:
# Line 723  Line 1037 
1037    case TOK_BREAK:    case TOK_BREAK:
1038    case TOK_CONTINUE:    case TOK_CONTINUE:
1039      assert(node->pn_arity == PN_NAME || node->pn_arity == PN_NULLARY);      assert(node->pn_arity == PN_NAME || node->pn_arity == PN_NULLARY);
1040      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
1041      fputs(node->pn_type == TOK_BREAK? "break": "continue", f);      Stream_write_string(f, node->pn_type == TOK_BREAK? "break": "continue");
1042      JSAtom * atom = node->pn_u.name.atom;      JSAtom * atom = node->pn_u.name.atom;
1043      if (atom != NULL) {      if (atom != NULL) {
1044        fputc(' ', f);        Stream_write_char(f, ' ');
1045        print_string_atom(node->pn_atom, f);        print_string_atom(node->pn_atom, f);
1046      }      }
1047      fprintf(f, ";\n");      Stream_write_string(f, ";\n");
1048      break;      break;
1049    case TOK_WITH:    case TOK_WITH:
1050      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
1051      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
1052      fprintf(f, "with (");      Stream_write_string(f, "with (");
1053      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
1054      fprintf(f, ") {\n");      Stream_write_string(f, ") {\n");
1055      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
1056      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
1057      fprintf(f, "}\n");      Stream_write_string(f, "}\n");
1058      break;      break;
1059    case TOK_VAR:    case TOK_VAR:
1060      instrument_var_statement(node, f, indent);      Stream_printf(f, "%*s", indent, "");
1061      fprintf(f, ";\n");      instrument_expression(node, f);
1062        Stream_write_string(f, ";\n");
1063      break;      break;
1064    case TOK_RETURN:    case TOK_RETURN:
1065      assert(node->pn_arity == PN_UNARY);      assert(node->pn_arity == PN_UNARY);
1066      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
1067      fprintf(f, "return");      Stream_write_string(f, "return");
1068      if (node->pn_kid != NULL) {      if (node->pn_kid != NULL) {
1069        fprintf(f, " ");        Stream_write_char(f, ' ');
1070        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
1071      }      }
1072      fprintf(f, ";\n");      Stream_write_string(f, ";\n");
1073      break;      break;
1074    case TOK_SEMI:    case TOK_SEMI:
1075      assert(node->pn_arity == PN_UNARY);      assert(node->pn_arity == PN_UNARY);
1076      fprintf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
1077      if (node->pn_kid != NULL) {      if (node->pn_kid != NULL) {
1078        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
1079      }      }
1080      fprintf(f, ";\n");      Stream_write_string(f, ";\n");
1081      break;      break;
1082    case TOK_COLON:    case TOK_COLON:
1083      assert(node->pn_arity == PN_NAME);      assert(node->pn_arity == PN_NAME);
# Line 770  Line 1085 
1085      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
1086      statement it's supposed to label ...      statement it's supposed to label ...
1087      */      */
1088      fprintf(f, "%*s", indent < 2? 0: indent - 2, "");      Stream_printf(f, "%*s", indent < 2? 0: indent - 2, "");
1089      print_string_atom(node->pn_atom, f);      print_string_atom(node->pn_atom, f);
1090      fprintf(f, ":\n");      Stream_write_string(f, ":\n");
1091      /*      /*
1092      ... use output_statement instead of instrument_statement.      ... use output_statement instead of instrument_statement.
1093      */      */
1094      output_statement(node->pn_expr, f, indent);      output_statement(node->pn_expr, f, indent, false);
1095        break;
1096      case TOK_LEXICALSCOPE:
1097        /* let statement */
1098        assert(node->pn_arity == PN_NAME);
1099        switch (node->pn_expr->pn_type) {
1100        case TOK_LET:
1101          /* let statement */
1102          assert(node->pn_expr->pn_arity == PN_BINARY);
1103          instrument_statement(node->pn_expr, f, indent, false);
1104          break;
1105        case TOK_LC:
1106          /* block */
1107          Stream_printf(f, "%*s", indent, "");
1108          Stream_write_string(f, "{\n");
1109          instrument_statement(node->pn_expr, f, indent + 2, false);
1110          Stream_printf(f, "%*s", indent, "");
1111          Stream_write_string(f, "}\n");
1112          break;
1113        case TOK_FOR:
1114          instrument_statement(node->pn_expr, f, indent, false);
1115          break;
1116        default:
1117          abort();
1118          break;
1119        }
1120        break;
1121      case TOK_LET:
1122        switch (node->pn_arity) {
1123        case PN_BINARY:
1124          /* let statement */
1125          Stream_printf(f, "%*s", indent, "");
1126          Stream_write_string(f, "let (");
1127          assert(node->pn_left->pn_type == TOK_LP);
1128          assert(node->pn_left->pn_arity == PN_LIST);
1129          instrument_declarations(node->pn_left, f);
1130          Stream_write_string(f, ") {\n");
1131          instrument_statement(node->pn_right, f, indent + 2, false);
1132          Stream_printf(f, "%*s", indent, "");
1133          Stream_write_string(f, "}\n");
1134          break;
1135        case PN_LIST:
1136          /* let definition */
1137          Stream_printf(f, "%*s", indent, "");
1138          instrument_expression(node, f);
1139          Stream_write_string(f, ";\n");
1140          break;
1141        default:
1142          abort();
1143          break;
1144        }
1145        break;
1146      case TOK_DEBUGGER:
1147        Stream_printf(f, "%*s", indent, "");
1148        Stream_write_string(f, "debugger;\n");
1149      break;      break;
1150    default:    default:
1151      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 1157 
1157  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
1158  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
1159  */  */
1160  static void instrument_statement(JSParseNode * node, FILE * f, int indent) {  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
1161    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC && node->pn_type != TOK_LEXICALSCOPE) {
1162      int line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
1163        if (line > num_lines) {
1164          fatal("%s: script contains more than 65,535 lines", file_id);
1165        }
1166    
1167      /* the root node has line number 0 */      /* the root node has line number 0 */
1168      if (line != 0) {      if (line != 0) {
1169        fprintf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
1170        fprintf(f, "_$jscoverage['%s'][%d]++;\n", file_id, line);        Stream_printf(f, "_$jscoverage['%s'][%d]++;\n", file_id, line);
1171        lines[line - 1] = 1;        lines[line - 1] = 1;
1172      }      }
1173    }    }
1174    output_statement(node, f, indent);    output_statement(node, f, indent, is_jscoverage_if);
1175  }  }
1176    
1177  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) {
1178    file_id = id;    const jschar * characters_end = characters + line_end;
1179      const jschar * cp = characters + line_start;
1180      const char * bp = prefix;
1181      for (;;) {
1182        if (*bp == '\0') {
1183          return true;
1184        }
1185        else if (cp == characters_end) {
1186          return false;
1187        }
1188        else if (*cp != *bp) {
1189          return false;
1190        }
1191        bp++;
1192        cp++;
1193      }
1194    }
1195    
1196    /* scan the javascript */  static bool characters_are_white_space(const jschar * characters, size_t line_start, size_t line_end) {
1197    JSTokenStream * token_stream = js_NewFileTokenStream(context, NULL, input);    /* XXX - other Unicode space */
1198    if (token_stream == NULL) {    const jschar * end = characters + line_end;
1199      fatal("cannot create token stream from file: %s", file_id);    for (const jschar * p = characters + line_start; p < end; p++) {
1200        jschar c = *p;
1201        if (c == 0x9 || c == 0xB || c == 0xC || c == 0x20 || c == 0xA0) {
1202          continue;
1203        }
1204        else {
1205          return false;
1206        }
1207    }    }
1208      return true;
1209    }
1210    
1211    static void error_reporter(JSContext * context, const char * message, JSErrorReport * report) {
1212      fprintf(stderr, "jscoverage: parse error: line %u: %s\n", report->lineno, message);
1213    }
1214    
1215    void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) {
1216      file_id = id;
1217    
1218    /* parse the javascript */    /* parse the javascript */
1219    JSParseNode * node = js_ParseTokenStream(context, global, token_stream);    JSParseContext parse_context;
1220      if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) {
1221        fatal("cannot create token stream from file: %s", file_id);
1222      }
1223      JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);
1224      JSParseNode * node = js_ParseScript(context, global, &parse_context);
1225    if (node == NULL) {    if (node == NULL) {
1226        js_ReportUncaughtException(context);
1227      fatal("parse error in file: %s", file_id);      fatal("parse error in file: %s", file_id);
1228    }    }
1229    int num_lines = node->pn_pos.end.lineno;    JS_SetErrorReporter(context, old_error_reporter);
1230      num_lines = node->pn_pos.end.lineno;
1231    lines = xmalloc(num_lines);    lines = xmalloc(num_lines);
1232    for (int i = 0; i < num_lines; i++) {    for (unsigned int i = 0; i < num_lines; i++) {
1233      lines[i] = 0;      lines[i] = 0;
1234    }    }
1235    
1236      /* search code for conditionals */
1237      exclusive_directives = xnew(bool, num_lines);
1238      for (unsigned int i = 0; i < num_lines; i++) {
1239        exclusive_directives[i] = false;
1240      }
1241    
1242      bool has_conditionals = false;
1243      struct IfDirective * if_directives = NULL;
1244      size_t line_number = 0;
1245      size_t i = 0;
1246      while (i < num_characters) {
1247        if (line_number == UINT16_MAX) {
1248          fatal("%s: script has more than 65,535 lines", file_id);
1249        }
1250        line_number++;
1251        size_t line_start = i;
1252        jschar c;
1253        bool done = false;
1254        while (! done && i < num_characters) {
1255          c = characters[i];
1256          switch (c) {
1257          case '\r':
1258          case '\n':
1259          case 0x2028:
1260          case 0x2029:
1261            done = true;
1262            break;
1263          default:
1264            i++;
1265          }
1266        }
1267        size_t line_end = i;
1268        if (i < num_characters) {
1269          i++;
1270          if (c == '\r' && i < num_characters && characters[i] == '\n') {
1271            i++;
1272          }
1273        }
1274    
1275        if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) {
1276          has_conditionals = true;
1277    
1278          if (characters_are_white_space(characters, line_start + 16, line_end)) {
1279            exclusive_directives[line_number - 1] = true;
1280          }
1281          else {
1282            struct IfDirective * if_directive = xnew(struct IfDirective, 1);
1283            if_directive->condition_start = characters + line_start + 16;
1284            if_directive->condition_end = characters + line_end;
1285            if_directive->start_line = line_number;
1286            if_directive->end_line = 0;
1287            if_directive->next = if_directives;
1288            if_directives = if_directive;
1289          }
1290        }
1291        else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) {
1292          for (struct IfDirective * p = if_directives; p != NULL; p = p->next) {
1293            if (p->end_line == 0) {
1294              p->end_line = line_number;
1295              break;
1296            }
1297          }
1298        }
1299      }
1300    
1301    /*    /*
1302    Create a temporary file - we can't write directly to the output because we    An instrumented JavaScript file has 4 sections:
1303    need to know the line number info first.    1. initialization
1304      2. instrumented source code
1305      3. conditionals
1306      4. original source code
1307    */    */
   FILE * temporary = fopen(temporary_file_name, "w+");  
   if (temporary == NULL) {  
     fatal("cannot create temporary file for script: %s", file_id);  
   }  
1308    
1309    /* write instrumented javascript to the temporary */    Stream * instrumented = Stream_new(0);
1310    instrument_statement(node, temporary, 0);    instrument_statement(node, instrumented, 0, false);
1311      js_FinishParseContext(context, &parse_context);
1312    
1313    /* write line number info to the output */    /* write line number info to the output */
1314    fprintf(output, "/* automatically generated by JSCoverage - do not edit */\n");    Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");
1315    fprintf(output, "if (! top._$jscoverage) {\n  top._$jscoverage = {};\n}\n");    Stream_write_string(output, "if (! top._$jscoverage) {\n  top._$jscoverage = {};\n}\n");
1316    fprintf(output, "var _$jscoverage = top._$jscoverage;\n");    Stream_write_string(output, "var _$jscoverage = top._$jscoverage;\n");
1317    fprintf(output, "if (! _$jscoverage['%s']) {\n", file_id);    Stream_printf(output, "if (! _$jscoverage['%s']) {\n", file_id);
1318    fprintf(output, "  _$jscoverage['%s'] = [];\n", file_id);    Stream_printf(output, "  _$jscoverage['%s'] = [];\n", file_id);
1319    for (int i = 0; i < num_lines; i++) {    for (int i = 0; i < num_lines; i++) {
1320      if (lines[i]) {      if (lines[i]) {
1321        fprintf(output, "  _$jscoverage['%s'][%d] = 0;\n", file_id, i + 1);        Stream_printf(output, "  _$jscoverage['%s'][%d] = 0;\n", file_id, i + 1);
1322      }      }
1323    }    }
1324    fprintf(output, "}\n");    Stream_write_string(output, "}\n");
1325    free(lines);    free(lines);
1326    lines = NULL;    lines = NULL;
1327      free(exclusive_directives);
1328      exclusive_directives = NULL;
1329    
1330      /* conditionals */
1331      if (has_conditionals) {
1332        Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);
1333      }
1334    
1335      /* copy the instrumented source code to the output */
1336      Stream_write(output, instrumented->data, instrumented->length);
1337    
1338      /* conditionals */
1339      for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) {
1340        Stream_write_string(output, "if (!(");
1341        print_javascript(if_directive->condition_start, if_directive->condition_end - if_directive->condition_start, output);
1342        Stream_write_string(output, ")) {\n");
1343        Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line);
1344        Stream_write_string(output, "}\n");
1345      }
1346    
1347      /* free */
1348      while (if_directives != NULL) {
1349        struct IfDirective * if_directive = if_directives;
1350        if_directives = if_directives->next;
1351        free(if_directive);
1352      }
1353    
1354    /* copy the temporary to the output */    /* copy the original source to the output */
1355    fseek(temporary, 0, SEEK_SET);    Stream_printf(output, "_$jscoverage['%s'].source = ", file_id);
1356    copy_stream(temporary, output);    jscoverage_write_source(id, characters, num_characters, output);
1357      Stream_printf(output, ";\n");
1358    
1359    fclose(temporary);    Stream_delete(instrumented);
1360    
1361    file_id = NULL;    file_id = NULL;
1362  }  }
1363    
1364  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) {
1365    instrument_js_stream(id, 0, input, output, temporary_file_name);    Stream_write_string(output, "[");
1366      if (jscoverage_highlight) {
1367        Stream * highlighted_stream = Stream_new(num_characters);
1368        jscoverage_highlight_js(context, id, characters, num_characters, highlighted_stream);
1369        size_t i = 0;
1370        while (i < highlighted_stream->length) {
1371          if (i > 0) {
1372            Stream_write_char(output, ',');
1373          }
1374    
1375          Stream_write_char(output, '"');
1376          bool done = false;
1377          while (! done) {
1378            char c = highlighted_stream->data[i];
1379            switch (c) {
1380            case 0x8:
1381              /* backspace */
1382              Stream_write_string(output, "\\b");
1383              break;
1384            case 0x9:
1385              /* horizontal tab */
1386              Stream_write_string(output, "\\t");
1387              break;
1388            case 0xa:
1389              /* line feed (new line) */
1390              done = true;
1391              break;
1392            /* IE doesn't support this */
1393            /*
1394            case 0xb:
1395              Stream_write_string(output, "\\v");
1396              break;
1397            */
1398            case 0xc:
1399              /* form feed */
1400              Stream_write_string(output, "\\f");
1401              break;
1402            case 0xd:
1403              /* carriage return */
1404              done = true;
1405              if (i + 1 < highlighted_stream->length && highlighted_stream->data[i + 1] == '\n') {
1406                i++;
1407              }
1408              break;
1409            case '"':
1410              Stream_write_string(output, "\\\"");
1411              break;
1412            case '\\':
1413              Stream_write_string(output, "\\\\");
1414              break;
1415            default:
1416              Stream_write_char(output, c);
1417              break;
1418            }
1419            i++;
1420            if (i >= highlighted_stream->length) {
1421              done = true;
1422            }
1423          }
1424          Stream_write_char(output, '"');
1425        }
1426        Stream_delete(highlighted_stream);
1427      }
1428      else {
1429        size_t i = 0;
1430        while (i < num_characters) {
1431          if (i > 0) {
1432            Stream_write_char(output, ',');
1433          }
1434    
1435          Stream_write_char(output, '"');
1436          bool done = false;
1437          while (! done) {
1438            jschar c = characters[i];
1439            switch (c) {
1440            case 0x8:
1441              /* backspace */
1442              Stream_write_string(output, "\\b");
1443              break;
1444            case 0x9:
1445              /* horizontal tab */
1446              Stream_write_string(output, "\\t");
1447              break;
1448            case 0xa:
1449              /* line feed (new line) */
1450              done = true;
1451              break;
1452            /* IE doesn't support this */
1453            /*
1454            case 0xb:
1455              Stream_write_string(output, "\\v");
1456              break;
1457            */
1458            case 0xc:
1459              /* form feed */
1460              Stream_write_string(output, "\\f");
1461              break;
1462            case 0xd:
1463              /* carriage return */
1464              done = true;
1465              if (i + 1 < num_characters && characters[i + 1] == '\n') {
1466                i++;
1467              }
1468              break;
1469            case '"':
1470              Stream_write_string(output, "\\\"");
1471              break;
1472            case '\\':
1473              Stream_write_string(output, "\\\\");
1474              break;
1475            case '&':
1476              Stream_write_string(output, "&amp;");
1477              break;
1478            case '<':
1479              Stream_write_string(output, "&lt;");
1480              break;
1481            case '>':
1482              Stream_write_string(output, "&gt;");
1483              break;
1484            case 0x2028:
1485            case 0x2029:
1486              done = true;
1487              break;
1488            default:
1489              if (32 <= c && c <= 126) {
1490                Stream_write_char(output, c);
1491              }
1492              else {
1493                Stream_printf(output, "&#%d;", c);
1494              }
1495              break;
1496            }
1497            i++;
1498            if (i >= num_characters) {
1499              done = true;
1500            }
1501          }
1502          Stream_write_char(output, '"');
1503        }
1504      }
1505      Stream_write_string(output, "]");
1506    }
1507    
1508    void jscoverage_copy_resources(const char * destination_directory) {
1509      copy_resource("jscoverage.html", destination_directory);
1510      copy_resource("jscoverage.css", destination_directory);
1511      copy_resource("jscoverage.js", destination_directory);
1512      copy_resource("jscoverage-ie.css", destination_directory);
1513      copy_resource("jscoverage-throbber.gif", destination_directory);
1514      copy_resource("jscoverage-highlight.css", destination_directory);
1515    }
1516    
1517    /*
1518    coverage reports
1519    */
1520    
1521    struct FileCoverageList {
1522      FileCoverage * file_coverage;
1523      struct FileCoverageList * next;
1524    };
1525    
1526    struct Coverage {
1527      JSHashTable * coverage_table;
1528      struct FileCoverageList * coverage_list;
1529    };
1530    
1531    static int compare_strings(const void * p1, const void * p2) {
1532      return strcmp(p1, p2) == 0;
1533    }
1534    
1535    Coverage * Coverage_new(void) {
1536      Coverage * result = xmalloc(sizeof(Coverage));
1537      result->coverage_table = JS_NewHashTable(1024, JS_HashString, compare_strings, NULL, NULL, NULL);
1538      if (result->coverage_table == NULL) {
1539        fatal("cannot create hash table");
1540      }
1541      result->coverage_list = NULL;
1542      return result;
1543    }
1544    
1545    void Coverage_delete(Coverage * coverage) {
1546      JS_HashTableDestroy(coverage->coverage_table);
1547      struct FileCoverageList * p = coverage->coverage_list;
1548      while (p != NULL) {
1549        free(p->file_coverage->coverage_lines);
1550        if (p->file_coverage->source_lines != NULL) {
1551          for (uint32 i = 0; i < p->file_coverage->num_source_lines; i++) {
1552            free(p->file_coverage->source_lines[i]);
1553          }
1554          free(p->file_coverage->source_lines);
1555        }
1556        free(p->file_coverage->id);
1557        free(p->file_coverage);
1558        struct FileCoverageList * q = p;
1559        p = p->next;
1560        free(q);
1561      }
1562      free(coverage);
1563    }
1564    
1565    struct EnumeratorArg {
1566      CoverageForeachFunction f;
1567      void * p;
1568    };
1569    
1570    static intN enumerator(JSHashEntry * entry, intN i, void * arg) {
1571      struct EnumeratorArg * enumerator_arg = arg;
1572      enumerator_arg->f(entry->value, i, enumerator_arg->p);
1573      return 0;
1574    }
1575    
1576    void Coverage_foreach_file(Coverage * coverage, CoverageForeachFunction f, void * p) {
1577      struct EnumeratorArg enumerator_arg;
1578      enumerator_arg.f = f;
1579      enumerator_arg.p = p;
1580      JS_HashTableEnumerateEntries(coverage->coverage_table, enumerator, &enumerator_arg);
1581    }
1582    
1583    int jscoverage_parse_json(Coverage * coverage, const uint8_t * json, size_t length) {
1584      int result = 0;
1585    
1586      jschar * base = js_InflateString(context, (char *) json, &length);
1587      if (base == NULL) {
1588        fatal("out of memory");
1589      }
1590    
1591      jschar * parenthesized_json = xnew(jschar, addst(length, 2));
1592      parenthesized_json[0] = '(';
1593      memcpy(parenthesized_json + 1, base, mulst(length, sizeof(jschar)));
1594      parenthesized_json[length + 1] = ')';
1595    
1596      JS_free(context, base);
1597    
1598      JSParseContext parse_context;
1599      if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) {
1600        free(parenthesized_json);
1601        return -1;
1602      }
1603      JSParseNode * root = js_ParseScript(context, global, &parse_context);
1604      free(parenthesized_json);
1605      if (root == NULL) {
1606        result = -1;
1607        goto done;
1608      }
1609    
1610      /* root node must be TOK_LC */
1611      if (root->pn_type != TOK_LC) {
1612        result = -1;
1613        goto done;
1614      }
1615      JSParseNode * semi = root->pn_u.list.head;
1616    
1617      /* the list must be TOK_SEMI and it must contain only one element */
1618      if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {
1619        result = -1;
1620        goto done;
1621      }
1622      JSParseNode * parenthesized = semi->pn_kid;
1623    
1624      /* this must be a parenthesized expression */
1625      if (parenthesized->pn_type != TOK_RP) {
1626        result = -1;
1627        goto done;
1628      }
1629      JSParseNode * object = parenthesized->pn_kid;
1630    
1631      /* this must be an object literal */
1632      if (object->pn_type != TOK_RC) {
1633        result = -1;
1634        goto done;
1635      }
1636    
1637      for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {
1638        /* every element of this list must be TOK_COLON */
1639        if (p->pn_type != TOK_COLON) {
1640          result = -1;
1641          goto done;
1642        }
1643    
1644        /* the key must be a string representing the file */
1645        JSParseNode * key = p->pn_left;
1646        if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {
1647          result = -1;
1648          goto done;
1649        }
1650        char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));
1651    
1652        /* the value must be an object literal OR an array */
1653        JSParseNode * value = p->pn_right;
1654        if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {
1655          result = -1;
1656          goto done;
1657        }
1658    
1659        JSParseNode * array = NULL;
1660        JSParseNode * source = NULL;
1661        if (value->pn_type == TOK_RB) {
1662          /* an array */
1663          array = value;
1664        }
1665        else if (value->pn_type == TOK_RC) {
1666          /* an object literal */
1667          if (value->pn_count != 2) {
1668            result = -1;
1669            goto done;
1670          }
1671          for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {
1672            if (element->pn_type != TOK_COLON) {
1673              result = -1;
1674              goto done;
1675            }
1676            JSParseNode * left = element->pn_left;
1677            if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {
1678              result = -1;
1679              goto done;
1680            }
1681            const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));
1682            if (strcmp(s, "coverage") == 0) {
1683              array = element->pn_right;
1684              if (array->pn_type != TOK_RB) {
1685                result = -1;
1686                goto done;
1687              }
1688            }
1689            else if (strcmp(s, "source") == 0) {
1690              source = element->pn_right;
1691              if (source->pn_type != TOK_RB) {
1692                result = -1;
1693                goto done;
1694              }
1695            }
1696            else {
1697              result = -1;
1698              goto done;
1699            }
1700          }
1701        }
1702        else {
1703          result = -1;
1704          goto done;
1705        }
1706    
1707        if (array == NULL) {
1708          result = -1;
1709          goto done;
1710        }
1711    
1712        /* look up the file in the coverage table */
1713        FileCoverage * file_coverage = JS_HashTableLookup(coverage->coverage_table, id_bytes);
1714        if (file_coverage == NULL) {
1715          /* not there: create a new one */
1716          char * id = xstrdup(id_bytes);
1717          file_coverage = xmalloc(sizeof(FileCoverage));
1718          file_coverage->id = id;
1719          file_coverage->num_coverage_lines = array->pn_count;
1720          file_coverage->coverage_lines = xnew(int, array->pn_count);
1721          file_coverage->source_lines = NULL;
1722    
1723          /* set coverage for all lines */
1724          uint32 i = 0;
1725          for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {
1726            if (element->pn_type == TOK_NUMBER) {
1727              file_coverage->coverage_lines[i] = (int) element->pn_dval;
1728            }
1729            else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1730              file_coverage->coverage_lines[i] = -1;
1731            }
1732            else {
1733              result = -1;
1734              goto done;
1735            }
1736          }
1737          assert(i == array->pn_count);
1738    
1739          /* add to the hash table */
1740          JS_HashTableAdd(coverage->coverage_table, id, file_coverage);
1741          struct FileCoverageList * coverage_list = xmalloc(sizeof(struct FileCoverageList));
1742          coverage_list->file_coverage = file_coverage;
1743          coverage_list->next = coverage->coverage_list;
1744          coverage->coverage_list = coverage_list;
1745        }
1746        else {
1747          /* sanity check */
1748          assert(strcmp(file_coverage->id, id_bytes) == 0);
1749          if (file_coverage->num_coverage_lines != array->pn_count) {
1750            result = -2;
1751            goto done;
1752          }
1753    
1754          /* merge the coverage */
1755          uint32 i = 0;
1756          for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {
1757            if (element->pn_type == TOK_NUMBER) {
1758              if (file_coverage->coverage_lines[i] == -1) {
1759                result = -2;
1760                goto done;
1761              }
1762              file_coverage->coverage_lines[i] += (int) element->pn_dval;
1763            }
1764            else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1765              if (file_coverage->coverage_lines[i] != -1) {
1766                result = -2;
1767                goto done;
1768              }
1769            }
1770            else {
1771              result = -1;
1772              goto done;
1773            }
1774          }
1775          assert(i == array->pn_count);
1776        }
1777    
1778        /* if this JSON file has source, use it */
1779        if (file_coverage->source_lines == NULL && source != NULL) {
1780          file_coverage->num_source_lines = source->pn_count;
1781          file_coverage->source_lines = xnew(char *, source->pn_count);
1782          uint32 i = 0;
1783          for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {
1784            if (element->pn_type != TOK_STRING) {
1785              result = -1;
1786              goto done;
1787            }
1788            file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));
1789          }
1790          assert(i == source->pn_count);
1791        }
1792      }
1793    
1794    done:
1795      js_FinishParseContext(context, &parse_context);
1796      return result;
1797  }  }

Legend:
Removed from v.90  
changed lines
  Added in v.353

  ViewVC Help
Powered by ViewVC 1.1.24