1 |
/* |
2 |
instrument-js.c - JavaScript instrumentation routines |
3 |
Copyright (C) 2007, 2008 siliconforks.com |
4 |
|
5 |
This program is free software; you can redistribute it and/or modify |
6 |
it under the terms of the GNU General Public License as published by |
7 |
the Free Software Foundation; either version 2 of the License, or |
8 |
(at your option) any later version. |
9 |
|
10 |
This program is distributed in the hope that it will be useful, |
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
GNU General Public License for more details. |
14 |
|
15 |
You should have received a copy of the GNU General Public License along |
16 |
with this program; if not, write to the Free Software Foundation, Inc., |
17 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
18 |
*/ |
19 |
|
20 |
#include <config.h> |
21 |
|
22 |
#include "instrument-js.h" |
23 |
|
24 |
#include <assert.h> |
25 |
#include <stdlib.h> |
26 |
#include <string.h> |
27 |
|
28 |
#include <jsapi.h> |
29 |
#include <jsarena.h> |
30 |
#include <jsatom.h> |
31 |
#include <jsexn.h> |
32 |
#include <jsfun.h> |
33 |
#include <jsinterp.h> |
34 |
#include <jsparse.h> |
35 |
#include <jsregexp.h> |
36 |
#include <jsscope.h> |
37 |
#include <jsstr.h> |
38 |
|
39 |
#include "encoding.h" |
40 |
#include "global.h" |
41 |
#include "highlight.h" |
42 |
#include "resource-manager.h" |
43 |
#include "util.h" |
44 |
|
45 |
struct IfDirective { |
46 |
const jschar * condition_start; |
47 |
const jschar * condition_end; |
48 |
uint16_t start_line; |
49 |
uint16_t end_line; |
50 |
struct IfDirective * next; |
51 |
}; |
52 |
|
53 |
static bool * exclusive_directives = NULL; |
54 |
|
55 |
static JSRuntime * runtime = NULL; |
56 |
static JSContext * context = NULL; |
57 |
static JSObject * global = NULL; |
58 |
|
59 |
/* |
60 |
JSParseNode objects store line numbers starting from 1. |
61 |
The lines array stores line numbers starting from 0. |
62 |
*/ |
63 |
static const char * file_id = NULL; |
64 |
static char * lines = NULL; |
65 |
static uint16_t num_lines = 0; |
66 |
|
67 |
void jscoverage_init(void) { |
68 |
runtime = JS_NewRuntime(8L * 1024L * 1024L); |
69 |
if (runtime == NULL) { |
70 |
fatal("cannot create runtime"); |
71 |
} |
72 |
|
73 |
context = JS_NewContext(runtime, 8192); |
74 |
if (context == NULL) { |
75 |
fatal("cannot create context"); |
76 |
} |
77 |
|
78 |
global = JS_NewObject(context, NULL, NULL, NULL); |
79 |
if (global == NULL) { |
80 |
fatal("cannot create global object"); |
81 |
} |
82 |
|
83 |
if (! JS_InitStandardClasses(context, global)) { |
84 |
fatal("cannot initialize standard classes"); |
85 |
} |
86 |
} |
87 |
|
88 |
void jscoverage_cleanup(void) { |
89 |
JS_DestroyContext(context); |
90 |
JS_DestroyRuntime(runtime); |
91 |
} |
92 |
|
93 |
static void print_javascript(const jschar * characters, size_t num_characters, Stream * f) { |
94 |
for (size_t i = 0; i < num_characters; i++) { |
95 |
jschar c = characters[i]; |
96 |
/* |
97 |
XXX does not handle no-break space, other unicode "space separator" |
98 |
*/ |
99 |
switch (c) { |
100 |
case 0x9: |
101 |
case 0xB: |
102 |
case 0xC: |
103 |
Stream_write_char(f, c); |
104 |
break; |
105 |
default: |
106 |
if (32 <= c && c <= 126) { |
107 |
Stream_write_char(f, c); |
108 |
} |
109 |
else { |
110 |
Stream_printf(f, "\\u%04x", c); |
111 |
} |
112 |
break; |
113 |
} |
114 |
} |
115 |
} |
116 |
|
117 |
static void print_string(JSString * s, Stream * f) { |
118 |
size_t length = JSSTRING_LENGTH(s); |
119 |
jschar * characters = JSSTRING_CHARS(s); |
120 |
for (size_t i = 0; i < length; i++) { |
121 |
jschar c = characters[i]; |
122 |
if (32 <= c && c <= 126) { |
123 |
switch (c) { |
124 |
case '"': |
125 |
Stream_write_string(f, "\\\""); |
126 |
break; |
127 |
/* |
128 |
case '\'': |
129 |
Stream_write_string(f, "\\'"); |
130 |
break; |
131 |
*/ |
132 |
case '\\': |
133 |
Stream_write_string(f, "\\\\"); |
134 |
break; |
135 |
default: |
136 |
Stream_write_char(f, c); |
137 |
break; |
138 |
} |
139 |
} |
140 |
else { |
141 |
switch (c) { |
142 |
case 0x8: |
143 |
Stream_write_string(f, "\\b"); |
144 |
break; |
145 |
case 0x9: |
146 |
Stream_write_string(f, "\\t"); |
147 |
break; |
148 |
case 0xa: |
149 |
Stream_write_string(f, "\\n"); |
150 |
break; |
151 |
/* IE doesn't support this */ |
152 |
/* |
153 |
case 0xb: |
154 |
Stream_write_string(f, "\\v"); |
155 |
break; |
156 |
*/ |
157 |
case 0xc: |
158 |
Stream_write_string(f, "\\f"); |
159 |
break; |
160 |
case 0xd: |
161 |
Stream_write_string(f, "\\r"); |
162 |
break; |
163 |
default: |
164 |
Stream_printf(f, "\\u%04x", c); |
165 |
break; |
166 |
} |
167 |
} |
168 |
} |
169 |
} |
170 |
|
171 |
static void print_string_atom(JSAtom * atom, Stream * f) { |
172 |
assert(ATOM_IS_STRING(atom)); |
173 |
JSString * s = ATOM_TO_STRING(atom); |
174 |
print_string(s, f); |
175 |
} |
176 |
|
177 |
static void print_regex(jsval value, Stream * f) { |
178 |
assert(JSVAL_IS_STRING(value)); |
179 |
JSString * s = JSVAL_TO_STRING(value); |
180 |
size_t length = JSSTRING_LENGTH(s); |
181 |
jschar * characters = JSSTRING_CHARS(s); |
182 |
for (size_t i = 0; i < length; i++) { |
183 |
jschar c = characters[i]; |
184 |
if (32 <= c && c <= 126) { |
185 |
Stream_write_char(f, c); |
186 |
} |
187 |
else { |
188 |
Stream_printf(f, "\\u%04x", c); |
189 |
} |
190 |
} |
191 |
} |
192 |
|
193 |
static void print_quoted_string_atom(JSAtom * atom, Stream * f) { |
194 |
assert(ATOM_IS_STRING(atom)); |
195 |
JSString * s = ATOM_TO_STRING(atom); |
196 |
Stream_write_char(f, '"'); |
197 |
print_string(s, f); |
198 |
Stream_write_char(f, '"'); |
199 |
} |
200 |
|
201 |
static const char * get_op(uint8 op) { |
202 |
switch(op) { |
203 |
case JSOP_OR: |
204 |
return "||"; |
205 |
case JSOP_AND: |
206 |
return "&&"; |
207 |
case JSOP_BITOR: |
208 |
return "|"; |
209 |
case JSOP_BITXOR: |
210 |
return "^"; |
211 |
case JSOP_BITAND: |
212 |
return "&"; |
213 |
case JSOP_EQ: |
214 |
return "=="; |
215 |
case JSOP_NE: |
216 |
return "!="; |
217 |
case JSOP_STRICTEQ: |
218 |
return "==="; |
219 |
case JSOP_STRICTNE: |
220 |
return "!=="; |
221 |
case JSOP_LT: |
222 |
return "<"; |
223 |
case JSOP_LE: |
224 |
return "<="; |
225 |
case JSOP_GT: |
226 |
return ">"; |
227 |
case JSOP_GE: |
228 |
return ">="; |
229 |
case JSOP_LSH: |
230 |
return "<<"; |
231 |
case JSOP_RSH: |
232 |
return ">>"; |
233 |
case JSOP_URSH: |
234 |
return ">>>"; |
235 |
case JSOP_ADD: |
236 |
return "+"; |
237 |
case JSOP_SUB: |
238 |
return "-"; |
239 |
case JSOP_MUL: |
240 |
return "*"; |
241 |
case JSOP_DIV: |
242 |
return "/"; |
243 |
case JSOP_MOD: |
244 |
return "%"; |
245 |
default: |
246 |
abort(); |
247 |
} |
248 |
} |
249 |
|
250 |
static void instrument_expression(JSParseNode * node, Stream * f); |
251 |
static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if); |
252 |
|
253 |
enum FunctionType { |
254 |
FUNCTION_NORMAL, |
255 |
FUNCTION_GETTER_OR_SETTER |
256 |
}; |
257 |
|
258 |
static void instrument_function(JSParseNode * node, Stream * f, int indent, enum FunctionType type) { |
259 |
assert(node->pn_type == TOK_FUNCTION); |
260 |
assert(node->pn_arity == PN_FUNC); |
261 |
JSObject * object = node->pn_funpob->object; |
262 |
assert(JS_ObjectIsFunction(context, object)); |
263 |
JSFunction * function = (JSFunction *) JS_GetPrivate(context, object); |
264 |
assert(function); |
265 |
assert(object == &function->object); |
266 |
Stream_printf(f, "%*s", indent, ""); |
267 |
if (type == FUNCTION_NORMAL) { |
268 |
Stream_write_string(f, "function"); |
269 |
} |
270 |
|
271 |
/* function name */ |
272 |
if (function->atom) { |
273 |
Stream_write_char(f, ' '); |
274 |
print_string_atom(function->atom, f); |
275 |
} |
276 |
|
277 |
/* |
278 |
function parameters - see JS_DecompileFunction in jsapi.cpp, which calls |
279 |
js_DecompileFunction in jsopcode.cpp |
280 |
*/ |
281 |
Stream_write_string(f, "("); |
282 |
JSArenaPool pool; |
283 |
JS_INIT_ARENA_POOL(&pool, "instrument_function", 256, 1, &context->scriptStackQuota); |
284 |
jsuword * local_names = NULL; |
285 |
if (JS_GET_LOCAL_NAME_COUNT(function)) { |
286 |
local_names = js_GetLocalNameArray(context, function, &pool); |
287 |
if (local_names == NULL) { |
288 |
fatal("out of memory"); |
289 |
} |
290 |
} |
291 |
for (int i = 0; i < function->nargs; i++) { |
292 |
if (i > 0) { |
293 |
Stream_write_string(f, ", "); |
294 |
} |
295 |
JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]); |
296 |
print_string_atom(param, f); |
297 |
} |
298 |
JS_FinishArenaPool(&pool); |
299 |
Stream_write_string(f, ") {\n"); |
300 |
|
301 |
/* function body */ |
302 |
instrument_statement(node->pn_body, f, indent + 2, false); |
303 |
|
304 |
Stream_write_string(f, "}\n"); |
305 |
} |
306 |
|
307 |
static void instrument_function_call(JSParseNode * node, Stream * f) { |
308 |
instrument_expression(node->pn_head, f); |
309 |
Stream_write_char(f, '('); |
310 |
for (struct JSParseNode * p = node->pn_head->pn_next; p != NULL; p = p->pn_next) { |
311 |
if (p != node->pn_head->pn_next) { |
312 |
Stream_write_string(f, ", "); |
313 |
} |
314 |
instrument_expression(p, f); |
315 |
} |
316 |
Stream_write_char(f, ')'); |
317 |
} |
318 |
|
319 |
/* |
320 |
See <Expressions> in jsparse.h. |
321 |
TOK_FUNCTION is handled as a statement and as an expression. |
322 |
TOK_DBLDOT is not handled (XML op). |
323 |
TOK_DEFSHARP and TOK_USESHARP are not handled. |
324 |
TOK_ANYNAME is not handled (XML op). |
325 |
TOK_AT is not handled (XML op). |
326 |
TOK_DBLCOLON is not handled. |
327 |
TOK_XML* are not handled. |
328 |
There seem to be some undocumented expressions: |
329 |
TOK_INSTANCEOF binary |
330 |
TOK_IN binary |
331 |
*/ |
332 |
static void instrument_expression(JSParseNode * node, Stream * f) { |
333 |
switch (node->pn_type) { |
334 |
case TOK_FUNCTION: |
335 |
instrument_function(node, f, 0, FUNCTION_NORMAL); |
336 |
break; |
337 |
case TOK_COMMA: |
338 |
for (struct JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) { |
339 |
if (p != node->pn_head) { |
340 |
Stream_write_string(f, ", "); |
341 |
} |
342 |
instrument_expression(p, f); |
343 |
} |
344 |
break; |
345 |
case TOK_ASSIGN: |
346 |
instrument_expression(node->pn_left, f); |
347 |
Stream_write_char(f, ' '); |
348 |
switch (node->pn_op) { |
349 |
case JSOP_ADD: |
350 |
case JSOP_SUB: |
351 |
case JSOP_MUL: |
352 |
case JSOP_MOD: |
353 |
case JSOP_LSH: |
354 |
case JSOP_RSH: |
355 |
case JSOP_URSH: |
356 |
case JSOP_BITAND: |
357 |
case JSOP_BITOR: |
358 |
case JSOP_BITXOR: |
359 |
case JSOP_DIV: |
360 |
Stream_printf(f, "%s", get_op(node->pn_op)); |
361 |
break; |
362 |
default: |
363 |
/* do nothing - it must be a simple assignment */ |
364 |
break; |
365 |
} |
366 |
Stream_write_string(f, "= "); |
367 |
instrument_expression(node->pn_right, f); |
368 |
break; |
369 |
case TOK_HOOK: |
370 |
instrument_expression(node->pn_kid1, f); |
371 |
Stream_write_string(f, "? "); |
372 |
instrument_expression(node->pn_kid2, f); |
373 |
Stream_write_string(f, ": "); |
374 |
instrument_expression(node->pn_kid3, f); |
375 |
break; |
376 |
case TOK_OR: |
377 |
case TOK_AND: |
378 |
case TOK_BITOR: |
379 |
case TOK_BITXOR: |
380 |
case TOK_BITAND: |
381 |
case TOK_EQOP: |
382 |
case TOK_RELOP: |
383 |
case TOK_SHOP: |
384 |
case TOK_PLUS: |
385 |
case TOK_MINUS: |
386 |
case TOK_STAR: |
387 |
case TOK_DIVOP: |
388 |
switch (node->pn_arity) { |
389 |
case PN_BINARY: |
390 |
instrument_expression(node->pn_left, f); |
391 |
Stream_printf(f, " %s ", get_op(node->pn_op)); |
392 |
instrument_expression(node->pn_right, f); |
393 |
break; |
394 |
case PN_LIST: |
395 |
for (struct JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) { |
396 |
if (p != node->pn_head) { |
397 |
Stream_printf(f, " %s ", get_op(node->pn_op)); |
398 |
} |
399 |
instrument_expression(p, f); |
400 |
} |
401 |
break; |
402 |
default: |
403 |
abort(); |
404 |
} |
405 |
break; |
406 |
case TOK_UNARYOP: |
407 |
switch (node->pn_op) { |
408 |
case JSOP_NEG: |
409 |
Stream_write_char(f, '-'); |
410 |
instrument_expression(node->pn_kid, f); |
411 |
break; |
412 |
case JSOP_POS: |
413 |
Stream_write_char(f, '+'); |
414 |
instrument_expression(node->pn_kid, f); |
415 |
break; |
416 |
case JSOP_NOT: |
417 |
Stream_write_char(f, '!'); |
418 |
instrument_expression(node->pn_kid, f); |
419 |
break; |
420 |
case JSOP_BITNOT: |
421 |
Stream_write_char(f, '~'); |
422 |
instrument_expression(node->pn_kid, f); |
423 |
break; |
424 |
case JSOP_TYPEOF: |
425 |
Stream_write_string(f, "typeof "); |
426 |
instrument_expression(node->pn_kid, f); |
427 |
break; |
428 |
case JSOP_VOID: |
429 |
Stream_write_string(f, "void "); |
430 |
instrument_expression(node->pn_kid, f); |
431 |
break; |
432 |
default: |
433 |
abort(); |
434 |
break; |
435 |
} |
436 |
break; |
437 |
case TOK_INC: |
438 |
case TOK_DEC: |
439 |
/* |
440 |
This is not documented, but node->pn_op tells whether it is pre- or post-increment. |
441 |
*/ |
442 |
switch (node->pn_op) { |
443 |
case JSOP_INCNAME: |
444 |
case JSOP_INCPROP: |
445 |
case JSOP_INCELEM: |
446 |
Stream_write_string(f, "++"); |
447 |
instrument_expression(node->pn_kid, f); |
448 |
break; |
449 |
case JSOP_DECNAME: |
450 |
case JSOP_DECPROP: |
451 |
case JSOP_DECELEM: |
452 |
Stream_write_string(f, "--"); |
453 |
instrument_expression(node->pn_kid, f); |
454 |
break; |
455 |
case JSOP_NAMEINC: |
456 |
case JSOP_PROPINC: |
457 |
case JSOP_ELEMINC: |
458 |
instrument_expression(node->pn_kid, f); |
459 |
Stream_write_string(f, "++"); |
460 |
break; |
461 |
case JSOP_NAMEDEC: |
462 |
case JSOP_PROPDEC: |
463 |
case JSOP_ELEMDEC: |
464 |
instrument_expression(node->pn_kid, f); |
465 |
Stream_write_string(f, "--"); |
466 |
break; |
467 |
default: |
468 |
abort(); |
469 |
break; |
470 |
} |
471 |
break; |
472 |
case TOK_NEW: |
473 |
Stream_write_string(f, "new "); |
474 |
instrument_function_call(node, f); |
475 |
break; |
476 |
case TOK_DELETE: |
477 |
Stream_write_string(f, "delete "); |
478 |
instrument_expression(node->pn_kid, f); |
479 |
break; |
480 |
case TOK_DOT: |
481 |
/* |
482 |
This may have originally been x['foo-bar']. Because the string 'foo-bar' |
483 |
contains illegal characters, we have to use the subscript syntax instead of |
484 |
the dot syntax. |
485 |
*/ |
486 |
instrument_expression(node->pn_expr, f); |
487 |
assert(ATOM_IS_STRING(node->pn_atom)); |
488 |
{ |
489 |
JSString * s = ATOM_TO_STRING(node->pn_atom); |
490 |
bool is_keyword = (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF); |
491 |
if (! is_keyword && js_IsIdentifier(s)) { |
492 |
Stream_write_char(f, '.'); |
493 |
print_string_atom(node->pn_atom, f); |
494 |
} |
495 |
else { |
496 |
Stream_write_char(f, '['); |
497 |
print_quoted_string_atom(node->pn_atom, f); |
498 |
Stream_write_char(f, ']'); |
499 |
} |
500 |
} |
501 |
break; |
502 |
case TOK_LB: |
503 |
instrument_expression(node->pn_left, f); |
504 |
Stream_write_char(f, '['); |
505 |
instrument_expression(node->pn_right, f); |
506 |
Stream_write_char(f, ']'); |
507 |
break; |
508 |
case TOK_LP: |
509 |
instrument_function_call(node, f); |
510 |
break; |
511 |
case TOK_RB: |
512 |
Stream_write_char(f, '['); |
513 |
for (struct JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) { |
514 |
if (p != node->pn_head) { |
515 |
Stream_write_string(f, ", "); |
516 |
} |
517 |
/* TOK_COMMA is a special case: a hole in the array */ |
518 |
if (p->pn_type != TOK_COMMA) { |
519 |
instrument_expression(p, f); |
520 |
} |
521 |
} |
522 |
if (node->pn_extra == PNX_ENDCOMMA) { |
523 |
Stream_write_char(f, ','); |
524 |
} |
525 |
Stream_write_char(f, ']'); |
526 |
break; |
527 |
case TOK_RC: |
528 |
Stream_write_char(f, '{'); |
529 |
for (struct JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) { |
530 |
assert(p->pn_type == TOK_COLON); |
531 |
if (p != node->pn_head) { |
532 |
Stream_write_string(f, ", "); |
533 |
} |
534 |
|
535 |
/* check whether this is a getter or setter */ |
536 |
switch (p->pn_op) { |
537 |
case JSOP_GETTER: |
538 |
case JSOP_SETTER: |
539 |
if (p->pn_op == JSOP_GETTER) { |
540 |
Stream_write_string(f, "get "); |
541 |
} |
542 |
else { |
543 |
Stream_write_string(f, "set "); |
544 |
} |
545 |
instrument_expression(p->pn_left, f); |
546 |
if (p->pn_right->pn_type != TOK_FUNCTION) { |
547 |
fatal("parse error: expected function"); |
548 |
} |
549 |
instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER); |
550 |
break; |
551 |
default: |
552 |
instrument_expression(p->pn_left, f); |
553 |
Stream_write_string(f, ": "); |
554 |
instrument_expression(p->pn_right, f); |
555 |
break; |
556 |
} |
557 |
} |
558 |
Stream_write_char(f, '}'); |
559 |
break; |
560 |
case TOK_RP: |
561 |
Stream_write_char(f, '('); |
562 |
instrument_expression(node->pn_kid, f); |
563 |
Stream_write_char(f, ')'); |
564 |
break; |
565 |
case TOK_NAME: |
566 |
print_string_atom(node->pn_atom, f); |
567 |
break; |
568 |
case TOK_STRING: |
569 |
print_quoted_string_atom(node->pn_atom, f); |
570 |
break; |
571 |
case TOK_REGEXP: |
572 |
assert(node->pn_op == JSOP_REGEXP); |
573 |
{ |
574 |
JSObject * object = node->pn_pob->object; |
575 |
jsval result; |
576 |
js_regexp_toString(context, object, &result); |
577 |
print_regex(result, f); |
578 |
} |
579 |
break; |
580 |
case TOK_NUMBER: |
581 |
/* |
582 |
A 64-bit IEEE 754 floating point number has a 52-bit fraction. |
583 |
2^(-52) = 2.22 x 10^(-16) |
584 |
Thus there are 16 significant digits. |
585 |
To keep the output simple, special-case zero. |
586 |
*/ |
587 |
if (node->pn_dval == 0.0) { |
588 |
Stream_write_string(f, "0"); |
589 |
} |
590 |
else { |
591 |
Stream_printf(f, "%.15g", node->pn_dval); |
592 |
} |
593 |
break; |
594 |
case TOK_PRIMARY: |
595 |
switch (node->pn_op) { |
596 |
case JSOP_TRUE: |
597 |
Stream_write_string(f, "true"); |
598 |
break; |
599 |
case JSOP_FALSE: |
600 |
Stream_write_string(f, "false"); |
601 |
break; |
602 |
case JSOP_NULL: |
603 |
Stream_write_string(f, "null"); |
604 |
break; |
605 |
case JSOP_THIS: |
606 |
Stream_write_string(f, "this"); |
607 |
break; |
608 |
/* jsscan.h mentions `super' ??? */ |
609 |
default: |
610 |
abort(); |
611 |
} |
612 |
break; |
613 |
case TOK_INSTANCEOF: |
614 |
instrument_expression(node->pn_left, f); |
615 |
Stream_write_string(f, " instanceof "); |
616 |
instrument_expression(node->pn_right, f); |
617 |
break; |
618 |
case TOK_IN: |
619 |
instrument_expression(node->pn_left, f); |
620 |
Stream_write_string(f, " in "); |
621 |
instrument_expression(node->pn_right, f); |
622 |
break; |
623 |
default: |
624 |
fatal("unsupported node type in file %s: %d", file_id, node->pn_type); |
625 |
} |
626 |
} |
627 |
|
628 |
static void instrument_var_statement(JSParseNode * node, Stream * f, int indent) { |
629 |
assert(node->pn_arity == PN_LIST); |
630 |
Stream_printf(f, "%*s", indent, ""); |
631 |
Stream_write_string(f, "var "); |
632 |
for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) { |
633 |
assert(p->pn_type == TOK_NAME); |
634 |
assert(p->pn_arity == PN_NAME); |
635 |
if (p != node->pn_head) { |
636 |
Stream_write_string(f, ", "); |
637 |
} |
638 |
print_string_atom(p->pn_atom, f); |
639 |
if (p->pn_expr != NULL) { |
640 |
Stream_write_string(f, " = "); |
641 |
instrument_expression(p->pn_expr, f); |
642 |
} |
643 |
} |
644 |
} |
645 |
|
646 |
static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) { |
647 |
switch (node->pn_type) { |
648 |
case TOK_FUNCTION: |
649 |
instrument_function(node, f, indent, FUNCTION_NORMAL); |
650 |
break; |
651 |
case TOK_LC: |
652 |
assert(node->pn_arity == PN_LIST); |
653 |
/* |
654 |
Stream_write_string(f, "{\n"); |
655 |
*/ |
656 |
for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) { |
657 |
instrument_statement(p, f, indent, false); |
658 |
} |
659 |
/* |
660 |
Stream_printf(f, "%*s", indent, ""); |
661 |
Stream_write_string(f, "}\n"); |
662 |
*/ |
663 |
break; |
664 |
case TOK_IF: |
665 |
{ |
666 |
assert(node->pn_arity == PN_TERNARY); |
667 |
|
668 |
uint16_t line = node->pn_pos.begin.lineno; |
669 |
if (! is_jscoverage_if) { |
670 |
if (line > num_lines) { |
671 |
fatal("%s: script contains more than 65,535 lines", file_id); |
672 |
} |
673 |
if (line >= 2 && exclusive_directives[line - 2]) { |
674 |
is_jscoverage_if = true; |
675 |
} |
676 |
} |
677 |
|
678 |
Stream_printf(f, "%*s", indent, ""); |
679 |
Stream_write_string(f, "if ("); |
680 |
instrument_expression(node->pn_kid1, f); |
681 |
Stream_write_string(f, ") {\n"); |
682 |
if (is_jscoverage_if && node->pn_kid3) { |
683 |
uint16_t else_start = node->pn_kid3->pn_pos.begin.lineno; |
684 |
uint16_t else_end = node->pn_kid3->pn_pos.end.lineno + 1; |
685 |
Stream_printf(f, "%*s", indent + 2, ""); |
686 |
Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, else_start, else_end); |
687 |
} |
688 |
instrument_statement(node->pn_kid2, f, indent + 2, false); |
689 |
Stream_printf(f, "%*s", indent, ""); |
690 |
Stream_write_string(f, "}\n"); |
691 |
|
692 |
if (node->pn_kid3 || is_jscoverage_if) { |
693 |
Stream_printf(f, "%*s", indent, ""); |
694 |
Stream_write_string(f, "else {\n"); |
695 |
|
696 |
if (is_jscoverage_if) { |
697 |
uint16_t if_start = node->pn_kid2->pn_pos.begin.lineno + 1; |
698 |
uint16_t if_end = node->pn_kid2->pn_pos.end.lineno + 1; |
699 |
Stream_printf(f, "%*s", indent + 2, ""); |
700 |
Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_start, if_end); |
701 |
} |
702 |
|
703 |
if (node->pn_kid3) { |
704 |
instrument_statement(node->pn_kid3, f, indent + 2, is_jscoverage_if); |
705 |
} |
706 |
|
707 |
Stream_printf(f, "%*s", indent, ""); |
708 |
Stream_write_string(f, "}\n"); |
709 |
} |
710 |
|
711 |
break; |
712 |
} |
713 |
case TOK_SWITCH: |
714 |
assert(node->pn_arity == PN_BINARY); |
715 |
Stream_printf(f, "%*s", indent, ""); |
716 |
Stream_write_string(f, "switch ("); |
717 |
instrument_expression(node->pn_left, f); |
718 |
Stream_write_string(f, ") {\n"); |
719 |
for (struct JSParseNode * p = node->pn_right->pn_head; p != NULL; p = p->pn_next) { |
720 |
Stream_printf(f, "%*s", indent, ""); |
721 |
switch (p->pn_type) { |
722 |
case TOK_CASE: |
723 |
Stream_write_string(f, "case "); |
724 |
instrument_expression(p->pn_left, f); |
725 |
Stream_write_string(f, ":\n"); |
726 |
break; |
727 |
case TOK_DEFAULT: |
728 |
Stream_write_string(f, "default:\n"); |
729 |
break; |
730 |
default: |
731 |
abort(); |
732 |
break; |
733 |
} |
734 |
instrument_statement(p->pn_right, f, indent + 2, false); |
735 |
} |
736 |
Stream_printf(f, "%*s", indent, ""); |
737 |
Stream_write_string(f, "}\n"); |
738 |
break; |
739 |
case TOK_CASE: |
740 |
case TOK_DEFAULT: |
741 |
abort(); |
742 |
break; |
743 |
case TOK_WHILE: |
744 |
assert(node->pn_arity == PN_BINARY); |
745 |
Stream_printf(f, "%*s", indent, ""); |
746 |
Stream_write_string(f, "while ("); |
747 |
instrument_expression(node->pn_left, f); |
748 |
Stream_write_string(f, ") {\n"); |
749 |
instrument_statement(node->pn_right, f, indent + 2, false); |
750 |
Stream_write_string(f, "}\n"); |
751 |
break; |
752 |
case TOK_DO: |
753 |
assert(node->pn_arity == PN_BINARY); |
754 |
Stream_printf(f, "%*s", indent, ""); |
755 |
Stream_write_string(f, "do {\n"); |
756 |
instrument_statement(node->pn_left, f, indent + 2, false); |
757 |
Stream_write_string(f, "}\n"); |
758 |
Stream_printf(f, "%*s", indent, ""); |
759 |
Stream_write_string(f, "while ("); |
760 |
instrument_expression(node->pn_right, f); |
761 |
Stream_write_string(f, ");\n"); |
762 |
break; |
763 |
case TOK_FOR: |
764 |
assert(node->pn_arity == PN_BINARY); |
765 |
Stream_printf(f, "%*s", indent, ""); |
766 |
Stream_write_string(f, "for ("); |
767 |
switch (node->pn_left->pn_type) { |
768 |
case TOK_IN: |
769 |
/* for/in */ |
770 |
assert(node->pn_left->pn_arity == PN_BINARY); |
771 |
switch (node->pn_left->pn_left->pn_type) { |
772 |
case TOK_VAR: |
773 |
instrument_var_statement(node->pn_left->pn_left, f, 0); |
774 |
break; |
775 |
case TOK_NAME: |
776 |
instrument_expression(node->pn_left->pn_left, f); |
777 |
break; |
778 |
default: |
779 |
/* this is undocumented: for (x.value in y) */ |
780 |
instrument_expression(node->pn_left->pn_left, f); |
781 |
break; |
782 |
/* |
783 |
default: |
784 |
fprintf(stderr, "unexpected node type: %d\n", node->pn_left->pn_left->pn_type); |
785 |
abort(); |
786 |
break; |
787 |
*/ |
788 |
} |
789 |
Stream_write_string(f, " in "); |
790 |
instrument_expression(node->pn_left->pn_right, f); |
791 |
break; |
792 |
case TOK_RESERVED: |
793 |
/* for (;;) */ |
794 |
assert(node->pn_left->pn_arity == PN_TERNARY); |
795 |
if (node->pn_left->pn_kid1) { |
796 |
if (node->pn_left->pn_kid1->pn_type == TOK_VAR) { |
797 |
instrument_var_statement(node->pn_left->pn_kid1, f, 0); |
798 |
} |
799 |
else { |
800 |
instrument_expression(node->pn_left->pn_kid1, f); |
801 |
} |
802 |
} |
803 |
Stream_write_string(f, ";"); |
804 |
if (node->pn_left->pn_kid2) { |
805 |
Stream_write_char(f, ' '); |
806 |
instrument_expression(node->pn_left->pn_kid2, f); |
807 |
} |
808 |
Stream_write_string(f, ";"); |
809 |
if (node->pn_left->pn_kid3) { |
810 |
Stream_write_char(f, ' '); |
811 |
instrument_expression(node->pn_left->pn_kid3, f); |
812 |
} |
813 |
break; |
814 |
default: |
815 |
abort(); |
816 |
break; |
817 |
} |
818 |
Stream_write_string(f, ") {\n"); |
819 |
instrument_statement(node->pn_right, f, indent + 2, false); |
820 |
Stream_write_string(f, "}\n"); |
821 |
break; |
822 |
case TOK_THROW: |
823 |
assert(node->pn_arity == PN_UNARY); |
824 |
Stream_printf(f, "%*s", indent, ""); |
825 |
Stream_write_string(f, "throw "); |
826 |
instrument_expression(node->pn_u.unary.kid, f); |
827 |
Stream_write_string(f, ";\n"); |
828 |
break; |
829 |
case TOK_TRY: |
830 |
Stream_printf(f, "%*s", indent, ""); |
831 |
Stream_write_string(f, "try {\n"); |
832 |
instrument_statement(node->pn_kid1, f, indent + 2, false); |
833 |
Stream_printf(f, "%*s", indent, ""); |
834 |
Stream_write_string(f, "}\n"); |
835 |
if (node->pn_kid2) { |
836 |
assert(node->pn_kid2->pn_type == TOK_RESERVED); |
837 |
for (JSParseNode * scope = node->pn_kid2->pn_head; scope != NULL; scope = scope->pn_next) { |
838 |
assert(scope->pn_type == TOK_LEXICALSCOPE); |
839 |
JSParseNode * catch = scope->pn_expr; |
840 |
assert(catch->pn_type == TOK_CATCH); |
841 |
Stream_printf(f, "%*s", indent, ""); |
842 |
Stream_write_string(f, "catch ("); |
843 |
assert(catch->pn_kid1->pn_arity == PN_NAME); |
844 |
print_string_atom(catch->pn_kid1->pn_atom, f); |
845 |
if (catch->pn_kid2) { |
846 |
Stream_write_string(f, " if "); |
847 |
instrument_expression(catch->pn_kid2, f); |
848 |
} |
849 |
Stream_write_string(f, ") {\n"); |
850 |
instrument_statement(catch->pn_kid3, f, indent + 2, false); |
851 |
Stream_printf(f, "%*s", indent, ""); |
852 |
Stream_write_string(f, "}\n"); |
853 |
} |
854 |
} |
855 |
if (node->pn_kid3) { |
856 |
Stream_printf(f, "%*s", indent, ""); |
857 |
Stream_write_string(f, "finally {\n"); |
858 |
instrument_statement(node->pn_kid3, f, indent + 2, false); |
859 |
Stream_printf(f, "%*s", indent, ""); |
860 |
Stream_write_string(f, "}\n"); |
861 |
} |
862 |
break; |
863 |
case TOK_CATCH: |
864 |
abort(); |
865 |
break; |
866 |
case TOK_BREAK: |
867 |
case TOK_CONTINUE: |
868 |
assert(node->pn_arity == PN_NAME || node->pn_arity == PN_NULLARY); |
869 |
Stream_printf(f, "%*s", indent, ""); |
870 |
Stream_write_string(f, node->pn_type == TOK_BREAK? "break": "continue"); |
871 |
JSAtom * atom = node->pn_u.name.atom; |
872 |
if (atom != NULL) { |
873 |
Stream_write_char(f, ' '); |
874 |
print_string_atom(node->pn_atom, f); |
875 |
} |
876 |
Stream_write_string(f, ";\n"); |
877 |
break; |
878 |
case TOK_WITH: |
879 |
assert(node->pn_arity == PN_BINARY); |
880 |
Stream_printf(f, "%*s", indent, ""); |
881 |
Stream_write_string(f, "with ("); |
882 |
instrument_expression(node->pn_left, f); |
883 |
Stream_write_string(f, ") {\n"); |
884 |
instrument_statement(node->pn_right, f, indent + 2, false); |
885 |
Stream_printf(f, "%*s", indent, ""); |
886 |
Stream_write_string(f, "}\n"); |
887 |
break; |
888 |
case TOK_VAR: |
889 |
instrument_var_statement(node, f, indent); |
890 |
Stream_write_string(f, ";\n"); |
891 |
break; |
892 |
case TOK_RETURN: |
893 |
assert(node->pn_arity == PN_UNARY); |
894 |
Stream_printf(f, "%*s", indent, ""); |
895 |
Stream_write_string(f, "return"); |
896 |
if (node->pn_kid != NULL) { |
897 |
Stream_write_char(f, ' '); |
898 |
instrument_expression(node->pn_kid, f); |
899 |
} |
900 |
Stream_write_string(f, ";\n"); |
901 |
break; |
902 |
case TOK_SEMI: |
903 |
assert(node->pn_arity == PN_UNARY); |
904 |
Stream_printf(f, "%*s", indent, ""); |
905 |
if (node->pn_kid != NULL) { |
906 |
instrument_expression(node->pn_kid, f); |
907 |
} |
908 |
Stream_write_string(f, ";\n"); |
909 |
break; |
910 |
case TOK_COLON: |
911 |
assert(node->pn_arity == PN_NAME); |
912 |
/* |
913 |
This one is tricky: can't output instrumentation between the label and the |
914 |
statement it's supposed to label ... |
915 |
*/ |
916 |
Stream_printf(f, "%*s", indent < 2? 0: indent - 2, ""); |
917 |
print_string_atom(node->pn_atom, f); |
918 |
Stream_write_string(f, ":\n"); |
919 |
/* |
920 |
... use output_statement instead of instrument_statement. |
921 |
*/ |
922 |
output_statement(node->pn_expr, f, indent, false); |
923 |
break; |
924 |
default: |
925 |
fatal("unsupported node type in file %s: %d", file_id, node->pn_type); |
926 |
} |
927 |
} |
928 |
|
929 |
/* |
930 |
See <Statements> in jsparse.h. |
931 |
TOK_FUNCTION is handled as a statement and as an expression. |
932 |
TOK_EXPORT, TOK_IMPORT are not handled. |
933 |
*/ |
934 |
static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) { |
935 |
if (node->pn_type != TOK_LC) { |
936 |
uint16_t line = node->pn_pos.begin.lineno; |
937 |
if (line > num_lines) { |
938 |
fatal("%s: script contains more than 65,535 lines", file_id); |
939 |
} |
940 |
|
941 |
/* the root node has line number 0 */ |
942 |
if (line != 0) { |
943 |
Stream_printf(f, "%*s", indent, ""); |
944 |
Stream_printf(f, "_$jscoverage['%s'][%d]++;\n", file_id, line); |
945 |
lines[line - 1] = 1; |
946 |
} |
947 |
} |
948 |
output_statement(node, f, indent, is_jscoverage_if); |
949 |
} |
950 |
|
951 |
static bool characters_start_with(const jschar * characters, size_t line_start, size_t line_end, const char * prefix) { |
952 |
const jschar * characters_end = characters + line_end; |
953 |
const jschar * cp = characters + line_start; |
954 |
const char * bp = prefix; |
955 |
for (;;) { |
956 |
if (*bp == '\0') { |
957 |
return true; |
958 |
} |
959 |
else if (cp == characters_end) { |
960 |
return false; |
961 |
} |
962 |
else if (*cp != *bp) { |
963 |
return false; |
964 |
} |
965 |
bp++; |
966 |
cp++; |
967 |
} |
968 |
} |
969 |
|
970 |
static bool characters_are_white_space(const jschar * characters, size_t line_start, size_t line_end) { |
971 |
/* XXX - other Unicode space */ |
972 |
const jschar * end = characters + line_end; |
973 |
for (const jschar * p = characters + line_start; p < end; p++) { |
974 |
jschar c = *p; |
975 |
if (c == 0x9 || c == 0xB || c == 0xC || c == 0x20 || c == 0xA0) { |
976 |
continue; |
977 |
} |
978 |
else { |
979 |
return false; |
980 |
} |
981 |
} |
982 |
return true; |
983 |
} |
984 |
|
985 |
static void error_reporter(JSContext * context, const char * message, JSErrorReport * report) { |
986 |
fprintf(stderr, "jscoverage: parse error: line %u: %s\n", report->lineno, message); |
987 |
} |
988 |
|
989 |
void jscoverage_instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output) { |
990 |
file_id = id; |
991 |
|
992 |
/* parse the javascript */ |
993 |
JSParseContext parse_context; |
994 |
if (! js_InitParseContext(context, &parse_context, NULL, NULL, characters, num_characters, NULL, NULL, 1)) { |
995 |
fatal("cannot create token stream from file: %s", file_id); |
996 |
} |
997 |
JSErrorReporter old_error_reporter = JS_SetErrorReporter(context, error_reporter); |
998 |
JSParseNode * node = js_ParseScript(context, global, &parse_context); |
999 |
if (node == NULL) { |
1000 |
js_ReportUncaughtException(context); |
1001 |
fatal("parse error in file: %s", file_id); |
1002 |
} |
1003 |
JS_SetErrorReporter(context, old_error_reporter); |
1004 |
num_lines = node->pn_pos.end.lineno; |
1005 |
lines = xmalloc(num_lines); |
1006 |
for (unsigned int i = 0; i < num_lines; i++) { |
1007 |
lines[i] = 0; |
1008 |
} |
1009 |
|
1010 |
/* search code for conditionals */ |
1011 |
exclusive_directives = xnew(bool, num_lines); |
1012 |
for (unsigned int i = 0; i < num_lines; i++) { |
1013 |
exclusive_directives[i] = false; |
1014 |
} |
1015 |
|
1016 |
bool has_conditionals = false; |
1017 |
struct IfDirective * if_directives = NULL; |
1018 |
size_t line_number = 0; |
1019 |
size_t i = 0; |
1020 |
while (i < num_characters) { |
1021 |
if (line_number == UINT16_MAX) { |
1022 |
fatal("%s: script has more than 65,535 lines", file_id); |
1023 |
} |
1024 |
line_number++; |
1025 |
size_t line_start = i; |
1026 |
jschar c; |
1027 |
bool done = false; |
1028 |
while (! done && i < num_characters) { |
1029 |
c = characters[i]; |
1030 |
switch (c) { |
1031 |
case '\r': |
1032 |
case '\n': |
1033 |
case 0x2028: |
1034 |
case 0x2029: |
1035 |
done = true; |
1036 |
break; |
1037 |
default: |
1038 |
i++; |
1039 |
} |
1040 |
} |
1041 |
size_t line_end = i; |
1042 |
if (i < num_characters) { |
1043 |
i++; |
1044 |
if (c == '\r' && i < num_characters && characters[i] == '\n') { |
1045 |
i++; |
1046 |
} |
1047 |
} |
1048 |
|
1049 |
if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_IF")) { |
1050 |
has_conditionals = true; |
1051 |
|
1052 |
if (characters_are_white_space(characters, line_start + 16, line_end)) { |
1053 |
exclusive_directives[line_number - 1] = true; |
1054 |
} |
1055 |
else { |
1056 |
struct IfDirective * if_directive = xnew(struct IfDirective, 1); |
1057 |
if_directive->condition_start = characters + line_start + 16; |
1058 |
if_directive->condition_end = characters + line_end; |
1059 |
if_directive->start_line = line_number; |
1060 |
if_directive->end_line = 0; |
1061 |
if_directive->next = if_directives; |
1062 |
if_directives = if_directive; |
1063 |
} |
1064 |
} |
1065 |
else if (characters_start_with(characters, line_start, line_end, "//#JSCOVERAGE_ENDIF")) { |
1066 |
for (struct IfDirective * p = if_directives; p != NULL; p = p->next) { |
1067 |
if (p->end_line == 0) { |
1068 |
p->end_line = line_number; |
1069 |
break; |
1070 |
} |
1071 |
} |
1072 |
} |
1073 |
} |
1074 |
|
1075 |
/* |
1076 |
An instrumented JavaScript file has 4 sections: |
1077 |
1. initialization |
1078 |
2. instrumented source code |
1079 |
3. conditionals |
1080 |
4. original source code |
1081 |
*/ |
1082 |
|
1083 |
Stream * instrumented = Stream_new(0); |
1084 |
instrument_statement(node, instrumented, 0, false); |
1085 |
js_FinishParseContext(context, &parse_context); |
1086 |
|
1087 |
/* write line number info to the output */ |
1088 |
Stream_write_string(output, "/* automatically generated by JSCoverage - do not edit */\n"); |
1089 |
Stream_write_string(output, "if (! top._$jscoverage) {\n top._$jscoverage = {};\n}\n"); |
1090 |
Stream_write_string(output, "var _$jscoverage = top._$jscoverage;\n"); |
1091 |
Stream_printf(output, "if (! _$jscoverage['%s']) {\n", file_id); |
1092 |
Stream_printf(output, " _$jscoverage['%s'] = [];\n", file_id); |
1093 |
for (int i = 0; i < num_lines; i++) { |
1094 |
if (lines[i]) { |
1095 |
Stream_printf(output, " _$jscoverage['%s'][%d] = 0;\n", file_id, i + 1); |
1096 |
} |
1097 |
} |
1098 |
Stream_write_string(output, "}\n"); |
1099 |
free(lines); |
1100 |
lines = NULL; |
1101 |
free(exclusive_directives); |
1102 |
exclusive_directives = NULL; |
1103 |
|
1104 |
/* conditionals */ |
1105 |
if (has_conditionals) { |
1106 |
Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id); |
1107 |
} |
1108 |
|
1109 |
/* copy the instrumented source code to the output */ |
1110 |
Stream_write(output, instrumented->data, instrumented->length); |
1111 |
|
1112 |
/* conditionals */ |
1113 |
for (struct IfDirective * if_directive = if_directives; if_directive != NULL; if_directive = if_directive->next) { |
1114 |
Stream_write_string(output, "if (!("); |
1115 |
print_javascript(if_directive->condition_start, if_directive->condition_end - if_directive->condition_start, output); |
1116 |
Stream_write_string(output, ")) {\n"); |
1117 |
Stream_printf(output, " _$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_directive->start_line, if_directive->end_line); |
1118 |
Stream_write_string(output, "}\n"); |
1119 |
} |
1120 |
|
1121 |
/* free */ |
1122 |
while (if_directives != NULL) { |
1123 |
struct IfDirective * if_directive = if_directives; |
1124 |
if_directives = if_directives->next; |
1125 |
free(if_directive); |
1126 |
} |
1127 |
|
1128 |
/* copy the original source to the output */ |
1129 |
Stream_printf(output, "_$jscoverage['%s'].source = ", file_id); |
1130 |
jscoverage_write_source(id, characters, num_characters, output); |
1131 |
Stream_printf(output, ";\n"); |
1132 |
|
1133 |
Stream_delete(instrumented); |
1134 |
|
1135 |
file_id = NULL; |
1136 |
} |
1137 |
|
1138 |
void jscoverage_write_source(const char * id, const jschar * characters, size_t num_characters, Stream * output) { |
1139 |
Stream_write_string(output, "["); |
1140 |
if (jscoverage_highlight) { |
1141 |
Stream * highlighted_stream = Stream_new(num_characters); |
1142 |
jscoverage_highlight_js(context, id, characters, num_characters, highlighted_stream); |
1143 |
size_t i = 0; |
1144 |
while (i < highlighted_stream->length) { |
1145 |
if (i > 0) { |
1146 |
Stream_write_char(output, ','); |
1147 |
} |
1148 |
|
1149 |
Stream_write_char(output, '"'); |
1150 |
bool done = false; |
1151 |
while (! done) { |
1152 |
char c = highlighted_stream->data[i]; |
1153 |
switch (c) { |
1154 |
case 0x8: |
1155 |
/* backspace */ |
1156 |
Stream_write_string(output, "\\b"); |
1157 |
break; |
1158 |
case 0x9: |
1159 |
/* horizontal tab */ |
1160 |
Stream_write_string(output, "\\t"); |
1161 |
break; |
1162 |
case 0xa: |
1163 |
/* line feed (new line) */ |
1164 |
done = true; |
1165 |
break; |
1166 |
/* IE doesn't support this */ |
1167 |
/* |
1168 |
case 0xb: |
1169 |
Stream_write_string(output, "\\v"); |
1170 |
break; |
1171 |
*/ |
1172 |
case 0xc: |
1173 |
/* form feed */ |
1174 |
Stream_write_string(output, "\\f"); |
1175 |
break; |
1176 |
case 0xd: |
1177 |
/* carriage return */ |
1178 |
done = true; |
1179 |
if (i + 1 < highlighted_stream->length && highlighted_stream->data[i + 1] == '\n') { |
1180 |
i++; |
1181 |
} |
1182 |
break; |
1183 |
case '"': |
1184 |
Stream_write_string(output, "\\\""); |
1185 |
break; |
1186 |
case '\\': |
1187 |
Stream_write_string(output, "\\\\"); |
1188 |
break; |
1189 |
default: |
1190 |
Stream_write_char(output, c); |
1191 |
break; |
1192 |
} |
1193 |
i++; |
1194 |
if (i >= highlighted_stream->length) { |
1195 |
done = true; |
1196 |
} |
1197 |
} |
1198 |
Stream_write_char(output, '"'); |
1199 |
} |
1200 |
Stream_delete(highlighted_stream); |
1201 |
} |
1202 |
else { |
1203 |
size_t i = 0; |
1204 |
while (i < num_characters) { |
1205 |
if (i > 0) { |
1206 |
Stream_write_char(output, ','); |
1207 |
} |
1208 |
|
1209 |
Stream_write_char(output, '"'); |
1210 |
bool done = false; |
1211 |
while (! done) { |
1212 |
jschar c = characters[i]; |
1213 |
switch (c) { |
1214 |
case 0x8: |
1215 |
/* backspace */ |
1216 |
Stream_write_string(output, "\\b"); |
1217 |
break; |
1218 |
case 0x9: |
1219 |
/* horizontal tab */ |
1220 |
Stream_write_string(output, "\\t"); |
1221 |
break; |
1222 |
case 0xa: |
1223 |
/* line feed (new line) */ |
1224 |
done = true; |
1225 |
break; |
1226 |
/* IE doesn't support this */ |
1227 |
/* |
1228 |
case 0xb: |
1229 |
Stream_write_string(output, "\\v"); |
1230 |
break; |
1231 |
*/ |
1232 |
case 0xc: |
1233 |
/* form feed */ |
1234 |
Stream_write_string(output, "\\f"); |
1235 |
break; |
1236 |
case 0xd: |
1237 |
/* carriage return */ |
1238 |
done = true; |
1239 |
if (i + 1 < num_characters && characters[i + 1] == '\n') { |
1240 |
i++; |
1241 |
} |
1242 |
break; |
1243 |
case '"': |
1244 |
Stream_write_string(output, "\\\""); |
1245 |
break; |
1246 |
case '\\': |
1247 |
Stream_write_string(output, "\\\\"); |
1248 |
break; |
1249 |
case '&': |
1250 |
Stream_write_string(output, "&"); |
1251 |
break; |
1252 |
case '<': |
1253 |
Stream_write_string(output, "<"); |
1254 |
break; |
1255 |
case '>': |
1256 |
Stream_write_string(output, ">"); |
1257 |
break; |
1258 |
case 0x2028: |
1259 |
case 0x2029: |
1260 |
done = true; |
1261 |
break; |
1262 |
default: |
1263 |
if (32 <= c && c <= 126) { |
1264 |
Stream_write_char(output, c); |
1265 |
} |
1266 |
else { |
1267 |
Stream_printf(output, "&#%d;", c); |
1268 |
} |
1269 |
break; |
1270 |
} |
1271 |
i++; |
1272 |
if (i >= num_characters) { |
1273 |
done = true; |
1274 |
} |
1275 |
} |
1276 |
Stream_write_char(output, '"'); |
1277 |
} |
1278 |
} |
1279 |
Stream_write_string(output, "]"); |
1280 |
} |
1281 |
|
1282 |
void jscoverage_copy_resources(const char * destination_directory) { |
1283 |
copy_resource("jscoverage.html", destination_directory); |
1284 |
copy_resource("jscoverage.css", destination_directory); |
1285 |
copy_resource("jscoverage.js", destination_directory); |
1286 |
copy_resource("jscoverage-ie.css", destination_directory); |
1287 |
copy_resource("jscoverage-throbber.gif", destination_directory); |
1288 |
copy_resource("jscoverage-highlight.css", destination_directory); |
1289 |
} |
1290 |
|
1291 |
/* |
1292 |
coverage reports |
1293 |
*/ |
1294 |
|
1295 |
struct FileCoverageList { |
1296 |
FileCoverage * file_coverage; |
1297 |
struct FileCoverageList * next; |
1298 |
}; |
1299 |
|
1300 |
struct Coverage { |
1301 |
JSHashTable * coverage_table; |
1302 |
struct FileCoverageList * coverage_list; |
1303 |
}; |
1304 |
|
1305 |
static int compare_strings(const void * p1, const void * p2) { |
1306 |
return strcmp(p1, p2) == 0; |
1307 |
} |
1308 |
|
1309 |
Coverage * Coverage_new(void) { |
1310 |
Coverage * result = xmalloc(sizeof(Coverage)); |
1311 |
result->coverage_table = JS_NewHashTable(1024, JS_HashString, compare_strings, NULL, NULL, NULL); |
1312 |
if (result->coverage_table == NULL) { |
1313 |
fatal("cannot create hash table"); |
1314 |
} |
1315 |
result->coverage_list = NULL; |
1316 |
return result; |
1317 |
} |
1318 |
|
1319 |
void Coverage_delete(Coverage * coverage) { |
1320 |
JS_HashTableDestroy(coverage->coverage_table); |
1321 |
struct FileCoverageList * p = coverage->coverage_list; |
1322 |
while (p != NULL) { |
1323 |
free(p->file_coverage->coverage_lines); |
1324 |
if (p->file_coverage->source_lines != NULL) { |
1325 |
for (uint32 i = 0; i < p->file_coverage->num_source_lines; i++) { |
1326 |
free(p->file_coverage->source_lines[i]); |
1327 |
} |
1328 |
free(p->file_coverage->source_lines); |
1329 |
} |
1330 |
free(p->file_coverage->id); |
1331 |
free(p->file_coverage); |
1332 |
struct FileCoverageList * q = p; |
1333 |
p = p->next; |
1334 |
free(q); |
1335 |
} |
1336 |
free(coverage); |
1337 |
} |
1338 |
|
1339 |
struct EnumeratorArg { |
1340 |
CoverageForeachFunction f; |
1341 |
void * p; |
1342 |
}; |
1343 |
|
1344 |
static intN enumerator(JSHashEntry * entry, intN i, void * arg) { |
1345 |
struct EnumeratorArg * enumerator_arg = arg; |
1346 |
enumerator_arg->f(entry->value, i, enumerator_arg->p); |
1347 |
return 0; |
1348 |
} |
1349 |
|
1350 |
void Coverage_foreach_file(Coverage * coverage, CoverageForeachFunction f, void * p) { |
1351 |
struct EnumeratorArg enumerator_arg; |
1352 |
enumerator_arg.f = f; |
1353 |
enumerator_arg.p = p; |
1354 |
JS_HashTableEnumerateEntries(coverage->coverage_table, enumerator, &enumerator_arg); |
1355 |
} |
1356 |
|
1357 |
int jscoverage_parse_json(Coverage * coverage, const uint8_t * json, size_t length) { |
1358 |
int result = 0; |
1359 |
|
1360 |
jschar * base = js_InflateString(context, (char *) json, &length); |
1361 |
if (base == NULL) { |
1362 |
fatal("out of memory"); |
1363 |
} |
1364 |
|
1365 |
jschar * parenthesized_json = xnew(jschar, addst(length, 2)); |
1366 |
parenthesized_json[0] = '('; |
1367 |
memcpy(parenthesized_json + 1, base, mulst(length, sizeof(jschar))); |
1368 |
parenthesized_json[length + 1] = ')'; |
1369 |
|
1370 |
JS_free(context, base); |
1371 |
|
1372 |
JSParseContext parse_context; |
1373 |
if (! js_InitParseContext(context, &parse_context, NULL, NULL, parenthesized_json, length + 2, NULL, NULL, 1)) { |
1374 |
free(parenthesized_json); |
1375 |
return -1; |
1376 |
} |
1377 |
JSParseNode * root = js_ParseScript(context, global, &parse_context); |
1378 |
free(parenthesized_json); |
1379 |
if (root == NULL) { |
1380 |
result = -1; |
1381 |
goto done; |
1382 |
} |
1383 |
|
1384 |
/* root node must be TOK_LC */ |
1385 |
if (root->pn_type != TOK_LC) { |
1386 |
result = -1; |
1387 |
goto done; |
1388 |
} |
1389 |
JSParseNode * semi = root->pn_u.list.head; |
1390 |
|
1391 |
/* the list must be TOK_SEMI and it must contain only one element */ |
1392 |
if (semi->pn_type != TOK_SEMI || semi->pn_next != NULL) { |
1393 |
result = -1; |
1394 |
goto done; |
1395 |
} |
1396 |
JSParseNode * parenthesized = semi->pn_kid; |
1397 |
|
1398 |
/* this must be a parenthesized expression */ |
1399 |
if (parenthesized->pn_type != TOK_RP) { |
1400 |
result = -1; |
1401 |
goto done; |
1402 |
} |
1403 |
JSParseNode * object = parenthesized->pn_kid; |
1404 |
|
1405 |
/* this must be an object literal */ |
1406 |
if (object->pn_type != TOK_RC) { |
1407 |
result = -1; |
1408 |
goto done; |
1409 |
} |
1410 |
|
1411 |
for (JSParseNode * p = object->pn_head; p != NULL; p = p->pn_next) { |
1412 |
/* every element of this list must be TOK_COLON */ |
1413 |
if (p->pn_type != TOK_COLON) { |
1414 |
result = -1; |
1415 |
goto done; |
1416 |
} |
1417 |
|
1418 |
/* the key must be a string representing the file */ |
1419 |
JSParseNode * key = p->pn_left; |
1420 |
if (key->pn_type != TOK_STRING || ! ATOM_IS_STRING(key->pn_atom)) { |
1421 |
result = -1; |
1422 |
goto done; |
1423 |
} |
1424 |
char * id_bytes = JS_GetStringBytes(ATOM_TO_STRING(key->pn_atom)); |
1425 |
|
1426 |
/* the value must be an object literal OR an array */ |
1427 |
JSParseNode * value = p->pn_right; |
1428 |
if (! (value->pn_type == TOK_RC || value->pn_type == TOK_RB)) { |
1429 |
result = -1; |
1430 |
goto done; |
1431 |
} |
1432 |
|
1433 |
JSParseNode * array = NULL; |
1434 |
JSParseNode * source = NULL; |
1435 |
if (value->pn_type == TOK_RB) { |
1436 |
/* an array */ |
1437 |
array = value; |
1438 |
} |
1439 |
else if (value->pn_type == TOK_RC) { |
1440 |
/* an object literal */ |
1441 |
if (value->pn_count != 2) { |
1442 |
result = -1; |
1443 |
goto done; |
1444 |
} |
1445 |
for (JSParseNode * element = value->pn_head; element != NULL; element = element->pn_next) { |
1446 |
if (element->pn_type != TOK_COLON) { |
1447 |
result = -1; |
1448 |
goto done; |
1449 |
} |
1450 |
JSParseNode * left = element->pn_left; |
1451 |
if (left->pn_type != TOK_STRING || ! ATOM_IS_STRING(left->pn_atom)) { |
1452 |
result = -1; |
1453 |
goto done; |
1454 |
} |
1455 |
const char * s = JS_GetStringBytes(ATOM_TO_STRING(left->pn_atom)); |
1456 |
if (strcmp(s, "coverage") == 0) { |
1457 |
array = element->pn_right; |
1458 |
if (array->pn_type != TOK_RB) { |
1459 |
result = -1; |
1460 |
goto done; |
1461 |
} |
1462 |
} |
1463 |
else if (strcmp(s, "source") == 0) { |
1464 |
source = element->pn_right; |
1465 |
if (source->pn_type != TOK_RB) { |
1466 |
result = -1; |
1467 |
goto done; |
1468 |
} |
1469 |
} |
1470 |
else { |
1471 |
result = -1; |
1472 |
goto done; |
1473 |
} |
1474 |
} |
1475 |
} |
1476 |
else { |
1477 |
result = -1; |
1478 |
goto done; |
1479 |
} |
1480 |
|
1481 |
if (array == NULL) { |
1482 |
result = -1; |
1483 |
goto done; |
1484 |
} |
1485 |
|
1486 |
/* look up the file in the coverage table */ |
1487 |
FileCoverage * file_coverage = JS_HashTableLookup(coverage->coverage_table, id_bytes); |
1488 |
if (file_coverage == NULL) { |
1489 |
/* not there: create a new one */ |
1490 |
char * id = xstrdup(id_bytes); |
1491 |
file_coverage = xmalloc(sizeof(FileCoverage)); |
1492 |
file_coverage->id = id; |
1493 |
file_coverage->num_coverage_lines = array->pn_count; |
1494 |
file_coverage->coverage_lines = xnew(int, array->pn_count); |
1495 |
file_coverage->source_lines = NULL; |
1496 |
|
1497 |
/* set coverage for all lines */ |
1498 |
uint32 i = 0; |
1499 |
for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) { |
1500 |
if (element->pn_type == TOK_NUMBER) { |
1501 |
file_coverage->coverage_lines[i] = (int) element->pn_dval; |
1502 |
} |
1503 |
else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) { |
1504 |
file_coverage->coverage_lines[i] = -1; |
1505 |
} |
1506 |
else { |
1507 |
result = -1; |
1508 |
goto done; |
1509 |
} |
1510 |
} |
1511 |
assert(i == array->pn_count); |
1512 |
|
1513 |
/* add to the hash table */ |
1514 |
JS_HashTableAdd(coverage->coverage_table, id, file_coverage); |
1515 |
struct FileCoverageList * coverage_list = xmalloc(sizeof(struct FileCoverageList)); |
1516 |
coverage_list->file_coverage = file_coverage; |
1517 |
coverage_list->next = coverage->coverage_list; |
1518 |
coverage->coverage_list = coverage_list; |
1519 |
} |
1520 |
else { |
1521 |
/* sanity check */ |
1522 |
assert(strcmp(file_coverage->id, id_bytes) == 0); |
1523 |
if (file_coverage->num_coverage_lines != array->pn_count) { |
1524 |
result = -2; |
1525 |
goto done; |
1526 |
} |
1527 |
|
1528 |
/* merge the coverage */ |
1529 |
uint32 i = 0; |
1530 |
for (JSParseNode * element = array->pn_head; element != NULL; element = element->pn_next, i++) { |
1531 |
if (element->pn_type == TOK_NUMBER) { |
1532 |
if (file_coverage->coverage_lines[i] == -1) { |
1533 |
result = -2; |
1534 |
goto done; |
1535 |
} |
1536 |
file_coverage->coverage_lines[i] += (int) element->pn_dval; |
1537 |
} |
1538 |
else if (element->pn_type == TOK_PRIMARY && element->pn_op == JSOP_NULL) { |
1539 |
if (file_coverage->coverage_lines[i] != -1) { |
1540 |
result = -2; |
1541 |
goto done; |
1542 |
} |
1543 |
} |
1544 |
else { |
1545 |
result = -1; |
1546 |
goto done; |
1547 |
} |
1548 |
} |
1549 |
assert(i == array->pn_count); |
1550 |
} |
1551 |
|
1552 |
/* if this JSON file has source, use it */ |
1553 |
if (file_coverage->source_lines == NULL && source != NULL) { |
1554 |
file_coverage->num_source_lines = source->pn_count; |
1555 |
file_coverage->source_lines = xnew(char *, source->pn_count); |
1556 |
uint32 i = 0; |
1557 |
for (JSParseNode * element = source->pn_head; element != NULL; element = element->pn_next, i++) { |
1558 |
if (element->pn_type != TOK_STRING) { |
1559 |
result = -1; |
1560 |
goto done; |
1561 |
} |
1562 |
file_coverage->source_lines[i] = xstrdup(JS_GetStringBytes(ATOM_TO_STRING(element->pn_atom))); |
1563 |
} |
1564 |
assert(i == source->pn_count); |
1565 |
} |
1566 |
} |
1567 |
|
1568 |
done: |
1569 |
js_FinishParseContext(context, &parse_context); |
1570 |
return result; |
1571 |
} |