/[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 341 by siliconforks, Fri Oct 24 16:14:43 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  /*  /*
356  See <Expressions> in jsparse.h.  See <Expressions> in jsparse.h.
357  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 656 
656      Stream_write_string(f, " in ");      Stream_write_string(f, " in ");
657      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
658      break;      break;
659      case TOK_LEXICALSCOPE:
660        assert(node->pn_arity == PN_NAME);
661        assert(node->pn_expr->pn_type == TOK_LET);
662        assert(node->pn_expr->pn_arity == PN_BINARY);
663        Stream_write_string(f, "let(");
664        assert(node->pn_expr->pn_left->pn_type == TOK_LP);
665        assert(node->pn_expr->pn_left->pn_arity == PN_LIST);
666        instrument_declarations(node->pn_expr->pn_left, f);
667        Stream_write_string(f, ") ");
668        instrument_expression(node->pn_expr->pn_right, f);
669        break;
670    default:    default:
671      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);
672    }    }
# Line 629  Line 676 
676    assert(node->pn_arity == PN_LIST);    assert(node->pn_arity == PN_LIST);
677    Stream_printf(f, "%*s", indent, "");    Stream_printf(f, "%*s", indent, "");
678    Stream_write_string(f, "var ");    Stream_write_string(f, "var ");
679    for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) {    instrument_declarations(node, f);
680      assert(p->pn_type == TOK_NAME);  }
681      assert(p->pn_arity == PN_NAME);  
682      if (p != node->pn_head) {  static void instrument_let_definition(JSParseNode * node, Stream * f, int indent) {
683        Stream_write_string(f, ", ");    assert(node->pn_arity == PN_LIST);
684      }    Stream_printf(f, "%*s", indent, "");
685      print_string_atom(p->pn_atom, f);    Stream_write_string(f, "let ");
686      if (p->pn_expr != NULL) {    instrument_declarations(node, f);
       Stream_write_string(f, " = ");  
       instrument_expression(p->pn_expr, f);  
     }  
   }  
687  }  }
688    
689  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) {
# Line 763  Line 806 
806    case TOK_FOR:    case TOK_FOR:
807      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
808      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
809      Stream_write_string(f, "for (");      Stream_write_string(f, "for ");
810        if (node->pn_iflags & JSITER_FOREACH) {
811          Stream_write_string(f, "each ");
812        }
813        Stream_write_char(f, '(');
814      switch (node->pn_left->pn_type) {      switch (node->pn_left->pn_type) {
815      case TOK_IN:      case TOK_IN:
816        /* for/in */        /* for/in */
# Line 772  Line 819 
819        case TOK_VAR:        case TOK_VAR:
820          instrument_var_statement(node->pn_left->pn_left, f, 0);          instrument_var_statement(node->pn_left->pn_left, f, 0);
821          break;          break;
822          case TOK_LET:
823            instrument_let_definition(node->pn_left->pn_left, f, 0);
824            break;
825        case TOK_NAME:        case TOK_NAME:
826          instrument_expression(node->pn_left->pn_left, f);          instrument_expression(node->pn_left->pn_left, f);
827          break;          break;
# Line 793  Line 843 
843        /* for (;;) */        /* for (;;) */
844        assert(node->pn_left->pn_arity == PN_TERNARY);        assert(node->pn_left->pn_arity == PN_TERNARY);
845        if (node->pn_left->pn_kid1) {        if (node->pn_left->pn_kid1) {
846          if (node->pn_left->pn_kid1->pn_type == TOK_VAR) {          switch (node->pn_left->pn_kid1->pn_type) {
847            case TOK_VAR:
848            instrument_var_statement(node->pn_left->pn_kid1, f, 0);            instrument_var_statement(node->pn_left->pn_kid1, f, 0);
849          }            break;
850          else {          case TOK_LET:
851              instrument_let_definition(node->pn_left->pn_kid1, f, 0);
852              break;
853            default:
854            instrument_expression(node->pn_left->pn_kid1, f);            instrument_expression(node->pn_left->pn_kid1, f);
855              break;
856          }          }
857        }        }
858        Stream_write_string(f, ";");        Stream_write_string(f, ";");
# Line 840  Line 895 
895          assert(catch->pn_type == TOK_CATCH);          assert(catch->pn_type == TOK_CATCH);
896          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
897          Stream_write_string(f, "catch (");          Stream_write_string(f, "catch (");
898            /* this may not be a name - destructuring assignment */
899            /*
900          assert(catch->pn_kid1->pn_arity == PN_NAME);          assert(catch->pn_kid1->pn_arity == PN_NAME);
901          print_string_atom(catch->pn_kid1->pn_atom, f);          print_string_atom(catch->pn_kid1->pn_atom, f);
902            */
903            instrument_expression(catch->pn_kid1, f);
904          if (catch->pn_kid2) {          if (catch->pn_kid2) {
905            Stream_write_string(f, " if ");            Stream_write_string(f, " if ");
906            instrument_expression(catch->pn_kid2, f);            instrument_expression(catch->pn_kid2, f);
# Line 921  Line 980 
980      */      */
981      output_statement(node->pn_expr, f, indent, false);      output_statement(node->pn_expr, f, indent, false);
982      break;      break;
983      case TOK_LEXICALSCOPE:
984        /* let statement */
985        assert(node->pn_arity == PN_NAME);
986        switch (node->pn_expr->pn_type) {
987        case TOK_LET:
988          /* let statement */
989          assert(node->pn_expr->pn_arity == PN_BINARY);
990          instrument_statement(node->pn_expr, f, indent, false);
991          break;
992        case TOK_LC:
993          /* block */
994          Stream_printf(f, "%*s", indent, "");
995          Stream_write_string(f, "{\n");
996          instrument_statement(node->pn_expr, f, indent + 2, false);
997          Stream_printf(f, "%*s", indent, "");
998          Stream_write_string(f, "}\n");
999          break;
1000        case TOK_FOR:
1001          instrument_statement(node->pn_expr, f, indent, false);
1002          break;
1003        default:
1004          abort();
1005          break;
1006        }
1007        break;
1008      case TOK_LET:
1009        switch (node->pn_arity) {
1010        case PN_BINARY:
1011          /* let statement */
1012          Stream_printf(f, "%*s", indent, "");
1013          Stream_write_string(f, "let (");
1014          assert(node->pn_left->pn_type == TOK_LP);
1015          assert(node->pn_left->pn_arity == PN_LIST);
1016          instrument_declarations(node->pn_left, f);
1017          Stream_write_string(f, ") {\n");
1018          instrument_statement(node->pn_right, f, indent + 2, false);
1019          Stream_printf(f, "%*s", indent, "");
1020          Stream_write_string(f, "}\n");
1021          break;
1022        case PN_LIST:
1023          /* let definition */
1024          instrument_let_definition(node, f, indent);
1025          Stream_write_string(f, ";\n");
1026          break;
1027        default:
1028          abort();
1029          break;
1030        }
1031        break;
1032    default:    default:
1033      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);
1034    }    }
# Line 932  Line 1040 
1040  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
1041  */  */
1042  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) {
1043    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC && node->pn_type != TOK_LEXICALSCOPE) {
1044      uint16_t line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
1045      if (line > num_lines) {      if (line > num_lines) {
1046        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.341

  ViewVC Help
Powered by ViewVC 1.1.24