/[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 246 by siliconforks, Sun Oct 5 18:05:42 2008 UTC revision 352 by siliconforks, Fri Oct 24 16:18:18 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 <jsemit.h>
32    #include <jsexn.h>
33  #include <jsfun.h>  #include <jsfun.h>
34  #include <jsinterp.h>  #include <jsinterp.h>
35    #include <jsiter.h>
36  #include <jsparse.h>  #include <jsparse.h>
37  #include <jsregexp.h>  #include <jsregexp.h>
38  #include <jsscope.h>  #include <jsscope.h>
# Line 53  Line 57 
57  static JSRuntime * runtime = NULL;  static JSRuntime * runtime = NULL;
58  static JSContext * context = NULL;  static JSContext * context = NULL;
59  static JSObject * global = NULL;  static JSObject * global = NULL;
60    static JSVersion js_version = JSVERSION_ECMA_3;
61    
62  /*  /*
63  JSParseNode objects store line numbers starting from 1.  JSParseNode objects store line numbers starting from 1.
# Line 62  Line 67 
67  static char * lines = NULL;  static char * lines = NULL;
68  static uint16_t num_lines = 0;  static uint16_t num_lines = 0;
69    
70    void jscoverage_set_js_version(const char * version) {
71      js_version = atoi(version);
72    }
73    
74  void jscoverage_init(void) {  void jscoverage_init(void) {
75    runtime = JS_NewRuntime(8L * 1024L * 1024L);    runtime = JS_NewRuntime(8L * 1024L * 1024L);
76    if (runtime == NULL) {    if (runtime == NULL) {
# Line 73  Line 82 
82      fatal("cannot create context");      fatal("cannot create context");
83    }    }
84    
85      JS_SetVersion(context, js_version);
86    
87    global = JS_NewObject(context, NULL, NULL, NULL);    global = JS_NewObject(context, NULL, NULL, NULL);
88    if (global == NULL) {    if (global == NULL) {
89      fatal("cannot create global object");      fatal("cannot create global object");
# Line 146  Line 157 
157        case 0xa:        case 0xa:
158          Stream_write_string(f, "\\n");          Stream_write_string(f, "\\n");
159          break;          break;
160          /* IE doesn't support this */
161          /*
162        case 0xb:        case 0xb:
163          Stream_write_string(f, "\\v");          Stream_write_string(f, "\\v");
164          break;          break;
165          */
166        case 0xc:        case 0xc:
167          Stream_write_string(f, "\\f");          Stream_write_string(f, "\\f");
168          break;          break;
# Line 195  Line 209 
209    
210  static const char * get_op(uint8 op) {  static const char * get_op(uint8 op) {
211    switch(op) {    switch(op) {
212      case JSOP_OR:
213        return "||";
214      case JSOP_AND:
215        return "&&";
216    case JSOP_BITOR:    case JSOP_BITOR:
217      return "|";      return "|";
218    case JSOP_BITXOR:    case JSOP_BITXOR:
# Line 205  Line 223 
223      return "==";      return "==";
224    case JSOP_NE:    case JSOP_NE:
225      return "!=";      return "!=";
226    case JSOP_NEW_EQ:    case JSOP_STRICTEQ:
227      return "===";      return "===";
228    case JSOP_NEW_NE:    case JSOP_STRICTNE:
229      return "!==";      return "!==";
230    case JSOP_LT:    case JSOP_LT:
231      return "<";      return "<";
# Line 240  Line 258 
258    
259  static void instrument_expression(JSParseNode * node, Stream * f);  static void instrument_expression(JSParseNode * node, Stream * f);
260  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
261    static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
262    
263  enum FunctionType {  enum FunctionType {
264    FUNCTION_NORMAL,    FUNCTION_NORMAL,
265    FUNCTION_GETTER_OR_SETTER    FUNCTION_GETTER_OR_SETTER
266  };  };
267    
268    static void output_for_in(JSParseNode * node, Stream * f) {
269      assert(node->pn_type == TOK_FOR);
270      assert(node->pn_arity == PN_BINARY);
271      Stream_write_string(f, "for ");
272      if (node->pn_iflags & JSITER_FOREACH) {
273        Stream_write_string(f, "each ");
274      }
275      Stream_write_char(f, '(');
276      instrument_expression(node->pn_left, f);
277      Stream_write_char(f, ')');
278    }
279    
280  static void instrument_function(JSParseNode * node, Stream * f, int indent, enum FunctionType type) {  static void instrument_function(JSParseNode * node, Stream * f, int indent, enum FunctionType type) {
281      assert(node->pn_type == TOK_FUNCTION);
282    assert(node->pn_arity == PN_FUNC);    assert(node->pn_arity == PN_FUNC);
283    assert(ATOM_IS_OBJECT(node->pn_funAtom));    JSObject * object = node->pn_funpob->object;
   JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom);  
284    assert(JS_ObjectIsFunction(context, object));    assert(JS_ObjectIsFunction(context, object));
285    JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);    JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
286    assert(function);    assert(function);
287    assert(object == function->object);    assert(object == &function->object);
288    Stream_printf(f, "%*s", indent, "");    Stream_printf(f, "%*s", indent, "");
289    if (type == FUNCTION_NORMAL) {    if (type == FUNCTION_NORMAL) {
290      Stream_write_string(f, "function");      Stream_write_string(f, "function");
# Line 265  Line 296 
296      print_string_atom(function->atom, f);      print_string_atom(function->atom, f);
297    }    }
298    
299    /* function parameters */    /*
300      function parameters - see JS_DecompileFunction in jsapi.cpp, which calls
301      js_DecompileFunction in jsopcode.cpp
302      */
303    Stream_write_string(f, "(");    Stream_write_string(f, "(");
304    JSAtom ** params = xnew(JSAtom *, function->nargs);    JSArenaPool pool;
305    for (int i = 0; i < function->nargs; i++) {    JS_INIT_ARENA_POOL(&pool, "instrument_function", 256, 1, &context->scriptStackQuota);
306      /* initialize to NULL for sanity check */    jsuword * local_names = NULL;
307      params[i] = NULL;    if (JS_GET_LOCAL_NAME_COUNT(function)) {
308    }      local_names = js_GetLocalNameArray(context, function, &pool);
309    JSScope * scope = OBJ_SCOPE(object);      if (local_names == NULL) {
310    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;  
311      }      }
     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);  
312    }    }
313    for (int i = 0; i < function->nargs; i++) {    for (int i = 0; i < function->nargs; i++) {
     assert(params[i] != NULL);  
314      if (i > 0) {      if (i > 0) {
315        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
316      }      }
317      if (ATOM_IS_STRING(params[i])) {      JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]);
318        print_string_atom(params[i], f);      if (param == NULL) {
319          fatal("unsupported parameter type for function: %s", file_id);
320      }      }
321        print_string_atom(param, f);
322    }    }
323      JS_FinishArenaPool(&pool);
324    Stream_write_string(f, ") {\n");    Stream_write_string(f, ") {\n");
   free(params);  
325    
326    /* function body */    /* function body */
327    instrument_statement(node->pn_body, f, indent + 2, false);    if (function->flags & JSFUN_EXPR_CLOSURE) {
328        /* expression closure */
329        output_statement(node->pn_body, f, indent + 2, false);
330      }
331      else {
332        instrument_statement(node->pn_body, f, indent + 2, false);
333      }
334    
335    Stream_write_string(f, "}\n");    Stream_write_string(f, "}\n");
336  }  }
337    
338  static void instrument_function_call(JSParseNode * node, Stream * f) {  static void instrument_function_call(JSParseNode * node, Stream * f) {
339    instrument_expression(node->pn_head, f);    JSParseNode * function_node = node->pn_head;
340      if (function_node->pn_type == TOK_FUNCTION) {
341        JSObject * object = function_node->pn_funpob->object;
342        assert(JS_ObjectIsFunction(context, object));
343        JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
344        assert(function);
345        assert(object == &function->object);
346    
347        if (function_node->pn_flags & TCF_GENEXP_LAMBDA) {
348          /* it's a generator expression */
349          JSParseNode * lexical_scope_node = function_node->pn_body;
350          assert(lexical_scope_node->pn_type == TOK_LEXICALSCOPE);
351          assert(lexical_scope_node->pn_arity == PN_NAME);
352          JSParseNode * for_node = lexical_scope_node->pn_body;
353          assert(for_node->pn_type == TOK_FOR);
354          assert(for_node->pn_arity == PN_BINARY);
355          JSParseNode * if_node = NULL;
356          JSParseNode * semi_node;
357          switch (for_node->pn_right->pn_type) {
358          case TOK_SEMI:
359            semi_node = for_node->pn_right;
360            break;
361          case TOK_IF:
362            if_node = for_node->pn_right;
363            assert(if_node->pn_arity == PN_TERNARY);
364            semi_node = if_node->pn_kid2;
365            assert(semi_node->pn_type == TOK_SEMI);
366            break;
367          default:
368            abort();
369            break;
370          }
371          assert(semi_node->pn_arity == PN_UNARY);
372          JSParseNode * yield_node = semi_node->pn_kid;
373          assert(yield_node->pn_type == TOK_YIELD);
374          Stream_write_char(f, '(');
375          instrument_expression(yield_node->pn_kid, f);
376          Stream_write_char(f, ' ');
377          output_for_in(for_node, f);
378          if (if_node) {
379            Stream_write_string(f, " if (");
380            instrument_expression(if_node->pn_kid1, f);
381            Stream_write_char(f, ')');
382          }
383          Stream_write_char(f, ')');
384          return;
385        }
386        else {
387          Stream_write_char(f, '(');
388          instrument_expression(function_node, f);
389          Stream_write_char(f, ')');
390        }
391      }
392      else {
393        instrument_expression(function_node, f);
394      }
395    Stream_write_char(f, '(');    Stream_write_char(f, '(');
396    for (struct JSParseNode * p = node->pn_head->pn_next; p != NULL; p = p->pn_next) {    for (struct JSParseNode * p = function_node->pn_next; p != NULL; p = p->pn_next) {
397      if (p != node->pn_head->pn_next) {      if (p != node->pn_head->pn_next) {
398        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
399      }      }
# Line 312  Line 402 
402    Stream_write_char(f, ')');    Stream_write_char(f, ')');
403  }  }
404    
405    static void instrument_declarations(JSParseNode * list, Stream * f) {
406      assert(list->pn_arity == PN_LIST);
407      for (JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
408        switch (p->pn_type) {
409        case TOK_NAME:
410          assert(p->pn_arity == PN_NAME);
411          if (p != list->pn_head) {
412            Stream_write_string(f, ", ");
413          }
414          print_string_atom(p->pn_atom, f);
415          if (p->pn_expr != NULL) {
416            Stream_write_string(f, " = ");
417            instrument_expression(p->pn_expr, f);
418          }
419          break;
420        case TOK_ASSIGN:
421        case TOK_RB:
422        case TOK_RC:
423          /* destructuring */
424          instrument_expression(p, f);
425          break;
426        default:
427          abort();
428          break;
429        }
430      }
431    }
432    
433  /*  /*
434  See <Expressions> in jsparse.h.  See <Expressions> in jsparse.h.
435  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
# Line 370  Line 488 
488      instrument_expression(node->pn_kid3, f);      instrument_expression(node->pn_kid3, f);
489      break;      break;
490    case TOK_OR:    case TOK_OR:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " || ");  
     instrument_expression(node->pn_right, f);  
     break;  
491    case TOK_AND:    case TOK_AND:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " && ");  
     instrument_expression(node->pn_right, f);  
     break;  
492    case TOK_BITOR:    case TOK_BITOR:
493    case TOK_BITXOR:    case TOK_BITXOR:
494    case TOK_BITAND:    case TOK_BITAND:
# Line 491  Line 601 
601      assert(ATOM_IS_STRING(node->pn_atom));      assert(ATOM_IS_STRING(node->pn_atom));
602      {      {
603        JSString * s = ATOM_TO_STRING(node->pn_atom);        JSString * s = ATOM_TO_STRING(node->pn_atom);
604        /* XXX - semantics changed in 1.7 */        bool must_quote;
605        if (! ATOM_KEYWORD(node->pn_atom) && js_IsIdentifier(s)) {        if (JSSTRING_LENGTH(s) == 0) {
606          Stream_write_char(f, '.');          must_quote = true;
607          print_string_atom(node->pn_atom, f);        }
608          else if (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF) {
609            must_quote = true;
610          }
611          else if (! js_IsIdentifier(s)) {
612            must_quote = true;
613        }        }
614        else {        else {
615            must_quote = false;
616          }
617          if (must_quote) {
618          Stream_write_char(f, '[');          Stream_write_char(f, '[');
619          print_quoted_string_atom(node->pn_atom, f);          print_quoted_string_atom(node->pn_atom, f);
620          Stream_write_char(f, ']');          Stream_write_char(f, ']');
621        }        }
622          else {
623            Stream_write_char(f, '.');
624            print_string_atom(node->pn_atom, f);
625          }
626      }      }
627      break;      break;
628    case TOK_LB:    case TOK_LB:
# Line 539  Line 661 
661        /* check whether this is a getter or setter */        /* check whether this is a getter or setter */
662        switch (p->pn_op) {        switch (p->pn_op) {
663        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;  
664        case JSOP_SETTER:        case JSOP_SETTER:
665          Stream_write_string(f, "set ");          if (p->pn_op == JSOP_GETTER) {
666              Stream_write_string(f, "get ");
667            }
668            else {
669              Stream_write_string(f, "set ");
670            }
671          instrument_expression(p->pn_left, f);          instrument_expression(p->pn_left, f);
672            if (p->pn_right->pn_type != TOK_FUNCTION) {
673              fatal("parse error: expected function");
674            }
675          instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);          instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);
676          break;          break;
677        default:        default:
# Line 568  Line 694 
694    case TOK_STRING:    case TOK_STRING:
695      print_quoted_string_atom(node->pn_atom, f);      print_quoted_string_atom(node->pn_atom, f);
696      break;      break;
697    case TOK_OBJECT:    case TOK_REGEXP:
698      switch (node->pn_op) {      assert(node->pn_op == JSOP_REGEXP);
699      case JSOP_OBJECT:      {
700        /* I assume this is JSOP_REGEXP */        JSObject * object = node->pn_pob->object;
701        abort();        jsval result;
702        break;        js_regexp_toString(context, object, &result);
703      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;  
704      }      }
705      break;      break;
706    case TOK_NUMBER:    case TOK_NUMBER:
# Line 631  Line 746 
746      Stream_write_string(f, " in ");      Stream_write_string(f, " in ");
747      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
748      break;      break;
749      case TOK_LEXICALSCOPE:
750        assert(node->pn_arity == PN_NAME);
751        assert(node->pn_expr->pn_type == TOK_LET);
752        assert(node->pn_expr->pn_arity == PN_BINARY);
753        Stream_write_string(f, "let(");
754        assert(node->pn_expr->pn_left->pn_type == TOK_LP);
755        assert(node->pn_expr->pn_left->pn_arity == PN_LIST);
756        instrument_declarations(node->pn_expr->pn_left, f);
757        Stream_write_string(f, ") ");
758        instrument_expression(node->pn_expr->pn_right, f);
759        break;
760      case TOK_YIELD:
761        assert(node->pn_arity == PN_UNARY);
762        Stream_write_string(f, "yield ");
763        instrument_expression(node->pn_kid, f);
764        break;
765      case TOK_ARRAYCOMP:
766        assert(node->pn_arity == PN_LIST);
767        {
768          JSParseNode * block_node;
769          switch (node->pn_count) {
770          case 1:
771            block_node = node->pn_head;
772            break;
773          case 2:
774            block_node = node->pn_head->pn_next;
775            break;
776          default:
777            abort();
778            break;
779          }
780          assert(block_node->pn_type == TOK_LEXICALSCOPE);
781          assert(block_node->pn_arity == PN_NAME);
782          JSParseNode * for_node = block_node->pn_expr;
783          assert(for_node->pn_type == TOK_FOR);
784          assert(for_node->pn_arity == PN_BINARY);
785          JSParseNode * if_node = NULL;
786          JSParseNode * push_node;
787          switch (for_node->pn_right->pn_type) {
788          case TOK_ARRAYPUSH:
789            push_node = for_node->pn_right;
790            assert(push_node->pn_arity == PN_UNARY);
791            break;
792          case TOK_IF:
793            if_node = for_node->pn_right;
794            assert(if_node->pn_arity == PN_TERNARY);
795            push_node = if_node->pn_kid2;
796            break;
797          default:
798            abort();
799            break;
800          }
801          Stream_write_char(f, '[');
802          instrument_expression(push_node->pn_kid, f);
803          Stream_write_char(f, ' ');
804          output_for_in(for_node, f);
805          if (if_node) {
806            Stream_write_string(f, " if (");
807            instrument_expression(if_node->pn_kid1, f);
808            Stream_write_char(f, ')');
809          }
810          Stream_write_char(f, ']');
811        }
812        break;
813      case TOK_VAR:
814        assert(node->pn_arity == PN_LIST);
815        Stream_write_string(f, "var ");
816        instrument_declarations(node, f);
817        break;
818      case TOK_LET:
819        assert(node->pn_arity == PN_LIST);
820        Stream_write_string(f, "let ");
821        instrument_declarations(node, f);
822        break;
823    default:    default:
824      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);
825    }    }
826  }  }
827    
 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);  
     }  
   }  
 }  
   
828  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) {
829    switch (node->pn_type) {    switch (node->pn_type) {
830    case TOK_FUNCTION:    case TOK_FUNCTION:
# Line 727  Line 898 
898      Stream_write_string(f, "switch (");      Stream_write_string(f, "switch (");
899      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
900      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
901      for (struct JSParseNode * p = node->pn_right->pn_head; p != NULL; p = p->pn_next) {      {
902        Stream_printf(f, "%*s", indent, "");        JSParseNode * list = node->pn_right;
903        switch (p->pn_type) {        if (list->pn_type == TOK_LEXICALSCOPE) {
904        case TOK_CASE:          list = list->pn_expr;
905          Stream_write_string(f, "case ");        }
906          instrument_expression(p->pn_left, f);        for (struct JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
907          Stream_write_string(f, ":\n");          Stream_printf(f, "%*s", indent, "");
908          break;          switch (p->pn_type) {
909        case TOK_DEFAULT:          case TOK_CASE:
910          Stream_write_string(f, "default:\n");            Stream_write_string(f, "case ");
911          break;            instrument_expression(p->pn_left, f);
912        default:            Stream_write_string(f, ":\n");
913          abort();            break;
914          break;          case TOK_DEFAULT:
915              Stream_write_string(f, "default:\n");
916              break;
917            default:
918              abort();
919              break;
920            }
921            instrument_statement(p->pn_right, f, indent + 2, false);
922        }        }
       instrument_statement(p->pn_right, f, indent + 2, false);  
923      }      }
924      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
925      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
# Line 774  Line 951 
951    case TOK_FOR:    case TOK_FOR:
952      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
953      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
     Stream_write_string(f, "for (");  
954      switch (node->pn_left->pn_type) {      switch (node->pn_left->pn_type) {
955      case TOK_IN:      case TOK_IN:
956        /* for/in */        /* for/in */
957        assert(node->pn_left->pn_arity == PN_BINARY);        assert(node->pn_left->pn_arity == PN_BINARY);
958        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);  
959        break;        break;
960      case TOK_RESERVED:      case TOK_RESERVED:
961        /* for (;;) */        /* for (;;) */
962        assert(node->pn_left->pn_arity == PN_TERNARY);        assert(node->pn_left->pn_arity == PN_TERNARY);
963          Stream_write_string(f, "for (");
964        if (node->pn_left->pn_kid1) {        if (node->pn_left->pn_kid1) {
965          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);  
         }  
966        }        }
967        Stream_write_string(f, ";");        Stream_write_string(f, ";");
968        if (node->pn_left->pn_kid2) {        if (node->pn_left->pn_kid2) {
# Line 821  Line 974 
974          Stream_write_char(f, ' ');          Stream_write_char(f, ' ');
975          instrument_expression(node->pn_left->pn_kid3, f);          instrument_expression(node->pn_left->pn_kid3, f);
976        }        }
977          Stream_write_char(f, ')');
978        break;        break;
979      default:      default:
980        abort();        abort();
981        break;        break;
982      }      }
983      Stream_write_string(f, ") {\n");      Stream_write_string(f, " {\n");
984      instrument_statement(node->pn_right, f, indent + 2, false);      instrument_statement(node->pn_right, f, indent + 2, false);
985      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
986      break;      break;
# Line 843  Line 997 
997      instrument_statement(node->pn_kid1, f, indent + 2, false);      instrument_statement(node->pn_kid1, f, indent + 2, false);
998      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
999      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
1000      {      if (node->pn_kid2) {
1001        for (JSParseNode * catch = node->pn_kid2; catch != NULL; catch = catch->pn_kid2) {        assert(node->pn_kid2->pn_type == TOK_RESERVED);
1002          for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) {
1003            assert(scope->pn_type == TOK_LEXICALSCOPE);
1004            JSParseNode * catch = scope->pn_expr;
1005          assert(catch->pn_type == TOK_CATCH);          assert(catch->pn_type == TOK_CATCH);
1006          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
1007          Stream_write_string(f, "catch (");          Stream_write_string(f, "catch (");
1008            /* this may not be a name - destructuring assignment */
1009            /*
1010          assert(catch->pn_kid1->pn_arity == PN_NAME);          assert(catch->pn_kid1->pn_arity == PN_NAME);
1011          print_string_atom(catch->pn_kid1->pn_atom, f);          print_string_atom(catch->pn_kid1->pn_atom, f);
1012          if (catch->pn_kid1->pn_expr) {          */
1013            instrument_expression(catch->pn_kid1, f);
1014            if (catch->pn_kid2) {
1015            Stream_write_string(f, " if ");            Stream_write_string(f, " if ");
1016            instrument_expression(catch->pn_kid1->pn_expr, f);            instrument_expression(catch->pn_kid2, f);
1017          }          }
1018          Stream_write_string(f, ") {\n");          Stream_write_string(f, ") {\n");
1019          instrument_statement(catch->pn_kid3, f, indent + 2, false);          instrument_statement(catch->pn_kid3, f, indent + 2, false);
# Line 894  Line 1055 
1055      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
1056      break;      break;
1057    case TOK_VAR:    case TOK_VAR:
1058      instrument_var_statement(node, f, indent);      Stream_printf(f, "%*s", indent, "");
1059        instrument_expression(node, f);
1060      Stream_write_string(f, ";\n");      Stream_write_string(f, ";\n");
1061      break;      break;
1062    case TOK_RETURN:    case TOK_RETURN:
# Line 929  Line 1091 
1091      */      */
1092      output_statement(node->pn_expr, f, indent, false);      output_statement(node->pn_expr, f, indent, false);
1093      break;      break;
1094      case TOK_LEXICALSCOPE:
1095        /* let statement */
1096        assert(node->pn_arity == PN_NAME);
1097        switch (node->pn_expr->pn_type) {
1098        case TOK_LET:
1099          /* let statement */
1100          assert(node->pn_expr->pn_arity == PN_BINARY);
1101          instrument_statement(node->pn_expr, f, indent, false);
1102          break;
1103        case TOK_LC:
1104          /* block */
1105          Stream_printf(f, "%*s", indent, "");
1106          Stream_write_string(f, "{\n");
1107          instrument_statement(node->pn_expr, f, indent + 2, false);
1108          Stream_printf(f, "%*s", indent, "");
1109          Stream_write_string(f, "}\n");
1110          break;
1111        case TOK_FOR:
1112          instrument_statement(node->pn_expr, f, indent, false);
1113          break;
1114        default:
1115          abort();
1116          break;
1117        }
1118        break;
1119      case TOK_LET:
1120        switch (node->pn_arity) {
1121        case PN_BINARY:
1122          /* let statement */
1123          Stream_printf(f, "%*s", indent, "");
1124          Stream_write_string(f, "let (");
1125          assert(node->pn_left->pn_type == TOK_LP);
1126          assert(node->pn_left->pn_arity == PN_LIST);
1127          instrument_declarations(node->pn_left, f);
1128          Stream_write_string(f, ") {\n");
1129          instrument_statement(node->pn_right, f, indent + 2, false);
1130          Stream_printf(f, "%*s", indent, "");
1131          Stream_write_string(f, "}\n");
1132          break;
1133        case PN_LIST:
1134          /* let definition */
1135          Stream_printf(f, "%*s", indent, "");
1136          instrument_expression(node, f);
1137          Stream_write_string(f, ";\n");
1138          break;
1139        default:
1140          abort();
1141          break;
1142        }
1143        break;
1144      case TOK_DEBUGGER:
1145        Stream_printf(f, "%*s", indent, "");
1146        Stream_write_string(f, "debugger;\n");
1147        break;
1148    default:    default:
1149      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);
1150    }    }
# Line 940  Line 1156 
1156  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
1157  */  */
1158  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) {
1159    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC && node->pn_type != TOK_LEXICALSCOPE) {
1160      uint16_t line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
1161      if (line > num_lines) {      if (line > num_lines) {
1162        fatal("%s: script contains more than 65,535 lines", file_id);        fatal("%s: script contains more than 65,535 lines", file_id);
# Line 990  Line 1206 
1206    return true;    return true;
1207  }  }
1208    
1209    static void error_reporter(JSContext * context, const char * message, JSErrorReport * report) {
1210      fprintf(stderr, "jscoverage: parse error: line %u: %s\n", report->lineno, message);
1211    }
1212    
1213  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) {
1214    file_id = id;    file_id = id;
1215    
1216    /* scan the javascript */    /* parse the javascript */
1217    JSTokenStream * token_stream = js_NewTokenStream(context, characters, num_characters, NULL, 1, NULL);    JSParseContext parse_context;
1218    if (token_stream == NULL) {    if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) {
1219      fatal("cannot create token stream from file: %s", file_id);      fatal("cannot create token stream from file: %s", file_id);
1220    }    }
1221      JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);
1222    /* parse the javascript */    JSParseNode * node = js_ParseScript(context, global, &parse_context);
   JSParseNode * node = js_ParseTokenStream(context, global, token_stream);  
1223    if (node == NULL) {    if (node == NULL) {
1224        js_ReportUncaughtException(context);
1225      fatal("parse error in file: %s", file_id);      fatal("parse error in file: %s", file_id);
1226    }    }
1227      JS_SetErrorReporter(context, old_error_reporter);
1228    num_lines = node->pn_pos.end.lineno;    num_lines = node->pn_pos.end.lineno;
1229    lines = xmalloc(num_lines);    lines = xmalloc(num_lines);
1230    for (unsigned int i = 0; i < num_lines; i++) {    for (unsigned int i = 0; i < num_lines; i++) {
# Line 1085  Line 1306 
1306    
1307    Stream * instrumented = Stream_new(0);    Stream * instrumented = Stream_new(0);
1308    instrument_statement(node, instrumented, 0, false);    instrument_statement(node, instrumented, 0, false);
1309      js_FinishParseContext(context, &parse_context);
1310    
1311    /* write line number info to the output */    /* write line number info to the output */
1312    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 1165  Line 1387 
1387            /* line feed (new line) */            /* line feed (new line) */
1388            done = true;            done = true;
1389            break;            break;
1390            /* IE doesn't support this */
1391            /*
1392          case 0xb:          case 0xb:
           /* vertical tab */  
1393            Stream_write_string(output, "\\v");            Stream_write_string(output, "\\v");
1394            break;            break;
1395            */
1396          case 0xc:          case 0xc:
1397            /* form feed */            /* form feed */
1398            Stream_write_string(output, "\\f");            Stream_write_string(output, "\\f");
# Line 1223  Line 1447 
1447            /* line feed (new line) */            /* line feed (new line) */
1448            done = true;            done = true;
1449            break;            break;
1450            /* IE doesn't support this */
1451            /*
1452          case 0xb:          case 0xb:
           /* vertical tab */  
1453            Stream_write_string(output, "\\v");            Stream_write_string(output, "\\v");
1454            break;            break;
1455            */
1456          case 0xc:          case 0xc:
1457            /* form feed */            /* form feed */
1458            Stream_write_string(output, "\\f");            Stream_write_string(output, "\\f");
# Line 1353  Line 1579 
1579  }  }
1580    
1581  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) {
1582      int result = 0;
1583    
1584    jschar * base = js_InflateString(context, (char *) json, &length);    jschar * base = js_InflateString(context, (char *) json, &length);
1585    if (base == NULL) {    if (base == NULL) {
1586      fatal("out of memory");      fatal("out of memory");
# Line 1365  Line 1593 
1593    
1594    JS_free(context, base);    JS_free(context, base);
1595    
1596    JSTokenStream * token_stream = js_NewTokenStream(context, parenthesized_json, length + 2, NULL, 1, NULL);    JSParseContext parse_context;
1597    if (token_stream == NULL) {    if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) {
1598      fatal("cannot create token stream");      free(parenthesized_json);
1599        return -1;
1600    }    }
1601      JSParseNode * root = js_ParseScript(context, global, &parse_context);
   JSParseNode * root = js_ParseTokenStream(context, global, token_stream);  
1602    free(parenthesized_json);    free(parenthesized_json);
1603    if (root == NULL) {    if (root == NULL) {
1604      return -1;      result = -1;
1605        goto done;
1606    }    }
1607    
1608    /* root node must be TOK_LC */    /* root node must be TOK_LC */
1609    if (root->pn_type != TOK_LC) {    if (root->pn_type != TOK_LC) {
1610      return -1;      result = -1;
1611        goto done;
1612    }    }
1613    JSParseNode * semi = root->pn_u.list.head;    JSParseNode * semi = root->pn_u.list.head;
1614    
1615    /* 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 */
1616    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {
1617      return -1;      result = -1;
1618        goto done;
1619    }    }
1620    JSParseNode * parenthesized = semi->pn_kid;    JSParseNode * parenthesized = semi->pn_kid;
1621    
1622    /* this must be a parenthesized expression */    /* this must be a parenthesized expression */
1623    if (parenthesized->pn_type != TOK_RP) {    if (parenthesized->pn_type != TOK_RP) {
1624      return -1;      result = -1;
1625        goto done;
1626    }    }
1627    JSParseNode * object = parenthesized->pn_kid;    JSParseNode * object = parenthesized->pn_kid;
1628    
1629    /* this must be an object literal */    /* this must be an object literal */
1630    if (object->pn_type != TOK_RC) {    if (object->pn_type != TOK_RC) {
1631      return -1;      result = -1;
1632        goto done;
1633    }    }
1634    
1635    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {
1636      /* every element of this list must be TOK_COLON */      /* every element of this list must be TOK_COLON */
1637      if (p->pn_type != TOK_COLON) {      if (p->pn_type != TOK_COLON) {
1638        return -1;        result = -1;
1639          goto done;
1640      }      }
1641    
1642      /* the key must be a string representing the file */      /* the key must be a string representing the file */
1643      JSParseNode * key = p->pn_left;      JSParseNode * key = p->pn_left;
1644      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {
1645        return -1;        result = -1;
1646          goto done;
1647      }      }
1648      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));
1649    
1650      /* the value must be an object literal OR an array */      /* the value must be an object literal OR an array */
1651      JSParseNode * value = p->pn_right;      JSParseNode * value = p->pn_right;
1652      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {
1653        return -1;        result = -1;
1654          goto done;
1655      }      }
1656    
1657      JSParseNode * array = NULL;      JSParseNode * array = NULL;
# Line 1427  Line 1663 
1663      else if (value->pn_type == TOK_RC) {      else if (value->pn_type == TOK_RC) {
1664        /* an object literal */        /* an object literal */
1665        if (value->pn_count != 2) {        if (value->pn_count != 2) {
1666          return -1;          result = -1;
1667            goto done;
1668        }        }
1669        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {
1670          if (element->pn_type != TOK_COLON) {          if (element->pn_type != TOK_COLON) {
1671            return -1;            result = -1;
1672              goto done;
1673          }          }
1674          JSParseNode * left = element->pn_left;          JSParseNode * left = element->pn_left;
1675          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {
1676            return -1;            result = -1;
1677              goto done;
1678          }          }
1679          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));
1680          if (strcmp(s, "coverage") == 0) {          if (strcmp(s, "coverage") == 0) {
1681            array = element->pn_right;            array = element->pn_right;
1682            if (array->pn_type != TOK_RB) {            if (array->pn_type != TOK_RB) {
1683              return -1;              result = -1;
1684                goto done;
1685            }            }
1686          }          }
1687          else if (strcmp(s, "source") == 0) {          else if (strcmp(s, "source") == 0) {
1688            source = element->pn_right;            source = element->pn_right;
1689            if (source->pn_type != TOK_RB) {            if (source->pn_type != TOK_RB) {
1690              return -1;              result = -1;
1691                goto done;
1692            }            }
1693          }          }
1694          else {          else {
1695            return -1;            result = -1;
1696              goto done;
1697          }          }
1698        }        }
1699      }      }
1700      else {      else {
1701        return -1;        result = -1;
1702          goto done;
1703      }      }
1704    
1705      if (array == NULL) {      if (array == NULL) {
1706        return -1;        result = -1;
1707          goto done;
1708      }      }
1709    
1710      /* look up the file in the coverage table */      /* look up the file in the coverage table */
# Line 1484  Line 1728 
1728            file_coverage->coverage_lines[i] = -1;            file_coverage->coverage_lines[i] = -1;
1729          }          }
1730          else {          else {
1731            return -1;            result = -1;
1732              goto done;
1733          }          }
1734        }        }
1735        assert(i == array->pn_count);        assert(i == array->pn_count);
# Line 1500  Line 1745 
1745        /* sanity check */        /* sanity check */
1746        assert(strcmp(file_coverage->id, id_bytes) == 0);        assert(strcmp(file_coverage->id, id_bytes) == 0);
1747        if (file_coverage->num_coverage_lines != array->pn_count) {        if (file_coverage->num_coverage_lines != array->pn_count) {
1748          return -2;          result = -2;
1749            goto done;
1750        }        }
1751    
1752        /* merge the coverage */        /* merge the coverage */
# Line 1508  Line 1754 
1754        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++) {
1755          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1756            if (file_coverage->coverage_lines[i] == -1) {            if (file_coverage->coverage_lines[i] == -1) {
1757              return -2;              result = -2;
1758                goto done;
1759            }            }
1760            file_coverage->coverage_lines[i] += (int) element->pn_dval;            file_coverage->coverage_lines[i] += (int) element->pn_dval;
1761          }          }
1762          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1763            if (file_coverage->coverage_lines[i] != -1) {            if (file_coverage->coverage_lines[i] != -1) {
1764              return -2;              result = -2;
1765                goto done;
1766            }            }
1767          }          }
1768          else {          else {
1769            return -1;            result = -1;
1770              goto done;
1771          }          }
1772        }        }
1773        assert(i == array->pn_count);        assert(i == array->pn_count);
# Line 1531  Line 1780 
1780        uint32 i = 0;        uint32 i = 0;
1781        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++) {
1782          if (element->pn_type != TOK_STRING) {          if (element->pn_type != TOK_STRING) {
1783            return -1;            result = -1;
1784              goto done;
1785          }          }
1786          file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));          file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));
1787        }        }
# Line 1539  Line 1789 
1789      }      }
1790    }    }
1791    
1792    return 0;  done:
1793      js_FinishParseContext(context, &parse_context);
1794      return result;
1795  }  }

Legend:
Removed from v.246  
changed lines
  Added in v.352

  ViewVC Help
Powered by ViewVC 1.1.24