/[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 342 by siliconforks, Fri Oct 24 16:15:04 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      case TOK_YIELD:
671        assert(node->pn_arity == PN_UNARY);
672        Stream_write_string(f, "yield ");
673        instrument_expression(node->pn_kid, f);
674        break;
675    default:    default:
676      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);
677    }    }
# Line 629  Line 681 
681    assert(node->pn_arity == PN_LIST);    assert(node->pn_arity == PN_LIST);
682    Stream_printf(f, "%*s", indent, "");    Stream_printf(f, "%*s", indent, "");
683    Stream_write_string(f, "var ");    Stream_write_string(f, "var ");
684    for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) {    instrument_declarations(node, f);
685      assert(p->pn_type == TOK_NAME);  }
686      assert(p->pn_arity == PN_NAME);  
687      if (p != node->pn_head) {  static void instrument_let_definition(JSParseNode * node, Stream * f, int indent) {
688        Stream_write_string(f, ", ");    assert(node->pn_arity == PN_LIST);
689      }    Stream_printf(f, "%*s", indent, "");
690      print_string_atom(p->pn_atom, f);    Stream_write_string(f, "let ");
691      if (p->pn_expr != NULL) {    instrument_declarations(node, f);
       Stream_write_string(f, " = ");  
       instrument_expression(p->pn_expr, f);  
     }  
   }  
692  }  }
693    
694  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 811 
811    case TOK_FOR:    case TOK_FOR:
812      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
813      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
814      Stream_write_string(f, "for (");      Stream_write_string(f, "for ");
815        if (node->pn_iflags & JSITER_FOREACH) {
816          Stream_write_string(f, "each ");
817        }
818        Stream_write_char(f, '(');
819      switch (node->pn_left->pn_type) {      switch (node->pn_left->pn_type) {
820      case TOK_IN:      case TOK_IN:
821        /* for/in */        /* for/in */
# Line 772  Line 824 
824        case TOK_VAR:        case TOK_VAR:
825          instrument_var_statement(node->pn_left->pn_left, f, 0);          instrument_var_statement(node->pn_left->pn_left, f, 0);
826          break;          break;
827          case TOK_LET:
828            instrument_let_definition(node->pn_left->pn_left, f, 0);
829            break;
830        case TOK_NAME:        case TOK_NAME:
831          instrument_expression(node->pn_left->pn_left, f);          instrument_expression(node->pn_left->pn_left, f);
832          break;          break;
# Line 793  Line 848 
848        /* for (;;) */        /* for (;;) */
849        assert(node->pn_left->pn_arity == PN_TERNARY);        assert(node->pn_left->pn_arity == PN_TERNARY);
850        if (node->pn_left->pn_kid1) {        if (node->pn_left->pn_kid1) {
851          if (node->pn_left->pn_kid1->pn_type == TOK_VAR) {          switch (node->pn_left->pn_kid1->pn_type) {
852            case TOK_VAR:
853            instrument_var_statement(node->pn_left->pn_kid1, f, 0);            instrument_var_statement(node->pn_left->pn_kid1, f, 0);
854          }            break;
855          else {          case TOK_LET:
856              instrument_let_definition(node->pn_left->pn_kid1, f, 0);
857              break;
858            default:
859            instrument_expression(node->pn_left->pn_kid1, f);            instrument_expression(node->pn_left->pn_kid1, f);
860              break;
861          }          }
862        }        }
863        Stream_write_string(f, ";");        Stream_write_string(f, ";");
# Line 840  Line 900 
900          assert(catch->pn_type == TOK_CATCH);          assert(catch->pn_type == TOK_CATCH);
901          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
902          Stream_write_string(f, "catch (");          Stream_write_string(f, "catch (");
903            /* this may not be a name - destructuring assignment */
904            /*
905          assert(catch->pn_kid1->pn_arity == PN_NAME);          assert(catch->pn_kid1->pn_arity == PN_NAME);
906          print_string_atom(catch->pn_kid1->pn_atom, f);          print_string_atom(catch->pn_kid1->pn_atom, f);
907            */
908            instrument_expression(catch->pn_kid1, f);
909          if (catch->pn_kid2) {          if (catch->pn_kid2) {
910            Stream_write_string(f, " if ");            Stream_write_string(f, " if ");
911            instrument_expression(catch->pn_kid2, f);            instrument_expression(catch->pn_kid2, f);
# Line 921  Line 985 
985      */      */
986      output_statement(node->pn_expr, f, indent, false);      output_statement(node->pn_expr, f, indent, false);
987      break;      break;
988      case TOK_LEXICALSCOPE:
989        /* let statement */
990        assert(node->pn_arity == PN_NAME);
991        switch (node->pn_expr->pn_type) {
992        case TOK_LET:
993          /* let statement */
994          assert(node->pn_expr->pn_arity == PN_BINARY);
995          instrument_statement(node->pn_expr, f, indent, false);
996          break;
997        case TOK_LC:
998          /* block */
999          Stream_printf(f, "%*s", indent, "");
1000          Stream_write_string(f, "{\n");
1001          instrument_statement(node->pn_expr, f, indent + 2, false);
1002          Stream_printf(f, "%*s", indent, "");
1003          Stream_write_string(f, "}\n");
1004          break;
1005        case TOK_FOR:
1006          instrument_statement(node->pn_expr, f, indent, false);
1007          break;
1008        default:
1009          abort();
1010          break;
1011        }
1012        break;
1013      case TOK_LET:
1014        switch (node->pn_arity) {
1015        case PN_BINARY:
1016          /* let statement */
1017          Stream_printf(f, "%*s", indent, "");
1018          Stream_write_string(f, "let (");
1019          assert(node->pn_left->pn_type == TOK_LP);
1020          assert(node->pn_left->pn_arity == PN_LIST);
1021          instrument_declarations(node->pn_left, f);
1022          Stream_write_string(f, ") {\n");
1023          instrument_statement(node->pn_right, f, indent + 2, false);
1024          Stream_printf(f, "%*s", indent, "");
1025          Stream_write_string(f, "}\n");
1026          break;
1027        case PN_LIST:
1028          /* let definition */
1029          instrument_let_definition(node, f, indent);
1030          Stream_write_string(f, ";\n");
1031          break;
1032        default:
1033          abort();
1034          break;
1035        }
1036        break;
1037    default:    default:
1038      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);
1039    }    }
# Line 932  Line 1045 
1045  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
1046  */  */
1047  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) {
1048    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC && node->pn_type != TOK_LEXICALSCOPE) {
1049      uint16_t line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
1050      if (line > num_lines) {      if (line > num_lines) {
1051        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.342

  ViewVC Help
Powered by ViewVC 1.1.24