26 |
#include <string.h> |
#include <string.h> |
27 |
|
|
28 |
#include <jsapi.h> |
#include <jsapi.h> |
29 |
|
#include <jsarena.h> |
30 |
#include <jsatom.h> |
#include <jsatom.h> |
31 |
#include <jsexn.h> |
#include <jsexn.h> |
32 |
#include <jsfun.h> |
#include <jsfun.h> |
33 |
#include <jsinterp.h> |
#include <jsinterp.h> |
34 |
|
#include <jsiter.h> |
35 |
#include <jsparse.h> |
#include <jsparse.h> |
36 |
#include <jsregexp.h> |
#include <jsregexp.h> |
37 |
#include <jsscope.h> |
#include <jsscope.h> |
56 |
static JSRuntime * runtime = NULL; |
static JSRuntime * runtime = NULL; |
57 |
static JSContext * context = NULL; |
static JSContext * context = NULL; |
58 |
static JSObject * global = NULL; |
static JSObject * global = NULL; |
59 |
|
static JSVersion js_version = JSVERSION_ECMA_3; |
60 |
|
|
61 |
/* |
/* |
62 |
JSParseNode objects store line numbers starting from 1. |
JSParseNode objects store line numbers starting from 1. |
66 |
static char * lines = NULL; |
static char * lines = NULL; |
67 |
static uint16_t num_lines = 0; |
static uint16_t num_lines = 0; |
68 |
|
|
69 |
|
void jscoverage_set_js_version(const char * version) { |
70 |
|
js_version = atoi(version); |
71 |
|
} |
72 |
|
|
73 |
void jscoverage_init(void) { |
void jscoverage_init(void) { |
74 |
runtime = JS_NewRuntime(8L * 1024L * 1024L); |
runtime = JS_NewRuntime(8L * 1024L * 1024L); |
75 |
if (runtime == NULL) { |
if (runtime == NULL) { |
81 |
fatal("cannot create context"); |
fatal("cannot create context"); |
82 |
} |
} |
83 |
|
|
84 |
|
JS_SetVersion(context, js_version); |
85 |
|
|
86 |
global = JS_NewObject(context, NULL, NULL, NULL); |
global = JS_NewObject(context, NULL, NULL, NULL); |
87 |
if (global == NULL) { |
if (global == NULL) { |
88 |
fatal("cannot create global object"); |
fatal("cannot create global object"); |
208 |
|
|
209 |
static const char * get_op(uint8 op) { |
static const char * get_op(uint8 op) { |
210 |
switch(op) { |
switch(op) { |
211 |
|
case JSOP_OR: |
212 |
|
return "||"; |
213 |
|
case JSOP_AND: |
214 |
|
return "&&"; |
215 |
case JSOP_BITOR: |
case JSOP_BITOR: |
216 |
return "|"; |
return "|"; |
217 |
case JSOP_BITXOR: |
case JSOP_BITXOR: |
222 |
return "=="; |
return "=="; |
223 |
case JSOP_NE: |
case JSOP_NE: |
224 |
return "!="; |
return "!="; |
225 |
case JSOP_NEW_EQ: |
case JSOP_STRICTEQ: |
226 |
return "==="; |
return "==="; |
227 |
case JSOP_NEW_NE: |
case JSOP_STRICTNE: |
228 |
return "!=="; |
return "!=="; |
229 |
case JSOP_LT: |
case JSOP_LT: |
230 |
return "<"; |
return "<"; |
264 |
}; |
}; |
265 |
|
|
266 |
static void instrument_function(JSParseNode * node, Stream * f, int indent, enum FunctionType type) { |
static void instrument_function(JSParseNode * node, Stream * f, int indent, enum FunctionType type) { |
267 |
|
assert(node->pn_type == TOK_FUNCTION); |
268 |
assert(node->pn_arity == PN_FUNC); |
assert(node->pn_arity == PN_FUNC); |
269 |
assert(ATOM_IS_OBJECT(node->pn_funAtom)); |
JSObject * object = node->pn_funpob->object; |
|
JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom); |
|
270 |
assert(JS_ObjectIsFunction(context, object)); |
assert(JS_ObjectIsFunction(context, object)); |
271 |
JSFunction * function = (JSFunction *) JS_GetPrivate(context, object); |
JSFunction * function = (JSFunction *) JS_GetPrivate(context, object); |
272 |
assert(function); |
assert(function); |
273 |
assert(object == function->object); |
assert(object == &function->object); |
274 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
275 |
if (type == FUNCTION_NORMAL) { |
if (type == FUNCTION_NORMAL) { |
276 |
Stream_write_string(f, "function"); |
Stream_write_string(f, "function"); |
282 |
print_string_atom(function->atom, f); |
print_string_atom(function->atom, f); |
283 |
} |
} |
284 |
|
|
285 |
/* function parameters */ |
/* |
286 |
|
function parameters - see JS_DecompileFunction in jsapi.cpp, which calls |
287 |
|
js_DecompileFunction in jsopcode.cpp |
288 |
|
*/ |
289 |
Stream_write_string(f, "("); |
Stream_write_string(f, "("); |
290 |
JSAtom ** params = xnew(JSAtom *, function->nargs); |
JSArenaPool pool; |
291 |
for (int i = 0; i < function->nargs; i++) { |
JS_INIT_ARENA_POOL(&pool, "instrument_function", 256, 1, &context->scriptStackQuota); |
292 |
/* initialize to NULL for sanity check */ |
jsuword * local_names = NULL; |
293 |
params[i] = NULL; |
if (JS_GET_LOCAL_NAME_COUNT(function)) { |
294 |
} |
local_names = js_GetLocalNameArray(context, function, &pool); |
295 |
JSScope * scope = OBJ_SCOPE(object); |
if (local_names == NULL) { |
296 |
for (JSScopeProperty * scope_property = SCOPE_LAST_PROP(scope); scope_property != NULL; scope_property = scope_property->parent) { |
fatal("out of memory"); |
|
if (scope_property->getter != js_GetArgument) { |
|
|
continue; |
|
297 |
} |
} |
|
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); |
|
298 |
} |
} |
299 |
for (int i = 0; i < function->nargs; i++) { |
for (int i = 0; i < function->nargs; i++) { |
|
assert(params[i] != NULL); |
|
300 |
if (i > 0) { |
if (i > 0) { |
301 |
Stream_write_string(f, ", "); |
Stream_write_string(f, ", "); |
302 |
} |
} |
303 |
if (ATOM_IS_STRING(params[i])) { |
JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]); |
304 |
print_string_atom(params[i], f); |
print_string_atom(param, f); |
|
} |
|
305 |
} |
} |
306 |
|
JS_FinishArenaPool(&pool); |
307 |
Stream_write_string(f, ") {\n"); |
Stream_write_string(f, ") {\n"); |
|
free(params); |
|
308 |
|
|
309 |
/* function body */ |
/* function body */ |
310 |
instrument_statement(node->pn_body, f, indent + 2, false); |
instrument_statement(node->pn_body, f, indent + 2, false); |
324 |
Stream_write_char(f, ')'); |
Stream_write_char(f, ')'); |
325 |
} |
} |
326 |
|
|
327 |
|
static void instrument_declarations(JSParseNode * list, Stream * f) { |
328 |
|
assert(list->pn_arity == PN_LIST); |
329 |
|
for (JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) { |
330 |
|
switch (p->pn_type) { |
331 |
|
case TOK_NAME: |
332 |
|
assert(p->pn_arity == PN_NAME); |
333 |
|
if (p != list->pn_head) { |
334 |
|
Stream_write_string(f, ", "); |
335 |
|
} |
336 |
|
print_string_atom(p->pn_atom, f); |
337 |
|
if (p->pn_expr != NULL) { |
338 |
|
Stream_write_string(f, " = "); |
339 |
|
instrument_expression(p->pn_expr, f); |
340 |
|
} |
341 |
|
break; |
342 |
|
case TOK_ASSIGN: |
343 |
|
case TOK_RB: |
344 |
|
case TOK_RC: |
345 |
|
/* destructuring */ |
346 |
|
instrument_expression(p, f); |
347 |
|
break; |
348 |
|
default: |
349 |
|
abort(); |
350 |
|
break; |
351 |
|
} |
352 |
|
} |
353 |
|
} |
354 |
|
|
355 |
/* |
/* |
356 |
See <Expressions> in jsparse.h. |
See <Expressions> in jsparse.h. |
357 |
TOK_FUNCTION is handled as a statement and as an expression. |
TOK_FUNCTION is handled as a statement and as an expression. |
410 |
instrument_expression(node->pn_kid3, f); |
instrument_expression(node->pn_kid3, f); |
411 |
break; |
break; |
412 |
case TOK_OR: |
case TOK_OR: |
|
instrument_expression(node->pn_left, f); |
|
|
Stream_write_string(f, " || "); |
|
|
instrument_expression(node->pn_right, f); |
|
|
break; |
|
413 |
case TOK_AND: |
case TOK_AND: |
|
instrument_expression(node->pn_left, f); |
|
|
Stream_write_string(f, " && "); |
|
|
instrument_expression(node->pn_right, f); |
|
|
break; |
|
414 |
case TOK_BITOR: |
case TOK_BITOR: |
415 |
case TOK_BITXOR: |
case TOK_BITXOR: |
416 |
case TOK_BITAND: |
case TOK_BITAND: |
523 |
assert(ATOM_IS_STRING(node->pn_atom)); |
assert(ATOM_IS_STRING(node->pn_atom)); |
524 |
{ |
{ |
525 |
JSString * s = ATOM_TO_STRING(node->pn_atom); |
JSString * s = ATOM_TO_STRING(node->pn_atom); |
526 |
/* XXX - semantics changed in 1.7 */ |
bool is_keyword = (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF); |
527 |
if (! ATOM_KEYWORD(node->pn_atom) && js_IsIdentifier(s)) { |
if (! is_keyword && js_IsIdentifier(s)) { |
528 |
Stream_write_char(f, '.'); |
Stream_write_char(f, '.'); |
529 |
print_string_atom(node->pn_atom, f); |
print_string_atom(node->pn_atom, f); |
530 |
} |
} |
604 |
case TOK_STRING: |
case TOK_STRING: |
605 |
print_quoted_string_atom(node->pn_atom, f); |
print_quoted_string_atom(node->pn_atom, f); |
606 |
break; |
break; |
607 |
case TOK_OBJECT: |
case TOK_REGEXP: |
608 |
switch (node->pn_op) { |
assert(node->pn_op == JSOP_REGEXP); |
609 |
case JSOP_OBJECT: |
{ |
610 |
/* I assume this is JSOP_REGEXP */ |
JSObject * object = node->pn_pob->object; |
611 |
abort(); |
jsval result; |
612 |
break; |
js_regexp_toString(context, object, &result); |
613 |
case JSOP_REGEXP: |
print_regex(result, f); |
|
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; |
|
614 |
} |
} |
615 |
break; |
break; |
616 |
case TOK_NUMBER: |
case TOK_NUMBER: |
656 |
Stream_write_string(f, " in "); |
Stream_write_string(f, " in "); |
657 |
instrument_expression(node->pn_right, f); |
instrument_expression(node->pn_right, f); |
658 |
break; |
break; |
659 |
|
case TOK_LEXICALSCOPE: |
660 |
|
assert(node->pn_arity == PN_NAME); |
661 |
|
assert(node->pn_expr->pn_type == TOK_LET); |
662 |
|
assert(node->pn_expr->pn_arity == PN_BINARY); |
663 |
|
Stream_write_string(f, "let("); |
664 |
|
assert(node->pn_expr->pn_left->pn_type == TOK_LP); |
665 |
|
assert(node->pn_expr->pn_left->pn_arity == PN_LIST); |
666 |
|
instrument_declarations(node->pn_expr->pn_left, f); |
667 |
|
Stream_write_string(f, ") "); |
668 |
|
instrument_expression(node->pn_expr->pn_right, f); |
669 |
|
break; |
670 |
default: |
default: |
671 |
fatal("unsupported node type in file %s: %d", file_id, node->pn_type); |
fatal("unsupported node type in file %s: %d", file_id, node->pn_type); |
672 |
} |
} |
676 |
assert(node->pn_arity == PN_LIST); |
assert(node->pn_arity == PN_LIST); |
677 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
678 |
Stream_write_string(f, "var "); |
Stream_write_string(f, "var "); |
679 |
for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) { |
instrument_declarations(node, f); |
680 |
assert(p->pn_type == TOK_NAME); |
} |
681 |
assert(p->pn_arity == PN_NAME); |
|
682 |
if (p != node->pn_head) { |
static void instrument_let_definition(JSParseNode * node, Stream * f, int indent) { |
683 |
Stream_write_string(f, ", "); |
assert(node->pn_arity == PN_LIST); |
684 |
} |
Stream_printf(f, "%*s", indent, ""); |
685 |
print_string_atom(p->pn_atom, f); |
Stream_write_string(f, "let "); |
686 |
if (p->pn_expr != NULL) { |
instrument_declarations(node, f); |
|
Stream_write_string(f, " = "); |
|
|
instrument_expression(p->pn_expr, f); |
|
|
} |
|
|
} |
|
687 |
} |
} |
688 |
|
|
689 |
static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) { |
static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) { |
806 |
case TOK_FOR: |
case TOK_FOR: |
807 |
assert(node->pn_arity == PN_BINARY); |
assert(node->pn_arity == PN_BINARY); |
808 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
809 |
Stream_write_string(f, "for ("); |
Stream_write_string(f, "for "); |
810 |
|
if (node->pn_iflags & JSITER_FOREACH) { |
811 |
|
Stream_write_string(f, "each "); |
812 |
|
} |
813 |
|
Stream_write_char(f, '('); |
814 |
switch (node->pn_left->pn_type) { |
switch (node->pn_left->pn_type) { |
815 |
case TOK_IN: |
case TOK_IN: |
816 |
/* for/in */ |
/* for/in */ |
819 |
case TOK_VAR: |
case TOK_VAR: |
820 |
instrument_var_statement(node->pn_left->pn_left, f, 0); |
instrument_var_statement(node->pn_left->pn_left, f, 0); |
821 |
break; |
break; |
822 |
|
case TOK_LET: |
823 |
|
instrument_let_definition(node->pn_left->pn_left, f, 0); |
824 |
|
break; |
825 |
case TOK_NAME: |
case TOK_NAME: |
826 |
instrument_expression(node->pn_left->pn_left, f); |
instrument_expression(node->pn_left->pn_left, f); |
827 |
break; |
break; |
843 |
/* for (;;) */ |
/* for (;;) */ |
844 |
assert(node->pn_left->pn_arity == PN_TERNARY); |
assert(node->pn_left->pn_arity == PN_TERNARY); |
845 |
if (node->pn_left->pn_kid1) { |
if (node->pn_left->pn_kid1) { |
846 |
if (node->pn_left->pn_kid1->pn_type == TOK_VAR) { |
switch (node->pn_left->pn_kid1->pn_type) { |
847 |
|
case TOK_VAR: |
848 |
instrument_var_statement(node->pn_left->pn_kid1, f, 0); |
instrument_var_statement(node->pn_left->pn_kid1, f, 0); |
849 |
} |
break; |
850 |
else { |
case TOK_LET: |
851 |
|
instrument_let_definition(node->pn_left->pn_kid1, f, 0); |
852 |
|
break; |
853 |
|
default: |
854 |
instrument_expression(node->pn_left->pn_kid1, f); |
instrument_expression(node->pn_left->pn_kid1, f); |
855 |
|
break; |
856 |
} |
} |
857 |
} |
} |
858 |
Stream_write_string(f, ";"); |
Stream_write_string(f, ";"); |
887 |
instrument_statement(node->pn_kid1, f, indent + 2, false); |
instrument_statement(node->pn_kid1, f, indent + 2, false); |
888 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
889 |
Stream_write_string(f, "}\n"); |
Stream_write_string(f, "}\n"); |
890 |
{ |
if (node->pn_kid2) { |
891 |
for (JSParseNode * catch = node->pn_kid2; catch != NULL; catch = catch->pn_kid2) { |
assert(node->pn_kid2->pn_type == TOK_RESERVED); |
892 |
|
for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) { |
893 |
|
assert(scope->pn_type == TOK_LEXICALSCOPE); |
894 |
|
JSParseNode * catch = scope->pn_expr; |
895 |
assert(catch->pn_type == TOK_CATCH); |
assert(catch->pn_type == TOK_CATCH); |
896 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
897 |
Stream_write_string(f, "catch ("); |
Stream_write_string(f, "catch ("); |
898 |
|
/* this may not be a name - destructuring assignment */ |
899 |
|
/* |
900 |
assert(catch->pn_kid1->pn_arity == PN_NAME); |
assert(catch->pn_kid1->pn_arity == PN_NAME); |
901 |
print_string_atom(catch->pn_kid1->pn_atom, f); |
print_string_atom(catch->pn_kid1->pn_atom, f); |
902 |
if (catch->pn_kid1->pn_expr) { |
*/ |
903 |
|
instrument_expression(catch->pn_kid1, f); |
904 |
|
if (catch->pn_kid2) { |
905 |
Stream_write_string(f, " if "); |
Stream_write_string(f, " if "); |
906 |
instrument_expression(catch->pn_kid1->pn_expr, f); |
instrument_expression(catch->pn_kid2, f); |
907 |
} |
} |
908 |
Stream_write_string(f, ") {\n"); |
Stream_write_string(f, ") {\n"); |
909 |
instrument_statement(catch->pn_kid3, f, indent + 2, false); |
instrument_statement(catch->pn_kid3, f, indent + 2, false); |
980 |
*/ |
*/ |
981 |
output_statement(node->pn_expr, f, indent, false); |
output_statement(node->pn_expr, f, indent, false); |
982 |
break; |
break; |
983 |
|
case TOK_LEXICALSCOPE: |
984 |
|
/* let statement */ |
985 |
|
assert(node->pn_arity == PN_NAME); |
986 |
|
switch (node->pn_expr->pn_type) { |
987 |
|
case TOK_LET: |
988 |
|
/* let statement */ |
989 |
|
assert(node->pn_expr->pn_arity == PN_BINARY); |
990 |
|
instrument_statement(node->pn_expr, f, indent, false); |
991 |
|
break; |
992 |
|
case TOK_LC: |
993 |
|
/* block */ |
994 |
|
Stream_printf(f, "%*s", indent, ""); |
995 |
|
Stream_write_string(f, "{\n"); |
996 |
|
instrument_statement(node->pn_expr, f, indent + 2, false); |
997 |
|
Stream_printf(f, "%*s", indent, ""); |
998 |
|
Stream_write_string(f, "}\n"); |
999 |
|
break; |
1000 |
|
case TOK_FOR: |
1001 |
|
instrument_statement(node->pn_expr, f, indent, false); |
1002 |
|
break; |
1003 |
|
default: |
1004 |
|
abort(); |
1005 |
|
break; |
1006 |
|
} |
1007 |
|
break; |
1008 |
|
case TOK_LET: |
1009 |
|
switch (node->pn_arity) { |
1010 |
|
case PN_BINARY: |
1011 |
|
/* let statement */ |
1012 |
|
Stream_printf(f, "%*s", indent, ""); |
1013 |
|
Stream_write_string(f, "let ("); |
1014 |
|
assert(node->pn_left->pn_type == TOK_LP); |
1015 |
|
assert(node->pn_left->pn_arity == PN_LIST); |
1016 |
|
instrument_declarations(node->pn_left, f); |
1017 |
|
Stream_write_string(f, ") {\n"); |
1018 |
|
instrument_statement(node->pn_right, f, indent + 2, false); |
1019 |
|
Stream_printf(f, "%*s", indent, ""); |
1020 |
|
Stream_write_string(f, "}\n"); |
1021 |
|
break; |
1022 |
|
case PN_LIST: |
1023 |
|
/* let definition */ |
1024 |
|
instrument_let_definition(node, f, indent); |
1025 |
|
Stream_write_string(f, ";\n"); |
1026 |
|
break; |
1027 |
|
default: |
1028 |
|
abort(); |
1029 |
|
break; |
1030 |
|
} |
1031 |
|
break; |
1032 |
default: |
default: |
1033 |
fatal("unsupported node type in file %s: %d", file_id, node->pn_type); |
fatal("unsupported node type in file %s: %d", file_id, node->pn_type); |
1034 |
} |
} |
1040 |
TOK_EXPORT, TOK_IMPORT are not handled. |
TOK_EXPORT, TOK_IMPORT are not handled. |
1041 |
*/ |
*/ |
1042 |
static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) { |
static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) { |
1043 |
if (node->pn_type != TOK_LC) { |
if (node->pn_type != TOK_LC && node->pn_type != TOK_LEXICALSCOPE) { |
1044 |
uint16_t line = node->pn_pos.begin.lineno; |
uint16_t line = node->pn_pos.begin.lineno; |
1045 |
if (line > num_lines) { |
if (line > num_lines) { |
1046 |
fatal("%s: script contains more than 65,535 lines", file_id); |
fatal("%s: script contains more than 65,535 lines", file_id); |
1097 |
void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) { |
void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) { |
1098 |
file_id = id; |
file_id = id; |
1099 |
|
|
1100 |
/* scan the javascript */ |
/* parse the javascript */ |
1101 |
JSTokenStream * token_stream = js_NewTokenStream(context, characters, num_characters, NULL, 1, NULL); |
JSParseContext parse_context; |
1102 |
if (token_stream == NULL) { |
if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) { |
1103 |
fatal("cannot create token stream from file: %s", file_id); |
fatal("cannot create token stream from file: %s", file_id); |
1104 |
} |
} |
|
|
|
|
/* parse the javascript */ |
|
1105 |
JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter); |
JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter); |
1106 |
JSParseNode * node = js_ParseTokenStream(context, global, token_stream); |
JSParseNode * node = js_ParseScript(context, global, &parse_context); |
1107 |
if (node == NULL) { |
if (node == NULL) { |
1108 |
js_ReportUncaughtException(context); |
js_ReportUncaughtException(context); |
1109 |
fatal("parse error in file: %s", file_id); |
fatal("parse error in file: %s", file_id); |
1190 |
|
|
1191 |
Stream * instrumented = Stream_new(0); |
Stream * instrumented = Stream_new(0); |
1192 |
instrument_statement(node, instrumented, 0, false); |
instrument_statement(node, instrumented, 0, false); |
1193 |
|
js_FinishParseContext(context, &parse_context); |
1194 |
|
|
1195 |
/* write line number info to the output */ |
/* write line number info to the output */ |
1196 |
Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n"); |
Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n"); |
1463 |
} |
} |
1464 |
|
|
1465 |
int jscoverage_parse_json(Coverage * coverage, const uint8_t * json, size_t length) { |
int jscoverage_parse_json(Coverage * coverage, const uint8_t * json, size_t length) { |
1466 |
|
int result = 0; |
1467 |
|
|
1468 |
jschar * base = js_InflateString(context, (char *) json, &length); |
jschar * base = js_InflateString(context, (char *) json, &length); |
1469 |
if (base == NULL) { |
if (base == NULL) { |
1470 |
fatal("out of memory"); |
fatal("out of memory"); |
1477 |
|
|
1478 |
JS_free(context, base); |
JS_free(context, base); |
1479 |
|
|
1480 |
JSTokenStream * token_stream = js_NewTokenStream(context, parenthesized_json, length + 2, NULL, 1, NULL); |
JSParseContext parse_context; |
1481 |
if (token_stream == NULL) { |
if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) { |
1482 |
fatal("cannot create token stream"); |
free(parenthesized_json); |
1483 |
|
return -1; |
1484 |
} |
} |
1485 |
|
JSParseNode * root = js_ParseScript(context, global, &parse_context); |
|
JSParseNode * root = js_ParseTokenStream(context, global, token_stream); |
|
1486 |
free(parenthesized_json); |
free(parenthesized_json); |
1487 |
if (root == NULL) { |
if (root == NULL) { |
1488 |
return -1; |
result = -1; |
1489 |
|
goto done; |
1490 |
} |
} |
1491 |
|
|
1492 |
/* root node must be TOK_LC */ |
/* root node must be TOK_LC */ |
1493 |
if (root->pn_type != TOK_LC) { |
if (root->pn_type != TOK_LC) { |
1494 |
return -1; |
result = -1; |
1495 |
|
goto done; |
1496 |
} |
} |
1497 |
JSParseNode * semi = root->pn_u.list.head; |
JSParseNode * semi = root->pn_u.list.head; |
1498 |
|
|
1499 |
/* the list must be TOK_SEMI and it must contain only one element */ |
/* the list must be TOK_SEMI and it must contain only one element */ |
1500 |
if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) { |
if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) { |
1501 |
return -1; |
result = -1; |
1502 |
|
goto done; |
1503 |
} |
} |
1504 |
JSParseNode * parenthesized = semi->pn_kid; |
JSParseNode * parenthesized = semi->pn_kid; |
1505 |
|
|
1506 |
/* this must be a parenthesized expression */ |
/* this must be a parenthesized expression */ |
1507 |
if (parenthesized->pn_type != TOK_RP) { |
if (parenthesized->pn_type != TOK_RP) { |
1508 |
return -1; |
result = -1; |
1509 |
|
goto done; |
1510 |
} |
} |
1511 |
JSParseNode * object = parenthesized->pn_kid; |
JSParseNode * object = parenthesized->pn_kid; |
1512 |
|
|
1513 |
/* this must be an object literal */ |
/* this must be an object literal */ |
1514 |
if (object->pn_type != TOK_RC) { |
if (object->pn_type != TOK_RC) { |
1515 |
return -1; |
result = -1; |
1516 |
|
goto done; |
1517 |
} |
} |
1518 |
|
|
1519 |
for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) { |
for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) { |
1520 |
/* every element of this list must be TOK_COLON */ |
/* every element of this list must be TOK_COLON */ |
1521 |
if (p->pn_type != TOK_COLON) { |
if (p->pn_type != TOK_COLON) { |
1522 |
return -1; |
result = -1; |
1523 |
|
goto done; |
1524 |
} |
} |
1525 |
|
|
1526 |
/* the key must be a string representing the file */ |
/* the key must be a string representing the file */ |
1527 |
JSParseNode * key = p->pn_left; |
JSParseNode * key = p->pn_left; |
1528 |
if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) { |
if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) { |
1529 |
return -1; |
result = -1; |
1530 |
|
goto done; |
1531 |
} |
} |
1532 |
char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom)); |
char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom)); |
1533 |
|
|
1534 |
/* the value must be an object literal OR an array */ |
/* the value must be an object literal OR an array */ |
1535 |
JSParseNode * value = p->pn_right; |
JSParseNode * value = p->pn_right; |
1536 |
if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) { |
if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) { |
1537 |
return -1; |
result = -1; |
1538 |
|
goto done; |
1539 |
} |
} |
1540 |
|
|
1541 |
JSParseNode * array = NULL; |
JSParseNode * array = NULL; |
1547 |
else if (value->pn_type == TOK_RC) { |
else if (value->pn_type == TOK_RC) { |
1548 |
/* an object literal */ |
/* an object literal */ |
1549 |
if (value->pn_count != 2) { |
if (value->pn_count != 2) { |
1550 |
return -1; |
result = -1; |
1551 |
|
goto done; |
1552 |
} |
} |
1553 |
for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) { |
for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) { |
1554 |
if (element->pn_type != TOK_COLON) { |
if (element->pn_type != TOK_COLON) { |
1555 |
return -1; |
result = -1; |
1556 |
|
goto done; |
1557 |
} |
} |
1558 |
JSParseNode * left = element->pn_left; |
JSParseNode * left = element->pn_left; |
1559 |
if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) { |
if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) { |
1560 |
return -1; |
result = -1; |
1561 |
|
goto done; |
1562 |
} |
} |
1563 |
const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom)); |
const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom)); |
1564 |
if (strcmp(s, "coverage") == 0) { |
if (strcmp(s, "coverage") == 0) { |
1565 |
array = element->pn_right; |
array = element->pn_right; |
1566 |
if (array->pn_type != TOK_RB) { |
if (array->pn_type != TOK_RB) { |
1567 |
return -1; |
result = -1; |
1568 |
|
goto done; |
1569 |
} |
} |
1570 |
} |
} |
1571 |
else if (strcmp(s, "source") == 0) { |
else if (strcmp(s, "source") == 0) { |
1572 |
source = element->pn_right; |
source = element->pn_right; |
1573 |
if (source->pn_type != TOK_RB) { |
if (source->pn_type != TOK_RB) { |
1574 |
return -1; |
result = -1; |
1575 |
|
goto done; |
1576 |
} |
} |
1577 |
} |
} |
1578 |
else { |
else { |
1579 |
return -1; |
result = -1; |
1580 |
|
goto done; |
1581 |
} |
} |
1582 |
} |
} |
1583 |
} |
} |
1584 |
else { |
else { |
1585 |
return -1; |
result = -1; |
1586 |
|
goto done; |
1587 |
} |
} |
1588 |
|
|
1589 |
if (array == NULL) { |
if (array == NULL) { |
1590 |
return -1; |
result = -1; |
1591 |
|
goto done; |
1592 |
} |
} |
1593 |
|
|
1594 |
/* look up the file in the coverage table */ |
/* look up the file in the coverage table */ |
1612 |
file_coverage->coverage_lines[i] = -1; |
file_coverage->coverage_lines[i] = -1; |
1613 |
} |
} |
1614 |
else { |
else { |
1615 |
return -1; |
result = -1; |
1616 |
|
goto done; |
1617 |
} |
} |
1618 |
} |
} |
1619 |
assert(i == array->pn_count); |
assert(i == array->pn_count); |
1629 |
/* sanity check */ |
/* sanity check */ |
1630 |
assert(strcmp(file_coverage->id, id_bytes) == 0); |
assert(strcmp(file_coverage->id, id_bytes) == 0); |
1631 |
if (file_coverage->num_coverage_lines != array->pn_count) { |
if (file_coverage->num_coverage_lines != array->pn_count) { |
1632 |
return -2; |
result = -2; |
1633 |
|
goto done; |
1634 |
} |
} |
1635 |
|
|
1636 |
/* merge the coverage */ |
/* merge the coverage */ |
1638 |
for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) { |
for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) { |
1639 |
if (element->pn_type == TOK_NUMBER) { |
if (element->pn_type == TOK_NUMBER) { |
1640 |
if (file_coverage->coverage_lines[i] == -1) { |
if (file_coverage->coverage_lines[i] == -1) { |
1641 |
return -2; |
result = -2; |
1642 |
|
goto done; |
1643 |
} |
} |
1644 |
file_coverage->coverage_lines[i] += (int) element->pn_dval; |
file_coverage->coverage_lines[i] += (int) element->pn_dval; |
1645 |
} |
} |
1646 |
else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) { |
else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) { |
1647 |
if (file_coverage->coverage_lines[i] != -1) { |
if (file_coverage->coverage_lines[i] != -1) { |
1648 |
return -2; |
result = -2; |
1649 |
|
goto done; |
1650 |
} |
} |
1651 |
} |
} |
1652 |
else { |
else { |
1653 |
return -1; |
result = -1; |
1654 |
|
goto done; |
1655 |
} |
} |
1656 |
} |
} |
1657 |
assert(i == array->pn_count); |
assert(i == array->pn_count); |
1664 |
uint32 i = 0; |
uint32 i = 0; |
1665 |
for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) { |
for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) { |
1666 |
if (element->pn_type != TOK_STRING) { |
if (element->pn_type != TOK_STRING) { |
1667 |
return -1; |
result = -1; |
1668 |
|
goto done; |
1669 |
} |
} |
1670 |
file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom))); |
file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom))); |
1671 |
} |
} |
1673 |
} |
} |
1674 |
} |
} |
1675 |
|
|
1676 |
return 0; |
done: |
1677 |
|
js_FinishParseContext(context, &parse_context); |
1678 |
|
return result; |
1679 |
} |
} |