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> |
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> |
43 |
#include "resource-manager.h" |
#include "resource-manager.h" |
44 |
#include "util.h" |
#include "util.h" |
45 |
|
|
46 |
|
struct IfDirective { |
47 |
|
const jschar * condition_start; |
48 |
|
const jschar * condition_end; |
49 |
|
uint16_t start_line; |
50 |
|
uint16_t end_line; |
51 |
|
struct IfDirective * next; |
52 |
|
}; |
53 |
|
|
54 |
|
static bool * exclusive_directives = NULL; |
55 |
|
|
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"); |
98 |
JS_DestroyRuntime(runtime); |
JS_DestroyRuntime(runtime); |
99 |
} |
} |
100 |
|
|
101 |
|
static void print_javascript(const jschar * characters, size_t num_characters, Stream * f) { |
102 |
|
for (size_t i = 0; i < num_characters; i++) { |
103 |
|
jschar c = characters[i]; |
104 |
|
/* |
105 |
|
XXX does not handle no-break space, other unicode "space separator" |
106 |
|
*/ |
107 |
|
switch (c) { |
108 |
|
case 0x9: |
109 |
|
case 0xB: |
110 |
|
case 0xC: |
111 |
|
Stream_write_char(f, c); |
112 |
|
break; |
113 |
|
default: |
114 |
|
if (32 <= c && c <= 126) { |
115 |
|
Stream_write_char(f, c); |
116 |
|
} |
117 |
|
else { |
118 |
|
Stream_printf(f, "\\u%04x", c); |
119 |
|
} |
120 |
|
break; |
121 |
|
} |
122 |
|
} |
123 |
|
} |
124 |
|
|
125 |
static void print_string(JSString * s, Stream * f) { |
static void print_string(JSString * s, Stream * f) { |
126 |
size_t length = JSSTRING_LENGTH(s); |
size_t length = JSSTRING_LENGTH(s); |
127 |
jschar * characters = JSSTRING_CHARS(s); |
jschar * characters = JSSTRING_CHARS(s); |
156 |
case 0xa: |
case 0xa: |
157 |
Stream_write_string(f, "\\n"); |
Stream_write_string(f, "\\n"); |
158 |
break; |
break; |
159 |
|
/* IE doesn't support this */ |
160 |
|
/* |
161 |
case 0xb: |
case 0xb: |
162 |
Stream_write_string(f, "\\v"); |
Stream_write_string(f, "\\v"); |
163 |
break; |
break; |
164 |
|
*/ |
165 |
case 0xc: |
case 0xc: |
166 |
Stream_write_string(f, "\\f"); |
Stream_write_string(f, "\\f"); |
167 |
break; |
break; |
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 "<"; |
256 |
} |
} |
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); |
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, |
264 |
FUNCTION_GETTER_OR_SETTER |
FUNCTION_GETTER_OR_SETTER |
265 |
}; |
}; |
266 |
|
|
267 |
|
static void output_for_in(JSParseNode * node, Stream * f) { |
268 |
|
assert(node->pn_type == TOK_FOR); |
269 |
|
assert(node->pn_arity == PN_BINARY); |
270 |
|
Stream_write_string(f, "for "); |
271 |
|
if (node->pn_iflags & JSITER_FOREACH) { |
272 |
|
Stream_write_string(f, "each "); |
273 |
|
} |
274 |
|
Stream_write_char(f, '('); |
275 |
|
instrument_expression(node->pn_left, f); |
276 |
|
Stream_write_char(f, ')'); |
277 |
|
} |
278 |
|
|
279 |
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) { |
280 |
|
assert(node->pn_type == TOK_FUNCTION); |
281 |
assert(node->pn_arity == PN_FUNC); |
assert(node->pn_arity == PN_FUNC); |
282 |
assert(ATOM_IS_OBJECT(node->pn_funAtom)); |
JSObject * object = node->pn_funpob->object; |
|
JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom); |
|
283 |
assert(JS_ObjectIsFunction(context, object)); |
assert(JS_ObjectIsFunction(context, object)); |
284 |
JSFunction * function = (JSFunction *) JS_GetPrivate(context, object); |
JSFunction * function = (JSFunction *) JS_GetPrivate(context, object); |
285 |
assert(function); |
assert(function); |
286 |
assert(object == function->object); |
assert(object == &function->object); |
287 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
288 |
if (type == FUNCTION_NORMAL) { |
if (type == FUNCTION_NORMAL) { |
289 |
Stream_write_string(f, "function"); |
Stream_write_string(f, "function"); |
295 |
print_string_atom(function->atom, f); |
print_string_atom(function->atom, f); |
296 |
} |
} |
297 |
|
|
298 |
/* function parameters */ |
/* |
299 |
|
function parameters - see JS_DecompileFunction in jsapi.cpp, which calls |
300 |
|
js_DecompileFunction in jsopcode.cpp |
301 |
|
*/ |
302 |
Stream_write_string(f, "("); |
Stream_write_string(f, "("); |
303 |
JSAtom ** params = xnew(JSAtom *, function->nargs); |
JSArenaPool pool; |
304 |
for (int i = 0; i < function->nargs; i++) { |
JS_INIT_ARENA_POOL(&pool, "instrument_function", 256, 1, &context->scriptStackQuota); |
305 |
/* initialize to NULL for sanity check */ |
jsuword * local_names = NULL; |
306 |
params[i] = NULL; |
if (JS_GET_LOCAL_NAME_COUNT(function)) { |
307 |
} |
local_names = js_GetLocalNameArray(context, function, &pool); |
308 |
JSScope * scope = OBJ_SCOPE(object); |
if (local_names == NULL) { |
309 |
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; |
|
310 |
} |
} |
|
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); |
|
311 |
} |
} |
312 |
for (int i = 0; i < function->nargs; i++) { |
for (int i = 0; i < function->nargs; i++) { |
|
assert(params[i] != NULL); |
|
313 |
if (i > 0) { |
if (i > 0) { |
314 |
Stream_write_string(f, ", "); |
Stream_write_string(f, ", "); |
315 |
} |
} |
316 |
if (ATOM_IS_STRING(params[i])) { |
JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]); |
317 |
print_string_atom(params[i], f); |
print_string_atom(param, f); |
|
} |
|
318 |
} |
} |
319 |
|
JS_FinishArenaPool(&pool); |
320 |
Stream_write_string(f, ") {\n"); |
Stream_write_string(f, ") {\n"); |
|
free(params); |
|
321 |
|
|
322 |
/* function body */ |
/* function body */ |
323 |
instrument_statement(node->pn_body, f, indent + 2); |
if (function->flags & JSFUN_EXPR_CLOSURE) { |
324 |
|
/* expression closure */ |
325 |
|
output_statement(node->pn_body, f, indent + 2, false); |
326 |
|
} |
327 |
|
else { |
328 |
|
instrument_statement(node->pn_body, f, indent + 2, false); |
329 |
|
} |
330 |
|
|
331 |
Stream_write_string(f, "}\n"); |
Stream_write_string(f, "}\n"); |
332 |
} |
} |
333 |
|
|
334 |
static void instrument_function_call(JSParseNode * node, Stream * f) { |
static void instrument_function_call(JSParseNode * node, Stream * f) { |
335 |
instrument_expression(node->pn_head, f); |
if (node->pn_head->pn_type == TOK_FUNCTION) { |
336 |
Stream_write_char(f, '('); |
/* it's a generator expression */ |
337 |
for (struct JSParseNode * p = node->pn_head->pn_next; p != NULL; p = p->pn_next) { |
JSParseNode * function_node = node->pn_head; |
338 |
if (p != node->pn_head->pn_next) { |
JSParseNode * lexical_scope_node = function_node->pn_body; |
339 |
Stream_write_string(f, ", "); |
assert(lexical_scope_node->pn_type == TOK_LEXICALSCOPE); |
340 |
|
assert(lexical_scope_node->pn_arity == PN_NAME); |
341 |
|
JSParseNode * for_node = lexical_scope_node->pn_body; |
342 |
|
assert(for_node->pn_type == TOK_FOR); |
343 |
|
assert(for_node->pn_arity == PN_BINARY); |
344 |
|
JSParseNode * if_node = NULL; |
345 |
|
JSParseNode * semi_node; |
346 |
|
switch (for_node->pn_right->pn_type) { |
347 |
|
case TOK_SEMI: |
348 |
|
semi_node = for_node->pn_right; |
349 |
|
break; |
350 |
|
case TOK_IF: |
351 |
|
if_node = for_node->pn_right; |
352 |
|
assert(if_node->pn_arity == PN_TERNARY); |
353 |
|
semi_node = if_node->pn_kid2; |
354 |
|
assert(semi_node->pn_type == TOK_SEMI); |
355 |
|
break; |
356 |
|
default: |
357 |
|
abort(); |
358 |
|
break; |
359 |
|
} |
360 |
|
assert(semi_node->pn_arity == PN_UNARY); |
361 |
|
JSParseNode * yield_node = semi_node->pn_kid; |
362 |
|
assert(yield_node->pn_type == TOK_YIELD); |
363 |
|
Stream_write_char(f, '('); |
364 |
|
instrument_expression(yield_node->pn_kid, f); |
365 |
|
Stream_write_char(f, ' '); |
366 |
|
output_for_in(for_node, f); |
367 |
|
if (if_node) { |
368 |
|
Stream_write_string(f, " if ("); |
369 |
|
instrument_expression(if_node->pn_kid1, f); |
370 |
|
Stream_write_char(f, ')'); |
371 |
|
} |
372 |
|
Stream_write_char(f, ')'); |
373 |
|
} |
374 |
|
else { |
375 |
|
instrument_expression(node->pn_head, f); |
376 |
|
Stream_write_char(f, '('); |
377 |
|
for (struct JSParseNode * p = node->pn_head->pn_next; p != NULL; p = p->pn_next) { |
378 |
|
if (p != node->pn_head->pn_next) { |
379 |
|
Stream_write_string(f, ", "); |
380 |
|
} |
381 |
|
instrument_expression(p, f); |
382 |
|
} |
383 |
|
Stream_write_char(f, ')'); |
384 |
|
} |
385 |
|
} |
386 |
|
|
387 |
|
static void instrument_declarations(JSParseNode * list, Stream * f) { |
388 |
|
assert(list->pn_arity == PN_LIST); |
389 |
|
for (JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) { |
390 |
|
switch (p->pn_type) { |
391 |
|
case TOK_NAME: |
392 |
|
assert(p->pn_arity == PN_NAME); |
393 |
|
if (p != list->pn_head) { |
394 |
|
Stream_write_string(f, ", "); |
395 |
|
} |
396 |
|
print_string_atom(p->pn_atom, f); |
397 |
|
if (p->pn_expr != NULL) { |
398 |
|
Stream_write_string(f, " = "); |
399 |
|
instrument_expression(p->pn_expr, f); |
400 |
|
} |
401 |
|
break; |
402 |
|
case TOK_ASSIGN: |
403 |
|
case TOK_RB: |
404 |
|
case TOK_RC: |
405 |
|
/* destructuring */ |
406 |
|
instrument_expression(p, f); |
407 |
|
break; |
408 |
|
default: |
409 |
|
abort(); |
410 |
|
break; |
411 |
} |
} |
|
instrument_expression(p, f); |
|
412 |
} |
} |
|
Stream_write_char(f, ')'); |
|
413 |
} |
} |
414 |
|
|
415 |
/* |
/* |
470 |
instrument_expression(node->pn_kid3, f); |
instrument_expression(node->pn_kid3, f); |
471 |
break; |
break; |
472 |
case TOK_OR: |
case TOK_OR: |
|
instrument_expression(node->pn_left, f); |
|
|
Stream_write_string(f, " || "); |
|
|
instrument_expression(node->pn_right, f); |
|
|
break; |
|
473 |
case TOK_AND: |
case TOK_AND: |
|
instrument_expression(node->pn_left, f); |
|
|
Stream_write_string(f, " && "); |
|
|
instrument_expression(node->pn_right, f); |
|
|
break; |
|
474 |
case TOK_BITOR: |
case TOK_BITOR: |
475 |
case TOK_BITXOR: |
case TOK_BITXOR: |
476 |
case TOK_BITAND: |
case TOK_BITAND: |
583 |
assert(ATOM_IS_STRING(node->pn_atom)); |
assert(ATOM_IS_STRING(node->pn_atom)); |
584 |
{ |
{ |
585 |
JSString * s = ATOM_TO_STRING(node->pn_atom); |
JSString * s = ATOM_TO_STRING(node->pn_atom); |
586 |
/* XXX - semantics changed in 1.7 */ |
bool must_quote; |
587 |
if (! ATOM_KEYWORD(node->pn_atom) && js_IsIdentifier(s)) { |
if (JSSTRING_LENGTH(s) == 0) { |
588 |
Stream_write_char(f, '.'); |
must_quote = true; |
589 |
print_string_atom(node->pn_atom, f); |
} |
590 |
|
else if (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF) { |
591 |
|
must_quote = true; |
592 |
|
} |
593 |
|
else if (! js_IsIdentifier(s)) { |
594 |
|
must_quote = true; |
595 |
} |
} |
596 |
else { |
else { |
597 |
|
must_quote = false; |
598 |
|
} |
599 |
|
if (must_quote) { |
600 |
Stream_write_char(f, '['); |
Stream_write_char(f, '['); |
601 |
print_quoted_string_atom(node->pn_atom, f); |
print_quoted_string_atom(node->pn_atom, f); |
602 |
Stream_write_char(f, ']'); |
Stream_write_char(f, ']'); |
603 |
} |
} |
604 |
|
else { |
605 |
|
Stream_write_char(f, '.'); |
606 |
|
print_string_atom(node->pn_atom, f); |
607 |
|
} |
608 |
} |
} |
609 |
break; |
break; |
610 |
case TOK_LB: |
case TOK_LB: |
643 |
/* check whether this is a getter or setter */ |
/* check whether this is a getter or setter */ |
644 |
switch (p->pn_op) { |
switch (p->pn_op) { |
645 |
case JSOP_GETTER: |
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; |
|
646 |
case JSOP_SETTER: |
case JSOP_SETTER: |
647 |
Stream_write_string(f, "set "); |
if (p->pn_op == JSOP_GETTER) { |
648 |
|
Stream_write_string(f, "get "); |
649 |
|
} |
650 |
|
else { |
651 |
|
Stream_write_string(f, "set "); |
652 |
|
} |
653 |
instrument_expression(p->pn_left, f); |
instrument_expression(p->pn_left, f); |
654 |
|
if (p->pn_right->pn_type != TOK_FUNCTION) { |
655 |
|
fatal("parse error: expected function"); |
656 |
|
} |
657 |
instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER); |
instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER); |
658 |
break; |
break; |
659 |
default: |
default: |
676 |
case TOK_STRING: |
case TOK_STRING: |
677 |
print_quoted_string_atom(node->pn_atom, f); |
print_quoted_string_atom(node->pn_atom, f); |
678 |
break; |
break; |
679 |
case TOK_OBJECT: |
case TOK_REGEXP: |
680 |
switch (node->pn_op) { |
assert(node->pn_op == JSOP_REGEXP); |
681 |
case JSOP_OBJECT: |
{ |
682 |
/* I assume this is JSOP_REGEXP */ |
JSObject * object = node->pn_pob->object; |
683 |
abort(); |
jsval result; |
684 |
break; |
js_regexp_toString(context, object, &result); |
685 |
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; |
|
686 |
} |
} |
687 |
break; |
break; |
688 |
case TOK_NUMBER: |
case TOK_NUMBER: |
728 |
Stream_write_string(f, " in "); |
Stream_write_string(f, " in "); |
729 |
instrument_expression(node->pn_right, f); |
instrument_expression(node->pn_right, f); |
730 |
break; |
break; |
731 |
|
case TOK_LEXICALSCOPE: |
732 |
|
assert(node->pn_arity == PN_NAME); |
733 |
|
assert(node->pn_expr->pn_type == TOK_LET); |
734 |
|
assert(node->pn_expr->pn_arity == PN_BINARY); |
735 |
|
Stream_write_string(f, "let("); |
736 |
|
assert(node->pn_expr->pn_left->pn_type == TOK_LP); |
737 |
|
assert(node->pn_expr->pn_left->pn_arity == PN_LIST); |
738 |
|
instrument_declarations(node->pn_expr->pn_left, f); |
739 |
|
Stream_write_string(f, ") "); |
740 |
|
instrument_expression(node->pn_expr->pn_right, f); |
741 |
|
break; |
742 |
|
case TOK_YIELD: |
743 |
|
assert(node->pn_arity == PN_UNARY); |
744 |
|
Stream_write_string(f, "yield "); |
745 |
|
instrument_expression(node->pn_kid, f); |
746 |
|
break; |
747 |
|
case TOK_ARRAYCOMP: |
748 |
|
assert(node->pn_arity == PN_LIST); |
749 |
|
{ |
750 |
|
JSParseNode * block_node; |
751 |
|
switch (node->pn_count) { |
752 |
|
case 1: |
753 |
|
block_node = node->pn_head; |
754 |
|
break; |
755 |
|
case 2: |
756 |
|
block_node = node->pn_head->pn_next; |
757 |
|
break; |
758 |
|
default: |
759 |
|
abort(); |
760 |
|
break; |
761 |
|
} |
762 |
|
assert(block_node->pn_type == TOK_LEXICALSCOPE); |
763 |
|
assert(block_node->pn_arity == PN_NAME); |
764 |
|
JSParseNode * for_node = block_node->pn_expr; |
765 |
|
assert(for_node->pn_type == TOK_FOR); |
766 |
|
assert(for_node->pn_arity == PN_BINARY); |
767 |
|
JSParseNode * if_node = NULL; |
768 |
|
JSParseNode * push_node; |
769 |
|
switch (for_node->pn_right->pn_type) { |
770 |
|
case TOK_ARRAYPUSH: |
771 |
|
push_node = for_node->pn_right; |
772 |
|
assert(push_node->pn_arity == PN_UNARY); |
773 |
|
break; |
774 |
|
case TOK_IF: |
775 |
|
if_node = for_node->pn_right; |
776 |
|
assert(if_node->pn_arity == PN_TERNARY); |
777 |
|
push_node = if_node->pn_kid2; |
778 |
|
break; |
779 |
|
default: |
780 |
|
abort(); |
781 |
|
break; |
782 |
|
} |
783 |
|
Stream_write_char(f, '['); |
784 |
|
instrument_expression(push_node->pn_kid, f); |
785 |
|
Stream_write_char(f, ' '); |
786 |
|
output_for_in(for_node, f); |
787 |
|
if (if_node) { |
788 |
|
Stream_write_string(f, " if ("); |
789 |
|
instrument_expression(if_node->pn_kid1, f); |
790 |
|
Stream_write_char(f, ')'); |
791 |
|
} |
792 |
|
Stream_write_char(f, ']'); |
793 |
|
} |
794 |
|
break; |
795 |
|
case TOK_VAR: |
796 |
|
assert(node->pn_arity == PN_LIST); |
797 |
|
Stream_write_string(f, "var "); |
798 |
|
instrument_declarations(node, f); |
799 |
|
break; |
800 |
|
case TOK_LET: |
801 |
|
assert(node->pn_arity == PN_LIST); |
802 |
|
Stream_write_string(f, "let "); |
803 |
|
instrument_declarations(node, f); |
804 |
|
break; |
805 |
default: |
default: |
806 |
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); |
807 |
} |
} |
808 |
} |
} |
809 |
|
|
810 |
static void instrument_var_statement(JSParseNode * node, Stream * f, int indent) { |
static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) { |
|
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); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
static void output_statement(JSParseNode * node, Stream * f, int indent) { |
|
811 |
switch (node->pn_type) { |
switch (node->pn_type) { |
812 |
case TOK_FUNCTION: |
case TOK_FUNCTION: |
813 |
instrument_function(node, f, indent, FUNCTION_NORMAL); |
instrument_function(node, f, indent, FUNCTION_NORMAL); |
818 |
Stream_write_string(f, "{\n"); |
Stream_write_string(f, "{\n"); |
819 |
*/ |
*/ |
820 |
for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) { |
for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) { |
821 |
instrument_statement(p, f, indent); |
instrument_statement(p, f, indent, false); |
822 |
} |
} |
823 |
/* |
/* |
824 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
826 |
*/ |
*/ |
827 |
break; |
break; |
828 |
case TOK_IF: |
case TOK_IF: |
829 |
|
{ |
830 |
assert(node->pn_arity == PN_TERNARY); |
assert(node->pn_arity == PN_TERNARY); |
831 |
|
|
832 |
|
uint16_t line = node->pn_pos.begin.lineno; |
833 |
|
if (! is_jscoverage_if) { |
834 |
|
if (line > num_lines) { |
835 |
|
fatal("%s: script contains more than 65,535 lines", file_id); |
836 |
|
} |
837 |
|
if (line >= 2 && exclusive_directives[line - 2]) { |
838 |
|
is_jscoverage_if = true; |
839 |
|
} |
840 |
|
} |
841 |
|
|
842 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
843 |
Stream_write_string(f, "if ("); |
Stream_write_string(f, "if ("); |
844 |
instrument_expression(node->pn_kid1, f); |
instrument_expression(node->pn_kid1, f); |
845 |
Stream_write_string(f, ") {\n"); |
Stream_write_string(f, ") {\n"); |
846 |
instrument_statement(node->pn_kid2, f, indent + 2); |
if (is_jscoverage_if && node->pn_kid3) { |
847 |
|
uint16_t else_start = node->pn_kid3->pn_pos.begin.lineno; |
848 |
|
uint16_t else_end = node->pn_kid3->pn_pos.end.lineno + 1; |
849 |
|
Stream_printf(f, "%*s", indent + 2, ""); |
850 |
|
Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, else_start, else_end); |
851 |
|
} |
852 |
|
instrument_statement(node->pn_kid2, f, indent + 2, false); |
853 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
854 |
Stream_write_string(f, "}\n"); |
Stream_write_string(f, "}\n"); |
855 |
if (node->pn_kid3) { |
|
856 |
|
if (node->pn_kid3 || is_jscoverage_if) { |
857 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
858 |
Stream_write_string(f, "else {\n"); |
Stream_write_string(f, "else {\n"); |
859 |
instrument_statement(node->pn_kid3, f, indent + 2); |
|
860 |
|
if (is_jscoverage_if) { |
861 |
|
uint16_t if_start = node->pn_kid2->pn_pos.begin.lineno + 1; |
862 |
|
uint16_t if_end = node->pn_kid2->pn_pos.end.lineno + 1; |
863 |
|
Stream_printf(f, "%*s", indent + 2, ""); |
864 |
|
Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_start, if_end); |
865 |
|
} |
866 |
|
|
867 |
|
if (node->pn_kid3) { |
868 |
|
instrument_statement(node->pn_kid3, f, indent + 2, is_jscoverage_if); |
869 |
|
} |
870 |
|
|
871 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
872 |
Stream_write_string(f, "}\n"); |
Stream_write_string(f, "}\n"); |
873 |
} |
} |
874 |
|
|
875 |
break; |
break; |
876 |
|
} |
877 |
case TOK_SWITCH: |
case TOK_SWITCH: |
878 |
assert(node->pn_arity == PN_BINARY); |
assert(node->pn_arity == PN_BINARY); |
879 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
895 |
abort(); |
abort(); |
896 |
break; |
break; |
897 |
} |
} |
898 |
instrument_statement(p->pn_right, f, indent + 2); |
instrument_statement(p->pn_right, f, indent + 2, false); |
899 |
} |
} |
900 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
901 |
Stream_write_string(f, "}\n"); |
Stream_write_string(f, "}\n"); |
910 |
Stream_write_string(f, "while ("); |
Stream_write_string(f, "while ("); |
911 |
instrument_expression(node->pn_left, f); |
instrument_expression(node->pn_left, f); |
912 |
Stream_write_string(f, ") {\n"); |
Stream_write_string(f, ") {\n"); |
913 |
instrument_statement(node->pn_right, f, indent + 2); |
instrument_statement(node->pn_right, f, indent + 2, false); |
914 |
Stream_write_string(f, "}\n"); |
Stream_write_string(f, "}\n"); |
915 |
break; |
break; |
916 |
case TOK_DO: |
case TOK_DO: |
917 |
assert(node->pn_arity == PN_BINARY); |
assert(node->pn_arity == PN_BINARY); |
918 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
919 |
Stream_write_string(f, "do {\n"); |
Stream_write_string(f, "do {\n"); |
920 |
instrument_statement(node->pn_left, f, indent + 2); |
instrument_statement(node->pn_left, f, indent + 2, false); |
921 |
Stream_write_string(f, "}\n"); |
Stream_write_string(f, "}\n"); |
922 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
923 |
Stream_write_string(f, "while ("); |
Stream_write_string(f, "while ("); |
927 |
case TOK_FOR: |
case TOK_FOR: |
928 |
assert(node->pn_arity == PN_BINARY); |
assert(node->pn_arity == PN_BINARY); |
929 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
|
Stream_write_string(f, "for ("); |
|
930 |
switch (node->pn_left->pn_type) { |
switch (node->pn_left->pn_type) { |
931 |
case TOK_IN: |
case TOK_IN: |
932 |
/* for/in */ |
/* for/in */ |
933 |
assert(node->pn_left->pn_arity == PN_BINARY); |
assert(node->pn_left->pn_arity == PN_BINARY); |
934 |
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); |
|
935 |
break; |
break; |
936 |
case TOK_RESERVED: |
case TOK_RESERVED: |
937 |
/* for (;;) */ |
/* for (;;) */ |
938 |
assert(node->pn_left->pn_arity == PN_TERNARY); |
assert(node->pn_left->pn_arity == PN_TERNARY); |
939 |
|
Stream_write_string(f, "for ("); |
940 |
if (node->pn_left->pn_kid1) { |
if (node->pn_left->pn_kid1) { |
941 |
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); |
|
|
} |
|
942 |
} |
} |
943 |
Stream_write_string(f, ";"); |
Stream_write_string(f, ";"); |
944 |
if (node->pn_left->pn_kid2) { |
if (node->pn_left->pn_kid2) { |
950 |
Stream_write_char(f, ' '); |
Stream_write_char(f, ' '); |
951 |
instrument_expression(node->pn_left->pn_kid3, f); |
instrument_expression(node->pn_left->pn_kid3, f); |
952 |
} |
} |
953 |
|
Stream_write_char(f, ')'); |
954 |
break; |
break; |
955 |
default: |
default: |
956 |
abort(); |
abort(); |
957 |
break; |
break; |
958 |
} |
} |
959 |
Stream_write_string(f, ") {\n"); |
Stream_write_string(f, " {\n"); |
960 |
instrument_statement(node->pn_right, f, indent + 2); |
instrument_statement(node->pn_right, f, indent + 2, false); |
961 |
Stream_write_string(f, "}\n"); |
Stream_write_string(f, "}\n"); |
962 |
break; |
break; |
963 |
case TOK_THROW: |
case TOK_THROW: |
970 |
case TOK_TRY: |
case TOK_TRY: |
971 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
972 |
Stream_write_string(f, "try {\n"); |
Stream_write_string(f, "try {\n"); |
973 |
instrument_statement(node->pn_kid1, f, indent + 2); |
instrument_statement(node->pn_kid1, f, indent + 2, false); |
974 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
975 |
Stream_write_string(f, "}\n"); |
Stream_write_string(f, "}\n"); |
976 |
{ |
if (node->pn_kid2) { |
977 |
for (JSParseNode * catch = node->pn_kid2; catch != NULL; catch = catch->pn_kid2) { |
assert(node->pn_kid2->pn_type == TOK_RESERVED); |
978 |
|
for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) { |
979 |
|
assert(scope->pn_type == TOK_LEXICALSCOPE); |
980 |
|
JSParseNode * catch = scope->pn_expr; |
981 |
assert(catch->pn_type == TOK_CATCH); |
assert(catch->pn_type == TOK_CATCH); |
982 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
983 |
Stream_write_string(f, "catch ("); |
Stream_write_string(f, "catch ("); |
984 |
|
/* this may not be a name - destructuring assignment */ |
985 |
|
/* |
986 |
assert(catch->pn_kid1->pn_arity == PN_NAME); |
assert(catch->pn_kid1->pn_arity == PN_NAME); |
987 |
print_string_atom(catch->pn_kid1->pn_atom, f); |
print_string_atom(catch->pn_kid1->pn_atom, f); |
988 |
if (catch->pn_kid1->pn_expr) { |
*/ |
989 |
|
instrument_expression(catch->pn_kid1, f); |
990 |
|
if (catch->pn_kid2) { |
991 |
Stream_write_string(f, " if "); |
Stream_write_string(f, " if "); |
992 |
instrument_expression(catch->pn_kid1->pn_expr, f); |
instrument_expression(catch->pn_kid2, f); |
993 |
} |
} |
994 |
Stream_write_string(f, ") {\n"); |
Stream_write_string(f, ") {\n"); |
995 |
instrument_statement(catch->pn_kid3, f, indent + 2); |
instrument_statement(catch->pn_kid3, f, indent + 2, false); |
996 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
997 |
Stream_write_string(f, "}\n"); |
Stream_write_string(f, "}\n"); |
998 |
} |
} |
1000 |
if (node->pn_kid3) { |
if (node->pn_kid3) { |
1001 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
1002 |
Stream_write_string(f, "finally {\n"); |
Stream_write_string(f, "finally {\n"); |
1003 |
instrument_statement(node->pn_kid3, f, indent + 2); |
instrument_statement(node->pn_kid3, f, indent + 2, false); |
1004 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
1005 |
Stream_write_string(f, "}\n"); |
Stream_write_string(f, "}\n"); |
1006 |
} |
} |
1026 |
Stream_write_string(f, "with ("); |
Stream_write_string(f, "with ("); |
1027 |
instrument_expression(node->pn_left, f); |
instrument_expression(node->pn_left, f); |
1028 |
Stream_write_string(f, ") {\n"); |
Stream_write_string(f, ") {\n"); |
1029 |
instrument_statement(node->pn_right, f, indent + 2); |
instrument_statement(node->pn_right, f, indent + 2, false); |
1030 |
Stream_printf(f, "%*s", indent, ""); |
Stream_printf(f, "%*s", indent, ""); |
1031 |
Stream_write_string(f, "}\n"); |
Stream_write_string(f, "}\n"); |
1032 |
break; |
break; |
1033 |
case TOK_VAR: |
case TOK_VAR: |
1034 |
instrument_var_statement(node, f, indent); |
Stream_printf(f, "%*s", indent, ""); |
1035 |
|
instrument_expression(node, f); |
1036 |
Stream_write_string(f, ";\n"); |
Stream_write_string(f, ";\n"); |
1037 |
break; |
break; |
1038 |
case TOK_RETURN: |
case TOK_RETURN: |
1065 |
/* |
/* |
1066 |
... use output_statement instead of instrument_statement. |
... use output_statement instead of instrument_statement. |
1067 |
*/ |
*/ |
1068 |
output_statement(node->pn_expr, f, indent); |
output_statement(node->pn_expr, f, indent, false); |
1069 |
|
break; |
1070 |
|
case TOK_LEXICALSCOPE: |
1071 |
|
/* let statement */ |
1072 |
|
assert(node->pn_arity == PN_NAME); |
1073 |
|
switch (node->pn_expr->pn_type) { |
1074 |
|
case TOK_LET: |
1075 |
|
/* let statement */ |
1076 |
|
assert(node->pn_expr->pn_arity == PN_BINARY); |
1077 |
|
instrument_statement(node->pn_expr, f, indent, false); |
1078 |
|
break; |
1079 |
|
case TOK_LC: |
1080 |
|
/* block */ |
1081 |
|
Stream_printf(f, "%*s", indent, ""); |
1082 |
|
Stream_write_string(f, "{\n"); |
1083 |
|
instrument_statement(node->pn_expr, f, indent + 2, false); |
1084 |
|
Stream_printf(f, "%*s", indent, ""); |
1085 |
|
Stream_write_string(f, "}\n"); |
1086 |
|
break; |
1087 |
|
case TOK_FOR: |
1088 |
|
instrument_statement(node->pn_expr, f, indent, false); |
1089 |
|
break; |
1090 |
|
default: |
1091 |
|
abort(); |
1092 |
|
break; |
1093 |
|
} |
1094 |
|
break; |
1095 |
|
case TOK_LET: |
1096 |
|
switch (node->pn_arity) { |
1097 |
|
case PN_BINARY: |
1098 |
|
/* let statement */ |
1099 |
|
Stream_printf(f, "%*s", indent, ""); |
1100 |
|
Stream_write_string(f, "let ("); |
1101 |
|
assert(node->pn_left->pn_type == TOK_LP); |
1102 |
|
assert(node->pn_left->pn_arity == PN_LIST); |
1103 |
|
instrument_declarations(node->pn_left, f); |
1104 |
|
Stream_write_string(f, ") {\n"); |
1105 |
|
instrument_statement(node->pn_right, f, indent + 2, false); |
1106 |
|
Stream_printf(f, "%*s", indent, ""); |
1107 |
|
Stream_write_string(f, "}\n"); |
1108 |
|
break; |
1109 |
|
case PN_LIST: |
1110 |
|
/* let definition */ |
1111 |
|
Stream_printf(f, "%*s", indent, ""); |
1112 |
|
instrument_expression(node, f); |
1113 |
|
Stream_write_string(f, ";\n"); |
1114 |
|
break; |
1115 |
|
default: |
1116 |
|
abort(); |
1117 |
|
break; |
1118 |
|
} |
1119 |
|
break; |
1120 |
|
case TOK_DEBUGGER: |
1121 |
|
Stream_printf(f, "%*s", indent, ""); |
1122 |
|
Stream_write_string(f, "debugger;\n"); |
1123 |
break; |
break; |
1124 |
default: |
default: |
1125 |
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); |
1131 |
TOK_FUNCTION is handled as a statement and as an expression. |
TOK_FUNCTION is handled as a statement and as an expression. |
1132 |
TOK_EXPORT, TOK_IMPORT are not handled. |
TOK_EXPORT, TOK_IMPORT are not handled. |
1133 |
*/ |
*/ |
1134 |
static void instrument_statement(JSParseNode * node, Stream * f, int indent) { |
static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) { |
1135 |
if (node->pn_type != TOK_LC) { |
if (node->pn_type != TOK_LC && node->pn_type != TOK_LEXICALSCOPE) { |
1136 |
uint16_t line = node->pn_pos.begin.lineno; |
uint16_t line = node->pn_pos.begin.lineno; |
1137 |
if (line > num_lines) { |
if (line > num_lines) { |
1138 |
fatal("%s: script contains more than 65,535 lines", file_id); |
fatal("%s: script contains more than 65,535 lines", file_id); |
1145 |
lines[line - 1] = 1; |
lines[line - 1] = 1; |
1146 |
} |
} |
1147 |
} |
} |
1148 |
output_statement(node, f, indent); |
output_statement(node, f, indent, is_jscoverage_if); |
1149 |
} |
} |
1150 |
|
|
1151 |
static bool characters_start_with(const jschar * characters, size_t line_start, size_t line_end, const char * prefix) { |
static bool characters_start_with(const jschar * characters, size_t line_start, size_t line_end, const char * prefix) { |
1167 |
} |
} |
1168 |
} |
} |
1169 |
|
|
1170 |
|
static bool characters_are_white_space(const jschar * characters, size_t line_start, size_t line_end) { |
1171 |
|
/* XXX - other Unicode space */ |
1172 |
|
const jschar * end = characters + line_end; |
1173 |
|
for (const jschar * p = characters + line_start; p < end; p++) { |
1174 |
|
jschar c = *p; |
1175 |
|
if (c == 0x9 || c == 0xB || c == 0xC || c == 0x20 || c == 0xA0) { |
1176 |
|
continue; |
1177 |
|
} |
1178 |
|
else { |
1179 |
|
return false; |
1180 |
|
} |
1181 |
|
} |
1182 |
|
return true; |
1183 |
|
} |
1184 |
|
|
1185 |
|
static void error_reporter(JSContext * context, const char * message, JSErrorReport * report) { |
1186 |
|
fprintf(stderr, "jscoverage: parse error: line %u: %s\n", report->lineno, message); |
1187 |
|
} |
1188 |
|
|
1189 |
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) { |
1190 |
file_id = id; |
file_id = id; |
1191 |
|
|
1192 |
/* scan the javascript */ |
/* parse the javascript */ |
1193 |
JSTokenStream * token_stream = js_NewTokenStream(context, characters, num_characters, NULL, 1, NULL); |
JSParseContext parse_context; |
1194 |
if (token_stream == NULL) { |
if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) { |
1195 |
fatal("cannot create token stream from file: %s", file_id); |
fatal("cannot create token stream from file: %s", file_id); |
1196 |
} |
} |
1197 |
|
JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter); |
1198 |
/* parse the javascript */ |
JSParseNode * node = js_ParseScript(context, global, &parse_context); |
|
JSParseNode * node = js_ParseTokenStream(context, global, token_stream); |
|
1199 |
if (node == NULL) { |
if (node == NULL) { |
1200 |
|
js_ReportUncaughtException(context); |
1201 |
fatal("parse error in file: %s", file_id); |
fatal("parse error in file: %s", file_id); |
1202 |
} |
} |
1203 |
|
JS_SetErrorReporter(context, old_error_reporter); |
1204 |
num_lines = node->pn_pos.end.lineno; |
num_lines = node->pn_pos.end.lineno; |
1205 |
lines = xmalloc(num_lines); |
lines = xmalloc(num_lines); |
1206 |
for (int i = 0; i < num_lines; i++) { |
for (unsigned int i = 0; i < num_lines; i++) { |
1207 |
lines[i] = 0; |
lines[i] = 0; |
1208 |
} |
} |
1209 |
|
|
1210 |
/* |
/* search code for conditionals */ |
1211 |
An instrumented JavaScript file has 3 sections: |
exclusive_directives = xnew(bool, num_lines); |
1212 |
1. initialization |
for (unsigned int i = 0; i < num_lines; i++) { |
1213 |
2. instrumented source code |
exclusive_directives[i] = false; |
|
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); |
|
|
} |
|
1214 |
} |
} |
|
Stream_write_string(output, "}\n"); |
|
|
free(lines); |
|
|
lines = NULL; |
|
1215 |
|
|
|
/* copy the instrumented source code to the output */ |
|
|
Stream_write(output, instrumented->data, instrumented->length); |
|
|
|
|
|
/* conditionals */ |
|
1216 |
bool has_conditionals = false; |
bool has_conditionals = false; |
1217 |
|
struct IfDirective * if_directives = NULL; |
1218 |
size_t line_number = 0; |
size_t line_number = 0; |
1219 |
size_t i = 0; |
size_t i = 0; |
1220 |
while (i < num_characters) { |
while (i < num_characters) { |
1221 |
|
if (line_number == UINT16_MAX) { |
1222 |
|
fatal("%s: script has more than 65,535 lines", file_id); |
1223 |
|
} |
1224 |
line_number++; |
line_number++; |
1225 |
size_t line_start = i; |
size_t line_start = i; |
1226 |
jschar c; |
jschar c; |
1236 |
break; |
break; |
1237 |
default: |
default: |
1238 |
i++; |
i++; |
|
break; |
|
1239 |
} |
} |
1240 |
} |
} |
1241 |
size_t line_end = i; |
size_t line_end = i; |
1245 |
i++; |
i++; |
1246 |
} |
} |
1247 |
} |
} |
1248 |
|
|
1249 |
if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) { |
if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) { |
1250 |
if (! has_conditionals) { |
has_conditionals = true; |
1251 |
has_conditionals = true; |
|
1252 |
Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id); |
if (characters_are_white_space(characters, line_start + 16, line_end)) { |
1253 |
} |
exclusive_directives[line_number - 1] = true; |
1254 |
Stream_write_string(output, "if (!("); |
} |
1255 |
for (size_t j = line_start + 16; j < line_end; j++) { |
else { |
1256 |
jschar c = characters[j]; |
struct IfDirective * if_directive = xnew(struct IfDirective, 1); |
1257 |
if (c == '\t' || (32 <= c && c <= 126)) { |
if_directive->condition_start = characters + line_start + 16; |
1258 |
Stream_write_char(output, c); |
if_directive->condition_end = characters + line_end; |
1259 |
} |
if_directive->start_line = line_number; |
1260 |
else { |
if_directive->end_line = 0; |
1261 |
Stream_printf(output, "\\u%04x", c); |
if_directive->next = if_directives; |
1262 |
} |
if_directives = if_directive; |
1263 |
} |
} |
|
Stream_write_string(output, ")) {\n"); |
|
|
Stream_printf(output, " _$jscoverage['%s'].conditionals[%d] = ", file_id, line_number); |
|
1264 |
} |
} |
1265 |
else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) { |
else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) { |
1266 |
Stream_printf(output, "%d;\n", line_number); |
for (struct IfDirective * p = if_directives; p != NULL; p = p->next) { |
1267 |
Stream_printf(output, "}\n"); |
if (p->end_line == 0) { |
1268 |
|
p->end_line = line_number; |
1269 |
|
break; |
1270 |
|
} |
1271 |
|
} |
1272 |
} |
} |
1273 |
} |
} |
1274 |
|
|
1275 |
|
/* |
1276 |
|
An instrumented JavaScript file has 4 sections: |
1277 |
|
1. initialization |
1278 |
|
2. instrumented source code |
1279 |
|
3. conditionals |
1280 |
|
4. original source code |
1281 |
|
*/ |
1282 |
|
|
1283 |
|
Stream * instrumented = Stream_new(0); |
1284 |
|
instrument_statement(node, instrumented, 0, false); |
1285 |
|
js_FinishParseContext(context, &parse_context); |
1286 |
|
|
1287 |
|
/* write line number info to the output */ |
1288 |
|
Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n"); |
1289 |
|
Stream_write_string(output, "if (! top._$jscoverage) {\n top._$jscoverage = {};\n}\n"); |
1290 |
|
Stream_write_string(output, "var _$jscoverage = top._$jscoverage;\n"); |
1291 |
|
Stream_printf(output, "if (! _$jscoverage['%s']) {\n", file_id); |
1292 |
|
Stream_printf(output, " _$jscoverage['%s'] = [];\n", file_id); |
1293 |
|
for (int i = 0; i < num_lines; i++) { |
1294 |
|
if (lines[i]) { |
1295 |
|
Stream_printf(output, " _$jscoverage['%s'][%d] = 0;\n", file_id, i + 1); |
1296 |
|
} |
1297 |
|
} |
1298 |
|
Stream_write_string(output, "}\n"); |
1299 |
|
free(lines); |
1300 |
|
lines = NULL; |
1301 |
|
free(exclusive_directives); |
1302 |
|
exclusive_directives = NULL; |
1303 |
|
|
1304 |
|
/* conditionals */ |
1305 |
|
if (has_conditionals) { |
1306 |
|
Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id); |
1307 |
|
} |
1308 |
|
|
1309 |
|
/* copy the instrumented source code to the output */ |
1310 |
|
Stream_write(output, instrumented->data, instrumented->length); |
1311 |
|
|
1312 |
|
/* conditionals */ |
1313 |
|
for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) { |
1314 |
|
Stream_write_string(output, "if (!("); |
1315 |
|
print_javascript(if_directive->condition_start, if_directive->condition_end - if_directive->condition_start, output); |
1316 |
|
Stream_write_string(output, ")) {\n"); |
1317 |
|
Stream_printf(output, " _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line); |
1318 |
|
Stream_write_string(output, "}\n"); |
1319 |
|
} |
1320 |
|
|
1321 |
|
/* free */ |
1322 |
|
while (if_directives != NULL) { |
1323 |
|
struct IfDirective * if_directive = if_directives; |
1324 |
|
if_directives = if_directives->next; |
1325 |
|
free(if_directive); |
1326 |
|
} |
1327 |
|
|
1328 |
/* copy the original source to the output */ |
/* copy the original source to the output */ |
1329 |
Stream_printf(output, "_$jscoverage['%s'].source = ", file_id); |
Stream_printf(output, "_$jscoverage['%s'].source = ", file_id); |
1330 |
jscoverage_write_source(id, characters, num_characters, output); |
jscoverage_write_source(id, characters, num_characters, output); |
1363 |
/* line feed (new line) */ |
/* line feed (new line) */ |
1364 |
done = true; |
done = true; |
1365 |
break; |
break; |
1366 |
|
/* IE doesn't support this */ |
1367 |
|
/* |
1368 |
case 0xb: |
case 0xb: |
|
/* vertical tab */ |
|
1369 |
Stream_write_string(output, "\\v"); |
Stream_write_string(output, "\\v"); |
1370 |
break; |
break; |
1371 |
|
*/ |
1372 |
case 0xc: |
case 0xc: |
1373 |
/* form feed */ |
/* form feed */ |
1374 |
Stream_write_string(output, "\\f"); |
Stream_write_string(output, "\\f"); |
1423 |
/* line feed (new line) */ |
/* line feed (new line) */ |
1424 |
done = true; |
done = true; |
1425 |
break; |
break; |
1426 |
|
/* IE doesn't support this */ |
1427 |
|
/* |
1428 |
case 0xb: |
case 0xb: |
|
/* vertical tab */ |
|
1429 |
Stream_write_string(output, "\\v"); |
Stream_write_string(output, "\\v"); |
1430 |
break; |
break; |
1431 |
|
*/ |
1432 |
case 0xc: |
case 0xc: |
1433 |
/* form feed */ |
/* form feed */ |
1434 |
Stream_write_string(output, "\\f"); |
Stream_write_string(output, "\\f"); |
1555 |
} |
} |
1556 |
|
|
1557 |
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) { |
1558 |
|
int result = 0; |
1559 |
|
|
1560 |
jschar * base = js_InflateString(context, (char *) json, &length); |
jschar * base = js_InflateString(context, (char *) json, &length); |
1561 |
if (base == NULL) { |
if (base == NULL) { |
1562 |
fatal("out of memory"); |
fatal("out of memory"); |
1569 |
|
|
1570 |
JS_free(context, base); |
JS_free(context, base); |
1571 |
|
|
1572 |
JSTokenStream * token_stream = js_NewTokenStream(context, parenthesized_json, length + 2, NULL, 1, NULL); |
JSParseContext parse_context; |
1573 |
if (token_stream == NULL) { |
if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) { |
1574 |
fatal("cannot create token stream"); |
free(parenthesized_json); |
1575 |
|
return -1; |
1576 |
} |
} |
1577 |
|
JSParseNode * root = js_ParseScript(context, global, &parse_context); |
|
JSParseNode * root = js_ParseTokenStream(context, global, token_stream); |
|
1578 |
free(parenthesized_json); |
free(parenthesized_json); |
1579 |
if (root == NULL) { |
if (root == NULL) { |
1580 |
return -1; |
result = -1; |
1581 |
|
goto done; |
1582 |
} |
} |
1583 |
|
|
1584 |
/* root node must be TOK_LC */ |
/* root node must be TOK_LC */ |
1585 |
if (root->pn_type != TOK_LC) { |
if (root->pn_type != TOK_LC) { |
1586 |
return -1; |
result = -1; |
1587 |
|
goto done; |
1588 |
} |
} |
1589 |
JSParseNode * semi = root->pn_u.list.head; |
JSParseNode * semi = root->pn_u.list.head; |
1590 |
|
|
1591 |
/* 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 */ |
1592 |
if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) { |
if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) { |
1593 |
return -1; |
result = -1; |
1594 |
|
goto done; |
1595 |
} |
} |
1596 |
JSParseNode * parenthesized = semi->pn_kid; |
JSParseNode * parenthesized = semi->pn_kid; |
1597 |
|
|
1598 |
/* this must be a parenthesized expression */ |
/* this must be a parenthesized expression */ |
1599 |
if (parenthesized->pn_type != TOK_RP) { |
if (parenthesized->pn_type != TOK_RP) { |
1600 |
return -1; |
result = -1; |
1601 |
|
goto done; |
1602 |
} |
} |
1603 |
JSParseNode * object = parenthesized->pn_kid; |
JSParseNode * object = parenthesized->pn_kid; |
1604 |
|
|
1605 |
/* this must be an object literal */ |
/* this must be an object literal */ |
1606 |
if (object->pn_type != TOK_RC) { |
if (object->pn_type != TOK_RC) { |
1607 |
return -1; |
result = -1; |
1608 |
|
goto done; |
1609 |
} |
} |
1610 |
|
|
1611 |
for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) { |
for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) { |
1612 |
/* every element of this list must be TOK_COLON */ |
/* every element of this list must be TOK_COLON */ |
1613 |
if (p->pn_type != TOK_COLON) { |
if (p->pn_type != TOK_COLON) { |
1614 |
return -1; |
result = -1; |
1615 |
|
goto done; |
1616 |
} |
} |
1617 |
|
|
1618 |
/* the key must be a string representing the file */ |
/* the key must be a string representing the file */ |
1619 |
JSParseNode * key = p->pn_left; |
JSParseNode * key = p->pn_left; |
1620 |
if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) { |
if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) { |
1621 |
return -1; |
result = -1; |
1622 |
|
goto done; |
1623 |
} |
} |
1624 |
char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom)); |
char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom)); |
1625 |
|
|
1626 |
/* the value must be an object literal OR an array */ |
/* the value must be an object literal OR an array */ |
1627 |
JSParseNode * value = p->pn_right; |
JSParseNode * value = p->pn_right; |
1628 |
if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) { |
if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) { |
1629 |
return -1; |
result = -1; |
1630 |
|
goto done; |
1631 |
} |
} |
1632 |
|
|
1633 |
JSParseNode * array = NULL; |
JSParseNode * array = NULL; |
1639 |
else if (value->pn_type == TOK_RC) { |
else if (value->pn_type == TOK_RC) { |
1640 |
/* an object literal */ |
/* an object literal */ |
1641 |
if (value->pn_count != 2) { |
if (value->pn_count != 2) { |
1642 |
return -1; |
result = -1; |
1643 |
|
goto done; |
1644 |
} |
} |
1645 |
for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) { |
for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) { |
1646 |
if (element->pn_type != TOK_COLON) { |
if (element->pn_type != TOK_COLON) { |
1647 |
return -1; |
result = -1; |
1648 |
|
goto done; |
1649 |
} |
} |
1650 |
JSParseNode * left = element->pn_left; |
JSParseNode * left = element->pn_left; |
1651 |
if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) { |
if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) { |
1652 |
return -1; |
result = -1; |
1653 |
|
goto done; |
1654 |
} |
} |
1655 |
const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom)); |
const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom)); |
1656 |
if (strcmp(s, "coverage") == 0) { |
if (strcmp(s, "coverage") == 0) { |
1657 |
array = element->pn_right; |
array = element->pn_right; |
1658 |
if (array->pn_type != TOK_RB) { |
if (array->pn_type != TOK_RB) { |
1659 |
return -1; |
result = -1; |
1660 |
|
goto done; |
1661 |
} |
} |
1662 |
} |
} |
1663 |
else if (strcmp(s, "source") == 0) { |
else if (strcmp(s, "source") == 0) { |
1664 |
source = element->pn_right; |
source = element->pn_right; |
1665 |
if (source->pn_type != TOK_RB) { |
if (source->pn_type != TOK_RB) { |
1666 |
return -1; |
result = -1; |
1667 |
|
goto done; |
1668 |
} |
} |
1669 |
} |
} |
1670 |
else { |
else { |
1671 |
return -1; |
result = -1; |
1672 |
|
goto done; |
1673 |
} |
} |
1674 |
} |
} |
1675 |
} |
} |
1676 |
else { |
else { |
1677 |
return -1; |
result = -1; |
1678 |
|
goto done; |
1679 |
} |
} |
1680 |
|
|
1681 |
if (array == NULL) { |
if (array == NULL) { |
1682 |
return -1; |
result = -1; |
1683 |
|
goto done; |
1684 |
} |
} |
1685 |
|
|
1686 |
/* look up the file in the coverage table */ |
/* look up the file in the coverage table */ |
1692 |
file_coverage->id = id; |
file_coverage->id = id; |
1693 |
file_coverage->num_coverage_lines = array->pn_count; |
file_coverage->num_coverage_lines = array->pn_count; |
1694 |
file_coverage->coverage_lines = xnew(int, array->pn_count); |
file_coverage->coverage_lines = xnew(int, array->pn_count); |
1695 |
if (source == NULL) { |
file_coverage->source_lines = 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); |
|
|
} |
|
1696 |
|
|
1697 |
/* set coverage for all lines */ |
/* set coverage for all lines */ |
1698 |
uint32 i = 0; |
uint32 i = 0; |
1704 |
file_coverage->coverage_lines[i] = -1; |
file_coverage->coverage_lines[i] = -1; |
1705 |
} |
} |
1706 |
else { |
else { |
1707 |
return -1; |
result = -1; |
1708 |
|
goto done; |
1709 |
} |
} |
1710 |
} |
} |
1711 |
assert(i == array->pn_count); |
assert(i == array->pn_count); |
1721 |
/* sanity check */ |
/* sanity check */ |
1722 |
assert(strcmp(file_coverage->id, id_bytes) == 0); |
assert(strcmp(file_coverage->id, id_bytes) == 0); |
1723 |
if (file_coverage->num_coverage_lines != array->pn_count) { |
if (file_coverage->num_coverage_lines != array->pn_count) { |
1724 |
return -2; |
result = -2; |
1725 |
|
goto done; |
1726 |
} |
} |
1727 |
|
|
1728 |
/* merge the coverage */ |
/* merge the coverage */ |
1730 |
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++) { |
1731 |
if (element->pn_type == TOK_NUMBER) { |
if (element->pn_type == TOK_NUMBER) { |
1732 |
if (file_coverage->coverage_lines[i] == -1) { |
if (file_coverage->coverage_lines[i] == -1) { |
1733 |
return -2; |
result = -2; |
1734 |
|
goto done; |
1735 |
} |
} |
1736 |
file_coverage->coverage_lines[i] += (int) element->pn_dval; |
file_coverage->coverage_lines[i] += (int) element->pn_dval; |
1737 |
} |
} |
1738 |
else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) { |
else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) { |
1739 |
if (file_coverage->coverage_lines[i] != -1) { |
if (file_coverage->coverage_lines[i] != -1) { |
1740 |
return -2; |
result = -2; |
1741 |
|
goto done; |
1742 |
} |
} |
1743 |
} |
} |
1744 |
else { |
else { |
1745 |
return -1; |
result = -1; |
1746 |
|
goto done; |
1747 |
} |
} |
1748 |
} |
} |
1749 |
assert(i == array->pn_count); |
assert(i == array->pn_count); |
1750 |
|
} |
1751 |
|
|
1752 |
/* if this JSON file has source, use it */ |
/* if this JSON file has source, use it */ |
1753 |
if (file_coverage->source_lines == NULL && source != NULL) { |
if (file_coverage->source_lines == NULL && source != NULL) { |
1754 |
file_coverage->num_source_lines = source->pn_count; |
file_coverage->num_source_lines = source->pn_count; |
1755 |
file_coverage->source_lines = xnew(char *, source->pn_count); |
file_coverage->source_lines = xnew(char *, source->pn_count); |
1756 |
uint32 i = 0; |
uint32 i = 0; |
1757 |
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++) { |
1758 |
if (element->pn_type != TOK_STRING) { |
if (element->pn_type != TOK_STRING) { |
1759 |
return -1; |
result = -1; |
1760 |
} |
goto done; |
|
file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom))); |
|
1761 |
} |
} |
1762 |
assert(i == source->pn_count); |
file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom))); |
1763 |
} |
} |
1764 |
|
assert(i == source->pn_count); |
1765 |
} |
} |
1766 |
} |
} |
1767 |
|
|
1768 |
return 0; |
done: |
1769 |
|
js_FinishParseContext(context, &parse_context); |
1770 |
|
return result; |
1771 |
} |
} |