/[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 336 by siliconforks, Thu Oct 23 20:08:29 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>  #include <jsexn.h>
32  #include <jsfun.h>  #include <jsfun.h>
# Line 199  Line 200 
200    
201  static const char * get_op(uint8 op) {  static const char * get_op(uint8 op) {
202    switch(op) {    switch(op) {
203      case JSOP_OR:
204        return "||";
205      case JSOP_AND:
206        return "&&";
207    case JSOP_BITOR:    case JSOP_BITOR:
208      return "|";      return "|";
209    case JSOP_BITXOR:    case JSOP_BITXOR:
# Line 209  Line 214 
214      return "==";      return "==";
215    case JSOP_NE:    case JSOP_NE:
216      return "!=";      return "!=";
217    case JSOP_NEW_EQ:    case JSOP_STRICTEQ:
218      return "===";      return "===";
219    case JSOP_NEW_NE:    case JSOP_STRICTNE:
220      return "!==";      return "!==";
221    case JSOP_LT:    case JSOP_LT:
222      return "<";      return "<";
# Line 251  Line 256 
256  };  };
257    
258  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) {
259      assert(node->pn_type == TOK_FUNCTION);
260    assert(node->pn_arity == PN_FUNC);    assert(node->pn_arity == PN_FUNC);
261    assert(ATOM_IS_OBJECT(node->pn_funAtom));    JSObject * object = node->pn_funpob->object;
   JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom);  
262    assert(JS_ObjectIsFunction(context, object));    assert(JS_ObjectIsFunction(context, object));
263    JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);    JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
264    assert(function);    assert(function);
265    assert(object == function->object);    assert(object == &function->object);
266    Stream_printf(f, "%*s", indent, "");    Stream_printf(f, "%*s", indent, "");
267    if (type == FUNCTION_NORMAL) {    if (type == FUNCTION_NORMAL) {
268      Stream_write_string(f, "function");      Stream_write_string(f, "function");
# Line 269  Line 274 
274      print_string_atom(function->atom, f);      print_string_atom(function->atom, f);
275    }    }
276    
277    /* function parameters */    /*
278      function parameters - see JS_DecompileFunction in jsapi.cpp, which calls
279      js_DecompileFunction in jsopcode.cpp
280      */
281    Stream_write_string(f, "(");    Stream_write_string(f, "(");
282    JSAtom ** params = xnew(JSAtom *, function->nargs);    JSArenaPool pool;
283    for (int i = 0; i < function->nargs; i++) {    JS_INIT_ARENA_POOL(&pool, "instrument_function", 256, 1, &context->scriptStackQuota);
284      /* initialize to NULL for sanity check */    jsuword * local_names = NULL;
285      params[i] = NULL;    if (JS_GET_LOCAL_NAME_COUNT(function)) {
286    }      local_names = js_GetLocalNameArray(context, function, &pool);
287    JSScope * scope = OBJ_SCOPE(object);      if (local_names == NULL) {
288    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;  
289      }      }
     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);  
290    }    }
291    for (int i = 0; i < function->nargs; i++) {    for (int i = 0; i < function->nargs; i++) {
     assert(params[i] != NULL);  
292      if (i > 0) {      if (i > 0) {
293        Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
294      }      }
295      if (ATOM_IS_STRING(params[i])) {      JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]);
296        print_string_atom(params[i], f);      print_string_atom(param, f);
     }  
297    }    }
298      JS_FinishArenaPool(&pool);
299    Stream_write_string(f, ") {\n");    Stream_write_string(f, ") {\n");
   free(params);  
300    
301    /* function body */    /* function body */
302    instrument_statement(node->pn_body, f, indent + 2, false);    instrument_statement(node->pn_body, f, indent + 2, false);
# Line 374  Line 374 
374      instrument_expression(node->pn_kid3, f);      instrument_expression(node->pn_kid3, f);
375      break;      break;
376    case TOK_OR:    case TOK_OR:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " || ");  
     instrument_expression(node->pn_right, f);  
     break;  
377    case TOK_AND:    case TOK_AND:
     instrument_expression(node->pn_left, f);  
     Stream_write_string(f, " && ");  
     instrument_expression(node->pn_right, f);  
     break;  
378    case TOK_BITOR:    case TOK_BITOR:
379    case TOK_BITXOR:    case TOK_BITXOR:
380    case TOK_BITAND:    case TOK_BITAND:
# Line 495  Line 487 
487      assert(ATOM_IS_STRING(node->pn_atom));      assert(ATOM_IS_STRING(node->pn_atom));
488      {      {
489        JSString * s = ATOM_TO_STRING(node->pn_atom);        JSString * s = ATOM_TO_STRING(node->pn_atom);
490        /* XXX - semantics changed in 1.7 */        bool is_keyword = (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF);
491        if (! ATOM_KEYWORD(node->pn_atom) && js_IsIdentifier(s)) {        if (! is_keyword && js_IsIdentifier(s)) {
492          Stream_write_char(f, '.');          Stream_write_char(f, '.');
493          print_string_atom(node->pn_atom, f);          print_string_atom(node->pn_atom, f);
494        }        }
# Line 576  Line 568 
568    case TOK_STRING:    case TOK_STRING:
569      print_quoted_string_atom(node->pn_atom, f);      print_quoted_string_atom(node->pn_atom, f);
570      break;      break;
571    case TOK_OBJECT:    case TOK_REGEXP:
572      switch (node->pn_op) {      assert(node->pn_op == JSOP_REGEXP);
573      case JSOP_OBJECT:      {
574        /* I assume this is JSOP_REGEXP */        JSObject * object = node->pn_pob->object;
575        abort();        jsval result;
576        break;        js_regexp_toString(context, object, &result);
577      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;  
578      }      }
579      break;      break;
580    case TOK_NUMBER:    case TOK_NUMBER:
# Line 851  Line 832 
832      instrument_statement(node->pn_kid1, f, indent + 2, false);      instrument_statement(node->pn_kid1, f, indent + 2, false);
833      Stream_printf(f, "%*s", indent, "");      Stream_printf(f, "%*s", indent, "");
834      Stream_write_string(f, "}\n");      Stream_write_string(f, "}\n");
835      {      if (node->pn_kid2) {
836        for (JSParseNode * catch = node->pn_kid2; catch != NULL; catch = catch->pn_kid2) {        assert(node->pn_kid2->pn_type == TOK_RESERVED);
837          for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) {
838            assert(scope->pn_type == TOK_LEXICALSCOPE);
839            JSParseNode * catch = scope->pn_expr;
840          assert(catch->pn_type == TOK_CATCH);          assert(catch->pn_type == TOK_CATCH);
841          Stream_printf(f, "%*s", indent, "");          Stream_printf(f, "%*s", indent, "");
842          Stream_write_string(f, "catch (");          Stream_write_string(f, "catch (");
843          assert(catch->pn_kid1->pn_arity == PN_NAME);          assert(catch->pn_kid1->pn_arity == PN_NAME);
844          print_string_atom(catch->pn_kid1->pn_atom, f);          print_string_atom(catch->pn_kid1->pn_atom, f);
845          if (catch->pn_kid1->pn_expr) {          if (catch->pn_kid2) {
846            Stream_write_string(f, " if ");            Stream_write_string(f, " if ");
847            instrument_expression(catch->pn_kid1->pn_expr, f);            instrument_expression(catch->pn_kid2, f);
848          }          }
849          Stream_write_string(f, ") {\n");          Stream_write_string(f, ") {\n");
850          instrument_statement(catch->pn_kid3, f, indent + 2, false);          instrument_statement(catch->pn_kid3, f, indent + 2, false);
# Line 1005  Line 989 
989  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) {
990    file_id = id;    file_id = id;
991    
992    /* scan the javascript */    /* parse the javascript */
993    JSTokenStream * token_stream = js_NewTokenStream(context, characters, num_characters, NULL, 1, NULL);    JSParseContext parse_context;
994    if (token_stream == NULL) {    if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) {
995      fatal("cannot create token stream from file: %s", file_id);      fatal("cannot create token stream from file: %s", file_id);
996    }    }
   
   /* parse the javascript */  
997    JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);    JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter);
998    JSParseNode * node = js_ParseTokenStream(context, global, token_stream);    JSParseNode * node = js_ParseScript(context, global, &parse_context);
999    if (node == NULL) {    if (node == NULL) {
1000      js_ReportUncaughtException(context);      js_ReportUncaughtException(context);
1001      fatal("parse error in file: %s", file_id);      fatal("parse error in file: %s", file_id);
# Line 1100  Line 1082 
1082    
1083    Stream * instrumented = Stream_new(0);    Stream * instrumented = Stream_new(0);
1084    instrument_statement(node, instrumented, 0, false);    instrument_statement(node, instrumented, 0, false);
1085      js_FinishParseContext(context, &parse_context);
1086    
1087    /* write line number info to the output */    /* write line number info to the output */
1088    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 1355 
1355  }  }
1356    
1357  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) {
1358      int result = 0;
1359    
1360    jschar * base = js_InflateString(context, (char *) json, &length);    jschar * base = js_InflateString(context, (char *) json, &length);
1361    if (base == NULL) {    if (base == NULL) {
1362      fatal("out of memory");      fatal("out of memory");
# Line 1384  Line 1369 
1369    
1370    JS_free(context, base);    JS_free(context, base);
1371    
1372    JSTokenStream * token_stream = js_NewTokenStream(context, parenthesized_json, length + 2, NULL, 1, NULL);    JSParseContext parse_context;
1373    if (token_stream == NULL) {    if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) {
1374      fatal("cannot create token stream");      free(parenthesized_json);
1375        return -1;
1376    }    }
1377      JSParseNode * root = js_ParseScript(context, global, &parse_context);
   JSParseNode * root = js_ParseTokenStream(context, global, token_stream);  
1378    free(parenthesized_json);    free(parenthesized_json);
1379    if (root == NULL) {    if (root == NULL) {
1380      return -1;      result = -1;
1381        goto done;
1382    }    }
1383    
1384    /* root node must be TOK_LC */    /* root node must be TOK_LC */
1385    if (root->pn_type != TOK_LC) {    if (root->pn_type != TOK_LC) {
1386      return -1;      result = -1;
1387        goto done;
1388    }    }
1389    JSParseNode * semi = root->pn_u.list.head;    JSParseNode * semi = root->pn_u.list.head;
1390    
1391    /* 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 */
1392    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {    if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) {
1393      return -1;      result = -1;
1394        goto done;
1395    }    }
1396    JSParseNode * parenthesized = semi->pn_kid;    JSParseNode * parenthesized = semi->pn_kid;
1397    
1398    /* this must be a parenthesized expression */    /* this must be a parenthesized expression */
1399    if (parenthesized->pn_type != TOK_RP) {    if (parenthesized->pn_type != TOK_RP) {
1400      return -1;      result = -1;
1401        goto done;
1402    }    }
1403    JSParseNode * object = parenthesized->pn_kid;    JSParseNode * object = parenthesized->pn_kid;
1404    
1405    /* this must be an object literal */    /* this must be an object literal */
1406    if (object->pn_type != TOK_RC) {    if (object->pn_type != TOK_RC) {
1407      return -1;      result = -1;
1408        goto done;
1409    }    }
1410    
1411    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {    for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) {
1412      /* every element of this list must be TOK_COLON */      /* every element of this list must be TOK_COLON */
1413      if (p->pn_type != TOK_COLON) {      if (p->pn_type != TOK_COLON) {
1414        return -1;        result = -1;
1415          goto done;
1416      }      }
1417    
1418      /* the key must be a string representing the file */      /* the key must be a string representing the file */
1419      JSParseNode * key = p->pn_left;      JSParseNode * key = p->pn_left;
1420      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {      if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) {
1421        return -1;        result = -1;
1422          goto done;
1423      }      }
1424      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));      char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom));
1425    
1426      /* the value must be an object literal OR an array */      /* the value must be an object literal OR an array */
1427      JSParseNode * value = p->pn_right;      JSParseNode * value = p->pn_right;
1428      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {      if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) {
1429        return -1;        result = -1;
1430          goto done;
1431      }      }
1432    
1433      JSParseNode * array = NULL;      JSParseNode * array = NULL;
# Line 1446  Line 1439 
1439      else if (value->pn_type == TOK_RC) {      else if (value->pn_type == TOK_RC) {
1440        /* an object literal */        /* an object literal */
1441        if (value->pn_count != 2) {        if (value->pn_count != 2) {
1442          return -1;          result = -1;
1443            goto done;
1444        }        }
1445        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {        for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) {
1446          if (element->pn_type != TOK_COLON) {          if (element->pn_type != TOK_COLON) {
1447            return -1;            result = -1;
1448              goto done;
1449          }          }
1450          JSParseNode * left = element->pn_left;          JSParseNode * left = element->pn_left;
1451          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {          if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) {
1452            return -1;            result = -1;
1453              goto done;
1454          }          }
1455          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));          const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom));
1456          if (strcmp(s, "coverage") == 0) {          if (strcmp(s, "coverage") == 0) {
1457            array = element->pn_right;            array = element->pn_right;
1458            if (array->pn_type != TOK_RB) {            if (array->pn_type != TOK_RB) {
1459              return -1;              result = -1;
1460                goto done;
1461            }            }
1462          }          }
1463          else if (strcmp(s, "source") == 0) {          else if (strcmp(s, "source") == 0) {
1464            source = element->pn_right;            source = element->pn_right;
1465            if (source->pn_type != TOK_RB) {            if (source->pn_type != TOK_RB) {
1466              return -1;              result = -1;
1467                goto done;
1468            }            }
1469          }          }
1470          else {          else {
1471            return -1;            result = -1;
1472              goto done;
1473          }          }
1474        }        }
1475      }      }
1476      else {      else {
1477        return -1;        result = -1;
1478          goto done;
1479      }      }
1480    
1481      if (array == NULL) {      if (array == NULL) {
1482        return -1;        result = -1;
1483          goto done;
1484      }      }
1485    
1486      /* look up the file in the coverage table */      /* look up the file in the coverage table */
# Line 1503  Line 1504 
1504            file_coverage->coverage_lines[i] = -1;            file_coverage->coverage_lines[i] = -1;
1505          }          }
1506          else {          else {
1507            return -1;            result = -1;
1508              goto done;
1509          }          }
1510        }        }
1511        assert(i == array->pn_count);        assert(i == array->pn_count);
# Line 1519  Line 1521 
1521        /* sanity check */        /* sanity check */
1522        assert(strcmp(file_coverage->id, id_bytes) == 0);        assert(strcmp(file_coverage->id, id_bytes) == 0);
1523        if (file_coverage->num_coverage_lines != array->pn_count) {        if (file_coverage->num_coverage_lines != array->pn_count) {
1524          return -2;          result = -2;
1525            goto done;
1526        }        }
1527    
1528        /* merge the coverage */        /* merge the coverage */
# Line 1527  Line 1530 
1530        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++) {
1531          if (element->pn_type == TOK_NUMBER) {          if (element->pn_type == TOK_NUMBER) {
1532            if (file_coverage->coverage_lines[i] == -1) {            if (file_coverage->coverage_lines[i] == -1) {
1533              return -2;              result = -2;
1534                goto done;
1535            }            }
1536            file_coverage->coverage_lines[i] += (int) element->pn_dval;            file_coverage->coverage_lines[i] += (int) element->pn_dval;
1537          }          }
1538          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {          else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) {
1539            if (file_coverage->coverage_lines[i] != -1) {            if (file_coverage->coverage_lines[i] != -1) {
1540              return -2;              result = -2;
1541                goto done;
1542            }            }
1543          }          }
1544          else {          else {
1545            return -1;            result = -1;
1546              goto done;
1547          }          }
1548        }        }
1549        assert(i == array->pn_count);        assert(i == array->pn_count);
# Line 1550  Line 1556 
1556        uint32 i = 0;        uint32 i = 0;
1557        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++) {
1558          if (element->pn_type != TOK_STRING) {          if (element->pn_type != TOK_STRING) {
1559            return -1;            result = -1;
1560              goto done;
1561          }          }
1562          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)));
1563        }        }
# Line 1558  Line 1565 
1565      }      }
1566    }    }
1567    
1568    return 0;  done:
1569      js_FinishParseContext(context, &parse_context);
1570      return result;
1571  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.24