--- trunk/instrument-js.c 2008/10/03 02:26:04 214 +++ trunk/instrument-js.c 2008/10/11 22:40:31 284 @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -40,6 +41,16 @@ #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; @@ -78,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); @@ -205,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, @@ -261,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"); } @@ -620,7 +655,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); @@ -631,7 +666,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, ""); @@ -639,22 +674,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, ""); @@ -676,7 +743,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"); @@ -691,14 +758,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 ("); @@ -761,7 +828,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: @@ -774,7 +841,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"); { @@ -789,7 +856,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"); } @@ -797,7 +864,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"); } @@ -823,7 +890,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; @@ -861,7 +928,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); @@ -873,7 +940,7 @@ 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) { uint16_t line = node->pn_pos.begin.lineno; if (line > num_lines) { @@ -887,7 +954,7 @@ 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) { @@ -909,6 +976,25 @@ } } +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 uint16_t * characters, size_t num_characters, Stream * output) { file_id = id; @@ -919,49 +1005,33 @@ } /* 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); } + 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; } - /* - An instrumented JavaScript file has 3 sections: - 1. initialization - 2. instrumented source code - 3. original source code - */ - - Stream * instrumented = Stream_new(0); - instrument_statement(node, instrumented, 0); - - /* write line number info to the output */ - Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n"); - Stream_write_string(output, "if (! top._$jscoverage) {\n top._$jscoverage = {};\n}\n"); - Stream_write_string(output, "var _$jscoverage = top._$jscoverage;\n"); - Stream_printf(output, "if (! _$jscoverage['%s']) {\n", file_id); - Stream_printf(output, " _$jscoverage['%s'] = [];\n", file_id); - for (int i = 0; i < num_lines; i++) { - if (lines[i]) { - Stream_printf(output, " _$jscoverage['%s'][%d] = 0;\n", file_id, i + 1); - } + /* search code for conditionals */ + exclusive_directives = xnew(bool, num_lines); + for (unsigned int i = 0; i < num_lines; i++) { + exclusive_directives[i] = false; } - Stream_write_string(output, "}\n"); - free(lines); - lines = NULL; - - /* copy the instrumented source code to the output */ - Stream_write(output, instrumented->data, instrumented->length); - /* conditionals */ 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; @@ -977,7 +1047,6 @@ break; default: i++; - break; } } size_t line_end = i; @@ -987,30 +1056,85 @@ i++; } } + 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_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); - } + 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; } - Stream_write_string(output, ")) {\n"); - Stream_printf(output, " _$jscoverage['%s'].conditionals[%d] = ", file_id, line_number); } else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) { - Stream_printf(output, "%d;\n", line_number); - Stream_printf(output, "}\n"); + 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 4 sections: + 1. initialization + 2. instrumented source code + 3. conditionals + 4. original source code + */ + + Stream * instrumented = Stream_new(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"); + Stream_write_string(output, "if (! top._$jscoverage) {\n top._$jscoverage = {};\n}\n"); + Stream_write_string(output, "var _$jscoverage = top._$jscoverage;\n"); + Stream_printf(output, "if (! _$jscoverage['%s']) {\n", file_id); + Stream_printf(output, " _$jscoverage['%s'] = [];\n", file_id); + for (int i = 0; i < num_lines; i++) { + if (lines[i]) { + Stream_printf(output, " _$jscoverage['%s'][%d] = 0;\n", file_id, i + 1); + } + } + 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); @@ -1356,21 +1480,7 @@ file_coverage->id = id; file_coverage->num_coverage_lines = array->pn_count; file_coverage->coverage_lines = xnew(int, array->pn_count); - if (source == 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); - } + file_coverage->source_lines = NULL; /* set coverage for all lines */ uint32 i = 0; @@ -1420,20 +1530,20 @@ } } 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))); + /* 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; } - assert(i == source->pn_count); + file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom))); } + assert(i == source->pn_count); } }