--- trunk/instrument-js.c 2008/09/20 23:27:14 174 +++ trunk/instrument-js.c 2008/10/03 02:26:04 214 @@ -35,6 +35,8 @@ #include #include "encoding.h" +#include "global.h" +#include "highlight.h" #include "resource-manager.h" #include "util.h" @@ -48,6 +50,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); @@ -872,7 +875,11 @@ */ static void instrument_statement(JSParseNode * node, Stream * f, int indent) { 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, ""); @@ -883,20 +890,30 @@ output_statement(node, f, indent); } -void jscoverage_instrument_js(const char * id, const char * encoding, Stream * input, Stream * output) { +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++; + } +} + +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 = NULL; - int result = jscoverage_bytes_to_characters(encoding, input->data, input->length, &base, &input_length); - 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, 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); } @@ -906,7 +923,7 @@ if (node == NULL) { fatal("parse error in file: %s", file_id); } - int num_lines = node->pn_pos.end.lineno; + num_lines = node->pn_pos.end.lineno; lines = xmalloc(num_lines); for (int i = 0; i < num_lines; i++) { lines[i] = 0; @@ -939,89 +956,209 @@ /* 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 < input_length) { + while (i < num_characters) { line_number++; size_t line_start = i; - while (i < input_length && base[i] != '\r' && base[i] != '\n') { - i++; - } - size_t line_end = i; - if (i < input_length) { - if (base[i] == '\r') { - line_end = 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++; - if (i < input_length && base[i] == '\n') { - i++; - } + break; } - else if (base[i] == '\n') { - line_end = i; + } + size_t line_end = i; + if (i < num_characters) { + i++; + if (c == '\r' && i < num_characters && characters[i] == '\n') { i++; } - else { - abort(); - } } - char * line = js_DeflateString(context, base + line_start, line_end - line_start); - if (str_starts_with(line, "//#JSCOVERAGE_IF")) { + if (characters_start_with(characters, line_start, line_end, "//#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_write_string(output, "if (!("); + for (size_t j = line_start + 16; j < line_end; j++) { + jschar c = characters[j]; + if (c == '\t' || (32 <= c && c <= 126)) { + Stream_write_char(output, c); + } + else { + Stream_printf(output, "\\u%04x", c); + } + } + Stream_write_string(output, ")) {\n"); Stream_printf(output, " _$jscoverage['%s'].conditionals[%d] = ", file_id, line_number); } - else if (str_starts_with(line, "//#JSCOVERAGE_ENDIF")) { + else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) { Stream_printf(output, "%d;\n", line_number); Stream_printf(output, "}\n"); } - JS_free(context, line); } /* copy the original source to the output */ - i = 0; - while (i < input_length) { - Stream_write_string(output, "// "); - size_t line_start = i; - while (i < input_length && 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 < input_length) { - 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 < input_length && 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 +1167,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 +1202,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 +1330,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 +1354,32 @@ 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); + file_coverage->num_coverage_lines = array->pn_count; + file_coverage->coverage_lines = xnew(int, array->pn_count); if (source == NULL) { - file_coverage->source = NULL; + file_coverage->source_lines = NULL; } else { - file_coverage->source = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(source->pn_atom))); + 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); } /* 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 +1397,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 +1405,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; } } @@ -1273,8 +1422,17 @@ 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 (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); } } }