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

Diff of /trunk/instrument-js.cpp

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

revision 190 by siliconforks, Tue Sep 23 03:49:01 2008 UTC revision 342 by siliconforks, Fri Oct 24 16:15:04 2008 UTC
# Line 26  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>
# Line 40  Line 43 
43  #include "resource-manager.h"  #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 50  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 62  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 77  Line 98 
98    JS_DestroyRuntime(runtime);    JS_DestroyRuntime(runtime);
99  }  }
100    
101    static void print_javascript(const jschar * characters, size_t num_characters, Stream * f) {
102      for (size_t i = 0; i < num_characters; i++) {
103        jschar c = characters[i];
104        /*
105        XXX does not handle no-break space, other unicode "space separator"
106        */
107        switch (c) {
108        case 0x9:
109        case 0xB:
110        case 0xC:
111          Stream_write_char(f, c);
112          break;
113        default:
114          if (32 <= c && c <= 126) {
115            Stream_write_char(f, c);
116          }
117          else {
118            Stream_printf(f, "\\u%04x", c);
119          }
120          break;
121        }
122      }
123    }
124    
125  static void print_string(JSString * s, Stream * f) {  static void print_string(JSString * s, Stream * f) {
126    size_t length = JSSTRING_LENGTH(s);    size_t length = JSSTRING_LENGTH(s);
127    jschar * characters = JSSTRING_CHARS(s);    jschar * characters = JSSTRING_CHARS(s);
# Line 111  Line 156 
156        case 0xa:        case 0xa:
157          Stream_write_string(f, "\\n");          Stream_write_string(f, "\\n");
158          break;          break;
159          /* IE doesn't support this */
160          /*
161        case 0xb:        case 0xb:
162          Stream_write_string(f, "\\v");          Stream_write_string(f, "\\v");
163          break;          break;
164          */
165        case 0xc:        case 0xc:
166          Stream_write_string(f, "\\f");          Stream_write_string(f, "\\f");
167          break;          break;
# Line 160  Line 208 
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 170  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 204  Line 256 
256  }  }
257    
258  static void instrument_expression(JSParseNode * node, Stream * f);  static void instrument_expression(JSParseNode * node, Stream * f);
259  static void instrument_statement(JSParseNode * node, Stream * f, int indent);  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
260    
261  enum FunctionType {  enum FunctionType {
262    FUNCTION_NORMAL,    FUNCTION_NORMAL,
# Line 212  Line 264 
264  };  };
265    
266  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) {
267      assert(node->pn_type == TOK_FUNCTION);
268    assert(node->pn_arity == PN_FUNC);    assert(node->pn_arity == PN_FUNC);
269    assert(ATOM_IS_OBJECT(node->pn_funAtom));    JSObject * object = node->pn_funpob->object;
   JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom);  
270    assert(JS_ObjectIsFunction(context, object));    assert(JS_ObjectIsFunction(context, object));
271    JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);    JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
272    assert(function);    assert(function);
273    assert(object == function->object);    assert(object == &function->object);
274    Stream_printf(f, "%*s", indent, "");    Stream_printf(f, "%*s", indent, "");
275    if (type == FUNCTION_NORMAL) {    if (type == FUNCTION_NORMAL) {
276      Stream_write_string(f, "function");      Stream_write_string(f, "function");
# Line 230  Line 282 
282      print_string_atom(function->atom, f);      print_string_atom(function->atom, f);
283    }    }
284    
285    /* function parameters */    /*
286      function parameters - see JS_DecompileFunction in jsapi.cpp, which calls
287      js_DecompileFunction in jsopcode.cpp
288      */
289    Stream_write_string(f, "(");    Stream_write_string(f, "(");
290    JSAtom ** params = xnew(JSAtom *, function->nargs);    JSArenaPool pool;
291    for (int i = 0; i < function->nargs; i++) {    JS_INIT_ARENA_POOL(&pool, "instrument_function", 256, 1, &context->scriptStackQuota);
292      /* initialize to NULL for sanity check */    jsuword * local_names = NULL;
293      params[i] = NULL;    if (JS_GET_LOCAL_NAME_COUNT(function)) {
294    }      local_names = js_GetLocalNameArray(context, function, &pool);
295    JSScope * scope = OBJ_SCOPE(object);      if (local_names == NULL) {
296    for (JSScopeProperty * scope_property = SCOPE_LAST_PROP(scope); scope_property != NULL; scope_property = scope_property->parent) {        fatal("out of memory");
     if (scope_property->getter != js_GetArgument) {  
       continue;  
297      }      }
     assert(scope_property->flags & SPROP_HAS_SHORTID);  
     assert((uint16) scope_property->shortid < function->nargs);  
     assert(JSID_IS_ATOM(scope_property->id));  
     params[(uint16) scope_property->shortid] = JSID_TO_ATOM(scope_property->id);  
298    }    }
299    for (int i = 0; i < function->nargs; i++) {    for (int i = 0; i < function->nargs; i++) {
     assert(params[i] != NULL);  
300      if (i > 0) {      if (i > 0) {
301        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
302      }      }
303      if (ATOM_IS_STRING(params[i])) {      JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]);
304        print_string_atom(params[i], f);      print_string_atom(param, f);
     }  
305    }    }
306      JS_FinishArenaPool(&pool);
307    Stream_write_string(f, ") {\n");    Stream_write_string(f, ") {\n");
   free(params);  
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    Stream_write_string(f, "}\n");    Stream_write_string(f, "}\n");
313  }  }
# Line 277  Line 324 
324    Stream_write_char(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  /*  /*
356  See <Expressions> in jsparse.h.  See <Expressions> in jsparse.h.
357  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
# Line 335  Line 410 
410      instrument_expression(node->pn_kid3, f);      instrument_expression(node->pn_kid3, f);
411      break;      break;
412    case TOK_OR:    case TOK_OR:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " || ");  
     instrument_expression(node->pn_right, f);  
     break;  
413    case TOK_AND:    case TOK_AND:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " && ");  
     instrument_expression(node->pn_right, f);  
     break;  
414    case TOK_BITOR:    case TOK_BITOR:
415    case TOK_BITXOR:    case TOK_BITXOR:
416    case TOK_BITAND:    case TOK_BITAND:
# Line 456  Line 523 
523      assert(ATOM_IS_STRING(node->pn_atom));      assert(ATOM_IS_STRING(node->pn_atom));
524      {      {
525        JSString * s = ATOM_TO_STRING(node->pn_atom);        JSString * s = ATOM_TO_STRING(node->pn_atom);
526        /* XXX - semantics changed in 1.7 */        bool is_keyword = (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF);
527        if (! ATOM_KEYWORD(node->pn_atom) && js_IsIdentifier(s)) {        if (! is_keyword && js_IsIdentifier(s)) {
528          Stream_write_char(f, '.');          Stream_write_char(f, '.');
529          print_string_atom(node->pn_atom, f);          print_string_atom(node->pn_atom, f);
530        }        }
# Line 504  Line 571 
571        /* check whether this is a getter or setter */        /* check whether this is a getter or setter */
572        switch (p->pn_op) {        switch (p->pn_op) {
573        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;  
574        case JSOP_SETTER:        case JSOP_SETTER:
575          Stream_write_string(f, "set ");          if (p->pn_op == JSOP_GETTER) {
576              Stream_write_string(f, "get ");
577            }
578            else {
579              Stream_write_string(f, "set ");
580            }
581          instrument_expression(p->pn_left, f);          instrument_expression(p->pn_left, f);
582            if (p->pn_right->pn_type != TOK_FUNCTION) {
583              fatal("parse error: expected function");
584            }
585          instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);          instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);
586          break;          break;
587        default:        default:
# Line 533  Line 604 
604    case TOK_STRING:    case TOK_STRING:
605      print_quoted_string_atom(node->pn_atom, f);      print_quoted_string_atom(node->pn_atom, f);
606      break;      break;
607    case TOK_OBJECT:    case TOK_REGEXP:
608      switch (node->pn_op) {      assert(node->pn_op == JSOP_REGEXP);
609      case JSOP_OBJECT:      {
610        /* I assume this is JSOP_REGEXP */        JSObject * object = node->pn_pob->object;
611        abort();        jsval result;
612        break;        js_regexp_toString(context, object, &result);
613      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;  
614      }      }
615      break;      break;
616    case TOK_NUMBER:    case TOK_NUMBER:
# Line 596  Line 656 
656      Stream_write_string(f, " in ");      Stream_write_string(f, " in ");
657      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
658      break;      break;
659      case TOK_LEXICALSCOPE:
660        assert(node->pn_arity == PN_NAME);
661        assert(node->pn_expr->pn_type == TOK_LET);
662        assert(node->pn_expr->pn_arity == PN_BINARY);
663        Stream_write_string(f, "let(");
664        assert(node->pn_expr->pn_left->pn_type == TOK_LP);
665        assert(node->pn_expr->pn_left->pn_arity == PN_LIST);
666        instrument_declarations(node->pn_expr->pn_left, f);
667        Stream_write_string(f, ") ");
668        instrument_expression(node->pn_expr->pn_right, f);
669        break;
670      case TOK_YIELD:
671        assert(node->pn_arity == PN_UNARY);
672        Stream_write_string(f, "yield ");
673        instrument_expression(node->pn_kid, f);
674        break;
675    default:    default:
676      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);
677    }    }
# Line 605  Line 681 
681    assert(node->pn_arity == PN_LIST);    assert(node->pn_arity == PN_LIST);
682    Stream_printf(f, "%*s", indent, "");    Stream_printf(f, "%*s", indent, "");
683    Stream_write_string(f, "var ");    Stream_write_string(f, "var ");
684    for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) {    instrument_declarations(node, f);
     assert(p->pn_type == TOK_NAME);  
     assert(p->pn_arity == PN_NAME);  
     if (p != node->pn_head) {  
       Stream_write_string(f, ", ");  
     }  
     print_string_atom(p->pn_atom, f);  
     if (p->pn_expr != NULL) {  
       Stream_write_string(f, " = ");  
       instrument_expression(p->pn_expr, f);  
     }  
   }  
685  }  }
686    
687  static void output_statement(JSParseNode * node, Stream * f, int indent) {  static void instrument_let_definition(JSParseNode * node, Stream * f, int indent) {
688      assert(node->pn_arity == PN_LIST);
689      Stream_printf(f, "%*s", indent, "");
690      Stream_write_string(f, "let ");
691      instrument_declarations(node, f);
692    }
693    
694    static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
695    switch (node->pn_type) {    switch (node->pn_type) {
696    case TOK_FUNCTION:    case TOK_FUNCTION:
697      instrument_function(node, f, indent, FUNCTION_NORMAL);      instrument_function(node, f, indent, FUNCTION_NORMAL);
# Line 630  Line 702 
702      Stream_write_string(f, "{\n");      Stream_write_string(f, "{\n");
703  */  */
704      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) {
705        instrument_statement(p, f, indent);        instrument_statement(p, f, indent, false);
706      }      }
707  /*  /*
708      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 638  Line 710 
710  */  */
711      break;      break;
712    case TOK_IF:    case TOK_IF:
713      {
714      assert(node->pn_arity == PN_TERNARY);      assert(node->pn_arity == PN_TERNARY);
715    
716        uint16_t line = node->pn_pos.begin.lineno;
717        if (! is_jscoverage_if) {
718          if (line > num_lines) {
719            fatal("%s: script contains more than 65,535 lines", file_id);
720          }
721          if (line >= 2 && exclusive_directives[line - 2]) {
722            is_jscoverage_if = true;
723          }
724        }
725    
726      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
727      Stream_write_string(f, "if (");      Stream_write_string(f, "if (");
728      instrument_expression(node->pn_kid1, f);      instrument_expression(node->pn_kid1, f);
729      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
730      instrument_statement(node->pn_kid2, f, indent + 2);      if (is_jscoverage_if && node->pn_kid3) {
731          uint16_t else_start = node->pn_kid3->pn_pos.begin.lineno;
732          uint16_t else_end = node->pn_kid3->pn_pos.end.lineno + 1;
733          Stream_printf(f, "%*s", indent + 2, "");
734          Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, else_start, else_end);
735        }
736        instrument_statement(node->pn_kid2, f, indent + 2, false);
737      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
738      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
739      if (node->pn_kid3) {  
740        if (node->pn_kid3 || is_jscoverage_if) {
741        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
742        Stream_write_string(f, "else {\n");        Stream_write_string(f, "else {\n");
743        instrument_statement(node->pn_kid3, f, indent + 2);  
744          if (is_jscoverage_if) {
745            uint16_t if_start = node->pn_kid2->pn_pos.begin.lineno + 1;
746            uint16_t if_end = node->pn_kid2->pn_pos.end.lineno + 1;
747            Stream_printf(f, "%*s", indent + 2, "");
748            Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_start, if_end);
749          }
750    
751          if (node->pn_kid3) {
752            instrument_statement(node->pn_kid3, f, indent + 2, is_jscoverage_if);
753          }
754    
755        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
756        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
757      }      }
758    
759      break;      break;
760      }
761    case TOK_SWITCH:    case TOK_SWITCH:
762      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
763      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 675  Line 779 
779          abort();          abort();
780          break;          break;
781        }        }
782        instrument_statement(p->pn_right, f, indent + 2);        instrument_statement(p->pn_right, f, indent + 2, false);
783      }      }
784      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
785      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
# Line 690  Line 794 
794      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
795      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
796      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
797      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
798      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
799      break;      break;
800    case TOK_DO:    case TOK_DO:
801      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
802      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
803      Stream_write_string(f, "do {\n");      Stream_write_string(f, "do {\n");
804      instrument_statement(node->pn_left, f, indent + 2);      instrument_statement(node->pn_left, f, indent + 2, false);
805      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
806      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
807      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
# Line 707  Line 811 
811    case TOK_FOR:    case TOK_FOR:
812      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
813      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
814      Stream_write_string(f, "for (");      Stream_write_string(f, "for ");
815        if (node->pn_iflags & JSITER_FOREACH) {
816          Stream_write_string(f, "each ");
817        }
818        Stream_write_char(f, '(');
819      switch (node->pn_left->pn_type) {      switch (node->pn_left->pn_type) {
820      case TOK_IN:      case TOK_IN:
821        /* for/in */        /* for/in */
# Line 716  Line 824 
824        case TOK_VAR:        case TOK_VAR:
825          instrument_var_statement(node->pn_left->pn_left, f, 0);          instrument_var_statement(node->pn_left->pn_left, f, 0);
826          break;          break;
827          case TOK_LET:
828            instrument_let_definition(node->pn_left->pn_left, f, 0);
829            break;
830        case TOK_NAME:        case TOK_NAME:
831          instrument_expression(node->pn_left->pn_left, f);          instrument_expression(node->pn_left->pn_left, f);
832          break;          break;
# Line 737  Line 848 
848        /* for (;;) */        /* for (;;) */
849        assert(node->pn_left->pn_arity == PN_TERNARY);        assert(node->pn_left->pn_arity == PN_TERNARY);
850        if (node->pn_left->pn_kid1) {        if (node->pn_left->pn_kid1) {
851          if (node->pn_left->pn_kid1->pn_type == TOK_VAR) {          switch (node->pn_left->pn_kid1->pn_type) {
852            case TOK_VAR:
853            instrument_var_statement(node->pn_left->pn_kid1, f, 0);            instrument_var_statement(node->pn_left->pn_kid1, f, 0);
854          }            break;
855          else {          case TOK_LET:
856              instrument_let_definition(node->pn_left->pn_kid1, f, 0);
857              break;
858            default:
859            instrument_expression(node->pn_left->pn_kid1, f);            instrument_expression(node->pn_left->pn_kid1, f);
860              break;
861          }          }
862        }        }
863        Stream_write_string(f, ";");        Stream_write_string(f, ";");
# Line 760  Line 876 
876        break;        break;
877      }      }
878      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
879      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
880      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
881      break;      break;
882    case TOK_THROW:    case TOK_THROW:
# Line 773  Line 889 
889    case TOK_TRY:    case TOK_TRY:
890      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
891      Stream_write_string(f, "try {\n");      Stream_write_string(f, "try {\n");
892      instrument_statement(node->pn_kid1, f, indent + 2);      instrument_statement(node->pn_kid1, f, indent + 2, false);
893      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
894      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
895      {      if (node->pn_kid2) {
896        for (JSParseNode * catch = node->pn_kid2; catch != NULL; catch = catch->pn_kid2) {        assert(node->pn_kid2->pn_type == TOK_RESERVED);
897          for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) {
898            assert(scope->pn_type == TOK_LEXICALSCOPE);
899            JSParseNode * catch = scope->pn_expr;
900          assert(catch->pn_type == TOK_CATCH);          assert(catch->pn_type == TOK_CATCH);
901          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
902          Stream_write_string(f, "catch (");          Stream_write_string(f, "catch (");
903            /* this may not be a name - destructuring assignment */
904            /*
905          assert(catch->pn_kid1->pn_arity == PN_NAME);          assert(catch->pn_kid1->pn_arity == PN_NAME);
906          print_string_atom(catch->pn_kid1->pn_atom, f);          print_string_atom(catch->pn_kid1->pn_atom, f);
907          if (catch->pn_kid1->pn_expr) {          */
908            instrument_expression(catch->pn_kid1, f);
909            if (catch->pn_kid2) {
910            Stream_write_string(f, " if ");            Stream_write_string(f, " if ");
911            instrument_expression(catch->pn_kid1->pn_expr, f);            instrument_expression(catch->pn_kid2, f);
912          }          }
913          Stream_write_string(f, ") {\n");          Stream_write_string(f, ") {\n");
914          instrument_statement(catch->pn_kid3, f, indent + 2);          instrument_statement(catch->pn_kid3, 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        }        }
# Line 796  Line 919 
919      if (node->pn_kid3) {      if (node->pn_kid3) {
920        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
921        Stream_write_string(f, "finally {\n");        Stream_write_string(f, "finally {\n");
922        instrument_statement(node->pn_kid3, f, indent + 2);        instrument_statement(node->pn_kid3, f, indent + 2, false);
923        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
924        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
925      }      }
# Line 822  Line 945 
945      Stream_write_string(f, "with (");      Stream_write_string(f, "with (");
946      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
947      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
948      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
949      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
950      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
951      break;      break;
# Line 860  Line 983 
983      /*      /*
984      ... use output_statement instead of instrument_statement.      ... use output_statement instead of instrument_statement.
985      */      */
986      output_statement(node->pn_expr, f, indent);      output_statement(node->pn_expr, f, indent, false);
987        break;
988      case TOK_LEXICALSCOPE:
989        /* let statement */
990        assert(node->pn_arity == PN_NAME);
991        switch (node->pn_expr->pn_type) {
992        case TOK_LET:
993          /* let statement */
994          assert(node->pn_expr->pn_arity == PN_BINARY);
995          instrument_statement(node->pn_expr, f, indent, false);
996          break;
997        case TOK_LC:
998          /* block */
999          Stream_printf(f, "%*s", indent, "");
1000          Stream_write_string(f, "{\n");
1001          instrument_statement(node->pn_expr, f, indent + 2, false);
1002          Stream_printf(f, "%*s", indent, "");
1003          Stream_write_string(f, "}\n");
1004          break;
1005        case TOK_FOR:
1006          instrument_statement(node->pn_expr, f, indent, false);
1007          break;
1008        default:
1009          abort();
1010          break;
1011        }
1012        break;
1013      case TOK_LET:
1014        switch (node->pn_arity) {
1015        case PN_BINARY:
1016          /* let statement */
1017          Stream_printf(f, "%*s", indent, "");
1018          Stream_write_string(f, "let (");
1019          assert(node->pn_left->pn_type == TOK_LP);
1020          assert(node->pn_left->pn_arity == PN_LIST);
1021          instrument_declarations(node->pn_left, f);
1022          Stream_write_string(f, ") {\n");
1023          instrument_statement(node->pn_right, f, indent + 2, false);
1024          Stream_printf(f, "%*s", indent, "");
1025          Stream_write_string(f, "}\n");
1026          break;
1027        case PN_LIST:
1028          /* let definition */
1029          instrument_let_definition(node, f, indent);
1030          Stream_write_string(f, ";\n");
1031          break;
1032        default:
1033          abort();
1034          break;
1035        }
1036      break;      break;
1037    default:    default:
1038      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 872  Line 1044 
1044  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
1045  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
1046  */  */
1047  static void instrument_statement(JSParseNode * node, Stream * f, int indent) {  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
1048    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC && node->pn_type != TOK_LEXICALSCOPE) {
1049      int line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
1050        if (line > num_lines) {
1051          fatal("%s: script contains more than 65,535 lines", file_id);
1052        }
1053    
1054      /* the root node has line number 0 */      /* the root node has line number 0 */
1055      if (line != 0) {      if (line != 0) {
1056        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
# Line 882  Line 1058 
1058        lines[line - 1] = 1;        lines[line - 1] = 1;
1059      }      }
1060    }    }
1061    output_statement(node, f, indent);    output_statement(node, f, indent, is_jscoverage_if);
1062  }  }
1063    
1064  static bool characters_start_with(const jschar * characters, size_t line_start, size_t line_end, const char * prefix) {  static bool characters_start_with(const jschar * characters, size_t line_start, size_t line_end, const char * prefix) {
# Line 904  Line 1080 
1080    }    }
1081  }  }
1082    
1083    static bool characters_are_white_space(const jschar * characters, size_t line_start, size_t line_end) {
1084      /* XXX - other Unicode space */
1085      const jschar * end = characters + line_end;
1086      for (const jschar * p = characters + line_start; p < end; p++) {
1087        jschar c = *p;
1088        if (c == 0x9 || c == 0xB || c == 0xC || c == 0x20 || c == 0xA0) {
1089          continue;
1090        }
1091        else {
1092          return false;
1093        }
1094      }
1095      return true;
1096    }
1097    
1098    static void error_reporter(JSContext * context, const char * message, JSErrorReport * report) {
1099      fprintf(stderr, "jscoverage: parse error: line %u: %s\n", report->lineno, message);
1100    }
1101    
1102  void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) {  void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) {
1103    file_id = id;    file_id = id;
1104    
1105    /* scan the javascript */    /* parse the javascript */
1106    JSTokenStream * token_stream = js_NewTokenStream(context, characters, num_characters, NULL, 1, NULL);    JSParseContext parse_context;
1107    if (token_stream == NULL) {    if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) {
1108      fatal("cannot create token stream from file: %s", file_id);      fatal("cannot create token stream from file: %s", file_id);
1109    }    }
1110      JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);
1111    /* parse the javascript */    JSParseNode * node = js_ParseScript(context, global, &parse_context);
   JSParseNode * node = js_ParseTokenStream(context, global, token_stream);  
1112    if (node == NULL) {    if (node == NULL) {
1113        js_ReportUncaughtException(context);
1114      fatal("parse error in file: %s", file_id);      fatal("parse error in file: %s", file_id);
1115    }    }
1116    int num_lines = node->pn_pos.end.lineno;    JS_SetErrorReporter(context, old_error_reporter);
1117      num_lines = node->pn_pos.end.lineno;
1118    lines = xmalloc(num_lines);    lines = xmalloc(num_lines);
1119    for (int i = 0; i < num_lines; i++) {    for (unsigned int i = 0; i < num_lines; i++) {
1120      lines[i] = 0;      lines[i] = 0;
1121    }    }
1122    
1123    /*    /* search code for conditionals */
1124    An instrumented JavaScript file has 3 sections:    exclusive_directives = xnew(bool, num_lines);
1125    1. initialization    for (unsigned int i = 0; i < num_lines; i++) {
1126    2. instrumented source code      exclusive_directives[i] = false;
   3. original source code  
   */  
   
   Stream * instrumented = Stream_new(0);  
   instrument_statement(node, instrumented, 0);  
   
   /* write line number info to the output */  
   Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");  
   Stream_write_string(output, "if (! top._$jscoverage) {\n  top._$jscoverage = {};\n}\n");  
   Stream_write_string(output, "var _$jscoverage = top._$jscoverage;\n");  
   Stream_printf(output, "if (! _$jscoverage['%s']) {\n", file_id);  
   Stream_printf(output, "  _$jscoverage['%s'] = [];\n", file_id);  
   for (int i = 0; i < num_lines; i++) {  
     if (lines[i]) {  
       Stream_printf(output, "  _$jscoverage['%s'][%d] = 0;\n", file_id, i + 1);  
     }  
1127    }    }
   Stream_write_string(output, "}\n");  
   free(lines);  
   lines = NULL;  
1128    
   /* copy the instrumented source code to the output */  
   Stream_write(output, instrumented->data, instrumented->length);  
   
   /* conditionals */  
1129    bool has_conditionals = false;    bool has_conditionals = false;
1130      struct IfDirective * if_directives = NULL;
1131    size_t line_number = 0;    size_t line_number = 0;
1132    size_t i = 0;    size_t i = 0;
1133    while (i < num_characters) {    while (i < num_characters) {
1134        if (line_number == UINT16_MAX) {
1135          fatal("%s: script has more than 65,535 lines", file_id);
1136        }
1137      line_number++;      line_number++;
1138      size_t line_start = i;      size_t line_start = i;
1139      jschar c;      jschar c;
# Line 972  Line 1149 
1149          break;          break;
1150        default:        default:
1151          i++;          i++;
         break;  
1152        }        }
1153      }      }
1154      size_t line_end = i;      size_t line_end = i;
# Line 982  Line 1158 
1158          i++;          i++;
1159        }        }
1160      }      }
1161    
1162      if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) {      if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) {
1163        if (! has_conditionals) {        has_conditionals = true;
1164          has_conditionals = true;  
1165          Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);        if (characters_are_white_space(characters, line_start + 16, line_end)) {
1166        }          exclusive_directives[line_number - 1] = true;
1167        Stream_write_string(output, "if (!(");        }
1168        for (size_t j = line_start + 16; j < line_end; j++) {        else {
1169          jschar c = characters[j];          struct IfDirective * if_directive = xnew(struct IfDirective, 1);
1170          if (c == '\t' || (32 <= c && c <= 126)) {          if_directive->condition_start = characters + line_start + 16;
1171            Stream_write_char(output, c);          if_directive->condition_end = characters + line_end;
1172          }          if_directive->start_line = line_number;
1173          else {          if_directive->end_line = 0;
1174            Stream_printf(output, "\\u%04x", c);          if_directive->next = if_directives;
1175          }          if_directives = if_directive;
1176        }        }
       Stream_write_string(output, ")) {\n");  
       Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = ", file_id, line_number);  
1177      }      }
1178      else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) {      else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) {
1179        Stream_printf(output, "%d;\n", line_number);        for (struct IfDirective * p = if_directives; p != NULL; p = p->next) {
1180        Stream_printf(output, "}\n");          if (p->end_line == 0) {
1181              p->end_line = line_number;
1182              break;
1183            }
1184          }
1185      }      }
1186    }    }
1187    
1188      /*
1189      An instrumented JavaScript file has 4 sections:
1190      1. initialization
1191      2. instrumented source code
1192      3. conditionals
1193      4. original source code
1194      */
1195    
1196      Stream * instrumented = Stream_new(0);
1197      instrument_statement(node, instrumented, 0, false);
1198      js_FinishParseContext(context, &parse_context);
1199    
1200      /* write line number info to the output */
1201      Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");
1202      Stream_write_string(output, "if (! top._$jscoverage) {\n  top._$jscoverage = {};\n}\n");
1203      Stream_write_string(output, "var _$jscoverage = top._$jscoverage;\n");
1204      Stream_printf(output, "if (! _$jscoverage['%s']) {\n", file_id);
1205      Stream_printf(output, "  _$jscoverage['%s'] = [];\n", file_id);
1206      for (int i = 0; i < num_lines; i++) {
1207        if (lines[i]) {
1208          Stream_printf(output, "  _$jscoverage['%s'][%d] = 0;\n", file_id, i + 1);
1209        }
1210      }
1211      Stream_write_string(output, "}\n");
1212      free(lines);
1213      lines = NULL;
1214      free(exclusive_directives);
1215      exclusive_directives = NULL;
1216    
1217      /* conditionals */
1218      if (has_conditionals) {
1219        Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);
1220      }
1221    
1222      /* copy the instrumented source code to the output */
1223      Stream_write(output, instrumented->data, instrumented->length);
1224    
1225      /* conditionals */
1226      for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) {
1227        Stream_write_string(output, "if (!(");
1228        print_javascript(if_directive->condition_start, if_directive->condition_end - if_directive->condition_start, output);
1229        Stream_write_string(output, ")) {\n");
1230        Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line);
1231        Stream_write_string(output, "}\n");
1232      }
1233    
1234      /* free */
1235      while (if_directives != NULL) {
1236        struct IfDirective * if_directive = if_directives;
1237        if_directives = if_directives->next;
1238        free(if_directive);
1239      }
1240    
1241    /* copy the original source to the output */    /* copy the original source to the output */
1242    Stream_printf(output, "_$jscoverage['%s'].source = ", file_id);    Stream_printf(output, "_$jscoverage['%s'].source = ", file_id);
1243    jscoverage_write_source(id, characters, num_characters, output);    jscoverage_write_source(id, characters, num_characters, output);
# Line 1044  Line 1276 
1276            /* line feed (new line) */            /* line feed (new line) */
1277            done = true;            done = true;
1278            break;            break;
1279            /* IE doesn't support this */
1280            /*
1281          case 0xb:          case 0xb:
           /* vertical tab */  
1282            Stream_write_string(output, "\\v");            Stream_write_string(output, "\\v");
1283            break;            break;
1284            */
1285          case 0xc:          case 0xc:
1286            /* form feed */            /* form feed */
1287            Stream_write_string(output, "\\f");            Stream_write_string(output, "\\f");
# Line 1102  Line 1336 
1336            /* line feed (new line) */            /* line feed (new line) */
1337            done = true;            done = true;
1338            break;            break;
1339            /* IE doesn't support this */
1340            /*
1341          case 0xb:          case 0xb:
           /* vertical tab */  
1342            Stream_write_string(output, "\\v");            Stream_write_string(output, "\\v");
1343            break;            break;
1344            */
1345          case 0xc:          case 0xc:
1346            /* form feed */            /* form feed */
1347            Stream_write_string(output, "\\f");            Stream_write_string(output, "\\f");
# Line 1232  Line 1468 
1468  }  }
1469    
1470  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) {
1471      int result = 0;
1472    
1473    jschar * base = js_InflateString(context, (char *) json, &length);    jschar * base = js_InflateString(context, (char *) json, &length);
1474    if (base == NULL) {    if (base == NULL) {
1475      fatal("out of memory");      fatal("out of memory");
# Line 1244  Line 1482 
1482    
1483    JS_free(context, base);    JS_free(context, base);
1484    
1485    JSTokenStream * token_stream = js_NewTokenStream(context, parenthesized_json, length + 2, NULL, 1, NULL);    JSParseContext parse_context;
1486    if (token_stream == NULL) {    if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) {
1487      fatal("cannot create token stream");      free(parenthesized_json);
1488        return -1;
1489    }    }
1490      JSParseNode * root = js_ParseScript(context, global, &parse_context);
   JSParseNode * root = js_ParseTokenStream(context, global, token_stream);  
1491    free(parenthesized_json);    free(parenthesized_json);
1492    if (root == NULL) {    if (root == NULL) {
1493      return -1;      result = -1;
1494        goto done;
1495    }    }
1496    
1497    /* root node must be TOK_LC */    /* root node must be TOK_LC */
1498    if (root->pn_type != TOK_LC) {    if (root->pn_type != TOK_LC) {
1499      return -1;      result = -1;
1500        goto done;
1501    }    }
1502    JSParseNode * semi = root->pn_u.list.head;    JSParseNode * semi = root->pn_u.list.head;
1503    
1504    /* 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 */
1505    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {
1506      return -1;      result = -1;
1507        goto done;
1508    }    }
1509    JSParseNode * parenthesized = semi->pn_kid;    JSParseNode * parenthesized = semi->pn_kid;
1510    
1511    /* this must be a parenthesized expression */    /* this must be a parenthesized expression */
1512    if (parenthesized->pn_type != TOK_RP) {    if (parenthesized->pn_type != TOK_RP) {
1513      return -1;      result = -1;
1514        goto done;
1515    }    }
1516    JSParseNode * object = parenthesized->pn_kid;    JSParseNode * object = parenthesized->pn_kid;
1517    
1518    /* this must be an object literal */    /* this must be an object literal */
1519    if (object->pn_type != TOK_RC) {    if (object->pn_type != TOK_RC) {
1520      return -1;      result = -1;
1521        goto done;
1522    }    }
1523    
1524    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {
1525      /* every element of this list must be TOK_COLON */      /* every element of this list must be TOK_COLON */
1526      if (p->pn_type != TOK_COLON) {      if (p->pn_type != TOK_COLON) {
1527        return -1;        result = -1;
1528          goto done;
1529      }      }
1530    
1531      /* the key must be a string representing the file */      /* the key must be a string representing the file */
1532      JSParseNode * key = p->pn_left;      JSParseNode * key = p->pn_left;
1533      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {
1534        return -1;        result = -1;
1535          goto done;
1536      }      }
1537      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));
1538    
1539      /* the value must be an object literal OR an array */      /* the value must be an object literal OR an array */
1540      JSParseNode * value = p->pn_right;      JSParseNode * value = p->pn_right;
1541      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {
1542        return -1;        result = -1;
1543          goto done;
1544      }      }
1545    
1546      JSParseNode * array = NULL;      JSParseNode * array = NULL;
# Line 1306  Line 1552 
1552      else if (value->pn_type == TOK_RC) {      else if (value->pn_type == TOK_RC) {
1553        /* an object literal */        /* an object literal */
1554        if (value->pn_count != 2) {        if (value->pn_count != 2) {
1555          return -1;          result = -1;
1556            goto done;
1557        }        }
1558        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {
1559          if (element->pn_type != TOK_COLON) {          if (element->pn_type != TOK_COLON) {
1560            return -1;            result = -1;
1561              goto done;
1562          }          }
1563          JSParseNode * left = element->pn_left;          JSParseNode * left = element->pn_left;
1564          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {
1565            return -1;            result = -1;
1566              goto done;
1567          }          }
1568          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));
1569          if (strcmp(s, "coverage") == 0) {          if (strcmp(s, "coverage") == 0) {
1570            array = element->pn_right;            array = element->pn_right;
1571            if (array->pn_type != TOK_RB) {            if (array->pn_type != TOK_RB) {
1572              return -1;              result = -1;
1573                goto done;
1574            }            }
1575          }          }
1576          else if (strcmp(s, "source") == 0) {          else if (strcmp(s, "source") == 0) {
1577            source = element->pn_right;            source = element->pn_right;
1578            if (source->pn_type != TOK_RB) {            if (source->pn_type != TOK_RB) {
1579              return -1;              result = -1;
1580                goto done;
1581            }            }
1582          }          }
1583          else {          else {
1584            return -1;            result = -1;
1585              goto done;
1586          }          }
1587        }        }
1588      }      }
1589      else {      else {
1590        return -1;        result = -1;
1591          goto done;
1592      }      }
1593    
1594      if (array == NULL) {      if (array == NULL) {
1595        return -1;        result = -1;
1596          goto done;
1597      }      }
1598    
1599      /* look up the file in the coverage table */      /* look up the file in the coverage table */
# Line 1351  Line 1605 
1605        file_coverage->id = id;        file_coverage->id = id;
1606        file_coverage->num_coverage_lines = array->pn_count;        file_coverage->num_coverage_lines = array->pn_count;
1607        file_coverage->coverage_lines = xnew(int, array->pn_count);        file_coverage->coverage_lines = xnew(int, array->pn_count);
1608        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);  
       }  
1609    
1610        /* set coverage for all lines */        /* set coverage for all lines */
1611        uint32 i = 0;        uint32 i = 0;
# Line 1377  Line 1617 
1617            file_coverage->coverage_lines[i] = -1;            file_coverage->coverage_lines[i] = -1;
1618          }          }
1619          else {          else {
1620            return -1;            result = -1;
1621              goto done;
1622          }          }
1623        }        }
1624        assert(i == array->pn_count);        assert(i == array->pn_count);
# Line 1393  Line 1634 
1634        /* sanity check */        /* sanity check */
1635        assert(strcmp(file_coverage->id, id_bytes) == 0);        assert(strcmp(file_coverage->id, id_bytes) == 0);
1636        if (file_coverage->num_coverage_lines != array->pn_count) {        if (file_coverage->num_coverage_lines != array->pn_count) {
1637          return -2;          result = -2;
1638            goto done;
1639        }        }
1640    
1641        /* merge the coverage */        /* merge the coverage */
# Line 1401  Line 1643 
1643        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++) {
1644          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1645            if (file_coverage->coverage_lines[i] == -1) {            if (file_coverage->coverage_lines[i] == -1) {
1646              return -2;              result = -2;
1647                goto done;
1648            }            }
1649            file_coverage->coverage_lines[i] += (int) element->pn_dval;            file_coverage->coverage_lines[i] += (int) element->pn_dval;
1650          }          }
1651          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1652            if (file_coverage->coverage_lines[i] != -1) {            if (file_coverage->coverage_lines[i] != -1) {
1653              return -2;              result = -2;
1654                goto done;
1655            }            }
1656          }          }
1657          else {          else {
1658            return -1;            result = -1;
1659              goto done;
1660          }          }
1661        }        }
1662        assert(i == array->pn_count);        assert(i == array->pn_count);
1663        }
1664    
1665        /* if this JSON file has source, use it */      /* if this JSON file has source, use it */
1666        if (file_coverage->source_lines == NULL && source != NULL) {      if (file_coverage->source_lines == NULL && source != NULL) {
1667          file_coverage->num_source_lines = source->pn_count;        file_coverage->num_source_lines = source->pn_count;
1668          file_coverage->source_lines = xnew(char *, source->pn_count);        file_coverage->source_lines = xnew(char *, source->pn_count);
1669          uint32 i = 0;        uint32 i = 0;
1670          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++) {
1671            if (element->pn_type != TOK_STRING) {          if (element->pn_type != TOK_STRING) {
1672              return -1;            result = -1;
1673            }            goto done;
           file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));  
1674          }          }
1675          assert(i == source->pn_count);          file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));
1676        }        }
1677          assert(i == source->pn_count);
1678      }      }
1679    }    }
1680    
1681    return 0;  done:
1682      js_FinishParseContext(context, &parse_context);
1683      return result;
1684  }  }

Legend:
Removed from v.190  
changed lines
  Added in v.342

  ViewVC Help
Powered by ViewVC 1.1.24