/[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 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>
35    #include <jsiter.h>
36  #include <jsparse.h>  #include <jsparse.h>
37  #include <jsregexp.h>  #include <jsregexp.h>
38  #include <jsscope.h>  #include <jsscope.h>
# Line 55  Line 57 
57  static JSRuntime * runtime = NULL;  static JSRuntime * runtime = NULL;
58  static JSContext * context = NULL;  static JSContext * context = NULL;
59  static JSObject * global = NULL;  static JSObject * global = NULL;
60    static JSVersion js_version = JSVERSION_ECMA_3;
61    
62  /*  /*
63  JSParseNode objects store line numbers starting from 1.  JSParseNode objects store line numbers starting from 1.
# Line 64  Line 67 
67  static char * lines = NULL;  static char * lines = NULL;
68  static uint16_t num_lines = 0;  static uint16_t num_lines = 0;
69    
70    void jscoverage_set_js_version(const char * version) {
71      js_version = atoi(version);
72    }
73    
74  void jscoverage_init(void) {  void jscoverage_init(void) {
75    runtime = JS_NewRuntime(8L * 1024L * 1024L);    runtime = JS_NewRuntime(8L * 1024L * 1024L);
76    if (runtime == NULL) {    if (runtime == NULL) {
# Line 75  Line 82 
82      fatal("cannot create context");      fatal("cannot create context");
83    }    }
84    
85      JS_SetVersion(context, js_version);
86    
87    global = JS_NewObject(context, NULL, NULL, NULL);    global = JS_NewObject(context, NULL, NULL, NULL);
88    if (global == NULL) {    if (global == NULL) {
89      fatal("cannot create global object");      fatal("cannot create global object");
# Line 249  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 299  Line 321 
321    Stream_write_string(f, ") {\n");    Stream_write_string(f, ") {\n");
322    
323    /* function body */    /* function body */
324    instrument_statement(node->pn_body, f, indent + 2, false);    if (function->flags & JSFUN_EXPR_CLOSURE) {
325        /* expression closure */
326        output_statement(node->pn_body, f, indent + 2, false);
327      }
328      else {
329        instrument_statement(node->pn_body, f, indent + 2, false);
330      }
331    
332    Stream_write_string(f, "}\n");    Stream_write_string(f, "}\n");
333  }  }
334    
335  static void instrument_function_call(JSParseNode * node, Stream * f) {  static void instrument_function_call(JSParseNode * node, Stream * f) {
336    instrument_expression(node->pn_head, f);    JSParseNode * function_node = node->pn_head;
337      if (function_node->pn_type == TOK_FUNCTION) {
338        JSObject * object = function_node->pn_funpob->object;
339        assert(JS_ObjectIsFunction(context, object));
340        JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
341        assert(function);
342        assert(object == &function->object);
343    
344        if (function_node->pn_flags & TCF_GENEXP_LAMBDA) {
345          /* it's a generator expression */
346          JSParseNode * lexical_scope_node = function_node->pn_body;
347          assert(lexical_scope_node->pn_type == TOK_LEXICALSCOPE);
348          assert(lexical_scope_node->pn_arity == PN_NAME);
349          JSParseNode * for_node = lexical_scope_node->pn_body;
350          assert(for_node->pn_type == TOK_FOR);
351          assert(for_node->pn_arity == PN_BINARY);
352          JSParseNode * if_node = NULL;
353          JSParseNode * semi_node;
354          switch (for_node->pn_right->pn_type) {
355          case TOK_SEMI:
356            semi_node = for_node->pn_right;
357            break;
358          case TOK_IF:
359            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        else {
384          Stream_write_char(f, '(');
385          instrument_expression(function_node, f);
386          Stream_write_char(f, ')');
387        }
388      }
389      else {
390        instrument_expression(function_node, f);
391      }
392    Stream_write_char(f, '(');    Stream_write_char(f, '(');
393    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) {
394      if (p != node->pn_head->pn_next) {      if (p != node->pn_head->pn_next) {
395        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
396      }      }
# Line 316  Line 399 
399    Stream_write_char(f, ')');    Stream_write_char(f, ')');
400  }  }
401    
402    static void instrument_declarations(JSParseNode * list, Stream * f) {
403      assert(list->pn_arity == PN_LIST);
404      for (JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
405        switch (p->pn_type) {
406        case TOK_NAME:
407          assert(p->pn_arity == PN_NAME);
408          if (p != list->pn_head) {
409            Stream_write_string(f, ", ");
410          }
411          print_string_atom(p->pn_atom, f);
412          if (p->pn_expr != NULL) {
413            Stream_write_string(f, " = ");
414            instrument_expression(p->pn_expr, f);
415          }
416          break;
417        case TOK_ASSIGN:
418        case TOK_RB:
419        case TOK_RC:
420          /* destructuring */
421          instrument_expression(p, f);
422          break;
423        default:
424          abort();
425          break;
426        }
427      }
428    }
429    
430  /*  /*
431  See <Expressions> in jsparse.h.  See <Expressions> in jsparse.h.
432  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
# Line 487  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 620  Line 743 
743      Stream_write_string(f, " in ");      Stream_write_string(f, " in ");
744      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
745      break;      break;
746      case TOK_LEXICALSCOPE:
747        assert(node->pn_arity == PN_NAME);
748        assert(node->pn_expr->pn_type == TOK_LET);
749        assert(node->pn_expr->pn_arity == PN_BINARY);
750        Stream_write_string(f, "let(");
751        assert(node->pn_expr->pn_left->pn_type == TOK_LP);
752        assert(node->pn_expr->pn_left->pn_arity == PN_LIST);
753        instrument_declarations(node->pn_expr->pn_left, f);
754        Stream_write_string(f, ") ");
755        instrument_expression(node->pn_expr->pn_right, f);
756        break;
757      case TOK_YIELD:
758        assert(node->pn_arity == PN_UNARY);
759        Stream_write_string(f, "yield ");
760        instrument_expression(node->pn_kid, f);
761        break;
762      case TOK_ARRAYCOMP:
763        assert(node->pn_arity == PN_LIST);
764        {
765          JSParseNode * block_node;
766          switch (node->pn_count) {
767          case 1:
768            block_node = node->pn_head;
769            break;
770          case 2:
771            block_node = node->pn_head->pn_next;
772            break;
773          default:
774            abort();
775            break;
776          }
777          assert(block_node->pn_type == TOK_LEXICALSCOPE);
778          assert(block_node->pn_arity == PN_NAME);
779          JSParseNode * for_node = block_node->pn_expr;
780          assert(for_node->pn_type == TOK_FOR);
781          assert(for_node->pn_arity == PN_BINARY);
782          JSParseNode * if_node = NULL;
783          JSParseNode * push_node;
784          switch (for_node->pn_right->pn_type) {
785          case TOK_ARRAYPUSH:
786            push_node = for_node->pn_right;
787            assert(push_node->pn_arity == PN_UNARY);
788            break;
789          case TOK_IF:
790            if_node = for_node->pn_right;
791            assert(if_node->pn_arity == PN_TERNARY);
792            push_node = if_node->pn_kid2;
793            break;
794          default:
795            abort();
796            break;
797          }
798          Stream_write_char(f, '[');
799          instrument_expression(push_node->pn_kid, f);
800          Stream_write_char(f, ' ');
801          output_for_in(for_node, f);
802          if (if_node) {
803            Stream_write_string(f, " if (");
804            instrument_expression(if_node->pn_kid1, f);
805            Stream_write_char(f, ')');
806          }
807          Stream_write_char(f, ']');
808        }
809        break;
810      case TOK_VAR:
811        assert(node->pn_arity == PN_LIST);
812        Stream_write_string(f, "var ");
813        instrument_declarations(node, f);
814        break;
815      case TOK_LET:
816        assert(node->pn_arity == PN_LIST);
817        Stream_write_string(f, "let ");
818        instrument_declarations(node, f);
819        break;
820    default:    default:
821      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);
822    }    }
823  }  }
824    
 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);  
     }  
   }  
 }  
   
825  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) {
826    switch (node->pn_type) {    switch (node->pn_type) {
827    case TOK_FUNCTION:    case TOK_FUNCTION:
# Line 716  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");
# Line 763  Line 948 
948    case TOK_FOR:    case TOK_FOR:
949      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
950      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
     Stream_write_string(f, "for (");  
951      switch (node->pn_left->pn_type) {      switch (node->pn_left->pn_type) {
952      case TOK_IN:      case TOK_IN:
953        /* for/in */        /* for/in */
954        assert(node->pn_left->pn_arity == PN_BINARY);        assert(node->pn_left->pn_arity == PN_BINARY);
955        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);  
956        break;        break;
957      case TOK_RESERVED:      case TOK_RESERVED:
958        /* for (;;) */        /* for (;;) */
959        assert(node->pn_left->pn_arity == PN_TERNARY);        assert(node->pn_left->pn_arity == PN_TERNARY);
960          Stream_write_string(f, "for (");
961        if (node->pn_left->pn_kid1) {        if (node->pn_left->pn_kid1) {
962          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);  
         }  
963        }        }
964        Stream_write_string(f, ";");        Stream_write_string(f, ";");
965        if (node->pn_left->pn_kid2) {        if (node->pn_left->pn_kid2) {
# Line 810  Line 971 
971          Stream_write_char(f, ' ');          Stream_write_char(f, ' ');
972          instrument_expression(node->pn_left->pn_kid3, f);          instrument_expression(node->pn_left->pn_kid3, f);
973        }        }
974          Stream_write_char(f, ')');
975        break;        break;
976      default:      default:
977        abort();        abort();
978        break;        break;
979      }      }
980      Stream_write_string(f, ") {\n");      Stream_write_string(f, " {\n");
981      instrument_statement(node->pn_right, f, indent + 2, false);      instrument_statement(node->pn_right, f, indent + 2, false);
982      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
983      break;      break;
# Line 840  Line 1002 
1002          assert(catch->pn_type == TOK_CATCH);          assert(catch->pn_type == TOK_CATCH);
1003          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
1004          Stream_write_string(f, "catch (");          Stream_write_string(f, "catch (");
1005            /* this may not be a name - destructuring assignment */
1006            /*
1007          assert(catch->pn_kid1->pn_arity == PN_NAME);          assert(catch->pn_kid1->pn_arity == PN_NAME);
1008          print_string_atom(catch->pn_kid1->pn_atom, f);          print_string_atom(catch->pn_kid1->pn_atom, f);
1009            */
1010            instrument_expression(catch->pn_kid1, f);
1011          if (catch->pn_kid2) {          if (catch->pn_kid2) {
1012            Stream_write_string(f, " if ");            Stream_write_string(f, " if ");
1013            instrument_expression(catch->pn_kid2, f);            instrument_expression(catch->pn_kid2, f);
# Line 886  Line 1052 
1052      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
1053      break;      break;
1054    case TOK_VAR:    case TOK_VAR:
1055      instrument_var_statement(node, f, indent);      Stream_printf(f, "%*s", indent, "");
1056        instrument_expression(node, f);
1057      Stream_write_string(f, ";\n");      Stream_write_string(f, ";\n");
1058      break;      break;
1059    case TOK_RETURN:    case TOK_RETURN:
# Line 921  Line 1088 
1088      */      */
1089      output_statement(node->pn_expr, f, indent, false);      output_statement(node->pn_expr, f, indent, false);
1090      break;      break;
1091      case TOK_LEXICALSCOPE:
1092        /* let statement */
1093        assert(node->pn_arity == PN_NAME);
1094        switch (node->pn_expr->pn_type) {
1095        case TOK_LET:
1096          /* let statement */
1097          assert(node->pn_expr->pn_arity == PN_BINARY);
1098          instrument_statement(node->pn_expr, f, indent, false);
1099          break;
1100        case TOK_LC:
1101          /* block */
1102          Stream_printf(f, "%*s", indent, "");
1103          Stream_write_string(f, "{\n");
1104          instrument_statement(node->pn_expr, f, indent + 2, false);
1105          Stream_printf(f, "%*s", indent, "");
1106          Stream_write_string(f, "}\n");
1107          break;
1108        case TOK_FOR:
1109          instrument_statement(node->pn_expr, f, indent, false);
1110          break;
1111        default:
1112          abort();
1113          break;
1114        }
1115        break;
1116      case TOK_LET:
1117        switch (node->pn_arity) {
1118        case PN_BINARY:
1119          /* let statement */
1120          Stream_printf(f, "%*s", indent, "");
1121          Stream_write_string(f, "let (");
1122          assert(node->pn_left->pn_type == TOK_LP);
1123          assert(node->pn_left->pn_arity == PN_LIST);
1124          instrument_declarations(node->pn_left, f);
1125          Stream_write_string(f, ") {\n");
1126          instrument_statement(node->pn_right, f, indent + 2, false);
1127          Stream_printf(f, "%*s", indent, "");
1128          Stream_write_string(f, "}\n");
1129          break;
1130        case PN_LIST:
1131          /* let definition */
1132          Stream_printf(f, "%*s", indent, "");
1133          instrument_expression(node, f);
1134          Stream_write_string(f, ";\n");
1135          break;
1136        default:
1137          abort();
1138          break;
1139        }
1140        break;
1141      case TOK_DEBUGGER:
1142        Stream_printf(f, "%*s", indent, "");
1143        Stream_write_string(f, "debugger;\n");
1144        break;
1145    default:    default:
1146      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);
1147    }    }
# Line 932  Line 1153 
1153  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
1154  */  */
1155  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) {
1156    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC && node->pn_type != TOK_LEXICALSCOPE) {
1157      uint16_t line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
1158      if (line > num_lines) {      if (line > num_lines) {
1159        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.351

  ViewVC Help
Powered by ViewVC 1.1.24