forked from visionmedia/node-jscoverage
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstrument-js.cpp
1873 lines (1768 loc) · 53.3 KB
/
instrument-js.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
instrument-js.cpp - JavaScript instrumentation routines
Copyright (C) 2007, 2008 siliconforks.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <config.h>
#include "instrument-js.h"
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <jsapi.h>
#include <jsarena.h>
#include <jsatom.h>
#include <jsemit.h>
#include <jsexn.h>
#include <jsfun.h>
#include <jsinterp.h>
#include <jsiter.h>
#include <jsparse.h>
#include <jsregexp.h>
#include <jsscope.h>
#include <jsstr.h>
#include "encoding.h"
#include "global.h"
#include "highlight.h"
#include "resource-manager.h"
#include "util.h"
struct IfDirective {
const jschar * condition_start;
const jschar * condition_end;
uint16_t start_line;
uint16_t end_line;
struct IfDirective * next;
};
bool jscoverage_mozilla = false;
static bool * exclusive_directives = NULL;
static JSRuntime * runtime = NULL;
static JSContext * context = NULL;
static JSObject * global = NULL;
static JSVersion js_version = JSVERSION_ECMA_3;
/*
JSParseNode objects store line numbers starting from 1.
The lines array stores line numbers starting from 0.
*/
static const char * file_id = NULL;
static char * lines = NULL;
static uint16_t num_lines = 0;
void jscoverage_set_js_version(const char * version) {
js_version = JS_StringToVersion(version);
if (js_version != JSVERSION_UNKNOWN) {
return;
}
char * end;
js_version = (JSVersion) strtol(version, &end, 10);
if ((size_t) (end - version) != strlen(version)) {
fatal("invalid version: %s", version);
}
}
void jscoverage_init(void) {
runtime = JS_NewRuntime(8L * 1024L * 1024L);
if (runtime == NULL) {
fatal("cannot create runtime");
}
context = JS_NewContext(runtime, 8192);
if (context == NULL) {
fatal("cannot create context");
}
JS_SetVersion(context, js_version);
global = JS_NewObject(context, NULL, NULL, NULL);
if (global == NULL) {
fatal("cannot create global object");
}
if (! JS_InitStandardClasses(context, global)) {
fatal("cannot initialize standard classes");
}
}
void jscoverage_cleanup(void) {
JS_DestroyContext(context);
JS_DestroyRuntime(runtime);
}
static void print_javascript(const jschar * characters, size_t num_characters, Stream * f) {
for (size_t i = 0; i < num_characters; i++) {
jschar c = characters[i];
/*
XXX does not handle no-break space, other unicode "space separator"
*/
switch (c) {
case 0x9:
case 0xB:
case 0xC:
Stream_write_char(f, c);
break;
default:
if (32 <= c && c <= 126) {
Stream_write_char(f, c);
}
else {
Stream_printf(f, "\\u%04x", c);
}
break;
}
}
}
static void print_string(JSString * s, Stream * f) {
size_t length = JSSTRING_LENGTH(s);
jschar * characters = JSSTRING_CHARS(s);
for (size_t i = 0; i < length; i++) {
jschar c = characters[i];
if (32 <= c && c <= 126) {
switch (c) {
case '"':
Stream_write_string(f, "\\\"");
break;
/*
case '\'':
Stream_write_string(f, "\\'");
break;
*/
case '\\':
Stream_write_string(f, "\\\\");
break;
default:
Stream_write_char(f, c);
break;
}
}
else {
switch (c) {
case 0x8:
Stream_write_string(f, "\\b");
break;
case 0x9:
Stream_write_string(f, "\\t");
break;
case 0xa:
Stream_write_string(f, "\\n");
break;
/* IE doesn't support this */
/*
case 0xb:
Stream_write_string(f, "\\v");
break;
*/
case 0xc:
Stream_write_string(f, "\\f");
break;
case 0xd:
Stream_write_string(f, "\\r");
break;
default:
Stream_printf(f, "\\u%04x", c);
break;
}
}
}
}
static void print_string_atom(JSAtom * atom, Stream * f) {
assert(ATOM_IS_STRING(atom));
JSString * s = ATOM_TO_STRING(atom);
print_string(s, f);
}
static void print_regex(jsval value, Stream * f) {
assert(JSVAL_IS_STRING(value));
JSString * s = JSVAL_TO_STRING(value);
size_t length = JSSTRING_LENGTH(s);
jschar * characters = JSSTRING_CHARS(s);
for (size_t i = 0; i < length; i++) {
jschar c = characters[i];
if (32 <= c && c <= 126) {
Stream_write_char(f, c);
}
else {
Stream_printf(f, "\\u%04x", c);
}
}
}
static void print_quoted_string_atom(JSAtom * atom, Stream * f) {
assert(ATOM_IS_STRING(atom));
JSString * s = ATOM_TO_STRING(atom);
Stream_write_char(f, '"');
print_string(s, f);
Stream_write_char(f, '"');
}
static const char * get_op(uint8 op) {
switch(op) {
case JSOP_OR:
return "||";
case JSOP_AND:
return "&&";
case JSOP_BITOR:
return "|";
case JSOP_BITXOR:
return "^";
case JSOP_BITAND:
return "&";
case JSOP_EQ:
return "==";
case JSOP_NE:
return "!=";
case JSOP_STRICTEQ:
return "===";
case JSOP_STRICTNE:
return "!==";
case JSOP_LT:
return "<";
case JSOP_LE:
return "<=";
case JSOP_GT:
return ">";
case JSOP_GE:
return ">=";
case JSOP_LSH:
return "<<";
case JSOP_RSH:
return ">>";
case JSOP_URSH:
return ">>>";
case JSOP_ADD:
return "+";
case JSOP_SUB:
return "-";
case JSOP_MUL:
return "*";
case JSOP_DIV:
return "/";
case JSOP_MOD:
return "%";
default:
abort();
}
}
static void output_expression(JSParseNode * node, Stream * f, bool parenthesize_object_literals);
static void instrument_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if);
enum FunctionType {
FUNCTION_NORMAL,
FUNCTION_GETTER_OR_SETTER
};
static void output_for_in(JSParseNode * node, Stream * f) {
assert(node->pn_type == TOK_FOR);
assert(node->pn_arity == PN_BINARY);
Stream_write_string(f, "for ");
if (node->pn_iflags & JSITER_FOREACH) {
Stream_write_string(f, "each ");
}
Stream_write_char(f, '(');
output_expression(node->pn_left, f, false);
Stream_write_char(f, ')');
}
static void output_array_comprehension_or_generator_expression(JSParseNode * node, Stream * f) {
assert(node->pn_type == TOK_LEXICALSCOPE);
assert(node->pn_arity == PN_NAME);
JSParseNode * for_node = node->pn_expr;
assert(for_node->pn_type == TOK_FOR);
assert(for_node->pn_arity == PN_BINARY);
JSParseNode * p = for_node;
while (p->pn_type == TOK_FOR) {
p = p->pn_right;
}
JSParseNode * if_node = NULL;
if (p->pn_type == TOK_IF) {
if_node = p;
assert(if_node->pn_arity == PN_TERNARY);
p = if_node->pn_kid2;
}
switch (p->pn_arity) {
case PN_UNARY:
p = p->pn_kid;
if (p->pn_type == TOK_YIELD) {
/* for generator expressions */
p = p->pn_kid;
}
output_expression(p, f, false);
break;
case PN_LIST:
/*
When the array comprehension contains "if (0)", it will be optimized away and
the result will be an empty TOK_LC list.
*/
assert(p->pn_type == TOK_LC);
assert(p->pn_head == NULL);
/* the "1" is arbitrary (since the list is empty) */
Stream_write_char(f, '1');
break;
default:
abort();
break;
}
p = for_node;
while (p->pn_type == TOK_FOR) {
Stream_write_char(f, ' ');
output_for_in(p, f);
p = p->pn_right;
}
if (p->pn_type == TOK_LC) {
/* this is the optimized-away "if (0)" */
Stream_write_string(f, " if (0)");
}
else if (if_node) {
Stream_write_string(f, " if (");
output_expression(if_node->pn_kid1, f, false);
Stream_write_char(f, ')');
}
}
static void instrument_function(JSParseNode * node, Stream * f, int indent, enum FunctionType type) {
assert(node->pn_type == TOK_FUNCTION);
assert(node->pn_arity == PN_FUNC);
JSObject * object = node->pn_funpob->object;
assert(JS_ObjectIsFunction(context, object));
JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
assert(function);
assert(object == &function->object);
Stream_printf(f, "%*s", indent, "");
if (type == FUNCTION_NORMAL) {
Stream_write_string(f, "function ");
}
/* function name */
if (function->atom) {
print_string_atom(function->atom, f);
}
/*
function parameters - see JS_DecompileFunction in jsapi.cpp, which calls
js_DecompileFunction in jsopcode.cpp
*/
Stream_write_char(f, '(');
JSArenaPool pool;
JS_INIT_ARENA_POOL(&pool, "instrument_function", 256, 1, &context->scriptStackQuota);
jsuword * local_names = NULL;
if (JS_GET_LOCAL_NAME_COUNT(function)) {
local_names = js_GetLocalNameArray(context, function, &pool);
if (local_names == NULL) {
fatal("out of memory");
}
}
bool destructuring = false;
for (int i = 0; i < function->nargs; i++) {
if (i > 0) {
Stream_write_string(f, ", ");
}
JSAtom * param = JS_LOCAL_NAME_TO_ATOM(local_names[i]);
if (param == NULL) {
destructuring = true;
JSParseNode * expression = NULL;
assert(node->pn_body->pn_type == TOK_LC || node->pn_body->pn_type == TOK_SEQ);
JSParseNode * semi = node->pn_body->pn_head;
assert(semi->pn_type == TOK_SEMI);
JSParseNode * comma = semi->pn_kid;
assert(comma->pn_type == TOK_COMMA);
for (JSParseNode * p = comma->pn_head; p != NULL; p = p->pn_next) {
assert(p->pn_type == TOK_ASSIGN);
JSParseNode * rhs = p->pn_right;
assert(JSSTRING_LENGTH(ATOM_TO_STRING(rhs->pn_atom)) == 0);
if (rhs->pn_slot == i) {
expression = p->pn_left;
break;
}
}
assert(expression != NULL);
output_expression(expression, f, false);
}
else {
print_string_atom(param, f);
}
}
JS_FinishArenaPool(&pool);
Stream_write_string(f, ") {\n");
/* function body */
if (function->flags & JSFUN_EXPR_CLOSURE) {
/* expression closure - use output_statement instead of instrument_statement */
if (node->pn_body->pn_type == TOK_SEQ) {
assert(node->pn_body->pn_arity == PN_LIST);
assert(node->pn_body->pn_count == 2);
output_statement(node->pn_body->pn_head->pn_next, f, indent + 2, false);
}
else {
output_statement(node->pn_body, f, indent + 2, false);
}
}
else {
assert(node->pn_body->pn_type == TOK_LC);
assert(node->pn_body->pn_arity == PN_LIST);
JSParseNode * p = node->pn_body->pn_head;
if (destructuring) {
p = p->pn_next;
}
for (; p != NULL; p = p->pn_next) {
instrument_statement(p, f, indent + 2, false);
}
}
Stream_write_char(f, '}');
}
static void instrument_function_call(JSParseNode * node, Stream * f) {
JSParseNode * function_node = node->pn_head;
if (function_node->pn_type == TOK_FUNCTION) {
JSObject * object = function_node->pn_funpob->object;
assert(JS_ObjectIsFunction(context, object));
JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
assert(function);
assert(object == &function->object);
if (function_node->pn_flags & TCF_GENEXP_LAMBDA) {
/* it's a generator expression */
Stream_write_char(f, '(');
output_array_comprehension_or_generator_expression(function_node->pn_body, f);
Stream_write_char(f, ')');
return;
}
}
output_expression(function_node, f, false);
Stream_write_char(f, '(');
for (struct JSParseNode * p = function_node->pn_next; p != NULL; p = p->pn_next) {
if (p != node->pn_head->pn_next) {
Stream_write_string(f, ", ");
}
output_expression(p, f, false);
}
Stream_write_char(f, ')');
}
static void instrument_declarations(JSParseNode * list, Stream * f) {
assert(list->pn_arity == PN_LIST);
for (JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
if (p != list->pn_head) {
Stream_write_string(f, ", ");
}
output_expression(p, f, false);
}
}
/*
See <Expressions> in jsparse.h.
TOK_FUNCTION is handled as a statement and as an expression.
TOK_DBLDOT is not handled (XML op).
TOK_DEFSHARP and TOK_USESHARP are not handled.
TOK_ANYNAME is not handled (XML op).
TOK_AT is not handled (XML op).
TOK_DBLCOLON is not handled.
TOK_XML* are not handled.
There seem to be some undocumented expressions:
TOK_INSTANCEOF binary
TOK_IN binary
*/
static void output_expression(JSParseNode * node, Stream * f, bool parenthesize_object_literals) {
switch (node->pn_type) {
case TOK_FUNCTION:
Stream_write_char(f, '(');
instrument_function(node, f, 0, FUNCTION_NORMAL);
Stream_write_char(f, ')');
break;
case TOK_COMMA:
for (struct JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) {
if (p != node->pn_head) {
Stream_write_string(f, ", ");
}
output_expression(p, f, parenthesize_object_literals);
}
break;
case TOK_ASSIGN:
output_expression(node->pn_left, f, parenthesize_object_literals);
Stream_write_char(f, ' ');
switch (node->pn_op) {
case JSOP_ADD:
case JSOP_SUB:
case JSOP_MUL:
case JSOP_MOD:
case JSOP_LSH:
case JSOP_RSH:
case JSOP_URSH:
case JSOP_BITAND:
case JSOP_BITOR:
case JSOP_BITXOR:
case JSOP_DIV:
Stream_printf(f, "%s", get_op(node->pn_op));
break;
default:
/* do nothing - it must be a simple assignment */
break;
}
Stream_write_string(f, "= ");
output_expression(node->pn_right, f, false);
break;
case TOK_HOOK:
output_expression(node->pn_kid1, f, parenthesize_object_literals);
Stream_write_string(f, "? ");
output_expression(node->pn_kid2, f, false);
Stream_write_string(f, ": ");
output_expression(node->pn_kid3, f, false);
break;
case TOK_OR:
case TOK_AND:
case TOK_BITOR:
case TOK_BITXOR:
case TOK_BITAND:
case TOK_EQOP:
case TOK_RELOP:
case TOK_SHOP:
case TOK_PLUS:
case TOK_MINUS:
case TOK_STAR:
case TOK_DIVOP:
switch (node->pn_arity) {
case PN_BINARY:
output_expression(node->pn_left, f, parenthesize_object_literals);
Stream_printf(f, " %s ", get_op(node->pn_op));
output_expression(node->pn_right, f, false);
break;
case PN_LIST:
for (struct JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) {
if (p == node->pn_head) {
output_expression(p, f, parenthesize_object_literals);
}
else {
Stream_printf(f, " %s ", get_op(node->pn_op));
output_expression(p, f, false);
}
}
break;
default:
abort();
}
break;
case TOK_UNARYOP:
switch (node->pn_op) {
case JSOP_NEG:
Stream_write_string(f, "- ");
output_expression(node->pn_kid, f, false);
break;
case JSOP_POS:
Stream_write_string(f, "+ ");
output_expression(node->pn_kid, f, false);
break;
case JSOP_NOT:
Stream_write_string(f, "! ");
output_expression(node->pn_kid, f, false);
break;
case JSOP_BITNOT:
Stream_write_string(f, "~ ");
output_expression(node->pn_kid, f, false);
break;
case JSOP_TYPEOF:
Stream_write_string(f, "typeof ");
output_expression(node->pn_kid, f, false);
break;
case JSOP_VOID:
Stream_write_string(f, "void ");
output_expression(node->pn_kid, f, false);
break;
default:
fatal_source(file_id, node->pn_pos.begin.lineno, "unknown operator (%d)", node->pn_op);
break;
}
break;
case TOK_INC:
case TOK_DEC:
/*
This is not documented, but node->pn_op tells whether it is pre- or post-increment.
*/
switch (node->pn_op) {
case JSOP_INCNAME:
case JSOP_INCPROP:
case JSOP_INCELEM:
Stream_write_string(f, "++");
output_expression(node->pn_kid, f, false);
break;
case JSOP_DECNAME:
case JSOP_DECPROP:
case JSOP_DECELEM:
Stream_write_string(f, "--");
output_expression(node->pn_kid, f, false);
break;
case JSOP_NAMEINC:
case JSOP_PROPINC:
case JSOP_ELEMINC:
output_expression(node->pn_kid, f, parenthesize_object_literals);
Stream_write_string(f, "++");
break;
case JSOP_NAMEDEC:
case JSOP_PROPDEC:
case JSOP_ELEMDEC:
output_expression(node->pn_kid, f, parenthesize_object_literals);
Stream_write_string(f, "--");
break;
default:
abort();
break;
}
break;
case TOK_NEW:
Stream_write_string(f, "new ");
instrument_function_call(node, f);
break;
case TOK_DELETE:
Stream_write_string(f, "delete ");
output_expression(node->pn_kid, f, false);
break;
case TOK_DOT:
/* numeric literals must be parenthesized */
switch (node->pn_expr->pn_type) {
case TOK_NUMBER:
Stream_write_char(f, '(');
output_expression(node->pn_expr, f, false);
Stream_write_char(f, ')');
break;
default:
output_expression(node->pn_expr, f, true);
break;
}
/*
This may have originally been x['foo-bar']. Because the string 'foo-bar'
contains illegal characters, we have to use the subscript syntax instead of
the dot syntax.
*/
assert(ATOM_IS_STRING(node->pn_atom));
{
JSString * s = ATOM_TO_STRING(node->pn_atom);
bool must_quote;
if (JSSTRING_LENGTH(s) == 0) {
must_quote = true;
}
else if (js_CheckKeyword(JSSTRING_CHARS(s), JSSTRING_LENGTH(s)) != TOK_EOF) {
must_quote = true;
}
else if (! js_IsIdentifier(s)) {
must_quote = true;
}
else {
must_quote = false;
}
if (must_quote) {
Stream_write_char(f, '[');
print_quoted_string_atom(node->pn_atom, f);
Stream_write_char(f, ']');
}
else {
Stream_write_char(f, '.');
print_string_atom(node->pn_atom, f);
}
}
break;
case TOK_LB:
output_expression(node->pn_left, f, false);
Stream_write_char(f, '[');
output_expression(node->pn_right, f, false);
Stream_write_char(f, ']');
break;
case TOK_LP:
instrument_function_call(node, f);
break;
case TOK_RB:
Stream_write_char(f, '[');
for (struct JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) {
if (p != node->pn_head) {
Stream_write_string(f, ", ");
}
/* TOK_COMMA is a special case: a hole in the array */
if (p->pn_type != TOK_COMMA) {
output_expression(p, f, false);
}
}
if (node->pn_extra == PNX_ENDCOMMA) {
Stream_write_char(f, ',');
}
Stream_write_char(f, ']');
break;
case TOK_RC:
if (parenthesize_object_literals) {
Stream_write_char(f, '(');
}
Stream_write_char(f, '{');
for (struct JSParseNode * p = node->pn_head; p != NULL; p = p->pn_next) {
if (p->pn_type != TOK_COLON) {
fatal_source(file_id, p->pn_pos.begin.lineno, "unsupported node type (%d)", p->pn_type);
}
if (p != node->pn_head) {
Stream_write_string(f, ", ");
}
/* check whether this is a getter or setter */
switch (p->pn_op) {
case JSOP_GETTER:
case JSOP_SETTER:
if (p->pn_op == JSOP_GETTER) {
Stream_write_string(f, "get ");
}
else {
Stream_write_string(f, "set ");
}
output_expression(p->pn_left, f, false);
Stream_write_char(f, ' ');
if (p->pn_right->pn_type != TOK_FUNCTION) {
fatal_source(file_id, p->pn_pos.begin.lineno, "expected function");
}
instrument_function(p->pn_right, f, 0, FUNCTION_GETTER_OR_SETTER);
break;
default:
output_expression(p->pn_left, f, false);
Stream_write_string(f, ": ");
output_expression(p->pn_right, f, false);
break;
}
}
Stream_write_char(f, '}');
if (parenthesize_object_literals) {
Stream_write_char(f, ')');
}
break;
case TOK_RP:
Stream_write_char(f, '(');
output_expression(node->pn_kid, f, false);
Stream_write_char(f, ')');
break;
case TOK_NAME:
print_string_atom(node->pn_atom, f);
if (node->pn_expr != NULL) {
Stream_write_string(f, " = ");
output_expression(node->pn_expr, f, false);
}
break;
case TOK_STRING:
print_quoted_string_atom(node->pn_atom, f);
break;
case TOK_REGEXP:
assert(node->pn_op == JSOP_REGEXP);
{
JSObject * object = node->pn_pob->object;
jsval result;
js_regexp_toString(context, object, &result);
print_regex(result, f);
}
break;
case TOK_NUMBER:
/*
A 64-bit IEEE 754 floating point number has a 52-bit fraction.
2^(-52) = 2.22 x 10^(-16)
Thus there are 16 significant digits.
To keep the output simple, special-case zero.
*/
if (node->pn_dval == 0.0) {
if (signbit(node->pn_dval)) {
Stream_write_string(f, "-0");
}
else {
Stream_write_string(f, "0");
}
}
else if (node->pn_dval == INFINITY) {
Stream_write_string(f, "Number.POSITIVE_INFINITY");
}
else if (node->pn_dval == -INFINITY) {
Stream_write_string(f, "Number.NEGATIVE_INFINITY");
}
else if (isnan(node->pn_dval)) {
Stream_write_string(f, "Number.NaN");
}
else {
Stream_printf(f, "%.15g", node->pn_dval);
}
break;
case TOK_PRIMARY:
switch (node->pn_op) {
case JSOP_TRUE:
Stream_write_string(f, "true");
break;
case JSOP_FALSE:
Stream_write_string(f, "false");
break;
case JSOP_NULL:
Stream_write_string(f, "null");
break;
case JSOP_THIS:
Stream_write_string(f, "this");
break;
/* jsscan.h mentions `super' ??? */
default:
abort();
}
break;
case TOK_INSTANCEOF:
output_expression(node->pn_left, f, parenthesize_object_literals);
Stream_write_string(f, " instanceof ");
output_expression(node->pn_right, f, false);
break;
case TOK_IN:
output_expression(node->pn_left, f, false);
Stream_write_string(f, " in ");
output_expression(node->pn_right, f, false);
break;
case TOK_LEXICALSCOPE:
assert(node->pn_arity == PN_NAME);
assert(node->pn_expr->pn_type == TOK_LET);
assert(node->pn_expr->pn_arity == PN_BINARY);
Stream_write_string(f, "let(");
assert(node->pn_expr->pn_left->pn_type == TOK_LP);
assert(node->pn_expr->pn_left->pn_arity == PN_LIST);
instrument_declarations(node->pn_expr->pn_left, f);
Stream_write_string(f, ") ");
output_expression(node->pn_expr->pn_right, f, true);
break;
case TOK_YIELD:
assert(node->pn_arity == PN_UNARY);
Stream_write_string(f, "yield");
if (node->pn_kid != NULL) {
Stream_write_char(f, ' ');
output_expression(node->pn_kid, f, true);
}
break;
case TOK_ARRAYCOMP:
assert(node->pn_arity == PN_LIST);
{
JSParseNode * block_node;
switch (node->pn_count) {
case 1:
block_node = node->pn_head;
break;
case 2:
block_node = node->pn_head->pn_next;
break;
default:
abort();
break;
}
Stream_write_char(f, '[');
output_array_comprehension_or_generator_expression(block_node, f);
Stream_write_char(f, ']');
}
break;
case TOK_VAR:
assert(node->pn_arity == PN_LIST);
Stream_write_string(f, "var ");
instrument_declarations(node, f);
break;
case TOK_LET:
assert(node->pn_arity == PN_LIST);
Stream_write_string(f, "let ");
instrument_declarations(node, f);
break;
default:
fatal_source(file_id, node->pn_pos.begin.lineno, "unsupported node type (%d)", node->pn_type);
}
}
static void output_statement(JSParseNode * node, Stream * f, int indent, bool is_jscoverage_if) {
switch (node->pn_type) {
case TOK_FUNCTION:
instrument_function(node, f, indent, FUNCTION_NORMAL);
Stream_write_char(f, '\n');
break;
case TOK_LC:
assert(node->pn_arity == PN_LIST);
/*
Stream_write_string(f, "{\n");
*/
for (struct JSParseNode * p = node->pn_u.list.head; p != NULL; p = p->pn_next) {
instrument_statement(p, f, indent, false);
}
/*
Stream_printf(f, "%*s", indent, "");
Stream_write_string(f, "}\n");
*/
break;
case TOK_IF:
{
assert(node->pn_arity == PN_TERNARY);
uint16_t line = node->pn_pos.begin.lineno;
if (! is_jscoverage_if) {
if (line > num_lines) {
fatal("file %s contains more than 65,535 lines", file_id);
}
if (line >= 2 && exclusive_directives[line - 2]) {
is_jscoverage_if = true;
}
}
Stream_printf(f, "%*s", indent, "");
Stream_write_string(f, "if (");
output_expression(node->pn_kid1, f, false);
Stream_write_string(f, ") {\n");
if (is_jscoverage_if && node->pn_kid3) {
uint16_t else_start = node->pn_kid3->pn_pos.begin.lineno;
uint16_t else_end = node->pn_kid3->pn_pos.end.lineno + 1;
Stream_printf(f, "%*s", indent + 2, "");
Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, else_start, else_end);
}
instrument_statement(node->pn_kid2, f, indent + 2, false);
Stream_printf(f, "%*s", indent, "");
Stream_write_string(f, "}\n");
if (node->pn_kid3 || is_jscoverage_if) {
Stream_printf(f, "%*s", indent, "");
Stream_write_string(f, "else {\n");
if (is_jscoverage_if) {
uint16_t if_start = node->pn_kid2->pn_pos.begin.lineno + 1;
uint16_t if_end = node->pn_kid2->pn_pos.end.lineno + 1;
Stream_printf(f, "%*s", indent + 2, "");
Stream_printf(f, "_$jscoverage['%s'].conditionals[%d] = %d;\n", file_id, if_start, if_end);
}
if (node->pn_kid3) {
instrument_statement(node->pn_kid3, f, indent + 2, is_jscoverage_if);
}
Stream_printf(f, "%*s", indent, "");
Stream_write_string(f, "}\n");
}
break;
}
case TOK_SWITCH:
assert(node->pn_arity == PN_BINARY);
Stream_printf(f, "%*s", indent, "");
Stream_write_string(f, "switch (");
output_expression(node->pn_left, f, false);
Stream_write_string(f, ") {\n");
{
JSParseNode * list = node->pn_right;
if (list->pn_type == TOK_LEXICALSCOPE) {
list = list->pn_expr;
}
for (struct JSParseNode * p = list->pn_head; p != NULL; p = p->pn_next) {
Stream_printf(f, "%*s", indent, "");
switch (p->pn_type) {
case TOK_CASE:
Stream_write_string(f, "case ");
output_expression(p->pn_left, f, false);
Stream_write_string(f, ":\n");
break;
case TOK_DEFAULT:
Stream_write_string(f, "default:\n");
break;
default:
abort();
break;
}
instrument_statement(p->pn_right, f, indent + 2, false);
}
}
Stream_printf(f, "%*s", indent, "");
Stream_write_string(f, "}\n");
break;
case TOK_CASE:
case TOK_DEFAULT:
abort();
break;
case TOK_WHILE:
assert(node->pn_arity == PN_BINARY);
Stream_printf(f, "%*s", indent, "");
Stream_write_string(f, "while (");
output_expression(node->pn_left, f, false);
Stream_write_string(f, ") {\n");