/[jscoverage]/trunk/instrument-js.cpp
ViewVC logotype

Diff of /trunk/instrument-js.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

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

  ViewVC Help
Powered by ViewVC 1.1.24