/[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 214 by siliconforks, Fri Oct 3 02:26:04 2008 UTC revision 339 by siliconforks, Fri Oct 24 16:13:57 2008 UTC
# Line 26  Line 26 
26  #include <string.h>  #include <string.h>
27    
28  #include <jsapi.h>  #include <jsapi.h>
29    #include <jsarena.h>
30  #include <jsatom.h>  #include <jsatom.h>
31    #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 40  Line 43 
43  #include "resource-manager.h"  #include "resource-manager.h"
44  #include "util.h"  #include "util.h"
45    
46    struct IfDirective {
47      const jschar * condition_start;
48      const jschar * condition_end;
49      uint16_t start_line;
50      uint16_t end_line;
51      struct IfDirective * next;
52    };
53    
54    static bool * exclusive_directives = NULL;
55    
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 52  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 63  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 78  Line 98 
98    JS_DestroyRuntime(runtime);    JS_DestroyRuntime(runtime);
99  }  }
100    
101    static void print_javascript(const jschar * characters, size_t num_characters, Stream * f) {
102      for (size_t i = 0; i < num_characters; i++) {
103        jschar c = characters[i];
104        /*
105        XXX does not handle no-break space, other unicode "space separator"
106        */
107        switch (c) {
108        case 0x9:
109        case 0xB:
110        case 0xC:
111          Stream_write_char(f, c);
112          break;
113        default:
114          if (32 <= c && c <= 126) {
115            Stream_write_char(f, c);
116          }
117          else {
118            Stream_printf(f, "\\u%04x", c);
119          }
120          break;
121        }
122      }
123    }
124    
125  static void print_string(JSString * s, Stream * f) {  static void print_string(JSString * s, Stream * f) {
126    size_t length = JSSTRING_LENGTH(s);    size_t length = JSSTRING_LENGTH(s);
127    jschar * characters = JSSTRING_CHARS(s);    jschar * characters = JSSTRING_CHARS(s);
# Line 112  Line 156 
156        case 0xa:        case 0xa:
157          Stream_write_string(f, "\\n");          Stream_write_string(f, "\\n");
158          break;          break;
159          /* IE doesn't support this */
160          /*
161        case 0xb:        case 0xb:
162          Stream_write_string(f, "\\v");          Stream_write_string(f, "\\v");
163          break;          break;
164          */
165        case 0xc:        case 0xc:
166          Stream_write_string(f, "\\f");          Stream_write_string(f, "\\f");
167          break;          break;
# Line 161  Line 208 
208    
209  static const char * get_op(uint8 op) {  static const char * get_op(uint8 op) {
210    switch(op) {    switch(op) {
211      case JSOP_OR:
212        return "||";
213      case JSOP_AND:
214        return "&&";
215    case JSOP_BITOR:    case JSOP_BITOR:
216      return "|";      return "|";
217    case JSOP_BITXOR:    case JSOP_BITXOR:
# Line 171  Line 222 
222      return "==";      return "==";
223    case JSOP_NE:    case JSOP_NE:
224      return "!=";      return "!=";
225    case JSOP_NEW_EQ:    case JSOP_STRICTEQ:
226      return "===";      return "===";
227    case JSOP_NEW_NE:    case JSOP_STRICTNE:
228      return "!==";      return "!==";
229    case JSOP_LT:    case JSOP_LT:
230      return "<";      return "<";
# Line 205  Line 256 
256  }  }
257    
258  static void instrument_expression(JSParseNode * node, Stream * f);  static void instrument_expression(JSParseNode * node, Stream * f);
259  static void instrument_statement(JSParseNode * node, Stream * f, int indent);  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
260    
261  enum FunctionType {  enum FunctionType {
262    FUNCTION_NORMAL,    FUNCTION_NORMAL,
# Line 213  Line 264 
264  };  };
265    
266  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) {
267      assert(node->pn_type == TOK_FUNCTION);
268    assert(node->pn_arity == PN_FUNC);    assert(node->pn_arity == PN_FUNC);
269    assert(ATOM_IS_OBJECT(node->pn_funAtom));    JSObject * object = node->pn_funpob->object;
   JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom);  
270    assert(JS_ObjectIsFunction(context, object));    assert(JS_ObjectIsFunction(context, object));
271    JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);    JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
272    assert(function);    assert(function);
273    assert(object == function->object);    assert(object == &function->object);
274    Stream_printf(f, "%*s", indent, "");    Stream_printf(f, "%*s", indent, "");
275    if (type == FUNCTION_NORMAL) {    if (type == FUNCTION_NORMAL) {
276      Stream_write_string(f, "function");      Stream_write_string(f, "function");
# Line 231  Line 282 
282      print_string_atom(function->atom, f);      print_string_atom(function->atom, f);
283    }    }
284    
285    /* function parameters */    /*
286      function parameters - see JS_DecompileFunction in jsapi.cpp, which calls
287      js_DecompileFunction in jsopcode.cpp
288      */
289    Stream_write_string(f, "(");    Stream_write_string(f, "(");
290    JSAtom ** params = xnew(JSAtom *, function->nargs);    JSArenaPool pool;
291    for (int i = 0; i < function->nargs; i++) {    JS_INIT_ARENA_POOL(&pool, "instrument_function", 256, 1, &context->scriptStackQuota);
292      /* initialize to NULL for sanity check */    jsuword * local_names = NULL;
293      params[i] = NULL;    if (JS_GET_LOCAL_NAME_COUNT(function)) {
294    }      local_names = js_GetLocalNameArray(context, function, &pool);
295    JSScope * scope = OBJ_SCOPE(object);      if (local_names == NULL) {
296    for (JSScopeProperty * scope_property = SCOPE_LAST_PROP(scope); scope_property != NULL; scope_property = scope_property->parent) {        fatal("out of memory");
     if (scope_property->getter != js_GetArgument) {  
       continue;  
297      }      }
     assert(scope_property->flags & SPROP_HAS_SHORTID);  
     assert((uint16) scope_property->shortid < function->nargs);  
     assert(JSID_IS_ATOM(scope_property->id));  
     params[(uint16) scope_property->shortid] = JSID_TO_ATOM(scope_property->id);  
298    }    }
299    for (int i = 0; i < function->nargs; i++) {    for (int i = 0; i < function->nargs; i++) {
     assert(params[i] != NULL);  
300      if (i > 0) {      if (i > 0) {
301        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
302      }      }
303      if (ATOM_IS_STRING(params[i])) {      JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]);
304        print_string_atom(params[i], f);      print_string_atom(param, f);
     }  
305    }    }
306      JS_FinishArenaPool(&pool);
307    Stream_write_string(f, ") {\n");    Stream_write_string(f, ") {\n");
   free(params);  
308    
309    /* function body */    /* function body */
310    instrument_statement(node->pn_body, f, indent + 2);    instrument_statement(node->pn_body, f, indent + 2, false);
311    
312    Stream_write_string(f, "}\n");    Stream_write_string(f, "}\n");
313  }  }
# Line 336  Line 382 
382      instrument_expression(node->pn_kid3, f);      instrument_expression(node->pn_kid3, f);
383      break;      break;
384    case TOK_OR:    case TOK_OR:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " || ");  
     instrument_expression(node->pn_right, f);  
     break;  
385    case TOK_AND:    case TOK_AND:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " && ");  
     instrument_expression(node->pn_right, f);  
     break;  
386    case TOK_BITOR:    case TOK_BITOR:
387    case TOK_BITXOR:    case TOK_BITXOR:
388    case TOK_BITAND:    case TOK_BITAND:
# Line 457  Line 495 
495      assert(ATOM_IS_STRING(node->pn_atom));      assert(ATOM_IS_STRING(node->pn_atom));
496      {      {
497        JSString * s = ATOM_TO_STRING(node->pn_atom);        JSString * s = ATOM_TO_STRING(node->pn_atom);
498        /* XXX - semantics changed in 1.7 */        bool is_keyword = (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF);
499        if (! ATOM_KEYWORD(node->pn_atom) && js_IsIdentifier(s)) {        if (! is_keyword && js_IsIdentifier(s)) {
500          Stream_write_char(f, '.');          Stream_write_char(f, '.');
501          print_string_atom(node->pn_atom, f);          print_string_atom(node->pn_atom, f);
502        }        }
# Line 505  Line 543 
543        /* check whether this is a getter or setter */        /* check whether this is a getter or setter */
544        switch (p->pn_op) {        switch (p->pn_op) {
545        case JSOP_GETTER:        case JSOP_GETTER:
         Stream_write_string(f, "get ");  
         instrument_expression(p->pn_left, f);  
         instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);  
         break;  
546        case JSOP_SETTER:        case JSOP_SETTER:
547          Stream_write_string(f, "set ");          if (p->pn_op == JSOP_GETTER) {
548              Stream_write_string(f, "get ");
549            }
550            else {
551              Stream_write_string(f, "set ");
552            }
553          instrument_expression(p->pn_left, f);          instrument_expression(p->pn_left, f);
554            if (p->pn_right->pn_type != TOK_FUNCTION) {
555              fatal("parse error: expected function");
556            }
557          instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);          instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);
558          break;          break;
559        default:        default:
# Line 534  Line 576 
576    case TOK_STRING:    case TOK_STRING:
577      print_quoted_string_atom(node->pn_atom, f);      print_quoted_string_atom(node->pn_atom, f);
578      break;      break;
579    case TOK_OBJECT:    case TOK_REGEXP:
580      switch (node->pn_op) {      assert(node->pn_op == JSOP_REGEXP);
581      case JSOP_OBJECT:      {
582        /* I assume this is JSOP_REGEXP */        JSObject * object = node->pn_pob->object;
583        abort();        jsval result;
584        break;        js_regexp_toString(context, object, &result);
585      case JSOP_REGEXP:        print_regex(result, f);
       assert(ATOM_IS_OBJECT(node->pn_atom));  
       {  
         JSObject * object = ATOM_TO_OBJECT(node->pn_atom);  
         jsval result;  
         js_regexp_toString(context, object, 0, NULL, &result);  
         print_regex(result, f);  
       }  
       break;  
     default:  
       abort();  
       break;  
586      }      }
587      break;      break;
588    case TOK_NUMBER:    case TOK_NUMBER:
# Line 620  Line 651 
651    }    }
652  }  }
653    
654  static void output_statement(JSParseNode * node, Stream * f, int indent) {  static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
655    switch (node->pn_type) {    switch (node->pn_type) {
656    case TOK_FUNCTION:    case TOK_FUNCTION:
657      instrument_function(node, f, indent, FUNCTION_NORMAL);      instrument_function(node, f, indent, FUNCTION_NORMAL);
# Line 631  Line 662 
662      Stream_write_string(f, "{\n");      Stream_write_string(f, "{\n");
663  */  */
664      for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) {      for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) {
665        instrument_statement(p, f, indent);        instrument_statement(p, f, indent, false);
666      }      }
667  /*  /*
668      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 639  Line 670 
670  */  */
671      break;      break;
672    case TOK_IF:    case TOK_IF:
673      {
674      assert(node->pn_arity == PN_TERNARY);      assert(node->pn_arity == PN_TERNARY);
675    
676        uint16_t line = node->pn_pos.begin.lineno;
677        if (! is_jscoverage_if) {
678          if (line > num_lines) {
679            fatal("%s: script contains more than 65,535 lines", file_id);
680          }
681          if (line >= 2 && exclusive_directives[line - 2]) {
682            is_jscoverage_if = true;
683          }
684        }
685    
686      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
687      Stream_write_string(f, "if (");      Stream_write_string(f, "if (");
688      instrument_expression(node->pn_kid1, f);      instrument_expression(node->pn_kid1, f);
689      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
690      instrument_statement(node->pn_kid2, f, indent + 2);      if (is_jscoverage_if && node->pn_kid3) {
691          uint16_t else_start = node->pn_kid3->pn_pos.begin.lineno;
692          uint16_t else_end = node->pn_kid3->pn_pos.end.lineno + 1;
693          Stream_printf(f, "%*s", indent + 2, "");
694          Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, else_start, else_end);
695        }
696        instrument_statement(node->pn_kid2, f, indent + 2, false);
697      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
698      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
699      if (node->pn_kid3) {  
700        if (node->pn_kid3 || is_jscoverage_if) {
701        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
702        Stream_write_string(f, "else {\n");        Stream_write_string(f, "else {\n");
703        instrument_statement(node->pn_kid3, f, indent + 2);  
704          if (is_jscoverage_if) {
705            uint16_t if_start = node->pn_kid2->pn_pos.begin.lineno + 1;
706            uint16_t if_end = node->pn_kid2->pn_pos.end.lineno + 1;
707            Stream_printf(f, "%*s", indent + 2, "");
708            Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_start, if_end);
709          }
710    
711          if (node->pn_kid3) {
712            instrument_statement(node->pn_kid3, f, indent + 2, is_jscoverage_if);
713          }
714    
715        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
716        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
717      }      }
718    
719      break;      break;
720      }
721    case TOK_SWITCH:    case TOK_SWITCH:
722      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
723      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
# Line 676  Line 739 
739          abort();          abort();
740          break;          break;
741        }        }
742        instrument_statement(p->pn_right, f, indent + 2);        instrument_statement(p->pn_right, f, indent + 2, false);
743      }      }
744      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
745      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
# Line 691  Line 754 
754      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
755      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
756      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
757      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
758      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
759      break;      break;
760    case TOK_DO:    case TOK_DO:
761      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
762      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
763      Stream_write_string(f, "do {\n");      Stream_write_string(f, "do {\n");
764      instrument_statement(node->pn_left, f, indent + 2);      instrument_statement(node->pn_left, f, indent + 2, false);
765      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
766      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
767      Stream_write_string(f, "while (");      Stream_write_string(f, "while (");
# Line 708  Line 771 
771    case TOK_FOR:    case TOK_FOR:
772      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
773      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
774      Stream_write_string(f, "for (");      Stream_write_string(f, "for ");
775        if (node->pn_iflags & JSITER_FOREACH) {
776          Stream_write_string(f, "each ");
777        }
778        Stream_write_char(f, '(');
779      switch (node->pn_left->pn_type) {      switch (node->pn_left->pn_type) {
780      case TOK_IN:      case TOK_IN:
781        /* for/in */        /* for/in */
# Line 761  Line 828 
828        break;        break;
829      }      }
830      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
831      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
832      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
833      break;      break;
834    case TOK_THROW:    case TOK_THROW:
# Line 774  Line 841 
841    case TOK_TRY:    case TOK_TRY:
842      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
843      Stream_write_string(f, "try {\n");      Stream_write_string(f, "try {\n");
844      instrument_statement(node->pn_kid1, f, indent + 2);      instrument_statement(node->pn_kid1, f, indent + 2, false);
845      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
846      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
847      {      if (node->pn_kid2) {
848        for (JSParseNode * catch = node->pn_kid2; catch != NULL; catch = catch->pn_kid2) {        assert(node->pn_kid2->pn_type == TOK_RESERVED);
849          for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) {
850            assert(scope->pn_type == TOK_LEXICALSCOPE);
851            JSParseNode * catch = scope->pn_expr;
852          assert(catch->pn_type == TOK_CATCH);          assert(catch->pn_type == TOK_CATCH);
853          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
854          Stream_write_string(f, "catch (");          Stream_write_string(f, "catch (");
855          assert(catch->pn_kid1->pn_arity == PN_NAME);          assert(catch->pn_kid1->pn_arity == PN_NAME);
856          print_string_atom(catch->pn_kid1->pn_atom, f);          print_string_atom(catch->pn_kid1->pn_atom, f);
857          if (catch->pn_kid1->pn_expr) {          if (catch->pn_kid2) {
858            Stream_write_string(f, " if ");            Stream_write_string(f, " if ");
859            instrument_expression(catch->pn_kid1->pn_expr, f);            instrument_expression(catch->pn_kid2, f);
860          }          }
861          Stream_write_string(f, ") {\n");          Stream_write_string(f, ") {\n");
862          instrument_statement(catch->pn_kid3, f, indent + 2);          instrument_statement(catch->pn_kid3, f, indent + 2, false);
863          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
864          Stream_write_string(f, "}\n");          Stream_write_string(f, "}\n");
865        }        }
# Line 797  Line 867 
867      if (node->pn_kid3) {      if (node->pn_kid3) {
868        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
869        Stream_write_string(f, "finally {\n");        Stream_write_string(f, "finally {\n");
870        instrument_statement(node->pn_kid3, f, indent + 2);        instrument_statement(node->pn_kid3, f, indent + 2, false);
871        Stream_printf(f, "%*s", indent, "");        Stream_printf(f, "%*s", indent, "");
872        Stream_write_string(f, "}\n");        Stream_write_string(f, "}\n");
873      }      }
# Line 823  Line 893 
893      Stream_write_string(f, "with (");      Stream_write_string(f, "with (");
894      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
895      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
896      instrument_statement(node->pn_right, f, indent + 2);      instrument_statement(node->pn_right, f, indent + 2, false);
897      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
898      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
899      break;      break;
# Line 861  Line 931 
931      /*      /*
932      ... use output_statement instead of instrument_statement.      ... use output_statement instead of instrument_statement.
933      */      */
934      output_statement(node->pn_expr, f, indent);      output_statement(node->pn_expr, f, indent, false);
935      break;      break;
936    default:    default:
937      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);
# Line 873  Line 943 
943  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
944  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
945  */  */
946  static void instrument_statement(JSParseNode * node, Stream * f, int indent) {  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
947    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC) {
948      uint16_t line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
949      if (line > num_lines) {      if (line > num_lines) {
# Line 887  Line 957 
957        lines[line - 1] = 1;        lines[line - 1] = 1;
958      }      }
959    }    }
960    output_statement(node, f, indent);    output_statement(node, f, indent, is_jscoverage_if);
961  }  }
962    
963  static bool characters_start_with(const jschar * characters, size_t line_start, size_t line_end, const char * prefix) {  static bool characters_start_with(const jschar * characters, size_t line_start, size_t line_end, const char * prefix) {
# Line 909  Line 979 
979    }    }
980  }  }
981    
982    static bool characters_are_white_space(const jschar * characters, size_t line_start, size_t line_end) {
983      /* XXX - other Unicode space */
984      const jschar * end = characters + line_end;
985      for (const jschar * p = characters + line_start; p < end; p++) {
986        jschar c = *p;
987        if (c == 0x9 || c == 0xB || c == 0xC || c == 0x20 || c == 0xA0) {
988          continue;
989        }
990        else {
991          return false;
992        }
993      }
994      return true;
995    }
996    
997    static void error_reporter(JSContext * context, const char * message, JSErrorReport * report) {
998      fprintf(stderr, "jscoverage: parse error: line %u: %s\n", report->lineno, message);
999    }
1000    
1001  void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) {  void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) {
1002    file_id = id;    file_id = id;
1003    
1004    /* scan the javascript */    /* parse the javascript */
1005    JSTokenStream * token_stream = js_NewTokenStream(context, characters, num_characters, NULL, 1, NULL);    JSParseContext parse_context;
1006    if (token_stream == NULL) {    if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) {
1007      fatal("cannot create token stream from file: %s", file_id);      fatal("cannot create token stream from file: %s", file_id);
1008    }    }
1009      JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);
1010    /* parse the javascript */    JSParseNode * node = js_ParseScript(context, global, &parse_context);
   JSParseNode * node = js_ParseTokenStream(context, global, token_stream);  
1011    if (node == NULL) {    if (node == NULL) {
1012        js_ReportUncaughtException(context);
1013      fatal("parse error in file: %s", file_id);      fatal("parse error in file: %s", file_id);
1014    }    }
1015      JS_SetErrorReporter(context, old_error_reporter);
1016    num_lines = node->pn_pos.end.lineno;    num_lines = node->pn_pos.end.lineno;
1017    lines = xmalloc(num_lines);    lines = xmalloc(num_lines);
1018    for (int i = 0; i < num_lines; i++) {    for (unsigned int i = 0; i < num_lines; i++) {
1019      lines[i] = 0;      lines[i] = 0;
1020    }    }
1021    
1022    /*    /* search code for conditionals */
1023    An instrumented JavaScript file has 3 sections:    exclusive_directives = xnew(bool, num_lines);
1024    1. initialization    for (unsigned int i = 0; i < num_lines; i++) {
1025    2. instrumented source code      exclusive_directives[i] = false;
   3. original source code  
   */  
   
   Stream * instrumented = Stream_new(0);  
   instrument_statement(node, instrumented, 0);  
   
   /* write line number info to the output */  
   Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");  
   Stream_write_string(output, "if (! top._$jscoverage) {\n  top._$jscoverage = {};\n}\n");  
   Stream_write_string(output, "var _$jscoverage = top._$jscoverage;\n");  
   Stream_printf(output, "if (! _$jscoverage['%s']) {\n", file_id);  
   Stream_printf(output, "  _$jscoverage['%s'] = [];\n", file_id);  
   for (int i = 0; i < num_lines; i++) {  
     if (lines[i]) {  
       Stream_printf(output, "  _$jscoverage['%s'][%d] = 0;\n", file_id, i + 1);  
     }  
1026    }    }
   Stream_write_string(output, "}\n");  
   free(lines);  
   lines = NULL;  
1027    
   /* copy the instrumented source code to the output */  
   Stream_write(output, instrumented->data, instrumented->length);  
   
   /* conditionals */  
1028    bool has_conditionals = false;    bool has_conditionals = false;
1029      struct IfDirective * if_directives = NULL;
1030    size_t line_number = 0;    size_t line_number = 0;
1031    size_t i = 0;    size_t i = 0;
1032    while (i < num_characters) {    while (i < num_characters) {
1033        if (line_number == UINT16_MAX) {
1034          fatal("%s: script has more than 65,535 lines", file_id);
1035        }
1036      line_number++;      line_number++;
1037      size_t line_start = i;      size_t line_start = i;
1038      jschar c;      jschar c;
# Line 977  Line 1048 
1048          break;          break;
1049        default:        default:
1050          i++;          i++;
         break;  
1051        }        }
1052      }      }
1053      size_t line_end = i;      size_t line_end = i;
# Line 987  Line 1057 
1057          i++;          i++;
1058        }        }
1059      }      }
1060    
1061      if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) {      if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) {
1062        if (! has_conditionals) {        has_conditionals = true;
1063          has_conditionals = true;  
1064          Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);        if (characters_are_white_space(characters, line_start + 16, line_end)) {
1065        }          exclusive_directives[line_number - 1] = true;
1066        Stream_write_string(output, "if (!(");        }
1067        for (size_t j = line_start + 16; j < line_end; j++) {        else {
1068          jschar c = characters[j];          struct IfDirective * if_directive = xnew(struct IfDirective, 1);
1069          if (c == '\t' || (32 <= c && c <= 126)) {          if_directive->condition_start = characters + line_start + 16;
1070            Stream_write_char(output, c);          if_directive->condition_end = characters + line_end;
1071          }          if_directive->start_line = line_number;
1072          else {          if_directive->end_line = 0;
1073            Stream_printf(output, "\\u%04x", c);          if_directive->next = if_directives;
1074          }          if_directives = if_directive;
1075        }        }
       Stream_write_string(output, ")) {\n");  
       Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = ", file_id, line_number);  
1076      }      }
1077      else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) {      else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) {
1078        Stream_printf(output, "%d;\n", line_number);        for (struct IfDirective * p = if_directives; p != NULL; p = p->next) {
1079        Stream_printf(output, "}\n");          if (p->end_line == 0) {
1080              p->end_line = line_number;
1081              break;
1082            }
1083          }
1084      }      }
1085    }    }
1086    
1087      /*
1088      An instrumented JavaScript file has 4 sections:
1089      1. initialization
1090      2. instrumented source code
1091      3. conditionals
1092      4. original source code
1093      */
1094    
1095      Stream * instrumented = Stream_new(0);
1096      instrument_statement(node, instrumented, 0, false);
1097      js_FinishParseContext(context, &parse_context);
1098    
1099      /* write line number info to the output */
1100      Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");
1101      Stream_write_string(output, "if (! top._$jscoverage) {\n  top._$jscoverage = {};\n}\n");
1102      Stream_write_string(output, "var _$jscoverage = top._$jscoverage;\n");
1103      Stream_printf(output, "if (! _$jscoverage['%s']) {\n", file_id);
1104      Stream_printf(output, "  _$jscoverage['%s'] = [];\n", file_id);
1105      for (int i = 0; i < num_lines; i++) {
1106        if (lines[i]) {
1107          Stream_printf(output, "  _$jscoverage['%s'][%d] = 0;\n", file_id, i + 1);
1108        }
1109      }
1110      Stream_write_string(output, "}\n");
1111      free(lines);
1112      lines = NULL;
1113      free(exclusive_directives);
1114      exclusive_directives = NULL;
1115    
1116      /* conditionals */
1117      if (has_conditionals) {
1118        Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);
1119      }
1120    
1121      /* copy the instrumented source code to the output */
1122      Stream_write(output, instrumented->data, instrumented->length);
1123    
1124      /* conditionals */
1125      for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) {
1126        Stream_write_string(output, "if (!(");
1127        print_javascript(if_directive->condition_start, if_directive->condition_end - if_directive->condition_start, output);
1128        Stream_write_string(output, ")) {\n");
1129        Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line);
1130        Stream_write_string(output, "}\n");
1131      }
1132    
1133      /* free */
1134      while (if_directives != NULL) {
1135        struct IfDirective * if_directive = if_directives;
1136        if_directives = if_directives->next;
1137        free(if_directive);
1138      }
1139    
1140    /* copy the original source to the output */    /* copy the original source to the output */
1141    Stream_printf(output, "_$jscoverage['%s'].source = ", file_id);    Stream_printf(output, "_$jscoverage['%s'].source = ", file_id);
1142    jscoverage_write_source(id, characters, num_characters, output);    jscoverage_write_source(id, characters, num_characters, output);
# Line 1049  Line 1175 
1175            /* line feed (new line) */            /* line feed (new line) */
1176            done = true;            done = true;
1177            break;            break;
1178            /* IE doesn't support this */
1179            /*
1180          case 0xb:          case 0xb:
           /* vertical tab */  
1181            Stream_write_string(output, "\\v");            Stream_write_string(output, "\\v");
1182            break;            break;
1183            */
1184          case 0xc:          case 0xc:
1185            /* form feed */            /* form feed */
1186            Stream_write_string(output, "\\f");            Stream_write_string(output, "\\f");
# Line 1107  Line 1235 
1235            /* line feed (new line) */            /* line feed (new line) */
1236            done = true;            done = true;
1237            break;            break;
1238            /* IE doesn't support this */
1239            /*
1240          case 0xb:          case 0xb:
           /* vertical tab */  
1241            Stream_write_string(output, "\\v");            Stream_write_string(output, "\\v");
1242            break;            break;
1243            */
1244          case 0xc:          case 0xc:
1245            /* form feed */            /* form feed */
1246            Stream_write_string(output, "\\f");            Stream_write_string(output, "\\f");
# Line 1237  Line 1367 
1367  }  }
1368    
1369  int jscoverage_parse_json(Coverage * coverage, const uint8_t * json, size_t length) {  int jscoverage_parse_json(Coverage * coverage, const uint8_t * json, size_t length) {
1370      int result = 0;
1371    
1372    jschar * base = js_InflateString(context, (char *) json, &length);    jschar * base = js_InflateString(context, (char *) json, &length);
1373    if (base == NULL) {    if (base == NULL) {
1374      fatal("out of memory");      fatal("out of memory");
# Line 1249  Line 1381 
1381    
1382    JS_free(context, base);    JS_free(context, base);
1383    
1384    JSTokenStream * token_stream = js_NewTokenStream(context, parenthesized_json, length + 2, NULL, 1, NULL);    JSParseContext parse_context;
1385    if (token_stream == NULL) {    if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) {
1386      fatal("cannot create token stream");      free(parenthesized_json);
1387        return -1;
1388    }    }
1389      JSParseNode * root = js_ParseScript(context, global, &parse_context);
   JSParseNode * root = js_ParseTokenStream(context, global, token_stream);  
1390    free(parenthesized_json);    free(parenthesized_json);
1391    if (root == NULL) {    if (root == NULL) {
1392      return -1;      result = -1;
1393        goto done;
1394    }    }
1395    
1396    /* root node must be TOK_LC */    /* root node must be TOK_LC */
1397    if (root->pn_type != TOK_LC) {    if (root->pn_type != TOK_LC) {
1398      return -1;      result = -1;
1399        goto done;
1400    }    }
1401    JSParseNode * semi = root->pn_u.list.head;    JSParseNode * semi = root->pn_u.list.head;
1402    
1403    /* the list must be TOK_SEMI and it must contain only one element */    /* the list must be TOK_SEMI and it must contain only one element */
1404    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {
1405      return -1;      result = -1;
1406        goto done;
1407    }    }
1408    JSParseNode * parenthesized = semi->pn_kid;    JSParseNode * parenthesized = semi->pn_kid;
1409    
1410    /* this must be a parenthesized expression */    /* this must be a parenthesized expression */
1411    if (parenthesized->pn_type != TOK_RP) {    if (parenthesized->pn_type != TOK_RP) {
1412      return -1;      result = -1;
1413        goto done;
1414    }    }
1415    JSParseNode * object = parenthesized->pn_kid;    JSParseNode * object = parenthesized->pn_kid;
1416    
1417    /* this must be an object literal */    /* this must be an object literal */
1418    if (object->pn_type != TOK_RC) {    if (object->pn_type != TOK_RC) {
1419      return -1;      result = -1;
1420        goto done;
1421    }    }
1422    
1423    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {
1424      /* every element of this list must be TOK_COLON */      /* every element of this list must be TOK_COLON */
1425      if (p->pn_type != TOK_COLON) {      if (p->pn_type != TOK_COLON) {
1426        return -1;        result = -1;
1427          goto done;
1428      }      }
1429    
1430      /* the key must be a string representing the file */      /* the key must be a string representing the file */
1431      JSParseNode * key = p->pn_left;      JSParseNode * key = p->pn_left;
1432      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {
1433        return -1;        result = -1;
1434          goto done;
1435      }      }
1436      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));
1437    
1438      /* the value must be an object literal OR an array */      /* the value must be an object literal OR an array */
1439      JSParseNode * value = p->pn_right;      JSParseNode * value = p->pn_right;
1440      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {
1441        return -1;        result = -1;
1442          goto done;
1443      }      }
1444    
1445      JSParseNode * array = NULL;      JSParseNode * array = NULL;
# Line 1311  Line 1451 
1451      else if (value->pn_type == TOK_RC) {      else if (value->pn_type == TOK_RC) {
1452        /* an object literal */        /* an object literal */
1453        if (value->pn_count != 2) {        if (value->pn_count != 2) {
1454          return -1;          result = -1;
1455            goto done;
1456        }        }
1457        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {
1458          if (element->pn_type != TOK_COLON) {          if (element->pn_type != TOK_COLON) {
1459            return -1;            result = -1;
1460              goto done;
1461          }          }
1462          JSParseNode * left = element->pn_left;          JSParseNode * left = element->pn_left;
1463          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {
1464            return -1;            result = -1;
1465              goto done;
1466          }          }
1467          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));
1468          if (strcmp(s, "coverage") == 0) {          if (strcmp(s, "coverage") == 0) {
1469            array = element->pn_right;            array = element->pn_right;
1470            if (array->pn_type != TOK_RB) {            if (array->pn_type != TOK_RB) {
1471              return -1;              result = -1;
1472                goto done;
1473            }            }
1474          }          }
1475          else if (strcmp(s, "source") == 0) {          else if (strcmp(s, "source") == 0) {
1476            source = element->pn_right;            source = element->pn_right;
1477            if (source->pn_type != TOK_RB) {            if (source->pn_type != TOK_RB) {
1478              return -1;              result = -1;
1479                goto done;
1480            }            }
1481          }          }
1482          else {          else {
1483            return -1;            result = -1;
1484              goto done;
1485          }          }
1486        }        }
1487      }      }
1488      else {      else {
1489        return -1;        result = -1;
1490          goto done;
1491      }      }
1492    
1493      if (array == NULL) {      if (array == NULL) {
1494        return -1;        result = -1;
1495          goto done;
1496      }      }
1497    
1498      /* look up the file in the coverage table */      /* look up the file in the coverage table */
# Line 1356  Line 1504 
1504        file_coverage->id = id;        file_coverage->id = id;
1505        file_coverage->num_coverage_lines = array->pn_count;        file_coverage->num_coverage_lines = array->pn_count;
1506        file_coverage->coverage_lines = xnew(int, array->pn_count);        file_coverage->coverage_lines = xnew(int, array->pn_count);
1507        if (source == NULL) {        file_coverage->source_lines = NULL;
         file_coverage->source_lines = NULL;  
       }  
       else {  
         file_coverage->num_source_lines = source->pn_count;  
         file_coverage->source_lines = xnew(char *, source->pn_count);  
         uint32 i = 0;  
         for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {  
           if (element->pn_type != TOK_STRING) {  
             return -1;  
           }  
           file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));  
         }  
         assert(i == source->pn_count);  
       }  
1508    
1509        /* set coverage for all lines */        /* set coverage for all lines */
1510        uint32 i = 0;        uint32 i = 0;
# Line 1382  Line 1516 
1516            file_coverage->coverage_lines[i] = -1;            file_coverage->coverage_lines[i] = -1;
1517          }          }
1518          else {          else {
1519            return -1;            result = -1;
1520              goto done;
1521          }          }
1522        }        }
1523        assert(i == array->pn_count);        assert(i == array->pn_count);
# Line 1398  Line 1533 
1533        /* sanity check */        /* sanity check */
1534        assert(strcmp(file_coverage->id, id_bytes) == 0);        assert(strcmp(file_coverage->id, id_bytes) == 0);
1535        if (file_coverage->num_coverage_lines != array->pn_count) {        if (file_coverage->num_coverage_lines != array->pn_count) {
1536          return -2;          result = -2;
1537            goto done;
1538        }        }
1539    
1540        /* merge the coverage */        /* merge the coverage */
# Line 1406  Line 1542 
1542        for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {        for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) {
1543          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1544            if (file_coverage->coverage_lines[i] == -1) {            if (file_coverage->coverage_lines[i] == -1) {
1545              return -2;              result = -2;
1546                goto done;
1547            }            }
1548            file_coverage->coverage_lines[i] += (int) element->pn_dval;            file_coverage->coverage_lines[i] += (int) element->pn_dval;
1549          }          }
1550          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1551            if (file_coverage->coverage_lines[i] != -1) {            if (file_coverage->coverage_lines[i] != -1) {
1552              return -2;              result = -2;
1553                goto done;
1554            }            }
1555          }          }
1556          else {          else {
1557            return -1;            result = -1;
1558              goto done;
1559          }          }
1560        }        }
1561        assert(i == array->pn_count);        assert(i == array->pn_count);
1562        }
1563    
1564        /* if this JSON file has source, use it */      /* if this JSON file has source, use it */
1565        if (file_coverage->source_lines == NULL && source != NULL) {      if (file_coverage->source_lines == NULL && source != NULL) {
1566          file_coverage->num_source_lines = source->pn_count;        file_coverage->num_source_lines = source->pn_count;
1567          file_coverage->source_lines = xnew(char *, source->pn_count);        file_coverage->source_lines = xnew(char *, source->pn_count);
1568          uint32 i = 0;        uint32 i = 0;
1569          for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {        for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {
1570            if (element->pn_type != TOK_STRING) {          if (element->pn_type != TOK_STRING) {
1571              return -1;            result = -1;
1572            }            goto done;
           file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));  
1573          }          }
1574          assert(i == source->pn_count);          file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));
1575        }        }
1576          assert(i == source->pn_count);
1577      }      }
1578    }    }
1579    
1580    return 0;  done:
1581      js_FinishParseContext(context, &parse_context);
1582      return result;
1583  }  }

Legend:
Removed from v.214  
changed lines
  Added in v.339

  ViewVC Help
Powered by ViewVC 1.1.24