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

Diff of /trunk/instrument-js.cpp

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

revision 240 by siliconforks, Fri Oct 3 23:51:32 2008 UTC revision 340 by siliconforks, Fri Oct 24 16:14:20 2008 UTC
# Line 26  Line 26 
26  #include <string.h>  #include <string.h>
27    
28  #include <jsapi.h>  #include <jsapi.h>
29    #include <jsarena.h>
30  #include <jsatom.h>  #include <jsatom.h>
31    #include <jsexn.h>
32  #include <jsfun.h>  #include <jsfun.h>
33  #include <jsinterp.h>  #include <jsinterp.h>
34    #include <jsiter.h>
35  #include <jsparse.h>  #include <jsparse.h>
36  #include <jsregexp.h>  #include <jsregexp.h>
37  #include <jsscope.h>  #include <jsscope.h>
# Line 53  Line 56 
56  static JSRuntime * runtime = NULL;  static JSRuntime * runtime = NULL;
57  static JSContext * context = NULL;  static JSContext * context = NULL;
58  static JSObject * global = NULL;  static JSObject * global = NULL;
59    static JSVersion js_version = JSVERSION_ECMA_3;
60    
61  /*  /*
62  JSParseNode objects store line numbers starting from 1.  JSParseNode objects store line numbers starting from 1.
# Line 62  Line 66 
66  static char * lines = NULL;  static char * lines = NULL;
67  static uint16_t num_lines = 0;  static uint16_t num_lines = 0;
68    
69    void jscoverage_set_js_version(const char * version) {
70      js_version = atoi(version);
71    }
72    
73  void jscoverage_init(void) {  void jscoverage_init(void) {
74    runtime = JS_NewRuntime(8L * 1024L * 1024L);    runtime = JS_NewRuntime(8L * 1024L * 1024L);
75    if (runtime == NULL) {    if (runtime == NULL) {
# Line 73  Line 81 
81      fatal("cannot create context");      fatal("cannot create context");
82    }    }
83    
84      JS_SetVersion(context, js_version);
85    
86    global = JS_NewObject(context, NULL, NULL, NULL);    global = JS_NewObject(context, NULL, NULL, NULL);
87    if (global == NULL) {    if (global == NULL) {
88      fatal("cannot create global object");      fatal("cannot create global object");
# Line 88  Line 98 
98    JS_DestroyRuntime(runtime);    JS_DestroyRuntime(runtime);
99  }  }
100    
101    static void print_javascript(const jschar * characters, size_t num_characters, Stream * f) {
102      for (size_t i = 0; i < num_characters; i++) {
103        jschar c = characters[i];
104        /*
105        XXX does not handle no-break space, other unicode "space separator"
106        */
107        switch (c) {
108        case 0x9:
109        case 0xB:
110        case 0xC:
111          Stream_write_char(f, c);
112          break;
113        default:
114          if (32 <= c && c <= 126) {
115            Stream_write_char(f, c);
116          }
117          else {
118            Stream_printf(f, "\\u%04x", c);
119          }
120          break;
121        }
122      }
123    }
124    
125  static void print_string(JSString * s, Stream * f) {  static void print_string(JSString * s, Stream * f) {
126    size_t length = JSSTRING_LENGTH(s);    size_t length = JSSTRING_LENGTH(s);
127    jschar * characters = JSSTRING_CHARS(s);    jschar * characters = JSSTRING_CHARS(s);
# Line 122  Line 156 
156        case 0xa:        case 0xa:
157          Stream_write_string(f, "\\n");          Stream_write_string(f, "\\n");
158          break;          break;
159          /* IE doesn't support this */
160          /*
161        case 0xb:        case 0xb:
162          Stream_write_string(f, "\\v");          Stream_write_string(f, "\\v");
163          break;          break;
164          */
165        case 0xc:        case 0xc:
166          Stream_write_string(f, "\\f");          Stream_write_string(f, "\\f");
167          break;          break;
# Line 171  Line 208 
208    
209  static const char * get_op(uint8 op) {  static const char * get_op(uint8 op) {
210    switch(op) {    switch(op) {
211      case JSOP_OR:
212        return "||";
213      case JSOP_AND:
214        return "&&";
215    case JSOP_BITOR:    case JSOP_BITOR:
216      return "|";      return "|";
217    case JSOP_BITXOR:    case JSOP_BITXOR:
# Line 181  Line 222 
222      return "==";      return "==";
223    case JSOP_NE:    case JSOP_NE:
224      return "!=";      return "!=";
225    case JSOP_NEW_EQ:    case JSOP_STRICTEQ:
226      return "===";      return "===";
227    case JSOP_NEW_NE:    case JSOP_STRICTNE:
228      return "!==";      return "!==";
229    case JSOP_LT:    case JSOP_LT:
230      return "<";      return "<";
# Line 223  Line 264 
264  };  };
265    
266  static void instrument_function(JSParseNode * node, Stream * f, int indent, enum FunctionType type) {  static void instrument_function(JSParseNode * node, Stream * f, int indent, enum FunctionType type) {
267      assert(node->pn_type == TOK_FUNCTION);
268    assert(node->pn_arity == PN_FUNC);    assert(node->pn_arity == PN_FUNC);
269    assert(ATOM_IS_OBJECT(node->pn_funAtom));    JSObject * object = node->pn_funpob->object;
   JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom);  
270    assert(JS_ObjectIsFunction(context, object));    assert(JS_ObjectIsFunction(context, object));
271    JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);    JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
272    assert(function);    assert(function);
273    assert(object == function->object);    assert(object == &function->object);
274    Stream_printf(f, "%*s", indent, "");    Stream_printf(f, "%*s", indent, "");
275    if (type == FUNCTION_NORMAL) {    if (type == FUNCTION_NORMAL) {
276      Stream_write_string(f, "function");      Stream_write_string(f, "function");
# Line 241  Line 282 
282      print_string_atom(function->atom, f);      print_string_atom(function->atom, f);
283    }    }
284    
285    /* function parameters */    /*
286      function parameters - see JS_DecompileFunction in jsapi.cpp, which calls
287      js_DecompileFunction in jsopcode.cpp
288      */
289    Stream_write_string(f, "(");    Stream_write_string(f, "(");
290    JSAtom ** params = xnew(JSAtom *, function->nargs);    JSArenaPool pool;
291    for (int i = 0; i < function->nargs; i++) {    JS_INIT_ARENA_POOL(&pool, "instrument_function", 256, 1, &context->scriptStackQuota);
292      /* initialize to NULL for sanity check */    jsuword * local_names = NULL;
293      params[i] = NULL;    if (JS_GET_LOCAL_NAME_COUNT(function)) {
294    }      local_names = js_GetLocalNameArray(context, function, &pool);
295    JSScope * scope = OBJ_SCOPE(object);      if (local_names == NULL) {
296    for (JSScopeProperty * scope_property = SCOPE_LAST_PROP(scope); scope_property != NULL; scope_property = scope_property->parent) {        fatal("out of memory");
     if (scope_property->getter != js_GetArgument) {  
       continue;  
297      }      }
     assert(scope_property->flags & SPROP_HAS_SHORTID);  
     assert((uint16) scope_property->shortid < function->nargs);  
     assert(JSID_IS_ATOM(scope_property->id));  
     params[(uint16) scope_property->shortid] = JSID_TO_ATOM(scope_property->id);  
298    }    }
299    for (int i = 0; i < function->nargs; i++) {    for (int i = 0; i < function->nargs; i++) {
     assert(params[i] != NULL);  
300      if (i > 0) {      if (i > 0) {
301        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
302      }      }
303      if (ATOM_IS_STRING(params[i])) {      JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]);
304        print_string_atom(params[i], f);      print_string_atom(param, f);
     }  
305    }    }
306      JS_FinishArenaPool(&pool);
307    Stream_write_string(f, ") {\n");    Stream_write_string(f, ") {\n");
   free(params);  
308    
309    /* function body */    /* function body */
310    instrument_statement(node->pn_body, f, indent + 2, false);    instrument_statement(node->pn_body, f, indent + 2, false);
# Line 288  Line 324 
324    Stream_write_char(f, ')');    Stream_write_char(f, ')');
325  }  }
326    
327    static void instrument_declarations(JSParseNode * list, Stream * f) {
328      assert(list->pn_arity == PN_LIST);
329      for (JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
330        assert(p->pn_type == TOK_NAME);
331        assert(p->pn_arity == PN_NAME);
332        if (p != list->pn_head) {
333          Stream_write_string(f, ", ");
334        }
335        print_string_atom(p->pn_atom, f);
336        if (p->pn_expr != NULL) {
337          Stream_write_string(f, " = ");
338          instrument_expression(p->pn_expr, f);
339        }
340      }
341    }
342    
343  /*  /*
344  See <Expressions> in jsparse.h.  See <Expressions> in jsparse.h.
345  TOK_FUNCTION is handled as a statement and as an expression.  TOK_FUNCTION is handled as a statement and as an expression.
# Line 346  Line 398 
398      instrument_expression(node->pn_kid3, f);      instrument_expression(node->pn_kid3, f);
399      break;      break;
400    case TOK_OR:    case TOK_OR:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " || ");  
     instrument_expression(node->pn_right, f);  
     break;  
401    case TOK_AND:    case TOK_AND:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " && ");  
     instrument_expression(node->pn_right, f);  
     break;  
402    case TOK_BITOR:    case TOK_BITOR:
403    case TOK_BITXOR:    case TOK_BITXOR:
404    case TOK_BITAND:    case TOK_BITAND:
# Line 467  Line 511 
511      assert(ATOM_IS_STRING(node->pn_atom));      assert(ATOM_IS_STRING(node->pn_atom));
512      {      {
513        JSString * s = ATOM_TO_STRING(node->pn_atom);        JSString * s = ATOM_TO_STRING(node->pn_atom);
514        /* XXX - semantics changed in 1.7 */        bool is_keyword = (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF);
515        if (! ATOM_KEYWORD(node->pn_atom) && js_IsIdentifier(s)) {        if (! is_keyword && js_IsIdentifier(s)) {
516          Stream_write_char(f, '.');          Stream_write_char(f, '.');
517          print_string_atom(node->pn_atom, f);          print_string_atom(node->pn_atom, f);
518        }        }
# Line 515  Line 559 
559        /* check whether this is a getter or setter */        /* check whether this is a getter or setter */
560        switch (p->pn_op) {        switch (p->pn_op) {
561        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;  
562        case JSOP_SETTER:        case JSOP_SETTER:
563          Stream_write_string(f, "set ");          if (p->pn_op == JSOP_GETTER) {
564              Stream_write_string(f, "get ");
565            }
566            else {
567              Stream_write_string(f, "set ");
568            }
569          instrument_expression(p->pn_left, f);          instrument_expression(p->pn_left, f);
570            if (p->pn_right->pn_type != TOK_FUNCTION) {
571              fatal("parse error: expected function");
572            }
573          instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);          instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);
574          break;          break;
575        default:        default:
# Line 544  Line 592 
592    case TOK_STRING:    case TOK_STRING:
593      print_quoted_string_atom(node->pn_atom, f);      print_quoted_string_atom(node->pn_atom, f);
594      break;      break;
595    case TOK_OBJECT:    case TOK_REGEXP:
596      switch (node->pn_op) {      assert(node->pn_op == JSOP_REGEXP);
597      case JSOP_OBJECT:      {
598        /* I assume this is JSOP_REGEXP */        JSObject * object = node->pn_pob->object;
599        abort();        jsval result;
600        break;        js_regexp_toString(context, object, &result);
601      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;  
602      }      }
603      break;      break;
604    case TOK_NUMBER:    case TOK_NUMBER:
# Line 607  Line 644 
644      Stream_write_string(f, " in ");      Stream_write_string(f, " in ");
645      instrument_expression(node->pn_right, f);      instrument_expression(node->pn_right, f);
646      break;      break;
647      case TOK_LEXICALSCOPE:
648        assert(node->pn_arity == PN_NAME);
649        assert(node->pn_expr->pn_type == TOK_LET);
650        assert(node->pn_expr->pn_arity == PN_BINARY);
651        Stream_write_string(f, "let(");
652        assert(node->pn_expr->pn_left->pn_type == TOK_LP);
653        assert(node->pn_expr->pn_left->pn_arity == PN_LIST);
654        instrument_declarations(node->pn_expr->pn_left, f);
655        Stream_write_string(f, ") ");
656        instrument_expression(node->pn_expr->pn_right, f);
657        break;
658    default:    default:
659      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);
660    }    }
# Line 616  Line 664 
664    assert(node->pn_arity == PN_LIST);    assert(node->pn_arity == PN_LIST);
665    Stream_printf(f, "%*s", indent, "");    Stream_printf(f, "%*s", indent, "");
666    Stream_write_string(f, "var ");    Stream_write_string(f, "var ");
667    for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) {    instrument_declarations(node, f);
668      assert(p->pn_type == TOK_NAME);  }
669      assert(p->pn_arity == PN_NAME);  
670      if (p != node->pn_head) {  static void instrument_let_definition(JSParseNode * node, Stream * f, int indent) {
671        Stream_write_string(f, ", ");    assert(node->pn_arity == PN_LIST);
672      }    Stream_printf(f, "%*s", indent, "");
673      print_string_atom(p->pn_atom, f);    Stream_write_string(f, "let ");
674      if (p->pn_expr != NULL) {    instrument_declarations(node, f);
       Stream_write_string(f, " = ");  
       instrument_expression(p->pn_expr, f);  
     }  
   }  
675  }  }
676    
677  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) {
# Line 750  Line 794 
794    case TOK_FOR:    case TOK_FOR:
795      assert(node->pn_arity == PN_BINARY);      assert(node->pn_arity == PN_BINARY);
796      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
797      Stream_write_string(f, "for (");      Stream_write_string(f, "for ");
798        if (node->pn_iflags & JSITER_FOREACH) {
799          Stream_write_string(f, "each ");
800        }
801        Stream_write_char(f, '(');
802      switch (node->pn_left->pn_type) {      switch (node->pn_left->pn_type) {
803      case TOK_IN:      case TOK_IN:
804        /* for/in */        /* for/in */
# Line 759  Line 807 
807        case TOK_VAR:        case TOK_VAR:
808          instrument_var_statement(node->pn_left->pn_left, f, 0);          instrument_var_statement(node->pn_left->pn_left, f, 0);
809          break;          break;
810          case TOK_LET:
811            instrument_let_definition(node->pn_left->pn_left, f, 0);
812            break;
813        case TOK_NAME:        case TOK_NAME:
814          instrument_expression(node->pn_left->pn_left, f);          instrument_expression(node->pn_left->pn_left, f);
815          break;          break;
# Line 780  Line 831 
831        /* for (;;) */        /* for (;;) */
832        assert(node->pn_left->pn_arity == PN_TERNARY);        assert(node->pn_left->pn_arity == PN_TERNARY);
833        if (node->pn_left->pn_kid1) {        if (node->pn_left->pn_kid1) {
834          if (node->pn_left->pn_kid1->pn_type == TOK_VAR) {          switch (node->pn_left->pn_kid1->pn_type) {
835            case TOK_VAR:
836            instrument_var_statement(node->pn_left->pn_kid1, f, 0);            instrument_var_statement(node->pn_left->pn_kid1, f, 0);
837          }            break;
838          else {          case TOK_LET:
839              instrument_let_definition(node->pn_left->pn_kid1, f, 0);
840              break;
841            default:
842            instrument_expression(node->pn_left->pn_kid1, f);            instrument_expression(node->pn_left->pn_kid1, f);
843              break;
844          }          }
845        }        }
846        Stream_write_string(f, ";");        Stream_write_string(f, ";");
# Line 819  Line 875 
875      instrument_statement(node->pn_kid1, f, indent + 2, false);      instrument_statement(node->pn_kid1, f, indent + 2, false);
876      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
877      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
878      {      if (node->pn_kid2) {
879        for (JSParseNode * catch = node->pn_kid2; catch != NULL; catch = catch->pn_kid2) {        assert(node->pn_kid2->pn_type == TOK_RESERVED);
880          for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) {
881            assert(scope->pn_type == TOK_LEXICALSCOPE);
882            JSParseNode * catch = scope->pn_expr;
883          assert(catch->pn_type == TOK_CATCH);          assert(catch->pn_type == TOK_CATCH);
884          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
885          Stream_write_string(f, "catch (");          Stream_write_string(f, "catch (");
886          assert(catch->pn_kid1->pn_arity == PN_NAME);          assert(catch->pn_kid1->pn_arity == PN_NAME);
887          print_string_atom(catch->pn_kid1->pn_atom, f);          print_string_atom(catch->pn_kid1->pn_atom, f);
888          if (catch->pn_kid1->pn_expr) {          if (catch->pn_kid2) {
889            Stream_write_string(f, " if ");            Stream_write_string(f, " if ");
890            instrument_expression(catch->pn_kid1->pn_expr, f);            instrument_expression(catch->pn_kid2, f);
891          }          }
892          Stream_write_string(f, ") {\n");          Stream_write_string(f, ") {\n");
893          instrument_statement(catch->pn_kid3, f, indent + 2, false);          instrument_statement(catch->pn_kid3, f, indent + 2, false);
# Line 905  Line 964 
964      */      */
965      output_statement(node->pn_expr, f, indent, false);      output_statement(node->pn_expr, f, indent, false);
966      break;      break;
967      case TOK_LEXICALSCOPE:
968        /* let statement */
969        assert(node->pn_arity == PN_NAME);
970        switch (node->pn_expr->pn_type) {
971        case TOK_LET:
972          /* let statement */
973          assert(node->pn_expr->pn_arity == PN_BINARY);
974          instrument_statement(node->pn_expr, f, indent, false);
975          break;
976        case TOK_LC:
977          /* block */
978          Stream_printf(f, "%*s", indent, "");
979          Stream_write_string(f, "{\n");
980          instrument_statement(node->pn_expr, f, indent + 2, false);
981          Stream_printf(f, "%*s", indent, "");
982          Stream_write_string(f, "}\n");
983          break;
984        case TOK_FOR:
985          instrument_statement(node->pn_expr, f, indent, false);
986          break;
987        default:
988          abort();
989          break;
990        }
991        break;
992      case TOK_LET:
993        switch (node->pn_arity) {
994        case PN_BINARY:
995          /* let statement */
996          Stream_printf(f, "%*s", indent, "");
997          Stream_write_string(f, "let (");
998          assert(node->pn_left->pn_type == TOK_LP);
999          assert(node->pn_left->pn_arity == PN_LIST);
1000          instrument_declarations(node->pn_left, f);
1001          Stream_write_string(f, ") {\n");
1002          instrument_statement(node->pn_right, f, indent + 2, false);
1003          Stream_printf(f, "%*s", indent, "");
1004          Stream_write_string(f, "}\n");
1005          break;
1006        case PN_LIST:
1007          /* let definition */
1008          instrument_let_definition(node, f, indent);
1009          Stream_write_string(f, ";\n");
1010          break;
1011        default:
1012          abort();
1013          break;
1014        }
1015        break;
1016    default:    default:
1017      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);
1018    }    }
# Line 916  Line 1024 
1024  TOK_EXPORT, TOK_IMPORT are not handled.  TOK_EXPORT, TOK_IMPORT are not handled.
1025  */  */
1026  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) {
1027    if (node->pn_type != TOK_LC) {    if (node->pn_type != TOK_LC && node->pn_type != TOK_LEXICALSCOPE) {
1028      uint16_t line = node->pn_pos.begin.lineno;      uint16_t line = node->pn_pos.begin.lineno;
1029      if (line > num_lines) {      if (line > num_lines) {
1030        fatal("%s: script contains more than 65,535 lines", file_id);        fatal("%s: script contains more than 65,535 lines", file_id);
# Line 966  Line 1074 
1074    return true;    return true;
1075  }  }
1076    
1077    static void error_reporter(JSContext * context, const char * message, JSErrorReport * report) {
1078      fprintf(stderr, "jscoverage: parse error: line %u: %s\n", report->lineno, message);
1079    }
1080    
1081  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) {
1082    file_id = id;    file_id = id;
1083    
1084    /* scan the javascript */    /* parse the javascript */
1085    JSTokenStream * token_stream = js_NewTokenStream(context, characters, num_characters, NULL, 1, NULL);    JSParseContext parse_context;
1086    if (token_stream == NULL) {    if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) {
1087      fatal("cannot create token stream from file: %s", file_id);      fatal("cannot create token stream from file: %s", file_id);
1088    }    }
1089      JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);
1090    /* parse the javascript */    JSParseNode * node = js_ParseScript(context, global, &parse_context);
   JSParseNode * node = js_ParseTokenStream(context, global, token_stream);  
1091    if (node == NULL) {    if (node == NULL) {
1092        js_ReportUncaughtException(context);
1093      fatal("parse error in file: %s", file_id);      fatal("parse error in file: %s", file_id);
1094    }    }
1095      JS_SetErrorReporter(context, old_error_reporter);
1096    num_lines = node->pn_pos.end.lineno;    num_lines = node->pn_pos.end.lineno;
1097    lines = xmalloc(num_lines);    lines = xmalloc(num_lines);
1098    for (unsigned int i = 0; i < num_lines; i++) {    for (unsigned int i = 0; i < num_lines; i++) {
# Line 1061  Line 1174 
1174    
1175    Stream * instrumented = Stream_new(0);    Stream * instrumented = Stream_new(0);
1176    instrument_statement(node, instrumented, 0, false);    instrument_statement(node, instrumented, 0, false);
1177      js_FinishParseContext(context, &parse_context);
1178    
1179    /* write line number info to the output */    /* write line number info to the output */
1180    Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");    Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n");
# Line 1090  Line 1204 
1204    /* conditionals */    /* conditionals */
1205    for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) {    for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) {
1206      Stream_write_string(output, "if (!(");      Stream_write_string(output, "if (!(");
1207      for (const jschar * p = if_directive->condition_start; p < if_directive->condition_end; p++) {      print_javascript(if_directive->condition_start, if_directive->condition_end - if_directive->condition_start, output);
       jschar c = *p;  
       if (c == '\t' || (32 <= c && c <= 126)) {  
         Stream_write_char(output, c);  
       }  
       else {  
         Stream_printf(output, "\\u%04x", c);  
       }  
     }  
1208      Stream_write_string(output, ")) {\n");      Stream_write_string(output, ")) {\n");
1209      Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line);      Stream_printf(output, "  _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line);
1210      Stream_write_string(output, "}\n");      Stream_write_string(output, "}\n");
# Line 1149  Line 1255 
1255            /* line feed (new line) */            /* line feed (new line) */
1256            done = true;            done = true;
1257            break;            break;
1258            /* IE doesn't support this */
1259            /*
1260          case 0xb:          case 0xb:
           /* vertical tab */  
1261            Stream_write_string(output, "\\v");            Stream_write_string(output, "\\v");
1262            break;            break;
1263            */
1264          case 0xc:          case 0xc:
1265            /* form feed */            /* form feed */
1266            Stream_write_string(output, "\\f");            Stream_write_string(output, "\\f");
# Line 1207  Line 1315 
1315            /* line feed (new line) */            /* line feed (new line) */
1316            done = true;            done = true;
1317            break;            break;
1318            /* IE doesn't support this */
1319            /*
1320          case 0xb:          case 0xb:
           /* vertical tab */  
1321            Stream_write_string(output, "\\v");            Stream_write_string(output, "\\v");
1322            break;            break;
1323            */
1324          case 0xc:          case 0xc:
1325            /* form feed */            /* form feed */
1326            Stream_write_string(output, "\\f");            Stream_write_string(output, "\\f");
# Line 1337  Line 1447 
1447  }  }
1448    
1449  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) {
1450      int result = 0;
1451    
1452    jschar * base = js_InflateString(context, (char *) json, &length);    jschar * base = js_InflateString(context, (char *) json, &length);
1453    if (base == NULL) {    if (base == NULL) {
1454      fatal("out of memory");      fatal("out of memory");
# Line 1349  Line 1461 
1461    
1462    JS_free(context, base);    JS_free(context, base);
1463    
1464    JSTokenStream * token_stream = js_NewTokenStream(context, parenthesized_json, length + 2, NULL, 1, NULL);    JSParseContext parse_context;
1465    if (token_stream == NULL) {    if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) {
1466      fatal("cannot create token stream");      free(parenthesized_json);
1467        return -1;
1468    }    }
1469      JSParseNode * root = js_ParseScript(context, global, &parse_context);
   JSParseNode * root = js_ParseTokenStream(context, global, token_stream);  
1470    free(parenthesized_json);    free(parenthesized_json);
1471    if (root == NULL) {    if (root == NULL) {
1472      return -1;      result = -1;
1473        goto done;
1474    }    }
1475    
1476    /* root node must be TOK_LC */    /* root node must be TOK_LC */
1477    if (root->pn_type != TOK_LC) {    if (root->pn_type != TOK_LC) {
1478      return -1;      result = -1;
1479        goto done;
1480    }    }
1481    JSParseNode * semi = root->pn_u.list.head;    JSParseNode * semi = root->pn_u.list.head;
1482    
1483    /* 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 */
1484    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {
1485      return -1;      result = -1;
1486        goto done;
1487    }    }
1488    JSParseNode * parenthesized = semi->pn_kid;    JSParseNode * parenthesized = semi->pn_kid;
1489    
1490    /* this must be a parenthesized expression */    /* this must be a parenthesized expression */
1491    if (parenthesized->pn_type != TOK_RP) {    if (parenthesized->pn_type != TOK_RP) {
1492      return -1;      result = -1;
1493        goto done;
1494    }    }
1495    JSParseNode * object = parenthesized->pn_kid;    JSParseNode * object = parenthesized->pn_kid;
1496    
1497    /* this must be an object literal */    /* this must be an object literal */
1498    if (object->pn_type != TOK_RC) {    if (object->pn_type != TOK_RC) {
1499      return -1;      result = -1;
1500        goto done;
1501    }    }
1502    
1503    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {
1504      /* every element of this list must be TOK_COLON */      /* every element of this list must be TOK_COLON */
1505      if (p->pn_type != TOK_COLON) {      if (p->pn_type != TOK_COLON) {
1506        return -1;        result = -1;
1507          goto done;
1508      }      }
1509    
1510      /* the key must be a string representing the file */      /* the key must be a string representing the file */
1511      JSParseNode * key = p->pn_left;      JSParseNode * key = p->pn_left;
1512      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {
1513        return -1;        result = -1;
1514          goto done;
1515      }      }
1516      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));
1517    
1518      /* the value must be an object literal OR an array */      /* the value must be an object literal OR an array */
1519      JSParseNode * value = p->pn_right;      JSParseNode * value = p->pn_right;
1520      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {
1521        return -1;        result = -1;
1522          goto done;
1523      }      }
1524    
1525      JSParseNode * array = NULL;      JSParseNode * array = NULL;
# Line 1411  Line 1531 
1531      else if (value->pn_type == TOK_RC) {      else if (value->pn_type == TOK_RC) {
1532        /* an object literal */        /* an object literal */
1533        if (value->pn_count != 2) {        if (value->pn_count != 2) {
1534          return -1;          result = -1;
1535            goto done;
1536        }        }
1537        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {
1538          if (element->pn_type != TOK_COLON) {          if (element->pn_type != TOK_COLON) {
1539            return -1;            result = -1;
1540              goto done;
1541          }          }
1542          JSParseNode * left = element->pn_left;          JSParseNode * left = element->pn_left;
1543          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {
1544            return -1;            result = -1;
1545              goto done;
1546          }          }
1547          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));
1548          if (strcmp(s, "coverage") == 0) {          if (strcmp(s, "coverage") == 0) {
1549            array = element->pn_right;            array = element->pn_right;
1550            if (array->pn_type != TOK_RB) {            if (array->pn_type != TOK_RB) {
1551              return -1;              result = -1;
1552                goto done;
1553            }            }
1554          }          }
1555          else if (strcmp(s, "source") == 0) {          else if (strcmp(s, "source") == 0) {
1556            source = element->pn_right;            source = element->pn_right;
1557            if (source->pn_type != TOK_RB) {            if (source->pn_type != TOK_RB) {
1558              return -1;              result = -1;
1559                goto done;
1560            }            }
1561          }          }
1562          else {          else {
1563            return -1;            result = -1;
1564              goto done;
1565          }          }
1566        }        }
1567      }      }
1568      else {      else {
1569        return -1;        result = -1;
1570          goto done;
1571      }      }
1572    
1573      if (array == NULL) {      if (array == NULL) {
1574        return -1;        result = -1;
1575          goto done;
1576      }      }
1577    
1578      /* look up the file in the coverage table */      /* look up the file in the coverage table */
# Line 1456  Line 1584 
1584        file_coverage->id = id;        file_coverage->id = id;
1585        file_coverage->num_coverage_lines = array->pn_count;        file_coverage->num_coverage_lines = array->pn_count;
1586        file_coverage->coverage_lines = xnew(int, array->pn_count);        file_coverage->coverage_lines = xnew(int, array->pn_count);
1587        if (source == NULL) {        file_coverage->source_lines = NULL;
         file_coverage->source_lines = NULL;  
       }  
       else {  
         file_coverage->num_source_lines = source->pn_count;  
         file_coverage->source_lines = xnew(char *, source->pn_count);  
         uint32 i = 0;  
         for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) {  
           if (element->pn_type != TOK_STRING) {  
             return -1;  
           }  
           file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));  
         }  
         assert(i == source->pn_count);  
       }  
1588    
1589        /* set coverage for all lines */        /* set coverage for all lines */
1590        uint32 i = 0;        uint32 i = 0;
# Line 1482  Line 1596 
1596            file_coverage->coverage_lines[i] = -1;            file_coverage->coverage_lines[i] = -1;
1597          }          }
1598          else {          else {
1599            return -1;            result = -1;
1600              goto done;
1601          }          }
1602        }        }
1603        assert(i == array->pn_count);        assert(i == array->pn_count);
# Line 1498  Line 1613 
1613        /* sanity check */        /* sanity check */
1614        assert(strcmp(file_coverage->id, id_bytes) == 0);        assert(strcmp(file_coverage->id, id_bytes) == 0);
1615        if (file_coverage->num_coverage_lines != array->pn_count) {        if (file_coverage->num_coverage_lines != array->pn_count) {
1616          return -2;          result = -2;
1617            goto done;
1618        }        }
1619    
1620        /* merge the coverage */        /* merge the coverage */
# Line 1506  Line 1622 
1622        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++) {
1623          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1624            if (file_coverage->coverage_lines[i] == -1) {            if (file_coverage->coverage_lines[i] == -1) {
1625              return -2;              result = -2;
1626                goto done;
1627            }            }
1628            file_coverage->coverage_lines[i] += (int) element->pn_dval;            file_coverage->coverage_lines[i] += (int) element->pn_dval;
1629          }          }
1630          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1631            if (file_coverage->coverage_lines[i] != -1) {            if (file_coverage->coverage_lines[i] != -1) {
1632              return -2;              result = -2;
1633                goto done;
1634            }            }
1635          }          }
1636          else {          else {
1637            return -1;            result = -1;
1638              goto done;
1639          }          }
1640        }        }
1641        assert(i == array->pn_count);        assert(i == array->pn_count);
1642        }
1643    
1644        /* if this JSON file has source, use it */      /* if this JSON file has source, use it */
1645        if (file_coverage->source_lines == NULL && source != NULL) {      if (file_coverage->source_lines == NULL && source != NULL) {
1646          file_coverage->num_source_lines = source->pn_count;        file_coverage->num_source_lines = source->pn_count;
1647          file_coverage->source_lines = xnew(char *, source->pn_count);        file_coverage->source_lines = xnew(char *, source->pn_count);
1648          uint32 i = 0;        uint32 i = 0;
1649          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++) {
1650            if (element->pn_type != TOK_STRING) {          if (element->pn_type != TOK_STRING) {
1651              return -1;            result = -1;
1652            }            goto done;
           file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));  
1653          }          }
1654          assert(i == source->pn_count);          file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom)));
1655        }        }
1656          assert(i == source->pn_count);
1657      }      }
1658    }    }
1659    
1660    return 0;  done:
1661      js_FinishParseContext(context, &parse_context);
1662      return result;
1663  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.24