forked from mozilla/rhino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterpreter.java
More file actions
1993 lines (1887 loc) · 92.1 KB
/
Copy pathInterpreter.java
File metadata and controls
1993 lines (1887 loc) · 92.1 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: java; 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.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Rhino code, released
* May 6, 1999.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1997-2000 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Patrick Beard
* Norris Boyd
* Roger Lawrence
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU Public License (the "GPL"), in which case the
* provisions of the GPL are applicable instead of those above.
* If you wish to allow use of your version of this file only
* under the terms of the GPL and not to allow others to use your
* version of this file under the NPL, indicate your decision by
* deleting the provisions above and replace them with the notice
* and other provisions required by the GPL. If you do not delete
* the provisions above, a recipient may use your version of this
* file under either the NPL or the GPL.
*/
package org.mozilla.javascript;
import java.io.*;
import java.util.Vector;
import java.util.Enumeration;
import org.mozilla.javascript.debug.*;
public class Interpreter extends LabelTable {
public static final boolean printICode = false;
public IRFactory createIRFactory(TokenStream ts,
ClassNameHelper nameHelper, Scriptable scope)
{
return new IRFactory(ts, scope);
}
public Node transform(Node tree, TokenStream ts, Scriptable scope) {
return (new NodeTransformer()).transform(tree, null, ts, scope);
}
public Object compile(Context cx, Scriptable scope, Node tree,
Object securityDomain,
SecuritySupport securitySupport,
ClassNameHelper nameHelper)
throws IOException
{
version = cx.getLanguageVersion();
itsData = new InterpreterData(0, 0, 0, securityDomain,
cx.hasCompileFunctionsWithDynamicScope());
if (tree instanceof FunctionNode) {
FunctionNode f = (FunctionNode) tree;
InterpretedFunction result =
generateFunctionICode(cx, scope, f, securityDomain);
result.itsData.itsFunctionType = f.getFunctionType();
createFunctionObject(result, scope);
return result;
}
return generateScriptICode(cx, scope, tree, securityDomain);
}
private void generateICodeFromTree(Node tree,
VariableTable varTable,
boolean needsActivation,
Object securityDomain)
{
int theICodeTop = 0;
itsData.itsVariableTable = varTable;
itsData.itsNeedsActivation = needsActivation;
theICodeTop = generateICode(tree, theICodeTop);
itsData.itsICodeTop = theICodeTop;
if (itsEpilogLabel != -1)
markLabel(itsEpilogLabel, theICodeTop);
for (int i = 0; i < itsLabelTableTop; i++)
itsLabelTable[i].fixGotos(itsData.itsICode);
}
private Object[] generateRegExpLiterals(Context cx,
Scriptable scope,
Vector regexps)
{
Object[] result = new Object[regexps.size()];
RegExpProxy rep = cx.getRegExpProxy();
for (int i = 0; i < regexps.size(); i++) {
Node regexp = (Node) regexps.elementAt(i);
Node left = regexp.getFirstChild();
Node right = regexp.getLastChild();
result[i] = rep.newRegExp(cx, scope, left.getString(),
(left != right) ? right.getString() : null, false);
regexp.putProp(Node.REGEXP_PROP, new Integer(i));
}
return result;
}
private InterpretedScript generateScriptICode(Context cx,
Scriptable scope,
Node tree,
Object securityDomain)
{
itsSourceFile = (String) tree.getProp(Node.SOURCENAME_PROP);
itsFunctionList = (Vector) tree.getProp(Node.FUNCTION_PROP);
if (itsFunctionList != null)
generateNestedFunctions(scope, cx, securityDomain);
Object[] regExpLiterals = null;
Vector regexps = (Vector)tree.getProp(Node.REGEXP_PROP);
if (regexps != null)
regExpLiterals = generateRegExpLiterals(cx, scope, regexps);
VariableTable varTable = (VariableTable)tree.getProp(Node.VARS_PROP);
generateICodeFromTree(tree, varTable, false, securityDomain);
itsData.itsNestedFunctions = itsNestedFunctions;
itsData.itsRegExpLiterals = regExpLiterals;
if (printICode) dumpICode(itsData);
InterpretedScript result = new InterpretedScript(itsData, cx);
if (cx.debugger != null)
cx.debugger.handleCompilationDone(cx, result, null);
return result;
}
private void generateNestedFunctions(Scriptable scope,
Context cx,
Object securityDomain)
{
itsNestedFunctions = new InterpretedFunction[itsFunctionList.size()];
for (short i = 0; i < itsFunctionList.size(); i++) {
FunctionNode def = (FunctionNode)itsFunctionList.elementAt(i);
Interpreter jsi = new Interpreter();
jsi.itsSourceFile = itsSourceFile;
jsi.itsData = new InterpreterData(0, 0, 0, securityDomain,
cx.hasCompileFunctionsWithDynamicScope());
jsi.itsData.itsFunctionType = def.getFunctionType();
jsi.itsInFunctionFlag = true;
itsNestedFunctions[i] = jsi.generateFunctionICode(cx, scope, def,
securityDomain);
def.putProp(Node.FUNCTION_PROP, new Short(i));
}
}
private InterpretedFunction
generateFunctionICode(Context cx, Scriptable scope,
FunctionNode theFunction, Object securityDomain)
{
itsFunctionList = (Vector) theFunction.getProp(Node.FUNCTION_PROP);
if (itsFunctionList != null)
generateNestedFunctions(scope, cx, securityDomain);
Object[] regExpLiterals = null;
Vector regexps = (Vector)theFunction.getProp(Node.REGEXP_PROP);
if (regexps != null)
regExpLiterals = generateRegExpLiterals(cx, scope, regexps);
VariableTable varTable = theFunction.getVariableTable();
boolean needsActivation = theFunction.requiresActivation() ||
cx.isGeneratingDebug();
generateICodeFromTree(theFunction.getLastChild(),
varTable, needsActivation,
securityDomain);
itsData.itsName = theFunction.getFunctionName();
itsData.itsSourceFile = (String) theFunction.getProp(
Node.SOURCENAME_PROP);
itsData.itsSource = (String)theFunction.getProp(Node.SOURCE_PROP);
itsData.itsNestedFunctions = itsNestedFunctions;
itsData.itsRegExpLiterals = regExpLiterals;
if (printICode) dumpICode(itsData);
InterpretedFunction result = new InterpretedFunction(itsData, cx);
if (cx.debugger != null)
cx.debugger.handleCompilationDone(cx, result, null);
return result;
}
boolean itsInFunctionFlag;
Vector itsFunctionList;
InterpreterData itsData;
int itsTryDepth = 0;
int itsStackDepth = 0;
int itsEpilogLabel = -1;
String itsSourceFile;
int itsLineNumber = 0;
InterpretedFunction[] itsNestedFunctions = null;
private int updateLineNumber(Node node, int iCodeTop)
{
Object datum = node.getDatum();
if (datum == null || !(datum instanceof Number))
return iCodeTop;
short lineNumber = ((Number) datum).shortValue();
if (lineNumber != itsLineNumber) {
itsLineNumber = lineNumber;
if (itsData.itsLineNumberTable == null &&
Context.getCurrentContext().isGeneratingDebug())
{
itsData.itsLineNumberTable = new java.util.Hashtable();
}
if (itsData.itsLineNumberTable != null) {
itsData.itsLineNumberTable.put(new Integer(lineNumber),
new Integer(iCodeTop));
}
iCodeTop = addByte((byte) TokenStream.LINE, iCodeTop);
iCodeTop = addByte((byte)(lineNumber >> 8), iCodeTop);
iCodeTop = addByte((byte)(lineNumber & 0xff), iCodeTop);
}
return iCodeTop;
}
private void badTree(Node node)
{
try {
out = new PrintWriter(new FileOutputStream("icode.txt", true));
out.println("Un-handled node : " + node.toString());
out.close();
}
catch (IOException x) {}
throw new RuntimeException("Un-handled node : "
+ node.toString());
}
private int generateICode(Node node, int iCodeTop)
{
int type = node.getType();
Node child = node.getFirstChild();
Node firstChild = child;
switch (type) {
case TokenStream.FUNCTION : {
iCodeTop = addByte((byte) TokenStream.CLOSURE, iCodeTop);
Node fn = (Node) node.getProp(Node.FUNCTION_PROP);
Short index = (Short) fn.getProp(Node.FUNCTION_PROP);
iCodeTop = addByte((byte)(index.shortValue() >> 8), iCodeTop);
iCodeTop = addByte((byte)(index.shortValue() & 0xff), iCodeTop);
itsStackDepth++;
if (itsStackDepth > itsData.itsMaxStack)
itsData.itsMaxStack = itsStackDepth;
}
break;
case TokenStream.SCRIPT :
iCodeTop = updateLineNumber(node, iCodeTop);
while (child != null) {
if (child.getType() != TokenStream.FUNCTION)
iCodeTop = generateICode(child, iCodeTop);
child = child.getNextSibling();
}
break;
case TokenStream.CASE :
iCodeTop = updateLineNumber(node, iCodeTop);
child = child.getNextSibling();
while (child != null) {
iCodeTop = generateICode(child, iCodeTop);
child = child.getNextSibling();
}
break;
case TokenStream.LABEL :
case TokenStream.WITH :
case TokenStream.LOOP :
case TokenStream.DEFAULT :
case TokenStream.BLOCK :
case TokenStream.VOID :
case TokenStream.NOP :
iCodeTop = updateLineNumber(node, iCodeTop);
while (child != null) {
iCodeTop = generateICode(child, iCodeTop);
child = child.getNextSibling();
}
break;
case TokenStream.COMMA :
iCodeTop = generateICode(child, iCodeTop);
iCodeTop = addByte((byte) TokenStream.POP, iCodeTop);
itsStackDepth--;
child = child.getNextSibling();
iCodeTop = generateICode(child, iCodeTop);
break;
case TokenStream.SWITCH : {
iCodeTop = updateLineNumber(node, iCodeTop);
iCodeTop = generateICode(child, iCodeTop);
int theLocalSlot = itsData.itsMaxLocals++;
iCodeTop = addByte((byte) TokenStream.NEWTEMP, iCodeTop);
iCodeTop = addByte((byte)theLocalSlot, iCodeTop);
iCodeTop = addByte((byte) TokenStream.POP, iCodeTop);
itsStackDepth--;
/*
reminder - below we construct new GOTO nodes that aren't
linked into the tree just for the purpose of having a node
to pass to the addGoto routine. (Parallels codegen here).
Seems unnecessary.
*/
Vector cases = (Vector) node.getProp(Node.CASES_PROP);
for (int i = 0; i < cases.size(); i++) {
Node thisCase = (Node)cases.elementAt(i);
Node first = thisCase.getFirstChild();
// the case expression is the firstmost child
// the rest will be generated when the case
// statements are encountered as siblings of
// the switch statement.
iCodeTop = generateICode(first, iCodeTop);
iCodeTop = addByte((byte) TokenStream.USETEMP, iCodeTop);
itsStackDepth++;
if (itsStackDepth > itsData.itsMaxStack)
itsData.itsMaxStack = itsStackDepth;
iCodeTop = addByte((byte) theLocalSlot, iCodeTop);
iCodeTop = addByte((byte) TokenStream.SHEQ, iCodeTop);
Node target = new Node(TokenStream.TARGET);
thisCase.addChildAfter(target, first);
Node branch = new Node(TokenStream.IFEQ);
branch.putProp(Node.TARGET_PROP, target);
iCodeTop = addGoto(branch, TokenStream.IFEQ,
iCodeTop);
itsStackDepth--;
}
Node defaultNode = (Node) node.getProp(Node.DEFAULT_PROP);
if (defaultNode != null) {
Node defaultTarget = new Node(TokenStream.TARGET);
defaultNode.getFirstChild().addChildToFront(defaultTarget);
Node branch = new Node(TokenStream.GOTO);
branch.putProp(Node.TARGET_PROP, defaultTarget);
iCodeTop = addGoto(branch, TokenStream.GOTO,
iCodeTop);
}
Node breakTarget = (Node) node.getProp(Node.BREAK_PROP);
Node branch = new Node(TokenStream.GOTO);
branch.putProp(Node.TARGET_PROP, breakTarget);
iCodeTop = addGoto(branch, TokenStream.GOTO,
iCodeTop);
}
break;
case TokenStream.TARGET : {
Object lblObect = node.getProp(Node.LABEL_PROP);
if (lblObect == null) {
int label = markLabel(acquireLabel(), iCodeTop);
node.putProp(Node.LABEL_PROP, new Integer(label));
}
else {
int label = ((Integer)lblObect).intValue();
markLabel(label, iCodeTop);
}
// if this target has a FINALLY_PROP, it is a JSR target
// and so has a PC value on the top of the stack
if (node.getProp(Node.FINALLY_PROP) != null) {
itsStackDepth = 1;
if (itsStackDepth > itsData.itsMaxStack)
itsData.itsMaxStack = itsStackDepth;
}
}
break;
case TokenStream.EQOP :
case TokenStream.RELOP : {
iCodeTop = generateICode(child, iCodeTop);
child = child.getNextSibling();
iCodeTop = generateICode(child, iCodeTop);
int op = node.getInt();
if (version == Context.VERSION_1_2) {
if (op == TokenStream.EQ)
op = TokenStream.SHEQ;
else if (op == TokenStream.NE)
op = TokenStream.SHNE;
}
iCodeTop = addByte((byte) op, iCodeTop);
itsStackDepth--;
}
break;
case TokenStream.NEW :
case TokenStream.CALL : {
if (itsSourceFile != null && (itsData.itsSourceFile == null || ! itsSourceFile.equals(itsData.itsSourceFile)))
itsData.itsSourceFile = itsSourceFile;
iCodeTop = addByte((byte)TokenStream.SOURCEFILE, iCodeTop);
int childCount = 0;
short nameIndex = -1;
while (child != null) {
iCodeTop = generateICode(child, iCodeTop);
if (nameIndex == -1) {
if (child.getType() == TokenStream.NAME)
nameIndex = (short)(itsData.itsStringTableIndex - 1);
else if (child.getType() == TokenStream.GETPROP)
nameIndex = (short)(itsData.itsStringTableIndex - 1);
}
child = child.getNextSibling();
childCount++;
}
if (node.getProp(Node.SPECIALCALL_PROP) != null) {
// embed line number and source filename
iCodeTop = addByte((byte) TokenStream.CALLSPECIAL, iCodeTop);
iCodeTop = addByte((byte)(itsLineNumber >> 8), iCodeTop);
iCodeTop = addByte((byte)(itsLineNumber & 0xff), iCodeTop);
iCodeTop = addString(itsSourceFile, iCodeTop);
}
else {
iCodeTop = addByte((byte) type, iCodeTop);
iCodeTop = addByte((byte)(nameIndex >> 8), iCodeTop);
iCodeTop = addByte((byte)(nameIndex & 0xFF), iCodeTop);
}
itsStackDepth -= (childCount - 1); // always a result value
// subtract from child count to account for [thisObj &] fun
if (type == TokenStream.NEW)
childCount -= 1;
else
childCount -= 2;
iCodeTop = addByte((byte)(childCount >> 8), iCodeTop);
iCodeTop = addByte((byte)(childCount & 0xff), iCodeTop);
if (childCount > itsData.itsMaxArgs)
itsData.itsMaxArgs = childCount;
iCodeTop = addByte((byte)TokenStream.SOURCEFILE, iCodeTop);
}
break;
case TokenStream.NEWLOCAL :
case TokenStream.NEWTEMP : {
iCodeTop = generateICode(child, iCodeTop);
iCodeTop = addByte((byte) TokenStream.NEWTEMP, iCodeTop);
iCodeTop = addLocalRef(node, iCodeTop);
}
break;
case TokenStream.USELOCAL : {
if (node.getProp(Node.TARGET_PROP) != null)
iCodeTop = addByte((byte) TokenStream.RETSUB, iCodeTop);
else {
iCodeTop = addByte((byte) TokenStream.USETEMP, iCodeTop);
itsStackDepth++;
if (itsStackDepth > itsData.itsMaxStack)
itsData.itsMaxStack = itsStackDepth;
}
Node temp = (Node) node.getProp(Node.LOCAL_PROP);
iCodeTop = addLocalRef(temp, iCodeTop);
}
break;
case TokenStream.USETEMP : {
iCodeTop = addByte((byte) TokenStream.USETEMP, iCodeTop);
Node temp = (Node) node.getProp(Node.TEMP_PROP);
iCodeTop = addLocalRef(temp, iCodeTop);
itsStackDepth++;
if (itsStackDepth > itsData.itsMaxStack)
itsData.itsMaxStack = itsStackDepth;
}
break;
case TokenStream.IFEQ :
case TokenStream.IFNE :
iCodeTop = generateICode(child, iCodeTop);
itsStackDepth--; // after the conditional GOTO, really
// fall thru...
case TokenStream.GOTO :
iCodeTop = addGoto(node, (byte) type, iCodeTop);
break;
case TokenStream.JSR : {
/*
mark the target with a FINALLY_PROP to indicate
that it will have an incoming PC value on the top
of the stack.
!!!
This only works if the target follows the JSR
in the tree.
!!!
*/
Node target = (Node)(node.getProp(Node.TARGET_PROP));
target.putProp(Node.FINALLY_PROP, node);
iCodeTop = addGoto(node, TokenStream.GOSUB,
iCodeTop);
}
break;
case TokenStream.AND : {
iCodeTop = generateICode(child, iCodeTop);
iCodeTop = addByte((byte) TokenStream.DUP, iCodeTop);
itsStackDepth++;
if (itsStackDepth > itsData.itsMaxStack)
itsData.itsMaxStack = itsStackDepth;
int falseTarget = acquireLabel();
iCodeTop = addGoto(falseTarget, TokenStream.IFNE,
iCodeTop);
iCodeTop = addByte((byte) TokenStream.POP, iCodeTop);
itsStackDepth--;
child = child.getNextSibling();
iCodeTop = generateICode(child, iCodeTop);
markLabel(falseTarget, iCodeTop);
}
break;
case TokenStream.OR : {
iCodeTop = generateICode(child, iCodeTop);
iCodeTop = addByte((byte) TokenStream.DUP, iCodeTop);
itsStackDepth++;
if (itsStackDepth > itsData.itsMaxStack)
itsData.itsMaxStack = itsStackDepth;
int trueTarget = acquireLabel();
iCodeTop = addGoto(trueTarget, TokenStream.IFEQ,
iCodeTop);
iCodeTop = addByte((byte) TokenStream.POP, iCodeTop);
itsStackDepth--;
child = child.getNextSibling();
iCodeTop = generateICode(child, iCodeTop);
markLabel(trueTarget, iCodeTop);
}
break;
case TokenStream.GETPROP : {
iCodeTop = generateICode(child, iCodeTop);
String s = (String) node.getProp(Node.SPECIAL_PROP_PROP);
if (s != null) {
if (s.equals("__proto__"))
iCodeTop = addByte((byte) TokenStream.GETPROTO, iCodeTop);
else
if (s.equals("__parent__"))
iCodeTop = addByte((byte) TokenStream.GETSCOPEPARENT, iCodeTop);
else
badTree(node);
}
else {
child = child.getNextSibling();
iCodeTop = generateICode(child, iCodeTop);
iCodeTop = addByte((byte) TokenStream.GETPROP, iCodeTop);
itsStackDepth--;
}
}
break;
case TokenStream.DELPROP :
case TokenStream.BITAND :
case TokenStream.BITOR :
case TokenStream.BITXOR :
case TokenStream.LSH :
case TokenStream.RSH :
case TokenStream.URSH :
case TokenStream.ADD :
case TokenStream.SUB :
case TokenStream.MOD :
case TokenStream.DIV :
case TokenStream.MUL :
case TokenStream.GETELEM :
iCodeTop = generateICode(child, iCodeTop);
child = child.getNextSibling();
iCodeTop = generateICode(child, iCodeTop);
iCodeTop = addByte((byte) type, iCodeTop);
itsStackDepth--;
break;
case TokenStream.CONVERT : {
iCodeTop = generateICode(child, iCodeTop);
Object toType = node.getProp(Node.TYPE_PROP);
if (toType == ScriptRuntime.NumberClass)
iCodeTop = addByte((byte) TokenStream.POS, iCodeTop);
else
badTree(node);
}
break;
case TokenStream.UNARYOP :
iCodeTop = generateICode(child, iCodeTop);
switch (node.getInt()) {
case TokenStream.VOID :
iCodeTop = addByte((byte) TokenStream.POP, iCodeTop);
iCodeTop = addByte((byte) TokenStream.UNDEFINED, iCodeTop);
break;
case TokenStream.NOT : {
int trueTarget = acquireLabel();
int beyond = acquireLabel();
iCodeTop = addGoto(trueTarget, TokenStream.IFEQ,
iCodeTop);
iCodeTop = addByte((byte) TokenStream.TRUE, iCodeTop);
iCodeTop = addGoto(beyond, TokenStream.GOTO,
iCodeTop);
markLabel(trueTarget, iCodeTop);
iCodeTop = addByte((byte) TokenStream.FALSE, iCodeTop);
markLabel(beyond, iCodeTop);
}
break;
case TokenStream.BITNOT :
iCodeTop = addByte((byte) TokenStream.BITNOT, iCodeTop);
break;
case TokenStream.TYPEOF :
iCodeTop = addByte((byte) TokenStream.TYPEOF, iCodeTop);
break;
case TokenStream.SUB :
iCodeTop = addByte((byte) TokenStream.NEG, iCodeTop);
break;
case TokenStream.ADD :
iCodeTop = addByte((byte) TokenStream.POS, iCodeTop);
break;
default:
badTree(node);
break;
}
break;
case TokenStream.SETPROP : {
iCodeTop = generateICode(child, iCodeTop);
child = child.getNextSibling();
iCodeTop = generateICode(child, iCodeTop);
String s = (String) node.getProp(Node.SPECIAL_PROP_PROP);
if (s != null) {
if (s.equals("__proto__"))
iCodeTop = addByte((byte) TokenStream.SETPROTO, iCodeTop);
else
if (s.equals("__parent__"))
iCodeTop = addByte((byte) TokenStream.SETPARENT, iCodeTop);
else
badTree(node);
}
else {
child = child.getNextSibling();
iCodeTop = generateICode(child, iCodeTop);
iCodeTop = addByte((byte) TokenStream.SETPROP, iCodeTop);
itsStackDepth -= 2;
}
}
break;
case TokenStream.SETELEM :
iCodeTop = generateICode(child, iCodeTop);
child = child.getNextSibling();
iCodeTop = generateICode(child, iCodeTop);
child = child.getNextSibling();
iCodeTop = generateICode(child, iCodeTop);
iCodeTop = addByte((byte) type, iCodeTop);
itsStackDepth -= 2;
break;
case TokenStream.SETNAME :
iCodeTop = generateICode(child, iCodeTop);
child = child.getNextSibling();
iCodeTop = generateICode(child, iCodeTop);
iCodeTop = addByte((byte) TokenStream.SETNAME, iCodeTop);
iCodeTop = addString(firstChild.getString(), iCodeTop);
itsStackDepth--;
break;
case TokenStream.TYPEOF : {
String name = node.getString();
int index = -1;
// use typeofname if an activation frame exists
// since the vars all exist there instead of in jregs
if (itsInFunctionFlag && !itsData.itsNeedsActivation)
index = itsData.itsVariableTable.getOrdinal(name);
if (index == -1) {
iCodeTop = addByte((byte) TokenStream.TYPEOFNAME, iCodeTop);
iCodeTop = addString(name, iCodeTop);
}
else {
iCodeTop = addByte((byte) TokenStream.GETVAR, iCodeTop);
iCodeTop = addByte((byte) index, iCodeTop);
iCodeTop = addByte((byte) TokenStream.TYPEOF, iCodeTop);
}
itsStackDepth++;
if (itsStackDepth > itsData.itsMaxStack)
itsData.itsMaxStack = itsStackDepth;
}
break;
case TokenStream.PARENT :
iCodeTop = generateICode(child, iCodeTop);
iCodeTop = addByte((byte) TokenStream.GETPARENT, iCodeTop);
break;
case TokenStream.GETBASE :
case TokenStream.BINDNAME :
case TokenStream.NAME :
case TokenStream.STRING :
iCodeTop = addByte((byte) type, iCodeTop);
iCodeTop = addString(node.getString(), iCodeTop);
itsStackDepth++;
if (itsStackDepth > itsData.itsMaxStack)
itsData.itsMaxStack = itsStackDepth;
break;
case TokenStream.INC :
case TokenStream.DEC : {
int childType = child.getType();
switch (childType) {
case TokenStream.GETVAR : {
String name = child.getString();
if (itsData.itsNeedsActivation) {
iCodeTop = addByte((byte) TokenStream.SCOPE, iCodeTop);
iCodeTop = addByte((byte) TokenStream.STRING, iCodeTop);
iCodeTop = addString(name, iCodeTop);
itsStackDepth += 2;
if (itsStackDepth > itsData.itsMaxStack)
itsData.itsMaxStack = itsStackDepth;
iCodeTop = addByte((byte)
(type == TokenStream.INC
? TokenStream.PROPINC
: TokenStream.PROPDEC),
iCodeTop);
itsStackDepth--;
}
else {
iCodeTop = addByte((byte)
(type == TokenStream.INC
? TokenStream.VARINC
: TokenStream.VARDEC),
iCodeTop);
int i = itsData.itsVariableTable.
getOrdinal(name);
iCodeTop = addByte((byte)i, iCodeTop);
itsStackDepth++;
if (itsStackDepth > itsData.itsMaxStack)
itsData.itsMaxStack = itsStackDepth;
}
}
break;
case TokenStream.GETPROP :
case TokenStream.GETELEM : {
Node getPropChild = child.getFirstChild();
iCodeTop = generateICode(getPropChild,
iCodeTop);
getPropChild = getPropChild.getNextSibling();
iCodeTop = generateICode(getPropChild,
iCodeTop);
if (childType == TokenStream.GETPROP)
iCodeTop = addByte((byte)
(type == TokenStream.INC
? TokenStream.PROPINC
: TokenStream.PROPDEC),
iCodeTop);
else
iCodeTop = addByte((byte)
(type == TokenStream.INC
? TokenStream.ELEMINC
: TokenStream.ELEMDEC),
iCodeTop);
itsStackDepth--;
}
break;
default : {
iCodeTop = addByte((byte)
(type == TokenStream.INC
? TokenStream.NAMEINC
: TokenStream.NAMEDEC),
iCodeTop);
iCodeTop = addString(child.getString(),
iCodeTop);
itsStackDepth++;
if (itsStackDepth > itsData.itsMaxStack)
itsData.itsMaxStack = itsStackDepth;
}
break;
}
}
break;
case TokenStream.NUMBER : {
Number num = (Number)node.getDatum();
if (num.doubleValue() == 0.0)
iCodeTop = addByte((byte) TokenStream.ZERO, iCodeTop);
else
if (num.doubleValue() == 1.0)
iCodeTop = addByte((byte) TokenStream.ONE, iCodeTop);
else {
iCodeTop = addByte((byte) TokenStream.NUMBER, iCodeTop);
iCodeTop = addNumber(num, iCodeTop);
}
}
itsStackDepth++;
if (itsStackDepth > itsData.itsMaxStack)
itsData.itsMaxStack = itsStackDepth;
break;
case TokenStream.POP :
case TokenStream.POPV :
iCodeTop = updateLineNumber(node, iCodeTop);
case TokenStream.ENTERWITH :
iCodeTop = generateICode(child, iCodeTop);
iCodeTop = addByte((byte) type, iCodeTop);
itsStackDepth--;
break;
case TokenStream.GETTHIS :
iCodeTop = generateICode(child, iCodeTop);
iCodeTop = addByte((byte) type, iCodeTop);
break;
case TokenStream.NEWSCOPE :
iCodeTop = addByte((byte) type, iCodeTop);
itsStackDepth++;
if (itsStackDepth > itsData.itsMaxStack)
itsData.itsMaxStack = itsStackDepth;
break;
case TokenStream.LEAVEWITH :
iCodeTop = addByte((byte) type, iCodeTop);
break;
case TokenStream.TRY : {
itsTryDepth++;
if (itsTryDepth > itsData.itsMaxTryDepth)
itsData.itsMaxTryDepth = itsTryDepth;
Node catchTarget = (Node)node.getProp(Node.TARGET_PROP);
Node finallyTarget = (Node)node.getProp(Node.FINALLY_PROP);
if (catchTarget == null) {
iCodeTop = addByte((byte) TokenStream.TRY, iCodeTop);
iCodeTop = addByte((byte)0, iCodeTop);
iCodeTop = addByte((byte)0, iCodeTop);
}
else
iCodeTop =
addGoto(node, TokenStream.TRY, iCodeTop);
int finallyHandler = 0;
if (finallyTarget != null) {
finallyHandler = acquireLabel();
int theLabel = finallyHandler & 0x7FFFFFFF;
itsLabelTable[theLabel].addFixup(iCodeTop);
}
iCodeTop = addByte((byte)0, iCodeTop);
iCodeTop = addByte((byte)0, iCodeTop);
Node lastChild = null;
/*
when we encounter the child of the catchTarget, we
set the stackDepth to 1 to account for the incoming
exception object.
*/
while (child != null) {
if (lastChild == catchTarget) {
itsStackDepth = 1;
if (itsStackDepth > itsData.itsMaxStack)
itsData.itsMaxStack = itsStackDepth;
}
/*
When the following child is the catchTarget,
the current child is the goto at the end of
the try statemets, we need to emit the endtry
before that goto.
*/
if (child.getNextSibling() == catchTarget)
iCodeTop = addByte((byte) TokenStream.ENDTRY,
iCodeTop);
iCodeTop = generateICode(child, iCodeTop);
lastChild = child;
child = child.getNextSibling();
}
itsStackDepth = 0;
if (finallyTarget != null) {
// normal flow goes around the finally handler stublet
int skippy = acquireLabel();
iCodeTop =
addGoto(skippy, TokenStream.GOTO, iCodeTop);
// on entry the stack will have the exception object
markLabel(finallyHandler, iCodeTop);
itsStackDepth = 1;
if (itsStackDepth > itsData.itsMaxStack)
itsData.itsMaxStack = itsStackDepth;
int theLocalSlot = itsData.itsMaxLocals++;
iCodeTop = addByte((byte) TokenStream.NEWTEMP, iCodeTop);
iCodeTop = addByte((byte)theLocalSlot, iCodeTop);
iCodeTop = addByte((byte) TokenStream.POP, iCodeTop);
Integer finallyLabel
= (Integer)(finallyTarget.getProp(Node.LABEL_PROP));
iCodeTop = addGoto(finallyLabel.intValue(),
TokenStream.GOSUB, iCodeTop);
iCodeTop = addByte((byte) TokenStream.USETEMP, iCodeTop);
iCodeTop = addByte((byte)theLocalSlot, iCodeTop);
iCodeTop = addByte((byte) TokenStream.JTHROW, iCodeTop);
itsStackDepth = 0;
markLabel(skippy, iCodeTop);
}
itsTryDepth--;
}
break;
case TokenStream.THROW :
iCodeTop = updateLineNumber(node, iCodeTop);
iCodeTop = generateICode(child, iCodeTop);
iCodeTop = addByte((byte) TokenStream.THROW, iCodeTop);
itsStackDepth--;
break;
case TokenStream.RETURN :
iCodeTop = updateLineNumber(node, iCodeTop);
if (child != null)
iCodeTop = generateICode(child, iCodeTop);
else {
iCodeTop = addByte((byte) TokenStream.UNDEFINED, iCodeTop);
itsStackDepth++;
if (itsStackDepth > itsData.itsMaxStack)
itsData.itsMaxStack = itsStackDepth;
}
iCodeTop = addGoto(node, TokenStream.RETURN, iCodeTop);
itsStackDepth--;
break;
case TokenStream.GETVAR : {
String name = node.getString();
if (itsData.itsNeedsActivation) {
// SETVAR handled this by turning into a SETPROP, but
// we can't do that to a GETVAR without manufacturing
// bogus children. Instead we use a special op to
// push the current scope.
iCodeTop = addByte((byte) TokenStream.SCOPE, iCodeTop);
iCodeTop = addByte((byte) TokenStream.STRING, iCodeTop);
iCodeTop = addString(name, iCodeTop);
itsStackDepth += 2;
if (itsStackDepth > itsData.itsMaxStack)
itsData.itsMaxStack = itsStackDepth;
iCodeTop = addByte((byte) TokenStream.GETPROP, iCodeTop);
itsStackDepth--;
}
else {
int index = itsData.itsVariableTable.getOrdinal(name);
iCodeTop = addByte((byte) TokenStream.GETVAR, iCodeTop);
iCodeTop = addByte((byte)index, iCodeTop);
itsStackDepth++;
if (itsStackDepth > itsData.itsMaxStack)
itsData.itsMaxStack = itsStackDepth;
}
}
break;
case TokenStream.SETVAR : {
if (itsData.itsNeedsActivation) {
child.setType(TokenStream.BINDNAME);
node.setType(TokenStream.SETNAME);
iCodeTop = generateICode(node, iCodeTop);
}
else {
String name = child.getString();
child = child.getNextSibling();
iCodeTop = generateICode(child, iCodeTop);
int index = itsData.itsVariableTable.getOrdinal(name);
iCodeTop = addByte((byte) TokenStream.SETVAR, iCodeTop);
iCodeTop = addByte((byte)index, iCodeTop);
}
}
break;
case TokenStream.PRIMARY:
iCodeTop = addByte((byte) node.getInt(), iCodeTop);
itsStackDepth++;
if (itsStackDepth > itsData.itsMaxStack)
itsData.itsMaxStack = itsStackDepth;
break;
case TokenStream.ENUMINIT :
iCodeTop = generateICode(child, iCodeTop);
iCodeTop = addByte((byte) TokenStream.ENUMINIT, iCodeTop);
iCodeTop = addLocalRef(node, iCodeTop);
itsStackDepth--;
break;
case TokenStream.ENUMNEXT : {
iCodeTop = addByte((byte) TokenStream.ENUMNEXT, iCodeTop);
Node init = (Node)node.getProp(Node.ENUM_PROP);
iCodeTop = addLocalRef(init, iCodeTop);
itsStackDepth++;
if (itsStackDepth > itsData.itsMaxStack)
itsData.itsMaxStack = itsStackDepth;
}
break;
case TokenStream.ENUMDONE :
// could release the local here??
break;
case TokenStream.OBJECT : {
Node regexp = (Node) node.getProp(Node.REGEXP_PROP);
int index = ((Integer)(regexp.getProp(
Node.REGEXP_PROP))).intValue();
iCodeTop = addByte((byte) TokenStream.OBJECT, iCodeTop);
iCodeTop = addByte((byte)(index >> 8), iCodeTop);
iCodeTop = addByte((byte)(index & 0xff), iCodeTop);
itsStackDepth++;
if (itsStackDepth > itsData.itsMaxStack)
itsData.itsMaxStack = itsStackDepth;
}
break;
default :