/[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 348 by siliconforks, Fri Oct 24 16:17:00 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 332  Line 333 
333  }  }
334    
335  static void instrument_function_call(JSParseNode * node, Stream * f) {  static void instrument_function_call(JSParseNode * node, Stream * f) {
336    if (node->pn_head->pn_type == TOK_FUNCTION) {    JSParseNode * function_node = node->pn_head;
337      /* it's a generator expression */    if (function_node->pn_type == TOK_FUNCTION) {
338      JSParseNode * function_node = node->pn_head;      JSObject * object = function_node->pn_funpob->object;
339      JSParseNode * lexical_scope_node = function_node->pn_body;      assert(JS_ObjectIsFunction(context, object));
340      assert(lexical_scope_node->pn_type == TOK_LEXICALSCOPE);      JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
341      assert(lexical_scope_node->pn_arity == PN_NAME);      assert(function);
342      JSParseNode * for_node = lexical_scope_node->pn_body;      assert(object == &function->object);
343      assert(for_node->pn_type == TOK_FOR);  
344      assert(for_node->pn_arity == PN_BINARY);      if (function_node->pn_flags & TCF_GENEXP_LAMBDA) {
345      JSParseNode * if_node = NULL;        /* it's a generator expression */
346      JSParseNode * semi_node;        JSParseNode * lexical_scope_node = function_node->pn_body;
347      switch (for_node->pn_right->pn_type) {        assert(lexical_scope_node->pn_type == TOK_LEXICALSCOPE);
348      case TOK_SEMI:        assert(lexical_scope_node->pn_arity == PN_NAME);
349        semi_node = for_node->pn_right;        JSParseNode * for_node = lexical_scope_node->pn_body;
350        break;        assert(for_node->pn_type == TOK_FOR);
351      case TOK_IF:        assert(for_node->pn_arity == PN_BINARY);
352        if_node = for_node->pn_right;        JSParseNode * if_node = NULL;
353        assert(if_node->pn_arity == PN_TERNARY);        JSParseNode * semi_node;
354        semi_node = if_node->pn_kid2;        switch (for_node->pn_right->pn_type) {
355        assert(semi_node->pn_type == TOK_SEMI);        case TOK_SEMI:
356        break;          semi_node = for_node->pn_right;
357      default:          break;
358        abort();        case TOK_IF:
359        break;          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      assert(semi_node->pn_arity == PN_UNARY);      else {
384      JSParseNode * yield_node = semi_node->pn_kid;        Stream_write_char(f, '(');
385      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);  
386        Stream_write_char(f, ')');        Stream_write_char(f, ')');
387      }      }
     Stream_write_char(f, ')');  
388    }    }
389    else {    else {
390      instrument_expression(node->pn_head, f);      instrument_expression(function_node, f);
391      Stream_write_char(f, '(');    }
392      for (struct JSParseNode * p = node->pn_head->pn_next; p != NULL; p = p->pn_next) {    Stream_write_char(f, '(');
393        if (p != node->pn_head->pn_next) {    for (struct JSParseNode * p = function_node->pn_next; p != NULL; p = p->pn_next) {
394          Stream_write_string(f, ", ");      if (p != node->pn_head->pn_next) {
395        }        Stream_write_string(f, ", ");
       instrument_expression(p, f);  
396      }      }
397      Stream_write_char(f, ')');      instrument_expression(p, f);
398    }    }
399      Stream_write_char(f, ')');
400  }  }
401    
402  static void instrument_declarations(JSParseNode * list, Stream * f) {  static void instrument_declarations(JSParseNode * list, Stream * f) {
# Line 583  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 868  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");

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

  ViewVC Help
Powered by ViewVC 1.1.24