-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsopcode.c
More file actions
2219 lines (1989 loc) · 54.2 KB
/
jsopcode.c
File metadata and controls
2219 lines (1989 loc) · 54.2 KB
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
/*
* JS bytecode descriptors, disassemblers, and decompilers.
*/
#include "jsstddef.h"
#include <memory.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jstypes.h"
#include "jsarena.h" /* Added by JSIFY */
#include "jsutil.h" /* Added by JSIFY */
#include "jsdtoa.h"
#include "jsprf.h"
#include "jsapi.h"
#include "jsarray.h"
#include "jsatom.h"
#include "jscntxt.h"
#include "jsconfig.h"
#include "jsdbgapi.h"
#include "jsemit.h"
#include "jsfun.h"
#include "jslock.h"
#include "jsobj.h"
#include "jsopcode.h"
#include "jsscope.h"
#include "jsscript.h"
#include "jsstr.h"
char js_in_str[] = "in";
char js_instanceof_str[] = "instanceof";
char js_new_str[] = "new";
char js_delete_str[] = "delete";
char js_typeof_str[] = "typeof";
char js_void_str[] = "void";
char js_null_str[] = "null";
char js_this_str[] = "this";
char js_false_str[] = "false";
char js_true_str[] = "true";
char *js_incop_str[] = {"++", "--"};
/* Pollute the namespace locally for MSVC Win16, but not for WatCom. */
#ifdef __WINDOWS_386__
#ifdef FAR
#undef FAR
#endif
#else /* !__WINDOWS_386__ */
#ifndef FAR
#define FAR
#endif
#endif /* !__WINDOWS_386__ */
JSCodeSpec FAR js_CodeSpec[] = {
#define OPDEF(op,val,name,token,length,nuses,ndefs,prec,format) \
{name,token,length,nuses,ndefs,prec,format},
#include "jsopcode.tbl"
#undef OPDEF
};
uintN js_NumCodeSpecs = sizeof (js_CodeSpec) / sizeof js_CodeSpec[0];
/************************************************************************/
#ifdef DEBUG
JS_FRIEND_API(void)
js_Disassemble(JSContext *cx, JSScript *script, JSBool lines, FILE *fp)
{
jsbytecode *pc, *end;
uintN len;
pc = script->code;
end = pc + script->length;
while (pc < end) {
len = js_Disassemble1(cx, script, pc, pc - script->code, lines, fp);
if (!len)
return;
pc += len;
}
}
JS_FRIEND_API(uintN)
js_Disassemble1(JSContext *cx, JSScript *script, jsbytecode *pc, uintN loc,
JSBool lines, FILE *fp)
{
JSOp op;
JSCodeSpec *cs;
intN len, off;
JSAtom *atom;
JSString *str;
char *cstr;
op = (JSOp)*pc;
if (op >= JSOP_LIMIT) {
char numBuf1[12], numBuf2[12];
JS_snprintf(numBuf1, sizeof numBuf1, "%d", op);
JS_snprintf(numBuf2, sizeof numBuf2, "%d", JSOP_LIMIT);
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
JSMSG_BYTECODE_TOO_BIG, numBuf1, numBuf2);
return 0;
}
cs = &js_CodeSpec[op];
len = (intN)cs->length;
fprintf(fp, "%05u:", loc);
if (lines)
fprintf(fp, "%4u", JS_PCToLineNumber(cx, script, pc));
fprintf(fp, " %s", cs->name);
switch (cs->format & JOF_TYPEMASK) {
case JOF_BYTE:
if (op == JSOP_TRAP) {
op = JS_GetTrapOpcode(cx, script, pc);
if (op == JSOP_LIMIT)
return 0;
len = (intN)js_CodeSpec[op].length;
}
break;
case JOF_JUMP:
off = GET_JUMP_OFFSET(pc);
fprintf(fp, " %u (%d)", loc + off, off);
break;
case JOF_CONST:
atom = GET_ATOM(cx, script, pc);
str = js_ValueToString(cx, ATOM_KEY(atom));
if (!str)
return 0;
cstr = js_DeflateString(cx, str->chars, str->length);
if (!cstr)
return 0;
fprintf(fp, (op == JSOP_STRING) ? " \"%s\"" : " %s", cstr);
JS_free(cx, cstr);
break;
case JOF_UINT16:
fprintf(fp, " %u", GET_ARGC(pc));
break;
#if JS_HAS_SWITCH_STATEMENT
case JOF_TABLESWITCH:
{
jsbytecode *pc2, *end;
jsint i, low, high;
pc2 = pc;
off = GET_JUMP_OFFSET(pc2);
end = pc + off;
pc2 += JUMP_OFFSET_LEN;
low = GET_JUMP_OFFSET(pc2);
pc2 += JUMP_OFFSET_LEN;
high = GET_JUMP_OFFSET(pc2);
pc2 += JUMP_OFFSET_LEN;
fprintf(fp, " defaultOffset %d low %d high %d", off, low, high);
if (pc2 + 1 < end) {
for (i = low; i <= high; i++) {
off = GET_JUMP_OFFSET(pc2);
fprintf(fp, "\n\t%d: %d", i, off);
pc2 += JUMP_OFFSET_LEN;
}
}
len = 1 + pc2 - pc;
break;
}
case JOF_LOOKUPSWITCH:
{
jsbytecode *pc2 = pc;
uintN npairs;
jsval key;
off = GET_JUMP_OFFSET(pc2);
pc2 += JUMP_OFFSET_LEN;
npairs = (uintN) GET_ATOM_INDEX(pc2);
pc2 += ATOM_INDEX_LEN;
fprintf(fp, " offset %d npairs %u", off, npairs);
while (npairs) {
atom = GET_ATOM(cx, script, pc2);
pc2 += ATOM_INDEX_LEN;
off = GET_JUMP_OFFSET(pc2);
pc2 += JUMP_OFFSET_LEN;
key = ATOM_KEY(atom);
str = js_ValueToString(cx, key);
if (!str)
return 0;
cstr = js_DeflateString(cx, str->chars, str->length);
if (!cstr)
return 0;
if (JSVAL_IS_STRING(key))
fprintf(fp, "\n\t\"%s\": %d", cstr, off);
else
fprintf(fp, "\n\t%s: %d", cstr, off);
JS_free(cx, cstr);
npairs--;
}
len = 1 + pc2 - pc;
break;
}
#endif /* JS_HAS_SWITCH_STATEMENT */
case JOF_QARG:
fprintf(fp, " %u", GET_ARGNO(pc));
break;
case JOF_QVAR:
fprintf(fp, " %u", GET_VARNO(pc));
break;
default: {
char numBuf[12];
JS_snprintf(numBuf, sizeof numBuf, "%lx", (unsigned long) cs->format);
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
JSMSG_UNKNOWN_FORMAT, numBuf);
return 0;
}
}
fputs("\n", fp);
return len;
}
#endif /* DEBUG */
/************************************************************************/
/*
* Sprintf, but with unlimited and automatically allocated buffering.
*/
typedef struct Sprinter {
JSContext *context; /* context executing the decompiler */
JSArenaPool *pool; /* string allocation pool */
char *base; /* base address of buffer in pool */
size_t size; /* size of buffer allocated at base */
ptrdiff_t offset; /* offset of next free char in buffer */
} Sprinter;
#define INIT_SPRINTER(cx, sp, ap, off) \
((sp)->context = cx, (sp)->pool = ap, (sp)->base = NULL, (sp)->size = 0, \
(sp)->offset = off)
#define OFF2STR(sp,off) ((sp)->base + (off))
#define STR2OFF(sp,str) ((str) - (sp)->base)
static JSBool
SprintAlloc(Sprinter *sp, size_t nb)
{
if (!sp->base) {
JS_ARENA_ALLOCATE(sp->base, sp->pool, nb);
} else {
JS_ARENA_GROW(sp->base, sp->pool, sp->size, nb);
}
if (!sp->base) {
JS_ReportOutOfMemory(sp->context);
return JS_FALSE;
}
sp->size += nb;
return JS_TRUE;
}
static ptrdiff_t
SprintPut(Sprinter *sp, const char *s, size_t len)
{
ptrdiff_t nb, offset;
char *bp;
/* Allocate space for s, including the '\0' at the end. */
nb = (sp->offset + len + 1) - sp->size;
if (nb > 0 && !SprintAlloc(sp, nb))
return -1;
/* Advance offset and copy s into sp's buffer. */
offset = sp->offset;
sp->offset += len;
bp = sp->base + offset;
memcpy(bp, s, len);
bp[len] = 0;
return offset;
}
static ptrdiff_t
Sprint(Sprinter *sp, const char *format, ...)
{
va_list ap;
char *bp;
ptrdiff_t offset;
va_start(ap, format);
bp = JS_vsmprintf(format, ap); /* XXX vsaprintf */
va_end(ap);
if (!bp) {
JS_ReportOutOfMemory(sp->context);
return -1;
}
offset = SprintPut(sp, bp, strlen(bp));
free(bp);
return offset;
}
jschar js_EscapeMap[] = {
'\b', 'b',
'\f', 'f',
'\n', 'n',
'\r', 'r',
'\t', 't',
'\v', 'v',
'"', '"',
'\'', '\''
};
static char *
QuoteString(Sprinter *sp, JSString *str, jschar quote)
{
ptrdiff_t off, len, nb;
const jschar *s, *t, *u;
char *bp;
jschar c;
JSBool ok;
off = sp->offset;
s = str->chars;
t = s;
c = *t;
if (Sprint(sp, "%c", (char)quote) < 0)
return NULL;
do {
while (JS_ISPRINT(c) && c != quote && !(c >> 8))
c = *++t;
len = PTRDIFF(t, s, jschar);
/* Allocate space for s, including the '\0' at the end. */
nb = (sp->offset + len + 1) - sp->size;
if (nb > 0 && !SprintAlloc(sp, nb))
return NULL;
/* Advance sp->offset and copy s into sp's buffer. */
bp = sp->base + sp->offset;
sp->offset += len;
while (--len >= 0)
*bp++ = (char) *s++;
*bp = '\0';
if (c == 0)
break;
if ((u = js_strchr(js_EscapeMap, c)) != NULL)
ok = Sprint(sp, "\\%c", (char)u[1]) >= 0;
else
ok = Sprint(sp, (c >> 8) ? "\\u%04X" : "\\x%02X", c) >= 0;
if (!ok)
return NULL;
s = ++t;
c = *t;
} while (c != 0);
if (Sprint(sp, "%c", (char)quote) < 0)
return NULL;
return OFF2STR(sp, off);
}
JSString *
js_QuoteString(JSContext *cx, JSString *str, jschar quote)
{
void *mark;
Sprinter sprinter;
char *bytes;
JSString *escstr;
mark = JS_ARENA_MARK(&cx->tempPool);
INIT_SPRINTER(cx, &sprinter, &cx->tempPool, 0);
bytes = QuoteString(&sprinter, str, quote);
escstr = bytes ? JS_NewStringCopyZ(cx, bytes) : NULL;
JS_ARENA_RELEASE(&cx->tempPool, mark);
return escstr;
}
/************************************************************************/
struct JSPrinter {
Sprinter sprinter; /* base class state */
JSArenaPool pool; /* string allocation pool */
uintN indent; /* indentation in spaces */
JSScript *script; /* script being printed */
JSScope *scope; /* script function scope */
};
JSPrinter *
js_NewPrinter(JSContext *cx, const char *name, uintN indent)
{
JSPrinter *jp;
JSStackFrame *fp;
JSObject *obj;
JSObjectMap *map;
jp = JS_malloc(cx, sizeof(JSPrinter));
if (!jp)
return NULL;
INIT_SPRINTER(cx, &jp->sprinter, &jp->pool, 0);
JS_InitArenaPool(&jp->pool, name, 256, 1);
jp->indent = indent;
jp->script = NULL;
jp->scope = NULL;
fp = cx->fp;
if (fp && fp->scopeChain) {
obj = fp->scopeChain;
map = obj->map;
if (map->ops == &js_ObjectOps) {
if (OBJ_GET_CLASS(cx, obj) == &js_CallClass) {
obj = fp->fun ? fp->fun->object : NULL;
if (obj)
map = obj->map;
}
jp->scope = (JSScope *)map;
}
}
return jp;
}
void
js_DestroyPrinter(JSPrinter *jp)
{
JS_FinishArenaPool(&jp->pool);
JS_free(jp->sprinter.context, jp);
}
JSString *
js_GetPrinterOutput(JSPrinter *jp)
{
JSContext *cx;
JSString *str;
cx = jp->sprinter.context;
if (!jp->sprinter.base)
return cx->runtime->emptyString;
str = JS_NewStringCopyZ(cx, jp->sprinter.base);
if (!str)
return NULL;
JS_FreeArenaPool(&jp->pool);
INIT_SPRINTER(cx, &jp->sprinter, &jp->pool, 0);
return str;
}
int
js_printf(JSPrinter *jp, char *format, ...)
{
va_list ap;
char *bp;
int cc;
va_start(ap, format);
/* Expand magic tab into a run of jp->indent spaces. */
if (*format == '\t') {
if (Sprint(&jp->sprinter, "%*s", jp->indent, "") < 0)
return -1;
format++;
}
/* Allocate temp space, convert format, and put. */
bp = JS_vsmprintf(format, ap); /* XXX vsaprintf */
if (!bp) {
JS_ReportOutOfMemory(jp->sprinter.context);
return -1;
}
cc = strlen(bp);
if (SprintPut(&jp->sprinter, bp, (size_t)cc) < 0)
cc = -1;
free(bp);
va_end(ap);
return cc;
}
JSBool
js_puts(JSPrinter *jp, char *s)
{
return SprintPut(&jp->sprinter, s, strlen(s)) >= 0;
}
/************************************************************************/
typedef struct SprintStack {
Sprinter sprinter; /* sprinter for postfix to infix buffering */
ptrdiff_t *offsets; /* stack of postfix string offsets */
jsbytecode *opcodes; /* parallel stack of JS opcodes */
uintN top; /* top of stack index */
JSPrinter *printer; /* permanent output goes here */
} SprintStack;
/* Gap between stacked strings to allow for insertion of parens and commas. */
#define PAREN_SLOP (2 + 1)
/*
* These pseudo-ops help js_DecompileValueGenerator decompile JSOP_SETNAME,
* JSOP_SETPROP, and JSOP_SETELEM, respectively. See the first assertion in
* PushOff.
*/
#define JSOP_GETPROP2 254
#define JSOP_GETELEM2 255
static JSBool
PushOff(SprintStack *ss, ptrdiff_t off, JSOp op)
{
JS_ASSERT(JSOP_LIMIT <= 254);
if (!SprintAlloc(&ss->sprinter, PAREN_SLOP))
return JS_FALSE;
JS_ASSERT(ss->top < ss->printer->script->depth);
ss->offsets[ss->top] = off;
ss->opcodes[ss->top++] = (op >= 128) ? op - 128 : op;
ss->sprinter.offset += PAREN_SLOP;
return JS_TRUE;
}
static ptrdiff_t
PopOff(SprintStack *ss, JSOp op)
{
uintN top;
JSCodeSpec *cs, *topcs;
ptrdiff_t off;
top = --ss->top;
cs = &js_CodeSpec[op];
topcs = &js_CodeSpec[ss->opcodes[top]];
if (topcs->prec != 0 && topcs->prec < cs->prec) {
ss->offsets[top] -= 2;
ss->sprinter.offset = ss->offsets[top];
off = Sprint(&ss->sprinter, "(%s)",
OFF2STR(&ss->sprinter, ss->sprinter.offset + 2));
} else {
off = ss->sprinter.offset = ss->offsets[top];
}
return off;
}
#if JS_HAS_SWITCH_STATEMENT
typedef struct TableEntry {
jsval key;
ptrdiff_t offset;
} TableEntry;
static int
CompareOffsets(const void *v1, const void *v2, void *arg)
{
const TableEntry *te1 = v1, *te2 = v2;
return te1->offset - te2->offset;
}
static JSBool
Decompile(SprintStack *ss, jsbytecode *pc, intN nb);
static JSBool
DecompileSwitch(SprintStack *ss, TableEntry *table, uintN tableLength,
jsbytecode *pc, ptrdiff_t switchLength,
ptrdiff_t defaultOffset, JSBool isCondSwitch)
{
JSContext *cx;
JSPrinter *jp;
char *lval, *rval;
uintN i;
ptrdiff_t diff, off, off2, caseExprOff;
jsval key;
JSString *str;
cx = ss->sprinter.context;
jp = ss->printer;
lval = OFF2STR(&ss->sprinter, PopOff(ss, JSOP_NOP));
js_printf(jp, "\tswitch (%s) {\n", lval);
if (tableLength) {
diff = table[0].offset - defaultOffset;
if (diff > 0) {
jp->indent += 2;
js_printf(jp, "\tdefault:\n");
jp->indent += 2;
if (!Decompile(ss, pc + defaultOffset, diff))
return JS_FALSE;
jp->indent -= 4;
}
caseExprOff = isCondSwitch
? (ptrdiff_t) js_CodeSpec[JSOP_CONDSWITCH].length
: 0;
for (i = 0; i < tableLength; i++) {
off = table[i].offset;
if (i + 1 < tableLength)
off2 = table[i + 1].offset;
else
off2 = switchLength;
key = table[i].key;
if (isCondSwitch) {
ptrdiff_t nextCaseExprOff;
/*
* key encodes the JSOP_CASE bytecode's offset from switchtop.
* The next case expression follows immediately, unless we are
* at the last case.
*/
nextCaseExprOff = (ptrdiff_t)
(JSVAL_TO_INT(key) + js_CodeSpec[JSOP_CASE].length);
jp->indent += 2;
if (!Decompile(ss, pc + caseExprOff,
nextCaseExprOff - caseExprOff)) {
return JS_FALSE;
}
caseExprOff = nextCaseExprOff;
} else {
/*
* key comes from an atom, not the decompiler, so we need to
* quote it if it's a string literal.
*/
str = js_ValueToString(cx, key);
if (!str)
return JS_FALSE;
jp->indent += 2;
if (JSVAL_IS_STRING(key)) {
rval = QuoteString(&ss->sprinter, str, '"');
if (!rval)
return JS_FALSE;
} else {
rval = JS_GetStringBytes(str);
}
js_printf(jp, "\tcase %s:\n", rval);
}
jp->indent += 2;
if (off <= defaultOffset && defaultOffset < off2) {
diff = defaultOffset - off;
if (diff != 0) {
if (!Decompile(ss, pc + off, diff))
return JS_FALSE;
off = defaultOffset;
}
jp->indent -= 2;
js_printf(jp, "\tdefault:\n");
jp->indent += 2;
}
if (!Decompile(ss, pc + off, off2 - off))
return JS_FALSE;
jp->indent -= 4;
}
}
if (defaultOffset == switchLength) {
jp->indent += 2;
js_printf(jp, "\tdefault:\n");
jp->indent -= 2;
}
js_printf(jp, "\t}\n");
return JS_TRUE;
}
#endif
static JSAtom *
GetSlotAtom(JSScope *scope, JSPropertyOp getter, uintN slot)
{
JSScopeProperty *sprop;
if (!scope)
return NULL;
for (sprop = scope->props; sprop; sprop = sprop->next) {
if (sprop->getter != getter)
continue;
if ((uintN)JSVAL_TO_INT(sprop->id) == slot)
return sym_atom(sprop->symbols);
}
return NULL;
}
static JSBool
Decompile(SprintStack *ss, jsbytecode *pc, intN nb)
{
JSContext *cx;
JSPrinter *jp;
jsbytecode *endpc, *done;
ptrdiff_t len, todo, oplen, cond, next, tail;
JSOp op, lastop, saveop;
JSCodeSpec *cs, *topcs;
jssrcnote *sn;
const char *lval, *rval = NULL, *xval;
jsint i, argc;
char **argv;
JSAtom *atom;
JSString *str;
JSBool ok;
jsval key;
/*
* Local macros
*/
#define DECOMPILE_CODE(pc,nb) if (!Decompile(ss, pc, nb)) return JS_FALSE
#define POP_STR() OFF2STR(&ss->sprinter, PopOff(ss, op))
#define LOCAL_ASSERT(expr) JS_ASSERT(expr); if (!(expr)) return JS_FALSE
cx = ss->sprinter.context;
jp = ss->printer;
endpc = pc + nb;
todo = -2; /* NB: different from Sprint() error return. */
op = JSOP_NOP;
while (pc < endpc) {
lastop = op;
op = saveop = *pc;
if (op >= JSOP_LIMIT) {
switch (op) {
case JSOP_GETPROP2:
saveop = JSOP_GETPROP;
break;
case JSOP_GETELEM2:
saveop = JSOP_GETELEM;
break;
default:;
}
}
cs = &js_CodeSpec[saveop];
len = oplen = cs->length;
if (cs->token) {
switch (cs->nuses) {
case 2:
rval = POP_STR();
lval = POP_STR();
if ((sn = js_GetSrcNote(jp->script, pc)) != NULL &&
SN_TYPE(sn) == SRC_ASSIGNOP) {
/* Print only the right operand of the assignment-op. */
todo = SprintPut(&ss->sprinter, rval, strlen(rval));
} else {
todo = Sprint(&ss->sprinter, "%s %s %s",
lval, cs->token, rval);
}
break;
case 1:
rval = POP_STR();
todo = Sprint(&ss->sprinter, "%s%s", cs->token, rval);
break;
case 0:
todo = SprintPut(&ss->sprinter, cs->token, strlen(cs->token));
break;
default:
todo = -2;
break;
}
} else {
switch (op) {
case JSOP_NOP:
/*
* Check for extra user parenthesization, a do-while loop,
* a for-loop with an empty initializer part, a labeled
* statement, a function definition, or try/finally.
*/
sn = js_GetSrcNote(jp->script, pc);
todo = -2;
switch (sn ? SN_TYPE(sn) : SRC_NULL) {
case SRC_PAREN:
/* Use last real op so PopOff adds parens if needed. */
todo = PopOff(ss, lastop);
/* Now add user-supplied parens only if PopOff did not. */
cs = &js_CodeSpec[lastop];
topcs = &js_CodeSpec[ss->opcodes[ss->top]];
if (topcs->prec >= cs->prec) {
todo = Sprint(&ss->sprinter, "(%s)",
OFF2STR(&ss->sprinter, todo));
}
break;
#if JS_HAS_DO_WHILE_LOOP
case SRC_WHILE:
js_printf(jp, "\tdo {\n"); /* balance} */
jp->indent += 4;
break;
#endif /* JS_HAS_DO_WHILE_LOOP */
case SRC_FOR:
rval = "";
do_forloop:
/* Skip the JSOP_NOP or JSOP_POP bytecode. */
pc++;
/* Get the cond, next, and loop-closing tail offsets. */
cond = js_GetSrcNoteOffset(sn, 0);
next = js_GetSrcNoteOffset(sn, 1);
tail = js_GetSrcNoteOffset(sn, 2);
LOCAL_ASSERT(tail + GET_JUMP_OFFSET(pc + tail) == 0);
/* Print the keyword and the possibly empty init-part. */
js_printf(jp, "\tfor (%s;", rval);
if (pc[cond] == JSOP_IFEQ) {
/* Decompile the loop condition. */
DECOMPILE_CODE(pc, cond);
js_printf(jp, " %s", POP_STR());
}
/* Need a semicolon whether or not there was a cond. */
js_puts(jp, ";");
if (pc[next] != JSOP_GOTO) {
/* Decompile the loop updater. */
DECOMPILE_CODE(pc + next, tail - next - 1);
js_printf(jp, " %s", POP_STR());
}
/* Do the loop body. */
js_puts(jp, ") {\n");
jp->indent += 4;
oplen = (cond) ? js_CodeSpec[pc[cond]].length : 0;
DECOMPILE_CODE(pc + cond + oplen, next - cond - oplen);
jp->indent -= 4;
js_printf(jp, "\t}\n");
/* Set len so pc skips over the entire loop. */
len = tail + js_CodeSpec[pc[tail]].length;
break;
case SRC_LABEL:
atom = js_GetAtom(cx, &jp->script->atomMap,
(jsatomid) js_GetSrcNoteOffset(sn, 0));
jp->indent -= 4;
js_printf(jp, "\t%s:\n", ATOM_BYTES(atom));
jp->indent += 4;
break;
case SRC_LABELBRACE:
atom = js_GetAtom(cx, &jp->script->atomMap,
(jsatomid) js_GetSrcNoteOffset(sn, 0));
js_printf(jp, "\t%s: {\n", ATOM_BYTES(atom));
jp->indent += 4;
break;
case SRC_ENDBRACE:
jp->indent -= 4;
js_printf(jp, "\t}\n");
break;
case SRC_TRYFIN:
if (js_GetSrcNoteOffset(sn, 0) == 0) {
js_printf(jp, "\ttry {\n");
} else {
jp->indent -= 4;
js_printf(jp, "\t} finally {\n");
}
jp->indent += 4;
break;
case SRC_CATCH:
jp->indent -= 4;
sn = js_GetSrcNote(jp->script, pc);
pc += oplen;
js_printf(jp, "\t} catch ("); /* balance) */
pc += 6; /* name Object, pushobj, exception */
js_printf(jp, "%s",
ATOM_BYTES(GET_ATOM(cx, jp->script, pc)));
len = js_GetSrcNoteOffset(sn, 0);
pc += 4; /* initprop, enterwith */
if (len) {
js_printf(jp, " : ");
DECOMPILE_CODE(pc, len - 3); /* don't decompile ifeq */
js_printf(jp, "%s", POP_STR());
pc += len;
}
js_printf(jp, ") {\n"); /* balance} */
jp->indent += 4;
len = 0;
break;
case SRC_FUNCDEF: {
JSPrinter *jp2;
JSObject *obj;
JSFunction *fun;
atom = js_GetAtom(cx, &jp->script->atomMap,
(jsatomid) js_GetSrcNoteOffset(sn, 0));
JS_ASSERT(ATOM_IS_OBJECT(atom));
obj = ATOM_TO_OBJECT(atom);
fun = JS_GetPrivate(cx, obj);
jp2 = js_NewPrinter(cx, JS_GetFunctionName(fun),
jp->indent);
if (!jp2)
return JS_FALSE;
jp2->scope = jp->scope;
if (js_DecompileFunction(jp2, fun, JS_TRUE)) {
str = js_GetPrinterOutput(jp2);
if (str)
js_printf(jp, "%s\n", JS_GetStringBytes(str));
}
js_DestroyPrinter(jp2);
break;
}
default:;
}
break;
case JSOP_PUSH:
case JSOP_PUSHOBJ:
case JSOP_BINDNAME:
todo = Sprint(&ss->sprinter, "");
break;
#if JS_HAS_EXCEPTIONS
case JSOP_GOSUB:
case JSOP_RETSUB:
case JSOP_SETSP:
todo = -2;
break;
case JSOP_EXCEPTION:
sn = js_GetSrcNote(jp->script, pc);
if (sn && SN_TYPE(sn) == SRC_HIDDEN)
todo = -2;
break;
#endif /* JS_HAS_EXCEPTIONS */
case JSOP_POP:
case JSOP_POPV:
sn = js_GetSrcNote(jp->script, pc);
switch (sn ? SN_TYPE(sn) : SRC_NULL) {
case SRC_FOR:
rval = POP_STR();
todo = -2;
goto do_forloop;
case SRC_PCDELTA:
/* Pop and save to avoid blowing stack depth budget. */
lval = JS_strdup(cx, POP_STR());
if (!lval)
return JS_FALSE;
/*
* The offset tells distance to the end of the right-hand
* operand of the comma operator.
*/
done = pc + len;
pc += js_GetSrcNoteOffset(sn, 0);
len = 0;
if (!Decompile(ss, done, pc - done)) {
JS_free(cx, (char *)lval);
return JS_FALSE;
}
/* Pop Decompile result and print comma expression. */
rval = POP_STR();
todo = Sprint(&ss->sprinter, "%s, %s", lval, rval);
JS_free(cx, (char *)lval);
break;
case SRC_HIDDEN:
/* Hide this pop, it's from a goto in a with or for/in. */
todo = -2;
break;
default:
rval = POP_STR();
if (*rval != '\0')
js_printf(jp, "\t%s;\n", rval);
todo = -2;
break;
}
break;
case JSOP_POP2:
(void) PopOff(ss, op);
(void) PopOff(ss, op);
todo = -2;
break;
case JSOP_ENTERWITH:
sn = js_GetSrcNote(jp->script, pc);
todo = -2;
if (sn && SN_TYPE(sn) == SRC_HIDDEN)
break;
rval = POP_STR();
js_printf(jp, "\twith (%s) {\n", rval);
jp->indent += 4;
break;
case JSOP_LEAVEWITH:
sn = js_GetSrcNote(jp->script, pc);