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

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

  ViewVC Help
Powered by ViewVC 1.1.24