/[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 293 by siliconforks, Sun Oct 12 16:41:57 2008 UTC revision 357 by siliconforks, Fri Oct 24 21:48:21 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>  #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 54  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 63  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 74  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 147  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 196  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 206  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 241  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 output_array_comprehension_or_generator_expression(JSParseNode * node, Stream * f) {
281      assert(node->pn_type == TOK_LEXICALSCOPE);
282      assert(node->pn_arity == PN_NAME);
283      JSParseNode * for_node = node->pn_expr;
284      assert(for_node->pn_type == TOK_FOR);
285      assert(for_node->pn_arity == PN_BINARY);
286      JSParseNode * p = for_node;
287      while (p->pn_type == TOK_FOR) {
288        p = p->pn_right;
289      }
290      JSParseNode * if_node = NULL;
291      if (p->pn_type == TOK_IF) {
292        if_node = p;
293        assert(if_node->pn_arity == PN_TERNARY);
294        p = if_node->pn_kid2;
295      }
296      assert(p->pn_arity == PN_UNARY);
297      p = p->pn_kid;
298      if (p->pn_type == TOK_YIELD) {
299        /* for generator expressions */
300        p = p->pn_kid;
301      }
302    
303      instrument_expression(p, f);
304      p = for_node;
305      while (p->pn_type == TOK_FOR) {
306        Stream_write_char(f, ' ');
307        output_for_in(p, f);
308        p = p->pn_right;
309      }
310      if (if_node) {
311        Stream_write_string(f, " if (");
312        instrument_expression(if_node->pn_kid1, f);
313        Stream_write_char(f, ')');
314      }
315    }
316    
317  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) {
318      assert(node->pn_type == TOK_FUNCTION);
319    assert(node->pn_arity == PN_FUNC);    assert(node->pn_arity == PN_FUNC);
320    assert(ATOM_IS_OBJECT(node->pn_funAtom));    JSObject * object = node->pn_funpob->object;
   JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom);  
321    assert(JS_ObjectIsFunction(context, object));    assert(JS_ObjectIsFunction(context, object));
322    JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);    JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
323    assert(function);    assert(function);
324    assert(object == function->object);    assert(object == &function->object);
325    Stream_printf(f, "%*s", indent, "");    Stream_printf(f, "%*s", indent, "");
326    if (type == FUNCTION_NORMAL) {    if (type == FUNCTION_NORMAL) {
327      Stream_write_string(f, "function");      Stream_write_string(f, "function");
# Line 266  Line 333 
333      print_string_atom(function->atom, f);      print_string_atom(function->atom, f);
334    }    }
335    
336    /* function parameters */    /*
337      function parameters - see JS_DecompileFunction in jsapi.cpp, which calls
338      js_DecompileFunction in jsopcode.cpp
339      */
340    Stream_write_string(f, "(");    Stream_write_string(f, "(");
341    JSAtom ** params = xnew(JSAtom *, function->nargs);    JSArenaPool pool;
342    for (int i = 0; i < function->nargs; i++) {    JS_INIT_ARENA_POOL(&pool, "instrument_function", 256, 1, &context->scriptStackQuota);
343      /* initialize to NULL for sanity check */    jsuword * local_names = NULL;
344      params[i] = NULL;    if (JS_GET_LOCAL_NAME_COUNT(function)) {
345    }      local_names = js_GetLocalNameArray(context, function, &pool);
346    JSScope * scope = OBJ_SCOPE(object);      if (local_names == NULL) {
347    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;  
348      }      }
     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);  
349    }    }
350    for (int i = 0; i < function->nargs; i++) {    for (int i = 0; i < function->nargs; i++) {
     assert(params[i] != NULL);  
351      if (i > 0) {      if (i > 0) {
352        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
353      }      }
354      if (ATOM_IS_STRING(params[i])) {      JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]);
355        print_string_atom(params[i], f);      if (param == NULL) {
356          fatal("unsupported parameter type for function: %s", file_id);
357      }      }
358        print_string_atom(param, f);
359    }    }
360      JS_FinishArenaPool(&pool);
361    Stream_write_string(f, ") {\n");    Stream_write_string(f, ") {\n");
   free(params);  
362    
363    /* function body */    /* function body */
364    instrument_statement(node->pn_body, f, indent + 2, false);    if (function->flags & JSFUN_EXPR_CLOSURE) {
365        /* expression closure */
366        output_statement(node->pn_body, f, indent + 2, false);
367      }
368      else {
369        instrument_statement(node->pn_body, f, indent + 2, false);
370      }
371    
372    Stream_write_string(f, "}\n");    Stream_write_string(f, "}\n");
373  }  }
374    
375  static void instrument_function_call(JSParseNode * node, Stream * f) {  static void instrument_function_call(JSParseNode * node, Stream * f) {
376    instrument_expression(node->pn_head, f);    JSParseNode * function_node = node->pn_head;
377      if (function_node->pn_type == TOK_FUNCTION) {
378        JSObject * object = function_node->pn_funpob->object;
379        assert(JS_ObjectIsFunction(context, object));
380        JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
381        assert(function);
382        assert(object == &function->object);
383    
384        if (function_node->pn_flags & TCF_GENEXP_LAMBDA) {
385          /* it's a generator expression */
386          Stream_write_char(f, '(');
387          output_array_comprehension_or_generator_expression(function_node->pn_body, f);
388          Stream_write_char(f, ')');
389          return;
390        }
391        else {
392          Stream_write_char(f, '(');
393          instrument_expression(function_node, f);
394          Stream_write_char(f, ')');
395        }
396      }
397      else {
398        instrument_expression(function_node, f);
399      }
400    Stream_write_char(f, '(');    Stream_write_char(f, '(');
401    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) {
402      if (p != node->pn_head->pn_next) {      if (p != node->pn_head->pn_next) {
403        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
404      }      }
# Line 313  Line 407 
407    Stream_write_char(f, ')');    Stream_write_char(f, ')');
408  }  }
409    
410    static void instrument_declarations(JSParseNode * list, Stream * f) {
411      assert(list->pn_arity == PN_LIST);
412      for (JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
413        switch (p->pn_type) {
414        case TOK_NAME:
415          assert(p->pn_arity == PN_NAME);
416          if (p != list->pn_head) {
417            Stream_write_string(f, ", ");
418          }
419          print_string_atom(p->pn_atom, f);
420          if (p->pn_expr != NULL) {
421            Stream_write_string(f, " = ");
422            instrument_expression(p->pn_expr, f);
423          }
424          break;
425        case TOK_ASSIGN:
426        case TOK_RB:
427        case TOK_RC:
428          /* destructuring */
429          instrument_expression(p, f);
430          break;
431        default:
432          abort();
433          break;
434        }
435      }
436    }
437    
438  /*  /*
439  See <Expressions> in jsparse.h.  See <Expressions> in jsparse.h.
440  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
# Line 371  Line 493 
493      instrument_expression(node->pn_kid3, f);      instrument_expression(node->pn_kid3, f);
494      break;      break;
495    case TOK_OR:    case TOK_OR:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " || ");  
     instrument_expression(node->pn_right, f);  
     break;  
496    case TOK_AND:    case TOK_AND:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " && ");  
     instrument_expression(node->pn_right, f);  
     break;  
497    case TOK_BITOR:    case TOK_BITOR:
498    case TOK_BITXOR:    case TOK_BITXOR:
499    case TOK_BITAND:    case TOK_BITAND:
# Line 435  Line 549 
549        instrument_expression(node->pn_kid, f);        instrument_expression(node->pn_kid, f);
550        break;        break;
551      default:      default:
552        abort();        fatal("%s: unknown operator (%d) in file", file_id, node->pn_op);
553        break;        break;
554      }      }
555      break;      break;
# Line 492  Line 606 
606      assert(ATOM_IS_STRING(node->pn_atom));      assert(ATOM_IS_STRING(node->pn_atom));
607      {      {
608        JSString * s = ATOM_TO_STRING(node->pn_atom);        JSString * s = ATOM_TO_STRING(node->pn_atom);
609        /* XXX - semantics changed in 1.7 */        bool must_quote;
610        if (! ATOM_KEYWORD(node->pn_atom) && js_IsIdentifier(s)) {        if (JSSTRING_LENGTH(s) == 0) {
611          Stream_write_char(f, '.');          must_quote = true;
612          print_string_atom(node->pn_atom, f);        }
613          else if (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF) {
614            must_quote = true;
615          }
616          else if (! js_IsIdentifier(s)) {
617            must_quote = true;
618        }        }
619        else {        else {
620            must_quote = false;
621          }
622          if (must_quote) {
623          Stream_write_char(f, '[');          Stream_write_char(f, '[');
624          print_quoted_string_atom(node->pn_atom, f);          print_quoted_string_atom(node->pn_atom, f);
625          Stream_write_char(f, ']');          Stream_write_char(f, ']');
626        }        }
627          else {
628            Stream_write_char(f, '.');
629            print_string_atom(node->pn_atom, f);
630          }
631      }      }
632      break;      break;
633    case TOK_LB:    case TOK_LB:
# Line 532  Line 658 
658    case TOK_RC:    case TOK_RC:
659      Stream_write_char(f, '{');      Stream_write_char(f, '{');
660      for (struct JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) {      for (struct JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) {
661        assert(p->pn_type == TOK_COLON);        if (p->pn_type != TOK_COLON) {
662            fatal("unsupported node type in file %s: %d", file_id, p->pn_type);
663          }
664        if (p != node->pn_head) {        if (p != node->pn_head) {
665          Stream_write_string(f, ", ");          Stream_write_string(f, ", ");
666        }        }
# Line 573  Line 701 
701    case TOK_STRING:    case TOK_STRING:
702      print_quoted_string_atom(node->pn_atom, f);      print_quoted_string_atom(node->pn_atom, f);
703      break;      break;
704    case TOK_OBJECT:    case TOK_REGEXP:
705      switch (node->pn_op) {      assert(node->pn_op == JSOP_REGEXP);
706      case JSOP_OBJECT:      {
707        /* I assume this is JSOP_REGEXP */        JSObject * object = node->pn_pob->object;
708        abort();        jsval result;
709        break;        js_regexp_toString(context, object, &result);
710      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;  
711      }      }
712      break;      break;
713    case TOK_NUMBER:    case TOK_NUMBER:
# Line 636  Line 753 
753      Stream_write_string(f, " in ");      Stream_write_string(f, " in ");
754      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
755      break;      break;
756    default:    case TOK_LEXICALSCOPE:
757      fatal("unsupported node type in file %s: %d", file_id, node->pn_type);      assert(node->pn_arity == PN_NAME);
758    }      assert(node->pn_expr->pn_type == TOK_LET);
759  }      assert(node->pn_expr->pn_arity == PN_BINARY);
760        Stream_write_string(f, "let(");
761  static void instrument_var_statement(JSParseNode * node, Stream * f, int indent) {      assert(node->pn_expr->pn_left->pn_type == TOK_LP);
762    assert(node->pn_arity == PN_LIST);      assert(node->pn_expr->pn_left->pn_arity == PN_LIST);
763    Stream_printf(f, "%*s", indent, "");      instrument_declarations(node->pn_expr->pn_left, f);
764    Stream_write_string(f, "var ");      Stream_write_string(f, ") ");
765    for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) {      instrument_expression(node->pn_expr->pn_right, f);
766      assert(p->pn_type == TOK_NAME);      break;
767      assert(p->pn_arity == PN_NAME);    case TOK_YIELD:
768      if (p != node->pn_head) {      assert(node->pn_arity == PN_UNARY);
769        Stream_write_string(f, ", ");      Stream_write_string(f, "yield");
770        if (node->pn_kid != NULL) {
771          Stream_write_char(f, ' ');
772          instrument_expression(node->pn_kid, f);
773      }      }
774      print_string_atom(p->pn_atom, f);      break;
775      if (p->pn_expr != NULL) {    case TOK_ARRAYCOMP:
776        Stream_write_string(f, " = ");      assert(node->pn_arity == PN_LIST);
777        instrument_expression(p->pn_expr, f);      {
778          JSParseNode * block_node;
779          switch (node->pn_count) {
780          case 1:
781            block_node = node->pn_head;
782            break;
783          case 2:
784            block_node = node->pn_head->pn_next;
785            break;
786          default:
787            abort();
788            break;
789          }
790          Stream_write_char(f, '[');
791          output_array_comprehension_or_generator_expression(block_node, f);
792          Stream_write_char(f, ']');
793      }      }
794        break;
795      case TOK_VAR:
796        assert(node->pn_arity == PN_LIST);
797        Stream_write_string(f, "var ");
798        instrument_declarations(node, f);
799        break;
800      case TOK_LET:
801        assert(node->pn_arity == PN_LIST);
802        Stream_write_string(f, "let ");
803        instrument_declarations(node, f);
804        break;
805      default:
806        fatal("unsupported node type in file %s: %d", file_id, node->pn_type);
807    }    }
808  }  }
809    
# Line 732  Line 880 
880      Stream_write_string(f, "switch (");      Stream_write_string(f, "switch (");
881      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
882      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
883      for (struct JSParseNode * p = node->pn_right->pn_head; p != NULL; p = p->pn_next) {      {
884        Stream_printf(f, "%*s", indent, "");        JSParseNode * list = node->pn_right;
885        switch (p->pn_type) {        if (list->pn_type == TOK_LEXICALSCOPE) {
886        case TOK_CASE:          list = list->pn_expr;
887          Stream_write_string(f, "case ");        }
888          instrument_expression(p->pn_left, f);        for (struct JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
889          Stream_write_string(f, ":\n");          Stream_printf(f, "%*s", indent, "");
890          break;          switch (p->pn_type) {
891        case TOK_DEFAULT:          case TOK_CASE:
892          Stream_write_string(f, "default:\n");            Stream_write_string(f, "case ");
893          break;            instrument_expression(p->pn_left, f);
894        default:            Stream_write_string(f, ":\n");
895          abort();            break;
896          break;          case TOK_DEFAULT:
897              Stream_write_string(f, "default:\n");
898              break;
899            default:
900              abort();
901              break;
902            }
903            instrument_statement(p->pn_right, f, indent + 2, false);
904        }        }
       instrument_statement(p->pn_right, f, indent + 2, false);  
905      }      }
906      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
907      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
# Line 779  Line 933 
933    case TOK_FOR:    case TOK_FOR:
934      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
935      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
     Stream_write_string(f, "for (");  
936      switch (node->pn_left->pn_type) {      switch (node->pn_left->pn_type) {
937      case TOK_IN:      case TOK_IN:
938        /* for/in */        /* for/in */
939        assert(node->pn_left->pn_arity == PN_BINARY);        assert(node->pn_left->pn_arity == PN_BINARY);
940        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);  
941        break;        break;
942      case TOK_RESERVED:      case TOK_RESERVED:
943        /* for (;;) */        /* for (;;) */
944        assert(node->pn_left->pn_arity == PN_TERNARY);        assert(node->pn_left->pn_arity == PN_TERNARY);
945          Stream_write_string(f, "for (");
946        if (node->pn_left->pn_kid1) {        if (node->pn_left->pn_kid1) {
947          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);  
         }  
948        }        }
949        Stream_write_string(f, ";");        Stream_write_string(f, ";");
950        if (node->pn_left->pn_kid2) {        if (node->pn_left->pn_kid2) {
# Line 826  Line 956 
956          Stream_write_char(f, ' ');          Stream_write_char(f, ' ');
957          instrument_expression(node->pn_left->pn_kid3, f);          instrument_expression(node->pn_left->pn_kid3, f);
958        }        }
959          Stream_write_char(f, ')');
960        break;        break;
961      default:      default:
962        abort();        abort();
963        break;        break;
964      }      }
965      Stream_write_string(f, ") {\n");      Stream_write_string(f, " {\n");
966      instrument_statement(node->pn_right, f, indent + 2, false);      instrument_statement(node->pn_right, f, indent + 2, false);
967      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
968      break;      break;
# Line 848  Line 979 
979      instrument_statement(node->pn_kid1, f, indent + 2, false);      instrument_statement(node->pn_kid1, f, indent + 2, false);
980      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
981      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
982      {      if (node->pn_kid2) {
983        for (JSParseNode * catch = node->pn_kid2; catch != NULL; catch = catch->pn_kid2) {        assert(node->pn_kid2->pn_type == TOK_RESERVED);
984          for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) {
985            assert(scope->pn_type == TOK_LEXICALSCOPE);
986            JSParseNode * catch = scope->pn_expr;
987          assert(catch->pn_type == TOK_CATCH);          assert(catch->pn_type == TOK_CATCH);
988          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
989          Stream_write_string(f, "catch (");          Stream_write_string(f, "catch (");
990            /* this may not be a name - destructuring assignment */
991            /*
992          assert(catch->pn_kid1->pn_arity == PN_NAME);          assert(catch->pn_kid1->pn_arity == PN_NAME);
993          print_string_atom(catch->pn_kid1->pn_atom, f);          print_string_atom(catch->pn_kid1->pn_atom, f);
994          if (catch->pn_kid1->pn_expr) {          */
995            instrument_expression(catch->pn_kid1, f);
996            if (catch->pn_kid2) {
997            Stream_write_string(f, " if ");            Stream_write_string(f, " if ");
998            instrument_expression(catch->pn_kid1->pn_expr, f);            instrument_expression(catch->pn_kid2, f);
999          }          }
1000          Stream_write_string(f, ") {\n");          Stream_write_string(f, ") {\n");
1001          instrument_statement(catch->pn_kid3, f, indent + 2, false);          instrument_statement(catch->pn_kid3, f, indent + 2, false);
# Line 899  Line 1037 
1037      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
1038      break;      break;
1039    case TOK_VAR:    case TOK_VAR:
1040      instrument_var_statement(node, f, indent);      Stream_printf(f, "%*s", indent, "");
1041        instrument_expression(node, f);
1042      Stream_write_string(f, ";\n");      Stream_write_string(f, ";\n");
1043      break;      break;
1044    case TOK_RETURN:    case TOK_RETURN:
# Line 934  Line 1073 
1073      */      */
1074      output_statement(node->pn_expr, f, indent, false);      output_statement(node->pn_expr, f, indent, false);
1075      break;      break;
1076      case TOK_LEXICALSCOPE:
1077        /* let statement */
1078        assert(node->pn_arity == PN_NAME);
1079        switch (node->pn_expr->pn_type) {
1080        case TOK_LET:
1081          /* let statement */
1082          assert(node->pn_expr->pn_arity == PN_BINARY);
1083          instrument_statement(node->pn_expr, f, indent, false);
1084          break;
1085        case TOK_LC:
1086          /* block */
1087          Stream_printf(f, "%*s", indent, "");
1088          Stream_write_string(f, "{\n");
1089          instrument_statement(node->pn_expr, f, indent + 2, false);
1090          Stream_printf(f, "%*s", indent, "");
1091          Stream_write_string(f, "}\n");
1092          break;
1093        case TOK_FOR:
1094          instrument_statement(node->pn_expr, f, indent, false);
1095          break;
1096        default:
1097          abort();
1098          break;
1099        }
1100        break;
1101      case TOK_LET:
1102        switch (node->pn_arity) {
1103        case PN_BINARY:
1104          /* let statement */
1105          Stream_printf(f, "%*s", indent, "");
1106          Stream_write_string(f, "let (");
1107          assert(node->pn_left->pn_type == TOK_LP);
1108          assert(node->pn_left->pn_arity == PN_LIST);
1109          instrument_declarations(node->pn_left, f);
1110          Stream_write_string(f, ") {\n");
1111          instrument_statement(node->pn_right, f, indent + 2, false);
1112          Stream_printf(f, "%*s", indent, "");
1113          Stream_write_string(f, "}\n");
1114          break;
1115        case PN_LIST:
1116          /* let definition */
1117          Stream_printf(f, "%*s", indent, "");
1118          instrument_expression(node, f);
1119          Stream_write_string(f, ";\n");
1120          break;
1121        default:
1122          abort();
1123          break;
1124        }
1125        break;
1126      case TOK_DEBUGGER:
1127        Stream_printf(f, "%*s", indent, "");
1128        Stream_write_string(f, "debugger;\n");
1129        break;
1130    default:    default:
1131      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);
1132    }    }
# Line 945  Line 1138 
1138  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
1139  */  */
1140  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) {
1141    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC && node->pn_type != TOK_LEXICALSCOPE) {
1142      uint16_t line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
1143      if (line > num_lines) {      if (line > num_lines) {
1144        fatal("%s: script contains more than 65,535 lines", file_id);        fatal("%s: script contains more than 65,535 lines", file_id);
# Line 1002  Line 1195 
1195  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) {
1196    file_id = id;    file_id = id;
1197    
1198    /* scan the javascript */    /* parse the javascript */
1199    JSTokenStream * token_stream = js_NewTokenStream(context, characters, num_characters, NULL, 1, NULL);    JSParseContext parse_context;
1200    if (token_stream == NULL) {    if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) {
1201      fatal("cannot create token stream from file: %s", file_id);      fatal("cannot create token stream from file: %s", file_id);
1202    }    }
   
   /* parse the javascript */  
1203    JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);    JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);
1204    JSParseNode * node = js_ParseTokenStream(context, global, token_stream);    JSParseNode * node = js_ParseScript(context, global, &parse_context);
1205    if (node == NULL) {    if (node == NULL) {
1206      js_ReportUncaughtException(context);      js_ReportUncaughtException(context);
1207      fatal("parse error in file: %s", file_id);      fatal("parse error in file: %s", file_id);
# Line 1097  Line 1288 
1288    
1289    Stream * instrumented = Stream_new(0);    Stream * instrumented = Stream_new(0);
1290    instrument_statement(node, instrumented, 0, false);    instrument_statement(node, instrumented, 0, false);
1291      js_FinishParseContext(context, &parse_context);
1292    
1293    /* write line number info to the output */    /* write line number info to the output */
1294    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 1177  Line 1369 
1369            /* line feed (new line) */            /* line feed (new line) */
1370            done = true;            done = true;
1371            break;            break;
1372            /* IE doesn't support this */
1373            /*
1374          case 0xb:          case 0xb:
           /* vertical tab */  
1375            Stream_write_string(output, "\\v");            Stream_write_string(output, "\\v");
1376            break;            break;
1377            */
1378          case 0xc:          case 0xc:
1379            /* form feed */            /* form feed */
1380            Stream_write_string(output, "\\f");            Stream_write_string(output, "\\f");
# Line 1235  Line 1429 
1429            /* line feed (new line) */            /* line feed (new line) */
1430            done = true;            done = true;
1431            break;            break;
1432            /* IE doesn't support this */
1433            /*
1434          case 0xb:          case 0xb:
           /* vertical tab */  
1435            Stream_write_string(output, "\\v");            Stream_write_string(output, "\\v");
1436            break;            break;
1437            */
1438          case 0xc:          case 0xc:
1439            /* form feed */            /* form feed */
1440            Stream_write_string(output, "\\f");            Stream_write_string(output, "\\f");
# Line 1365  Line 1561 
1561  }  }
1562    
1563  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) {
1564      int result = 0;
1565    
1566    jschar * base = js_InflateString(context, (char *) json, &length);    jschar * base = js_InflateString(context, (char *) json, &length);
1567    if (base == NULL) {    if (base == NULL) {
1568      fatal("out of memory");      fatal("out of memory");
# Line 1377  Line 1575 
1575    
1576    JS_free(context, base);    JS_free(context, base);
1577    
1578    JSTokenStream * token_stream = js_NewTokenStream(context, parenthesized_json, length + 2, NULL, 1, NULL);    JSParseContext parse_context;
1579    if (token_stream == NULL) {    if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) {
1580      fatal("cannot create token stream");      free(parenthesized_json);
1581        return -1;
1582    }    }
1583      JSParseNode * root = js_ParseScript(context, global, &parse_context);
   JSParseNode * root = js_ParseTokenStream(context, global, token_stream);  
1584    free(parenthesized_json);    free(parenthesized_json);
1585    if (root == NULL) {    if (root == NULL) {
1586      return -1;      result = -1;
1587        goto done;
1588    }    }
1589    
1590    /* root node must be TOK_LC */    /* root node must be TOK_LC */
1591    if (root->pn_type != TOK_LC) {    if (root->pn_type != TOK_LC) {
1592      return -1;      result = -1;
1593        goto done;
1594    }    }
1595    JSParseNode * semi = root->pn_u.list.head;    JSParseNode * semi = root->pn_u.list.head;
1596    
1597    /* 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 */
1598    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {
1599      return -1;      result = -1;
1600        goto done;
1601    }    }
1602    JSParseNode * parenthesized = semi->pn_kid;    JSParseNode * parenthesized = semi->pn_kid;
1603    
1604    /* this must be a parenthesized expression */    /* this must be a parenthesized expression */
1605    if (parenthesized->pn_type != TOK_RP) {    if (parenthesized->pn_type != TOK_RP) {
1606      return -1;      result = -1;
1607        goto done;
1608    }    }
1609    JSParseNode * object = parenthesized->pn_kid;    JSParseNode * object = parenthesized->pn_kid;
1610    
1611    /* this must be an object literal */    /* this must be an object literal */
1612    if (object->pn_type != TOK_RC) {    if (object->pn_type != TOK_RC) {
1613      return -1;      result = -1;
1614        goto done;
1615    }    }
1616    
1617    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {
1618      /* every element of this list must be TOK_COLON */      /* every element of this list must be TOK_COLON */
1619      if (p->pn_type != TOK_COLON) {      if (p->pn_type != TOK_COLON) {
1620        return -1;        result = -1;
1621          goto done;
1622      }      }
1623    
1624      /* the key must be a string representing the file */      /* the key must be a string representing the file */
1625      JSParseNode * key = p->pn_left;      JSParseNode * key = p->pn_left;
1626      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {
1627        return -1;        result = -1;
1628          goto done;
1629      }      }
1630      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));
1631    
1632      /* the value must be an object literal OR an array */      /* the value must be an object literal OR an array */
1633      JSParseNode * value = p->pn_right;      JSParseNode * value = p->pn_right;
1634      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {
1635        return -1;        result = -1;
1636          goto done;
1637      }      }
1638    
1639      JSParseNode * array = NULL;      JSParseNode * array = NULL;
# Line 1439  Line 1645 
1645      else if (value->pn_type == TOK_RC) {      else if (value->pn_type == TOK_RC) {
1646        /* an object literal */        /* an object literal */
1647        if (value->pn_count != 2) {        if (value->pn_count != 2) {
1648          return -1;          result = -1;
1649            goto done;
1650        }        }
1651        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {
1652          if (element->pn_type != TOK_COLON) {          if (element->pn_type != TOK_COLON) {
1653            return -1;            result = -1;
1654              goto done;
1655          }          }
1656          JSParseNode * left = element->pn_left;          JSParseNode * left = element->pn_left;
1657          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {
1658            return -1;            result = -1;
1659              goto done;
1660          }          }
1661          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));
1662          if (strcmp(s, "coverage") == 0) {          if (strcmp(s, "coverage") == 0) {
1663            array = element->pn_right;            array = element->pn_right;
1664            if (array->pn_type != TOK_RB) {            if (array->pn_type != TOK_RB) {
1665              return -1;              result = -1;
1666                goto done;
1667            }            }
1668          }          }
1669          else if (strcmp(s, "source") == 0) {          else if (strcmp(s, "source") == 0) {
1670            source = element->pn_right;            source = element->pn_right;
1671            if (source->pn_type != TOK_RB) {            if (source->pn_type != TOK_RB) {
1672              return -1;              result = -1;
1673                goto done;
1674            }            }
1675          }          }
1676          else {          else {
1677            return -1;            result = -1;
1678              goto done;
1679          }          }
1680        }        }
1681      }      }
1682      else {      else {
1683        return -1;        result = -1;
1684          goto done;
1685      }      }
1686    
1687      if (array == NULL) {      if (array == NULL) {
1688        return -1;        result = -1;
1689          goto done;
1690      }      }
1691    
1692      /* look up the file in the coverage table */      /* look up the file in the coverage table */
# Line 1496  Line 1710 
1710            file_coverage->coverage_lines[i] = -1;            file_coverage->coverage_lines[i] = -1;
1711          }          }
1712          else {          else {
1713            return -1;            result = -1;
1714              goto done;
1715          }          }
1716        }        }
1717        assert(i == array->pn_count);        assert(i == array->pn_count);
# Line 1512  Line 1727 
1727        /* sanity check */        /* sanity check */
1728        assert(strcmp(file_coverage->id, id_bytes) == 0);        assert(strcmp(file_coverage->id, id_bytes) == 0);
1729        if (file_coverage->num_coverage_lines != array->pn_count) {        if (file_coverage->num_coverage_lines != array->pn_count) {
1730          return -2;          result = -2;
1731            goto done;
1732        }        }
1733    
1734        /* merge the coverage */        /* merge the coverage */
# Line 1520  Line 1736 
1736        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++) {
1737          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1738            if (file_coverage->coverage_lines[i] == -1) {            if (file_coverage->coverage_lines[i] == -1) {
1739              return -2;              result = -2;
1740                goto done;
1741            }            }
1742            file_coverage->coverage_lines[i] += (int) element->pn_dval;            file_coverage->coverage_lines[i] += (int) element->pn_dval;
1743          }          }
1744          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1745            if (file_coverage->coverage_lines[i] != -1) {            if (file_coverage->coverage_lines[i] != -1) {
1746              return -2;              result = -2;
1747                goto done;
1748            }            }
1749          }          }
1750          else {          else {
1751            return -1;            result = -1;
1752              goto done;
1753          }          }
1754        }        }
1755        assert(i == array->pn_count);        assert(i == array->pn_count);
# Line 1543  Line 1762 
1762        uint32 i = 0;        uint32 i = 0;
1763        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++) {
1764          if (element->pn_type != TOK_STRING) {          if (element->pn_type != TOK_STRING) {
1765            return -1;            result = -1;
1766              goto done;
1767          }          }
1768          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)));
1769        }        }
# Line 1551  Line 1771 
1771      }      }
1772    }    }
1773    
1774    return 0;  done:
1775      js_FinishParseContext(context, &parse_context);
1776      return result;
1777  }  }

Legend:
Removed from v.293  
changed lines
  Added in v.357

  ViewVC Help
Powered by ViewVC 1.1.24