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

Diff of /trunk/instrument-js.c

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

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

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

  ViewVC Help
Powered by ViewVC 1.1.24