/[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 343 by siliconforks, Fri Oct 24 16:15:25 2008 UTC revision 352 by siliconforks, Fri Oct 24 16:18:18 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 352  Line 430 
430    }    }
431  }  }
432    
 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, ')');  
 }  
   
433  /*  /*
434  See <Expressions> in jsparse.h.  See <Expressions> in jsparse.h.
435  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
# Line 535  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 704  Line 782 
782        JSParseNode * for_node = block_node->pn_expr;        JSParseNode * for_node = block_node->pn_expr;
783        assert(for_node->pn_type == TOK_FOR);        assert(for_node->pn_type == TOK_FOR);
784        assert(for_node->pn_arity == PN_BINARY);        assert(for_node->pn_arity == PN_BINARY);
       JSParseNode * push_node;  
785        JSParseNode * if_node = NULL;        JSParseNode * if_node = NULL;
786          JSParseNode * push_node;
787        switch (for_node->pn_right->pn_type) {        switch (for_node->pn_right->pn_type) {
788        case TOK_ARRAYPUSH:        case TOK_ARRAYPUSH:
789          push_node = for_node->pn_right;          push_node = for_node->pn_right;
# Line 820  Line 898 
898      Stream_write_string(f, "switch (");      Stream_write_string(f, "switch (");
899      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
900      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
901      for (struct JSParseNode * p = node->pn_right->pn_head; p != NULL; p = p->pn_next) {      {
902        Stream_printf(f, "%*s", indent, "");        JSParseNode * list = node->pn_right;
903        switch (p->pn_type) {        if (list->pn_type == TOK_LEXICALSCOPE) {
904        case TOK_CASE:          list = list->pn_expr;
905          Stream_write_string(f, "case ");        }
906          instrument_expression(p->pn_left, f);        for (struct JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
907          Stream_write_string(f, ":\n");          Stream_printf(f, "%*s", indent, "");
908          break;          switch (p->pn_type) {
909        case TOK_DEFAULT:          case TOK_CASE:
910          Stream_write_string(f, "default:\n");            Stream_write_string(f, "case ");
911          break;            instrument_expression(p->pn_left, f);
912        default:            Stream_write_string(f, ":\n");
913          abort();            break;
914          break;          case TOK_DEFAULT:
915              Stream_write_string(f, "default:\n");
916              break;
917            default:
918              abort();
919              break;
920            }
921            instrument_statement(p->pn_right, f, indent + 2, false);
922        }        }
       instrument_statement(p->pn_right, f, indent + 2, false);  
923      }      }
924      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
925      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
# Line 1057  Line 1141 
1141        break;        break;
1142      }      }
1143      break;      break;
1144      case TOK_DEBUGGER:
1145        Stream_printf(f, "%*s", indent, "");
1146        Stream_write_string(f, "debugger;\n");
1147        break;
1148    default:    default:
1149      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);
1150    }    }

Legend:
Removed from v.343  
changed lines
  Added in v.352

  ViewVC Help
Powered by ViewVC 1.1.24