/[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 357 by siliconforks, Fri Oct 24 21:48:21 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 output_array_comprehension_or_generator_expression(JSParseNode * node, Stream * f) {
281      assert(node->pn_type == TOK_LEXICALSCOPE);
282      assert(node->pn_arity == PN_NAME);
283      JSParseNode * for_node = node->pn_expr;
284      assert(for_node->pn_type == TOK_FOR);
285      assert(for_node->pn_arity == PN_BINARY);
286      JSParseNode * p = for_node;
287      while (p->pn_type == TOK_FOR) {
288        p = p->pn_right;
289      }
290      JSParseNode * if_node = NULL;
291      if (p->pn_type == TOK_IF) {
292        if_node = p;
293        assert(if_node->pn_arity == PN_TERNARY);
294        p = if_node->pn_kid2;
295      }
296      assert(p->pn_arity == PN_UNARY);
297      p = p->pn_kid;
298      if (p->pn_type == TOK_YIELD) {
299        /* for generator expressions */
300        p = p->pn_kid;
301      }
302    
303      instrument_expression(p, f);
304      p = for_node;
305      while (p->pn_type == TOK_FOR) {
306        Stream_write_char(f, ' ');
307        output_for_in(p, f);
308        p = p->pn_right;
309      }
310      if (if_node) {
311        Stream_write_string(f, " if (");
312        instrument_expression(if_node->pn_kid1, f);
313        Stream_write_char(f, ')');
314      }
315    }
316    
317  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) {
318    assert(node->pn_type == TOK_FUNCTION);    assert(node->pn_type == TOK_FUNCTION);
319    assert(node->pn_arity == PN_FUNC);    assert(node->pn_arity == PN_FUNC);
# Line 301  Line 352 
352        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
353      }      }
354      JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]);      JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]);
355        if (param == NULL) {
356          fatal("unsupported parameter type for function: %s", file_id);
357        }
358      print_string_atom(param, f);      print_string_atom(param, f);
359    }    }
360    JS_FinishArenaPool(&pool);    JS_FinishArenaPool(&pool);
361    Stream_write_string(f, ") {\n");    Stream_write_string(f, ") {\n");
362    
363    /* function body */    /* function body */
364    instrument_statement(node->pn_body, f, indent + 2, false);    if (function->flags & JSFUN_EXPR_CLOSURE) {
365        /* expression closure */
366        output_statement(node->pn_body, f, indent + 2, false);
367      }
368      else {
369        instrument_statement(node->pn_body, f, indent + 2, false);
370      }
371    
372    Stream_write_string(f, "}\n");    Stream_write_string(f, "}\n");
373  }  }
374    
375  static void instrument_function_call(JSParseNode * node, Stream * f) {  static void instrument_function_call(JSParseNode * node, Stream * f) {
376    instrument_expression(node->pn_head, f);    JSParseNode * function_node = node->pn_head;
377      if (function_node->pn_type == TOK_FUNCTION) {
378        JSObject * object = function_node->pn_funpob->object;
379        assert(JS_ObjectIsFunction(context, object));
380        JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
381        assert(function);
382        assert(object == &function->object);
383    
384        if (function_node->pn_flags & TCF_GENEXP_LAMBDA) {
385          /* it's a generator expression */
386          Stream_write_char(f, '(');
387          output_array_comprehension_or_generator_expression(function_node->pn_body, f);
388          Stream_write_char(f, ')');
389          return;
390        }
391        else {
392          Stream_write_char(f, '(');
393          instrument_expression(function_node, f);
394          Stream_write_char(f, ')');
395        }
396      }
397      else {
398        instrument_expression(function_node, f);
399      }
400    Stream_write_char(f, '(');    Stream_write_char(f, '(');
401    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) {
402      if (p != node->pn_head->pn_next) {      if (p != node->pn_head->pn_next) {
403        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
404      }      }
# Line 327  Line 410 
410  static void instrument_declarations(JSParseNode * list, Stream * f) {  static void instrument_declarations(JSParseNode * list, Stream * f) {
411    assert(list->pn_arity == PN_LIST);    assert(list->pn_arity == PN_LIST);
412    for (JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {    for (JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
413      assert(p->pn_type == TOK_NAME);      switch (p->pn_type) {
414      assert(p->pn_arity == PN_NAME);      case TOK_NAME:
415      if (p != list->pn_head) {        assert(p->pn_arity == PN_NAME);
416        Stream_write_string(f, ", ");        if (p != list->pn_head) {
417      }          Stream_write_string(f, ", ");
418      print_string_atom(p->pn_atom, f);        }
419      if (p->pn_expr != NULL) {        print_string_atom(p->pn_atom, f);
420        Stream_write_string(f, " = ");        if (p->pn_expr != NULL) {
421        instrument_expression(p->pn_expr, f);          Stream_write_string(f, " = ");
422            instrument_expression(p->pn_expr, f);
423          }
424          break;
425        case TOK_ASSIGN:
426        case TOK_RB:
427        case TOK_RC:
428          /* destructuring */
429          instrument_expression(p, f);
430          break;
431        default:
432          abort();
433          break;
434      }      }
435    }    }
436  }  }
# Line 454  Line 549 
549        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
550        break;        break;
551      default:      default:
552        abort();        fatal("%s: unknown operator (%d) in file", file_id, node->pn_op);
553        break;        break;
554      }      }
555      break;      break;
# Line 511  Line 606 
606      assert(ATOM_IS_STRING(node->pn_atom));      assert(ATOM_IS_STRING(node->pn_atom));
607      {      {
608        JSString * s = ATOM_TO_STRING(node->pn_atom);        JSString * s = ATOM_TO_STRING(node->pn_atom);
609        bool is_keyword = (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF);        bool must_quote;
610        if (! is_keyword && js_IsIdentifier(s)) {        if (JSSTRING_LENGTH(s) == 0) {
611          Stream_write_char(f, '.');          must_quote = true;
612          print_string_atom(node->pn_atom, f);        }
613          else if (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF) {
614            must_quote = true;
615          }
616          else if (! js_IsIdentifier(s)) {
617            must_quote = true;
618        }        }
619        else {        else {
620            must_quote = false;
621          }
622          if (must_quote) {
623          Stream_write_char(f, '[');          Stream_write_char(f, '[');
624          print_quoted_string_atom(node->pn_atom, f);          print_quoted_string_atom(node->pn_atom, f);
625          Stream_write_char(f, ']');          Stream_write_char(f, ']');
626        }        }
627          else {
628            Stream_write_char(f, '.');
629            print_string_atom(node->pn_atom, f);
630          }
631      }      }
632      break;      break;
633    case TOK_LB:    case TOK_LB:
# Line 551  Line 658 
658    case TOK_RC:    case TOK_RC:
659      Stream_write_char(f, '{');      Stream_write_char(f, '{');
660      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) {
661        assert(p->pn_type == TOK_COLON);        if (p->pn_type != TOK_COLON) {
662            fatal("unsupported node type in file %s: %d", file_id, p->pn_type);
663          }
664        if (p != node->pn_head) {        if (p != node->pn_head) {
665          Stream_write_string(f, ", ");          Stream_write_string(f, ", ");
666        }        }
# Line 655  Line 764 
764      Stream_write_string(f, ") ");      Stream_write_string(f, ") ");
765      instrument_expression(node->pn_expr->pn_right, f);      instrument_expression(node->pn_expr->pn_right, f);
766      break;      break;
767      case TOK_YIELD:
768        assert(node->pn_arity == PN_UNARY);
769        Stream_write_string(f, "yield");
770        if (node->pn_kid != NULL) {
771          Stream_write_char(f, ' ');
772          instrument_expression(node->pn_kid, f);
773        }
774        break;
775      case TOK_ARRAYCOMP:
776        assert(node->pn_arity == PN_LIST);
777        {
778          JSParseNode * block_node;
779          switch (node->pn_count) {
780          case 1:
781            block_node = node->pn_head;
782            break;
783          case 2:
784            block_node = node->pn_head->pn_next;
785            break;
786          default:
787            abort();
788            break;
789          }
790          Stream_write_char(f, '[');
791          output_array_comprehension_or_generator_expression(block_node, f);
792          Stream_write_char(f, ']');
793        }
794        break;
795      case TOK_VAR:
796        assert(node->pn_arity == PN_LIST);
797        Stream_write_string(f, "var ");
798        instrument_declarations(node, f);
799        break;
800      case TOK_LET:
801        assert(node->pn_arity == PN_LIST);
802        Stream_write_string(f, "let ");
803        instrument_declarations(node, f);
804        break;
805    default:    default:
806      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);
807    }    }
808  }  }
809    
 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);  
 }  
   
810  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) {
811    switch (node->pn_type) {    switch (node->pn_type) {
812    case TOK_FUNCTION:    case TOK_FUNCTION:
# Line 747  Line 880 
880      Stream_write_string(f, "switch (");      Stream_write_string(f, "switch (");
881      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
882      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
883      for (struct JSParseNode * p = node->pn_right->pn_head; p != NULL; p = p->pn_next) {      {
884        Stream_printf(f, "%*s", indent, "");        JSParseNode * list = node->pn_right;
885        switch (p->pn_type) {        if (list->pn_type == TOK_LEXICALSCOPE) {
886        case TOK_CASE:          list = list->pn_expr;
887          Stream_write_string(f, "case ");        }
888          instrument_expression(p->pn_left, f);        for (struct JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
889          Stream_write_string(f, ":\n");          Stream_printf(f, "%*s", indent, "");
890          break;          switch (p->pn_type) {
891        case TOK_DEFAULT:          case TOK_CASE:
892          Stream_write_string(f, "default:\n");            Stream_write_string(f, "case ");
893          break;            instrument_expression(p->pn_left, f);
894        default:            Stream_write_string(f, ":\n");
895          abort();            break;
896          break;          case TOK_DEFAULT:
897              Stream_write_string(f, "default:\n");
898              break;
899            default:
900              abort();
901              break;
902            }
903            instrument_statement(p->pn_right, f, indent + 2, false);
904        }        }
       instrument_statement(p->pn_right, f, indent + 2, false);  
905      }      }
906      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
907      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
# Line 794  Line 933 
933    case TOK_FOR:    case TOK_FOR:
934      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
935      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, '(');  
936      switch (node->pn_left->pn_type) {      switch (node->pn_left->pn_type) {
937      case TOK_IN:      case TOK_IN:
938        /* for/in */        /* for/in */
939        assert(node->pn_left->pn_arity == PN_BINARY);        assert(node->pn_left->pn_arity == PN_BINARY);
940        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);  
941        break;        break;
942      case TOK_RESERVED:      case TOK_RESERVED:
943        /* for (;;) */        /* for (;;) */
944        assert(node->pn_left->pn_arity == PN_TERNARY);        assert(node->pn_left->pn_arity == PN_TERNARY);
945          Stream_write_string(f, "for (");
946        if (node->pn_left->pn_kid1) {        if (node->pn_left->pn_kid1) {
947          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;  
         }  
948        }        }
949        Stream_write_string(f, ";");        Stream_write_string(f, ";");
950        if (node->pn_left->pn_kid2) {        if (node->pn_left->pn_kid2) {
# Line 853  Line 956 
956          Stream_write_char(f, ' ');          Stream_write_char(f, ' ');
957          instrument_expression(node->pn_left->pn_kid3, f);          instrument_expression(node->pn_left->pn_kid3, f);
958        }        }
959          Stream_write_char(f, ')');
960        break;        break;
961      default:      default:
962        abort();        abort();
963        break;        break;
964      }      }
965      Stream_write_string(f, ") {\n");      Stream_write_string(f, " {\n");
966      instrument_statement(node->pn_right, f, indent + 2, false);      instrument_statement(node->pn_right, f, indent + 2, false);
967      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
968      break;      break;
# Line 883  Line 987 
987          assert(catch->pn_type == TOK_CATCH);          assert(catch->pn_type == TOK_CATCH);
988          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
989          Stream_write_string(f, "catch (");          Stream_write_string(f, "catch (");
990            /* this may not be a name - destructuring assignment */
991            /*
992          assert(catch->pn_kid1->pn_arity == PN_NAME);          assert(catch->pn_kid1->pn_arity == PN_NAME);
993          print_string_atom(catch->pn_kid1->pn_atom, f);          print_string_atom(catch->pn_kid1->pn_atom, f);
994            */
995            instrument_expression(catch->pn_kid1, f);
996          if (catch->pn_kid2) {          if (catch->pn_kid2) {
997            Stream_write_string(f, " if ");            Stream_write_string(f, " if ");
998            instrument_expression(catch->pn_kid2, f);            instrument_expression(catch->pn_kid2, f);
# Line 929  Line 1037 
1037      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
1038      break;      break;
1039    case TOK_VAR:    case TOK_VAR:
1040      instrument_var_statement(node, f, indent);      Stream_printf(f, "%*s", indent, "");
1041        instrument_expression(node, f);
1042      Stream_write_string(f, ";\n");      Stream_write_string(f, ";\n");
1043      break;      break;
1044    case TOK_RETURN:    case TOK_RETURN:
# Line 1005  Line 1114 
1114        break;        break;
1115      case PN_LIST:      case PN_LIST:
1116        /* let definition */        /* let definition */
1117        instrument_let_definition(node, f, indent);        Stream_printf(f, "%*s", indent, "");
1118          instrument_expression(node, f);
1119        Stream_write_string(f, ";\n");        Stream_write_string(f, ";\n");
1120        break;        break;
1121      default:      default:
# Line 1013  Line 1123 
1123        break;        break;
1124      }      }
1125      break;      break;
1126      case TOK_DEBUGGER:
1127        Stream_printf(f, "%*s", indent, "");
1128        Stream_write_string(f, "debugger;\n");
1129        break;
1130    default:    default:
1131      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);
1132    }    }

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

  ViewVC Help
Powered by ViewVC 1.1.24