/[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 245 by siliconforks, Sun Oct 5 18:05:12 2008 UTC revision 361 by siliconforks, Sun Oct 26 05:28:03 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 <jsemit.h>
32    #include <jsexn.h>
33  #include <jsfun.h>  #include <jsfun.h>
34  #include <jsinterp.h>  #include <jsinterp.h>
35    #include <jsiter.h>
36  #include <jsparse.h>  #include <jsparse.h>
37  #include <jsregexp.h>  #include <jsregexp.h>
38  #include <jsscope.h>  #include <jsscope.h>
# Line 48  Line 52 
52    struct IfDirective * next;    struct IfDirective * next;
53  };  };
54    
55    bool jscoverage_mozilla = false;
56    
57  static bool * exclusive_directives = NULL;  static bool * exclusive_directives = NULL;
58    
59  static JSRuntime * runtime = NULL;  static JSRuntime * runtime = NULL;
60  static JSContext * context = NULL;  static JSContext * context = NULL;
61  static JSObject * global = NULL;  static JSObject * global = NULL;
62    static JSVersion js_version = JSVERSION_ECMA_3;
63    
64  /*  /*
65  JSParseNode objects store line numbers starting from 1.  JSParseNode objects store line numbers starting from 1.
# Line 62  Line 69 
69  static char * lines = NULL;  static char * lines = NULL;
70  static uint16_t num_lines = 0;  static uint16_t num_lines = 0;
71    
72    void jscoverage_set_js_version(const char * version) {
73      js_version = atoi(version);
74    }
75    
76  void jscoverage_init(void) {  void jscoverage_init(void) {
77    runtime = JS_NewRuntime(8L * 1024L * 1024L);    runtime = JS_NewRuntime(8L * 1024L * 1024L);
78    if (runtime == NULL) {    if (runtime == NULL) {
# Line 73  Line 84 
84      fatal("cannot create context");      fatal("cannot create context");
85    }    }
86    
87      JS_SetVersion(context, js_version);
88    
89    global = JS_NewObject(context, NULL, NULL, NULL);    global = JS_NewObject(context, NULL, NULL, NULL);
90    if (global == NULL) {    if (global == NULL) {
91      fatal("cannot create global object");      fatal("cannot create global object");
# Line 88  Line 101 
101    JS_DestroyRuntime(runtime);    JS_DestroyRuntime(runtime);
102  }  }
103    
104    static void print_javascript(const jschar * characters, size_t num_characters, Stream * f) {
105      for (size_t i = 0; i < num_characters; i++) {
106        jschar c = characters[i];
107        /*
108        XXX does not handle no-break space, other unicode "space separator"
109        */
110        switch (c) {
111        case 0x9:
112        case 0xB:
113        case 0xC:
114          Stream_write_char(f, c);
115          break;
116        default:
117          if (32 <= c && c <= 126) {
118            Stream_write_char(f, c);
119          }
120          else {
121            Stream_printf(f, "\\u%04x", c);
122          }
123          break;
124        }
125      }
126    }
127    
128  static void print_string(JSString * s, Stream * f) {  static void print_string(JSString * s, Stream * f) {
129    size_t length = JSSTRING_LENGTH(s);    size_t length = JSSTRING_LENGTH(s);
130    jschar * characters = JSSTRING_CHARS(s);    jschar * characters = JSSTRING_CHARS(s);
# Line 122  Line 159 
159        case 0xa:        case 0xa:
160          Stream_write_string(f, "\\n");          Stream_write_string(f, "\\n");
161          break;          break;
162          /* IE doesn't support this */
163          /*
164        case 0xb:        case 0xb:
165          Stream_write_string(f, "\\v");          Stream_write_string(f, "\\v");
166          break;          break;
167          */
168        case 0xc:        case 0xc:
169          Stream_write_string(f, "\\f");          Stream_write_string(f, "\\f");
170          break;          break;
# Line 171  Line 211 
211    
212  static const char * get_op(uint8 op) {  static const char * get_op(uint8 op) {
213    switch(op) {    switch(op) {
214      case JSOP_OR:
215        return "||";
216      case JSOP_AND:
217        return "&&";
218    case JSOP_BITOR:    case JSOP_BITOR:
219      return "|";      return "|";
220    case JSOP_BITXOR:    case JSOP_BITXOR:
# Line 181  Line 225 
225      return "==";      return "==";
226    case JSOP_NE:    case JSOP_NE:
227      return "!=";      return "!=";
228    case JSOP_NEW_EQ:    case JSOP_STRICTEQ:
229      return "===";      return "===";
230    case JSOP_NEW_NE:    case JSOP_STRICTNE:
231      return "!==";      return "!==";
232    case JSOP_LT:    case JSOP_LT:
233      return "<";      return "<";
# Line 216  Line 260 
260    
261  static void instrument_expression(JSParseNode * node, Stream * f);  static void instrument_expression(JSParseNode * node, Stream * f);
262  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
263    static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
264    
265  enum FunctionType {  enum FunctionType {
266    FUNCTION_NORMAL,    FUNCTION_NORMAL,
267    FUNCTION_GETTER_OR_SETTER    FUNCTION_GETTER_OR_SETTER
268  };  };
269    
270    static void output_for_in(JSParseNode * node, Stream * f) {
271      assert(node->pn_type == TOK_FOR);
272      assert(node->pn_arity == PN_BINARY);
273      Stream_write_string(f, "for ");
274      if (node->pn_iflags & JSITER_FOREACH) {
275        Stream_write_string(f, "each ");
276      }
277      Stream_write_char(f, '(');
278      instrument_expression(node->pn_left, f);
279      Stream_write_char(f, ')');
280    }
281    
282    static void output_array_comprehension_or_generator_expression(JSParseNode * node, Stream * f) {
283      assert(node->pn_type == TOK_LEXICALSCOPE);
284      assert(node->pn_arity == PN_NAME);
285      JSParseNode * for_node = node->pn_expr;
286      assert(for_node->pn_type == TOK_FOR);
287      assert(for_node->pn_arity == PN_BINARY);
288      JSParseNode * p = for_node;
289      while (p->pn_type == TOK_FOR) {
290        p = p->pn_right;
291      }
292      JSParseNode * if_node = NULL;
293      if (p->pn_type == TOK_IF) {
294        if_node = p;
295        assert(if_node->pn_arity == PN_TERNARY);
296        p = if_node->pn_kid2;
297      }
298      assert(p->pn_arity == PN_UNARY);
299      p = p->pn_kid;
300      if (p->pn_type == TOK_YIELD) {
301        /* for generator expressions */
302        p = p->pn_kid;
303      }
304    
305      instrument_expression(p, f);
306      p = for_node;
307      while (p->pn_type == TOK_FOR) {
308        Stream_write_char(f, ' ');
309        output_for_in(p, f);
310        p = p->pn_right;
311      }
312      if (if_node) {
313        Stream_write_string(f, " if (");
314        instrument_expression(if_node->pn_kid1, f);
315        Stream_write_char(f, ')');
316      }
317    }
318    
319  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) {
320      assert(node->pn_type == TOK_FUNCTION);
321    assert(node->pn_arity == PN_FUNC);    assert(node->pn_arity == PN_FUNC);
322    assert(ATOM_IS_OBJECT(node->pn_funAtom));    JSObject * object = node->pn_funpob->object;
   JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom);  
323    assert(JS_ObjectIsFunction(context, object));    assert(JS_ObjectIsFunction(context, object));
324    JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);    JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
325    assert(function);    assert(function);
326    assert(object == function->object);    assert(object == &function->object);
327    Stream_printf(f, "%*s", indent, "");    Stream_printf(f, "%*s", indent, "");
328    if (type == FUNCTION_NORMAL) {    if (type == FUNCTION_NORMAL) {
329      Stream_write_string(f, "function");      Stream_write_string(f, "function");
# Line 241  Line 335 
335      print_string_atom(function->atom, f);      print_string_atom(function->atom, f);
336    }    }
337    
338    /* function parameters */    /*
339      function parameters - see JS_DecompileFunction in jsapi.cpp, which calls
340      js_DecompileFunction in jsopcode.cpp
341      */
342    Stream_write_string(f, "(");    Stream_write_string(f, "(");
343    JSAtom ** params = xnew(JSAtom *, function->nargs);    JSArenaPool pool;
344    for (int i = 0; i < function->nargs; i++) {    JS_INIT_ARENA_POOL(&pool, "instrument_function", 256, 1, &context->scriptStackQuota);
345      /* initialize to NULL for sanity check */    jsuword * local_names = NULL;
346      params[i] = NULL;    if (JS_GET_LOCAL_NAME_COUNT(function)) {
347    }      local_names = js_GetLocalNameArray(context, function, &pool);
348    JSScope * scope = OBJ_SCOPE(object);      if (local_names == NULL) {
349    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;  
350      }      }
     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);  
351    }    }
352    for (int i = 0; i < function->nargs; i++) {    for (int i = 0; i < function->nargs; i++) {
     assert(params[i] != NULL);  
353      if (i > 0) {      if (i > 0) {
354        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
355      }      }
356      if (ATOM_IS_STRING(params[i])) {      JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]);
357        print_string_atom(params[i], f);      if (param == NULL) {
358          fatal("unsupported parameter type for function: %s", file_id);
359      }      }
360        print_string_atom(param, f);
361    }    }
362      JS_FinishArenaPool(&pool);
363    Stream_write_string(f, ") {\n");    Stream_write_string(f, ") {\n");
   free(params);  
364    
365    /* function body */    /* function body */
366    instrument_statement(node->pn_body, f, indent + 2, false);    if (function->flags & JSFUN_EXPR_CLOSURE) {
367        /* expression closure */
368        output_statement(node->pn_body, f, indent + 2, false);
369      }
370      else {
371        instrument_statement(node->pn_body, f, indent + 2, false);
372      }
373    
374    Stream_write_string(f, "}\n");    Stream_write_string(f, "}\n");
375  }  }
376    
377  static void instrument_function_call(JSParseNode * node, Stream * f) {  static void instrument_function_call(JSParseNode * node, Stream * f) {
378    instrument_expression(node->pn_head, f);    JSParseNode * function_node = node->pn_head;
379      if (function_node->pn_type == TOK_FUNCTION) {
380        JSObject * object = function_node->pn_funpob->object;
381        assert(JS_ObjectIsFunction(context, object));
382        JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
383        assert(function);
384        assert(object == &function->object);
385    
386        if (function_node->pn_flags & TCF_GENEXP_LAMBDA) {
387          /* it's a generator expression */
388          Stream_write_char(f, '(');
389          output_array_comprehension_or_generator_expression(function_node->pn_body, f);
390          Stream_write_char(f, ')');
391          return;
392        }
393        else {
394          Stream_write_char(f, '(');
395          instrument_expression(function_node, f);
396          Stream_write_char(f, ')');
397        }
398      }
399      else {
400        instrument_expression(function_node, f);
401      }
402    Stream_write_char(f, '(');    Stream_write_char(f, '(');
403    for (struct JSParseNode * p = node->pn_head->pn_next; p != NULL; p = p->pn_next) {    for (struct JSParseNode * p = function_node->pn_next; p != NULL; p = p->pn_next) {
404      if (p != node->pn_head->pn_next) {      if (p != node->pn_head->pn_next) {
405        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
406      }      }
# Line 288  Line 409 
409    Stream_write_char(f, ')');    Stream_write_char(f, ')');
410  }  }
411    
412    static void instrument_declarations(JSParseNode * list, Stream * f) {
413      assert(list->pn_arity == PN_LIST);
414      for (JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
415        switch (p->pn_type) {
416        case TOK_NAME:
417          assert(p->pn_arity == PN_NAME);
418          if (p != list->pn_head) {
419            Stream_write_string(f, ", ");
420          }
421          print_string_atom(p->pn_atom, f);
422          if (p->pn_expr != NULL) {
423            Stream_write_string(f, " = ");
424            instrument_expression(p->pn_expr, f);
425          }
426          break;
427        case TOK_ASSIGN:
428        case TOK_RB:
429        case TOK_RC:
430          /* destructuring */
431          instrument_expression(p, f);
432          break;
433        default:
434          abort();
435          break;
436        }
437      }
438    }
439    
440  /*  /*
441  See <Expressions> in jsparse.h.  See <Expressions> in jsparse.h.
442  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
# Line 346  Line 495 
495      instrument_expression(node->pn_kid3, f);      instrument_expression(node->pn_kid3, f);
496      break;      break;
497    case TOK_OR:    case TOK_OR:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " || ");  
     instrument_expression(node->pn_right, f);  
     break;  
498    case TOK_AND:    case TOK_AND:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " && ");  
     instrument_expression(node->pn_right, f);  
     break;  
499    case TOK_BITOR:    case TOK_BITOR:
500    case TOK_BITXOR:    case TOK_BITXOR:
501    case TOK_BITAND:    case TOK_BITAND:
# Line 410  Line 551 
551        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
552        break;        break;
553      default:      default:
554        abort();        fatal("%s: unknown operator (%d) in file", file_id, node->pn_op);
555        break;        break;
556      }      }
557      break;      break;
# Line 467  Line 608 
608      assert(ATOM_IS_STRING(node->pn_atom));      assert(ATOM_IS_STRING(node->pn_atom));
609      {      {
610        JSString * s = ATOM_TO_STRING(node->pn_atom);        JSString * s = ATOM_TO_STRING(node->pn_atom);
611        /* XXX - semantics changed in 1.7 */        bool must_quote;
612        if (! ATOM_KEYWORD(node->pn_atom) && js_IsIdentifier(s)) {        if (JSSTRING_LENGTH(s) == 0) {
613          Stream_write_char(f, '.');          must_quote = true;
614          print_string_atom(node->pn_atom, f);        }
615          else if (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF) {
616            must_quote = true;
617          }
618          else if (! js_IsIdentifier(s)) {
619            must_quote = true;
620        }        }
621        else {        else {
622            must_quote = false;
623          }
624          if (must_quote) {
625          Stream_write_char(f, '[');          Stream_write_char(f, '[');
626          print_quoted_string_atom(node->pn_atom, f);          print_quoted_string_atom(node->pn_atom, f);
627          Stream_write_char(f, ']');          Stream_write_char(f, ']');
628        }        }
629          else {
630            Stream_write_char(f, '.');
631            print_string_atom(node->pn_atom, f);
632          }
633      }      }
634      break;      break;
635    case TOK_LB:    case TOK_LB:
# Line 507  Line 660 
660    case TOK_RC:    case TOK_RC:
661      Stream_write_char(f, '{');      Stream_write_char(f, '{');
662      for (struct JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) {      for (struct JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) {
663        assert(p->pn_type == TOK_COLON);        if (p->pn_type != TOK_COLON) {
664            fatal("unsupported node type in file %s: %d", file_id, p->pn_type);
665          }
666        if (p != node->pn_head) {        if (p != node->pn_head) {
667          Stream_write_string(f, ", ");          Stream_write_string(f, ", ");
668        }        }
# Line 515  Line 670 
670        /* check whether this is a getter or setter */        /* check whether this is a getter or setter */
671        switch (p->pn_op) {        switch (p->pn_op) {
672        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;  
673        case JSOP_SETTER:        case JSOP_SETTER:
674          Stream_write_string(f, "set ");          if (p->pn_op == JSOP_GETTER) {
675              Stream_write_string(f, "get ");
676            }
677            else {
678              Stream_write_string(f, "set ");
679            }
680          instrument_expression(p->pn_left, f);          instrument_expression(p->pn_left, f);
681            if (p->pn_right->pn_type != TOK_FUNCTION) {
682              fatal("parse error: expected function");
683            }
684          instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);          instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);
685          break;          break;
686        default:        default:
# Line 544  Line 703 
703    case TOK_STRING:    case TOK_STRING:
704      print_quoted_string_atom(node->pn_atom, f);      print_quoted_string_atom(node->pn_atom, f);
705      break;      break;
706    case TOK_OBJECT:    case TOK_REGEXP:
707      switch (node->pn_op) {      assert(node->pn_op == JSOP_REGEXP);
708      case JSOP_OBJECT:      {
709        /* I assume this is JSOP_REGEXP */        JSObject * object = node->pn_pob->object;
710        abort();        jsval result;
711        break;        js_regexp_toString(context, object, &result);
712      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;  
713      }      }
714      break;      break;
715    case TOK_NUMBER:    case TOK_NUMBER:
# Line 607  Line 755 
755      Stream_write_string(f, " in ");      Stream_write_string(f, " in ");
756      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
757      break;      break;
758    default:    case TOK_LEXICALSCOPE:
759      fatal("unsupported node type in file %s: %d", file_id, node->pn_type);      assert(node->pn_arity == PN_NAME);
760    }      assert(node->pn_expr->pn_type == TOK_LET);
761  }      assert(node->pn_expr->pn_arity == PN_BINARY);
762        Stream_write_string(f, "let(");
763  static void instrument_var_statement(JSParseNode * node, Stream * f, int indent) {      assert(node->pn_expr->pn_left->pn_type == TOK_LP);
764    assert(node->pn_arity == PN_LIST);      assert(node->pn_expr->pn_left->pn_arity == PN_LIST);
765    Stream_printf(f, "%*s", indent, "");      instrument_declarations(node->pn_expr->pn_left, f);
766    Stream_write_string(f, "var ");      Stream_write_string(f, ") ");
767    for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) {      instrument_expression(node->pn_expr->pn_right, f);
768      assert(p->pn_type == TOK_NAME);      break;
769      assert(p->pn_arity == PN_NAME);    case TOK_YIELD:
770      if (p != node->pn_head) {      assert(node->pn_arity == PN_UNARY);
771        Stream_write_string(f, ", ");      Stream_write_string(f, "yield");
772        if (node->pn_kid != NULL) {
773          Stream_write_char(f, ' ');
774          instrument_expression(node->pn_kid, f);
775      }      }
776      print_string_atom(p->pn_atom, f);      break;
777      if (p->pn_expr != NULL) {    case TOK_ARRAYCOMP:
778        Stream_write_string(f, " = ");      assert(node->pn_arity == PN_LIST);
779        instrument_expression(p->pn_expr, f);      {
780          JSParseNode * block_node;
781          switch (node->pn_count) {
782          case 1:
783            block_node = node->pn_head;
784            break;
785          case 2:
786            block_node = node->pn_head->pn_next;
787            break;
788          default:
789            abort();
790            break;
791          }
792          Stream_write_char(f, '[');
793          output_array_comprehension_or_generator_expression(block_node, f);
794          Stream_write_char(f, ']');
795      }      }
796        break;
797      case TOK_VAR:
798        assert(node->pn_arity == PN_LIST);
799        Stream_write_string(f, "var ");
800        instrument_declarations(node, f);
801        break;
802      case TOK_LET:
803        assert(node->pn_arity == PN_LIST);
804        Stream_write_string(f, "let ");
805        instrument_declarations(node, f);
806        break;
807      default:
808        fatal("unsupported node type in file %s: %d", file_id, node->pn_type);
809    }    }
810  }  }
811    
# Line 703  Line 882 
882      Stream_write_string(f, "switch (");      Stream_write_string(f, "switch (");
883      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
884      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
885      for (struct JSParseNode * p = node->pn_right->pn_head; p != NULL; p = p->pn_next) {      {
886        Stream_printf(f, "%*s", indent, "");        JSParseNode * list = node->pn_right;
887        switch (p->pn_type) {        if (list->pn_type == TOK_LEXICALSCOPE) {
888        case TOK_CASE:          list = list->pn_expr;
889          Stream_write_string(f, "case ");        }
890          instrument_expression(p->pn_left, f);        for (struct JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
891          Stream_write_string(f, ":\n");          Stream_printf(f, "%*s", indent, "");
892          break;          switch (p->pn_type) {
893        case TOK_DEFAULT:          case TOK_CASE:
894          Stream_write_string(f, "default:\n");            Stream_write_string(f, "case ");
895          break;            instrument_expression(p->pn_left, f);
896        default:            Stream_write_string(f, ":\n");
897          abort();            break;
898          break;          case TOK_DEFAULT:
899              Stream_write_string(f, "default:\n");
900              break;
901            default:
902              abort();
903              break;
904            }
905            instrument_statement(p->pn_right, f, indent + 2, false);
906        }        }
       instrument_statement(p->pn_right, f, indent + 2, false);  
907      }      }
908      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
909      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
# Line 750  Line 935 
935    case TOK_FOR:    case TOK_FOR:
936      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
937      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
     Stream_write_string(f, "for (");  
938      switch (node->pn_left->pn_type) {      switch (node->pn_left->pn_type) {
939      case TOK_IN:      case TOK_IN:
940        /* for/in */        /* for/in */
941        assert(node->pn_left->pn_arity == PN_BINARY);        assert(node->pn_left->pn_arity == PN_BINARY);
942        switch (node->pn_left->pn_left->pn_type) {        output_for_in(node, f);
       case TOK_VAR:  
         instrument_var_statement(node->pn_left->pn_left, f, 0);  
         break;  
       case TOK_NAME:  
         instrument_expression(node->pn_left->pn_left, f);  
         break;  
       default:  
         /* this is undocumented: for (x.value in y) */  
         instrument_expression(node->pn_left->pn_left, f);  
         break;  
 /*  
       default:  
         fprintf(stderr, "unexpected node type: %d\n", node->pn_left->pn_left->pn_type);  
         abort();  
         break;  
 */  
       }  
       Stream_write_string(f, " in ");  
       instrument_expression(node->pn_left->pn_right, f);  
943        break;        break;
944      case TOK_RESERVED:      case TOK_RESERVED:
945        /* for (;;) */        /* for (;;) */
946        assert(node->pn_left->pn_arity == PN_TERNARY);        assert(node->pn_left->pn_arity == PN_TERNARY);
947          Stream_write_string(f, "for (");
948        if (node->pn_left->pn_kid1) {        if (node->pn_left->pn_kid1) {
949          if (node->pn_left->pn_kid1->pn_type == TOK_VAR) {          instrument_expression(node->pn_left->pn_kid1, f);
           instrument_var_statement(node->pn_left->pn_kid1, f, 0);  
         }  
         else {  
           instrument_expression(node->pn_left->pn_kid1, f);  
         }  
950        }        }
951        Stream_write_string(f, ";");        Stream_write_string(f, ";");
952        if (node->pn_left->pn_kid2) {        if (node->pn_left->pn_kid2) {
# Line 797  Line 958 
958          Stream_write_char(f, ' ');          Stream_write_char(f, ' ');
959          instrument_expression(node->pn_left->pn_kid3, f);          instrument_expression(node->pn_left->pn_kid3, f);
960        }        }
961          Stream_write_char(f, ')');
962        break;        break;
963      default:      default:
964        abort();        abort();
965        break;        break;
966      }      }
967      Stream_write_string(f, ") {\n");      Stream_write_string(f, " {\n");
968      instrument_statement(node->pn_right, f, indent + 2, false);      instrument_statement(node->pn_right, f, indent + 2, false);
969      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
970      break;      break;
# Line 819  Line 981 
981      instrument_statement(node->pn_kid1, f, indent + 2, false);      instrument_statement(node->pn_kid1, f, indent + 2, false);
982      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
983      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
984      {      if (node->pn_kid2) {
985        for (JSParseNode * catch = node->pn_kid2; catch != NULL; catch = catch->pn_kid2) {        assert(node->pn_kid2->pn_type == TOK_RESERVED);
986          for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) {
987            assert(scope->pn_type == TOK_LEXICALSCOPE);
988            JSParseNode * catch = scope->pn_expr;
989          assert(catch->pn_type == TOK_CATCH);          assert(catch->pn_type == TOK_CATCH);
990          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
991          Stream_write_string(f, "catch (");          Stream_write_string(f, "catch (");
992            /* this may not be a name - destructuring assignment */
993            /*
994          assert(catch->pn_kid1->pn_arity == PN_NAME);          assert(catch->pn_kid1->pn_arity == PN_NAME);
995          print_string_atom(catch->pn_kid1->pn_atom, f);          print_string_atom(catch->pn_kid1->pn_atom, f);
996          if (catch->pn_kid1->pn_expr) {          */
997            instrument_expression(catch->pn_kid1, f);
998            if (catch->pn_kid2) {
999            Stream_write_string(f, " if ");            Stream_write_string(f, " if ");
1000            instrument_expression(catch->pn_kid1->pn_expr, f);            instrument_expression(catch->pn_kid2, f);
1001          }          }
1002          Stream_write_string(f, ") {\n");          Stream_write_string(f, ") {\n");
1003          instrument_statement(catch->pn_kid3, f, indent + 2, false);          instrument_statement(catch->pn_kid3, f, indent + 2, false);
# Line 870  Line 1039 
1039      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
1040      break;      break;
1041    case TOK_VAR:    case TOK_VAR:
1042      instrument_var_statement(node, f, indent);      Stream_printf(f, "%*s", indent, "");
1043        instrument_expression(node, f);
1044      Stream_write_string(f, ";\n");      Stream_write_string(f, ";\n");
1045      break;      break;
1046    case TOK_RETURN:    case TOK_RETURN:
# Line 905  Line 1075 
1075      */      */
1076      output_statement(node->pn_expr, f, indent, false);      output_statement(node->pn_expr, f, indent, false);
1077      break;      break;
1078      case TOK_LEXICALSCOPE:
1079        /* let statement */
1080        assert(node->pn_arity == PN_NAME);
1081        switch (node->pn_expr->pn_type) {
1082        case TOK_LET:
1083          /* let statement */
1084          assert(node->pn_expr->pn_arity == PN_BINARY);
1085          instrument_statement(node->pn_expr, f, indent, false);
1086          break;
1087        case TOK_LC:
1088          /* block */
1089          Stream_printf(f, "%*s", indent, "");
1090          Stream_write_string(f, "{\n");
1091          instrument_statement(node->pn_expr, f, indent + 2, false);
1092          Stream_printf(f, "%*s", indent, "");
1093          Stream_write_string(f, "}\n");
1094          break;
1095        case TOK_FOR:
1096          instrument_statement(node->pn_expr, f, indent, false);
1097          break;
1098        default:
1099          abort();
1100          break;
1101        }
1102        break;
1103      case TOK_LET:
1104        switch (node->pn_arity) {
1105        case PN_BINARY:
1106          /* let statement */
1107          Stream_printf(f, "%*s", indent, "");
1108          Stream_write_string(f, "let (");
1109          assert(node->pn_left->pn_type == TOK_LP);
1110          assert(node->pn_left->pn_arity == PN_LIST);
1111          instrument_declarations(node->pn_left, f);
1112          Stream_write_string(f, ") {\n");
1113          instrument_statement(node->pn_right, f, indent + 2, false);
1114          Stream_printf(f, "%*s", indent, "");
1115          Stream_write_string(f, "}\n");
1116          break;
1117        case PN_LIST:
1118          /* let definition */
1119          Stream_printf(f, "%*s", indent, "");
1120          instrument_expression(node, f);
1121          Stream_write_string(f, ";\n");
1122          break;
1123        default:
1124          abort();
1125          break;
1126        }
1127        break;
1128      case TOK_DEBUGGER:
1129        Stream_printf(f, "%*s", indent, "");
1130        Stream_write_string(f, "debugger;\n");
1131        break;
1132    default:    default:
1133      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);
1134    }    }
# Line 916  Line 1140 
1140  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
1141  */  */
1142  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
1143    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC && node->pn_type != TOK_LEXICALSCOPE) {
1144      uint16_t line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
1145      if (line > num_lines) {      if (line > num_lines) {
1146        fatal("%s: script contains more than 65,535 lines", file_id);        fatal("%s: script contains more than 65,535 lines", file_id);
# Line 966  Line 1190 
1190    return true;    return true;
1191  }  }
1192    
1193    static void error_reporter(JSContext * context, const char * message, JSErrorReport * report) {
1194      fprintf(stderr, "jscoverage: parse error: line %u: %s\n", report->lineno, message);
1195    }
1196    
1197  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) {
1198    file_id = id;    file_id = id;
1199    
1200    /* scan the javascript */    /* parse the javascript */
1201    JSTokenStream * token_stream = js_NewTokenStream(context, characters, num_characters, NULL, 1, NULL);    JSParseContext parse_context;
1202    if (token_stream == NULL) {    if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) {
1203      fatal("cannot create token stream from file: %s", file_id);      fatal("cannot create token stream from file: %s", file_id);
1204    }    }
1205      JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);
1206    /* parse the javascript */    JSParseNode * node = js_ParseScript(context, global, &parse_context);
   JSParseNode * node = js_ParseTokenStream(context, global, token_stream);  
1207    if (node == NULL) {    if (node == NULL) {
1208        js_ReportUncaughtException(context);
1209      fatal("parse error in file: %s", file_id);      fatal("parse error in file: %s", file_id);
1210    }    }
1211      JS_SetErrorReporter(context, old_error_reporter);
1212    num_lines = node->pn_pos.end.lineno;    num_lines = node->pn_pos.end.lineno;
1213    lines = xmalloc(num_lines);    lines = xmalloc(num_lines);
1214    for (unsigned int i = 0; i < num_lines; i++) {    for (unsigned int i = 0; i < num_lines; i++) {
# Line 1061  Line 1290 
1290    
1291    Stream * instrumented = Stream_new(0);    Stream * instrumented = Stream_new(0);
1292    instrument_statement(node, instrumented, 0, false);    instrument_statement(node, instrumented, 0, false);
1293      js_FinishParseContext(context, &parse_context);
1294    
1295    /* write line number info to the output */    /* write line number info to the output */
1296    Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");    Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");
1297    Stream_write_string(output, "if (! top._$jscoverage) {\n  top._$jscoverage = {};\n}\n");    if (jscoverage_mozilla) {
1298    Stream_write_string(output, "var _$jscoverage = top._$jscoverage;\n");      Stream_write_string(output, "try {\n");
1299        Stream_write_string(output, "  Components.utils.import('resource://gre/modules/jscoverage.jsm');\n");
1300        Stream_printf(output, "  dump('%s: successfully imported jscoverage module\\n');\n", id);
1301        Stream_write_string(output, "}\n");
1302        Stream_write_string(output, "catch (e) {\n");
1303        Stream_write_string(output, "  _$jscoverage = {};\n");
1304        Stream_printf(output, "  dump('%s: failed to import jscoverage module - coverage not available for this file\\n');\n", id);
1305        Stream_write_string(output, "}\n");
1306      }
1307      else {
1308        Stream_write_string(output, "if (! top._$jscoverage) {\n  top._$jscoverage = {};\n}\n");
1309        Stream_write_string(output, "var _$jscoverage = top._$jscoverage;\n");
1310      }
1311    Stream_printf(output, "if (! _$jscoverage['%s']) {\n", file_id);    Stream_printf(output, "if (! _$jscoverage['%s']) {\n", file_id);
1312    Stream_printf(output, "  _$jscoverage['%s'] = [];\n", file_id);    Stream_printf(output, "  _$jscoverage['%s'] = [];\n", file_id);
1313    for (int i = 0; i < num_lines; i++) {    for (int i = 0; i < num_lines; i++) {
# Line 1090  Line 1332 
1332    /* conditionals */    /* conditionals */
1333    for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) {    for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) {
1334      Stream_write_string(output, "if (!(");      Stream_write_string(output, "if (!(");
1335      for (const jschar * p = if_directive->condition_start; p < if_directive->condition_end; p++) {      print_javascript(if_directive->condition_start, if_directive->condition_end - if_directive->condition_start, output);
       jschar c = *p;  
       if (c == '\t' || (32 <= c && c <= 126)) {  
         Stream_write_char(output, c);  
       }  
       else {  
         Stream_printf(output, "\\u%04x", c);  
       }  
     }  
1336      Stream_write_string(output, ")) {\n");      Stream_write_string(output, ")) {\n");
1337      Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line);      Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line);
1338      Stream_write_string(output, "}\n");      Stream_write_string(output, "}\n");
# Line 1149  Line 1383 
1383            /* line feed (new line) */            /* line feed (new line) */
1384            done = true;            done = true;
1385            break;            break;
1386            /* IE doesn't support this */
1387            /*
1388          case 0xb:          case 0xb:
           /* vertical tab */  
1389            Stream_write_string(output, "\\v");            Stream_write_string(output, "\\v");
1390            break;            break;
1391            */
1392          case 0xc:          case 0xc:
1393            /* form feed */            /* form feed */
1394            Stream_write_string(output, "\\f");            Stream_write_string(output, "\\f");
# Line 1207  Line 1443 
1443            /* line feed (new line) */            /* line feed (new line) */
1444            done = true;            done = true;
1445            break;            break;
1446            /* IE doesn't support this */
1447            /*
1448          case 0xb:          case 0xb:
           /* vertical tab */  
1449            Stream_write_string(output, "\\v");            Stream_write_string(output, "\\v");
1450            break;            break;
1451            */
1452          case 0xc:          case 0xc:
1453            /* form feed */            /* form feed */
1454            Stream_write_string(output, "\\f");            Stream_write_string(output, "\\f");
# Line 1337  Line 1575 
1575  }  }
1576    
1577  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) {
1578      int result = 0;
1579    
1580    jschar * base = js_InflateString(context, (char *) json, &length);    jschar * base = js_InflateString(context, (char *) json, &length);
1581    if (base == NULL) {    if (base == NULL) {
1582      fatal("out of memory");      fatal("out of memory");
# Line 1349  Line 1589 
1589    
1590    JS_free(context, base);    JS_free(context, base);
1591    
1592    JSTokenStream * token_stream = js_NewTokenStream(context, parenthesized_json, length + 2, NULL, 1, NULL);    JSParseContext parse_context;
1593    if (token_stream == NULL) {    if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) {
1594      fatal("cannot create token stream");      free(parenthesized_json);
1595        return -1;
1596    }    }
1597      JSParseNode * root = js_ParseScript(context, global, &parse_context);
   JSParseNode * root = js_ParseTokenStream(context, global, token_stream);  
1598    free(parenthesized_json);    free(parenthesized_json);
1599    if (root == NULL) {    if (root == NULL) {
1600      return -1;      result = -1;
1601        goto done;
1602    }    }
1603    
1604    /* root node must be TOK_LC */    /* root node must be TOK_LC */
1605    if (root->pn_type != TOK_LC) {    if (root->pn_type != TOK_LC) {
1606      return -1;      result = -1;
1607        goto done;
1608    }    }
1609    JSParseNode * semi = root->pn_u.list.head;    JSParseNode * semi = root->pn_u.list.head;
1610    
1611    /* 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 */
1612    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {
1613      return -1;      result = -1;
1614        goto done;
1615    }    }
1616    JSParseNode * parenthesized = semi->pn_kid;    JSParseNode * parenthesized = semi->pn_kid;
1617    
1618    /* this must be a parenthesized expression */    /* this must be a parenthesized expression */
1619    if (parenthesized->pn_type != TOK_RP) {    if (parenthesized->pn_type != TOK_RP) {
1620      return -1;      result = -1;
1621        goto done;
1622    }    }
1623    JSParseNode * object = parenthesized->pn_kid;    JSParseNode * object = parenthesized->pn_kid;
1624    
1625    /* this must be an object literal */    /* this must be an object literal */
1626    if (object->pn_type != TOK_RC) {    if (object->pn_type != TOK_RC) {
1627      return -1;      result = -1;
1628        goto done;
1629    }    }
1630    
1631    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {
1632      /* every element of this list must be TOK_COLON */      /* every element of this list must be TOK_COLON */
1633      if (p->pn_type != TOK_COLON) {      if (p->pn_type != TOK_COLON) {
1634        return -1;        result = -1;
1635          goto done;
1636      }      }
1637    
1638      /* the key must be a string representing the file */      /* the key must be a string representing the file */
1639      JSParseNode * key = p->pn_left;      JSParseNode * key = p->pn_left;
1640      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {
1641        return -1;        result = -1;
1642          goto done;
1643      }      }
1644      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));
1645    
1646      /* the value must be an object literal OR an array */      /* the value must be an object literal OR an array */
1647      JSParseNode * value = p->pn_right;      JSParseNode * value = p->pn_right;
1648      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {
1649        return -1;        result = -1;
1650          goto done;
1651      }      }
1652    
1653      JSParseNode * array = NULL;      JSParseNode * array = NULL;
# Line 1411  Line 1659 
1659      else if (value->pn_type == TOK_RC) {      else if (value->pn_type == TOK_RC) {
1660        /* an object literal */        /* an object literal */
1661        if (value->pn_count != 2) {        if (value->pn_count != 2) {
1662          return -1;          result = -1;
1663            goto done;
1664        }        }
1665        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {
1666          if (element->pn_type != TOK_COLON) {          if (element->pn_type != TOK_COLON) {
1667            return -1;            result = -1;
1668              goto done;
1669          }          }
1670          JSParseNode * left = element->pn_left;          JSParseNode * left = element->pn_left;
1671          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {
1672            return -1;            result = -1;
1673              goto done;
1674          }          }
1675          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));
1676          if (strcmp(s, "coverage") == 0) {          if (strcmp(s, "coverage") == 0) {
1677            array = element->pn_right;            array = element->pn_right;
1678            if (array->pn_type != TOK_RB) {            if (array->pn_type != TOK_RB) {
1679              return -1;              result = -1;
1680                goto done;
1681            }            }
1682          }          }
1683          else if (strcmp(s, "source") == 0) {          else if (strcmp(s, "source") == 0) {
1684            source = element->pn_right;            source = element->pn_right;
1685            if (source->pn_type != TOK_RB) {            if (source->pn_type != TOK_RB) {
1686              return -1;              result = -1;
1687                goto done;
1688            }            }
1689          }          }
1690          else {          else {
1691            return -1;            result = -1;
1692              goto done;
1693          }          }
1694        }        }
1695      }      }
1696      else {      else {
1697        return -1;        result = -1;
1698          goto done;
1699      }      }
1700    
1701      if (array == NULL) {      if (array == NULL) {
1702        return -1;        result = -1;
1703          goto done;
1704      }      }
1705    
1706      /* look up the file in the coverage table */      /* look up the file in the coverage table */
# Line 1468  Line 1724 
1724            file_coverage->coverage_lines[i] = -1;            file_coverage->coverage_lines[i] = -1;
1725          }          }
1726          else {          else {
1727            return -1;            result = -1;
1728              goto done;
1729          }          }
1730        }        }
1731        assert(i == array->pn_count);        assert(i == array->pn_count);
# Line 1484  Line 1741 
1741        /* sanity check */        /* sanity check */
1742        assert(strcmp(file_coverage->id, id_bytes) == 0);        assert(strcmp(file_coverage->id, id_bytes) == 0);
1743        if (file_coverage->num_coverage_lines != array->pn_count) {        if (file_coverage->num_coverage_lines != array->pn_count) {
1744          return -2;          result = -2;
1745            goto done;
1746        }        }
1747    
1748        /* merge the coverage */        /* merge the coverage */
# Line 1492  Line 1750 
1750        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++) {
1751          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1752            if (file_coverage->coverage_lines[i] == -1) {            if (file_coverage->coverage_lines[i] == -1) {
1753              return -2;              result = -2;
1754                goto done;
1755            }            }
1756            file_coverage->coverage_lines[i] += (int) element->pn_dval;            file_coverage->coverage_lines[i] += (int) element->pn_dval;
1757          }          }
1758          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1759            if (file_coverage->coverage_lines[i] != -1) {            if (file_coverage->coverage_lines[i] != -1) {
1760              return -2;              result = -2;
1761                goto done;
1762            }            }
1763          }          }
1764          else {          else {
1765            return -1;            result = -1;
1766              goto done;
1767          }          }
1768        }        }
1769        assert(i == array->pn_count);        assert(i == array->pn_count);
# Line 1515  Line 1776 
1776        uint32 i = 0;        uint32 i = 0;
1777        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++) {
1778          if (element->pn_type != TOK_STRING) {          if (element->pn_type != TOK_STRING) {
1779            return -1;            result = -1;
1780              goto done;
1781          }          }
1782          file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));          file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));
1783        }        }
# Line 1523  Line 1785 
1785      }      }
1786    }    }
1787    
1788    return 0;  done:
1789      js_FinishParseContext(context, &parse_context);
1790      return result;
1791  }  }

Legend:
Removed from v.245  
changed lines
  Added in v.361

  ViewVC Help
Powered by ViewVC 1.1.24