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 "<"; |
257 |
|
|
258 |
static void instrument_expression(JSParseNode * node, Stream * f); |
static void instrument_expression(JSParseNode * node, Stream * f); |
259 |
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); |
260 |
|
static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if); |
261 |
|
|
262 |
enum FunctionType { |
enum FunctionType { |
263 |
FUNCTION_NORMAL, |
FUNCTION_NORMAL, |
265 |
}; |
}; |
266 |
|
|
267 |
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) { |
268 |
|
assert(node->pn_type == TOK_FUNCTION); |
269 |
assert(node->pn_arity == PN_FUNC); |
assert(node->pn_arity == PN_FUNC); |
270 |
assert(ATOM_IS_OBJECT(node->pn_funAtom)); |
JSObject * object = node->pn_funpob->object; |
|
JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom); |
|
271 |
assert(JS_ObjectIsFunction(context, object)); |
assert(JS_ObjectIsFunction(context, object)); |
272 |
JSFunction * function = (JSFunction *) JS_GetPrivate(context, object); |
JSFunction * function = (JSFunction *) JS_GetPrivate(context, object); |
273 |
assert(function); |
assert(function); |
274 |
assert(object == function->object); |
assert(object == &function->object); |
275 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
276 |
if (type == FUNCTION_NORMAL) { |
if (type == FUNCTION_NORMAL) { |
277 |
Stream_write_string(f, "function"); |
Stream_write_string(f, "function"); |
283 |
print_string_atom(function->atom, f); |
print_string_atom(function->atom, f); |
284 |
} |
} |
285 |
|
|
286 |
/* function parameters */ |
/* |
287 |
|
function parameters - see JS_DecompileFunction in jsapi.cpp, which calls |
288 |
|
js_DecompileFunction in jsopcode.cpp |
289 |
|
*/ |
290 |
Stream_write_string(f, "("); |
Stream_write_string(f, "("); |
291 |
JSAtom ** params = xnew(JSAtom *, function->nargs); |
JSArenaPool pool; |
292 |
for (int i = 0; i < function->nargs; i++) { |
JS_INIT_ARENA_POOL(&pool, "instrument_function", 256, 1, &context->scriptStackQuota); |
293 |
/* initialize to NULL for sanity check */ |
jsuword * local_names = NULL; |
294 |
params[i] = NULL; |
if (JS_GET_LOCAL_NAME_COUNT(function)) { |
295 |
} |
local_names = js_GetLocalNameArray(context, function, &pool); |
296 |
JSScope * scope = OBJ_SCOPE(object); |
if (local_names == NULL) { |
297 |
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; |
|
298 |
} |
} |
|
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); |
|
299 |
} |
} |
300 |
for (int i = 0; i < function->nargs; i++) { |
for (int i = 0; i < function->nargs; i++) { |
|
assert(params[i] != NULL); |
|
301 |
if (i > 0) { |
if (i > 0) { |
302 |
Stream_write_string(f, ", "); |
Stream_write_string(f, ", "); |
303 |
} |
} |
304 |
if (ATOM_IS_STRING(params[i])) { |
JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]); |
305 |
print_string_atom(params[i], f); |
print_string_atom(param, f); |
|
} |
|
306 |
} |
} |
307 |
|
JS_FinishArenaPool(&pool); |
308 |
Stream_write_string(f, ") {\n"); |
Stream_write_string(f, ") {\n"); |
|
free(params); |
|
309 |
|
|
310 |
/* function body */ |
/* function body */ |
311 |
instrument_statement(node->pn_body, f, indent + 2, false); |
if (function->flags & JSFUN_EXPR_CLOSURE) { |
312 |
|
/* expression closure */ |
313 |
|
output_statement(node->pn_body, f, indent + 2, false); |
314 |
|
} |
315 |
|
else { |
316 |
|
instrument_statement(node->pn_body, f, indent + 2, false); |
317 |
|
} |
318 |
|
|
319 |
Stream_write_string(f, "}\n"); |
Stream_write_string(f, "}\n"); |
320 |
} |
} |
331 |
Stream_write_char(f, ')'); |
Stream_write_char(f, ')'); |
332 |
} |
} |
333 |
|
|
334 |
|
static void instrument_declarations(JSParseNode * list, Stream * f) { |
335 |
|
assert(list->pn_arity == PN_LIST); |
336 |
|
for (JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) { |
337 |
|
switch (p->pn_type) { |
338 |
|
case TOK_NAME: |
339 |
|
assert(p->pn_arity == PN_NAME); |
340 |
|
if (p != list->pn_head) { |
341 |
|
Stream_write_string(f, ", "); |
342 |
|
} |
343 |
|
print_string_atom(p->pn_atom, f); |
344 |
|
if (p->pn_expr != NULL) { |
345 |
|
Stream_write_string(f, " = "); |
346 |
|
instrument_expression(p->pn_expr, f); |
347 |
|
} |
348 |
|
break; |
349 |
|
case TOK_ASSIGN: |
350 |
|
case TOK_RB: |
351 |
|
case TOK_RC: |
352 |
|
/* destructuring */ |
353 |
|
instrument_expression(p, f); |
354 |
|
break; |
355 |
|
default: |
356 |
|
abort(); |
357 |
|
break; |
358 |
|
} |
359 |
|
} |
360 |
|
} |
361 |
|
|
362 |
|
static void output_for_in(JSParseNode * node, Stream * f) { |
363 |
|
assert(node->pn_type == TOK_FOR); |
364 |
|
assert(node->pn_arity == PN_BINARY); |
365 |
|
Stream_write_string(f, "for "); |
366 |
|
if (node->pn_iflags & JSITER_FOREACH) { |
367 |
|
Stream_write_string(f, "each "); |
368 |
|
} |
369 |
|
Stream_write_char(f, '('); |
370 |
|
instrument_expression(node->pn_left, f); |
371 |
|
Stream_write_char(f, ')'); |
372 |
|
} |
373 |
|
|
374 |
/* |
/* |
375 |
See <Expressions> in jsparse.h. |
See <Expressions> in jsparse.h. |
376 |
TOK_FUNCTION is handled as a statement and as an expression. |
TOK_FUNCTION is handled as a statement and as an expression. |
429 |
instrument_expression(node->pn_kid3, f); |
instrument_expression(node->pn_kid3, f); |
430 |
break; |
break; |
431 |
case TOK_OR: |
case TOK_OR: |
|
instrument_expression(node->pn_left, f); |
|
|
Stream_write_string(f, " || "); |
|
|
instrument_expression(node->pn_right, f); |
|
|
break; |
|
432 |
case TOK_AND: |
case TOK_AND: |
|
instrument_expression(node->pn_left, f); |
|
|
Stream_write_string(f, " && "); |
|
|
instrument_expression(node->pn_right, f); |
|
|
break; |
|
433 |
case TOK_BITOR: |
case TOK_BITOR: |
434 |
case TOK_BITXOR: |
case TOK_BITXOR: |
435 |
case TOK_BITAND: |
case TOK_BITAND: |
542 |
assert(ATOM_IS_STRING(node->pn_atom)); |
assert(ATOM_IS_STRING(node->pn_atom)); |
543 |
{ |
{ |
544 |
JSString * s = ATOM_TO_STRING(node->pn_atom); |
JSString * s = ATOM_TO_STRING(node->pn_atom); |
545 |
/* XXX - semantics changed in 1.7 */ |
bool is_keyword = (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF); |
546 |
if (! ATOM_KEYWORD(node->pn_atom) && js_IsIdentifier(s)) { |
if (! is_keyword && js_IsIdentifier(s)) { |
547 |
Stream_write_char(f, '.'); |
Stream_write_char(f, '.'); |
548 |
print_string_atom(node->pn_atom, f); |
print_string_atom(node->pn_atom, f); |
549 |
} |
} |
623 |
case TOK_STRING: |
case TOK_STRING: |
624 |
print_quoted_string_atom(node->pn_atom, f); |
print_quoted_string_atom(node->pn_atom, f); |
625 |
break; |
break; |
626 |
case TOK_OBJECT: |
case TOK_REGEXP: |
627 |
switch (node->pn_op) { |
assert(node->pn_op == JSOP_REGEXP); |
628 |
case JSOP_OBJECT: |
{ |
629 |
/* I assume this is JSOP_REGEXP */ |
JSObject * object = node->pn_pob->object; |
630 |
abort(); |
jsval result; |
631 |
break; |
js_regexp_toString(context, object, &result); |
632 |
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; |
|
633 |
} |
} |
634 |
break; |
break; |
635 |
case TOK_NUMBER: |
case TOK_NUMBER: |
675 |
Stream_write_string(f, " in "); |
Stream_write_string(f, " in "); |
676 |
instrument_expression(node->pn_right, f); |
instrument_expression(node->pn_right, f); |
677 |
break; |
break; |
678 |
|
case TOK_LEXICALSCOPE: |
679 |
|
assert(node->pn_arity == PN_NAME); |
680 |
|
assert(node->pn_expr->pn_type == TOK_LET); |
681 |
|
assert(node->pn_expr->pn_arity == PN_BINARY); |
682 |
|
Stream_write_string(f, "let("); |
683 |
|
assert(node->pn_expr->pn_left->pn_type == TOK_LP); |
684 |
|
assert(node->pn_expr->pn_left->pn_arity == PN_LIST); |
685 |
|
instrument_declarations(node->pn_expr->pn_left, f); |
686 |
|
Stream_write_string(f, ") "); |
687 |
|
instrument_expression(node->pn_expr->pn_right, f); |
688 |
|
break; |
689 |
|
case TOK_YIELD: |
690 |
|
assert(node->pn_arity == PN_UNARY); |
691 |
|
Stream_write_string(f, "yield "); |
692 |
|
instrument_expression(node->pn_kid, f); |
693 |
|
break; |
694 |
|
case TOK_ARRAYCOMP: |
695 |
|
assert(node->pn_arity == PN_LIST); |
696 |
|
{ |
697 |
|
JSParseNode * block_node; |
698 |
|
switch (node->pn_count) { |
699 |
|
case 1: |
700 |
|
block_node = node->pn_head; |
701 |
|
break; |
702 |
|
case 2: |
703 |
|
block_node = node->pn_head->pn_next; |
704 |
|
break; |
705 |
|
default: |
706 |
|
abort(); |
707 |
|
break; |
708 |
|
} |
709 |
|
assert(block_node->pn_type == TOK_LEXICALSCOPE); |
710 |
|
assert(block_node->pn_arity == PN_NAME); |
711 |
|
JSParseNode * for_node = block_node->pn_expr; |
712 |
|
assert(for_node->pn_type == TOK_FOR); |
713 |
|
assert(for_node->pn_arity == PN_BINARY); |
714 |
|
JSParseNode * push_node; |
715 |
|
JSParseNode * if_node = NULL; |
716 |
|
switch (for_node->pn_right->pn_type) { |
717 |
|
case TOK_ARRAYPUSH: |
718 |
|
push_node = for_node->pn_right; |
719 |
|
assert(push_node->pn_arity == PN_UNARY); |
720 |
|
break; |
721 |
|
case TOK_IF: |
722 |
|
if_node = for_node->pn_right; |
723 |
|
assert(if_node->pn_arity == PN_TERNARY); |
724 |
|
push_node = if_node->pn_kid2; |
725 |
|
break; |
726 |
|
default: |
727 |
|
abort(); |
728 |
|
break; |
729 |
|
} |
730 |
|
Stream_write_char(f, '['); |
731 |
|
instrument_expression(push_node->pn_kid, f); |
732 |
|
Stream_write_char(f, ' '); |
733 |
|
output_for_in(for_node, f); |
734 |
|
if (if_node) { |
735 |
|
Stream_write_string(f, " if ("); |
736 |
|
instrument_expression(if_node->pn_kid1, f); |
737 |
|
Stream_write_char(f, ')'); |
738 |
|
} |
739 |
|
Stream_write_char(f, ']'); |
740 |
|
} |
741 |
|
break; |
742 |
|
case TOK_VAR: |
743 |
|
assert(node->pn_arity == PN_LIST); |
744 |
|
Stream_write_string(f, "var "); |
745 |
|
instrument_declarations(node, f); |
746 |
|
break; |
747 |
|
case TOK_LET: |
748 |
|
assert(node->pn_arity == PN_LIST); |
749 |
|
Stream_write_string(f, "let "); |
750 |
|
instrument_declarations(node, f); |
751 |
|
break; |
752 |
default: |
default: |
753 |
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); |
754 |
} |
} |
755 |
} |
} |
756 |
|
|
|
static void instrument_var_statement(JSParseNode * node, Stream * f, int indent) { |
|
|
assert(node->pn_arity == PN_LIST); |
|
|
Stream_printf(f, "%*s", indent, ""); |
|
|
Stream_write_string(f, "var "); |
|
|
for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) { |
|
|
assert(p->pn_type == TOK_NAME); |
|
|
assert(p->pn_arity == PN_NAME); |
|
|
if (p != node->pn_head) { |
|
|
Stream_write_string(f, ", "); |
|
|
} |
|
|
print_string_atom(p->pn_atom, f); |
|
|
if (p->pn_expr != NULL) { |
|
|
Stream_write_string(f, " = "); |
|
|
instrument_expression(p->pn_expr, f); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
757 |
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) { |
758 |
switch (node->pn_type) { |
switch (node->pn_type) { |
759 |
case TOK_FUNCTION: |
case TOK_FUNCTION: |
874 |
case TOK_FOR: |
case TOK_FOR: |
875 |
assert(node->pn_arity == PN_BINARY); |
assert(node->pn_arity == PN_BINARY); |
876 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
|
Stream_write_string(f, "for ("); |
|
877 |
switch (node->pn_left->pn_type) { |
switch (node->pn_left->pn_type) { |
878 |
case TOK_IN: |
case TOK_IN: |
879 |
/* for/in */ |
/* for/in */ |
880 |
assert(node->pn_left->pn_arity == PN_BINARY); |
assert(node->pn_left->pn_arity == PN_BINARY); |
881 |
switch (node->pn_left->pn_left->pn_type) { |
output_for_in(node, f); |
|
case TOK_VAR: |
|
|
instrument_var_statement(node->pn_left->pn_left, f, 0); |
|
|
break; |
|
|
case TOK_NAME: |
|
|
instrument_expression(node->pn_left->pn_left, f); |
|
|
break; |
|
|
default: |
|
|
/* this is undocumented: for (x.value in y) */ |
|
|
instrument_expression(node->pn_left->pn_left, f); |
|
|
break; |
|
|
/* |
|
|
default: |
|
|
fprintf(stderr, "unexpected node type: %d\n", node->pn_left->pn_left->pn_type); |
|
|
abort(); |
|
|
break; |
|
|
*/ |
|
|
} |
|
|
Stream_write_string(f, " in "); |
|
|
instrument_expression(node->pn_left->pn_right, f); |
|
882 |
break; |
break; |
883 |
case TOK_RESERVED: |
case TOK_RESERVED: |
884 |
/* for (;;) */ |
/* for (;;) */ |
885 |
assert(node->pn_left->pn_arity == PN_TERNARY); |
assert(node->pn_left->pn_arity == PN_TERNARY); |
886 |
|
Stream_write_string(f, "for ("); |
887 |
if (node->pn_left->pn_kid1) { |
if (node->pn_left->pn_kid1) { |
888 |
if (node->pn_left->pn_kid1->pn_type == TOK_VAR) { |
instrument_expression(node->pn_left->pn_kid1, f); |
|
instrument_var_statement(node->pn_left->pn_kid1, f, 0); |
|
|
} |
|
|
else { |
|
|
instrument_expression(node->pn_left->pn_kid1, f); |
|
|
} |
|
889 |
} |
} |
890 |
Stream_write_string(f, ";"); |
Stream_write_string(f, ";"); |
891 |
if (node->pn_left->pn_kid2) { |
if (node->pn_left->pn_kid2) { |
897 |
Stream_write_char(f, ' '); |
Stream_write_char(f, ' '); |
898 |
instrument_expression(node->pn_left->pn_kid3, f); |
instrument_expression(node->pn_left->pn_kid3, f); |
899 |
} |
} |
900 |
|
Stream_write_char(f, ')'); |
901 |
break; |
break; |
902 |
default: |
default: |
903 |
abort(); |
abort(); |
904 |
break; |
break; |
905 |
} |
} |
906 |
Stream_write_string(f, ") {\n"); |
Stream_write_string(f, " {\n"); |
907 |
instrument_statement(node->pn_right, f, indent + 2, false); |
instrument_statement(node->pn_right, f, indent + 2, false); |
908 |
Stream_write_string(f, "}\n"); |
Stream_write_string(f, "}\n"); |
909 |
break; |
break; |
920 |
instrument_statement(node->pn_kid1, f, indent + 2, false); |
instrument_statement(node->pn_kid1, f, indent + 2, false); |
921 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
922 |
Stream_write_string(f, "}\n"); |
Stream_write_string(f, "}\n"); |
923 |
{ |
if (node->pn_kid2) { |
924 |
for (JSParseNode * catch = node->pn_kid2; catch != NULL; catch = catch->pn_kid2) { |
assert(node->pn_kid2->pn_type == TOK_RESERVED); |
925 |
|
for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) { |
926 |
|
assert(scope->pn_type == TOK_LEXICALSCOPE); |
927 |
|
JSParseNode * catch = scope->pn_expr; |
928 |
assert(catch->pn_type == TOK_CATCH); |
assert(catch->pn_type == TOK_CATCH); |
929 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
930 |
Stream_write_string(f, "catch ("); |
Stream_write_string(f, "catch ("); |
931 |
|
/* this may not be a name - destructuring assignment */ |
932 |
|
/* |
933 |
assert(catch->pn_kid1->pn_arity == PN_NAME); |
assert(catch->pn_kid1->pn_arity == PN_NAME); |
934 |
print_string_atom(catch->pn_kid1->pn_atom, f); |
print_string_atom(catch->pn_kid1->pn_atom, f); |
935 |
if (catch->pn_kid1->pn_expr) { |
*/ |
936 |
|
instrument_expression(catch->pn_kid1, f); |
937 |
|
if (catch->pn_kid2) { |
938 |
Stream_write_string(f, " if "); |
Stream_write_string(f, " if "); |
939 |
instrument_expression(catch->pn_kid1->pn_expr, f); |
instrument_expression(catch->pn_kid2, f); |
940 |
} |
} |
941 |
Stream_write_string(f, ") {\n"); |
Stream_write_string(f, ") {\n"); |
942 |
instrument_statement(catch->pn_kid3, f, indent + 2, false); |
instrument_statement(catch->pn_kid3, f, indent + 2, false); |
978 |
Stream_write_string(f, "}\n"); |
Stream_write_string(f, "}\n"); |
979 |
break; |
break; |
980 |
case TOK_VAR: |
case TOK_VAR: |
981 |
instrument_var_statement(node, f, indent); |
Stream_printf(f, "%*s", indent, ""); |
982 |
|
instrument_expression(node, f); |
983 |
Stream_write_string(f, ";\n"); |
Stream_write_string(f, ";\n"); |
984 |
break; |
break; |
985 |
case TOK_RETURN: |
case TOK_RETURN: |
1014 |
*/ |
*/ |
1015 |
output_statement(node->pn_expr, f, indent, false); |
output_statement(node->pn_expr, f, indent, false); |
1016 |
break; |
break; |
1017 |
|
case TOK_LEXICALSCOPE: |
1018 |
|
/* let statement */ |
1019 |
|
assert(node->pn_arity == PN_NAME); |
1020 |
|
switch (node->pn_expr->pn_type) { |
1021 |
|
case TOK_LET: |
1022 |
|
/* let statement */ |
1023 |
|
assert(node->pn_expr->pn_arity == PN_BINARY); |
1024 |
|
instrument_statement(node->pn_expr, f, indent, false); |
1025 |
|
break; |
1026 |
|
case TOK_LC: |
1027 |
|
/* block */ |
1028 |
|
Stream_printf(f, "%*s", indent, ""); |
1029 |
|
Stream_write_string(f, "{\n"); |
1030 |
|
instrument_statement(node->pn_expr, f, indent + 2, false); |
1031 |
|
Stream_printf(f, "%*s", indent, ""); |
1032 |
|
Stream_write_string(f, "}\n"); |
1033 |
|
break; |
1034 |
|
case TOK_FOR: |
1035 |
|
instrument_statement(node->pn_expr, f, indent, false); |
1036 |
|
break; |
1037 |
|
default: |
1038 |
|
abort(); |
1039 |
|
break; |
1040 |
|
} |
1041 |
|
break; |
1042 |
|
case TOK_LET: |
1043 |
|
switch (node->pn_arity) { |
1044 |
|
case PN_BINARY: |
1045 |
|
/* let statement */ |
1046 |
|
Stream_printf(f, "%*s", indent, ""); |
1047 |
|
Stream_write_string(f, "let ("); |
1048 |
|
assert(node->pn_left->pn_type == TOK_LP); |
1049 |
|
assert(node->pn_left->pn_arity == PN_LIST); |
1050 |
|
instrument_declarations(node->pn_left, f); |
1051 |
|
Stream_write_string(f, ") {\n"); |
1052 |
|
instrument_statement(node->pn_right, f, indent + 2, false); |
1053 |
|
Stream_printf(f, "%*s", indent, ""); |
1054 |
|
Stream_write_string(f, "}\n"); |
1055 |
|
break; |
1056 |
|
case PN_LIST: |
1057 |
|
/* let definition */ |
1058 |
|
Stream_printf(f, "%*s", indent, ""); |
1059 |
|
instrument_expression(node, f); |
1060 |
|
Stream_write_string(f, ";\n"); |
1061 |
|
break; |
1062 |
|
default: |
1063 |
|
abort(); |
1064 |
|
break; |
1065 |
|
} |
1066 |
|
break; |
1067 |
default: |
default: |
1068 |
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); |
1069 |
} |
} |
1075 |
TOK_EXPORT, TOK_IMPORT are not handled. |
TOK_EXPORT, TOK_IMPORT are not handled. |
1076 |
*/ |
*/ |
1077 |
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) { |
1078 |
if (node->pn_type != TOK_LC) { |
if (node->pn_type != TOK_LC && node->pn_type != TOK_LEXICALSCOPE) { |
1079 |
uint16_t line = node->pn_pos.begin.lineno; |
uint16_t line = node->pn_pos.begin.lineno; |
1080 |
if (line > num_lines) { |
if (line > num_lines) { |
1081 |
fatal("%s: script contains more than 65,535 lines", file_id); |
fatal("%s: script contains more than 65,535 lines", file_id); |
1132 |
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) { |
1133 |
file_id = id; |
file_id = id; |
1134 |
|
|
1135 |
/* scan the javascript */ |
/* parse the javascript */ |
1136 |
JSTokenStream * token_stream = js_NewTokenStream(context, characters, num_characters, NULL, 1, NULL); |
JSParseContext parse_context; |
1137 |
if (token_stream == NULL) { |
if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) { |
1138 |
fatal("cannot create token stream from file: %s", file_id); |
fatal("cannot create token stream from file: %s", file_id); |
1139 |
} |
} |
|
|
|
|
/* parse the javascript */ |
|
1140 |
JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter); |
JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter); |
1141 |
JSParseNode * node = js_ParseTokenStream(context, global, token_stream); |
JSParseNode * node = js_ParseScript(context, global, &parse_context); |
1142 |
if (node == NULL) { |
if (node == NULL) { |
1143 |
js_ReportUncaughtException(context); |
js_ReportUncaughtException(context); |
1144 |
fatal("parse error in file: %s", file_id); |
fatal("parse error in file: %s", file_id); |
1225 |
|
|
1226 |
Stream * instrumented = Stream_new(0); |
Stream * instrumented = Stream_new(0); |
1227 |
instrument_statement(node, instrumented, 0, false); |
instrument_statement(node, instrumented, 0, false); |
1228 |
|
js_FinishParseContext(context, &parse_context); |
1229 |
|
|
1230 |
/* write line number info to the output */ |
/* write line number info to the output */ |
1231 |
Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n"); |
Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n"); |
1498 |
} |
} |
1499 |
|
|
1500 |
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) { |
1501 |
|
int result = 0; |
1502 |
|
|
1503 |
jschar * base = js_InflateString(context, (char *) json, &length); |
jschar * base = js_InflateString(context, (char *) json, &length); |
1504 |
if (base == NULL) { |
if (base == NULL) { |
1505 |
fatal("out of memory"); |
fatal("out of memory"); |
1512 |
|
|
1513 |
JS_free(context, base); |
JS_free(context, base); |
1514 |
|
|
1515 |
JSTokenStream * token_stream = js_NewTokenStream(context, parenthesized_json, length + 2, NULL, 1, NULL); |
JSParseContext parse_context; |
1516 |
if (token_stream == NULL) { |
if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) { |
1517 |
fatal("cannot create token stream"); |
free(parenthesized_json); |
1518 |
|
return -1; |
1519 |
} |
} |
1520 |
|
JSParseNode * root = js_ParseScript(context, global, &parse_context); |
|
JSParseNode * root = js_ParseTokenStream(context, global, token_stream); |
|
1521 |
free(parenthesized_json); |
free(parenthesized_json); |
1522 |
if (root == NULL) { |
if (root == NULL) { |
1523 |
return -1; |
result = -1; |
1524 |
|
goto done; |
1525 |
} |
} |
1526 |
|
|
1527 |
/* root node must be TOK_LC */ |
/* root node must be TOK_LC */ |
1528 |
if (root->pn_type != TOK_LC) { |
if (root->pn_type != TOK_LC) { |
1529 |
return -1; |
result = -1; |
1530 |
|
goto done; |
1531 |
} |
} |
1532 |
JSParseNode * semi = root->pn_u.list.head; |
JSParseNode * semi = root->pn_u.list.head; |
1533 |
|
|
1534 |
/* 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 */ |
1535 |
if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) { |
if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) { |
1536 |
return -1; |
result = -1; |
1537 |
|
goto done; |
1538 |
} |
} |
1539 |
JSParseNode * parenthesized = semi->pn_kid; |
JSParseNode * parenthesized = semi->pn_kid; |
1540 |
|
|
1541 |
/* this must be a parenthesized expression */ |
/* this must be a parenthesized expression */ |
1542 |
if (parenthesized->pn_type != TOK_RP) { |
if (parenthesized->pn_type != TOK_RP) { |
1543 |
return -1; |
result = -1; |
1544 |
|
goto done; |
1545 |
} |
} |
1546 |
JSParseNode * object = parenthesized->pn_kid; |
JSParseNode * object = parenthesized->pn_kid; |
1547 |
|
|
1548 |
/* this must be an object literal */ |
/* this must be an object literal */ |
1549 |
if (object->pn_type != TOK_RC) { |
if (object->pn_type != TOK_RC) { |
1550 |
return -1; |
result = -1; |
1551 |
|
goto done; |
1552 |
} |
} |
1553 |
|
|
1554 |
for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) { |
for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) { |
1555 |
/* every element of this list must be TOK_COLON */ |
/* every element of this list must be TOK_COLON */ |
1556 |
if (p->pn_type != TOK_COLON) { |
if (p->pn_type != TOK_COLON) { |
1557 |
return -1; |
result = -1; |
1558 |
|
goto done; |
1559 |
} |
} |
1560 |
|
|
1561 |
/* the key must be a string representing the file */ |
/* the key must be a string representing the file */ |
1562 |
JSParseNode * key = p->pn_left; |
JSParseNode * key = p->pn_left; |
1563 |
if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) { |
if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) { |
1564 |
return -1; |
result = -1; |
1565 |
|
goto done; |
1566 |
} |
} |
1567 |
char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom)); |
char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom)); |
1568 |
|
|
1569 |
/* the value must be an object literal OR an array */ |
/* the value must be an object literal OR an array */ |
1570 |
JSParseNode * value = p->pn_right; |
JSParseNode * value = p->pn_right; |
1571 |
if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) { |
if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) { |
1572 |
return -1; |
result = -1; |
1573 |
|
goto done; |
1574 |
} |
} |
1575 |
|
|
1576 |
JSParseNode * array = NULL; |
JSParseNode * array = NULL; |
1582 |
else if (value->pn_type == TOK_RC) { |
else if (value->pn_type == TOK_RC) { |
1583 |
/* an object literal */ |
/* an object literal */ |
1584 |
if (value->pn_count != 2) { |
if (value->pn_count != 2) { |
1585 |
return -1; |
result = -1; |
1586 |
|
goto done; |
1587 |
} |
} |
1588 |
for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) { |
for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) { |
1589 |
if (element->pn_type != TOK_COLON) { |
if (element->pn_type != TOK_COLON) { |
1590 |
return -1; |
result = -1; |
1591 |
|
goto done; |
1592 |
} |
} |
1593 |
JSParseNode * left = element->pn_left; |
JSParseNode * left = element->pn_left; |
1594 |
if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) { |
if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) { |
1595 |
return -1; |
result = -1; |
1596 |
|
goto done; |
1597 |
} |
} |
1598 |
const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom)); |
const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom)); |
1599 |
if (strcmp(s, "coverage") == 0) { |
if (strcmp(s, "coverage") == 0) { |
1600 |
array = element->pn_right; |
array = element->pn_right; |
1601 |
if (array->pn_type != TOK_RB) { |
if (array->pn_type != TOK_RB) { |
1602 |
return -1; |
result = -1; |
1603 |
|
goto done; |
1604 |
} |
} |
1605 |
} |
} |
1606 |
else if (strcmp(s, "source") == 0) { |
else if (strcmp(s, "source") == 0) { |
1607 |
source = element->pn_right; |
source = element->pn_right; |
1608 |
if (source->pn_type != TOK_RB) { |
if (source->pn_type != TOK_RB) { |
1609 |
return -1; |
result = -1; |
1610 |
|
goto done; |
1611 |
} |
} |
1612 |
} |
} |
1613 |
else { |
else { |
1614 |
return -1; |
result = -1; |
1615 |
|
goto done; |
1616 |
} |
} |
1617 |
} |
} |
1618 |
} |
} |
1619 |
else { |
else { |
1620 |
return -1; |
result = -1; |
1621 |
|
goto done; |
1622 |
} |
} |
1623 |
|
|
1624 |
if (array == NULL) { |
if (array == NULL) { |
1625 |
return -1; |
result = -1; |
1626 |
|
goto done; |
1627 |
} |
} |
1628 |
|
|
1629 |
/* look up the file in the coverage table */ |
/* look up the file in the coverage table */ |
1647 |
file_coverage->coverage_lines[i] = -1; |
file_coverage->coverage_lines[i] = -1; |
1648 |
} |
} |
1649 |
else { |
else { |
1650 |
return -1; |
result = -1; |
1651 |
|
goto done; |
1652 |
} |
} |
1653 |
} |
} |
1654 |
assert(i == array->pn_count); |
assert(i == array->pn_count); |
1664 |
/* sanity check */ |
/* sanity check */ |
1665 |
assert(strcmp(file_coverage->id, id_bytes) == 0); |
assert(strcmp(file_coverage->id, id_bytes) == 0); |
1666 |
if (file_coverage->num_coverage_lines != array->pn_count) { |
if (file_coverage->num_coverage_lines != array->pn_count) { |
1667 |
return -2; |
result = -2; |
1668 |
|
goto done; |
1669 |
} |
} |
1670 |
|
|
1671 |
/* merge the coverage */ |
/* merge the coverage */ |
1673 |
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++) { |
1674 |
if (element->pn_type == TOK_NUMBER) { |
if (element->pn_type == TOK_NUMBER) { |
1675 |
if (file_coverage->coverage_lines[i] == -1) { |
if (file_coverage->coverage_lines[i] == -1) { |
1676 |
return -2; |
result = -2; |
1677 |
|
goto done; |
1678 |
} |
} |
1679 |
file_coverage->coverage_lines[i] += (int) element->pn_dval; |
file_coverage->coverage_lines[i] += (int) element->pn_dval; |
1680 |
} |
} |
1681 |
else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) { |
else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) { |
1682 |
if (file_coverage->coverage_lines[i] != -1) { |
if (file_coverage->coverage_lines[i] != -1) { |
1683 |
return -2; |
result = -2; |
1684 |
|
goto done; |
1685 |
} |
} |
1686 |
} |
} |
1687 |
else { |
else { |
1688 |
return -1; |
result = -1; |
1689 |
|
goto done; |
1690 |
} |
} |
1691 |
} |
} |
1692 |
assert(i == array->pn_count); |
assert(i == array->pn_count); |
1699 |
uint32 i = 0; |
uint32 i = 0; |
1700 |
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++) { |
1701 |
if (element->pn_type != TOK_STRING) { |
if (element->pn_type != TOK_STRING) { |
1702 |
return -1; |
result = -1; |
1703 |
|
goto done; |
1704 |
} |
} |
1705 |
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))); |
1706 |
} |
} |
1708 |
} |
} |
1709 |
} |
} |
1710 |
|
|
1711 |
return 0; |
done: |
1712 |
|
js_FinishParseContext(context, &parse_context); |
1713 |
|
return result; |
1714 |
} |
} |