/[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 341 by siliconforks, Fri Oct 24 16:14:43 2008 UTC revision 351 by siliconforks, Fri Oct 24 16:17:59 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 257  Line 258 
258    
259  static void instrument_expression(JSParseNode * node, Stream * f);  static void instrument_expression(JSParseNode * node, Stream * f);
260  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);
261    static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
262    
263  enum FunctionType {  enum FunctionType {
264    FUNCTION_NORMAL,    FUNCTION_NORMAL,
265    FUNCTION_GETTER_OR_SETTER    FUNCTION_GETTER_OR_SETTER
266  };  };
267    
268    static void output_for_in(JSParseNode * node, Stream * f) {
269      assert(node->pn_type == TOK_FOR);
270      assert(node->pn_arity == PN_BINARY);
271      Stream_write_string(f, "for ");
272      if (node->pn_iflags & JSITER_FOREACH) {
273        Stream_write_string(f, "each ");
274      }
275      Stream_write_char(f, '(');
276      instrument_expression(node->pn_left, f);
277      Stream_write_char(f, ')');
278    }
279    
280  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) {
281    assert(node->pn_type == TOK_FUNCTION);    assert(node->pn_type == TOK_FUNCTION);
282    assert(node->pn_arity == PN_FUNC);    assert(node->pn_arity == PN_FUNC);
# Line 307  Line 321 
321    Stream_write_string(f, ") {\n");    Stream_write_string(f, ") {\n");
322    
323    /* function body */    /* function body */
324    instrument_statement(node->pn_body, f, indent + 2, false);    if (function->flags & JSFUN_EXPR_CLOSURE) {
325        /* expression closure */
326        output_statement(node->pn_body, f, indent + 2, false);
327      }
328      else {
329        instrument_statement(node->pn_body, f, indent + 2, false);
330      }
331    
332    Stream_write_string(f, "}\n");    Stream_write_string(f, "}\n");
333  }  }
334    
335  static void instrument_function_call(JSParseNode * node, Stream * f) {  static void instrument_function_call(JSParseNode * node, Stream * f) {
336    instrument_expression(node->pn_head, f);    JSParseNode * function_node = node->pn_head;
337      if (function_node->pn_type == TOK_FUNCTION) {
338        JSObject * object = function_node->pn_funpob->object;
339        assert(JS_ObjectIsFunction(context, object));
340        JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
341        assert(function);
342        assert(object == &function->object);
343    
344        if (function_node->pn_flags & TCF_GENEXP_LAMBDA) {
345          /* it's a generator expression */
346          JSParseNode * lexical_scope_node = function_node->pn_body;
347          assert(lexical_scope_node->pn_type == TOK_LEXICALSCOPE);
348          assert(lexical_scope_node->pn_arity == PN_NAME);
349          JSParseNode * for_node = lexical_scope_node->pn_body;
350          assert(for_node->pn_type == TOK_FOR);
351          assert(for_node->pn_arity == PN_BINARY);
352          JSParseNode * if_node = NULL;
353          JSParseNode * semi_node;
354          switch (for_node->pn_right->pn_type) {
355          case TOK_SEMI:
356            semi_node = for_node->pn_right;
357            break;
358          case TOK_IF:
359            if_node = for_node->pn_right;
360            assert(if_node->pn_arity == PN_TERNARY);
361            semi_node = if_node->pn_kid2;
362            assert(semi_node->pn_type == TOK_SEMI);
363            break;
364          default:
365            abort();
366            break;
367          }
368          assert(semi_node->pn_arity == PN_UNARY);
369          JSParseNode * yield_node = semi_node->pn_kid;
370          assert(yield_node->pn_type == TOK_YIELD);
371          Stream_write_char(f, '(');
372          instrument_expression(yield_node->pn_kid, f);
373          Stream_write_char(f, ' ');
374          output_for_in(for_node, f);
375          if (if_node) {
376            Stream_write_string(f, " if (");
377            instrument_expression(if_node->pn_kid1, f);
378            Stream_write_char(f, ')');
379          }
380          Stream_write_char(f, ')');
381          return;
382        }
383        else {
384          Stream_write_char(f, '(');
385          instrument_expression(function_node, f);
386          Stream_write_char(f, ')');
387        }
388      }
389      else {
390        instrument_expression(function_node, f);
391      }
392    Stream_write_char(f, '(');    Stream_write_char(f, '(');
393    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) {
394      if (p != node->pn_head->pn_next) {      if (p != node->pn_head->pn_next) {
395        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
396      }      }
# Line 523  Line 598 
598      assert(ATOM_IS_STRING(node->pn_atom));      assert(ATOM_IS_STRING(node->pn_atom));
599      {      {
600        JSString * s = ATOM_TO_STRING(node->pn_atom);        JSString * s = ATOM_TO_STRING(node->pn_atom);
601        bool is_keyword = (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF);        bool must_quote;
602        if (! is_keyword && js_IsIdentifier(s)) {        if (JSSTRING_LENGTH(s) == 0) {
603          Stream_write_char(f, '.');          must_quote = true;
604          print_string_atom(node->pn_atom, f);        }
605          else if (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF) {
606            must_quote = true;
607          }
608          else if (! js_IsIdentifier(s)) {
609            must_quote = true;
610        }        }
611        else {        else {
612            must_quote = false;
613          }
614          if (must_quote) {
615          Stream_write_char(f, '[');          Stream_write_char(f, '[');
616          print_quoted_string_atom(node->pn_atom, f);          print_quoted_string_atom(node->pn_atom, f);
617          Stream_write_char(f, ']');          Stream_write_char(f, ']');
618        }        }
619          else {
620            Stream_write_char(f, '.');
621            print_string_atom(node->pn_atom, f);
622          }
623      }      }
624      break;      break;
625    case TOK_LB:    case TOK_LB:
# Line 667  Line 754 
754      Stream_write_string(f, ") ");      Stream_write_string(f, ") ");
755      instrument_expression(node->pn_expr->pn_right, f);      instrument_expression(node->pn_expr->pn_right, f);
756      break;      break;
757      case TOK_YIELD:
758        assert(node->pn_arity == PN_UNARY);
759        Stream_write_string(f, "yield ");
760        instrument_expression(node->pn_kid, f);
761        break;
762      case TOK_ARRAYCOMP:
763        assert(node->pn_arity == PN_LIST);
764        {
765          JSParseNode * block_node;
766          switch (node->pn_count) {
767          case 1:
768            block_node = node->pn_head;
769            break;
770          case 2:
771            block_node = node->pn_head->pn_next;
772            break;
773          default:
774            abort();
775            break;
776          }
777          assert(block_node->pn_type == TOK_LEXICALSCOPE);
778          assert(block_node->pn_arity == PN_NAME);
779          JSParseNode * for_node = block_node->pn_expr;
780          assert(for_node->pn_type == TOK_FOR);
781          assert(for_node->pn_arity == PN_BINARY);
782          JSParseNode * if_node = NULL;
783          JSParseNode * push_node;
784          switch (for_node->pn_right->pn_type) {
785          case TOK_ARRAYPUSH:
786            push_node = for_node->pn_right;
787            assert(push_node->pn_arity == PN_UNARY);
788            break;
789          case TOK_IF:
790            if_node = for_node->pn_right;
791            assert(if_node->pn_arity == PN_TERNARY);
792            push_node = if_node->pn_kid2;
793            break;
794          default:
795            abort();
796            break;
797          }
798          Stream_write_char(f, '[');
799          instrument_expression(push_node->pn_kid, f);
800          Stream_write_char(f, ' ');
801          output_for_in(for_node, f);
802          if (if_node) {
803            Stream_write_string(f, " if (");
804            instrument_expression(if_node->pn_kid1, f);
805            Stream_write_char(f, ')');
806          }
807          Stream_write_char(f, ']');
808        }
809        break;
810      case TOK_VAR:
811        assert(node->pn_arity == PN_LIST);
812        Stream_write_string(f, "var ");
813        instrument_declarations(node, f);
814        break;
815      case TOK_LET:
816        assert(node->pn_arity == PN_LIST);
817        Stream_write_string(f, "let ");
818        instrument_declarations(node, f);
819        break;
820    default:    default:
821      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);
822    }    }
823  }  }
824    
 static void instrument_var_statement(JSParseNode * node, Stream * f, int indent) {  
   assert(node->pn_arity == PN_LIST);  
   Stream_printf(f, "%*s", indent, "");  
   Stream_write_string(f, "var ");  
   instrument_declarations(node, f);  
 }  
   
 static void instrument_let_definition(JSParseNode * node, Stream * f, int indent) {  
   assert(node->pn_arity == PN_LIST);  
   Stream_printf(f, "%*s", indent, "");  
   Stream_write_string(f, "let ");  
   instrument_declarations(node, f);  
 }  
   
825  static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {  static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
826    switch (node->pn_type) {    switch (node->pn_type) {
827    case TOK_FUNCTION:    case TOK_FUNCTION:
# Line 759  Line 895 
895      Stream_write_string(f, "switch (");      Stream_write_string(f, "switch (");
896      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
897      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
898      for (struct JSParseNode * p = node->pn_right->pn_head; p != NULL; p = p->pn_next) {      {
899        Stream_printf(f, "%*s", indent, "");        JSParseNode * list = node->pn_right;
900        switch (p->pn_type) {        if (list->pn_type == TOK_LEXICALSCOPE) {
901        case TOK_CASE:          list = list->pn_expr;
902          Stream_write_string(f, "case ");        }
903          instrument_expression(p->pn_left, f);        for (struct JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
904          Stream_write_string(f, ":\n");          Stream_printf(f, "%*s", indent, "");
905          break;          switch (p->pn_type) {
906        case TOK_DEFAULT:          case TOK_CASE:
907          Stream_write_string(f, "default:\n");            Stream_write_string(f, "case ");
908          break;            instrument_expression(p->pn_left, f);
909        default:            Stream_write_string(f, ":\n");
910          abort();            break;
911          break;          case TOK_DEFAULT:
912              Stream_write_string(f, "default:\n");
913              break;
914            default:
915              abort();
916              break;
917            }
918            instrument_statement(p->pn_right, f, indent + 2, false);
919        }        }
       instrument_statement(p->pn_right, f, indent + 2, false);  
920      }      }
921      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
922      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
# Line 806  Line 948 
948    case TOK_FOR:    case TOK_FOR:
949      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
950      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
     Stream_write_string(f, "for ");  
     if (node->pn_iflags & JSITER_FOREACH) {  
       Stream_write_string(f, "each ");  
     }  
     Stream_write_char(f, '(');  
951      switch (node->pn_left->pn_type) {      switch (node->pn_left->pn_type) {
952      case TOK_IN:      case TOK_IN:
953        /* for/in */        /* for/in */
954        assert(node->pn_left->pn_arity == PN_BINARY);        assert(node->pn_left->pn_arity == PN_BINARY);
955        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_LET:  
         instrument_let_definition(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);  
956        break;        break;
957      case TOK_RESERVED:      case TOK_RESERVED:
958        /* for (;;) */        /* for (;;) */
959        assert(node->pn_left->pn_arity == PN_TERNARY);        assert(node->pn_left->pn_arity == PN_TERNARY);
960          Stream_write_string(f, "for (");
961        if (node->pn_left->pn_kid1) {        if (node->pn_left->pn_kid1) {
962          switch (node->pn_left->pn_kid1->pn_type) {          instrument_expression(node->pn_left->pn_kid1, f);
         case TOK_VAR:  
           instrument_var_statement(node->pn_left->pn_kid1, f, 0);  
           break;  
         case TOK_LET:  
           instrument_let_definition(node->pn_left->pn_kid1, f, 0);  
           break;  
         default:  
           instrument_expression(node->pn_left->pn_kid1, f);  
           break;  
         }  
963        }        }
964        Stream_write_string(f, ";");        Stream_write_string(f, ";");
965        if (node->pn_left->pn_kid2) {        if (node->pn_left->pn_kid2) {
# Line 865  Line 971 
971          Stream_write_char(f, ' ');          Stream_write_char(f, ' ');
972          instrument_expression(node->pn_left->pn_kid3, f);          instrument_expression(node->pn_left->pn_kid3, f);
973        }        }
974          Stream_write_char(f, ')');
975        break;        break;
976      default:      default:
977        abort();        abort();
978        break;        break;
979      }      }
980      Stream_write_string(f, ") {\n");      Stream_write_string(f, " {\n");
981      instrument_statement(node->pn_right, f, indent + 2, false);      instrument_statement(node->pn_right, f, indent + 2, false);
982      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
983      break;      break;
# Line 945  Line 1052 
1052      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
1053      break;      break;
1054    case TOK_VAR:    case TOK_VAR:
1055      instrument_var_statement(node, f, indent);      Stream_printf(f, "%*s", indent, "");
1056        instrument_expression(node, f);
1057      Stream_write_string(f, ";\n");      Stream_write_string(f, ";\n");
1058      break;      break;
1059    case TOK_RETURN:    case TOK_RETURN:
# Line 1021  Line 1129 
1129        break;        break;
1130      case PN_LIST:      case PN_LIST:
1131        /* let definition */        /* let definition */
1132        instrument_let_definition(node, f, indent);        Stream_printf(f, "%*s", indent, "");
1133          instrument_expression(node, f);
1134        Stream_write_string(f, ";\n");        Stream_write_string(f, ";\n");
1135        break;        break;
1136      default:      default:
# Line 1029  Line 1138 
1138        break;        break;
1139      }      }
1140      break;      break;
1141      case TOK_DEBUGGER:
1142        Stream_printf(f, "%*s", indent, "");
1143        Stream_write_string(f, "debugger;\n");
1144        break;
1145    default:    default:
1146      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);
1147    }    }

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

  ViewVC Help
Powered by ViewVC 1.1.24