--- trunk/instrument-js.c 2008/05/07 04:21:22 92 +++ trunk/instrument-js.c 2008/10/12 16:41:57 293 @@ -17,6 +17,8 @@ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include + #include "instrument-js.h" #include @@ -25,6 +27,7 @@ #include #include +#include #include #include #include @@ -32,8 +35,22 @@ #include #include +#include "encoding.h" +#include "global.h" +#include "highlight.h" +#include "resource-manager.h" #include "util.h" +struct IfDirective { + const jschar * condition_start; + const jschar * condition_end; + uint16_t start_line; + uint16_t end_line; + struct IfDirective * next; +}; + +static bool * exclusive_directives = NULL; + static JSRuntime * runtime = NULL; static JSContext * context = NULL; static JSObject * global = NULL; @@ -44,6 +61,7 @@ */ static const char * file_id = NULL; static char * lines = NULL; +static uint16_t num_lines = 0; void jscoverage_init(void) { runtime = JS_NewRuntime(8L * 1024L * 1024L); @@ -71,10 +89,78 @@ JS_DestroyRuntime(runtime); } +static void print_javascript(const jschar * characters, size_t num_characters, Stream * f) { + for (size_t i = 0; i < num_characters; i++) { + jschar c = characters[i]; + /* + XXX does not handle no-break space, other unicode "space separator" + */ + switch (c) { + case 0x9: + case 0xB: + case 0xC: + Stream_write_char(f, c); + break; + default: + if (32 <= c && c <= 126) { + Stream_write_char(f, c); + } + else { + Stream_printf(f, "\\u%04x", c); + } + break; + } + } +} + static void print_string(JSString * s, Stream * f) { - for (int i = 0; i < s->length; i++) { - char c = s->chars[i]; - Stream_write_char(f, c); + size_t length = JSSTRING_LENGTH(s); + jschar * characters = JSSTRING_CHARS(s); + for (size_t i = 0; i < length; i++) { + jschar c = characters[i]; + if (32 <= c && c <= 126) { + switch (c) { + case '"': + Stream_write_string(f, "\\\""); + break; +/* + case '\'': + Stream_write_string(f, "\\'"); + break; +*/ + case '\\': + Stream_write_string(f, "\\\\"); + break; + default: + Stream_write_char(f, c); + break; + } + } + else { + switch (c) { + case 0x8: + Stream_write_string(f, "\\b"); + break; + case 0x9: + Stream_write_string(f, "\\t"); + break; + case 0xa: + Stream_write_string(f, "\\n"); + break; + case 0xb: + Stream_write_string(f, "\\v"); + break; + case 0xc: + Stream_write_string(f, "\\f"); + break; + case 0xd: + Stream_write_string(f, "\\r"); + break; + default: + Stream_printf(f, "\\u%04x", c); + break; + } + } } } @@ -84,17 +170,28 @@ print_string(s, f); } -static void print_string_jsval(jsval value, Stream * f) { +static void print_regex(jsval value, Stream * f) { assert(JSVAL_IS_STRING(value)); JSString * s = JSVAL_TO_STRING(value); - print_string(s, f); + size_t length = JSSTRING_LENGTH(s); + jschar * characters = JSSTRING_CHARS(s); + for (size_t i = 0; i < length; i++) { + jschar c = characters[i]; + if (32 <= c && c <= 126) { + Stream_write_char(f, c); + } + else { + Stream_printf(f, "\\u%04x", c); + } + } } static void print_quoted_string_atom(JSAtom * atom, Stream * f) { assert(ATOM_IS_STRING(atom)); JSString * s = ATOM_TO_STRING(atom); - JSString * quoted = js_QuoteString(context, s, '"'); - print_string(quoted, f); + Stream_write_char(f, '"'); + print_string(s, f); + Stream_write_char(f, '"'); } static const char * get_op(uint8 op) { @@ -143,58 +240,65 @@ } static void instrument_expression(JSParseNode * node, Stream * f); -static void instrument_statement(JSParseNode * node, Stream * f, int indent); +static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if); -static void instrument_function(JSParseNode * node, Stream * f, int indent) { - assert(node->pn_arity == PN_FUNC); - assert(ATOM_IS_OBJECT(node->pn_funAtom)); - JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom); - assert(JS_ObjectIsFunction(context, object)); - JSFunction * function = (JSFunction *) JS_GetPrivate(context, object); - assert(function); - assert(object == function->object); - Stream_printf(f, "%*s", indent, ""); +enum FunctionType { + FUNCTION_NORMAL, + FUNCTION_GETTER_OR_SETTER +}; + +static void instrument_function(JSParseNode * node, Stream * f, int indent, enum FunctionType type) { + assert(node->pn_arity == PN_FUNC); + assert(ATOM_IS_OBJECT(node->pn_funAtom)); + JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom); + assert(JS_ObjectIsFunction(context, object)); + JSFunction * function = (JSFunction *) JS_GetPrivate(context, object); + assert(function); + assert(object == function->object); + Stream_printf(f, "%*s", indent, ""); + if (type == FUNCTION_NORMAL) { Stream_write_string(f, "function"); + } - /* function name */ - if (function->atom) { - Stream_write_char(f, ' '); - print_string_atom(function->atom, f); - } + /* function name */ + if (function->atom) { + Stream_write_char(f, ' '); + print_string_atom(function->atom, f); + } - /* function parameters */ - Stream_write_string(f, "("); - JSAtom ** params = xmalloc(function->nargs * sizeof(JSAtom *)); - for (int i = 0; i < function->nargs; i++) { - /* initialize to NULL for sanity check */ - params[i] = NULL; - } - JSScope * scope = OBJ_SCOPE(object); - for (JSScopeProperty * scope_property = SCOPE_LAST_PROP(scope); scope_property != NULL; scope_property = scope_property->parent) { - if (scope_property->getter != js_GetArgument) { - continue; - } - 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); + /* function parameters */ + Stream_write_string(f, "("); + JSAtom ** params = xnew(JSAtom *, function->nargs); + for (int i = 0; i < function->nargs; i++) { + /* initialize to NULL for sanity check */ + params[i] = NULL; + } + JSScope * scope = OBJ_SCOPE(object); + for (JSScopeProperty * scope_property = SCOPE_LAST_PROP(scope); scope_property != NULL; scope_property = scope_property->parent) { + if (scope_property->getter != js_GetArgument) { + continue; + } + 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); + } + for (int i = 0; i < function->nargs; i++) { + assert(params[i] != NULL); + if (i > 0) { + Stream_write_string(f, ", "); } - for (int i = 0; i < function->nargs; i++) { - assert(params[i] != NULL); - if (i > 0) { - Stream_write_string(f, ", "); - } - if (ATOM_IS_STRING(params[i])) { - print_string_atom(params[i], f); - } + if (ATOM_IS_STRING(params[i])) { + print_string_atom(params[i], f); } - Stream_write_string(f, ") {\n"); - free(params); + } + Stream_write_string(f, ") {\n"); + free(params); - /* function body */ - instrument_statement(node->pn_body, f, indent + 2); + /* function body */ + instrument_statement(node->pn_body, f, indent + 2, false); - Stream_write_string(f, "}\n"); + Stream_write_string(f, "}\n"); } static void instrument_function_call(JSParseNode * node, Stream * f) { @@ -225,7 +329,7 @@ static void instrument_expression(JSParseNode * node, Stream * f) { switch (node->pn_type) { case TOK_FUNCTION: - instrument_function(node, f, 0); + instrument_function(node, f, 0, FUNCTION_NORMAL); break; case TOK_COMMA: for (struct JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) { @@ -432,9 +536,29 @@ if (p != node->pn_head) { Stream_write_string(f, ", "); } - instrument_expression(p->pn_left, f); - Stream_write_string(f, ": "); - instrument_expression(p->pn_right, f); + + /* check whether this is a getter or setter */ + switch (p->pn_op) { + case JSOP_GETTER: + case JSOP_SETTER: + if (p->pn_op == JSOP_GETTER) { + Stream_write_string(f, "get "); + } + else { + Stream_write_string(f, "set "); + } + instrument_expression(p->pn_left, f); + if (p->pn_right->pn_type != TOK_FUNCTION) { + fatal("parse error: expected function"); + } + instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER); + break; + default: + instrument_expression(p->pn_left, f); + Stream_write_string(f, ": "); + instrument_expression(p->pn_right, f); + break; + } } Stream_write_char(f, '}'); break; @@ -461,7 +585,7 @@ JSObject * object = ATOM_TO_OBJECT(node->pn_atom); jsval result; js_regexp_toString(context, object, 0, NULL, &result); - print_string_jsval(result, f); + print_regex(result, f); } break; default: @@ -535,10 +659,10 @@ } } -static void output_statement(JSParseNode * node, Stream * f, int indent) { +static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) { switch (node->pn_type) { case TOK_FUNCTION: - instrument_function(node, f, indent); + instrument_function(node, f, indent, FUNCTION_NORMAL); break; case TOK_LC: assert(node->pn_arity == PN_LIST); @@ -546,7 +670,7 @@ Stream_write_string(f, "{\n"); */ for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) { - instrument_statement(p, f, indent); + instrument_statement(p, f, indent, false); } /* Stream_printf(f, "%*s", indent, ""); @@ -554,22 +678,54 @@ */ break; case TOK_IF: + { assert(node->pn_arity == PN_TERNARY); + + uint16_t line = node->pn_pos.begin.lineno; + if (! is_jscoverage_if) { + if (line > num_lines) { + fatal("%s: script contains more than 65,535 lines", file_id); + } + if (line >= 2 && exclusive_directives[line - 2]) { + is_jscoverage_if = true; + } + } + Stream_printf(f, "%*s", indent, ""); Stream_write_string(f, "if ("); instrument_expression(node->pn_kid1, f); Stream_write_string(f, ") {\n"); - instrument_statement(node->pn_kid2, f, indent + 2); + if (is_jscoverage_if && node->pn_kid3) { + uint16_t else_start = node->pn_kid3->pn_pos.begin.lineno; + uint16_t else_end = node->pn_kid3->pn_pos.end.lineno + 1; + Stream_printf(f, "%*s", indent + 2, ""); + Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, else_start, else_end); + } + instrument_statement(node->pn_kid2, f, indent + 2, false); Stream_printf(f, "%*s", indent, ""); Stream_write_string(f, "}\n"); - if (node->pn_kid3) { + + if (node->pn_kid3 || is_jscoverage_if) { Stream_printf(f, "%*s", indent, ""); Stream_write_string(f, "else {\n"); - instrument_statement(node->pn_kid3, f, indent + 2); + + if (is_jscoverage_if) { + uint16_t if_start = node->pn_kid2->pn_pos.begin.lineno + 1; + uint16_t if_end = node->pn_kid2->pn_pos.end.lineno + 1; + Stream_printf(f, "%*s", indent + 2, ""); + Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_start, if_end); + } + + if (node->pn_kid3) { + instrument_statement(node->pn_kid3, f, indent + 2, is_jscoverage_if); + } + Stream_printf(f, "%*s", indent, ""); Stream_write_string(f, "}\n"); } + break; + } case TOK_SWITCH: assert(node->pn_arity == PN_BINARY); Stream_printf(f, "%*s", indent, ""); @@ -591,7 +747,7 @@ abort(); break; } - instrument_statement(p->pn_right, f, indent + 2); + instrument_statement(p->pn_right, f, indent + 2, false); } Stream_printf(f, "%*s", indent, ""); Stream_write_string(f, "}\n"); @@ -606,14 +762,14 @@ Stream_write_string(f, "while ("); instrument_expression(node->pn_left, f); Stream_write_string(f, ") {\n"); - instrument_statement(node->pn_right, f, indent + 2); + instrument_statement(node->pn_right, f, indent + 2, false); Stream_write_string(f, "}\n"); break; case TOK_DO: assert(node->pn_arity == PN_BINARY); Stream_printf(f, "%*s", indent, ""); Stream_write_string(f, "do {\n"); - instrument_statement(node->pn_left, f, indent + 2); + instrument_statement(node->pn_left, f, indent + 2, false); Stream_write_string(f, "}\n"); Stream_printf(f, "%*s", indent, ""); Stream_write_string(f, "while ("); @@ -676,7 +832,7 @@ break; } Stream_write_string(f, ") {\n"); - instrument_statement(node->pn_right, f, indent + 2); + instrument_statement(node->pn_right, f, indent + 2, false); Stream_write_string(f, "}\n"); break; case TOK_THROW: @@ -689,7 +845,7 @@ case TOK_TRY: Stream_printf(f, "%*s", indent, ""); Stream_write_string(f, "try {\n"); - instrument_statement(node->pn_kid1, f, indent + 2); + instrument_statement(node->pn_kid1, f, indent + 2, false); Stream_printf(f, "%*s", indent, ""); Stream_write_string(f, "}\n"); { @@ -704,7 +860,7 @@ instrument_expression(catch->pn_kid1->pn_expr, f); } Stream_write_string(f, ") {\n"); - instrument_statement(catch->pn_kid3, f, indent + 2); + instrument_statement(catch->pn_kid3, f, indent + 2, false); Stream_printf(f, "%*s", indent, ""); Stream_write_string(f, "}\n"); } @@ -712,7 +868,7 @@ if (node->pn_kid3) { Stream_printf(f, "%*s", indent, ""); Stream_write_string(f, "finally {\n"); - instrument_statement(node->pn_kid3, f, indent + 2); + instrument_statement(node->pn_kid3, f, indent + 2, false); Stream_printf(f, "%*s", indent, ""); Stream_write_string(f, "}\n"); } @@ -738,7 +894,7 @@ Stream_write_string(f, "with ("); instrument_expression(node->pn_left, f); Stream_write_string(f, ") {\n"); - instrument_statement(node->pn_right, f, indent + 2); + instrument_statement(node->pn_right, f, indent + 2, false); Stream_printf(f, "%*s", indent, ""); Stream_write_string(f, "}\n"); break; @@ -776,7 +932,7 @@ /* ... use output_statement instead of instrument_statement. */ - output_statement(node->pn_expr, f, indent); + output_statement(node->pn_expr, f, indent, false); break; default: fatal("unsupported node type in file %s: %d", file_id, node->pn_type); @@ -788,9 +944,13 @@ TOK_FUNCTION is handled as a statement and as an expression. TOK_EXPORT, TOK_IMPORT are not handled. */ -static void instrument_statement(JSParseNode * node, Stream * f, int indent) { +static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) { if (node->pn_type != TOK_LC) { - int line = node->pn_pos.begin.lineno; + uint16_t line = node->pn_pos.begin.lineno; + if (line > num_lines) { + fatal("%s: script contains more than 65,535 lines", file_id); + } + /* the root node has line number 0 */ if (line != 0) { Stream_printf(f, "%*s", indent, ""); @@ -798,43 +958,145 @@ lines[line - 1] = 1; } } - output_statement(node, f, indent); + output_statement(node, f, indent, is_jscoverage_if); +} + +static bool characters_start_with(const jschar * characters, size_t line_start, size_t line_end, const char * prefix) { + const jschar * characters_end = characters + line_end; + const jschar * cp = characters + line_start; + const char * bp = prefix; + for (;;) { + if (*bp == '\0') { + return true; + } + else if (cp == characters_end) { + return false; + } + else if (*cp != *bp) { + return false; + } + bp++; + cp++; + } +} + +static bool characters_are_white_space(const jschar * characters, size_t line_start, size_t line_end) { + /* XXX - other Unicode space */ + const jschar * end = characters + line_end; + for (const jschar * p = characters + line_start; p < end; p++) { + jschar c = *p; + if (c == 0x9 || c == 0xB || c == 0xC || c == 0x20 || c == 0xA0) { + continue; + } + else { + return false; + } + } + return true; +} + +static void error_reporter(JSContext * context, const char * message, JSErrorReport * report) { + fprintf(stderr, "jscoverage: parse error: line %u: %s\n", report->lineno, message); } -void jscoverage_instrument_js(const char * id, Stream * input, Stream * output) { +void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) { file_id = id; /* scan the javascript */ - size_t input_length = input->length; - jschar * base = js_InflateString(context, input->data, &input_length); - if (base == NULL) { - fatal("out of memory"); - } - JSTokenStream * token_stream = js_NewTokenStream(context, base, input_length, NULL, 1, NULL); + JSTokenStream * token_stream = js_NewTokenStream(context, characters, num_characters, NULL, 1, NULL); if (token_stream == NULL) { fatal("cannot create token stream from file: %s", file_id); } /* parse the javascript */ + JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter); JSParseNode * node = js_ParseTokenStream(context, global, token_stream); if (node == NULL) { + js_ReportUncaughtException(context); fatal("parse error in file: %s", file_id); } - int num_lines = node->pn_pos.end.lineno; + JS_SetErrorReporter(context, old_error_reporter); + num_lines = node->pn_pos.end.lineno; lines = xmalloc(num_lines); - for (int i = 0; i < num_lines; i++) { + for (unsigned int i = 0; i < num_lines; i++) { lines[i] = 0; } + /* search code for conditionals */ + exclusive_directives = xnew(bool, num_lines); + for (unsigned int i = 0; i < num_lines; i++) { + exclusive_directives[i] = false; + } + + bool has_conditionals = false; + struct IfDirective * if_directives = NULL; + size_t line_number = 0; + size_t i = 0; + while (i < num_characters) { + if (line_number == UINT16_MAX) { + fatal("%s: script has more than 65,535 lines", file_id); + } + line_number++; + size_t line_start = i; + jschar c; + bool done = false; + while (! done && i < num_characters) { + c = characters[i]; + switch (c) { + case '\r': + case '\n': + case 0x2028: + case 0x2029: + done = true; + break; + default: + i++; + } + } + size_t line_end = i; + if (i < num_characters) { + i++; + if (c == '\r' && i < num_characters && characters[i] == '\n') { + i++; + } + } + + if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) { + has_conditionals = true; + + if (characters_are_white_space(characters, line_start + 16, line_end)) { + exclusive_directives[line_number - 1] = true; + } + else { + struct IfDirective * if_directive = xnew(struct IfDirective, 1); + if_directive->condition_start = characters + line_start + 16; + if_directive->condition_end = characters + line_end; + if_directive->start_line = line_number; + if_directive->end_line = 0; + if_directive->next = if_directives; + if_directives = if_directive; + } + } + else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) { + for (struct IfDirective * p = if_directives; p != NULL; p = p->next) { + if (p->end_line == 0) { + p->end_line = line_number; + break; + } + } + } + } + /* - An instrumented JavaScript file has 3 sections: + An instrumented JavaScript file has 4 sections: 1. initialization 2. instrumented source code - 3. original source code (TODO) + 3. conditionals + 4. original source code */ Stream * instrumented = Stream_new(0); - instrument_statement(node, instrumented, 0); + instrument_statement(node, instrumented, 0, false); /* write line number info to the output */ Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n"); @@ -850,13 +1112,444 @@ Stream_write_string(output, "}\n"); free(lines); lines = NULL; + free(exclusive_directives); + exclusive_directives = NULL; + + /* conditionals */ + if (has_conditionals) { + Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id); + } /* copy the instrumented source code to the output */ Stream_write(output, instrumented->data, instrumented->length); + /* conditionals */ + for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) { + Stream_write_string(output, "if (!("); + print_javascript(if_directive->condition_start, if_directive->condition_end - if_directive->condition_start, output); + Stream_write_string(output, ")) {\n"); + Stream_printf(output, " _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line); + Stream_write_string(output, "}\n"); + } + + /* free */ + while (if_directives != NULL) { + struct IfDirective * if_directive = if_directives; + if_directives = if_directives->next; + free(if_directive); + } + + /* copy the original source to the output */ + Stream_printf(output, "_$jscoverage['%s'].source = ", file_id); + jscoverage_write_source(id, characters, num_characters, output); + Stream_printf(output, ";\n"); + Stream_delete(instrumented); + file_id = NULL; +} + +void jscoverage_write_source(const char * id, const jschar * characters, size_t num_characters, Stream * output) { + Stream_write_string(output, "["); + if (jscoverage_highlight) { + Stream * highlighted_stream = Stream_new(num_characters); + jscoverage_highlight_js(context, id, characters, num_characters, highlighted_stream); + size_t i = 0; + while (i < highlighted_stream->length) { + if (i > 0) { + Stream_write_char(output, ','); + } + + Stream_write_char(output, '"'); + bool done = false; + while (! done) { + char c = highlighted_stream->data[i]; + switch (c) { + case 0x8: + /* backspace */ + Stream_write_string(output, "\\b"); + break; + case 0x9: + /* horizontal tab */ + Stream_write_string(output, "\\t"); + break; + case 0xa: + /* line feed (new line) */ + done = true; + break; + case 0xb: + /* vertical tab */ + Stream_write_string(output, "\\v"); + break; + case 0xc: + /* form feed */ + Stream_write_string(output, "\\f"); + break; + case 0xd: + /* carriage return */ + done = true; + if (i + 1 < highlighted_stream->length && highlighted_stream->data[i + 1] == '\n') { + i++; + } + break; + case '"': + Stream_write_string(output, "\\\""); + break; + case '\\': + Stream_write_string(output, "\\\\"); + break; + default: + Stream_write_char(output, c); + break; + } + i++; + if (i >= highlighted_stream->length) { + done = true; + } + } + Stream_write_char(output, '"'); + } + Stream_delete(highlighted_stream); + } + else { + size_t i = 0; + while (i < num_characters) { + if (i > 0) { + Stream_write_char(output, ','); + } + + Stream_write_char(output, '"'); + bool done = false; + while (! done) { + jschar c = characters[i]; + switch (c) { + case 0x8: + /* backspace */ + Stream_write_string(output, "\\b"); + break; + case 0x9: + /* horizontal tab */ + Stream_write_string(output, "\\t"); + break; + case 0xa: + /* line feed (new line) */ + done = true; + break; + case 0xb: + /* vertical tab */ + Stream_write_string(output, "\\v"); + break; + case 0xc: + /* form feed */ + Stream_write_string(output, "\\f"); + break; + case 0xd: + /* carriage return */ + done = true; + if (i + 1 < num_characters && characters[i + 1] == '\n') { + i++; + } + break; + case '"': + Stream_write_string(output, "\\\""); + break; + case '\\': + Stream_write_string(output, "\\\\"); + break; + case '&': + Stream_write_string(output, "&"); + break; + case '<': + Stream_write_string(output, "<"); + break; + case '>': + Stream_write_string(output, ">"); + break; + case 0x2028: + case 0x2029: + done = true; + break; + default: + if (32 <= c && c <= 126) { + Stream_write_char(output, c); + } + else { + Stream_printf(output, "&#%d;", c); + } + break; + } + i++; + if (i >= num_characters) { + done = true; + } + } + Stream_write_char(output, '"'); + } + } + Stream_write_string(output, "]"); +} + +void jscoverage_copy_resources(const char * destination_directory) { + copy_resource("jscoverage.html", destination_directory); + copy_resource("jscoverage.css", destination_directory); + copy_resource("jscoverage.js", destination_directory); + copy_resource("jscoverage-ie.css", destination_directory); + copy_resource("jscoverage-throbber.gif", destination_directory); + copy_resource("jscoverage-highlight.css", destination_directory); +} + +/* +coverage reports +*/ + +struct FileCoverageList { + FileCoverage * file_coverage; + struct FileCoverageList * next; +}; + +struct Coverage { + JSHashTable * coverage_table; + struct FileCoverageList * coverage_list; +}; + +static int compare_strings(const void * p1, const void * p2) { + return strcmp(p1, p2) == 0; +} + +Coverage * Coverage_new(void) { + Coverage * result = xmalloc(sizeof(Coverage)); + result->coverage_table = JS_NewHashTable(1024, JS_HashString, compare_strings, NULL, NULL, NULL); + if (result->coverage_table == NULL) { + fatal("cannot create hash table"); + } + result->coverage_list = NULL; + return result; +} + +void Coverage_delete(Coverage * coverage) { + JS_HashTableDestroy(coverage->coverage_table); + struct FileCoverageList * p = coverage->coverage_list; + while (p != NULL) { + free(p->file_coverage->coverage_lines); + if (p->file_coverage->source_lines != NULL) { + for (uint32 i = 0; i < p->file_coverage->num_source_lines; i++) { + free(p->file_coverage->source_lines[i]); + } + free(p->file_coverage->source_lines); + } + free(p->file_coverage->id); + free(p->file_coverage); + struct FileCoverageList * q = p; + p = p->next; + free(q); + } + free(coverage); +} + +struct EnumeratorArg { + CoverageForeachFunction f; + void * p; +}; + +static intN enumerator(JSHashEntry * entry, intN i, void * arg) { + struct EnumeratorArg * enumerator_arg = arg; + enumerator_arg->f(entry->value, i, enumerator_arg->p); + return 0; +} + +void Coverage_foreach_file(Coverage * coverage, CoverageForeachFunction f, void * p) { + struct EnumeratorArg enumerator_arg; + enumerator_arg.f = f; + enumerator_arg.p = p; + JS_HashTableEnumerateEntries(coverage->coverage_table, enumerator, &enumerator_arg); +} + +int jscoverage_parse_json(Coverage * coverage, const uint8_t * json, size_t length) { + jschar * base = js_InflateString(context, (char *) json, &length); + if (base == NULL) { + fatal("out of memory"); + } + + jschar * parenthesized_json = xnew(jschar, addst(length, 2)); + parenthesized_json[0] = '('; + memcpy(parenthesized_json + 1, base, mulst(length, sizeof(jschar))); + parenthesized_json[length + 1] = ')'; + JS_free(context, base); - file_id = NULL; + JSTokenStream * token_stream = js_NewTokenStream(context, parenthesized_json, length + 2, NULL, 1, NULL); + if (token_stream == NULL) { + fatal("cannot create token stream"); + } + + JSParseNode * root = js_ParseTokenStream(context, global, token_stream); + free(parenthesized_json); + if (root == NULL) { + return -1; + } + + /* root node must be TOK_LC */ + if (root->pn_type != TOK_LC) { + return -1; + } + JSParseNode * semi = root->pn_u.list.head; + + /* the list must be TOK_SEMI and it must contain only one element */ + if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) { + return -1; + } + JSParseNode * parenthesized = semi->pn_kid; + + /* this must be a parenthesized expression */ + if (parenthesized->pn_type != TOK_RP) { + return -1; + } + JSParseNode * object = parenthesized->pn_kid; + + /* this must be an object literal */ + if (object->pn_type != TOK_RC) { + return -1; + } + + for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) { + /* every element of this list must be TOK_COLON */ + if (p->pn_type != TOK_COLON) { + return -1; + } + + /* the key must be a string representing the file */ + JSParseNode * key = p->pn_left; + if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) { + return -1; + } + char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom)); + + /* the value must be an object literal OR an array */ + JSParseNode * value = p->pn_right; + if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) { + return -1; + } + + JSParseNode * array = NULL; + JSParseNode * source = NULL; + if (value->pn_type == TOK_RB) { + /* an array */ + array = value; + } + else if (value->pn_type == TOK_RC) { + /* an object literal */ + if (value->pn_count != 2) { + return -1; + } + for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) { + if (element->pn_type != TOK_COLON) { + return -1; + } + JSParseNode * left = element->pn_left; + if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) { + return -1; + } + const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom)); + if (strcmp(s, "coverage") == 0) { + array = element->pn_right; + if (array->pn_type != TOK_RB) { + return -1; + } + } + else if (strcmp(s, "source") == 0) { + source = element->pn_right; + if (source->pn_type != TOK_RB) { + return -1; + } + } + else { + return -1; + } + } + } + else { + return -1; + } + + if (array == NULL) { + return -1; + } + + /* look up the file in the coverage table */ + FileCoverage * file_coverage = JS_HashTableLookup(coverage->coverage_table, id_bytes); + if (file_coverage == NULL) { + /* not there: create a new one */ + char * id = xstrdup(id_bytes); + file_coverage = xmalloc(sizeof(FileCoverage)); + file_coverage->id = id; + file_coverage->num_coverage_lines = array->pn_count; + file_coverage->coverage_lines = xnew(int, array->pn_count); + file_coverage->source_lines = NULL; + + /* set coverage for all lines */ + uint32 i = 0; + for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) { + if (element->pn_type == TOK_NUMBER) { + file_coverage->coverage_lines[i] = (int) element->pn_dval; + } + else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) { + file_coverage->coverage_lines[i] = -1; + } + else { + return -1; + } + } + assert(i == array->pn_count); + + /* add to the hash table */ + JS_HashTableAdd(coverage->coverage_table, id, file_coverage); + struct FileCoverageList * coverage_list = xmalloc(sizeof(struct FileCoverageList)); + coverage_list->file_coverage = file_coverage; + coverage_list->next = coverage->coverage_list; + coverage->coverage_list = coverage_list; + } + else { + /* sanity check */ + assert(strcmp(file_coverage->id, id_bytes) == 0); + if (file_coverage->num_coverage_lines != array->pn_count) { + return -2; + } + + /* merge the coverage */ + uint32 i = 0; + for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) { + if (element->pn_type == TOK_NUMBER) { + if (file_coverage->coverage_lines[i] == -1) { + return -2; + } + file_coverage->coverage_lines[i] += (int) element->pn_dval; + } + else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) { + if (file_coverage->coverage_lines[i] != -1) { + return -2; + } + } + else { + return -1; + } + } + assert(i == array->pn_count); + } + + /* if this JSON file has source, use it */ + if (file_coverage->source_lines == NULL && source != NULL) { + 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); + } + } + + return 0; }