/[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 340 by siliconforks, Fri Oct 24 16:14:20 2008 UTC revision 356 by siliconforks, Fri Oct 24 21:48:02 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 301  Line 315 
315        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
316      }      }
317      JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]);      JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]);
318        if (param == NULL) {
319          fatal("unsupported parameter type for function: %s", file_id);
320        }
321      print_string_atom(param, f);      print_string_atom(param, f);
322    }    }
323    JS_FinishArenaPool(&pool);    JS_FinishArenaPool(&pool);
324    Stream_write_string(f, ") {\n");    Stream_write_string(f, ") {\n");
325    
326    /* function body */    /* function body */
327    instrument_statement(node->pn_body, f, indent + 2, false);    if (function->flags & JSFUN_EXPR_CLOSURE) {
328        /* expression closure */
329        output_statement(node->pn_body, f, indent + 2, false);
330      }
331      else {
332        instrument_statement(node->pn_body, f, indent + 2, false);
333      }
334    
335    Stream_write_string(f, "}\n");    Stream_write_string(f, "}\n");
336  }  }
337    
338  static void instrument_function_call(JSParseNode * node, Stream * f) {  static void instrument_function_call(JSParseNode * node, Stream * f) {
339    instrument_expression(node->pn_head, f);    JSParseNode * function_node = node->pn_head;
340      if (function_node->pn_type == TOK_FUNCTION) {
341        JSObject * object = function_node->pn_funpob->object;
342        assert(JS_ObjectIsFunction(context, object));
343        JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
344        assert(function);
345        assert(object == &function->object);
346    
347        if (function_node->pn_flags & TCF_GENEXP_LAMBDA) {
348          /* it's a generator expression */
349          JSParseNode * lexical_scope_node = function_node->pn_body;
350          assert(lexical_scope_node->pn_type == TOK_LEXICALSCOPE);
351          assert(lexical_scope_node->pn_arity == PN_NAME);
352          JSParseNode * for_node = lexical_scope_node->pn_body;
353          assert(for_node->pn_type == TOK_FOR);
354          assert(for_node->pn_arity == PN_BINARY);
355          JSParseNode * if_node = NULL;
356          JSParseNode * semi_node;
357          switch (for_node->pn_right->pn_type) {
358          case TOK_SEMI:
359            semi_node = for_node->pn_right;
360            break;
361          case TOK_IF:
362            if_node = for_node->pn_right;
363            assert(if_node->pn_arity == PN_TERNARY);
364            semi_node = if_node->pn_kid2;
365            assert(semi_node->pn_type == TOK_SEMI);
366            break;
367          default:
368            abort();
369            break;
370          }
371          assert(semi_node->pn_arity == PN_UNARY);
372          JSParseNode * yield_node = semi_node->pn_kid;
373          assert(yield_node->pn_type == TOK_YIELD);
374          Stream_write_char(f, '(');
375          instrument_expression(yield_node->pn_kid, f);
376          Stream_write_char(f, ' ');
377          output_for_in(for_node, f);
378          if (if_node) {
379            Stream_write_string(f, " if (");
380            instrument_expression(if_node->pn_kid1, f);
381            Stream_write_char(f, ')');
382          }
383          Stream_write_char(f, ')');
384          return;
385        }
386        else {
387          Stream_write_char(f, '(');
388          instrument_expression(function_node, f);
389          Stream_write_char(f, ')');
390        }
391      }
392      else {
393        instrument_expression(function_node, f);
394      }
395    Stream_write_char(f, '(');    Stream_write_char(f, '(');
396    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) {
397      if (p != node->pn_head->pn_next) {      if (p != node->pn_head->pn_next) {
398        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
399      }      }
# Line 327  Line 405 
405  static void instrument_declarations(JSParseNode * list, Stream * f) {  static void instrument_declarations(JSParseNode * list, Stream * f) {
406    assert(list->pn_arity == PN_LIST);    assert(list->pn_arity == PN_LIST);
407    for (JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {    for (JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
408      assert(p->pn_type == TOK_NAME);      switch (p->pn_type) {
409      assert(p->pn_arity == PN_NAME);      case TOK_NAME:
410      if (p != list->pn_head) {        assert(p->pn_arity == PN_NAME);
411        Stream_write_string(f, ", ");        if (p != list->pn_head) {
412      }          Stream_write_string(f, ", ");
413      print_string_atom(p->pn_atom, f);        }
414      if (p->pn_expr != NULL) {        print_string_atom(p->pn_atom, f);
415        Stream_write_string(f, " = ");        if (p->pn_expr != NULL) {
416        instrument_expression(p->pn_expr, f);          Stream_write_string(f, " = ");
417            instrument_expression(p->pn_expr, f);
418          }
419          break;
420        case TOK_ASSIGN:
421        case TOK_RB:
422        case TOK_RC:
423          /* destructuring */
424          instrument_expression(p, f);
425          break;
426        default:
427          abort();
428          break;
429      }      }
430    }    }
431  }  }
# Line 454  Line 544 
544        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
545        break;        break;
546      default:      default:
547        abort();        fatal("%s: unknown operator (%d) in file", file_id, node->pn_op);
548        break;        break;
549      }      }
550      break;      break;
# Line 511  Line 601 
601      assert(ATOM_IS_STRING(node->pn_atom));      assert(ATOM_IS_STRING(node->pn_atom));
602      {      {
603        JSString * s = ATOM_TO_STRING(node->pn_atom);        JSString * s = ATOM_TO_STRING(node->pn_atom);
604        bool is_keyword = (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF);        bool must_quote;
605        if (! is_keyword && js_IsIdentifier(s)) {        if (JSSTRING_LENGTH(s) == 0) {
606          Stream_write_char(f, '.');          must_quote = true;
607          print_string_atom(node->pn_atom, f);        }
608          else if (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF) {
609            must_quote = true;
610          }
611          else if (! js_IsIdentifier(s)) {
612            must_quote = true;
613        }        }
614        else {        else {
615            must_quote = false;
616          }
617          if (must_quote) {
618          Stream_write_char(f, '[');          Stream_write_char(f, '[');
619          print_quoted_string_atom(node->pn_atom, f);          print_quoted_string_atom(node->pn_atom, f);
620          Stream_write_char(f, ']');          Stream_write_char(f, ']');
621        }        }
622          else {
623            Stream_write_char(f, '.');
624            print_string_atom(node->pn_atom, f);
625          }
626      }      }
627      break;      break;
628    case TOK_LB:    case TOK_LB:
# Line 551  Line 653 
653    case TOK_RC:    case TOK_RC:
654      Stream_write_char(f, '{');      Stream_write_char(f, '{');
655      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) {
656        assert(p->pn_type == TOK_COLON);        if (p->pn_type != TOK_COLON) {
657            fatal("unsupported node type in file %s: %d", file_id, p->pn_type);
658          }
659        if (p != node->pn_head) {        if (p != node->pn_head) {
660          Stream_write_string(f, ", ");          Stream_write_string(f, ", ");
661        }        }
# Line 655  Line 759 
759      Stream_write_string(f, ") ");      Stream_write_string(f, ") ");
760      instrument_expression(node->pn_expr->pn_right, f);      instrument_expression(node->pn_expr->pn_right, f);
761      break;      break;
762      case TOK_YIELD:
763        assert(node->pn_arity == PN_UNARY);
764        Stream_write_string(f, "yield");
765        if (node->pn_kid != NULL) {
766          Stream_write_char(f, ' ');
767          instrument_expression(node->pn_kid, f);
768        }
769        break;
770      case TOK_ARRAYCOMP:
771        assert(node->pn_arity == PN_LIST);
772        {
773          JSParseNode * block_node;
774          switch (node->pn_count) {
775          case 1:
776            block_node = node->pn_head;
777            break;
778          case 2:
779            block_node = node->pn_head->pn_next;
780            break;
781          default:
782            abort();
783            break;
784          }
785          assert(block_node->pn_type == TOK_LEXICALSCOPE);
786          assert(block_node->pn_arity == PN_NAME);
787          JSParseNode * for_node = block_node->pn_expr;
788          assert(for_node->pn_type == TOK_FOR);
789          assert(for_node->pn_arity == PN_BINARY);
790          JSParseNode * p = for_node;
791          while (p->pn_type == TOK_FOR) {
792            p = p->pn_right;
793          }
794          JSParseNode * if_node = NULL;
795          JSParseNode * push_node;
796          switch (p->pn_type) {
797          case TOK_ARRAYPUSH:
798            push_node = p;
799            assert(push_node->pn_arity == PN_UNARY);
800            break;
801          case TOK_IF:
802            if_node = p;
803            assert(if_node->pn_arity == PN_TERNARY);
804            push_node = if_node->pn_kid2;
805            assert(push_node->pn_arity == PN_UNARY);
806            break;
807          default:
808            abort();
809            break;
810          }
811          Stream_write_char(f, '[');
812          instrument_expression(push_node->pn_kid, f);
813          p = for_node;
814          while (p->pn_type == TOK_FOR) {
815            Stream_write_char(f, ' ');
816            output_for_in(p, f);
817            p = p->pn_right;
818          }
819          if (if_node) {
820            Stream_write_string(f, " if (");
821            instrument_expression(if_node->pn_kid1, f);
822            Stream_write_char(f, ')');
823          }
824          Stream_write_char(f, ']');
825        }
826        break;
827      case TOK_VAR:
828        assert(node->pn_arity == PN_LIST);
829        Stream_write_string(f, "var ");
830        instrument_declarations(node, f);
831        break;
832      case TOK_LET:
833        assert(node->pn_arity == PN_LIST);
834        Stream_write_string(f, "let ");
835        instrument_declarations(node, f);
836        break;
837    default:    default:
838      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);
839    }    }
840  }  }
841    
 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);  
 }  
   
842  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) {
843    switch (node->pn_type) {    switch (node->pn_type) {
844    case TOK_FUNCTION:    case TOK_FUNCTION:
# Line 747  Line 912 
912      Stream_write_string(f, "switch (");      Stream_write_string(f, "switch (");
913      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
914      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
915      for (struct JSParseNode * p = node->pn_right->pn_head; p != NULL; p = p->pn_next) {      {
916        Stream_printf(f, "%*s", indent, "");        JSParseNode * list = node->pn_right;
917        switch (p->pn_type) {        if (list->pn_type == TOK_LEXICALSCOPE) {
918        case TOK_CASE:          list = list->pn_expr;
919          Stream_write_string(f, "case ");        }
920          instrument_expression(p->pn_left, f);        for (struct JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
921          Stream_write_string(f, ":\n");          Stream_printf(f, "%*s", indent, "");
922          break;          switch (p->pn_type) {
923        case TOK_DEFAULT:          case TOK_CASE:
924          Stream_write_string(f, "default:\n");            Stream_write_string(f, "case ");
925          break;            instrument_expression(p->pn_left, f);
926        default:            Stream_write_string(f, ":\n");
927          abort();            break;
928          break;          case TOK_DEFAULT:
929              Stream_write_string(f, "default:\n");
930              break;
931            default:
932              abort();
933              break;
934            }
935            instrument_statement(p->pn_right, f, indent + 2, false);
936        }        }
       instrument_statement(p->pn_right, f, indent + 2, false);  
937      }      }
938      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
939      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
# Line 794  Line 965 
965    case TOK_FOR:    case TOK_FOR:
966      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
967      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, '(');  
968      switch (node->pn_left->pn_type) {      switch (node->pn_left->pn_type) {
969      case TOK_IN:      case TOK_IN:
970        /* for/in */        /* for/in */
971        assert(node->pn_left->pn_arity == PN_BINARY);        assert(node->pn_left->pn_arity == PN_BINARY);
972        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);  
973        break;        break;
974      case TOK_RESERVED:      case TOK_RESERVED:
975        /* for (;;) */        /* for (;;) */
976        assert(node->pn_left->pn_arity == PN_TERNARY);        assert(node->pn_left->pn_arity == PN_TERNARY);
977          Stream_write_string(f, "for (");
978        if (node->pn_left->pn_kid1) {        if (node->pn_left->pn_kid1) {
979          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;  
         }  
980        }        }
981        Stream_write_string(f, ";");        Stream_write_string(f, ";");
982        if (node->pn_left->pn_kid2) {        if (node->pn_left->pn_kid2) {
# Line 853  Line 988 
988          Stream_write_char(f, ' ');          Stream_write_char(f, ' ');
989          instrument_expression(node->pn_left->pn_kid3, f);          instrument_expression(node->pn_left->pn_kid3, f);
990        }        }
991          Stream_write_char(f, ')');
992        break;        break;
993      default:      default:
994        abort();        abort();
995        break;        break;
996      }      }
997      Stream_write_string(f, ") {\n");      Stream_write_string(f, " {\n");
998      instrument_statement(node->pn_right, f, indent + 2, false);      instrument_statement(node->pn_right, f, indent + 2, false);
999      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
1000      break;      break;
# Line 883  Line 1019 
1019          assert(catch->pn_type == TOK_CATCH);          assert(catch->pn_type == TOK_CATCH);
1020          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
1021          Stream_write_string(f, "catch (");          Stream_write_string(f, "catch (");
1022            /* this may not be a name - destructuring assignment */
1023            /*
1024          assert(catch->pn_kid1->pn_arity == PN_NAME);          assert(catch->pn_kid1->pn_arity == PN_NAME);
1025          print_string_atom(catch->pn_kid1->pn_atom, f);          print_string_atom(catch->pn_kid1->pn_atom, f);
1026            */
1027            instrument_expression(catch->pn_kid1, f);
1028          if (catch->pn_kid2) {          if (catch->pn_kid2) {
1029            Stream_write_string(f, " if ");            Stream_write_string(f, " if ");
1030            instrument_expression(catch->pn_kid2, f);            instrument_expression(catch->pn_kid2, f);
# Line 929  Line 1069 
1069      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
1070      break;      break;
1071    case TOK_VAR:    case TOK_VAR:
1072      instrument_var_statement(node, f, indent);      Stream_printf(f, "%*s", indent, "");
1073        instrument_expression(node, f);
1074      Stream_write_string(f, ";\n");      Stream_write_string(f, ";\n");
1075      break;      break;
1076    case TOK_RETURN:    case TOK_RETURN:
# Line 1005  Line 1146 
1146        break;        break;
1147      case PN_LIST:      case PN_LIST:
1148        /* let definition */        /* let definition */
1149        instrument_let_definition(node, f, indent);        Stream_printf(f, "%*s", indent, "");
1150          instrument_expression(node, f);
1151        Stream_write_string(f, ";\n");        Stream_write_string(f, ";\n");
1152        break;        break;
1153      default:      default:
# Line 1013  Line 1155 
1155        break;        break;
1156      }      }
1157      break;      break;
1158      case TOK_DEBUGGER:
1159        Stream_printf(f, "%*s", indent, "");
1160        Stream_write_string(f, "debugger;\n");
1161        break;
1162    default:    default:
1163      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);
1164    }    }

Legend:
Removed from v.340  
changed lines
  Added in v.356

  ViewVC Help
Powered by ViewVC 1.1.24