/[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 336 by siliconforks, Thu Oct 23 20:08:29 2008 UTC revision 343 by siliconforks, Fri Oct 24 16:15:25 2008 UTC
# Line 31  Line 31 
31  #include <jsexn.h>  #include <jsexn.h>
32  #include <jsfun.h>  #include <jsfun.h>
33  #include <jsinterp.h>  #include <jsinterp.h>
34    #include <jsiter.h>
35  #include <jsparse.h>  #include <jsparse.h>
36  #include <jsregexp.h>  #include <jsregexp.h>
37  #include <jsscope.h>  #include <jsscope.h>
# Line 55  Line 56 
56  static JSRuntime * runtime = NULL;  static JSRuntime * runtime = NULL;
57  static JSContext * context = NULL;  static JSContext * context = NULL;
58  static JSObject * global = NULL;  static JSObject * global = NULL;
59    static JSVersion js_version = JSVERSION_ECMA_3;
60    
61  /*  /*
62  JSParseNode objects store line numbers starting from 1.  JSParseNode objects store line numbers starting from 1.
# Line 64  Line 66 
66  static char * lines = NULL;  static char * lines = NULL;
67  static uint16_t num_lines = 0;  static uint16_t num_lines = 0;
68    
69    void jscoverage_set_js_version(const char * version) {
70      js_version = atoi(version);
71    }
72    
73  void jscoverage_init(void) {  void jscoverage_init(void) {
74    runtime = JS_NewRuntime(8L * 1024L * 1024L);    runtime = JS_NewRuntime(8L * 1024L * 1024L);
75    if (runtime == NULL) {    if (runtime == NULL) {
# Line 75  Line 81 
81      fatal("cannot create context");      fatal("cannot create context");
82    }    }
83    
84      JS_SetVersion(context, js_version);
85    
86    global = JS_NewObject(context, NULL, NULL, NULL);    global = JS_NewObject(context, NULL, NULL, NULL);
87    if (global == NULL) {    if (global == NULL) {
88      fatal("cannot create global object");      fatal("cannot create global object");
# Line 316  Line 324 
324    Stream_write_char(f, ')');    Stream_write_char(f, ')');
325  }  }
326    
327    static void instrument_declarations(JSParseNode * list, Stream * f) {
328      assert(list->pn_arity == PN_LIST);
329      for (JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
330        switch (p->pn_type) {
331        case TOK_NAME:
332          assert(p->pn_arity == PN_NAME);
333          if (p != list->pn_head) {
334            Stream_write_string(f, ", ");
335          }
336          print_string_atom(p->pn_atom, f);
337          if (p->pn_expr != NULL) {
338            Stream_write_string(f, " = ");
339            instrument_expression(p->pn_expr, f);
340          }
341          break;
342        case TOK_ASSIGN:
343        case TOK_RB:
344        case TOK_RC:
345          /* destructuring */
346          instrument_expression(p, f);
347          break;
348        default:
349          abort();
350          break;
351        }
352      }
353    }
354    
355    static void output_for_in(JSParseNode * node, Stream * f) {
356      assert(node->pn_type == TOK_FOR);
357      assert(node->pn_arity == PN_BINARY);
358      Stream_write_string(f, "for ");
359      if (node->pn_iflags & JSITER_FOREACH) {
360        Stream_write_string(f, "each ");
361      }
362      Stream_write_char(f, '(');
363      instrument_expression(node->pn_left, f);
364      Stream_write_char(f, ')');
365    }
366    
367  /*  /*
368  See <Expressions> in jsparse.h.  See <Expressions> in jsparse.h.
369  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
# Line 620  Line 668 
668      Stream_write_string(f, " in ");      Stream_write_string(f, " in ");
669      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
670      break;      break;
671      case TOK_LEXICALSCOPE:
672        assert(node->pn_arity == PN_NAME);
673        assert(node->pn_expr->pn_type == TOK_LET);
674        assert(node->pn_expr->pn_arity == PN_BINARY);
675        Stream_write_string(f, "let(");
676        assert(node->pn_expr->pn_left->pn_type == TOK_LP);
677        assert(node->pn_expr->pn_left->pn_arity == PN_LIST);
678        instrument_declarations(node->pn_expr->pn_left, f);
679        Stream_write_string(f, ") ");
680        instrument_expression(node->pn_expr->pn_right, f);
681        break;
682      case TOK_YIELD:
683        assert(node->pn_arity == PN_UNARY);
684        Stream_write_string(f, "yield ");
685        instrument_expression(node->pn_kid, f);
686        break;
687      case TOK_ARRAYCOMP:
688        assert(node->pn_arity == PN_LIST);
689        {
690          JSParseNode * block_node;
691          switch (node->pn_count) {
692          case 1:
693            block_node = node->pn_head;
694            break;
695          case 2:
696            block_node = node->pn_head->pn_next;
697            break;
698          default:
699            abort();
700            break;
701          }
702          assert(block_node->pn_type == TOK_LEXICALSCOPE);
703          assert(block_node->pn_arity == PN_NAME);
704          JSParseNode * for_node = block_node->pn_expr;
705          assert(for_node->pn_type == TOK_FOR);
706          assert(for_node->pn_arity == PN_BINARY);
707          JSParseNode * push_node;
708          JSParseNode * if_node = NULL;
709          switch (for_node->pn_right->pn_type) {
710          case TOK_ARRAYPUSH:
711            push_node = for_node->pn_right;
712            assert(push_node->pn_arity == PN_UNARY);
713            break;
714          case TOK_IF:
715            if_node = for_node->pn_right;
716            assert(if_node->pn_arity == PN_TERNARY);
717            push_node = if_node->pn_kid2;
718            break;
719          default:
720            abort();
721            break;
722          }
723          Stream_write_char(f, '[');
724          instrument_expression(push_node->pn_kid, f);
725          Stream_write_char(f, ' ');
726          output_for_in(for_node, f);
727          if (if_node) {
728            Stream_write_string(f, " if (");
729            instrument_expression(if_node->pn_kid1, f);
730            Stream_write_char(f, ')');
731          }
732          Stream_write_char(f, ']');
733        }
734        break;
735      case TOK_VAR:
736        assert(node->pn_arity == PN_LIST);
737        Stream_write_string(f, "var ");
738        instrument_declarations(node, f);
739        break;
740      case TOK_LET:
741        assert(node->pn_arity == PN_LIST);
742        Stream_write_string(f, "let ");
743        instrument_declarations(node, f);
744        break;
745    default:    default:
746      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);
747    }    }
748  }  }
749    
 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 ");  
   for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) {  
     assert(p->pn_type == TOK_NAME);  
     assert(p->pn_arity == PN_NAME);  
     if (p != node->pn_head) {  
       Stream_write_string(f, ", ");  
     }  
     print_string_atom(p->pn_atom, f);  
     if (p->pn_expr != NULL) {  
       Stream_write_string(f, " = ");  
       instrument_expression(p->pn_expr, f);  
     }  
   }  
 }  
   
750  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) {
751    switch (node->pn_type) {    switch (node->pn_type) {
752    case TOK_FUNCTION:    case TOK_FUNCTION:
# Line 763  Line 867 
867    case TOK_FOR:    case TOK_FOR:
868      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
869      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
     Stream_write_string(f, "for (");  
870      switch (node->pn_left->pn_type) {      switch (node->pn_left->pn_type) {
871      case TOK_IN:      case TOK_IN:
872        /* for/in */        /* for/in */
873        assert(node->pn_left->pn_arity == PN_BINARY);        assert(node->pn_left->pn_arity == PN_BINARY);
874        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_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);  
875        break;        break;
876      case TOK_RESERVED:      case TOK_RESERVED:
877        /* for (;;) */        /* for (;;) */
878        assert(node->pn_left->pn_arity == PN_TERNARY);        assert(node->pn_left->pn_arity == PN_TERNARY);
879          Stream_write_string(f, "for (");
880        if (node->pn_left->pn_kid1) {        if (node->pn_left->pn_kid1) {
881          if (node->pn_left->pn_kid1->pn_type == TOK_VAR) {          instrument_expression(node->pn_left->pn_kid1, f);
           instrument_var_statement(node->pn_left->pn_kid1, f, 0);  
         }  
         else {  
           instrument_expression(node->pn_left->pn_kid1, f);  
         }  
882        }        }
883        Stream_write_string(f, ";");        Stream_write_string(f, ";");
884        if (node->pn_left->pn_kid2) {        if (node->pn_left->pn_kid2) {
# Line 810  Line 890 
890          Stream_write_char(f, ' ');          Stream_write_char(f, ' ');
891          instrument_expression(node->pn_left->pn_kid3, f);          instrument_expression(node->pn_left->pn_kid3, f);
892        }        }
893          Stream_write_char(f, ')');
894        break;        break;
895      default:      default:
896        abort();        abort();
897        break;        break;
898      }      }
899      Stream_write_string(f, ") {\n");      Stream_write_string(f, " {\n");
900      instrument_statement(node->pn_right, f, indent + 2, false);      instrument_statement(node->pn_right, f, indent + 2, false);
901      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
902      break;      break;
# Line 840  Line 921 
921          assert(catch->pn_type == TOK_CATCH);          assert(catch->pn_type == TOK_CATCH);
922          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
923          Stream_write_string(f, "catch (");          Stream_write_string(f, "catch (");
924            /* this may not be a name - destructuring assignment */
925            /*
926          assert(catch->pn_kid1->pn_arity == PN_NAME);          assert(catch->pn_kid1->pn_arity == PN_NAME);
927          print_string_atom(catch->pn_kid1->pn_atom, f);          print_string_atom(catch->pn_kid1->pn_atom, f);
928            */
929            instrument_expression(catch->pn_kid1, f);
930          if (catch->pn_kid2) {          if (catch->pn_kid2) {
931            Stream_write_string(f, " if ");            Stream_write_string(f, " if ");
932            instrument_expression(catch->pn_kid2, f);            instrument_expression(catch->pn_kid2, f);
# Line 886  Line 971 
971      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
972      break;      break;
973    case TOK_VAR:    case TOK_VAR:
974      instrument_var_statement(node, f, indent);      Stream_printf(f, "%*s", indent, "");
975        instrument_expression(node, f);
976      Stream_write_string(f, ";\n");      Stream_write_string(f, ";\n");
977      break;      break;
978    case TOK_RETURN:    case TOK_RETURN:
# Line 921  Line 1007 
1007      */      */
1008      output_statement(node->pn_expr, f, indent, false);      output_statement(node->pn_expr, f, indent, false);
1009      break;      break;
1010      case TOK_LEXICALSCOPE:
1011        /* let statement */
1012        assert(node->pn_arity == PN_NAME);
1013        switch (node->pn_expr->pn_type) {
1014        case TOK_LET:
1015          /* let statement */
1016          assert(node->pn_expr->pn_arity == PN_BINARY);
1017          instrument_statement(node->pn_expr, f, indent, false);
1018          break;
1019        case TOK_LC:
1020          /* block */
1021          Stream_printf(f, "%*s", indent, "");
1022          Stream_write_string(f, "{\n");
1023          instrument_statement(node->pn_expr, f, indent + 2, false);
1024          Stream_printf(f, "%*s", indent, "");
1025          Stream_write_string(f, "}\n");
1026          break;
1027        case TOK_FOR:
1028          instrument_statement(node->pn_expr, f, indent, false);
1029          break;
1030        default:
1031          abort();
1032          break;
1033        }
1034        break;
1035      case TOK_LET:
1036        switch (node->pn_arity) {
1037        case PN_BINARY:
1038          /* let statement */
1039          Stream_printf(f, "%*s", indent, "");
1040          Stream_write_string(f, "let (");
1041          assert(node->pn_left->pn_type == TOK_LP);
1042          assert(node->pn_left->pn_arity == PN_LIST);
1043          instrument_declarations(node->pn_left, f);
1044          Stream_write_string(f, ") {\n");
1045          instrument_statement(node->pn_right, f, indent + 2, false);
1046          Stream_printf(f, "%*s", indent, "");
1047          Stream_write_string(f, "}\n");
1048          break;
1049        case PN_LIST:
1050          /* let definition */
1051          Stream_printf(f, "%*s", indent, "");
1052          instrument_expression(node, f);
1053          Stream_write_string(f, ";\n");
1054          break;
1055        default:
1056          abort();
1057          break;
1058        }
1059        break;
1060    default:    default:
1061      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);
1062    }    }
# Line 932  Line 1068 
1068  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
1069  */  */
1070  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) {
1071    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC && node->pn_type != TOK_LEXICALSCOPE) {
1072      uint16_t line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
1073      if (line > num_lines) {      if (line > num_lines) {
1074        fatal("%s: script contains more than 65,535 lines", file_id);        fatal("%s: script contains more than 65,535 lines", file_id);

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

  ViewVC Help
Powered by ViewVC 1.1.24