--- trunk/instrument-js.c 2008/09/20 23:29:42 176 +++ trunk/instrument-js.c 2008/10/12 16:41:57 293 @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -35,9 +36,21 @@ #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; @@ -48,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); @@ -75,6 +89,30 @@ 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) { size_t length = JSSTRING_LENGTH(s); jschar * characters = JSSTRING_CHARS(s); @@ -202,7 +240,7 @@ } 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); enum FunctionType { FUNCTION_NORMAL, @@ -258,7 +296,7 @@ free(params); /* function body */ - instrument_statement(node->pn_body, f, indent + 2); + instrument_statement(node->pn_body, f, indent + 2, false); Stream_write_string(f, "}\n"); } @@ -502,13 +540,17 @@ /* check whether this is a getter or setter */ switch (p->pn_op) { 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; case JSOP_SETTER: - Stream_write_string(f, "set "); + 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: @@ -617,7 +659,7 @@ } } -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, FUNCTION_NORMAL); @@ -628,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, ""); @@ -636,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, ""); @@ -673,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"); @@ -688,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 ("); @@ -758,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: @@ -771,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"); { @@ -786,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"); } @@ -794,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"); } @@ -820,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; @@ -858,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); @@ -870,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, ""); @@ -880,47 +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, const char * encoding, 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 num_characters = input->length; - jschar * base = NULL; - int result = jscoverage_bytes_to_characters(encoding, input->data, input->length, &base, &num_characters); - if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) { - fatal("encoding %s not supported in file %s", encoding, id); - } - else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) { - fatal("error decoding %s in file %s", encoding, id); - } - JSTokenStream * token_stream = js_NewTokenStream(context, base, num_characters, 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 + 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"); @@ -936,92 +1112,181 @@ 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); - Stream_write_char(output, '\n'); /* conditionals */ - bool has_conditionals = false; - size_t line_number = 0; - size_t i = 0; - while (i < num_characters) { - line_number++; - size_t line_start = i; - while (i < num_characters && base[i] != '\r' && base[i] != '\n') { - i++; - } - size_t line_end = i; - if (i < num_characters) { - if (base[i] == '\r') { - line_end = i; - i++; - if (i < num_characters && base[i] == '\n') { - i++; - } - } - else if (base[i] == '\n') { - line_end = i; - i++; - } - else { - abort(); - } - } - char * line = js_DeflateString(context, base + line_start, line_end - line_start); - if (str_starts_with(line, "//#JSCOVERAGE_IF")) { - if (! has_conditionals) { - has_conditionals = true; - Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id); - } - Stream_printf(output, "if (!%s) {\n", line + 16); - Stream_printf(output, " _$jscoverage['%s'].conditionals[%d] = ", file_id, line_number); - } - else if (str_starts_with(line, "//#JSCOVERAGE_ENDIF")) { - Stream_printf(output, "%d;\n", line_number); - Stream_printf(output, "}\n"); - } - JS_free(context, line); + 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 */ - i = 0; - while (i < num_characters) { - Stream_write_string(output, "// "); - size_t line_start = i; - while (i < num_characters && base[i] != '\r' && base[i] != '\n') { - i++; - } + Stream_printf(output, "_$jscoverage['%s'].source = ", file_id); + jscoverage_write_source(id, characters, num_characters, output); + Stream_printf(output, ";\n"); - size_t line_end = i; - if (i < num_characters) { - if (base[i] == '\r') { - line_end = i; + 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 < num_characters && base[i] == '\n') { - i++; + if (i >= highlighted_stream->length) { + done = true; } } - else if (base[i] == '\n') { - line_end = i; + 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; + } } - else { - abort(); - } + Stream_write_char(output, '"'); } - - char * line = js_DeflateString(context, base + line_start, line_end - line_start); - Stream_write_string(output, line); - Stream_write_char(output, '\n'); - JS_free(context, line); } - - Stream_delete(instrumented); - - JS_free(context, base); - - file_id = NULL; + Stream_write_string(output, "]"); } void jscoverage_copy_resources(const char * destination_directory) { @@ -1030,9 +1295,7 @@ copy_resource("jscoverage.js", destination_directory); copy_resource("jscoverage-ie.css", destination_directory); copy_resource("jscoverage-throbber.gif", destination_directory); - copy_resource("jscoverage-sh_main.js", destination_directory); - copy_resource("jscoverage-sh_javascript.js", destination_directory); - copy_resource("jscoverage-sh_nedit.css", destination_directory); + copy_resource("jscoverage-highlight.css", destination_directory); } /* @@ -1067,8 +1330,13 @@ JS_HashTableDestroy(coverage->coverage_table); struct FileCoverageList * p = coverage->coverage_list; while (p != NULL) { - free(p->file_coverage->lines); - free(p->file_coverage->source); + 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; @@ -1190,7 +1458,7 @@ } else if (strcmp(s, "source") == 0) { source = element->pn_right; - if (source->pn_type != TOK_STRING || ! ATOM_IS_STRING(source->pn_atom)) { + if (source->pn_type != TOK_RB) { return -1; } } @@ -1214,23 +1482,18 @@ char * id = xstrdup(id_bytes); file_coverage = xmalloc(sizeof(FileCoverage)); file_coverage->id = id; - file_coverage->num_lines = array->pn_count - 1; - file_coverage->lines = xnew(int, array->pn_count); - if (source == NULL) { - file_coverage->source = NULL; - } - else { - file_coverage->source = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(source->pn_atom))); - } + 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->lines[i] = (int) element->pn_dval; + file_coverage->coverage_lines[i] = (int) element->pn_dval; } else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) { - file_coverage->lines[i] = -1; + file_coverage->coverage_lines[i] = -1; } else { return -1; @@ -1248,7 +1511,7 @@ else { /* sanity check */ assert(strcmp(file_coverage->id, id_bytes) == 0); - if (file_coverage->num_lines != array->pn_count - 1) { + if (file_coverage->num_coverage_lines != array->pn_count) { return -2; } @@ -1256,13 +1519,13 @@ 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->lines[i] == -1) { + if (file_coverage->coverage_lines[i] == -1) { return -2; } - file_coverage->lines[i] += (int) element->pn_dval; + file_coverage->coverage_lines[i] += (int) element->pn_dval; } else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) { - if (file_coverage->lines[i] != -1) { + if (file_coverage->coverage_lines[i] != -1) { return -2; } } @@ -1271,11 +1534,20 @@ } } assert(i == array->pn_count); + } - /* if this JSON file has source, use it */ - if (file_coverage->source == NULL && source != NULL) { - file_coverage->source = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(source->pn_atom))); + /* 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); } }