/[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 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 276  Line 277 
277    Stream_write_char(f, ')');    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 314  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);
# Line 332  Line 373 
373  }  }
374    
375  static void instrument_function_call(JSParseNode * node, Stream * f) {  static void instrument_function_call(JSParseNode * node, Stream * f) {
376    if (node->pn_head->pn_type == TOK_FUNCTION) {    JSParseNode * function_node = node->pn_head;
377      /* it's a generator expression */    if (function_node->pn_type == TOK_FUNCTION) {
378      JSParseNode * function_node = node->pn_head;      JSObject * object = function_node->pn_funpob->object;
379      JSParseNode * lexical_scope_node = function_node->pn_body;      assert(JS_ObjectIsFunction(context, object));
380      assert(lexical_scope_node->pn_type == TOK_LEXICALSCOPE);      JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
381      assert(lexical_scope_node->pn_arity == PN_NAME);      assert(function);
382      JSParseNode * for_node = lexical_scope_node->pn_body;      assert(object == &function->object);
383      assert(for_node->pn_type == TOK_FOR);  
384      assert(for_node->pn_arity == PN_BINARY);      if (function_node->pn_flags & TCF_GENEXP_LAMBDA) {
385      JSParseNode * if_node = NULL;        /* it's a generator expression */
386      JSParseNode * semi_node;        Stream_write_char(f, '(');
387      switch (for_node->pn_right->pn_type) {        output_array_comprehension_or_generator_expression(function_node->pn_body, f);
388      case TOK_SEMI:        Stream_write_char(f, ')');
389        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;  
390      }      }
391      assert(semi_node->pn_arity == PN_UNARY);      else {
392      JSParseNode * yield_node = semi_node->pn_kid;        Stream_write_char(f, '(');
393      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);  
394        Stream_write_char(f, ')');        Stream_write_char(f, ')');
395      }      }
     Stream_write_char(f, ')');  
396    }    }
397    else {    else {
398      instrument_expression(node->pn_head, f);      instrument_expression(function_node, f);
399      Stream_write_char(f, '(');    }
400      for (struct JSParseNode * p = node->pn_head->pn_next; p != NULL; p = p->pn_next) {    Stream_write_char(f, '(');
401        if (p != node->pn_head->pn_next) {    for (struct JSParseNode * p = function_node->pn_next; p != NULL; p = p->pn_next) {
402          Stream_write_string(f, ", ");      if (p != node->pn_head->pn_next) {
403        }        Stream_write_string(f, ", ");
       instrument_expression(p, f);  
404      }      }
405      Stream_write_char(f, ')');      instrument_expression(p, f);
406    }    }
407      Stream_write_char(f, ')');
408  }  }
409    
410  static void instrument_declarations(JSParseNode * list, Stream * f) {  static void instrument_declarations(JSParseNode * list, Stream * f) {
# Line 526  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 583  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 623  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 729  Line 766 
766      break;      break;
767    case TOK_YIELD:    case TOK_YIELD:
768      assert(node->pn_arity == PN_UNARY);      assert(node->pn_arity == PN_UNARY);
769      Stream_write_string(f, "yield ");      Stream_write_string(f, "yield");
770      instrument_expression(node->pn_kid, f);      if (node->pn_kid != NULL) {
771          Stream_write_char(f, ' ');
772          instrument_expression(node->pn_kid, f);
773        }
774      break;      break;
775    case TOK_ARRAYCOMP:    case TOK_ARRAYCOMP:
776      assert(node->pn_arity == PN_LIST);      assert(node->pn_arity == PN_LIST);
# Line 747  Line 787 
787          abort();          abort();
788          break;          break;
789        }        }
       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;  
       }  
790        Stream_write_char(f, '[');        Stream_write_char(f, '[');
791        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, ')');  
       }  
792        Stream_write_char(f, ']');        Stream_write_char(f, ']');
793      }      }
794      break;      break;
# Line 868  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 1105  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.346  
changed lines
  Added in v.357

  ViewVC Help
Powered by ViewVC 1.1.24