/[jscoverage]/trunk/instrument-js.cpp
ViewVC logotype

Diff of /trunk/instrument-js.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 240 by siliconforks, Fri Oct 3 23:51:32 2008 UTC revision 346 by siliconforks, Fri Oct 24 16:16:24 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 53  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 62  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 73  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 88  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 122  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 171  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 181  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 216  Line 257 
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, bool is_jscoverage_if);  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
260    static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
261    
262  enum FunctionType {  enum FunctionType {
263    FUNCTION_NORMAL,    FUNCTION_NORMAL,
264    FUNCTION_GETTER_OR_SETTER    FUNCTION_GETTER_OR_SETTER
265  };  };
266    
267    static void output_for_in(JSParseNode * node, Stream * f) {
268      assert(node->pn_type == TOK_FOR);
269      assert(node->pn_arity == PN_BINARY);
270      Stream_write_string(f, "for ");
271      if (node->pn_iflags & JSITER_FOREACH) {
272        Stream_write_string(f, "each ");
273      }
274      Stream_write_char(f, '(');
275      instrument_expression(node->pn_left, f);
276      Stream_write_char(f, ')');
277    }
278    
279  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) {
280      assert(node->pn_type == TOK_FUNCTION);
281    assert(node->pn_arity == PN_FUNC);    assert(node->pn_arity == PN_FUNC);
282    assert(ATOM_IS_OBJECT(node->pn_funAtom));    JSObject * object = node->pn_funpob->object;
   JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom);  
283    assert(JS_ObjectIsFunction(context, object));    assert(JS_ObjectIsFunction(context, object));
284    JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);    JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
285    assert(function);    assert(function);
286    assert(object == function->object);    assert(object == &function->object);
287    Stream_printf(f, "%*s", indent, "");    Stream_printf(f, "%*s", indent, "");
288    if (type == FUNCTION_NORMAL) {    if (type == FUNCTION_NORMAL) {
289      Stream_write_string(f, "function");      Stream_write_string(f, "function");
# Line 241  Line 295 
295      print_string_atom(function->atom, f);      print_string_atom(function->atom, f);
296    }    }
297    
298    /* function parameters */    /*
299      function parameters - see JS_DecompileFunction in jsapi.cpp, which calls
300      js_DecompileFunction in jsopcode.cpp
301      */
302    Stream_write_string(f, "(");    Stream_write_string(f, "(");
303    JSAtom ** params = xnew(JSAtom *, function->nargs);    JSArenaPool pool;
304    for (int i = 0; i < function->nargs; i++) {    JS_INIT_ARENA_POOL(&pool, "instrument_function", 256, 1, &context->scriptStackQuota);
305      /* initialize to NULL for sanity check */    jsuword * local_names = NULL;
306      params[i] = NULL;    if (JS_GET_LOCAL_NAME_COUNT(function)) {
307    }      local_names = js_GetLocalNameArray(context, function, &pool);
308    JSScope * scope = OBJ_SCOPE(object);      if (local_names == NULL) {
309    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;  
310      }      }
     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);  
311    }    }
312    for (int i = 0; i < function->nargs; i++) {    for (int i = 0; i < function->nargs; i++) {
     assert(params[i] != NULL);  
313      if (i > 0) {      if (i > 0) {
314        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
315      }      }
316      if (ATOM_IS_STRING(params[i])) {      JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]);
317        print_string_atom(params[i], f);      print_string_atom(param, f);
     }  
318    }    }
319      JS_FinishArenaPool(&pool);
320    Stream_write_string(f, ") {\n");    Stream_write_string(f, ") {\n");
   free(params);  
321    
322    /* function body */    /* function body */
323    instrument_statement(node->pn_body, f, indent + 2, false);    if (function->flags & JSFUN_EXPR_CLOSURE) {
324        /* expression closure */
325        output_statement(node->pn_body, f, indent + 2, false);
326      }
327      else {
328        instrument_statement(node->pn_body, f, indent + 2, false);
329      }
330    
331    Stream_write_string(f, "}\n");    Stream_write_string(f, "}\n");
332  }  }
333    
334  static void instrument_function_call(JSParseNode * node, Stream * f) {  static void instrument_function_call(JSParseNode * node, Stream * f) {
335    instrument_expression(node->pn_head, f);    if (node->pn_head->pn_type == TOK_FUNCTION) {
336    Stream_write_char(f, '(');      /* it's a generator expression */
337    for (struct JSParseNode * p = node->pn_head->pn_next; p != NULL; p = p->pn_next) {      JSParseNode * function_node = node->pn_head;
338      if (p != node->pn_head->pn_next) {      JSParseNode * lexical_scope_node = function_node->pn_body;
339        Stream_write_string(f, ", ");      assert(lexical_scope_node->pn_type == TOK_LEXICALSCOPE);
340        assert(lexical_scope_node->pn_arity == PN_NAME);
341        JSParseNode * for_node = lexical_scope_node->pn_body;
342        assert(for_node->pn_type == TOK_FOR);
343        assert(for_node->pn_arity == PN_BINARY);
344        JSParseNode * if_node = NULL;
345        JSParseNode * semi_node;
346        switch (for_node->pn_right->pn_type) {
347        case TOK_SEMI:
348          semi_node = for_node->pn_right;
349          break;
350        case TOK_IF:
351          if_node = for_node->pn_right;
352          assert(if_node->pn_arity == PN_TERNARY);
353          semi_node = if_node->pn_kid2;
354          assert(semi_node->pn_type == TOK_SEMI);
355          break;
356        default:
357          abort();
358          break;
359        }
360        assert(semi_node->pn_arity == PN_UNARY);
361        JSParseNode * yield_node = semi_node->pn_kid;
362        assert(yield_node->pn_type == TOK_YIELD);
363        Stream_write_char(f, '(');
364        instrument_expression(yield_node->pn_kid, f);
365        Stream_write_char(f, ' ');
366        output_for_in(for_node, f);
367        if (if_node) {
368          Stream_write_string(f, " if (");
369          instrument_expression(if_node->pn_kid1, f);
370          Stream_write_char(f, ')');
371        }
372        Stream_write_char(f, ')');
373      }
374      else {
375        instrument_expression(node->pn_head, f);
376        Stream_write_char(f, '(');
377        for (struct JSParseNode * p = node->pn_head->pn_next; p != NULL; p = p->pn_next) {
378          if (p != node->pn_head->pn_next) {
379            Stream_write_string(f, ", ");
380          }
381          instrument_expression(p, f);
382        }
383        Stream_write_char(f, ')');
384      }
385    }
386    
387    static void instrument_declarations(JSParseNode * list, Stream * f) {
388      assert(list->pn_arity == PN_LIST);
389      for (JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
390        switch (p->pn_type) {
391        case TOK_NAME:
392          assert(p->pn_arity == PN_NAME);
393          if (p != list->pn_head) {
394            Stream_write_string(f, ", ");
395          }
396          print_string_atom(p->pn_atom, f);
397          if (p->pn_expr != NULL) {
398            Stream_write_string(f, " = ");
399            instrument_expression(p->pn_expr, f);
400          }
401          break;
402        case TOK_ASSIGN:
403        case TOK_RB:
404        case TOK_RC:
405          /* destructuring */
406          instrument_expression(p, f);
407          break;
408        default:
409          abort();
410          break;
411      }      }
     instrument_expression(p, f);  
412    }    }
   Stream_write_char(f, ')');  
413  }  }
414    
415  /*  /*
# Line 346  Line 470 
470      instrument_expression(node->pn_kid3, f);      instrument_expression(node->pn_kid3, f);
471      break;      break;
472    case TOK_OR:    case TOK_OR:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " || ");  
     instrument_expression(node->pn_right, f);  
     break;  
473    case TOK_AND:    case TOK_AND:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " && ");  
     instrument_expression(node->pn_right, f);  
     break;  
474    case TOK_BITOR:    case TOK_BITOR:
475    case TOK_BITXOR:    case TOK_BITXOR:
476    case TOK_BITAND:    case TOK_BITAND:
# Line 467  Line 583 
583      assert(ATOM_IS_STRING(node->pn_atom));      assert(ATOM_IS_STRING(node->pn_atom));
584      {      {
585        JSString * s = ATOM_TO_STRING(node->pn_atom);        JSString * s = ATOM_TO_STRING(node->pn_atom);
586        /* XXX - semantics changed in 1.7 */        bool is_keyword = (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF);
587        if (! ATOM_KEYWORD(node->pn_atom) && js_IsIdentifier(s)) {        if (! is_keyword && js_IsIdentifier(s)) {
588          Stream_write_char(f, '.');          Stream_write_char(f, '.');
589          print_string_atom(node->pn_atom, f);          print_string_atom(node->pn_atom, f);
590        }        }
# Line 515  Line 631 
631        /* check whether this is a getter or setter */        /* check whether this is a getter or setter */
632        switch (p->pn_op) {        switch (p->pn_op) {
633        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;  
634        case JSOP_SETTER:        case JSOP_SETTER:
635          Stream_write_string(f, "set ");          if (p->pn_op == JSOP_GETTER) {
636              Stream_write_string(f, "get ");
637            }
638            else {
639              Stream_write_string(f, "set ");
640            }
641          instrument_expression(p->pn_left, f);          instrument_expression(p->pn_left, f);
642            if (p->pn_right->pn_type != TOK_FUNCTION) {
643              fatal("parse error: expected function");
644            }
645          instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);          instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);
646          break;          break;
647        default:        default:
# Line 544  Line 664 
664    case TOK_STRING:    case TOK_STRING:
665      print_quoted_string_atom(node->pn_atom, f);      print_quoted_string_atom(node->pn_atom, f);
666      break;      break;
667    case TOK_OBJECT:    case TOK_REGEXP:
668      switch (node->pn_op) {      assert(node->pn_op == JSOP_REGEXP);
669      case JSOP_OBJECT:      {
670        /* I assume this is JSOP_REGEXP */        JSObject * object = node->pn_pob->object;
671        abort();        jsval result;
672        break;        js_regexp_toString(context, object, &result);
673      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;  
674      }      }
675      break;      break;
676    case TOK_NUMBER:    case TOK_NUMBER:
# Line 607  Line 716 
716      Stream_write_string(f, " in ");      Stream_write_string(f, " in ");
717      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
718      break;      break;
719      case TOK_LEXICALSCOPE:
720        assert(node->pn_arity == PN_NAME);
721        assert(node->pn_expr->pn_type == TOK_LET);
722        assert(node->pn_expr->pn_arity == PN_BINARY);
723        Stream_write_string(f, "let(");
724        assert(node->pn_expr->pn_left->pn_type == TOK_LP);
725        assert(node->pn_expr->pn_left->pn_arity == PN_LIST);
726        instrument_declarations(node->pn_expr->pn_left, f);
727        Stream_write_string(f, ") ");
728        instrument_expression(node->pn_expr->pn_right, f);
729        break;
730      case TOK_YIELD:
731        assert(node->pn_arity == PN_UNARY);
732        Stream_write_string(f, "yield ");
733        instrument_expression(node->pn_kid, f);
734        break;
735      case TOK_ARRAYCOMP:
736        assert(node->pn_arity == PN_LIST);
737        {
738          JSParseNode * block_node;
739          switch (node->pn_count) {
740          case 1:
741            block_node = node->pn_head;
742            break;
743          case 2:
744            block_node = node->pn_head->pn_next;
745            break;
746          default:
747            abort();
748            break;
749          }
750          assert(block_node->pn_type == TOK_LEXICALSCOPE);
751          assert(block_node->pn_arity == PN_NAME);
752          JSParseNode * for_node = block_node->pn_expr;
753          assert(for_node->pn_type == TOK_FOR);
754          assert(for_node->pn_arity == PN_BINARY);
755          JSParseNode * if_node = NULL;
756          JSParseNode * push_node;
757          switch (for_node->pn_right->pn_type) {
758          case TOK_ARRAYPUSH:
759            push_node = for_node->pn_right;
760            assert(push_node->pn_arity == PN_UNARY);
761            break;
762          case TOK_IF:
763            if_node = for_node->pn_right;
764            assert(if_node->pn_arity == PN_TERNARY);
765            push_node = if_node->pn_kid2;
766            break;
767          default:
768            abort();
769            break;
770          }
771          Stream_write_char(f, '[');
772          instrument_expression(push_node->pn_kid, f);
773          Stream_write_char(f, ' ');
774          output_for_in(for_node, f);
775          if (if_node) {
776            Stream_write_string(f, " if (");
777            instrument_expression(if_node->pn_kid1, f);
778            Stream_write_char(f, ')');
779          }
780          Stream_write_char(f, ']');
781        }
782        break;
783      case TOK_VAR:
784        assert(node->pn_arity == PN_LIST);
785        Stream_write_string(f, "var ");
786        instrument_declarations(node, f);
787        break;
788      case TOK_LET:
789        assert(node->pn_arity == PN_LIST);
790        Stream_write_string(f, "let ");
791        instrument_declarations(node, f);
792        break;
793    default:    default:
794      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);
795    }    }
796  }  }
797    
 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);  
     }  
   }  
 }  
   
798  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) {
799    switch (node->pn_type) {    switch (node->pn_type) {
800    case TOK_FUNCTION:    case TOK_FUNCTION:
# Line 750  Line 915 
915    case TOK_FOR:    case TOK_FOR:
916      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
917      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
     Stream_write_string(f, "for (");  
918      switch (node->pn_left->pn_type) {      switch (node->pn_left->pn_type) {
919      case TOK_IN:      case TOK_IN:
920        /* for/in */        /* for/in */
921        assert(node->pn_left->pn_arity == PN_BINARY);        assert(node->pn_left->pn_arity == PN_BINARY);
922        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);  
923        break;        break;
924      case TOK_RESERVED:      case TOK_RESERVED:
925        /* for (;;) */        /* for (;;) */
926        assert(node->pn_left->pn_arity == PN_TERNARY);        assert(node->pn_left->pn_arity == PN_TERNARY);
927          Stream_write_string(f, "for (");
928        if (node->pn_left->pn_kid1) {        if (node->pn_left->pn_kid1) {
929          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);  
         }  
930        }        }
931        Stream_write_string(f, ";");        Stream_write_string(f, ";");
932        if (node->pn_left->pn_kid2) {        if (node->pn_left->pn_kid2) {
# Line 797  Line 938 
938          Stream_write_char(f, ' ');          Stream_write_char(f, ' ');
939          instrument_expression(node->pn_left->pn_kid3, f);          instrument_expression(node->pn_left->pn_kid3, f);
940        }        }
941          Stream_write_char(f, ')');
942        break;        break;
943      default:      default:
944        abort();        abort();
945        break;        break;
946      }      }
947      Stream_write_string(f, ") {\n");      Stream_write_string(f, " {\n");
948      instrument_statement(node->pn_right, f, indent + 2, false);      instrument_statement(node->pn_right, f, indent + 2, false);
949      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
950      break;      break;
# Line 819  Line 961 
961      instrument_statement(node->pn_kid1, f, indent + 2, false);      instrument_statement(node->pn_kid1, f, indent + 2, false);
962      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
963      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
964      {      if (node->pn_kid2) {
965        for (JSParseNode * catch = node->pn_kid2; catch != NULL; catch = catch->pn_kid2) {        assert(node->pn_kid2->pn_type == TOK_RESERVED);
966          for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) {
967            assert(scope->pn_type == TOK_LEXICALSCOPE);
968            JSParseNode * catch = scope->pn_expr;
969          assert(catch->pn_type == TOK_CATCH);          assert(catch->pn_type == TOK_CATCH);
970          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
971          Stream_write_string(f, "catch (");          Stream_write_string(f, "catch (");
972            /* this may not be a name - destructuring assignment */
973            /*
974          assert(catch->pn_kid1->pn_arity == PN_NAME);          assert(catch->pn_kid1->pn_arity == PN_NAME);
975          print_string_atom(catch->pn_kid1->pn_atom, f);          print_string_atom(catch->pn_kid1->pn_atom, f);
976          if (catch->pn_kid1->pn_expr) {          */
977            instrument_expression(catch->pn_kid1, f);
978            if (catch->pn_kid2) {
979            Stream_write_string(f, " if ");            Stream_write_string(f, " if ");
980            instrument_expression(catch->pn_kid1->pn_expr, f);            instrument_expression(catch->pn_kid2, f);
981          }          }
982          Stream_write_string(f, ") {\n");          Stream_write_string(f, ") {\n");
983          instrument_statement(catch->pn_kid3, f, indent + 2, false);          instrument_statement(catch->pn_kid3, f, indent + 2, false);
# Line 870  Line 1019 
1019      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
1020      break;      break;
1021    case TOK_VAR:    case TOK_VAR:
1022      instrument_var_statement(node, f, indent);      Stream_printf(f, "%*s", indent, "");
1023        instrument_expression(node, f);
1024      Stream_write_string(f, ";\n");      Stream_write_string(f, ";\n");
1025      break;      break;
1026    case TOK_RETURN:    case TOK_RETURN:
# Line 905  Line 1055 
1055      */      */
1056      output_statement(node->pn_expr, f, indent, false);      output_statement(node->pn_expr, f, indent, false);
1057      break;      break;
1058      case TOK_LEXICALSCOPE:
1059        /* let statement */
1060        assert(node->pn_arity == PN_NAME);
1061        switch (node->pn_expr->pn_type) {
1062        case TOK_LET:
1063          /* let statement */
1064          assert(node->pn_expr->pn_arity == PN_BINARY);
1065          instrument_statement(node->pn_expr, f, indent, false);
1066          break;
1067        case TOK_LC:
1068          /* block */
1069          Stream_printf(f, "%*s", indent, "");
1070          Stream_write_string(f, "{\n");
1071          instrument_statement(node->pn_expr, f, indent + 2, false);
1072          Stream_printf(f, "%*s", indent, "");
1073          Stream_write_string(f, "}\n");
1074          break;
1075        case TOK_FOR:
1076          instrument_statement(node->pn_expr, f, indent, false);
1077          break;
1078        default:
1079          abort();
1080          break;
1081        }
1082        break;
1083      case TOK_LET:
1084        switch (node->pn_arity) {
1085        case PN_BINARY:
1086          /* let statement */
1087          Stream_printf(f, "%*s", indent, "");
1088          Stream_write_string(f, "let (");
1089          assert(node->pn_left->pn_type == TOK_LP);
1090          assert(node->pn_left->pn_arity == PN_LIST);
1091          instrument_declarations(node->pn_left, f);
1092          Stream_write_string(f, ") {\n");
1093          instrument_statement(node->pn_right, f, indent + 2, false);
1094          Stream_printf(f, "%*s", indent, "");
1095          Stream_write_string(f, "}\n");
1096          break;
1097        case PN_LIST:
1098          /* let definition */
1099          Stream_printf(f, "%*s", indent, "");
1100          instrument_expression(node, f);
1101          Stream_write_string(f, ";\n");
1102          break;
1103        default:
1104          abort();
1105          break;
1106        }
1107        break;
1108    default:    default:
1109      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);
1110    }    }
# Line 916  Line 1116 
1116  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
1117  */  */
1118  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) {
1119    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC && node->pn_type != TOK_LEXICALSCOPE) {
1120      uint16_t line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
1121      if (line > num_lines) {      if (line > num_lines) {
1122        fatal("%s: script contains more than 65,535 lines", file_id);        fatal("%s: script contains more than 65,535 lines", file_id);
# Line 966  Line 1166 
1166    return true;    return true;
1167  }  }
1168    
1169    static void error_reporter(JSContext * context, const char * message, JSErrorReport * report) {
1170      fprintf(stderr, "jscoverage: parse error: line %u: %s\n", report->lineno, message);
1171    }
1172    
1173  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) {
1174    file_id = id;    file_id = id;
1175    
1176    /* scan the javascript */    /* parse the javascript */
1177    JSTokenStream * token_stream = js_NewTokenStream(context, characters, num_characters, NULL, 1, NULL);    JSParseContext parse_context;
1178    if (token_stream == NULL) {    if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) {
1179      fatal("cannot create token stream from file: %s", file_id);      fatal("cannot create token stream from file: %s", file_id);
1180    }    }
1181      JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);
1182    /* parse the javascript */    JSParseNode * node = js_ParseScript(context, global, &parse_context);
   JSParseNode * node = js_ParseTokenStream(context, global, token_stream);  
1183    if (node == NULL) {    if (node == NULL) {
1184        js_ReportUncaughtException(context);
1185      fatal("parse error in file: %s", file_id);      fatal("parse error in file: %s", file_id);
1186    }    }
1187      JS_SetErrorReporter(context, old_error_reporter);
1188    num_lines = node->pn_pos.end.lineno;    num_lines = node->pn_pos.end.lineno;
1189    lines = xmalloc(num_lines);    lines = xmalloc(num_lines);
1190    for (unsigned int i = 0; i < num_lines; i++) {    for (unsigned int i = 0; i < num_lines; i++) {
# Line 1061  Line 1266 
1266    
1267    Stream * instrumented = Stream_new(0);    Stream * instrumented = Stream_new(0);
1268    instrument_statement(node, instrumented, 0, false);    instrument_statement(node, instrumented, 0, false);
1269      js_FinishParseContext(context, &parse_context);
1270    
1271    /* write line number info to the output */    /* write line number info to the output */
1272    Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");    Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");
# Line 1090  Line 1296 
1296    /* conditionals */    /* conditionals */
1297    for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) {    for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) {
1298      Stream_write_string(output, "if (!(");      Stream_write_string(output, "if (!(");
1299      for (const jschar * p = if_directive->condition_start; p < if_directive->condition_end; p++) {      print_javascript(if_directive->condition_start, if_directive->condition_end - if_directive->condition_start, output);
       jschar c = *p;  
       if (c == '\t' || (32 <= c && c <= 126)) {  
         Stream_write_char(output, c);  
       }  
       else {  
         Stream_printf(output, "\\u%04x", c);  
       }  
     }  
1300      Stream_write_string(output, ")) {\n");      Stream_write_string(output, ")) {\n");
1301      Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line);      Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line);
1302      Stream_write_string(output, "}\n");      Stream_write_string(output, "}\n");
# Line 1149  Line 1347 
1347            /* line feed (new line) */            /* line feed (new line) */
1348            done = true;            done = true;
1349            break;            break;
1350            /* IE doesn't support this */
1351            /*
1352          case 0xb:          case 0xb:
           /* vertical tab */  
1353            Stream_write_string(output, "\\v");            Stream_write_string(output, "\\v");
1354            break;            break;
1355            */
1356          case 0xc:          case 0xc:
1357            /* form feed */            /* form feed */
1358            Stream_write_string(output, "\\f");            Stream_write_string(output, "\\f");
# Line 1207  Line 1407 
1407            /* line feed (new line) */            /* line feed (new line) */
1408            done = true;            done = true;
1409            break;            break;
1410            /* IE doesn't support this */
1411            /*
1412          case 0xb:          case 0xb:
           /* vertical tab */  
1413            Stream_write_string(output, "\\v");            Stream_write_string(output, "\\v");
1414            break;            break;
1415            */
1416          case 0xc:          case 0xc:
1417            /* form feed */            /* form feed */
1418            Stream_write_string(output, "\\f");            Stream_write_string(output, "\\f");
# Line 1337  Line 1539 
1539  }  }
1540    
1541  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) {
1542      int result = 0;
1543    
1544    jschar * base = js_InflateString(context, (char *) json, &length);    jschar * base = js_InflateString(context, (char *) json, &length);
1545    if (base == NULL) {    if (base == NULL) {
1546      fatal("out of memory");      fatal("out of memory");
# Line 1349  Line 1553 
1553    
1554    JS_free(context, base);    JS_free(context, base);
1555    
1556    JSTokenStream * token_stream = js_NewTokenStream(context, parenthesized_json, length + 2, NULL, 1, NULL);    JSParseContext parse_context;
1557    if (token_stream == NULL) {    if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) {
1558      fatal("cannot create token stream");      free(parenthesized_json);
1559        return -1;
1560    }    }
1561      JSParseNode * root = js_ParseScript(context, global, &parse_context);
   JSParseNode * root = js_ParseTokenStream(context, global, token_stream);  
1562    free(parenthesized_json);    free(parenthesized_json);
1563    if (root == NULL) {    if (root == NULL) {
1564      return -1;      result = -1;
1565        goto done;
1566    }    }
1567    
1568    /* root node must be TOK_LC */    /* root node must be TOK_LC */
1569    if (root->pn_type != TOK_LC) {    if (root->pn_type != TOK_LC) {
1570      return -1;      result = -1;
1571        goto done;
1572    }    }
1573    JSParseNode * semi = root->pn_u.list.head;    JSParseNode * semi = root->pn_u.list.head;
1574    
1575    /* 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 */
1576    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {
1577      return -1;      result = -1;
1578        goto done;
1579    }    }
1580    JSParseNode * parenthesized = semi->pn_kid;    JSParseNode * parenthesized = semi->pn_kid;
1581    
1582    /* this must be a parenthesized expression */    /* this must be a parenthesized expression */
1583    if (parenthesized->pn_type != TOK_RP) {    if (parenthesized->pn_type != TOK_RP) {
1584      return -1;      result = -1;
1585        goto done;
1586    }    }
1587    JSParseNode * object = parenthesized->pn_kid;    JSParseNode * object = parenthesized->pn_kid;
1588    
1589    /* this must be an object literal */    /* this must be an object literal */
1590    if (object->pn_type != TOK_RC) {    if (object->pn_type != TOK_RC) {
1591      return -1;      result = -1;
1592        goto done;
1593    }    }
1594    
1595    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {
1596      /* every element of this list must be TOK_COLON */      /* every element of this list must be TOK_COLON */
1597      if (p->pn_type != TOK_COLON) {      if (p->pn_type != TOK_COLON) {
1598        return -1;        result = -1;
1599          goto done;
1600      }      }
1601    
1602      /* the key must be a string representing the file */      /* the key must be a string representing the file */
1603      JSParseNode * key = p->pn_left;      JSParseNode * key = p->pn_left;
1604      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {
1605        return -1;        result = -1;
1606          goto done;
1607      }      }
1608      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));
1609    
1610      /* the value must be an object literal OR an array */      /* the value must be an object literal OR an array */
1611      JSParseNode * value = p->pn_right;      JSParseNode * value = p->pn_right;
1612      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {
1613        return -1;        result = -1;
1614          goto done;
1615      }      }
1616    
1617      JSParseNode * array = NULL;      JSParseNode * array = NULL;
# Line 1411  Line 1623 
1623      else if (value->pn_type == TOK_RC) {      else if (value->pn_type == TOK_RC) {
1624        /* an object literal */        /* an object literal */
1625        if (value->pn_count != 2) {        if (value->pn_count != 2) {
1626          return -1;          result = -1;
1627            goto done;
1628        }        }
1629        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {
1630          if (element->pn_type != TOK_COLON) {          if (element->pn_type != TOK_COLON) {
1631            return -1;            result = -1;
1632              goto done;
1633          }          }
1634          JSParseNode * left = element->pn_left;          JSParseNode * left = element->pn_left;
1635          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {
1636            return -1;            result = -1;
1637              goto done;
1638          }          }
1639          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));
1640          if (strcmp(s, "coverage") == 0) {          if (strcmp(s, "coverage") == 0) {
1641            array = element->pn_right;            array = element->pn_right;
1642            if (array->pn_type != TOK_RB) {            if (array->pn_type != TOK_RB) {
1643              return -1;              result = -1;
1644                goto done;
1645            }            }
1646          }          }
1647          else if (strcmp(s, "source") == 0) {          else if (strcmp(s, "source") == 0) {
1648            source = element->pn_right;            source = element->pn_right;
1649            if (source->pn_type != TOK_RB) {            if (source->pn_type != TOK_RB) {
1650              return -1;              result = -1;
1651                goto done;
1652            }            }
1653          }          }
1654          else {          else {
1655            return -1;            result = -1;
1656              goto done;
1657          }          }
1658        }        }
1659      }      }
1660      else {      else {
1661        return -1;        result = -1;
1662          goto done;
1663      }      }
1664    
1665      if (array == NULL) {      if (array == NULL) {
1666        return -1;        result = -1;
1667          goto done;
1668      }      }
1669    
1670      /* look up the file in the coverage table */      /* look up the file in the coverage table */
# Line 1456  Line 1676 
1676        file_coverage->id = id;        file_coverage->id = id;
1677        file_coverage->num_coverage_lines = array->pn_count;        file_coverage->num_coverage_lines = array->pn_count;
1678        file_coverage->coverage_lines = xnew(int, array->pn_count);        file_coverage->coverage_lines = xnew(int, array->pn_count);
1679        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);  
       }  
1680    
1681        /* set coverage for all lines */        /* set coverage for all lines */
1682        uint32 i = 0;        uint32 i = 0;
# Line 1482  Line 1688 
1688            file_coverage->coverage_lines[i] = -1;            file_coverage->coverage_lines[i] = -1;
1689          }          }
1690          else {          else {
1691            return -1;            result = -1;
1692              goto done;
1693          }          }
1694        }        }
1695        assert(i == array->pn_count);        assert(i == array->pn_count);
# Line 1498  Line 1705 
1705        /* sanity check */        /* sanity check */
1706        assert(strcmp(file_coverage->id, id_bytes) == 0);        assert(strcmp(file_coverage->id, id_bytes) == 0);
1707        if (file_coverage->num_coverage_lines != array->pn_count) {        if (file_coverage->num_coverage_lines != array->pn_count) {
1708          return -2;          result = -2;
1709            goto done;
1710        }        }
1711    
1712        /* merge the coverage */        /* merge the coverage */
# Line 1506  Line 1714 
1714        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++) {
1715          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1716            if (file_coverage->coverage_lines[i] == -1) {            if (file_coverage->coverage_lines[i] == -1) {
1717              return -2;              result = -2;
1718                goto done;
1719            }            }
1720            file_coverage->coverage_lines[i] += (int) element->pn_dval;            file_coverage->coverage_lines[i] += (int) element->pn_dval;
1721          }          }
1722          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1723            if (file_coverage->coverage_lines[i] != -1) {            if (file_coverage->coverage_lines[i] != -1) {
1724              return -2;              result = -2;
1725                goto done;
1726            }            }
1727          }          }
1728          else {          else {
1729            return -1;            result = -1;
1730              goto done;
1731          }          }
1732        }        }
1733        assert(i == array->pn_count);        assert(i == array->pn_count);
1734        }
1735    
1736        /* if this JSON file has source, use it */      /* if this JSON file has source, use it */
1737        if (file_coverage->source_lines == NULL && source != NULL) {      if (file_coverage->source_lines == NULL && source != NULL) {
1738          file_coverage->num_source_lines = source->pn_count;        file_coverage->num_source_lines = source->pn_count;
1739          file_coverage->source_lines = xnew(char *, source->pn_count);        file_coverage->source_lines = xnew(char *, source->pn_count);
1740          uint32 i = 0;        uint32 i = 0;
1741          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++) {
1742            if (element->pn_type != TOK_STRING) {          if (element->pn_type != TOK_STRING) {
1743              return -1;            result = -1;
1744            }            goto done;
           file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));  
1745          }          }
1746          assert(i == source->pn_count);          file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));
1747        }        }
1748          assert(i == source->pn_count);
1749      }      }
1750    }    }
1751    
1752    return 0;  done:
1753      js_FinishParseContext(context, &parse_context);
1754      return result;
1755  }  }

Legend:
Removed from v.240  
changed lines
  Added in v.346

  ViewVC Help
Powered by ViewVC 1.1.24