/[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 346 by siliconforks, Fri Oct 24 16:16:24 2008 UTC revision 361 by siliconforks, Sun Oct 26 05:28:03 2008 UTC
# Line 28  Line 28 
28  #include <jsapi.h>  #include <jsapi.h>
29  #include <jsarena.h>  #include <jsarena.h>
30  #include <jsatom.h>  #include <jsatom.h>
31    #include <jsemit.h>
32  #include <jsexn.h>  #include <jsexn.h>
33  #include <jsfun.h>  #include <jsfun.h>
34  #include <jsinterp.h>  #include <jsinterp.h>
# Line 51  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;
# Line 276  Line 279 
279    Stream_write_char(f, ')');    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);    assert(node->pn_type == TOK_FUNCTION);
321    assert(node->pn_arity == PN_FUNC);    assert(node->pn_arity == PN_FUNC);
# Line 314  Line 354 
354        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
355      }      }
356      JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]);      JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]);
357        if (param == NULL) {
358          fatal("unsupported parameter type for function: %s", file_id);
359        }
360      print_string_atom(param, f);      print_string_atom(param, f);
361    }    }
362    JS_FinishArenaPool(&pool);    JS_FinishArenaPool(&pool);
# Line 332  Line 375 
375  }  }
376    
377  static void instrument_function_call(JSParseNode * node, Stream * f) {  static void instrument_function_call(JSParseNode * node, Stream * f) {
378    if (node->pn_head->pn_type == TOK_FUNCTION) {    JSParseNode * function_node = node->pn_head;
379      /* it's a generator expression */    if (function_node->pn_type == TOK_FUNCTION) {
380      JSParseNode * function_node = node->pn_head;      JSObject * object = function_node->pn_funpob->object;
381      JSParseNode * lexical_scope_node = function_node->pn_body;      assert(JS_ObjectIsFunction(context, object));
382      assert(lexical_scope_node->pn_type == TOK_LEXICALSCOPE);      JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
383      assert(lexical_scope_node->pn_arity == PN_NAME);      assert(function);
384      JSParseNode * for_node = lexical_scope_node->pn_body;      assert(object == &function->object);
385      assert(for_node->pn_type == TOK_FOR);  
386      assert(for_node->pn_arity == PN_BINARY);      if (function_node->pn_flags & TCF_GENEXP_LAMBDA) {
387      JSParseNode * if_node = NULL;        /* it's a generator expression */
388      JSParseNode * semi_node;        Stream_write_char(f, '(');
389      switch (for_node->pn_right->pn_type) {        output_array_comprehension_or_generator_expression(function_node->pn_body, f);
390      case TOK_SEMI:        Stream_write_char(f, ')');
391        semi_node = for_node->pn_right;        return;
       break;  
     case TOK_IF:  
       if_node = for_node->pn_right;  
       assert(if_node->pn_arity == PN_TERNARY);  
       semi_node = if_node->pn_kid2;  
       assert(semi_node->pn_type == TOK_SEMI);  
       break;  
     default:  
       abort();  
       break;  
392      }      }
393      assert(semi_node->pn_arity == PN_UNARY);      else {
394      JSParseNode * yield_node = semi_node->pn_kid;        Stream_write_char(f, '(');
395      assert(yield_node->pn_type == TOK_YIELD);        instrument_expression(function_node, f);
     Stream_write_char(f, '(');  
     instrument_expression(yield_node->pn_kid, f);  
     Stream_write_char(f, ' ');  
     output_for_in(for_node, f);  
     if (if_node) {  
       Stream_write_string(f, " if (");  
       instrument_expression(if_node->pn_kid1, f);  
396        Stream_write_char(f, ')');        Stream_write_char(f, ')');
397      }      }
     Stream_write_char(f, ')');  
398    }    }
399    else {    else {
400      instrument_expression(node->pn_head, f);      instrument_expression(function_node, f);
401      Stream_write_char(f, '(');    }
402      for (struct JSParseNode * p = node->pn_head->pn_next; p != NULL; p = p->pn_next) {    Stream_write_char(f, '(');
403        if (p != node->pn_head->pn_next) {    for (struct JSParseNode * p = function_node->pn_next; p != NULL; p = p->pn_next) {
404          Stream_write_string(f, ", ");      if (p != node->pn_head->pn_next) {
405        }        Stream_write_string(f, ", ");
       instrument_expression(p, f);  
406      }      }
407      Stream_write_char(f, ')');      instrument_expression(p, f);
408    }    }
409      Stream_write_char(f, ')');
410  }  }
411    
412  static void instrument_declarations(JSParseNode * list, Stream * f) {  static void instrument_declarations(JSParseNode * list, Stream * f) {
# Line 526  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 583  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        bool is_keyword = (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF);        bool must_quote;
612        if (! is_keyword && 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 623  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 729  Line 768 
768      break;      break;
769    case TOK_YIELD:    case TOK_YIELD:
770      assert(node->pn_arity == PN_UNARY);      assert(node->pn_arity == PN_UNARY);
771      Stream_write_string(f, "yield ");      Stream_write_string(f, "yield");
772      instrument_expression(node->pn_kid, f);      if (node->pn_kid != NULL) {
773          Stream_write_char(f, ' ');
774          instrument_expression(node->pn_kid, f);
775        }
776      break;      break;
777    case TOK_ARRAYCOMP:    case TOK_ARRAYCOMP:
778      assert(node->pn_arity == PN_LIST);      assert(node->pn_arity == PN_LIST);
# Line 747  Line 789 
789          abort();          abort();
790          break;          break;
791        }        }
       assert(block_node->pn_type == TOK_LEXICALSCOPE);  
       assert(block_node->pn_arity == PN_NAME);  
       JSParseNode * for_node = block_node->pn_expr;  
       assert(for_node->pn_type == TOK_FOR);  
       assert(for_node->pn_arity == PN_BINARY);  
       JSParseNode * if_node = NULL;  
       JSParseNode * push_node;  
       switch (for_node->pn_right->pn_type) {  
       case TOK_ARRAYPUSH:  
         push_node = for_node->pn_right;  
         assert(push_node->pn_arity == PN_UNARY);  
         break;  
       case TOK_IF:  
         if_node = for_node->pn_right;  
         assert(if_node->pn_arity == PN_TERNARY);  
         push_node = if_node->pn_kid2;  
         break;  
       default:  
         abort();  
         break;  
       }  
792        Stream_write_char(f, '[');        Stream_write_char(f, '[');
793        instrument_expression(push_node->pn_kid, f);        output_array_comprehension_or_generator_expression(block_node, f);
       Stream_write_char(f, ' ');  
       output_for_in(for_node, f);  
       if (if_node) {  
         Stream_write_string(f, " if (");  
         instrument_expression(if_node->pn_kid1, f);  
         Stream_write_char(f, ')');  
       }  
794        Stream_write_char(f, ']');        Stream_write_char(f, ']');
795      }      }
796      break;      break;
# Line 868  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 1105  Line 1125 
1125        break;        break;
1126      }      }
1127      break;      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 1270  Line 1294 
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++) {

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

  ViewVC Help
Powered by ViewVC 1.1.24