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

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

  ViewVC Help
Powered by ViewVC 1.1.24