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

Diff of /trunk/instrument-js.c

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

revision 169 by siliconforks, Mon Sep 15 16:22:41 2008 UTC revision 341 by siliconforks, Fri Oct 24 16:14:43 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>
38  #include <jsstr.h>  #include <jsstr.h>
39    
40    #include "encoding.h"
41    #include "global.h"
42    #include "highlight.h"
43  #include "resource-manager.h"  #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 47  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 59  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 74  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    for (size_t i = 0; i < s->length; i++) {    size_t length = JSSTRING_LENGTH(s);
127      char c = s->chars[i];    jschar * characters = JSSTRING_CHARS(s);
128      Stream_write_char(f, c);    for (size_t i = 0; i < length; i++) {
129        jschar c = characters[i];
130        if (32 <= c && c <= 126) {
131          switch (c) {
132          case '"':
133            Stream_write_string(f, "\\\"");
134            break;
135    /*
136          case '\'':
137            Stream_write_string(f, "\\'");
138            break;
139    */
140          case '\\':
141            Stream_write_string(f, "\\\\");
142            break;
143          default:
144            Stream_write_char(f, c);
145            break;
146          }
147        }
148        else {
149          switch (c) {
150          case 0x8:
151            Stream_write_string(f, "\\b");
152            break;
153          case 0x9:
154            Stream_write_string(f, "\\t");
155            break;
156          case 0xa:
157            Stream_write_string(f, "\\n");
158            break;
159          /* IE doesn't support this */
160          /*
161          case 0xb:
162            Stream_write_string(f, "\\v");
163            break;
164          */
165          case 0xc:
166            Stream_write_string(f, "\\f");
167            break;
168          case 0xd:
169            Stream_write_string(f, "\\r");
170            break;
171          default:
172            Stream_printf(f, "\\u%04x", c);
173            break;
174          }
175        }
176    }    }
177  }  }
178    
# Line 87  Line 182 
182    print_string(s, f);    print_string(s, f);
183  }  }
184    
185  static void print_string_jsval(jsval value, Stream * f) {  static void print_regex(jsval value, Stream * f) {
186    assert(JSVAL_IS_STRING(value));    assert(JSVAL_IS_STRING(value));
187    JSString * s = JSVAL_TO_STRING(value);    JSString * s = JSVAL_TO_STRING(value);
188    print_string(s, f);    size_t length = JSSTRING_LENGTH(s);
189      jschar * characters = JSSTRING_CHARS(s);
190      for (size_t i = 0; i < length; i++) {
191        jschar c = characters[i];
192        if (32 <= c && c <= 126) {
193          Stream_write_char(f, c);
194        }
195        else {
196          Stream_printf(f, "\\u%04x", c);
197        }
198      }
199  }  }
200    
201  static void print_quoted_string_atom(JSAtom * atom, Stream * f) {  static void print_quoted_string_atom(JSAtom * atom, Stream * f) {
202    assert(ATOM_IS_STRING(atom));    assert(ATOM_IS_STRING(atom));
203    JSString * s = ATOM_TO_STRING(atom);    JSString * s = ATOM_TO_STRING(atom);
204    JSString * quoted = js_QuoteString(context, s, '"');    Stream_write_char(f, '"');
205    print_string(quoted, f);    print_string(s, f);
206      Stream_write_char(f, '"');
207  }  }
208    
209  static const char * get_op(uint8 op) {  static const char * get_op(uint8 op) {
210    switch(op) {    switch(op) {
211      case JSOP_OR:
212        return "||";
213      case JSOP_AND:
214        return "&&";
215    case JSOP_BITOR:    case JSOP_BITOR:
216      return "|";      return "|";
217    case JSOP_BITXOR:    case JSOP_BITXOR:
# Line 112  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 146  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 154  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 172  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 219  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 277  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 398  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 446  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 475  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_string_jsval(result, f);  
       }  
       break;  
     default:  
       abort();  
       break;  
614      }      }
615      break;      break;
616    case TOK_NUMBER:    case TOK_NUMBER:
# Line 538  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    default:    default:
671      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);
672    }    }
# Line 547  Line 676 
676    assert(node->pn_arity == PN_LIST);    assert(node->pn_arity == PN_LIST);
677    Stream_printf(f, "%*s", indent, "");    Stream_printf(f, "%*s", indent, "");
678    Stream_write_string(f, "var ");    Stream_write_string(f, "var ");
679    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);  
     }  
   }  
680  }  }
681    
682  static void output_statement(JSParseNode * node, Stream * f, int indent) {  static void instrument_let_definition(JSParseNode * node, Stream * f, int indent) {
683      assert(node->pn_arity == PN_LIST);
684      Stream_printf(f, "%*s", indent, "");
685      Stream_write_string(f, "let ");
686      instrument_declarations(node, f);
687    }
688    
689    static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
690    switch (node->pn_type) {    switch (node->pn_type) {
691    case TOK_FUNCTION:    case TOK_FUNCTION:
692      instrument_function(node, f, indent, FUNCTION_NORMAL);      instrument_function(node, f, indent, FUNCTION_NORMAL);
# Line 572  Line 697 
697      Stream_write_string(f, "{\n");      Stream_write_string(f, "{\n");
698  */  */
699      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) {
700        instrument_statement(p, f, indent);        instrument_statement(p, f, indent, false);
701      }      }
702  /*  /*
703      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 580  Line 705 
705  */  */
706      break;      break;
707    case TOK_IF:    case TOK_IF:
708      {
709      assert(node->pn_arity == PN_TERNARY);      assert(node->pn_arity == PN_TERNARY);
710    
711        uint16_t line = node->pn_pos.begin.lineno;
712        if (! is_jscoverage_if) {
713          if (line > num_lines) {
714            fatal("%s: script contains more than 65,535 lines", file_id);
715          }
716          if (line >= 2 && exclusive_directives[line - 2]) {
717            is_jscoverage_if = true;
718          }
719        }
720    
721      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
722      Stream_write_string(f, "if (");      Stream_write_string(f, "if (");
723      instrument_expression(node->pn_kid1, f);      instrument_expression(node->pn_kid1, f);
724      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
725      instrument_statement(node->pn_kid2, f, indent + 2);      if (is_jscoverage_if && node->pn_kid3) {
726          uint16_t else_start = node->pn_kid3->pn_pos.begin.lineno;
727          uint16_t else_end = node->pn_kid3->pn_pos.end.lineno + 1;
728          Stream_printf(f, "%*s", indent + 2, "");
729          Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, else_start, else_end);
730        }
731        instrument_statement(node->pn_kid2, f, indent + 2, false);
732      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
733      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
734      if (node->pn_kid3) {  
735        if (node->pn_kid3 || is_jscoverage_if) {
736        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
737        Stream_write_string(f, "else {\n");        Stream_write_string(f, "else {\n");
738        instrument_statement(node->pn_kid3, f, indent + 2);  
739          if (is_jscoverage_if) {
740            uint16_t if_start = node->pn_kid2->pn_pos.begin.lineno + 1;
741            uint16_t if_end = node->pn_kid2->pn_pos.end.lineno + 1;
742            Stream_printf(f, "%*s", indent + 2, "");
743            Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_start, if_end);
744          }
745    
746          if (node->pn_kid3) {
747            instrument_statement(node->pn_kid3, f, indent + 2, is_jscoverage_if);
748          }
749    
750        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
751        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
752      }      }
753    
754      break;      break;
755      }
756    case TOK_SWITCH:    case TOK_SWITCH:
757      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
758      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 617  Line 774 
774          abort();          abort();
775          break;          break;
776        }        }
777        instrument_statement(p->pn_right, f, indent + 2);        instrument_statement(p->pn_right, f, indent + 2, false);
778      }      }
779      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
780      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
# Line 632  Line 789 
789      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
790      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
791      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
792      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
793      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
794      break;      break;
795    case TOK_DO:    case TOK_DO:
796      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
797      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
798      Stream_write_string(f, "do {\n");      Stream_write_string(f, "do {\n");
799      instrument_statement(node->pn_left, f, indent + 2);      instrument_statement(node->pn_left, f, indent + 2, false);
800      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
801      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
802      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
# Line 649  Line 806 
806    case TOK_FOR:    case TOK_FOR:
807      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
808      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
809      Stream_write_string(f, "for (");      Stream_write_string(f, "for ");
810        if (node->pn_iflags & JSITER_FOREACH) {
811          Stream_write_string(f, "each ");
812        }
813        Stream_write_char(f, '(');
814      switch (node->pn_left->pn_type) {      switch (node->pn_left->pn_type) {
815      case TOK_IN:      case TOK_IN:
816        /* for/in */        /* for/in */
# Line 658  Line 819 
819        case TOK_VAR:        case TOK_VAR:
820          instrument_var_statement(node->pn_left->pn_left, f, 0);          instrument_var_statement(node->pn_left->pn_left, f, 0);
821          break;          break;
822          case TOK_LET:
823            instrument_let_definition(node->pn_left->pn_left, f, 0);
824            break;
825        case TOK_NAME:        case TOK_NAME:
826          instrument_expression(node->pn_left->pn_left, f);          instrument_expression(node->pn_left->pn_left, f);
827          break;          break;
# Line 679  Line 843 
843        /* for (;;) */        /* for (;;) */
844        assert(node->pn_left->pn_arity == PN_TERNARY);        assert(node->pn_left->pn_arity == PN_TERNARY);
845        if (node->pn_left->pn_kid1) {        if (node->pn_left->pn_kid1) {
846          if (node->pn_left->pn_kid1->pn_type == TOK_VAR) {          switch (node->pn_left->pn_kid1->pn_type) {
847            case TOK_VAR:
848            instrument_var_statement(node->pn_left->pn_kid1, f, 0);            instrument_var_statement(node->pn_left->pn_kid1, f, 0);
849          }            break;
850          else {          case TOK_LET:
851              instrument_let_definition(node->pn_left->pn_kid1, f, 0);
852              break;
853            default:
854            instrument_expression(node->pn_left->pn_kid1, f);            instrument_expression(node->pn_left->pn_kid1, f);
855              break;
856          }          }
857        }        }
858        Stream_write_string(f, ";");        Stream_write_string(f, ";");
# Line 702  Line 871 
871        break;        break;
872      }      }
873      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
874      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
875      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
876      break;      break;
877    case TOK_THROW:    case TOK_THROW:
# Line 715  Line 884 
884    case TOK_TRY:    case TOK_TRY:
885      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
886      Stream_write_string(f, "try {\n");      Stream_write_string(f, "try {\n");
887      instrument_statement(node->pn_kid1, f, indent + 2);      instrument_statement(node->pn_kid1, f, indent + 2, false);
888      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
889      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
890      {      if (node->pn_kid2) {
891        for (JSParseNode * catch = node->pn_kid2; catch != NULL; catch = catch->pn_kid2) {        assert(node->pn_kid2->pn_type == TOK_RESERVED);
892          for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) {
893            assert(scope->pn_type == TOK_LEXICALSCOPE);
894            JSParseNode * catch = scope->pn_expr;
895          assert(catch->pn_type == TOK_CATCH);          assert(catch->pn_type == TOK_CATCH);
896          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
897          Stream_write_string(f, "catch (");          Stream_write_string(f, "catch (");
898            /* this may not be a name - destructuring assignment */
899            /*
900          assert(catch->pn_kid1->pn_arity == PN_NAME);          assert(catch->pn_kid1->pn_arity == PN_NAME);
901          print_string_atom(catch->pn_kid1->pn_atom, f);          print_string_atom(catch->pn_kid1->pn_atom, f);
902          if (catch->pn_kid1->pn_expr) {          */
903            instrument_expression(catch->pn_kid1, f);
904            if (catch->pn_kid2) {
905            Stream_write_string(f, " if ");            Stream_write_string(f, " if ");
906            instrument_expression(catch->pn_kid1->pn_expr, f);            instrument_expression(catch->pn_kid2, f);
907          }          }
908          Stream_write_string(f, ") {\n");          Stream_write_string(f, ") {\n");
909          instrument_statement(catch->pn_kid3, f, indent + 2);          instrument_statement(catch->pn_kid3, f, indent + 2, false);
910          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
911          Stream_write_string(f, "}\n");          Stream_write_string(f, "}\n");
912        }        }
# Line 738  Line 914 
914      if (node->pn_kid3) {      if (node->pn_kid3) {
915        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
916        Stream_write_string(f, "finally {\n");        Stream_write_string(f, "finally {\n");
917        instrument_statement(node->pn_kid3, f, indent + 2);        instrument_statement(node->pn_kid3, f, indent + 2, false);
918        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
919        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
920      }      }
# Line 764  Line 940 
940      Stream_write_string(f, "with (");      Stream_write_string(f, "with (");
941      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
942      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
943      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
944      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
945      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
946      break;      break;
# Line 802  Line 978 
978      /*      /*
979      ... use output_statement instead of instrument_statement.      ... use output_statement instead of instrument_statement.
980      */      */
981      output_statement(node->pn_expr, f, indent);      output_statement(node->pn_expr, f, indent, false);
982        break;
983      case TOK_LEXICALSCOPE:
984        /* let statement */
985        assert(node->pn_arity == PN_NAME);
986        switch (node->pn_expr->pn_type) {
987        case TOK_LET:
988          /* let statement */
989          assert(node->pn_expr->pn_arity == PN_BINARY);
990          instrument_statement(node->pn_expr, f, indent, false);
991          break;
992        case TOK_LC:
993          /* block */
994          Stream_printf(f, "%*s", indent, "");
995          Stream_write_string(f, "{\n");
996          instrument_statement(node->pn_expr, f, indent + 2, false);
997          Stream_printf(f, "%*s", indent, "");
998          Stream_write_string(f, "}\n");
999          break;
1000        case TOK_FOR:
1001          instrument_statement(node->pn_expr, f, indent, false);
1002          break;
1003        default:
1004          abort();
1005          break;
1006        }
1007        break;
1008      case TOK_LET:
1009        switch (node->pn_arity) {
1010        case PN_BINARY:
1011          /* let statement */
1012          Stream_printf(f, "%*s", indent, "");
1013          Stream_write_string(f, "let (");
1014          assert(node->pn_left->pn_type == TOK_LP);
1015          assert(node->pn_left->pn_arity == PN_LIST);
1016          instrument_declarations(node->pn_left, f);
1017          Stream_write_string(f, ") {\n");
1018          instrument_statement(node->pn_right, f, indent + 2, false);
1019          Stream_printf(f, "%*s", indent, "");
1020          Stream_write_string(f, "}\n");
1021          break;
1022        case PN_LIST:
1023          /* let definition */
1024          instrument_let_definition(node, f, indent);
1025          Stream_write_string(f, ";\n");
1026          break;
1027        default:
1028          abort();
1029          break;
1030        }
1031      break;      break;
1032    default:    default:
1033      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 814  Line 1039 
1039  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
1040  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
1041  */  */
1042  static void instrument_statement(JSParseNode * node, Stream * f, int indent) {  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
1043    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC && node->pn_type != TOK_LEXICALSCOPE) {
1044      int line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
1045        if (line > num_lines) {
1046          fatal("%s: script contains more than 65,535 lines", file_id);
1047        }
1048    
1049      /* the root node has line number 0 */      /* the root node has line number 0 */
1050      if (line != 0) {      if (line != 0) {
1051        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
# Line 824  Line 1053 
1053        lines[line - 1] = 1;        lines[line - 1] = 1;
1054      }      }
1055    }    }
1056    output_statement(node, f, indent);    output_statement(node, f, indent, is_jscoverage_if);
1057  }  }
1058    
1059  void jscoverage_instrument_js(const char * id, Stream * input, Stream * output) {  static bool characters_start_with(const jschar * characters, size_t line_start, size_t line_end, const char * prefix) {
1060    file_id = id;    const jschar * characters_end = characters + line_end;
1061      const jschar * cp = characters + line_start;
1062    /* scan the javascript */    const char * bp = prefix;
1063    size_t input_length = input->length;    for (;;) {
1064    jschar * base = js_InflateString(context, (char *) input->data, &input_length);      if (*bp == '\0') {
1065    if (base == NULL) {        return true;
1066      fatal("out of memory");      }
1067        else if (cp == characters_end) {
1068          return false;
1069        }
1070        else if (*cp != *bp) {
1071          return false;
1072        }
1073        bp++;
1074        cp++;
1075    }    }
1076    JSTokenStream * token_stream = js_NewTokenStream(context, base, input_length, NULL, 1, NULL);  }
1077    if (token_stream == NULL) {  
1078      fatal("cannot create token stream from file: %s", file_id);  static bool characters_are_white_space(const jschar * characters, size_t line_start, size_t line_end) {
1079      /* XXX - other Unicode space */
1080      const jschar * end = characters + line_end;
1081      for (const jschar * p = characters + line_start; p < end; p++) {
1082        jschar c = *p;
1083        if (c == 0x9 || c == 0xB || c == 0xC || c == 0x20 || c == 0xA0) {
1084          continue;
1085        }
1086        else {
1087          return false;
1088        }
1089    }    }
1090      return true;
1091    }
1092    
1093    static void error_reporter(JSContext * context, const char * message, JSErrorReport * report) {
1094      fprintf(stderr, "jscoverage: parse error: line %u: %s\n", report->lineno, message);
1095    }
1096    
1097    void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) {
1098      file_id = id;
1099    
1100    /* parse the javascript */    /* parse the javascript */
1101    JSParseNode * node = js_ParseTokenStream(context, global, token_stream);    JSParseContext parse_context;
1102      if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) {
1103        fatal("cannot create token stream from file: %s", file_id);
1104      }
1105      JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);
1106      JSParseNode * node = js_ParseScript(context, global, &parse_context);
1107    if (node == NULL) {    if (node == NULL) {
1108        js_ReportUncaughtException(context);
1109      fatal("parse error in file: %s", file_id);      fatal("parse error in file: %s", file_id);
1110    }    }
1111    int num_lines = node->pn_pos.end.lineno;    JS_SetErrorReporter(context, old_error_reporter);
1112      num_lines = node->pn_pos.end.lineno;
1113    lines = xmalloc(num_lines);    lines = xmalloc(num_lines);
1114    for (int i = 0; i < num_lines; i++) {    for (unsigned int i = 0; i < num_lines; i++) {
1115      lines[i] = 0;      lines[i] = 0;
1116    }    }
1117    
1118      /* search code for conditionals */
1119      exclusive_directives = xnew(bool, num_lines);
1120      for (unsigned int i = 0; i < num_lines; i++) {
1121        exclusive_directives[i] = false;
1122      }
1123    
1124      bool has_conditionals = false;
1125      struct IfDirective * if_directives = NULL;
1126      size_t line_number = 0;
1127      size_t i = 0;
1128      while (i < num_characters) {
1129        if (line_number == UINT16_MAX) {
1130          fatal("%s: script has more than 65,535 lines", file_id);
1131        }
1132        line_number++;
1133        size_t line_start = i;
1134        jschar c;
1135        bool done = false;
1136        while (! done && i < num_characters) {
1137          c = characters[i];
1138          switch (c) {
1139          case '\r':
1140          case '\n':
1141          case 0x2028:
1142          case 0x2029:
1143            done = true;
1144            break;
1145          default:
1146            i++;
1147          }
1148        }
1149        size_t line_end = i;
1150        if (i < num_characters) {
1151          i++;
1152          if (c == '\r' && i < num_characters && characters[i] == '\n') {
1153            i++;
1154          }
1155        }
1156    
1157        if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) {
1158          has_conditionals = true;
1159    
1160          if (characters_are_white_space(characters, line_start + 16, line_end)) {
1161            exclusive_directives[line_number - 1] = true;
1162          }
1163          else {
1164            struct IfDirective * if_directive = xnew(struct IfDirective, 1);
1165            if_directive->condition_start = characters + line_start + 16;
1166            if_directive->condition_end = characters + line_end;
1167            if_directive->start_line = line_number;
1168            if_directive->end_line = 0;
1169            if_directive->next = if_directives;
1170            if_directives = if_directive;
1171          }
1172        }
1173        else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) {
1174          for (struct IfDirective * p = if_directives; p != NULL; p = p->next) {
1175            if (p->end_line == 0) {
1176              p->end_line = line_number;
1177              break;
1178            }
1179          }
1180        }
1181      }
1182    
1183    /*    /*
1184    An instrumented JavaScript file has 3 sections:    An instrumented JavaScript file has 4 sections:
1185    1. initialization    1. initialization
1186    2. instrumented source code    2. instrumented source code
1187    3. original source code    3. conditionals
1188      4. original source code
1189    */    */
1190    
1191    Stream * instrumented = Stream_new(0);    Stream * instrumented = Stream_new(0);
1192    instrument_statement(node, instrumented, 0);    instrument_statement(node, instrumented, 0, false);
1193      js_FinishParseContext(context, &parse_context);
1194    
1195    /* write line number info to the output */    /* write line number info to the output */
1196    Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");    Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");
# Line 876  Line 1206 
1206    Stream_write_string(output, "}\n");    Stream_write_string(output, "}\n");
1207    free(lines);    free(lines);
1208    lines = NULL;    lines = NULL;
1209      free(exclusive_directives);
1210      exclusive_directives = NULL;
1211    
1212      /* conditionals */
1213      if (has_conditionals) {
1214        Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);
1215      }
1216    
1217    /* copy the instrumented source code to the output */    /* copy the instrumented source code to the output */
1218    Stream_write(output, instrumented->data, instrumented->length);    Stream_write(output, instrumented->data, instrumented->length);
   Stream_write_char(output, '\n');  
1219    
1220    /* conditionals */    /* conditionals */
1221    bool has_conditionals = false;    for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) {
1222    size_t line_number = 0;      Stream_write_string(output, "if (!(");
1223    size_t i = 0;      print_javascript(if_directive->condition_start, if_directive->condition_end - if_directive->condition_start, output);
1224    while (i < input_length) {      Stream_write_string(output, ")) {\n");
1225      line_number++;      Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line);
1226      size_t line_start = i;      Stream_write_string(output, "}\n");
1227      while (i < input_length && base[i] != '\r' && base[i] != '\n') {    }
1228        i++;  
1229      }    /* free */
1230      size_t line_end = i;    while (if_directives != NULL) {
1231      if (i < input_length) {      struct IfDirective * if_directive = if_directives;
1232        if (base[i] == '\r') {      if_directives = if_directives->next;
1233          line_end = i;      free(if_directive);
         i++;  
         if (i < input_length && base[i] == '\n') {  
           i++;  
         }  
       }  
       else if (base[i] == '\n') {  
         line_end = i;  
         i++;  
       }  
       else {  
         abort();  
       }  
     }  
     char * line = js_DeflateString(context, base + 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);  
1234    }    }
1235    
1236    /* copy the original source to the output */    /* copy the original source to the output */
1237    i = 0;    Stream_printf(output, "_$jscoverage['%s'].source = ", file_id);
1238    while (i < input_length) {    jscoverage_write_source(id, characters, num_characters, output);
1239      Stream_write_string(output, "// ");    Stream_printf(output, ";\n");
     size_t line_start = i;  
     while (i < input_length && base[i] != '\r' && base[i] != '\n') {  
       i++;  
     }  
1240    
1241      size_t line_end = i;    Stream_delete(instrumented);
1242      if (i < input_length) {  
1243        if (base[i] == '\r') {    file_id = NULL;
1244          line_end = i;  }
1245    
1246    void jscoverage_write_source(const char * id, const jschar * characters, size_t num_characters, Stream * output) {
1247      Stream_write_string(output, "[");
1248      if (jscoverage_highlight) {
1249        Stream * highlighted_stream = Stream_new(num_characters);
1250        jscoverage_highlight_js(context, id, characters, num_characters, highlighted_stream);
1251        size_t i = 0;
1252        while (i < highlighted_stream->length) {
1253          if (i > 0) {
1254            Stream_write_char(output, ',');
1255          }
1256    
1257          Stream_write_char(output, '"');
1258          bool done = false;
1259          while (! done) {
1260            char c = highlighted_stream->data[i];
1261            switch (c) {
1262            case 0x8:
1263              /* backspace */
1264              Stream_write_string(output, "\\b");
1265              break;
1266            case 0x9:
1267              /* horizontal tab */
1268              Stream_write_string(output, "\\t");
1269              break;
1270            case 0xa:
1271              /* line feed (new line) */
1272              done = true;
1273              break;
1274            /* IE doesn't support this */
1275            /*
1276            case 0xb:
1277              Stream_write_string(output, "\\v");
1278              break;
1279            */
1280            case 0xc:
1281              /* form feed */
1282              Stream_write_string(output, "\\f");
1283              break;
1284            case 0xd:
1285              /* carriage return */
1286              done = true;
1287              if (i + 1 < highlighted_stream->length && highlighted_stream->data[i + 1] == '\n') {
1288                i++;
1289              }
1290              break;
1291            case '"':
1292              Stream_write_string(output, "\\\"");
1293              break;
1294            case '\\':
1295              Stream_write_string(output, "\\\\");
1296              break;
1297            default:
1298              Stream_write_char(output, c);
1299              break;
1300            }
1301          i++;          i++;
1302          if (i < input_length && base[i] == '\n') {          if (i >= highlighted_stream->length) {
1303            i++;            done = true;
1304          }          }
1305        }        }
1306        else if (base[i] == '\n') {        Stream_write_char(output, '"');
1307          line_end = i;      }
1308        Stream_delete(highlighted_stream);
1309      }
1310      else {
1311        size_t i = 0;
1312        while (i < num_characters) {
1313          if (i > 0) {
1314            Stream_write_char(output, ',');
1315          }
1316    
1317          Stream_write_char(output, '"');
1318          bool done = false;
1319          while (! done) {
1320            jschar c = characters[i];
1321            switch (c) {
1322            case 0x8:
1323              /* backspace */
1324              Stream_write_string(output, "\\b");
1325              break;
1326            case 0x9:
1327              /* horizontal tab */
1328              Stream_write_string(output, "\\t");
1329              break;
1330            case 0xa:
1331              /* line feed (new line) */
1332              done = true;
1333              break;
1334            /* IE doesn't support this */
1335            /*
1336            case 0xb:
1337              Stream_write_string(output, "\\v");
1338              break;
1339            */
1340            case 0xc:
1341              /* form feed */
1342              Stream_write_string(output, "\\f");
1343              break;
1344            case 0xd:
1345              /* carriage return */
1346              done = true;
1347              if (i + 1 < num_characters && characters[i + 1] == '\n') {
1348                i++;
1349              }
1350              break;
1351            case '"':
1352              Stream_write_string(output, "\\\"");
1353              break;
1354            case '\\':
1355              Stream_write_string(output, "\\\\");
1356              break;
1357            case '&':
1358              Stream_write_string(output, "&amp;");
1359              break;
1360            case '<':
1361              Stream_write_string(output, "&lt;");
1362              break;
1363            case '>':
1364              Stream_write_string(output, "&gt;");
1365              break;
1366            case 0x2028:
1367            case 0x2029:
1368              done = true;
1369              break;
1370            default:
1371              if (32 <= c && c <= 126) {
1372                Stream_write_char(output, c);
1373              }
1374              else {
1375                Stream_printf(output, "&#%d;", c);
1376              }
1377              break;
1378            }
1379          i++;          i++;
1380            if (i >= num_characters) {
1381              done = true;
1382            }
1383        }        }
1384        else {        Stream_write_char(output, '"');
         abort();  
       }  
1385      }      }
   
     char * line = js_DeflateString(context, base + line_start, line_end - line_start);  
     Stream_write_string(output, line);  
     Stream_write_char(output, '\n');  
     JS_free(context, line);  
1386    }    }
1387      Stream_write_string(output, "]");
   Stream_delete(instrumented);  
   
   JS_free(context, base);  
   
   file_id = NULL;  
1388  }  }
1389    
1390  void jscoverage_copy_resources(const char * destination_directory) {  void jscoverage_copy_resources(const char * destination_directory) {
# Line 970  Line 1393 
1393    copy_resource("jscoverage.js", destination_directory);    copy_resource("jscoverage.js", destination_directory);
1394    copy_resource("jscoverage-ie.css", destination_directory);    copy_resource("jscoverage-ie.css", destination_directory);
1395    copy_resource("jscoverage-throbber.gif", destination_directory);    copy_resource("jscoverage-throbber.gif", destination_directory);
1396    copy_resource("jscoverage-sh_main.js", destination_directory);    copy_resource("jscoverage-highlight.css", destination_directory);
   copy_resource("jscoverage-sh_javascript.js", destination_directory);  
   copy_resource("jscoverage-sh_nedit.css", destination_directory);  
1397  }  }
1398    
1399  /*  /*
# Line 1007  Line 1428 
1428    JS_HashTableDestroy(coverage->coverage_table);    JS_HashTableDestroy(coverage->coverage_table);
1429    struct FileCoverageList * p = coverage->coverage_list;    struct FileCoverageList * p = coverage->coverage_list;
1430    while (p != NULL) {    while (p != NULL) {
1431      free(p->file_coverage->lines);      free(p->file_coverage->coverage_lines);
1432      free(p->file_coverage->source);      if (p->file_coverage->source_lines != NULL) {
1433          for (uint32 i = 0; i < p->file_coverage->num_source_lines; i++) {
1434            free(p->file_coverage->source_lines[i]);
1435          }
1436          free(p->file_coverage->source_lines);
1437        }
1438      free(p->file_coverage->id);      free(p->file_coverage->id);
1439      free(p->file_coverage);      free(p->file_coverage);
1440      struct FileCoverageList * q = p;      struct FileCoverageList * q = p;
# Line 1037  Line 1463 
1463  }  }
1464    
1465  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) {
1466      int result = 0;
1467    
1468    jschar * base = js_InflateString(context, (char *) json, &length);    jschar * base = js_InflateString(context, (char *) json, &length);
1469    if (base == NULL) {    if (base == NULL) {
1470      fatal("out of memory");      fatal("out of memory");
# Line 1049  Line 1477 
1477    
1478    JS_free(context, base);    JS_free(context, base);
1479    
1480    JSTokenStream * token_stream = js_NewTokenStream(context, parenthesized_json, length + 2, NULL, 1, NULL);    JSParseContext parse_context;
1481    if (token_stream == NULL) {    if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) {
1482      fatal("cannot create token stream");      free(parenthesized_json);
1483        return -1;
1484    }    }
1485      JSParseNode * root = js_ParseScript(context, global, &parse_context);
   JSParseNode * root = js_ParseTokenStream(context, global, token_stream);  
1486    free(parenthesized_json);    free(parenthesized_json);
1487    if (root == NULL) {    if (root == NULL) {
1488      return -1;      result = -1;
1489        goto done;
1490    }    }
1491    
1492    /* root node must be TOK_LC */    /* root node must be TOK_LC */
1493    if (root->pn_type != TOK_LC) {    if (root->pn_type != TOK_LC) {
1494      return -1;      result = -1;
1495        goto done;
1496    }    }
1497    JSParseNode * semi = root->pn_u.list.head;    JSParseNode * semi = root->pn_u.list.head;
1498    
1499    /* 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 */
1500    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {
1501      return -1;      result = -1;
1502        goto done;
1503    }    }
1504    JSParseNode * parenthesized = semi->pn_kid;    JSParseNode * parenthesized = semi->pn_kid;
1505    
1506    /* this must be a parenthesized expression */    /* this must be a parenthesized expression */
1507    if (parenthesized->pn_type != TOK_RP) {    if (parenthesized->pn_type != TOK_RP) {
1508      return -1;      result = -1;
1509        goto done;
1510    }    }
1511    JSParseNode * object = parenthesized->pn_kid;    JSParseNode * object = parenthesized->pn_kid;
1512    
1513    /* this must be an object literal */    /* this must be an object literal */
1514    if (object->pn_type != TOK_RC) {    if (object->pn_type != TOK_RC) {
1515      return -1;      result = -1;
1516        goto done;
1517    }    }
1518    
1519    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {
1520      /* every element of this list must be TOK_COLON */      /* every element of this list must be TOK_COLON */
1521      if (p->pn_type != TOK_COLON) {      if (p->pn_type != TOK_COLON) {
1522        return -1;        result = -1;
1523          goto done;
1524      }      }
1525    
1526      /* the key must be a string representing the file */      /* the key must be a string representing the file */
1527      JSParseNode * key = p->pn_left;      JSParseNode * key = p->pn_left;
1528      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {
1529        return -1;        result = -1;
1530          goto done;
1531      }      }
1532      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));
1533    
1534      /* the value must be an object literal OR an array */      /* the value must be an object literal OR an array */
1535      JSParseNode * value = p->pn_right;      JSParseNode * value = p->pn_right;
1536      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {
1537        return -1;        result = -1;
1538          goto done;
1539      }      }
1540    
1541      JSParseNode * array = NULL;      JSParseNode * array = NULL;
# Line 1111  Line 1547 
1547      else if (value->pn_type == TOK_RC) {      else if (value->pn_type == TOK_RC) {
1548        /* an object literal */        /* an object literal */
1549        if (value->pn_count != 2) {        if (value->pn_count != 2) {
1550          return -1;          result = -1;
1551            goto done;
1552        }        }
1553        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {
1554          if (element->pn_type != TOK_COLON) {          if (element->pn_type != TOK_COLON) {
1555            return -1;            result = -1;
1556              goto done;
1557          }          }
1558          JSParseNode * left = element->pn_left;          JSParseNode * left = element->pn_left;
1559          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {
1560            return -1;            result = -1;
1561              goto done;
1562          }          }
1563          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));
1564          if (strcmp(s, "coverage") == 0) {          if (strcmp(s, "coverage") == 0) {
1565            array = element->pn_right;            array = element->pn_right;
1566            if (array->pn_type != TOK_RB) {            if (array->pn_type != TOK_RB) {
1567              return -1;              result = -1;
1568                goto done;
1569            }            }
1570          }          }
1571          else if (strcmp(s, "source") == 0) {          else if (strcmp(s, "source") == 0) {
1572            source = element->pn_right;            source = element->pn_right;
1573            if (source->pn_type != TOK_STRING || ! ATOM_IS_STRING(source->pn_atom)) {            if (source->pn_type != TOK_RB) {
1574              return -1;              result = -1;
1575                goto done;
1576            }            }
1577          }          }
1578          else {          else {
1579            return -1;            result = -1;
1580              goto done;
1581          }          }
1582        }        }
1583      }      }
1584      else {      else {
1585        return -1;        result = -1;
1586          goto done;
1587      }      }
1588    
1589      if (array == NULL) {      if (array == NULL) {
1590        return -1;        result = -1;
1591          goto done;
1592      }      }
1593    
1594      /* look up the file in the coverage table */      /* look up the file in the coverage table */
# Line 1154  Line 1598 
1598        char * id = xstrdup(id_bytes);        char * id = xstrdup(id_bytes);
1599        file_coverage = xmalloc(sizeof(FileCoverage));        file_coverage = xmalloc(sizeof(FileCoverage));
1600        file_coverage->id = id;        file_coverage->id = id;
1601        file_coverage->num_lines = array->pn_count - 1;        file_coverage->num_coverage_lines = array->pn_count;
1602        file_coverage->lines = xnew(int, array->pn_count);        file_coverage->coverage_lines = xnew(int, array->pn_count);
1603        if (source == NULL) {        file_coverage->source_lines = NULL;
         file_coverage->source = NULL;  
       }  
       else {  
         file_coverage->source = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(source->pn_atom)));  
       }  
1604    
1605        /* set coverage for all lines */        /* set coverage for all lines */
1606        uint32 i = 0;        uint32 i = 0;
1607        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++) {
1608          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1609            file_coverage->lines[i] = (int) element->pn_dval;            file_coverage->coverage_lines[i] = (int) element->pn_dval;
1610          }          }
1611          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1612            file_coverage->lines[i] = -1;            file_coverage->coverage_lines[i] = -1;
1613          }          }
1614          else {          else {
1615            return -1;            result = -1;
1616              goto done;
1617          }          }
1618        }        }
1619        assert(i == array->pn_count);        assert(i == array->pn_count);
# Line 1188  Line 1628 
1628      else {      else {
1629        /* sanity check */        /* sanity check */
1630        assert(strcmp(file_coverage->id, id_bytes) == 0);        assert(strcmp(file_coverage->id, id_bytes) == 0);
1631        if (file_coverage->num_lines != array->pn_count - 1) {        if (file_coverage->num_coverage_lines != array->pn_count) {
1632          return -2;          result = -2;
1633            goto done;
1634        }        }
1635    
1636        /* merge the coverage */        /* merge the coverage */
1637        uint32 i = 0;        uint32 i = 0;
1638        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++) {
1639          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1640            if (file_coverage->lines[i] == -1) {            if (file_coverage->coverage_lines[i] == -1) {
1641              return -2;              result = -2;
1642                goto done;
1643            }            }
1644            file_coverage->lines[i] += (int) element->pn_dval;            file_coverage->coverage_lines[i] += (int) element->pn_dval;
1645          }          }
1646          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1647            if (file_coverage->lines[i] != -1) {            if (file_coverage->coverage_lines[i] != -1) {
1648              return -2;              result = -2;
1649                goto done;
1650            }            }
1651          }          }
1652          else {          else {
1653            return -1;            result = -1;
1654              goto done;
1655          }          }
1656        }        }
1657        assert(i == array->pn_count);        assert(i == array->pn_count);
1658        }
1659    
1660        /* if this JSON file has source, use it */      /* if this JSON file has source, use it */
1661        if (file_coverage->source == NULL && source != NULL) {      if (file_coverage->source_lines == NULL && source != NULL) {
1662          file_coverage->source = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(source->pn_atom)));        file_coverage->num_source_lines = source->pn_count;
1663          file_coverage->source_lines = xnew(char *, source->pn_count);
1664          uint32 i = 0;
1665          for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {
1666            if (element->pn_type != TOK_STRING) {
1667              result = -1;
1668              goto done;
1669            }
1670            file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));
1671        }        }
1672          assert(i == source->pn_count);
1673      }      }
1674    }    }
1675    
1676    return 0;  done:
1677      js_FinishParseContext(context, &parse_context);
1678      return result;
1679  }  }

Legend:
Removed from v.169  
changed lines
  Added in v.341

  ViewVC Help
Powered by ViewVC 1.1.24