--- trunk/instrument-js.c 2008/10/03 02:26:04 214 +++ trunk/instrument-js.c 2008/10/24 16:13:57 339 @@ -26,9 +26,12 @@ #include #include +#include #include +#include #include #include +#include #include #include #include @@ -40,9 +43,20 @@ #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; +static JSVersion js_version = JSVERSION_ECMA_3; /* JSParseNode objects store line numbers starting from 1. @@ -52,6 +66,10 @@ static char * lines = NULL; static uint16_t num_lines = 0; +void jscoverage_set_js_version(const char * version) { + js_version = atoi(version); +} + void jscoverage_init(void) { runtime = JS_NewRuntime(8L * 1024L * 1024L); if (runtime == NULL) { @@ -63,6 +81,8 @@ fatal("cannot create context"); } + JS_SetVersion(context, js_version); + global = JS_NewObject(context, NULL, NULL, NULL); if (global == NULL) { fatal("cannot create global object"); @@ -78,6 +98,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); @@ -112,9 +156,12 @@ case 0xa: Stream_write_string(f, "\\n"); break; + /* IE doesn't support this */ + /* case 0xb: Stream_write_string(f, "\\v"); break; + */ case 0xc: Stream_write_string(f, "\\f"); break; @@ -161,6 +208,10 @@ static const char * get_op(uint8 op) { switch(op) { + case JSOP_OR: + return "||"; + case JSOP_AND: + return "&&"; case JSOP_BITOR: return "|"; case JSOP_BITXOR: @@ -171,9 +222,9 @@ return "=="; case JSOP_NE: return "!="; - case JSOP_NEW_EQ: + case JSOP_STRICTEQ: return "==="; - case JSOP_NEW_NE: + case JSOP_STRICTNE: return "!=="; case JSOP_LT: return "<"; @@ -205,7 +256,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, @@ -213,13 +264,13 @@ }; static void instrument_function(JSParseNode * node, Stream * f, int indent, enum FunctionType type) { + assert(node->pn_type == TOK_FUNCTION); assert(node->pn_arity == PN_FUNC); - assert(ATOM_IS_OBJECT(node->pn_funAtom)); - JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom); + JSObject * object = node->pn_funpob->object; assert(JS_ObjectIsFunction(context, object)); JSFunction * function = (JSFunction *) JS_GetPrivate(context, object); assert(function); - assert(object == function->object); + assert(object == &function->object); Stream_printf(f, "%*s", indent, ""); if (type == FUNCTION_NORMAL) { Stream_write_string(f, "function"); @@ -231,37 +282,32 @@ print_string_atom(function->atom, f); } - /* function parameters */ + /* + function parameters - see JS_DecompileFunction in jsapi.cpp, which calls + js_DecompileFunction in jsopcode.cpp + */ 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; + JSArenaPool pool; + JS_INIT_ARENA_POOL(&pool, "instrument_function", 256, 1, &context->scriptStackQuota); + jsuword * local_names = NULL; + if (JS_GET_LOCAL_NAME_COUNT(function)) { + local_names = js_GetLocalNameArray(context, function, &pool); + if (local_names == NULL) { + fatal("out of memory"); } - 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, ", "); } - if (ATOM_IS_STRING(params[i])) { - print_string_atom(params[i], f); - } + JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]); + print_string_atom(param, f); } + JS_FinishArenaPool(&pool); Stream_write_string(f, ") {\n"); - 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"); } @@ -336,15 +382,7 @@ instrument_expression(node->pn_kid3, f); break; case TOK_OR: - instrument_expression(node->pn_left, f); - Stream_write_string(f, " || "); - instrument_expression(node->pn_right, f); - break; case TOK_AND: - instrument_expression(node->pn_left, f); - Stream_write_string(f, " && "); - instrument_expression(node->pn_right, f); - break; case TOK_BITOR: case TOK_BITXOR: case TOK_BITAND: @@ -457,8 +495,8 @@ assert(ATOM_IS_STRING(node->pn_atom)); { JSString * s = ATOM_TO_STRING(node->pn_atom); - /* XXX - semantics changed in 1.7 */ - if (! ATOM_KEYWORD(node->pn_atom) && js_IsIdentifier(s)) { + bool is_keyword = (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF); + if (! is_keyword && js_IsIdentifier(s)) { Stream_write_char(f, '.'); print_string_atom(node->pn_atom, f); } @@ -505,13 +543,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: @@ -534,24 +576,13 @@ case TOK_STRING: print_quoted_string_atom(node->pn_atom, f); break; - case TOK_OBJECT: - switch (node->pn_op) { - case JSOP_OBJECT: - /* I assume this is JSOP_REGEXP */ - abort(); - break; - case JSOP_REGEXP: - 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; + case TOK_REGEXP: + assert(node->pn_op == JSOP_REGEXP); + { + JSObject * object = node->pn_pob->object; + jsval result; + js_regexp_toString(context, object, &result); + print_regex(result, f); } break; case TOK_NUMBER: @@ -620,7 +651,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 +662,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 +670,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 +739,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 +754,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 ("); @@ -708,7 +771,11 @@ case TOK_FOR: assert(node->pn_arity == PN_BINARY); Stream_printf(f, "%*s", indent, ""); - Stream_write_string(f, "for ("); + Stream_write_string(f, "for "); + if (node->pn_iflags & JSITER_FOREACH) { + Stream_write_string(f, "each "); + } + Stream_write_char(f, '('); switch (node->pn_left->pn_type) { case TOK_IN: /* for/in */ @@ -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,22 +841,25 @@ 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"); - { - for (JSParseNode * catch = node->pn_kid2; catch != NULL; catch = catch->pn_kid2) { + if (node->pn_kid2) { + assert(node->pn_kid2->pn_type == TOK_RESERVED); + for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) { + assert(scope->pn_type == TOK_LEXICALSCOPE); + JSParseNode * catch = scope->pn_expr; assert(catch->pn_type == TOK_CATCH); Stream_printf(f, "%*s", indent, ""); Stream_write_string(f, "catch ("); assert(catch->pn_kid1->pn_arity == PN_NAME); print_string_atom(catch->pn_kid1->pn_atom, f); - if (catch->pn_kid1->pn_expr) { + if (catch->pn_kid2) { Stream_write_string(f, " if "); - instrument_expression(catch->pn_kid1->pn_expr, f); + instrument_expression(catch->pn_kid2, 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 +867,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 +893,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 +931,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 +943,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 +957,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,59 +979,60 @@ } } +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; - /* scan the javascript */ - JSTokenStream * token_stream = js_NewTokenStream(context, characters, num_characters, NULL, 1, NULL); - if (token_stream == NULL) { + /* parse the javascript */ + JSParseContext parse_context; + if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) { fatal("cannot create token stream from file: %s", file_id); } - - /* parse the javascript */ - JSParseNode * node = js_ParseTokenStream(context, global, token_stream); + JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter); + JSParseNode * node = js_ParseScript(context, global, &parse_context); 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 +1048,6 @@ break; default: i++; - break; } } size_t line_end = i; @@ -987,30 +1057,86 @@ 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); + js_FinishParseContext(context, &parse_context); + + /* 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); @@ -1049,10 +1175,12 @@ /* line feed (new line) */ done = true; break; + /* IE doesn't support this */ + /* case 0xb: - /* vertical tab */ Stream_write_string(output, "\\v"); break; + */ case 0xc: /* form feed */ Stream_write_string(output, "\\f"); @@ -1107,10 +1235,12 @@ /* line feed (new line) */ done = true; break; + /* IE doesn't support this */ + /* case 0xb: - /* vertical tab */ Stream_write_string(output, "\\v"); break; + */ case 0xc: /* form feed */ Stream_write_string(output, "\\f"); @@ -1237,6 +1367,8 @@ } int jscoverage_parse_json(Coverage * coverage, const uint8_t * json, size_t length) { + int result = 0; + jschar * base = js_InflateString(context, (char *) json, &length); if (base == NULL) { fatal("out of memory"); @@ -1249,57 +1381,65 @@ JS_free(context, base); - JSTokenStream * token_stream = js_NewTokenStream(context, parenthesized_json, length + 2, NULL, 1, NULL); - if (token_stream == NULL) { - fatal("cannot create token stream"); + JSParseContext parse_context; + if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) { + free(parenthesized_json); + return -1; } - - JSParseNode * root = js_ParseTokenStream(context, global, token_stream); + JSParseNode * root = js_ParseScript(context, global, &parse_context); free(parenthesized_json); if (root == NULL) { - return -1; + result = -1; + goto done; } /* root node must be TOK_LC */ if (root->pn_type != TOK_LC) { - return -1; + result = -1; + goto done; } 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; + result = -1; + goto done; } JSParseNode * parenthesized = semi->pn_kid; /* this must be a parenthesized expression */ if (parenthesized->pn_type != TOK_RP) { - return -1; + result = -1; + goto done; } JSParseNode * object = parenthesized->pn_kid; /* this must be an object literal */ if (object->pn_type != TOK_RC) { - return -1; + result = -1; + goto done; } 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; + result = -1; + goto done; } /* 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; + result = -1; + goto done; } 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; + result = -1; + goto done; } JSParseNode * array = NULL; @@ -1311,40 +1451,48 @@ else if (value->pn_type == TOK_RC) { /* an object literal */ if (value->pn_count != 2) { - return -1; + result = -1; + goto done; } for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) { if (element->pn_type != TOK_COLON) { - return -1; + result = -1; + goto done; } JSParseNode * left = element->pn_left; if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) { - return -1; + result = -1; + goto done; } 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; + result = -1; + goto done; } } else if (strcmp(s, "source") == 0) { source = element->pn_right; if (source->pn_type != TOK_RB) { - return -1; + result = -1; + goto done; } } else { - return -1; + result = -1; + goto done; } } } else { - return -1; + result = -1; + goto done; } if (array == NULL) { - return -1; + result = -1; + goto done; } /* look up the file in the coverage table */ @@ -1356,21 +1504,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; @@ -1382,7 +1516,8 @@ file_coverage->coverage_lines[i] = -1; } else { - return -1; + result = -1; + goto done; } } assert(i == array->pn_count); @@ -1398,7 +1533,8 @@ /* sanity check */ assert(strcmp(file_coverage->id, id_bytes) == 0); if (file_coverage->num_coverage_lines != array->pn_count) { - return -2; + result = -2; + goto done; } /* merge the coverage */ @@ -1406,36 +1542,42 @@ 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; + result = -2; + goto done; } 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; + result = -2; + goto done; } } else { - return -1; + result = -1; + goto done; } } 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) { + result = -1; + goto done; } - assert(i == source->pn_count); + file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom))); } + assert(i == source->pn_count); } } - return 0; +done: + js_FinishParseContext(context, &parse_context); + return result; }