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

Diff of /trunk/instrument-js.cpp

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

trunk/instrument-js.c revision 179 by siliconforks, Sun Sep 21 18:35:21 2008 UTC trunk/instrument-js.cpp revision 401 by siliconforks, Tue Dec 9 09:47:21 2008 UTC
# Line 1  Line 1 
1  /*  /*
2      instrument-js.c - JavaScript instrumentation routines      instrument-js.cpp - JavaScript instrumentation routines
3      Copyright (C) 2007, 2008 siliconforks.com      Copyright (C) 2007, 2008 siliconforks.com
4    
5      This program is free software; you can redistribute it and/or modify      This program is free software; you can redistribute it and/or modify
# Line 22  Line 22 
22  #include "instrument-js.h"  #include "instrument-js.h"
23    
24  #include <assert.h>  #include <assert.h>
25    #include <math.h>
26  #include <stdlib.h>  #include <stdlib.h>
27  #include <string.h>  #include <string.h>
28    
29  #include <jsapi.h>  #include <jsapi.h>
30    #include <jsarena.h>
31  #include <jsatom.h>  #include <jsatom.h>
32    #include <jsemit.h>
33    #include <jsexn.h>
34  #include <jsfun.h>  #include <jsfun.h>
35  #include <jsinterp.h>  #include <jsinterp.h>
36    #include <jsiter.h>
37  #include <jsparse.h>  #include <jsparse.h>
38  #include <jsregexp.h>  #include <jsregexp.h>
39  #include <jsscope.h>  #include <jsscope.h>
# Line 40  Line 45 
45  #include "resource-manager.h"  #include "resource-manager.h"
46  #include "util.h"  #include "util.h"
47    
48    struct IfDirective {
49      const jschar * condition_start;
50      const jschar * condition_end;
51      uint16_t start_line;
52      uint16_t end_line;
53      struct IfDirective * next;
54    };
55    
56    bool jscoverage_mozilla = false;
57    
58    static bool * exclusive_directives = NULL;
59    
60  static JSRuntime * runtime = NULL;  static JSRuntime * runtime = NULL;
61  static JSContext * context = NULL;  static JSContext * context = NULL;
62  static JSObject * global = NULL;  static JSObject * global = NULL;
63    static JSVersion js_version = JSVERSION_ECMA_3;
64    
65  /*  /*
66  JSParseNode objects store line numbers starting from 1.  JSParseNode objects store line numbers starting from 1.
# Line 50  Line 68 
68  */  */
69  static const char * file_id = NULL;  static const char * file_id = NULL;
70  static char * lines = NULL;  static char * lines = NULL;
71    static uint16_t num_lines = 0;
72    
73    void jscoverage_set_js_version(const char * version) {
74      js_version = JS_StringToVersion(version);
75      if (js_version != JSVERSION_UNKNOWN) {
76        return;
77      }
78    
79      char * end;
80      js_version = (JSVersion) strtol(version, &end, 10);
81      if ((size_t) (end - version) != strlen(version)) {
82        fatal("invalid version: %s", version);
83      }
84    }
85    
86  void jscoverage_init(void) {  void jscoverage_init(void) {
87    runtime = JS_NewRuntime(8L * 1024L * 1024L);    runtime = JS_NewRuntime(8L * 1024L * 1024L);
# Line 62  Line 94 
94      fatal("cannot create context");      fatal("cannot create context");
95    }    }
96    
97      JS_SetVersion(context, js_version);
98    
99    global = JS_NewObject(context, NULL, NULL, NULL);    global = JS_NewObject(context, NULL, NULL, NULL);
100    if (global == NULL) {    if (global == NULL) {
101      fatal("cannot create global object");      fatal("cannot create global object");
# Line 77  Line 111 
111    JS_DestroyRuntime(runtime);    JS_DestroyRuntime(runtime);
112  }  }
113    
114    static void print_javascript(const jschar * characters, size_t num_characters, Stream * f) {
115      for (size_t i = 0; i < num_characters; i++) {
116        jschar c = characters[i];
117        /*
118        XXX does not handle no-break space, other unicode "space separator"
119        */
120        switch (c) {
121        case 0x9:
122        case 0xB:
123        case 0xC:
124          Stream_write_char(f, c);
125          break;
126        default:
127          if (32 <= c && c <= 126) {
128            Stream_write_char(f, c);
129          }
130          else {
131            Stream_printf(f, "\\u%04x", c);
132          }
133          break;
134        }
135      }
136    }
137    
138  static void print_string(JSString * s, Stream * f) {  static void print_string(JSString * s, Stream * f) {
139    size_t length = JSSTRING_LENGTH(s);    size_t length = JSSTRING_LENGTH(s);
140    jschar * characters = JSSTRING_CHARS(s);    jschar * characters = JSSTRING_CHARS(s);
# Line 111  Line 169 
169        case 0xa:        case 0xa:
170          Stream_write_string(f, "\\n");          Stream_write_string(f, "\\n");
171          break;          break;
172          /* IE doesn't support this */
173          /*
174        case 0xb:        case 0xb:
175          Stream_write_string(f, "\\v");          Stream_write_string(f, "\\v");
176          break;          break;
177          */
178        case 0xc:        case 0xc:
179          Stream_write_string(f, "\\f");          Stream_write_string(f, "\\f");
180          break;          break;
# Line 160  Line 221 
221    
222  static const char * get_op(uint8 op) {  static const char * get_op(uint8 op) {
223    switch(op) {    switch(op) {
224      case JSOP_OR:
225        return "||";
226      case JSOP_AND:
227        return "&&";
228    case JSOP_BITOR:    case JSOP_BITOR:
229      return "|";      return "|";
230    case JSOP_BITXOR:    case JSOP_BITXOR:
# Line 170  Line 235 
235      return "==";      return "==";
236    case JSOP_NE:    case JSOP_NE:
237      return "!=";      return "!=";
238    case JSOP_NEW_EQ:    case JSOP_STRICTEQ:
239      return "===";      return "===";
240    case JSOP_NEW_NE:    case JSOP_STRICTNE:
241      return "!==";      return "!==";
242    case JSOP_LT:    case JSOP_LT:
243      return "<";      return "<";
# Line 203  Line 268 
268    }    }
269  }  }
270    
271  static void instrument_expression(JSParseNode * node, Stream * f);  static void output_expression(JSParseNode * node, Stream * f, bool parenthesize_object_literals);
272  static void instrument_statement(JSParseNode * node, Stream * f, int indent);  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
273    static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
274    
275  enum FunctionType {  enum FunctionType {
276    FUNCTION_NORMAL,    FUNCTION_NORMAL,
277    FUNCTION_GETTER_OR_SETTER    FUNCTION_GETTER_OR_SETTER
278  };  };
279    
280    static void output_for_in(JSParseNode * node, Stream * f) {
281      assert(node->pn_type == TOK_FOR);
282      assert(node->pn_arity == PN_BINARY);
283      Stream_write_string(f, "for ");
284      if (node->pn_iflags & JSITER_FOREACH) {
285        Stream_write_string(f, "each ");
286      }
287      Stream_write_char(f, '(');
288      output_expression(node->pn_left, f, false);
289      Stream_write_char(f, ')');
290    }
291    
292    static void output_array_comprehension_or_generator_expression(JSParseNode * node, Stream * f) {
293      assert(node->pn_type == TOK_LEXICALSCOPE);
294      assert(node->pn_arity == PN_NAME);
295      JSParseNode * for_node = node->pn_expr;
296      assert(for_node->pn_type == TOK_FOR);
297      assert(for_node->pn_arity == PN_BINARY);
298      JSParseNode * p = for_node;
299      while (p->pn_type == TOK_FOR) {
300        p = p->pn_right;
301      }
302      JSParseNode * if_node = NULL;
303      if (p->pn_type == TOK_IF) {
304        if_node = p;
305        assert(if_node->pn_arity == PN_TERNARY);
306        p = if_node->pn_kid2;
307      }
308      assert(p->pn_arity == PN_UNARY);
309      p = p->pn_kid;
310      if (p->pn_type == TOK_YIELD) {
311        /* for generator expressions */
312        p = p->pn_kid;
313      }
314    
315      output_expression(p, f, false);
316      p = for_node;
317      while (p->pn_type == TOK_FOR) {
318        Stream_write_char(f, ' ');
319        output_for_in(p, f);
320        p = p->pn_right;
321      }
322      if (if_node) {
323        Stream_write_string(f, " if (");
324        output_expression(if_node->pn_kid1, f, false);
325        Stream_write_char(f, ')');
326      }
327    }
328    
329  static void instrument_function(JSParseNode * node, Stream * f, int indent, enum FunctionType type) {  static void instrument_function(JSParseNode * node, Stream * f, int indent, enum FunctionType type) {
330      assert(node->pn_type == TOK_FUNCTION);
331    assert(node->pn_arity == PN_FUNC);    assert(node->pn_arity == PN_FUNC);
332    assert(ATOM_IS_OBJECT(node->pn_funAtom));    JSObject * object = node->pn_funpob->object;
   JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom);  
333    assert(JS_ObjectIsFunction(context, object));    assert(JS_ObjectIsFunction(context, object));
334    JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);    JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
335    assert(function);    assert(function);
336    assert(object == function->object);    assert(object == &function->object);
337    Stream_printf(f, "%*s", indent, "");    Stream_printf(f, "%*s", indent, "");
338    if (type == FUNCTION_NORMAL) {    if (type == FUNCTION_NORMAL) {
339      Stream_write_string(f, "function");      Stream_write_string(f, "function ");
340    }    }
341    
342    /* function name */    /* function name */
343    if (function->atom) {    if (function->atom) {
     Stream_write_char(f, ' ');  
344      print_string_atom(function->atom, f);      print_string_atom(function->atom, f);
345    }    }
346    
347    /* function parameters */    /*
348    Stream_write_string(f, "(");    function parameters - see JS_DecompileFunction in jsapi.cpp, which calls
349    JSAtom ** params = xnew(JSAtom *, function->nargs);    js_DecompileFunction in jsopcode.cpp
350    for (int i = 0; i < function->nargs; i++) {    */
351      /* initialize to NULL for sanity check */    Stream_write_char(f, '(');
352      params[i] = NULL;    JSArenaPool pool;
353    }    JS_INIT_ARENA_POOL(&pool, "instrument_function", 256, 1, &context->scriptStackQuota);
354    JSScope * scope = OBJ_SCOPE(object);    jsuword * local_names = NULL;
355    for (JSScopeProperty * scope_property = SCOPE_LAST_PROP(scope); scope_property != NULL; scope_property = scope_property->parent) {    if (JS_GET_LOCAL_NAME_COUNT(function)) {
356      if (scope_property->getter != js_GetArgument) {      local_names = js_GetLocalNameArray(context, function, &pool);
357        continue;      if (local_names == NULL) {
358          fatal("out of memory");
359      }      }
     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);  
360    }    }
361      bool destructuring = false;
362    for (int i = 0; i < function->nargs; i++) {    for (int i = 0; i < function->nargs; i++) {
     assert(params[i] != NULL);  
363      if (i > 0) {      if (i > 0) {
364        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
365      }      }
366      if (ATOM_IS_STRING(params[i])) {      JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]);
367        print_string_atom(params[i], f);      if (param == NULL) {
368          destructuring = true;
369          JSParseNode * expression = NULL;
370          assert(node->pn_body->pn_type == TOK_LC || node->pn_body->pn_type == TOK_SEQ);
371          JSParseNode * semi = node->pn_body->pn_head;
372          assert(semi->pn_type == TOK_SEMI);
373          JSParseNode * comma = semi->pn_kid;
374          assert(comma->pn_type == TOK_COMMA);
375          for (JSParseNode * p = comma->pn_head; p != NULL; p = p->pn_next) {
376            assert(p->pn_type == TOK_ASSIGN);
377            JSParseNode * rhs = p->pn_right;
378            assert(JSSTRING_LENGTH(ATOM_TO_STRING(rhs->pn_atom)) == 0);
379            if (rhs->pn_slot == i) {
380              expression = p->pn_left;
381              break;
382            }
383          }
384          assert(expression != NULL);
385          output_expression(expression, f, false);
386        }
387        else {
388          print_string_atom(param, f);
389      }      }
390    }    }
391      JS_FinishArenaPool(&pool);
392    Stream_write_string(f, ") {\n");    Stream_write_string(f, ") {\n");
   free(params);  
393    
394    /* function body */    /* function body */
395    instrument_statement(node->pn_body, f, indent + 2);    if (function->flags & JSFUN_EXPR_CLOSURE) {
396        /* expression closure - use output_statement instead of instrument_statement */
397        if (node->pn_body->pn_type == TOK_SEQ) {
398          assert(node->pn_body->pn_arity == PN_LIST);
399          assert(node->pn_body->pn_count == 2);
400          output_statement(node->pn_body->pn_head->pn_next, f, indent + 2, false);
401        }
402        else {
403          output_statement(node->pn_body, f, indent + 2, false);
404        }
405      }
406      else {
407        assert(node->pn_body->pn_type == TOK_LC);
408        assert(node->pn_body->pn_arity == PN_LIST);
409        JSParseNode * p = node->pn_body->pn_head;
410        if (destructuring) {
411          p = p->pn_next;
412        }
413        for (; p != NULL; p = p->pn_next) {
414          instrument_statement(p, f, indent + 2, false);
415        }
416      }
417    
418    Stream_write_string(f, "}\n");    Stream_write_char(f, '}');
419  }  }
420    
421  static void instrument_function_call(JSParseNode * node, Stream * f) {  static void instrument_function_call(JSParseNode * node, Stream * f) {
422    instrument_expression(node->pn_head, f);    JSParseNode * function_node = node->pn_head;
423      if (function_node->pn_type == TOK_FUNCTION) {
424        JSObject * object = function_node->pn_funpob->object;
425        assert(JS_ObjectIsFunction(context, object));
426        JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
427        assert(function);
428        assert(object == &function->object);
429    
430        if (function_node->pn_flags & TCF_GENEXP_LAMBDA) {
431          /* it's a generator expression */
432          Stream_write_char(f, '(');
433          output_array_comprehension_or_generator_expression(function_node->pn_body, f);
434          Stream_write_char(f, ')');
435          return;
436        }
437      }
438      output_expression(function_node, f, false);
439    Stream_write_char(f, '(');    Stream_write_char(f, '(');
440    for (struct JSParseNode * p = node->pn_head->pn_next; p != NULL; p = p->pn_next) {    for (struct JSParseNode * p = function_node->pn_next; p != NULL; p = p->pn_next) {
441      if (p != node->pn_head->pn_next) {      if (p != node->pn_head->pn_next) {
442        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
443      }      }
444      instrument_expression(p, f);      output_expression(p, f, false);
445    }    }
446    Stream_write_char(f, ')');    Stream_write_char(f, ')');
447  }  }
448    
449    static void instrument_declarations(JSParseNode * list, Stream * f) {
450      assert(list->pn_arity == PN_LIST);
451      for (JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
452        if (p != list->pn_head) {
453          Stream_write_string(f, ", ");
454        }
455        output_expression(p, f, false);
456      }
457    }
458    
459  /*  /*
460  See <Expressions> in jsparse.h.  See <Expressions> in jsparse.h.
461  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
# Line 290  Line 469 
469  TOK_INSTANCEOF  binary  TOK_INSTANCEOF  binary
470  TOK_IN          binary  TOK_IN          binary
471  */  */
472  static void instrument_expression(JSParseNode * node, Stream * f) {  static void output_expression(JSParseNode * node, Stream * f, bool parenthesize_object_literals) {
473    switch (node->pn_type) {    switch (node->pn_type) {
474    case TOK_FUNCTION:    case TOK_FUNCTION:
475        Stream_write_char(f, '(');
476      instrument_function(node, f, 0, FUNCTION_NORMAL);      instrument_function(node, f, 0, FUNCTION_NORMAL);
477        Stream_write_char(f, ')');
478      break;      break;
479    case TOK_COMMA:    case TOK_COMMA:
480      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) {
481        if (p != node->pn_head) {        if (p != node->pn_head) {
482          Stream_write_string(f, ", ");          Stream_write_string(f, ", ");
483        }        }
484        instrument_expression(p, f);        output_expression(p, f, parenthesize_object_literals);
485      }      }
486      break;      break;
487    case TOK_ASSIGN:    case TOK_ASSIGN:
488      instrument_expression(node->pn_left, f);      output_expression(node->pn_left, f, parenthesize_object_literals);
489      Stream_write_char(f, ' ');      Stream_write_char(f, ' ');
490      switch (node->pn_op) {      switch (node->pn_op) {
491      case JSOP_ADD:      case JSOP_ADD:
# Line 325  Line 506 
506        break;        break;
507      }      }
508      Stream_write_string(f, "= ");      Stream_write_string(f, "= ");
509      instrument_expression(node->pn_right, f);      output_expression(node->pn_right, f, false);
510      break;      break;
511    case TOK_HOOK:    case TOK_HOOK:
512      instrument_expression(node->pn_kid1, f);      output_expression(node->pn_kid1, f, parenthesize_object_literals);
513      Stream_write_string(f, "? ");      Stream_write_string(f, "? ");
514      instrument_expression(node->pn_kid2, f);      output_expression(node->pn_kid2, f, false);
515      Stream_write_string(f, ": ");      Stream_write_string(f, ": ");
516      instrument_expression(node->pn_kid3, f);      output_expression(node->pn_kid3, f, false);
517      break;      break;
518    case TOK_OR:    case TOK_OR:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " || ");  
     instrument_expression(node->pn_right, f);  
     break;  
519    case TOK_AND:    case TOK_AND:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " && ");  
     instrument_expression(node->pn_right, f);  
     break;  
520    case TOK_BITOR:    case TOK_BITOR:
521    case TOK_BITXOR:    case TOK_BITXOR:
522    case TOK_BITAND:    case TOK_BITAND:
# Line 356  Line 529 
529    case TOK_DIVOP:    case TOK_DIVOP:
530      switch (node->pn_arity) {      switch (node->pn_arity) {
531      case PN_BINARY:      case PN_BINARY:
532        instrument_expression(node->pn_left, f);        output_expression(node->pn_left, f, parenthesize_object_literals);
533        Stream_printf(f, " %s ", get_op(node->pn_op));        Stream_printf(f, " %s ", get_op(node->pn_op));
534        instrument_expression(node->pn_right, f);        output_expression(node->pn_right, f, false);
535        break;        break;
536      case PN_LIST:      case PN_LIST:
537        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) {
538          if (p != node->pn_head) {          if (p == node->pn_head) {
539              output_expression(p, f, parenthesize_object_literals);
540            }
541            else {
542            Stream_printf(f, " %s ", get_op(node->pn_op));            Stream_printf(f, " %s ", get_op(node->pn_op));
543              output_expression(p, f, false);
544          }          }
         instrument_expression(p, f);  
545        }        }
546        break;        break;
547      default:      default:
# Line 375  Line 551 
551    case TOK_UNARYOP:    case TOK_UNARYOP:
552      switch (node->pn_op) {      switch (node->pn_op) {
553      case JSOP_NEG:      case JSOP_NEG:
554        Stream_write_char(f, '-');        Stream_write_string(f, "- ");
555        instrument_expression(node->pn_kid, f);        output_expression(node->pn_kid, f, false);
556        break;        break;
557      case JSOP_POS:      case JSOP_POS:
558        Stream_write_char(f, '+');        Stream_write_string(f, "+ ");
559        instrument_expression(node->pn_kid, f);        output_expression(node->pn_kid, f, false);
560        break;        break;
561      case JSOP_NOT:      case JSOP_NOT:
562        Stream_write_char(f, '!');        Stream_write_string(f, "! ");
563        instrument_expression(node->pn_kid, f);        output_expression(node->pn_kid, f, false);
564        break;        break;
565      case JSOP_BITNOT:      case JSOP_BITNOT:
566        Stream_write_char(f, '~');        Stream_write_string(f, "~ ");
567        instrument_expression(node->pn_kid, f);        output_expression(node->pn_kid, f, false);
568        break;        break;
569      case JSOP_TYPEOF:      case JSOP_TYPEOF:
570        Stream_write_string(f, "typeof ");        Stream_write_string(f, "typeof ");
571        instrument_expression(node->pn_kid, f);        output_expression(node->pn_kid, f, false);
572        break;        break;
573      case JSOP_VOID:      case JSOP_VOID:
574        Stream_write_string(f, "void ");        Stream_write_string(f, "void ");
575        instrument_expression(node->pn_kid, f);        output_expression(node->pn_kid, f, false);
576        break;        break;
577      default:      default:
578        abort();        fatal_source(file_id, node->pn_pos.begin.lineno, "unknown operator (%d)", node->pn_op);
579        break;        break;
580      }      }
581      break;      break;
# Line 413  Line 589 
589      case JSOP_INCPROP:      case JSOP_INCPROP:
590      case JSOP_INCELEM:      case JSOP_INCELEM:
591        Stream_write_string(f, "++");        Stream_write_string(f, "++");
592        instrument_expression(node->pn_kid, f);        output_expression(node->pn_kid, f, false);
593        break;        break;
594      case JSOP_DECNAME:      case JSOP_DECNAME:
595      case JSOP_DECPROP:      case JSOP_DECPROP:
596      case JSOP_DECELEM:      case JSOP_DECELEM:
597        Stream_write_string(f, "--");        Stream_write_string(f, "--");
598        instrument_expression(node->pn_kid, f);        output_expression(node->pn_kid, f, false);
599        break;        break;
600      case JSOP_NAMEINC:      case JSOP_NAMEINC:
601      case JSOP_PROPINC:      case JSOP_PROPINC:
602      case JSOP_ELEMINC:      case JSOP_ELEMINC:
603        instrument_expression(node->pn_kid, f);        output_expression(node->pn_kid, f, parenthesize_object_literals);
604        Stream_write_string(f, "++");        Stream_write_string(f, "++");
605        break;        break;
606      case JSOP_NAMEDEC:      case JSOP_NAMEDEC:
607      case JSOP_PROPDEC:      case JSOP_PROPDEC:
608      case JSOP_ELEMDEC:      case JSOP_ELEMDEC:
609        instrument_expression(node->pn_kid, f);        output_expression(node->pn_kid, f, parenthesize_object_literals);
610        Stream_write_string(f, "--");        Stream_write_string(f, "--");
611        break;        break;
612      default:      default:
# Line 444  Line 620 
620      break;      break;
621    case TOK_DELETE:    case TOK_DELETE:
622      Stream_write_string(f, "delete ");      Stream_write_string(f, "delete ");
623      instrument_expression(node->pn_kid, f);      output_expression(node->pn_kid, f, false);
624      break;      break;
625    case TOK_DOT:    case TOK_DOT:
626        /* numeric literals must be parenthesized */
627        switch (node->pn_expr->pn_type) {
628        case TOK_NUMBER:
629          Stream_write_char(f, '(');
630          output_expression(node->pn_expr, f, false);
631          Stream_write_char(f, ')');
632          break;
633        default:
634          output_expression(node->pn_expr, f, true);
635          break;
636        }
637      /*      /*
638      This may have originally been x['foo-bar'].  Because the string 'foo-bar'      This may have originally been x['foo-bar'].  Because the string 'foo-bar'
639      contains illegal characters, we have to use the subscript syntax instead of      contains illegal characters, we have to use the subscript syntax instead of
640      the dot syntax.      the dot syntax.
641      */      */
     instrument_expression(node->pn_expr, f);  
642      assert(ATOM_IS_STRING(node->pn_atom));      assert(ATOM_IS_STRING(node->pn_atom));
643      {      {
644        JSString * s = ATOM_TO_STRING(node->pn_atom);        JSString * s = ATOM_TO_STRING(node->pn_atom);
645        /* XXX - semantics changed in 1.7 */        bool must_quote;
646        if (! ATOM_KEYWORD(node->pn_atom) && js_IsIdentifier(s)) {        if (JSSTRING_LENGTH(s) == 0) {
647          Stream_write_char(f, '.');          must_quote = true;
648          print_string_atom(node->pn_atom, f);        }
649          else if (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF) {
650            must_quote = true;
651          }
652          else if (! js_IsIdentifier(s)) {
653            must_quote = true;
654        }        }
655        else {        else {
656            must_quote = false;
657          }
658          if (must_quote) {
659          Stream_write_char(f, '[');          Stream_write_char(f, '[');
660          print_quoted_string_atom(node->pn_atom, f);          print_quoted_string_atom(node->pn_atom, f);
661          Stream_write_char(f, ']');          Stream_write_char(f, ']');
662        }        }
663          else {
664            Stream_write_char(f, '.');
665            print_string_atom(node->pn_atom, f);
666          }
667      }      }
668      break;      break;
669    case TOK_LB:    case TOK_LB:
670      instrument_expression(node->pn_left, f);      output_expression(node->pn_left, f, false);
671      Stream_write_char(f, '[');      Stream_write_char(f, '[');
672      instrument_expression(node->pn_right, f);      output_expression(node->pn_right, f, false);
673      Stream_write_char(f, ']');      Stream_write_char(f, ']');
674      break;      break;
675    case TOK_LP:    case TOK_LP:
# Line 485  Line 683 
683        }        }
684        /* TOK_COMMA is a special case: a hole in the array */        /* TOK_COMMA is a special case: a hole in the array */
685        if (p->pn_type != TOK_COMMA) {        if (p->pn_type != TOK_COMMA) {
686          instrument_expression(p, f);          output_expression(p, f, false);
687        }        }
688      }      }
689      if (node->pn_extra == PNX_ENDCOMMA) {      if (node->pn_extra == PNX_ENDCOMMA) {
# Line 494  Line 692 
692      Stream_write_char(f, ']');      Stream_write_char(f, ']');
693      break;      break;
694    case TOK_RC:    case TOK_RC:
695        if (parenthesize_object_literals) {
696          Stream_write_char(f, '(');
697        }
698      Stream_write_char(f, '{');      Stream_write_char(f, '{');
699      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) {
700        assert(p->pn_type == TOK_COLON);        if (p->pn_type != TOK_COLON) {
701            fatal_source(file_id, p->pn_pos.begin.lineno, "unsupported node type (%d)", p->pn_type);
702          }
703        if (p != node->pn_head) {        if (p != node->pn_head) {
704          Stream_write_string(f, ", ");          Stream_write_string(f, ", ");
705        }        }
# Line 504  Line 707 
707        /* check whether this is a getter or setter */        /* check whether this is a getter or setter */
708        switch (p->pn_op) {        switch (p->pn_op) {
709        case JSOP_GETTER:        case JSOP_GETTER:
         Stream_write_string(f, "get ");  
         instrument_expression(p->pn_left, f);  
         instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);  
         break;  
710        case JSOP_SETTER:        case JSOP_SETTER:
711          Stream_write_string(f, "set ");          if (p->pn_op == JSOP_GETTER) {
712          instrument_expression(p->pn_left, f);            Stream_write_string(f, "get ");
713            }
714            else {
715              Stream_write_string(f, "set ");
716            }
717            output_expression(p->pn_left, f, false);
718            Stream_write_char(f, ' ');
719            if (p->pn_right->pn_type != TOK_FUNCTION) {
720              fatal_source(file_id, p->pn_pos.begin.lineno, "expected function");
721            }
722          instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);          instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);
723          break;          break;
724        default:        default:
725          instrument_expression(p->pn_left, f);          output_expression(p->pn_left, f, false);
726          Stream_write_string(f, ": ");          Stream_write_string(f, ": ");
727          instrument_expression(p->pn_right, f);          output_expression(p->pn_right, f, false);
728          break;          break;
729        }        }
730      }      }
731      Stream_write_char(f, '}');      Stream_write_char(f, '}');
732        if (parenthesize_object_literals) {
733          Stream_write_char(f, ')');
734        }
735      break;      break;
736    case TOK_RP:    case TOK_RP:
737      Stream_write_char(f, '(');      Stream_write_char(f, '(');
738      instrument_expression(node->pn_kid, f);      output_expression(node->pn_kid, f, false);
739      Stream_write_char(f, ')');      Stream_write_char(f, ')');
740      break;      break;
741    case TOK_NAME:    case TOK_NAME:
742      print_string_atom(node->pn_atom, f);      print_string_atom(node->pn_atom, f);
743        if (node->pn_expr != NULL) {
744          Stream_write_string(f, " = ");
745          output_expression(node->pn_expr, f, false);
746        }
747      break;      break;
748    case TOK_STRING:    case TOK_STRING:
749      print_quoted_string_atom(node->pn_atom, f);      print_quoted_string_atom(node->pn_atom, f);
750      break;      break;
751    case TOK_OBJECT:    case TOK_REGEXP:
752      switch (node->pn_op) {      assert(node->pn_op == JSOP_REGEXP);
753      case JSOP_OBJECT:      {
754        /* I assume this is JSOP_REGEXP */        JSObject * object = node->pn_pob->object;
755        abort();        jsval result;
756        break;        js_regexp_toString(context, object, &result);
757      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_regex(result, f);  
       }  
       break;  
     default:  
       abort();  
       break;  
758      }      }
759      break;      break;
760    case TOK_NUMBER:    case TOK_NUMBER:
# Line 561  Line 765 
765      To keep the output simple, special-case zero.      To keep the output simple, special-case zero.
766      */      */
767      if (node->pn_dval == 0.0) {      if (node->pn_dval == 0.0) {
768        Stream_write_string(f, "0");        if (signbit(node->pn_dval)) {
769            Stream_write_string(f, "-0");
770          }
771          else {
772            Stream_write_string(f, "0");
773          }
774        }
775        else if (node->pn_dval == INFINITY) {
776          Stream_write_string(f, "Number.POSITIVE_INFINITY");
777        }
778        else if (node->pn_dval == -INFINITY) {
779          Stream_write_string(f, "Number.NEGATIVE_INFINITY");
780        }
781        else if (isnan(node->pn_dval)) {
782          Stream_write_string(f, "Number.NaN");
783      }      }
784      else {      else {
785        Stream_printf(f, "%.15g", node->pn_dval);        Stream_printf(f, "%.15g", node->pn_dval);
# Line 587  Line 805 
805      }      }
806      break;      break;
807    case TOK_INSTANCEOF:    case TOK_INSTANCEOF:
808      instrument_expression(node->pn_left, f);      output_expression(node->pn_left, f, parenthesize_object_literals);
809      Stream_write_string(f, " instanceof ");      Stream_write_string(f, " instanceof ");
810      instrument_expression(node->pn_right, f);      output_expression(node->pn_right, f, false);
811      break;      break;
812    case TOK_IN:    case TOK_IN:
813      instrument_expression(node->pn_left, f);      output_expression(node->pn_left, f, false);
814      Stream_write_string(f, " in ");      Stream_write_string(f, " in ");
815      instrument_expression(node->pn_right, f);      output_expression(node->pn_right, f, false);
816      break;      break;
817    default:    case TOK_LEXICALSCOPE:
818      fatal("unsupported node type in file %s: %d", file_id, node->pn_type);      assert(node->pn_arity == PN_NAME);
819    }      assert(node->pn_expr->pn_type == TOK_LET);
820  }      assert(node->pn_expr->pn_arity == PN_BINARY);
821        Stream_write_string(f, "let(");
822  static void instrument_var_statement(JSParseNode * node, Stream * f, int indent) {      assert(node->pn_expr->pn_left->pn_type == TOK_LP);
823    assert(node->pn_arity == PN_LIST);      assert(node->pn_expr->pn_left->pn_arity == PN_LIST);
824    Stream_printf(f, "%*s", indent, "");      instrument_declarations(node->pn_expr->pn_left, f);
825    Stream_write_string(f, "var ");      Stream_write_string(f, ") ");
826    for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) {      output_expression(node->pn_expr->pn_right, f, true);
827      assert(p->pn_type == TOK_NAME);      break;
828      assert(p->pn_arity == PN_NAME);    case TOK_YIELD:
829      if (p != node->pn_head) {      assert(node->pn_arity == PN_UNARY);
830        Stream_write_string(f, ", ");      Stream_write_string(f, "yield");
831        if (node->pn_kid != NULL) {
832          Stream_write_char(f, ' ');
833          output_expression(node->pn_kid, f, true);
834      }      }
835      print_string_atom(p->pn_atom, f);      break;
836      if (p->pn_expr != NULL) {    case TOK_ARRAYCOMP:
837        Stream_write_string(f, " = ");      assert(node->pn_arity == PN_LIST);
838        instrument_expression(p->pn_expr, f);      {
839          JSParseNode * block_node;
840          switch (node->pn_count) {
841          case 1:
842            block_node = node->pn_head;
843            break;
844          case 2:
845            block_node = node->pn_head->pn_next;
846            break;
847          default:
848            abort();
849            break;
850          }
851          Stream_write_char(f, '[');
852          output_array_comprehension_or_generator_expression(block_node, f);
853          Stream_write_char(f, ']');
854      }      }
855        break;
856      case TOK_VAR:
857        assert(node->pn_arity == PN_LIST);
858        Stream_write_string(f, "var ");
859        instrument_declarations(node, f);
860        break;
861      case TOK_LET:
862        assert(node->pn_arity == PN_LIST);
863        Stream_write_string(f, "let ");
864        instrument_declarations(node, f);
865        break;
866      default:
867        fatal_source(file_id, node->pn_pos.begin.lineno, "unsupported node type (%d)", node->pn_type);
868    }    }
869  }  }
870    
871  static void output_statement(JSParseNode * node, Stream * f, int indent) {  static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
872    switch (node->pn_type) {    switch (node->pn_type) {
873    case TOK_FUNCTION:    case TOK_FUNCTION:
874      instrument_function(node, f, indent, FUNCTION_NORMAL);      instrument_function(node, f, indent, FUNCTION_NORMAL);
875        Stream_write_char(f, '\n');
876      break;      break;
877    case TOK_LC:    case TOK_LC:
878      assert(node->pn_arity == PN_LIST);      assert(node->pn_arity == PN_LIST);
# Line 630  Line 880 
880      Stream_write_string(f, "{\n");      Stream_write_string(f, "{\n");
881  */  */
882      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) {
883        instrument_statement(p, f, indent);        instrument_statement(p, f, indent, false);
884      }      }
885  /*  /*
886      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 638  Line 888 
888  */  */
889      break;      break;
890    case TOK_IF:    case TOK_IF:
891      {
892      assert(node->pn_arity == PN_TERNARY);      assert(node->pn_arity == PN_TERNARY);
893    
894        uint16_t line = node->pn_pos.begin.lineno;
895        if (! is_jscoverage_if) {
896          if (line > num_lines) {
897            fatal("file %s contains more than 65,535 lines", file_id);
898          }
899          if (line >= 2 && exclusive_directives[line - 2]) {
900            is_jscoverage_if = true;
901          }
902        }
903    
904      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
905      Stream_write_string(f, "if (");      Stream_write_string(f, "if (");
906      instrument_expression(node->pn_kid1, f);      output_expression(node->pn_kid1, f, false);
907      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
908      instrument_statement(node->pn_kid2, f, indent + 2);      if (is_jscoverage_if && node->pn_kid3) {
909          uint16_t else_start = node->pn_kid3->pn_pos.begin.lineno;
910          uint16_t else_end = node->pn_kid3->pn_pos.end.lineno + 1;
911          Stream_printf(f, "%*s", indent + 2, "");
912          Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, else_start, else_end);
913        }
914        instrument_statement(node->pn_kid2, f, indent + 2, false);
915      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
916      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
917      if (node->pn_kid3) {  
918        if (node->pn_kid3 || is_jscoverage_if) {
919        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
920        Stream_write_string(f, "else {\n");        Stream_write_string(f, "else {\n");
921        instrument_statement(node->pn_kid3, f, indent + 2);  
922          if (is_jscoverage_if) {
923            uint16_t if_start = node->pn_kid2->pn_pos.begin.lineno + 1;
924            uint16_t if_end = node->pn_kid2->pn_pos.end.lineno + 1;
925            Stream_printf(f, "%*s", indent + 2, "");
926            Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_start, if_end);
927          }
928    
929          if (node->pn_kid3) {
930            instrument_statement(node->pn_kid3, f, indent + 2, is_jscoverage_if);
931          }
932    
933        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
934        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
935      }      }
936    
937      break;      break;
938      }
939    case TOK_SWITCH:    case TOK_SWITCH:
940      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
941      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
942      Stream_write_string(f, "switch (");      Stream_write_string(f, "switch (");
943      instrument_expression(node->pn_left, f);      output_expression(node->pn_left, f, false);
944      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
945      for (struct JSParseNode * p = node->pn_right->pn_head; p != NULL; p = p->pn_next) {      {
946        Stream_printf(f, "%*s", indent, "");        JSParseNode * list = node->pn_right;
947        switch (p->pn_type) {        if (list->pn_type == TOK_LEXICALSCOPE) {
948        case TOK_CASE:          list = list->pn_expr;
949          Stream_write_string(f, "case ");        }
950          instrument_expression(p->pn_left, f);        for (struct JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
951          Stream_write_string(f, ":\n");          Stream_printf(f, "%*s", indent, "");
952          break;          switch (p->pn_type) {
953        case TOK_DEFAULT:          case TOK_CASE:
954          Stream_write_string(f, "default:\n");            Stream_write_string(f, "case ");
955          break;            output_expression(p->pn_left, f, false);
956        default:            Stream_write_string(f, ":\n");
957          abort();            break;
958          break;          case TOK_DEFAULT:
959              Stream_write_string(f, "default:\n");
960              break;
961            default:
962              abort();
963              break;
964            }
965            instrument_statement(p->pn_right, f, indent + 2, false);
966        }        }
       instrument_statement(p->pn_right, f, indent + 2);  
967      }      }
968      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
969      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
# Line 688  Line 976 
976      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
977      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
978      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
979      instrument_expression(node->pn_left, f);      output_expression(node->pn_left, f, false);
980      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
981      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
982      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
983      break;      break;
984    case TOK_DO:    case TOK_DO:
985      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
986      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
987      Stream_write_string(f, "do {\n");      Stream_write_string(f, "do {\n");
988      instrument_statement(node->pn_left, f, indent + 2);      instrument_statement(node->pn_left, f, indent + 2, false);
989      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
990      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
991      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
992      instrument_expression(node->pn_right, f);      output_expression(node->pn_right, f, false);
993      Stream_write_string(f, ");\n");      Stream_write_string(f, ");\n");
994      break;      break;
995    case TOK_FOR:    case TOK_FOR:
996      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
997      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
     Stream_write_string(f, "for (");  
998      switch (node->pn_left->pn_type) {      switch (node->pn_left->pn_type) {
999      case TOK_IN:      case TOK_IN:
1000        /* for/in */        /* for/in */
1001        assert(node->pn_left->pn_arity == PN_BINARY);        assert(node->pn_left->pn_arity == PN_BINARY);
1002        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;  
 */  
       }  
       Stream_write_string(f, " in ");  
       instrument_expression(node->pn_left->pn_right, f);  
1003        break;        break;
1004      case TOK_RESERVED:      case TOK_FORHEAD:
1005        /* for (;;) */        /* for (;;) */
1006        assert(node->pn_left->pn_arity == PN_TERNARY);        assert(node->pn_left->pn_arity == PN_TERNARY);
1007          Stream_write_string(f, "for (");
1008        if (node->pn_left->pn_kid1) {        if (node->pn_left->pn_kid1) {
1009          if (node->pn_left->pn_kid1->pn_type == TOK_VAR) {          output_expression(node->pn_left->pn_kid1, f, false);
           instrument_var_statement(node->pn_left->pn_kid1, f, 0);  
         }  
         else {  
           instrument_expression(node->pn_left->pn_kid1, f);  
         }  
1010        }        }
1011        Stream_write_string(f, ";");        Stream_write_string(f, ";");
1012        if (node->pn_left->pn_kid2) {        if (node->pn_left->pn_kid2) {
1013          Stream_write_char(f, ' ');          Stream_write_char(f, ' ');
1014          instrument_expression(node->pn_left->pn_kid2, f);          output_expression(node->pn_left->pn_kid2, f, false);
1015        }        }
1016        Stream_write_string(f, ";");        Stream_write_string(f, ";");
1017        if (node->pn_left->pn_kid3) {        if (node->pn_left->pn_kid3) {
1018          Stream_write_char(f, ' ');          Stream_write_char(f, ' ');
1019          instrument_expression(node->pn_left->pn_kid3, f);          output_expression(node->pn_left->pn_kid3, f, false);
1020        }        }
1021          Stream_write_char(f, ')');
1022        break;        break;
1023      default:      default:
1024        abort();        abort();
1025        break;        break;
1026      }      }
1027      Stream_write_string(f, ") {\n");      Stream_write_string(f, " {\n");
1028      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
1029      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
1030      break;      break;
1031    case TOK_THROW:    case TOK_THROW:
1032      assert(node->pn_arity == PN_UNARY);      assert(node->pn_arity == PN_UNARY);
1033      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
1034      Stream_write_string(f, "throw ");      Stream_write_string(f, "throw ");
1035      instrument_expression(node->pn_u.unary.kid, f);      output_expression(node->pn_u.unary.kid, f, false);
1036      Stream_write_string(f, ";\n");      Stream_write_string(f, ";\n");
1037      break;      break;
1038    case TOK_TRY:    case TOK_TRY:
1039      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
1040      Stream_write_string(f, "try {\n");      Stream_write_string(f, "try {\n");
1041      instrument_statement(node->pn_kid1, f, indent + 2);      instrument_statement(node->pn_kid1, f, indent + 2, false);
1042      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
1043      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
1044      {      if (node->pn_kid2) {
1045        for (JSParseNode * catch = node->pn_kid2; catch != NULL; catch = catch->pn_kid2) {        assert(node->pn_kid2->pn_type == TOK_RESERVED);
1046          assert(catch->pn_type == TOK_CATCH);        for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) {
1047            assert(scope->pn_type == TOK_LEXICALSCOPE);
1048            JSParseNode * catch_node = scope->pn_expr;
1049            assert(catch_node->pn_type == TOK_CATCH);
1050          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
1051          Stream_write_string(f, "catch (");          Stream_write_string(f, "catch (");
1052          assert(catch->pn_kid1->pn_arity == PN_NAME);          output_expression(catch_node->pn_kid1, f, false);
1053          print_string_atom(catch->pn_kid1->pn_atom, f);          if (catch_node->pn_kid2) {
         if (catch->pn_kid1->pn_expr) {  
1054            Stream_write_string(f, " if ");            Stream_write_string(f, " if ");
1055            instrument_expression(catch->pn_kid1->pn_expr, f);            output_expression(catch_node->pn_kid2, f, false);
1056          }          }
1057          Stream_write_string(f, ") {\n");          Stream_write_string(f, ") {\n");
1058          instrument_statement(catch->pn_kid3, f, indent + 2);          instrument_statement(catch_node->pn_kid3, f, indent + 2, false);
1059          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
1060          Stream_write_string(f, "}\n");          Stream_write_string(f, "}\n");
1061        }        }
# Line 796  Line 1063 
1063      if (node->pn_kid3) {      if (node->pn_kid3) {
1064        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
1065        Stream_write_string(f, "finally {\n");        Stream_write_string(f, "finally {\n");
1066        instrument_statement(node->pn_kid3, f, indent + 2);        instrument_statement(node->pn_kid3, f, indent + 2, false);
1067        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
1068        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
1069      }      }
# Line 809  Line 1076 
1076      assert(node->pn_arity == PN_NAME || node->pn_arity == PN_NULLARY);      assert(node->pn_arity == PN_NAME || node->pn_arity == PN_NULLARY);
1077      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
1078      Stream_write_string(f, node->pn_type == TOK_BREAK? "break": "continue");      Stream_write_string(f, node->pn_type == TOK_BREAK? "break": "continue");
1079      JSAtom * atom = node->pn_u.name.atom;      if (node->pn_atom != NULL) {
     if (atom != NULL) {  
1080        Stream_write_char(f, ' ');        Stream_write_char(f, ' ');
1081        print_string_atom(node->pn_atom, f);        print_string_atom(node->pn_atom, f);
1082      }      }
# Line 820  Line 1086 
1086      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
1087      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
1088      Stream_write_string(f, "with (");      Stream_write_string(f, "with (");
1089      instrument_expression(node->pn_left, f);      output_expression(node->pn_left, f, false);
1090      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
1091      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
1092      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
1093      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
1094      break;      break;
1095    case TOK_VAR:    case TOK_VAR:
1096      instrument_var_statement(node, f, indent);      Stream_printf(f, "%*s", indent, "");
1097        output_expression(node, f, false);
1098      Stream_write_string(f, ";\n");      Stream_write_string(f, ";\n");
1099      break;      break;
1100    case TOK_RETURN:    case TOK_RETURN:
# Line 836  Line 1103 
1103      Stream_write_string(f, "return");      Stream_write_string(f, "return");
1104      if (node->pn_kid != NULL) {      if (node->pn_kid != NULL) {
1105        Stream_write_char(f, ' ');        Stream_write_char(f, ' ');
1106        instrument_expression(node->pn_kid, f);        output_expression(node->pn_kid, f, true);
1107      }      }
1108      Stream_write_string(f, ";\n");      Stream_write_string(f, ";\n");
1109      break;      break;
# Line 844  Line 1111 
1111      assert(node->pn_arity == PN_UNARY);      assert(node->pn_arity == PN_UNARY);
1112      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
1113      if (node->pn_kid != NULL) {      if (node->pn_kid != NULL) {
1114        instrument_expression(node->pn_kid, f);        output_expression(node->pn_kid, f, true);
1115      }      }
1116      Stream_write_string(f, ";\n");      Stream_write_string(f, ";\n");
1117      break;      break;
1118    case TOK_COLON:    case TOK_COLON:
1119      {
1120      assert(node->pn_arity == PN_NAME);      assert(node->pn_arity == PN_NAME);
     /*  
     This one is tricky: can't output instrumentation between the label and the  
     statement it's supposed to label ...  
     */  
1121      Stream_printf(f, "%*s", indent < 2? 0: indent - 2, "");      Stream_printf(f, "%*s", indent < 2? 0: indent - 2, "");
1122      print_string_atom(node->pn_atom, f);      print_string_atom(node->pn_atom, f);
1123      Stream_write_string(f, ":\n");      Stream_write_string(f, ":\n");
1124        JSParseNode * labelled = node->pn_expr;
1125        if (labelled->pn_type == TOK_LEXICALSCOPE) {
1126          labelled = labelled->pn_expr;
1127        }
1128        if (labelled->pn_type == TOK_LC) {
1129          /* labelled block */
1130          Stream_printf(f, "%*s", indent, "");
1131          Stream_write_string(f, "{\n");
1132          instrument_statement(labelled, f, indent + 2, false);
1133          Stream_printf(f, "%*s", indent, "");
1134          Stream_write_string(f, "}\n");
1135        }
1136        else {
1137          /*
1138          This one is tricky: can't output instrumentation between the label and the
1139          statement it's supposed to label, so use output_statement instead of
1140          instrument_statement.
1141          */
1142          output_statement(labelled, f, indent, false);
1143        }
1144        break;
1145      }
1146      case TOK_LEXICALSCOPE:
1147        /* let statement */
1148        assert(node->pn_arity == PN_NAME);
1149        switch (node->pn_expr->pn_type) {
1150        case TOK_LET:
1151          /* let statement */
1152          assert(node->pn_expr->pn_arity == PN_BINARY);
1153          instrument_statement(node->pn_expr, f, indent, false);
1154          break;
1155        case TOK_LC:
1156          /* block */
1157          Stream_printf(f, "%*s", indent, "");
1158          Stream_write_string(f, "{\n");
1159          instrument_statement(node->pn_expr, f, indent + 2, false);
1160          Stream_printf(f, "%*s", indent, "");
1161          Stream_write_string(f, "}\n");
1162          break;
1163        case TOK_FOR:
1164          instrument_statement(node->pn_expr, f, indent, false);
1165          break;
1166        default:
1167          abort();
1168          break;
1169        }
1170        break;
1171      case TOK_LET:
1172        switch (node->pn_arity) {
1173        case PN_BINARY:
1174          /* let statement */
1175          Stream_printf(f, "%*s", indent, "");
1176          Stream_write_string(f, "let (");
1177          assert(node->pn_left->pn_type == TOK_LP);
1178          assert(node->pn_left->pn_arity == PN_LIST);
1179          instrument_declarations(node->pn_left, f);
1180          Stream_write_string(f, ") {\n");
1181          instrument_statement(node->pn_right, f, indent + 2, false);
1182          Stream_printf(f, "%*s", indent, "");
1183          Stream_write_string(f, "}\n");
1184          break;
1185        case PN_LIST:
1186          /* let definition */
1187          Stream_printf(f, "%*s", indent, "");
1188          output_expression(node, f, false);
1189          Stream_write_string(f, ";\n");
1190          break;
1191        default:
1192          abort();
1193          break;
1194        }
1195        break;
1196      case TOK_DEBUGGER:
1197        Stream_printf(f, "%*s", indent, "");
1198        Stream_write_string(f, "debugger;\n");
1199        break;
1200      case TOK_SEQ:
1201      /*      /*
1202      ... use output_statement instead of instrument_statement.      This occurs with the statement:
1203        for (var a = b in c) {}
1204      */      */
1205      output_statement(node->pn_expr, f, indent);      assert(node->pn_arity == PN_LIST);
1206        for (JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) {
1207          instrument_statement(p, f, indent, false);
1208        }
1209      break;      break;
1210    default:    default:
1211      fatal("unsupported node type in file %s: %d", file_id, node->pn_type);      fatal_source(file_id, node->pn_pos.begin.lineno, "unsupported node type (%d)", node->pn_type);
1212    }    }
1213  }  }
1214    
# Line 872  Line 1217 
1217  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
1218  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
1219  */  */
1220  static void instrument_statement(JSParseNode * node, Stream * f, int indent) {  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
1221    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC && node->pn_type != TOK_LEXICALSCOPE) {
1222      int line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
1223        if (line > num_lines) {
1224          fatal("file %s contains more than 65,535 lines", file_id);
1225        }
1226    
1227      /* the root node has line number 0 */      /* the root node has line number 0 */
1228      if (line != 0) {      if (line != 0) {
1229        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
# Line 882  Line 1231 
1231        lines[line - 1] = 1;        lines[line - 1] = 1;
1232      }      }
1233    }    }
1234    output_statement(node, f, indent);    output_statement(node, f, indent, is_jscoverage_if);
1235  }  }
1236    
1237  void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) {  static bool characters_start_with(const jschar * characters, size_t line_start, size_t line_end, const char * prefix) {
1238    file_id = id;    const jschar * characters_end = characters + line_end;
1239      const jschar * cp = characters + line_start;
1240      const char * bp = prefix;
1241      for (;;) {
1242        if (*bp == '\0') {
1243          return true;
1244        }
1245        else if (cp == characters_end) {
1246          return false;
1247        }
1248        else if (*cp != *bp) {
1249          return false;
1250        }
1251        bp++;
1252        cp++;
1253      }
1254    }
1255    
1256    /* scan the javascript */  static bool characters_are_white_space(const jschar * characters, size_t line_start, size_t line_end) {
1257    JSTokenStream * token_stream = js_NewTokenStream(context, characters, num_characters, NULL, 1, NULL);    /* XXX - other Unicode space */
1258    if (token_stream == NULL) {    const jschar * end = characters + line_end;
1259      fatal("cannot create token stream from file: %s", file_id);    for (const jschar * p = characters + line_start; p < end; p++) {
1260        jschar c = *p;
1261        if (c == 0x9 || c == 0xB || c == 0xC || c == 0x20 || c == 0xA0) {
1262          continue;
1263        }
1264        else {
1265          return false;
1266        }
1267    }    }
1268      return true;
1269    }
1270    
1271    static void error_reporter(JSContext * context, const char * message, JSErrorReport * report) {
1272      warn_source(file_id, report->lineno, "%s", message);
1273    }
1274    
1275    void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) {
1276      file_id = id;
1277    
1278    /* parse the javascript */    /* parse the javascript */
1279    JSParseNode * node = js_ParseTokenStream(context, global, token_stream);    JSParseContext parse_context;
1280      if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) {
1281        fatal("cannot create token stream from file %s", file_id);
1282      }
1283      JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);
1284      JSParseNode * node = js_ParseScript(context, global, &parse_context);
1285    if (node == NULL) {    if (node == NULL) {
1286      fatal("parse error in file: %s", file_id);      js_ReportUncaughtException(context);
1287        fatal("parse error in file %s", file_id);
1288    }    }
1289    int num_lines = node->pn_pos.end.lineno;    JS_SetErrorReporter(context, old_error_reporter);
1290    lines = xmalloc(num_lines);    num_lines = node->pn_pos.end.lineno;
1291    for (int i = 0; i < num_lines; i++) {    lines = (char *) xmalloc(num_lines);
1292      for (unsigned int i = 0; i < num_lines; i++) {
1293      lines[i] = 0;      lines[i] = 0;
1294    }    }
1295    
1296      /* search code for conditionals */
1297      exclusive_directives = xnew(bool, num_lines);
1298      for (unsigned int i = 0; i < num_lines; i++) {
1299        exclusive_directives[i] = false;
1300      }
1301    
1302      bool has_conditionals = false;
1303      struct IfDirective * if_directives = NULL;
1304      size_t line_number = 0;
1305      size_t i = 0;
1306      while (i < num_characters) {
1307        if (line_number == UINT16_MAX) {
1308          fatal("file %s contains more than 65,535 lines", file_id);
1309        }
1310        line_number++;
1311        size_t line_start = i;
1312        jschar c;
1313        bool done = false;
1314        while (! done && i < num_characters) {
1315          c = characters[i];
1316          switch (c) {
1317          case '\r':
1318          case '\n':
1319          case 0x2028:
1320          case 0x2029:
1321            done = true;
1322            break;
1323          default:
1324            i++;
1325          }
1326        }
1327        size_t line_end = i;
1328        if (i < num_characters) {
1329          i++;
1330          if (c == '\r' && i < num_characters && characters[i] == '\n') {
1331            i++;
1332          }
1333        }
1334    
1335        if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) {
1336          has_conditionals = true;
1337    
1338          if (characters_are_white_space(characters, line_start + 16, line_end)) {
1339            exclusive_directives[line_number - 1] = true;
1340          }
1341          else {
1342            struct IfDirective * if_directive = xnew(struct IfDirective, 1);
1343            if_directive->condition_start = characters + line_start + 16;
1344            if_directive->condition_end = characters + line_end;
1345            if_directive->start_line = line_number;
1346            if_directive->end_line = 0;
1347            if_directive->next = if_directives;
1348            if_directives = if_directive;
1349          }
1350        }
1351        else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) {
1352          for (struct IfDirective * p = if_directives; p != NULL; p = p->next) {
1353            if (p->end_line == 0) {
1354              p->end_line = line_number;
1355              break;
1356            }
1357          }
1358        }
1359      }
1360    
1361    /*    /*
1362    An instrumented JavaScript file has 3 sections:    An instrumented JavaScript file has 4 sections:
1363    1. initialization    1. initialization
1364    2. instrumented source code    2. instrumented source code
1365    3. original source code    3. conditionals
1366      4. original source code
1367    */    */
1368    
1369    Stream * instrumented = Stream_new(0);    Stream * instrumented = Stream_new(0);
1370    instrument_statement(node, instrumented, 0);    instrument_statement(node, instrumented, 0, false);
1371      js_FinishParseContext(context, &parse_context);
1372    
1373    /* write line number info to the output */    /* write line number info to the output */
1374    Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");    Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");
1375    Stream_write_string(output, "if (! top._$jscoverage) {\n  top._$jscoverage = {};\n}\n");    if (jscoverage_mozilla) {
1376    Stream_write_string(output, "var _$jscoverage = top._$jscoverage;\n");      Stream_write_string(output, "try {\n");
1377        Stream_write_string(output, "  Components.utils.import('resource://gre/modules/jscoverage.jsm');\n");
1378        Stream_printf(output, "  dump('%s: successfully imported jscoverage module\\n');\n", id);
1379        Stream_write_string(output, "}\n");
1380        Stream_write_string(output, "catch (e) {\n");
1381        Stream_write_string(output, "  _$jscoverage = {};\n");
1382        Stream_printf(output, "  dump('%s: failed to import jscoverage module - coverage not available for this file\\n');\n", id);
1383        Stream_write_string(output, "}\n");
1384      }
1385      else {
1386        Stream_write_string(output, "if (! top._$jscoverage) {\n  top._$jscoverage = {};\n}\n");
1387        Stream_write_string(output, "var _$jscoverage = top._$jscoverage;\n");
1388      }
1389    Stream_printf(output, "if (! _$jscoverage['%s']) {\n", file_id);    Stream_printf(output, "if (! _$jscoverage['%s']) {\n", file_id);
1390    Stream_printf(output, "  _$jscoverage['%s'] = [];\n", file_id);    Stream_printf(output, "  _$jscoverage['%s'] = [];\n", file_id);
1391    for (int i = 0; i < num_lines; i++) {    for (int i = 0; i < num_lines; i++) {
# Line 929  Line 1396 
1396    Stream_write_string(output, "}\n");    Stream_write_string(output, "}\n");
1397    free(lines);    free(lines);
1398    lines = NULL;    lines = NULL;
1399      free(exclusive_directives);
1400      exclusive_directives = NULL;
1401    
1402      /* conditionals */
1403      if (has_conditionals) {
1404        Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);
1405      }
1406    
1407    /* copy the instrumented source code to the output */    /* copy the instrumented source code to the output */
1408    Stream_write(output, instrumented->data, instrumented->length);    Stream_write(output, instrumented->data, instrumented->length);
1409    
1410    /* conditionals */    /* conditionals */
1411    bool has_conditionals = false;    for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) {
1412    size_t line_number = 0;      Stream_write_string(output, "if (!(");
1413    size_t i = 0;      print_javascript(if_directive->condition_start, if_directive->condition_end - if_directive->condition_start, output);
1414    while (i < num_characters) {      Stream_write_string(output, ")) {\n");
1415      line_number++;      Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line);
1416      size_t line_start = i;      Stream_write_string(output, "}\n");
1417      /* FIXME */    }
1418      while (i < num_characters && characters[i] != '\r' && characters[i] != '\n') {  
1419        i++;    /* free */
1420      }    while (if_directives != NULL) {
1421      size_t line_end = i;      struct IfDirective * if_directive = if_directives;
1422      if (i < num_characters) {      if_directives = if_directives->next;
1423        if (characters[i] == '\r') {      free(if_directive);
         line_end = i;  
         i++;  
         if (i < num_characters && characters[i] == '\n') {  
           i++;  
         }  
       }  
       else if (characters[i] == '\n') {  
         line_end = i;  
         i++;  
       }  
       else {  
         abort();  
       }  
     }  
     char * line = js_DeflateString(context, characters + line_start, line_end - line_start);  
     if (str_starts_with(line, "//#JSCOVERAGE_IF")) {  
       if (! has_conditionals) {  
         has_conditionals = true;  
         Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);  
       }  
       Stream_printf(output, "if (!%s) {\n", line + 16);  
       Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = ", file_id, line_number);  
     }  
     else if (str_starts_with(line, "//#JSCOVERAGE_ENDIF")) {  
       Stream_printf(output, "%d;\n", line_number);  
       Stream_printf(output, "}\n");  
     }  
     JS_free(context, line);  
1424    }    }
1425    
1426    /* copy the original source to the output */    /* copy the original source to the output */
# Line 1015  Line 1461 
1461            /* line feed (new line) */            /* line feed (new line) */
1462            done = true;            done = true;
1463            break;            break;
1464            /* IE doesn't support this */
1465            /*
1466          case 0xb:          case 0xb:
           /* vertical tab */  
1467            Stream_write_string(output, "\\v");            Stream_write_string(output, "\\v");
1468            break;            break;
1469            */
1470          case 0xc:          case 0xc:
1471            /* form feed */            /* form feed */
1472            Stream_write_string(output, "\\f");            Stream_write_string(output, "\\f");
# Line 1073  Line 1521 
1521            /* line feed (new line) */            /* line feed (new line) */
1522            done = true;            done = true;
1523            break;            break;
1524            /* IE doesn't support this */
1525            /*
1526          case 0xb:          case 0xb:
           /* vertical tab */  
1527            Stream_write_string(output, "\\v");            Stream_write_string(output, "\\v");
1528            break;            break;
1529            */
1530          case 0xc:          case 0xc:
1531            /* form feed */            /* form feed */
1532            Stream_write_string(output, "\\f");            Stream_write_string(output, "\\f");
# Line 1151  Line 1601 
1601  };  };
1602    
1603  static int compare_strings(const void * p1, const void * p2) {  static int compare_strings(const void * p1, const void * p2) {
1604    return strcmp(p1, p2) == 0;    return strcmp((const char *) p1, (const char *) p2) == 0;
1605  }  }
1606    
1607  Coverage * Coverage_new(void) {  Coverage * Coverage_new(void) {
1608    Coverage * result = xmalloc(sizeof(Coverage));    Coverage * result = (Coverage *) xmalloc(sizeof(Coverage));
1609    result->coverage_table = JS_NewHashTable(1024, JS_HashString, compare_strings, NULL, NULL, NULL);    result->coverage_table = JS_NewHashTable(1024, JS_HashString, compare_strings, NULL, NULL, NULL);
1610    if (result->coverage_table == NULL) {    if (result->coverage_table == NULL) {
1611      fatal("cannot create hash table");      fatal("cannot create hash table");
# Line 1190  Line 1640 
1640  };  };
1641    
1642  static intN enumerator(JSHashEntry * entry, intN i, void * arg) {  static intN enumerator(JSHashEntry * entry, intN i, void * arg) {
1643    struct EnumeratorArg * enumerator_arg = arg;    struct EnumeratorArg * enumerator_arg = (struct EnumeratorArg *) arg;
1644    enumerator_arg->f(entry->value, i, enumerator_arg->p);    enumerator_arg->f((FileCoverage *) entry->value, i, enumerator_arg->p);
1645    return 0;    return 0;
1646  }  }
1647    
# Line 1203  Line 1653 
1653  }  }
1654    
1655  int jscoverage_parse_json(Coverage * coverage, const uint8_t * json, size_t length) {  int jscoverage_parse_json(Coverage * coverage, const uint8_t * json, size_t length) {
1656      int result = 0;
1657    
1658    jschar * base = js_InflateString(context, (char *) json, &length);    jschar * base = js_InflateString(context, (char *) json, &length);
1659    if (base == NULL) {    if (base == NULL) {
1660      fatal("out of memory");      fatal("out of memory");
# Line 1215  Line 1667 
1667    
1668    JS_free(context, base);    JS_free(context, base);
1669    
1670    JSTokenStream * token_stream = js_NewTokenStream(context, parenthesized_json, length + 2, NULL, 1, NULL);    JSParseContext parse_context;
1671    if (token_stream == NULL) {    if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) {
1672      fatal("cannot create token stream");      free(parenthesized_json);
1673        return -1;
1674    }    }
1675      JSParseNode * root = js_ParseScript(context, global, &parse_context);
   JSParseNode * root = js_ParseTokenStream(context, global, token_stream);  
1676    free(parenthesized_json);    free(parenthesized_json);
1677    
1678      JSParseNode * semi = NULL;
1679      JSParseNode * object = NULL;
1680    
1681    if (root == NULL) {    if (root == NULL) {
1682      return -1;      result = -1;
1683        goto done;
1684    }    }
1685    
1686    /* root node must be TOK_LC */    /* root node must be TOK_LC */
1687    if (root->pn_type != TOK_LC) {    if (root->pn_type != TOK_LC) {
1688      return -1;      result = -1;
1689        goto done;
1690    }    }
1691    JSParseNode * semi = root->pn_u.list.head;    semi = root->pn_u.list.head;
1692    
1693    /* the list must be TOK_SEMI and it must contain only one element */    /* the list must be TOK_SEMI and it must contain only one element */
1694    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {
1695      return -1;      result = -1;
1696        goto done;
1697    }    }
1698    JSParseNode * parenthesized = semi->pn_kid;    object = semi->pn_kid;
   
   /* this must be a parenthesized expression */  
   if (parenthesized->pn_type != TOK_RP) {  
     return -1;  
   }  
   JSParseNode * object = parenthesized->pn_kid;  
1699    
1700    /* this must be an object literal */    /* this must be an object literal */
1701    if (object->pn_type != TOK_RC) {    if (object->pn_type != TOK_RC) {
1702      return -1;      result = -1;
1703        goto done;
1704    }    }
1705    
1706    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {
1707      /* every element of this list must be TOK_COLON */      /* every element of this list must be TOK_COLON */
1708      if (p->pn_type != TOK_COLON) {      if (p->pn_type != TOK_COLON) {
1709        return -1;        result = -1;
1710          goto done;
1711      }      }
1712    
1713      /* the key must be a string representing the file */      /* the key must be a string representing the file */
1714      JSParseNode * key = p->pn_left;      JSParseNode * key = p->pn_left;
1715      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {
1716        return -1;        result = -1;
1717          goto done;
1718      }      }
1719      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));
1720    
1721      /* the value must be an object literal OR an array */      /* the value must be an object literal OR an array */
1722      JSParseNode * value = p->pn_right;      JSParseNode * value = p->pn_right;
1723      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {
1724        return -1;        result = -1;
1725          goto done;
1726      }      }
1727    
1728      JSParseNode * array = NULL;      JSParseNode * array = NULL;
# Line 1277  Line 1734 
1734      else if (value->pn_type == TOK_RC) {      else if (value->pn_type == TOK_RC) {
1735        /* an object literal */        /* an object literal */
1736        if (value->pn_count != 2) {        if (value->pn_count != 2) {
1737          return -1;          result = -1;
1738            goto done;
1739        }        }
1740        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {
1741          if (element->pn_type != TOK_COLON) {          if (element->pn_type != TOK_COLON) {
1742            return -1;            result = -1;
1743              goto done;
1744          }          }
1745          JSParseNode * left = element->pn_left;          JSParseNode * left = element->pn_left;
1746          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {
1747            return -1;            result = -1;
1748              goto done;
1749          }          }
1750          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));
1751          if (strcmp(s, "coverage") == 0) {          if (strcmp(s, "coverage") == 0) {
1752            array = element->pn_right;            array = element->pn_right;
1753            if (array->pn_type != TOK_RB) {            if (array->pn_type != TOK_RB) {
1754              return -1;              result = -1;
1755                goto done;
1756            }            }
1757          }          }
1758          else if (strcmp(s, "source") == 0) {          else if (strcmp(s, "source") == 0) {
1759            source = element->pn_right;            source = element->pn_right;
1760            if (source->pn_type != TOK_RB) {            if (source->pn_type != TOK_RB) {
1761              return -1;              result = -1;
1762                goto done;
1763            }            }
1764          }          }
1765          else {          else {
1766            return -1;            result = -1;
1767              goto done;
1768          }          }
1769        }        }
1770      }      }
1771      else {      else {
1772        return -1;        result = -1;
1773          goto done;
1774      }      }
1775    
1776      if (array == NULL) {      if (array == NULL) {
1777        return -1;        result = -1;
1778          goto done;
1779      }      }
1780    
1781      /* look up the file in the coverage table */      /* look up the file in the coverage table */
1782      FileCoverage * file_coverage = JS_HashTableLookup(coverage->coverage_table, id_bytes);      FileCoverage * file_coverage = (FileCoverage *) JS_HashTableLookup(coverage->coverage_table, id_bytes);
1783      if (file_coverage == NULL) {      if (file_coverage == NULL) {
1784        /* not there: create a new one */        /* not there: create a new one */
1785        char * id = xstrdup(id_bytes);        char * id = xstrdup(id_bytes);
1786        file_coverage = xmalloc(sizeof(FileCoverage));        file_coverage = (FileCoverage *) xmalloc(sizeof(FileCoverage));
1787        file_coverage->id = id;        file_coverage->id = id;
1788        file_coverage->num_coverage_lines = array->pn_count;        file_coverage->num_coverage_lines = array->pn_count;
1789        file_coverage->coverage_lines = xnew(int, array->pn_count);        file_coverage->coverage_lines = xnew(int, array->pn_count);
1790        if (source == NULL) {        file_coverage->source_lines = NULL;
         file_coverage->source_lines = NULL;  
       }  
       else {  
         file_coverage->num_source_lines = source->pn_count;  
         file_coverage->source_lines = xnew(char *, source->pn_count);  
         uint32 i = 0;  
         for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {  
           if (element->pn_type != TOK_STRING) {  
             return -1;  
           }  
           file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));  
         }  
         assert(i == source->pn_count);  
       }  
1791    
1792        /* set coverage for all lines */        /* set coverage for all lines */
1793        uint32 i = 0;        uint32 i = 0;
# Line 1348  Line 1799 
1799            file_coverage->coverage_lines[i] = -1;            file_coverage->coverage_lines[i] = -1;
1800          }          }
1801          else {          else {
1802            return -1;            result = -1;
1803              goto done;
1804          }          }
1805        }        }
1806        assert(i == array->pn_count);        assert(i == array->pn_count);
1807    
1808        /* add to the hash table */        /* add to the hash table */
1809        JS_HashTableAdd(coverage->coverage_table, id, file_coverage);        JS_HashTableAdd(coverage->coverage_table, id, file_coverage);
1810        struct FileCoverageList * coverage_list = xmalloc(sizeof(struct FileCoverageList));        struct FileCoverageList * coverage_list = (FileCoverageList *) xmalloc(sizeof(struct FileCoverageList));
1811        coverage_list->file_coverage = file_coverage;        coverage_list->file_coverage = file_coverage;
1812        coverage_list->next = coverage->coverage_list;        coverage_list->next = coverage->coverage_list;
1813        coverage->coverage_list = coverage_list;        coverage->coverage_list = coverage_list;
# Line 1364  Line 1816 
1816        /* sanity check */        /* sanity check */
1817        assert(strcmp(file_coverage->id, id_bytes) == 0);        assert(strcmp(file_coverage->id, id_bytes) == 0);
1818        if (file_coverage->num_coverage_lines != array->pn_count) {        if (file_coverage->num_coverage_lines != array->pn_count) {
1819          return -2;          result = -2;
1820            goto done;
1821        }        }
1822    
1823        /* merge the coverage */        /* merge the coverage */
# Line 1372  Line 1825 
1825        for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {        for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {
1826          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1827            if (file_coverage->coverage_lines[i] == -1) {            if (file_coverage->coverage_lines[i] == -1) {
1828              return -2;              result = -2;
1829                goto done;
1830            }            }
1831            file_coverage->coverage_lines[i] += (int) element->pn_dval;            file_coverage->coverage_lines[i] += (int) element->pn_dval;
1832          }          }
1833          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1834            if (file_coverage->coverage_lines[i] != -1) {            if (file_coverage->coverage_lines[i] != -1) {
1835              return -2;              result = -2;
1836                goto done;
1837            }            }
1838          }          }
1839          else {          else {
1840            return -1;            result = -1;
1841              goto done;
1842          }          }
1843        }        }
1844        assert(i == array->pn_count);        assert(i == array->pn_count);
1845        }
1846    
1847        /* if this JSON file has source, use it */      /* if this JSON file has source, use it */
1848        if (file_coverage->source_lines == NULL && source != NULL) {      if (file_coverage->source_lines == NULL && source != NULL) {
1849          file_coverage->num_source_lines = source->pn_count;        file_coverage->num_source_lines = source->pn_count;
1850          file_coverage->source_lines = xnew(char *, source->pn_count);        file_coverage->source_lines = xnew(char *, source->pn_count);
1851          uint32 i = 0;        uint32 i = 0;
1852          for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {        for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {
1853            if (element->pn_type != TOK_STRING) {          if (element->pn_type != TOK_STRING) {
1854              return -1;            result = -1;
1855            }            goto done;
           file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));  
1856          }          }
1857          assert(i == source->pn_count);          file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));
1858        }        }
1859          assert(i == source->pn_count);
1860      }      }
1861    }    }
1862    
1863    return 0;  done:
1864      js_FinishParseContext(context, &parse_context);
1865      return result;
1866  }  }

Legend:
Removed from v.179  
changed lines
  Added in v.401

  ViewVC Help
Powered by ViewVC 1.1.24