/[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 317 by siliconforks, Tue Oct 14 01:28:46 2008 UTC revision 351 by siliconforks, Fri Oct 24 16:17:59 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 199  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 209  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 244  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 269  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);      print_string_atom(param, f);
     }  
319    }    }
320      JS_FinishArenaPool(&pool);
321    Stream_write_string(f, ") {\n");    Stream_write_string(f, ") {\n");
   free(params);  
322    
323    /* function body */    /* function body */
324    instrument_statement(node->pn_body, f, indent + 2, false);    if (function->flags & JSFUN_EXPR_CLOSURE) {
325        /* expression closure */
326        output_statement(node->pn_body, f, indent + 2, false);
327      }
328      else {
329        instrument_statement(node->pn_body, f, indent + 2, false);
330      }
331    
332    Stream_write_string(f, "}\n");    Stream_write_string(f, "}\n");
333  }  }
334    
335  static void instrument_function_call(JSParseNode * node, Stream * f) {  static void instrument_function_call(JSParseNode * node, Stream * f) {
336    instrument_expression(node->pn_head, f);    JSParseNode * function_node = node->pn_head;
337      if (function_node->pn_type == TOK_FUNCTION) {
338        JSObject * object = function_node->pn_funpob->object;
339        assert(JS_ObjectIsFunction(context, object));
340        JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
341        assert(function);
342        assert(object == &function->object);
343    
344        if (function_node->pn_flags & TCF_GENEXP_LAMBDA) {
345          /* it's a generator expression */
346          JSParseNode * lexical_scope_node = function_node->pn_body;
347          assert(lexical_scope_node->pn_type == TOK_LEXICALSCOPE);
348          assert(lexical_scope_node->pn_arity == PN_NAME);
349          JSParseNode * for_node = lexical_scope_node->pn_body;
350          assert(for_node->pn_type == TOK_FOR);
351          assert(for_node->pn_arity == PN_BINARY);
352          JSParseNode * if_node = NULL;
353          JSParseNode * semi_node;
354          switch (for_node->pn_right->pn_type) {
355          case TOK_SEMI:
356            semi_node = for_node->pn_right;
357            break;
358          case TOK_IF:
359            if_node = for_node->pn_right;
360            assert(if_node->pn_arity == PN_TERNARY);
361            semi_node = if_node->pn_kid2;
362            assert(semi_node->pn_type == TOK_SEMI);
363            break;
364          default:
365            abort();
366            break;
367          }
368          assert(semi_node->pn_arity == PN_UNARY);
369          JSParseNode * yield_node = semi_node->pn_kid;
370          assert(yield_node->pn_type == TOK_YIELD);
371          Stream_write_char(f, '(');
372          instrument_expression(yield_node->pn_kid, f);
373          Stream_write_char(f, ' ');
374          output_for_in(for_node, f);
375          if (if_node) {
376            Stream_write_string(f, " if (");
377            instrument_expression(if_node->pn_kid1, f);
378            Stream_write_char(f, ')');
379          }
380          Stream_write_char(f, ')');
381          return;
382        }
383        else {
384          Stream_write_char(f, '(');
385          instrument_expression(function_node, f);
386          Stream_write_char(f, ')');
387        }
388      }
389      else {
390        instrument_expression(function_node, f);
391      }
392    Stream_write_char(f, '(');    Stream_write_char(f, '(');
393    for (struct JSParseNode * p = node->pn_head->pn_next; p != NULL; p = p->pn_next) {    for (struct JSParseNode * p = function_node->pn_next; p != NULL; p = p->pn_next) {
394      if (p != node->pn_head->pn_next) {      if (p != node->pn_head->pn_next) {
395        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
396      }      }
# Line 316  Line 399 
399    Stream_write_char(f, ')');    Stream_write_char(f, ')');
400  }  }
401    
402    static void instrument_declarations(JSParseNode * list, Stream * f) {
403      assert(list->pn_arity == PN_LIST);
404      for (JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
405        switch (p->pn_type) {
406        case TOK_NAME:
407          assert(p->pn_arity == PN_NAME);
408          if (p != list->pn_head) {
409            Stream_write_string(f, ", ");
410          }
411          print_string_atom(p->pn_atom, f);
412          if (p->pn_expr != NULL) {
413            Stream_write_string(f, " = ");
414            instrument_expression(p->pn_expr, f);
415          }
416          break;
417        case TOK_ASSIGN:
418        case TOK_RB:
419        case TOK_RC:
420          /* destructuring */
421          instrument_expression(p, f);
422          break;
423        default:
424          abort();
425          break;
426        }
427      }
428    }
429    
430  /*  /*
431  See <Expressions> in jsparse.h.  See <Expressions> in jsparse.h.
432  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
# Line 374  Line 485 
485      instrument_expression(node->pn_kid3, f);      instrument_expression(node->pn_kid3, f);
486      break;      break;
487    case TOK_OR:    case TOK_OR:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " || ");  
     instrument_expression(node->pn_right, f);  
     break;  
488    case TOK_AND:    case TOK_AND:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " && ");  
     instrument_expression(node->pn_right, f);  
     break;  
489    case TOK_BITOR:    case TOK_BITOR:
490    case TOK_BITXOR:    case TOK_BITXOR:
491    case TOK_BITAND:    case TOK_BITAND:
# Line 495  Line 598 
598      assert(ATOM_IS_STRING(node->pn_atom));      assert(ATOM_IS_STRING(node->pn_atom));
599      {      {
600        JSString * s = ATOM_TO_STRING(node->pn_atom);        JSString * s = ATOM_TO_STRING(node->pn_atom);
601        /* XXX - semantics changed in 1.7 */        bool must_quote;
602        if (! ATOM_KEYWORD(node->pn_atom) && js_IsIdentifier(s)) {        if (JSSTRING_LENGTH(s) == 0) {
603          Stream_write_char(f, '.');          must_quote = true;
604          print_string_atom(node->pn_atom, f);        }
605          else if (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF) {
606            must_quote = true;
607          }
608          else if (! js_IsIdentifier(s)) {
609            must_quote = true;
610        }        }
611        else {        else {
612            must_quote = false;
613          }
614          if (must_quote) {
615          Stream_write_char(f, '[');          Stream_write_char(f, '[');
616          print_quoted_string_atom(node->pn_atom, f);          print_quoted_string_atom(node->pn_atom, f);
617          Stream_write_char(f, ']');          Stream_write_char(f, ']');
618        }        }
619          else {
620            Stream_write_char(f, '.');
621            print_string_atom(node->pn_atom, f);
622          }
623      }      }
624      break;      break;
625    case TOK_LB:    case TOK_LB:
# Line 576  Line 691 
691    case TOK_STRING:    case TOK_STRING:
692      print_quoted_string_atom(node->pn_atom, f);      print_quoted_string_atom(node->pn_atom, f);
693      break;      break;
694    case TOK_OBJECT:    case TOK_REGEXP:
695      switch (node->pn_op) {      assert(node->pn_op == JSOP_REGEXP);
696      case JSOP_OBJECT:      {
697        /* I assume this is JSOP_REGEXP */        JSObject * object = node->pn_pob->object;
698        abort();        jsval result;
699        break;        js_regexp_toString(context, object, &result);
700      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;  
701      }      }
702      break;      break;
703    case TOK_NUMBER:    case TOK_NUMBER:
# Line 639  Line 743 
743      Stream_write_string(f, " in ");      Stream_write_string(f, " in ");
744      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
745      break;      break;
746      case TOK_LEXICALSCOPE:
747        assert(node->pn_arity == PN_NAME);
748        assert(node->pn_expr->pn_type == TOK_LET);
749        assert(node->pn_expr->pn_arity == PN_BINARY);
750        Stream_write_string(f, "let(");
751        assert(node->pn_expr->pn_left->pn_type == TOK_LP);
752        assert(node->pn_expr->pn_left->pn_arity == PN_LIST);
753        instrument_declarations(node->pn_expr->pn_left, f);
754        Stream_write_string(f, ") ");
755        instrument_expression(node->pn_expr->pn_right, f);
756        break;
757      case TOK_YIELD:
758        assert(node->pn_arity == PN_UNARY);
759        Stream_write_string(f, "yield ");
760        instrument_expression(node->pn_kid, f);
761        break;
762      case TOK_ARRAYCOMP:
763        assert(node->pn_arity == PN_LIST);
764        {
765          JSParseNode * block_node;
766          switch (node->pn_count) {
767          case 1:
768            block_node = node->pn_head;
769            break;
770          case 2:
771            block_node = node->pn_head->pn_next;
772            break;
773          default:
774            abort();
775            break;
776          }
777          assert(block_node->pn_type == TOK_LEXICALSCOPE);
778          assert(block_node->pn_arity == PN_NAME);
779          JSParseNode * for_node = block_node->pn_expr;
780          assert(for_node->pn_type == TOK_FOR);
781          assert(for_node->pn_arity == PN_BINARY);
782          JSParseNode * if_node = NULL;
783          JSParseNode * push_node;
784          switch (for_node->pn_right->pn_type) {
785          case TOK_ARRAYPUSH:
786            push_node = for_node->pn_right;
787            assert(push_node->pn_arity == PN_UNARY);
788            break;
789          case TOK_IF:
790            if_node = for_node->pn_right;
791            assert(if_node->pn_arity == PN_TERNARY);
792            push_node = if_node->pn_kid2;
793            break;
794          default:
795            abort();
796            break;
797          }
798          Stream_write_char(f, '[');
799          instrument_expression(push_node->pn_kid, f);
800          Stream_write_char(f, ' ');
801          output_for_in(for_node, f);
802          if (if_node) {
803            Stream_write_string(f, " if (");
804            instrument_expression(if_node->pn_kid1, f);
805            Stream_write_char(f, ')');
806          }
807          Stream_write_char(f, ']');
808        }
809        break;
810      case TOK_VAR:
811        assert(node->pn_arity == PN_LIST);
812        Stream_write_string(f, "var ");
813        instrument_declarations(node, f);
814        break;
815      case TOK_LET:
816        assert(node->pn_arity == PN_LIST);
817        Stream_write_string(f, "let ");
818        instrument_declarations(node, f);
819        break;
820    default:    default:
821      fatal("unsupported node type in file %s: %d", file_id, node->pn_type);      fatal("unsupported node type in file %s: %d", file_id, node->pn_type);
822    }    }
823  }  }
824    
 static void instrument_var_statement(JSParseNode * node, Stream * f, int indent) {  
   assert(node->pn_arity == PN_LIST);  
   Stream_printf(f, "%*s", indent, "");  
   Stream_write_string(f, "var ");  
   for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) {  
     assert(p->pn_type == TOK_NAME);  
     assert(p->pn_arity == PN_NAME);  
     if (p != node->pn_head) {  
       Stream_write_string(f, ", ");  
     }  
     print_string_atom(p->pn_atom, f);  
     if (p->pn_expr != NULL) {  
       Stream_write_string(f, " = ");  
       instrument_expression(p->pn_expr, f);  
     }  
   }  
 }  
   
825  static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {  static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
826    switch (node->pn_type) {    switch (node->pn_type) {
827    case TOK_FUNCTION:    case TOK_FUNCTION:
# Line 735  Line 895 
895      Stream_write_string(f, "switch (");      Stream_write_string(f, "switch (");
896      instrument_expression(node->pn_left, f);      instrument_expression(node->pn_left, f);
897      Stream_write_string(f, ") {\n");      Stream_write_string(f, ") {\n");
898      for (struct JSParseNode * p = node->pn_right->pn_head; p != NULL; p = p->pn_next) {      {
899        Stream_printf(f, "%*s", indent, "");        JSParseNode * list = node->pn_right;
900        switch (p->pn_type) {        if (list->pn_type == TOK_LEXICALSCOPE) {
901        case TOK_CASE:          list = list->pn_expr;
902          Stream_write_string(f, "case ");        }
903          instrument_expression(p->pn_left, f);        for (struct JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
904          Stream_write_string(f, ":\n");          Stream_printf(f, "%*s", indent, "");
905          break;          switch (p->pn_type) {
906        case TOK_DEFAULT:          case TOK_CASE:
907          Stream_write_string(f, "default:\n");            Stream_write_string(f, "case ");
908          break;            instrument_expression(p->pn_left, f);
909        default:            Stream_write_string(f, ":\n");
910          abort();            break;
911          break;          case TOK_DEFAULT:
912              Stream_write_string(f, "default:\n");
913              break;
914            default:
915              abort();
916              break;
917            }
918            instrument_statement(p->pn_right, f, indent + 2, false);
919        }        }
       instrument_statement(p->pn_right, f, indent + 2, false);  
920      }      }
921      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
922      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
# Line 782  Line 948 
948    case TOK_FOR:    case TOK_FOR:
949      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
950      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
     Stream_write_string(f, "for (");  
951      switch (node->pn_left->pn_type) {      switch (node->pn_left->pn_type) {
952      case TOK_IN:      case TOK_IN:
953        /* for/in */        /* for/in */
954        assert(node->pn_left->pn_arity == PN_BINARY);        assert(node->pn_left->pn_arity == PN_BINARY);
955        switch (node->pn_left->pn_left->pn_type) {        output_for_in(node, f);
       case TOK_VAR:  
         instrument_var_statement(node->pn_left->pn_left, f, 0);  
         break;  
       case TOK_NAME:  
         instrument_expression(node->pn_left->pn_left, f);  
         break;  
       default:  
         /* this is undocumented: for (x.value in y) */  
         instrument_expression(node->pn_left->pn_left, f);  
         break;  
 /*  
       default:  
         fprintf(stderr, "unexpected node type: %d\n", node->pn_left->pn_left->pn_type);  
         abort();  
         break;  
 */  
       }  
       Stream_write_string(f, " in ");  
       instrument_expression(node->pn_left->pn_right, f);  
956        break;        break;
957      case TOK_RESERVED:      case TOK_RESERVED:
958        /* for (;;) */        /* for (;;) */
959        assert(node->pn_left->pn_arity == PN_TERNARY);        assert(node->pn_left->pn_arity == PN_TERNARY);
960          Stream_write_string(f, "for (");
961        if (node->pn_left->pn_kid1) {        if (node->pn_left->pn_kid1) {
962          if (node->pn_left->pn_kid1->pn_type == TOK_VAR) {          instrument_expression(node->pn_left->pn_kid1, f);
           instrument_var_statement(node->pn_left->pn_kid1, f, 0);  
         }  
         else {  
           instrument_expression(node->pn_left->pn_kid1, f);  
         }  
963        }        }
964        Stream_write_string(f, ";");        Stream_write_string(f, ";");
965        if (node->pn_left->pn_kid2) {        if (node->pn_left->pn_kid2) {
# Line 829  Line 971 
971          Stream_write_char(f, ' ');          Stream_write_char(f, ' ');
972          instrument_expression(node->pn_left->pn_kid3, f);          instrument_expression(node->pn_left->pn_kid3, f);
973        }        }
974          Stream_write_char(f, ')');
975        break;        break;
976      default:      default:
977        abort();        abort();
978        break;        break;
979      }      }
980      Stream_write_string(f, ") {\n");      Stream_write_string(f, " {\n");
981      instrument_statement(node->pn_right, f, indent + 2, false);      instrument_statement(node->pn_right, f, indent + 2, false);
982      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
983      break;      break;
# Line 851  Line 994 
994      instrument_statement(node->pn_kid1, f, indent + 2, false);      instrument_statement(node->pn_kid1, f, indent + 2, false);
995      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
996      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
997      {      if (node->pn_kid2) {
998        for (JSParseNode * catch = node->pn_kid2; catch != NULL; catch = catch->pn_kid2) {        assert(node->pn_kid2->pn_type == TOK_RESERVED);
999          for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) {
1000            assert(scope->pn_type == TOK_LEXICALSCOPE);
1001            JSParseNode * catch = scope->pn_expr;
1002          assert(catch->pn_type == TOK_CATCH);          assert(catch->pn_type == TOK_CATCH);
1003          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
1004          Stream_write_string(f, "catch (");          Stream_write_string(f, "catch (");
1005            /* this may not be a name - destructuring assignment */
1006            /*
1007          assert(catch->pn_kid1->pn_arity == PN_NAME);          assert(catch->pn_kid1->pn_arity == PN_NAME);
1008          print_string_atom(catch->pn_kid1->pn_atom, f);          print_string_atom(catch->pn_kid1->pn_atom, f);
1009          if (catch->pn_kid1->pn_expr) {          */
1010            instrument_expression(catch->pn_kid1, f);
1011            if (catch->pn_kid2) {
1012            Stream_write_string(f, " if ");            Stream_write_string(f, " if ");
1013            instrument_expression(catch->pn_kid1->pn_expr, f);            instrument_expression(catch->pn_kid2, f);
1014          }          }
1015          Stream_write_string(f, ") {\n");          Stream_write_string(f, ") {\n");
1016          instrument_statement(catch->pn_kid3, f, indent + 2, false);          instrument_statement(catch->pn_kid3, f, indent + 2, false);
# Line 902  Line 1052 
1052      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
1053      break;      break;
1054    case TOK_VAR:    case TOK_VAR:
1055      instrument_var_statement(node, f, indent);      Stream_printf(f, "%*s", indent, "");
1056        instrument_expression(node, f);
1057      Stream_write_string(f, ";\n");      Stream_write_string(f, ";\n");
1058      break;      break;
1059    case TOK_RETURN:    case TOK_RETURN:
# Line 937  Line 1088 
1088      */      */
1089      output_statement(node->pn_expr, f, indent, false);      output_statement(node->pn_expr, f, indent, false);
1090      break;      break;
1091      case TOK_LEXICALSCOPE:
1092        /* let statement */
1093        assert(node->pn_arity == PN_NAME);
1094        switch (node->pn_expr->pn_type) {
1095        case TOK_LET:
1096          /* let statement */
1097          assert(node->pn_expr->pn_arity == PN_BINARY);
1098          instrument_statement(node->pn_expr, f, indent, false);
1099          break;
1100        case TOK_LC:
1101          /* block */
1102          Stream_printf(f, "%*s", indent, "");
1103          Stream_write_string(f, "{\n");
1104          instrument_statement(node->pn_expr, f, indent + 2, false);
1105          Stream_printf(f, "%*s", indent, "");
1106          Stream_write_string(f, "}\n");
1107          break;
1108        case TOK_FOR:
1109          instrument_statement(node->pn_expr, f, indent, false);
1110          break;
1111        default:
1112          abort();
1113          break;
1114        }
1115        break;
1116      case TOK_LET:
1117        switch (node->pn_arity) {
1118        case PN_BINARY:
1119          /* let statement */
1120          Stream_printf(f, "%*s", indent, "");
1121          Stream_write_string(f, "let (");
1122          assert(node->pn_left->pn_type == TOK_LP);
1123          assert(node->pn_left->pn_arity == PN_LIST);
1124          instrument_declarations(node->pn_left, f);
1125          Stream_write_string(f, ") {\n");
1126          instrument_statement(node->pn_right, f, indent + 2, false);
1127          Stream_printf(f, "%*s", indent, "");
1128          Stream_write_string(f, "}\n");
1129          break;
1130        case PN_LIST:
1131          /* let definition */
1132          Stream_printf(f, "%*s", indent, "");
1133          instrument_expression(node, f);
1134          Stream_write_string(f, ";\n");
1135          break;
1136        default:
1137          abort();
1138          break;
1139        }
1140        break;
1141      case TOK_DEBUGGER:
1142        Stream_printf(f, "%*s", indent, "");
1143        Stream_write_string(f, "debugger;\n");
1144        break;
1145    default:    default:
1146      fatal("unsupported node type in file %s: %d", file_id, node->pn_type);      fatal("unsupported node type in file %s: %d", file_id, node->pn_type);
1147    }    }
# Line 948  Line 1153 
1153  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
1154  */  */
1155  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {  static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
1156    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC && node->pn_type != TOK_LEXICALSCOPE) {
1157      uint16_t line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
1158      if (line > num_lines) {      if (line > num_lines) {
1159        fatal("%s: script contains more than 65,535 lines", file_id);        fatal("%s: script contains more than 65,535 lines", file_id);
# Line 1005  Line 1210 
1210  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) {
1211    file_id = id;    file_id = id;
1212    
1213    /* scan the javascript */    /* parse the javascript */
1214    JSTokenStream * token_stream = js_NewTokenStream(context, characters, num_characters, NULL, 1, NULL);    JSParseContext parse_context;
1215    if (token_stream == NULL) {    if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) {
1216      fatal("cannot create token stream from file: %s", file_id);      fatal("cannot create token stream from file: %s", file_id);
1217    }    }
   
   /* parse the javascript */  
1218    JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);    JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);
1219    JSParseNode * node = js_ParseTokenStream(context, global, token_stream);    JSParseNode * node = js_ParseScript(context, global, &parse_context);
1220    if (node == NULL) {    if (node == NULL) {
1221      js_ReportUncaughtException(context);      js_ReportUncaughtException(context);
1222      fatal("parse error in file: %s", file_id);      fatal("parse error in file: %s", file_id);
# Line 1100  Line 1303 
1303    
1304    Stream * instrumented = Stream_new(0);    Stream * instrumented = Stream_new(0);
1305    instrument_statement(node, instrumented, 0, false);    instrument_statement(node, instrumented, 0, false);
1306      js_FinishParseContext(context, &parse_context);
1307    
1308    /* write line number info to the output */    /* write line number info to the output */
1309    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 1372  Line 1576 
1576  }  }
1577    
1578  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) {
1579      int result = 0;
1580    
1581    jschar * base = js_InflateString(context, (char *) json, &length);    jschar * base = js_InflateString(context, (char *) json, &length);
1582    if (base == NULL) {    if (base == NULL) {
1583      fatal("out of memory");      fatal("out of memory");
# Line 1384  Line 1590 
1590    
1591    JS_free(context, base);    JS_free(context, base);
1592    
1593    JSTokenStream * token_stream = js_NewTokenStream(context, parenthesized_json, length + 2, NULL, 1, NULL);    JSParseContext parse_context;
1594    if (token_stream == NULL) {    if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) {
1595      fatal("cannot create token stream");      free(parenthesized_json);
1596        return -1;
1597    }    }
1598      JSParseNode * root = js_ParseScript(context, global, &parse_context);
   JSParseNode * root = js_ParseTokenStream(context, global, token_stream);  
1599    free(parenthesized_json);    free(parenthesized_json);
1600    if (root == NULL) {    if (root == NULL) {
1601      return -1;      result = -1;
1602        goto done;
1603    }    }
1604    
1605    /* root node must be TOK_LC */    /* root node must be TOK_LC */
1606    if (root->pn_type != TOK_LC) {    if (root->pn_type != TOK_LC) {
1607      return -1;      result = -1;
1608        goto done;
1609    }    }
1610    JSParseNode * semi = root->pn_u.list.head;    JSParseNode * semi = root->pn_u.list.head;
1611    
1612    /* 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 */
1613    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {
1614      return -1;      result = -1;
1615        goto done;
1616    }    }
1617    JSParseNode * parenthesized = semi->pn_kid;    JSParseNode * parenthesized = semi->pn_kid;
1618    
1619    /* this must be a parenthesized expression */    /* this must be a parenthesized expression */
1620    if (parenthesized->pn_type != TOK_RP) {    if (parenthesized->pn_type != TOK_RP) {
1621      return -1;      result = -1;
1622        goto done;
1623    }    }
1624    JSParseNode * object = parenthesized->pn_kid;    JSParseNode * object = parenthesized->pn_kid;
1625    
1626    /* this must be an object literal */    /* this must be an object literal */
1627    if (object->pn_type != TOK_RC) {    if (object->pn_type != TOK_RC) {
1628      return -1;      result = -1;
1629        goto done;
1630    }    }
1631    
1632    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {
1633      /* every element of this list must be TOK_COLON */      /* every element of this list must be TOK_COLON */
1634      if (p->pn_type != TOK_COLON) {      if (p->pn_type != TOK_COLON) {
1635        return -1;        result = -1;
1636          goto done;
1637      }      }
1638    
1639      /* the key must be a string representing the file */      /* the key must be a string representing the file */
1640      JSParseNode * key = p->pn_left;      JSParseNode * key = p->pn_left;
1641      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {
1642        return -1;        result = -1;
1643          goto done;
1644      }      }
1645      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));
1646    
1647      /* the value must be an object literal OR an array */      /* the value must be an object literal OR an array */
1648      JSParseNode * value = p->pn_right;      JSParseNode * value = p->pn_right;
1649      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {
1650        return -1;        result = -1;
1651          goto done;
1652      }      }
1653    
1654      JSParseNode * array = NULL;      JSParseNode * array = NULL;
# Line 1446  Line 1660 
1660      else if (value->pn_type == TOK_RC) {      else if (value->pn_type == TOK_RC) {
1661        /* an object literal */        /* an object literal */
1662        if (value->pn_count != 2) {        if (value->pn_count != 2) {
1663          return -1;          result = -1;
1664            goto done;
1665        }        }
1666        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {
1667          if (element->pn_type != TOK_COLON) {          if (element->pn_type != TOK_COLON) {
1668            return -1;            result = -1;
1669              goto done;
1670          }          }
1671          JSParseNode * left = element->pn_left;          JSParseNode * left = element->pn_left;
1672          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {
1673            return -1;            result = -1;
1674              goto done;
1675          }          }
1676          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));
1677          if (strcmp(s, "coverage") == 0) {          if (strcmp(s, "coverage") == 0) {
1678            array = element->pn_right;            array = element->pn_right;
1679            if (array->pn_type != TOK_RB) {            if (array->pn_type != TOK_RB) {
1680              return -1;              result = -1;
1681                goto done;
1682            }            }
1683          }          }
1684          else if (strcmp(s, "source") == 0) {          else if (strcmp(s, "source") == 0) {
1685            source = element->pn_right;            source = element->pn_right;
1686            if (source->pn_type != TOK_RB) {            if (source->pn_type != TOK_RB) {
1687              return -1;              result = -1;
1688                goto done;
1689            }            }
1690          }          }
1691          else {          else {
1692            return -1;            result = -1;
1693              goto done;
1694          }          }
1695        }        }
1696      }      }
1697      else {      else {
1698        return -1;        result = -1;
1699          goto done;
1700      }      }
1701    
1702      if (array == NULL) {      if (array == NULL) {
1703        return -1;        result = -1;
1704          goto done;
1705      }      }
1706    
1707      /* look up the file in the coverage table */      /* look up the file in the coverage table */
# Line 1503  Line 1725 
1725            file_coverage->coverage_lines[i] = -1;            file_coverage->coverage_lines[i] = -1;
1726          }          }
1727          else {          else {
1728            return -1;            result = -1;
1729              goto done;
1730          }          }
1731        }        }
1732        assert(i == array->pn_count);        assert(i == array->pn_count);
# Line 1519  Line 1742 
1742        /* sanity check */        /* sanity check */
1743        assert(strcmp(file_coverage->id, id_bytes) == 0);        assert(strcmp(file_coverage->id, id_bytes) == 0);
1744        if (file_coverage->num_coverage_lines != array->pn_count) {        if (file_coverage->num_coverage_lines != array->pn_count) {
1745          return -2;          result = -2;
1746            goto done;
1747        }        }
1748    
1749        /* merge the coverage */        /* merge the coverage */
# Line 1527  Line 1751 
1751        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++) {
1752          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1753            if (file_coverage->coverage_lines[i] == -1) {            if (file_coverage->coverage_lines[i] == -1) {
1754              return -2;              result = -2;
1755                goto done;
1756            }            }
1757            file_coverage->coverage_lines[i] += (int) element->pn_dval;            file_coverage->coverage_lines[i] += (int) element->pn_dval;
1758          }          }
1759          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1760            if (file_coverage->coverage_lines[i] != -1) {            if (file_coverage->coverage_lines[i] != -1) {
1761              return -2;              result = -2;
1762                goto done;
1763            }            }
1764          }          }
1765          else {          else {
1766            return -1;            result = -1;
1767              goto done;
1768          }          }
1769        }        }
1770        assert(i == array->pn_count);        assert(i == array->pn_count);
# Line 1550  Line 1777 
1777        uint32 i = 0;        uint32 i = 0;
1778        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++) {
1779          if (element->pn_type != TOK_STRING) {          if (element->pn_type != TOK_STRING) {
1780            return -1;            result = -1;
1781              goto done;
1782          }          }
1783          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)));
1784        }        }
# Line 1558  Line 1786 
1786      }      }
1787    }    }
1788    
1789    return 0;  done:
1790      js_FinishParseContext(context, &parse_context);
1791      return result;
1792  }  }

Legend:
Removed from v.317  
changed lines
  Added in v.351

  ViewVC Help
Powered by ViewVC 1.1.24