/[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 345 by siliconforks, Fri Oct 24 16:16:00 2008 UTC revision 350 by siliconforks, Fri Oct 24 16:17:39 2008 UTC
# Line 264  Line 264 
264    FUNCTION_GETTER_OR_SETTER    FUNCTION_GETTER_OR_SETTER
265  };  };
266    
267    static void output_for_in(JSParseNode * node, Stream * f) {
268      assert(node->pn_type == TOK_FOR);
269      assert(node->pn_arity == PN_BINARY);
270      Stream_write_string(f, "for ");
271      if (node->pn_iflags & JSITER_FOREACH) {
272        Stream_write_string(f, "each ");
273      }
274      Stream_write_char(f, '(');
275      instrument_expression(node->pn_left, f);
276      Stream_write_char(f, ')');
277    }
278    
279  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) {
280    assert(node->pn_type == TOK_FUNCTION);    assert(node->pn_type == TOK_FUNCTION);
281    assert(node->pn_arity == PN_FUNC);    assert(node->pn_arity == PN_FUNC);
# Line 320  Line 332 
332  }  }
333    
334  static void instrument_function_call(JSParseNode * node, Stream * f) {  static void instrument_function_call(JSParseNode * node, Stream * f) {
335    instrument_expression(node->pn_head, f);    if (node->pn_head->pn_type == TOK_FUNCTION) {
336    Stream_write_char(f, '(');      /* it's a generator expression */
337    for (struct JSParseNode * p = node->pn_head->pn_next; p != NULL; p = p->pn_next) {      JSParseNode * function_node = node->pn_head;
338      if (p != node->pn_head->pn_next) {      JSParseNode * lexical_scope_node = function_node->pn_body;
339        Stream_write_string(f, ", ");      assert(lexical_scope_node->pn_type == TOK_LEXICALSCOPE);
340        assert(lexical_scope_node->pn_arity == PN_NAME);
341        JSParseNode * for_node = lexical_scope_node->pn_body;
342        assert(for_node->pn_type == TOK_FOR);
343        assert(for_node->pn_arity == PN_BINARY);
344        JSParseNode * if_node = NULL;
345        JSParseNode * semi_node;
346        switch (for_node->pn_right->pn_type) {
347        case TOK_SEMI:
348          semi_node = for_node->pn_right;
349          break;
350        case TOK_IF:
351          if_node = for_node->pn_right;
352          assert(if_node->pn_arity == PN_TERNARY);
353          semi_node = if_node->pn_kid2;
354          assert(semi_node->pn_type == TOK_SEMI);
355          break;
356        default:
357          abort();
358          break;
359        }
360        assert(semi_node->pn_arity == PN_UNARY);
361        JSParseNode * yield_node = semi_node->pn_kid;
362        assert(yield_node->pn_type == TOK_YIELD);
363        Stream_write_char(f, '(');
364        instrument_expression(yield_node->pn_kid, f);
365        Stream_write_char(f, ' ');
366        output_for_in(for_node, f);
367        if (if_node) {
368          Stream_write_string(f, " if (");
369          instrument_expression(if_node->pn_kid1, f);
370          Stream_write_char(f, ')');
371      }      }
372      instrument_expression(p, f);      Stream_write_char(f, ')');
373      }
374      else {
375        instrument_expression(node->pn_head, f);
376        Stream_write_char(f, '(');
377        for (struct JSParseNode * p = node->pn_head->pn_next; p != NULL; p = p->pn_next) {
378          if (p != node->pn_head->pn_next) {
379            Stream_write_string(f, ", ");
380          }
381          instrument_expression(p, f);
382        }
383        Stream_write_char(f, ')');
384    }    }
   Stream_write_char(f, ')');  
385  }  }
386    
387  static void instrument_declarations(JSParseNode * list, Stream * f) {  static void instrument_declarations(JSParseNode * list, Stream * f) {
# Line 359  Line 412 
412    }    }
413  }  }
414    
 static void output_for_in(JSParseNode * node, Stream * f) {  
   assert(node->pn_type == TOK_FOR);  
   assert(node->pn_arity == PN_BINARY);  
   Stream_write_string(f, "for ");  
   if (node->pn_iflags & JSITER_FOREACH) {  
     Stream_write_string(f, "each ");  
   }  
   Stream_write_char(f, '(');  
   instrument_expression(node->pn_left, f);  
   Stream_write_char(f, ')');  
 }  
   
415  /*  /*
416  See <Expressions> in jsparse.h.  See <Expressions> in jsparse.h.
417  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
# Line 542  Line 583 
583      assert(ATOM_IS_STRING(node->pn_atom));      assert(ATOM_IS_STRING(node->pn_atom));
584      {      {
585        JSString * s = ATOM_TO_STRING(node->pn_atom);        JSString * s = ATOM_TO_STRING(node->pn_atom);
586        bool is_keyword = (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF);        bool must_quote;
587        if (! is_keyword && js_IsIdentifier(s)) {        if (JSSTRING_LENGTH(s) == 0) {
588          Stream_write_char(f, '.');          must_quote = true;
589          print_string_atom(node->pn_atom, f);        }
590          else if (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF) {
591            must_quote = true;
592          }
593          else if (! js_IsIdentifier(s)) {
594            must_quote = true;
595        }        }
596        else {        else {
597            must_quote = false;
598          }
599          if (must_quote) {
600          Stream_write_char(f, '[');          Stream_write_char(f, '[');
601          print_quoted_string_atom(node->pn_atom, f);          print_quoted_string_atom(node->pn_atom, f);
602          Stream_write_char(f, ']');          Stream_write_char(f, ']');
603        }        }
604          else {
605            Stream_write_char(f, '.');
606            print_string_atom(node->pn_atom, f);
607          }
608      }      }
609      break;      break;
610    case TOK_LB:    case TOK_LB:
# Line 711  Line 764 
764        JSParseNode * for_node = block_node->pn_expr;        JSParseNode * for_node = block_node->pn_expr;
765        assert(for_node->pn_type == TOK_FOR);        assert(for_node->pn_type == TOK_FOR);
766        assert(for_node->pn_arity == PN_BINARY);        assert(for_node->pn_arity == PN_BINARY);
       JSParseNode * push_node;  
767        JSParseNode * if_node = NULL;        JSParseNode * if_node = NULL;
768          JSParseNode * push_node;
769        switch (for_node->pn_right->pn_type) {        switch (for_node->pn_right->pn_type) {
770        case TOK_ARRAYPUSH:        case TOK_ARRAYPUSH:
771          push_node = for_node->pn_right;          push_node = for_node->pn_right;
# Line 827  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 1064  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.345  
changed lines
  Added in v.350

  ViewVC Help
Powered by ViewVC 1.1.24