-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmallObject.java
1242 lines (1097 loc) · 47.2 KB
/
SmallObject.java
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
/*
Little Smalltalk Interpreter written in Java
Written by Tim Budd, [email protected]
Version 0.8 (November 2002)
*/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.JTextComponent;
class SmallObject implements Serializable {
public SmallObject objClass;
public SmallObject [ ] data;
public SmallObject () { objClass = null; data = null; }
public SmallObject (SmallObject cl, int size)
{ objClass = cl; data = new SmallObject [size]; }
public SmallObject copy(SmallObject cl) { return this; }
}
class SmallInt extends SmallObject {
public int value;
public SmallInt (SmallObject IntegerClass, int v)
{ super (IntegerClass, 0); value = v; }
public String toString () { return "SmallInt: "+ value; }
}
class SmallByteArray extends SmallObject {
public byte [ ] values;
public SmallByteArray (SmallObject cl, int size)
{ super(cl, 0); values = new byte[size]; }
public SmallByteArray (SmallObject cl, String text) {
super(cl, 0);
int size = text.length();
values = new byte[size];
for (int i = 0; i < size; i++)
values[i] = (byte) text.charAt(i);
}
public String toString () {
// we assume its a string, tho not always true...
return new String(values);
}
public SmallObject copy (SmallObject cl) {
SmallByteArray newObj = new SmallByteArray(cl, values.length);
for (int i = 0; i < values.length; i++) {
newObj.values[i] = values[i];
}
return newObj;
}
}
class SmallJavaObject extends SmallObject {
public SmallJavaObject(SmallObject cls, Object v)
{ super(cls, 0); value = v; }
public Object value;
}
class SmallException extends Exception {
SmallException (String gripe, SmallObject c)
{ super(gripe); context = c;}
public SmallObject context;
}
class Sema {
public synchronized SmallObject get() { if (! hasBeenSet) try {
wait();
} catch(Exception e) { System.out.println("Sema got exception " + e); }
return value; }
public synchronized void set(SmallObject v)
{ value = v; hasBeenSet = true; notifyAll(); }
private SmallObject value;
private boolean hasBeenSet = false;
}
class SmallInterpreter implements Serializable {
// global constants
public SmallObject nilObject;
public SmallObject trueObject;
public SmallObject falseObject;
public SmallInt [ ] smallInts;
public SmallObject ArrayClass;
public SmallObject BlockClass;
public SmallObject ContextClass;
public SmallObject IntegerClass;
// create a new small integer
SmallInt newInteger (int val) {
if ((val >= 0) && (val < 10))
return smallInts[val];
else
return new SmallInt (IntegerClass, val);
}
private SmallObject methodLookup (SmallObject receiver,
SmallByteArray messageSelector, SmallObject context,
SmallObject arguments)
throws SmallException {
String name = messageSelector.toString();
SmallObject cls;
for (cls = receiver; cls != nilObject; cls = cls.data[1]) {
SmallObject dict = cls.data[2]; // dictionary in class
for (int i = 0; i < dict.data.length; i++) {
SmallObject aMethod = dict.data[i];
if (name.equals(aMethod.data[0].toString())) {
return aMethod;
}
}
}
// try once to handle method in Smalltalk before giving up
if (name.equals("error:"))
throw new SmallException("Unrecognized message selector: " +
messageSelector, context);
SmallObject[] newArgs = new SmallObject[2];
newArgs[0] = arguments.data[0]; // same receiver
newArgs[1] = new SmallByteArray(messageSelector.objClass,
"Unrecognized message selector: " + name);
arguments.data = newArgs;
return methodLookup(receiver,
new SmallByteArray(messageSelector.objClass, "error:"),
context, arguments);
}
SmallObject buildContext
(SmallObject oldContext, SmallObject arguments, SmallObject method) {
SmallObject context = new SmallObject(ContextClass, 7);
context.data[0] = method;
context.data[1] = arguments;
// allocate temporaries
int max = ((SmallInt) (method.data[4])).value;
if (max > 0) {
context.data[2] = new SmallObject(ArrayClass, max);
while (max > 0) // iniailize to nil
context.data[2].data[--max] = nilObject;
}
// allocate stack
max = ((SmallInt) (method.data[3])).value;
context.data[3] = new SmallObject(ArrayClass, max);
context.data[4] = smallInts[0]; // byte pointer
context.data[5] = smallInts[0]; // stacktop
context.data[6] = oldContext;
return context;
}
// execution method
SmallObject execute (SmallObject context,
final Thread myThread, final Thread parentThread) throws SmallException {
SmallObject [ ] selectorCache = new SmallObject[197];
SmallObject [ ] classCache = new SmallObject[197];
SmallObject [ ] methodCache = new SmallObject[197];
int lookup = 0;
int cached = 0;
SmallObject [ ] contextData = context.data;
outerLoop:
while (true) {
SmallObject method = contextData[0]; // method in context
byte [ ] code = ((SmallByteArray) method.data[1]).values; // code pointer
int bytePointer = ((SmallInt) contextData[4]).value;
SmallObject [ ] stack = contextData[3].data;
int stackTop = ((SmallInt) contextData[5]).value;
SmallObject returnedValue = null;
SmallObject temp;
SmallObject [ ] tempa;
// everything else can be null for now
SmallObject [ ] temporaries = null;
SmallObject [ ] instanceVariables = null;
SmallObject arguments = null;
SmallObject [ ] literals = null;
innerLoop:
while (true) {
int high = code[bytePointer++];
int low = high & 0x0F;
high = (high >>= 4) & 0x0F;
if (high == 0) {
high = low;
// convert to positive int
low = (int) code[bytePointer++] & 0x0FF;
}
switch (high) {
case 1: // PushInstance
if (arguments == null)
arguments = contextData[1];
if (instanceVariables == null)
instanceVariables = arguments.data[0].data;
stack[stackTop++] = instanceVariables[low];
break;
case 2: // PushArgument
if (arguments == null)
arguments = contextData[1];
stack[stackTop++] = arguments.data[low];
break;
case 3: // PushTemporary
if (temporaries == null)
temporaries = contextData[2].data;
stack[stackTop++] = temporaries[low];
break;
case 4: // PushLiteral
if (literals == null)
literals = method.data[2].data;
stack[stackTop++] = literals[low];
break;
case 5: // PushConstant
switch(low) {
case 0: case 1: case 2: case 3: case 4:
case 5: case 6: case 7: case 8: case 9:
stack[stackTop++] = smallInts[low]; break;
case 10:
stack[stackTop++] = nilObject; break;
case 11:
stack[stackTop++] = trueObject; break;
case 12:
stack[stackTop++] = falseObject; break;
default:
throw new SmallException("Unknown constant " + low, context);
}
break;
case 12: // PushBlock
// low is argument location
// next byte is goto value
high = (int) code[bytePointer++] & 0x0FF;
returnedValue = new SmallObject(BlockClass, 10);
tempa = returnedValue.data;
tempa[0] = contextData[0]; // share method
tempa[1] = contextData[1]; // share arguments
tempa[2] = contextData[2]; // share temporaries
tempa[3] = contextData[3]; // stack (later replaced)
tempa[4] = newInteger(bytePointer); // current byte pointer
tempa[5] = smallInts[0]; // stacktop
tempa[6] = contextData[6]; // previous context
tempa[7] = newInteger(low); // argument location
tempa[8] = context; // creating context
tempa[9] = newInteger(bytePointer); // current byte pointer
stack[stackTop++] = returnedValue;
bytePointer = high;
break;
case 14: // PushClassVariable
if (arguments == null)
arguments = contextData[1];
if (instanceVariables == null)
instanceVariables = arguments.data[0].data;
stack[stackTop++] = arguments.data[0].objClass.data[low+5];
break;
case 6: // AssignInstance
if (arguments == null)
arguments = contextData[1];
if (instanceVariables == null)
instanceVariables = arguments.data[0].data;
// leave result on stack
instanceVariables[low] = stack[stackTop-1];
break;
case 7: // AssignTemporary
if (temporaries == null)
temporaries = contextData[2].data;
temporaries[low] = stack[stackTop-1];
break;
case 8: // MarkArguments
SmallObject newArguments = new SmallObject(ArrayClass, low);
tempa = newArguments.data; // direct access to array
while (low > 0)
tempa[--low] = stack[--stackTop];
stack[stackTop++] = newArguments;
break;
case 9: // SendMessage
// save old context
arguments = stack[--stackTop];
// expand newInteger in line
//contextData[5] = newInteger(stackTop);
contextData[5] = (stackTop < 10)?smallInts[stackTop]:new SmallInt(IntegerClass,stackTop);
//contextData[4] = newInteger(bytePointer);
contextData[4] = (bytePointer < 10)?smallInts[bytePointer]:new SmallInt(IntegerClass,bytePointer);
// now build new context
if (literals == null)
literals = method.data[2].data;
returnedValue = literals[low]; // message selector
//System.out.println("Sending " + returnedValue);
//System.out.println("Arguments " + arguments);
//System.out.println("Arguments receiver " + arguments.data[0]);
//System.out.println("Arguments class " + arguments.data[0].objClass);
high = (arguments.data[0].objClass.hashCode() + returnedValue.hashCode())
% 197;
if ((selectorCache[high] != null) &&
(selectorCache[high] == returnedValue) &&
(classCache[high] == arguments.data[0].objClass)) {
method = methodCache[high];
cached++;
}
else {
method = methodLookup(arguments.data[0].objClass, (SmallByteArray) literals[low], context, arguments);
lookup++;
selectorCache[high] = returnedValue;
classCache[high] = arguments.data[0].objClass;
methodCache[high] = method;
}
context = buildContext(context, arguments, method);
contextData = context.data;
// load information from context
continue outerLoop;
case 10: // SendUnary
if (low == 0) { // isNil
SmallObject arg = stack[--stackTop];
stack[stackTop++] = (arg == nilObject)?trueObject:falseObject;
}
else if (low == 1) { // notNil
SmallObject arg = stack[--stackTop];
stack[stackTop++] = (arg != nilObject)?trueObject:falseObject;
}
else
throw new SmallException("Illegal SendUnary " + low, context);
break;
case 11:{// SendBinary
if ((stack[stackTop-1] instanceof SmallInt) &&
(stack[stackTop-2] instanceof SmallInt)) {
int j = ((SmallInt) stack[--stackTop]).value;
int i = ((SmallInt) stack[--stackTop]).value;
boolean done = true;
switch (low) {
case 0: // <
returnedValue = (i < j)?trueObject:falseObject;
break;
case 1: // <=
returnedValue = (i <= j)?trueObject:falseObject;
break;
case 2: // +
long li = i + (long) j;
if (li != (i+j))
done = false; // overflow
returnedValue = newInteger(i+j);
break;
}
if (done) {
stack[stackTop++] = returnedValue;
break;
} else stackTop += 2; // overflow, send message
}
// non optimized binary message
arguments = new SmallObject(ArrayClass, 2);
arguments.data[1] = stack[--stackTop];
arguments.data[0] = stack[--stackTop];
contextData[5] = newInteger(stackTop);
contextData[4] = newInteger(bytePointer);
SmallByteArray msg = null;
switch(low) {
case 0: msg = new SmallByteArray(null,"<"); break;
case 1: msg = new SmallByteArray(null,"<="); break;
case 2: msg = new SmallByteArray(null,"+"); break;
}
method = methodLookup(arguments.data[0].objClass, msg, context, arguments);
context = buildContext(context, arguments, method);
contextData = context.data;
continue outerLoop;
}
case 13: // Do Primitive, low is arg count, next byte is number
high = (int) code[bytePointer++] & 0x0FF;
switch (high) {
case 1: // object identity
returnedValue = stack[--stackTop];
if (returnedValue == stack[--stackTop])
returnedValue = trueObject;
else returnedValue = falseObject;
break;
case 2: // object class
returnedValue = stack[--stackTop].objClass;
break;
case 4: // object size
returnedValue = stack[--stackTop];
if (returnedValue instanceof SmallByteArray)
low = ((SmallByteArray) returnedValue).values.length;
else
low = returnedValue.data.length;
returnedValue = newInteger(low);
break;
case 5: // object at put
low = ((SmallInt) stack[--stackTop]).value;
returnedValue = stack[--stackTop];
returnedValue.data[low - 1] = stack[--stackTop];
break;
case 6: // new context execute
returnedValue = execute(stack[--stackTop], myThread, parentThread);
break;
case 7: // new object allocation
low = ((SmallInt) stack[--stackTop]).value;
returnedValue = new SmallObject(stack[--stackTop], low);
while (low > 0)
returnedValue.data[--low] = nilObject;
break;
case 8: { // block invocation
returnedValue = stack[--stackTop]; // the block
high = ((SmallInt) returnedValue.data[7]).value; // arg location
low -= 2;
if (low >= 0) {
temporaries = returnedValue.data[2].data;
while (low >= 0) {
temporaries[high + low--] = stack[--stackTop];
}
}
contextData[5] = newInteger(stackTop);
contextData[4] = newInteger(bytePointer);
SmallObject newContext = new SmallObject(ContextClass, 10);
for (int i = 0; i < 10; i++)
newContext.data[i] = returnedValue.data[i];
newContext.data[6] = contextData[6];
newContext.data[5] = smallInts[0]; // stack top
newContext.data[4] = returnedValue.data[9]; // starting addr
low = newContext.data[3].data.length; //stack size
newContext.data[3] = new SmallObject(ArrayClass, low); // new stack
context = newContext;
contextData = context.data;
continue outerLoop;
}
case 9: // read a char from input
try {
returnedValue = newInteger(System.in.read());
} catch(IOException e)
{ returnedValue = nilObject; }
break;
case 10:{ // small integer addition need to handle ovflow
low = ((SmallInt) stack[--stackTop]).value;
high = ((SmallInt) stack[--stackTop]).value;
long lhigh = ((long) high) + (long) low;
high += low;
if (lhigh == high) returnedValue = newInteger(high);
else returnedValue = nilObject;
} break;
case 11: // small integer quotient
low = ((SmallInt) stack[--stackTop]).value;
high = ((SmallInt) stack[--stackTop]).value;
high /= low;
returnedValue = newInteger(high);
break;
case 12: // small integer remainder
low = ((SmallInt) stack[--stackTop]).value;
high = ((SmallInt) stack[--stackTop]).value;
high %= low;
returnedValue = newInteger(high);
break;
case 14: // small int equality
low = ((SmallInt) stack[--stackTop]).value;
high = ((SmallInt) stack[--stackTop]).value;
returnedValue = (low == high)?trueObject:falseObject;
break;
case 15:{ // small integer multiplication
low = ((SmallInt) stack[--stackTop]).value;
high = ((SmallInt) stack[--stackTop]).value;
long lhigh = ((long) high) * (long) low;
high *= low;
if (lhigh == high) returnedValue = newInteger(high);
else returnedValue = nilObject;
} break;
case 16:{ // small integer subtraction
low = ((SmallInt) stack[--stackTop]).value;
high = ((SmallInt) stack[--stackTop]).value;
long lhigh = ((long) high) - (long) low;
high -= low;
if (lhigh == high) returnedValue = newInteger(high);
else returnedValue = nilObject;
} break;
case 17: // small integer as string
low = ((SmallInt) stack[--stackTop]).value;
returnedValue = new SmallByteArray(stack[--stackTop], String.valueOf(low));
break;
case 18: // debugg -- dummy for now
returnedValue = stack[--stackTop];
System.out.println("Debug " + returnedValue + " class " + returnedValue.objClass.data[0]);
break;
case 19: // block fork
returnedValue = stack[--stackTop];
new ActionThread(returnedValue, myThread).start();
break;
case 20: // byte array allocation
low = ((SmallInt) stack[--stackTop]).value;
returnedValue = new SmallByteArray(stack[--stackTop], low);
break;
case 21: // string at
low = ((SmallInt) stack[--stackTop]).value;
returnedValue = stack[--stackTop];
SmallByteArray baa = (SmallByteArray) returnedValue;
low = (int) baa.values[low-1] & 0x0FF;
returnedValue = newInteger(low);
break;
case 22: // string at put
low = ((SmallInt) stack[--stackTop]).value;
SmallByteArray ba = (SmallByteArray) stack[--stackTop];
high = ((SmallInt) stack[--stackTop]).value;
ba.values[low-1] = (byte) high;
returnedValue = ba;
break;
case 23: // string copy
returnedValue = stack[--stackTop];
returnedValue = stack[--stackTop].copy(returnedValue);
break;
case 24:{ // string append
SmallByteArray a = (SmallByteArray) stack[--stackTop];
SmallByteArray b = (SmallByteArray) stack[--stackTop];
low = a.values.length + b.values.length;
SmallByteArray n = new SmallByteArray(a.objClass, low);
high = 0;
for (int i = 0; i < a.values.length; i++)
n.values[high++] = a.values[i];
for (int i = 0; i < b.values.length; i++)
n.values[high++] = b.values[i];
returnedValue = n;
} break;
case 26: { // string compare
SmallByteArray a = (SmallByteArray) stack[--stackTop];
SmallByteArray b = (SmallByteArray) stack[--stackTop];
low = a.values.length;
high = b.values.length;
int s = (low < high)?low:high;
int r = 0;
for (int i = 0; i < s; i++)
if (a.values[i] < b.values[i])
{ r = 1; break; }
else if (b.values[i] < a.values[i])
{ r = -1; break; }
if (r == 0)
if (low < high) r = 1;
else if (low > high) r = -1;
returnedValue = newInteger(r);
} break;
case 29: { // image save
SmallByteArray a = (SmallByteArray) stack[--stackTop];
String name = a.toString();
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(name));
//oos.writeObject(this);
// write one by one to avoid serialization
oos.writeObject(nilObject);
oos.writeObject(trueObject);
oos.writeObject(falseObject);
oos.writeObject(smallInts);
oos.writeObject(ArrayClass);
oos.writeObject(BlockClass);
oos.writeObject(ContextClass);
oos.writeObject(IntegerClass);
} catch (Exception e) {
throw new SmallException("got I/O Exception " + e, context);
}
returnedValue = a;
} break;
case 30: // array at
low = ((SmallInt) stack[--stackTop]).value;
returnedValue = stack[--stackTop];
returnedValue = returnedValue.data[low-1];
break;
case 31: {// array with: (add new item)
SmallObject oldar = stack[--stackTop];
low = oldar.data.length;
returnedValue = new SmallObject(oldar.objClass, low+1);
for (int i = 0; i < low; i++)
returnedValue.data[i] = oldar.data[i];
returnedValue.data[low] = stack[--stackTop];
} break;
case 32:{ // object add: increase object size
returnedValue = stack[--stackTop];
low = returnedValue.data.length;
SmallObject na[] = new SmallObject[low+1];
for (int i = 0; i < low; i++)
na[i] = returnedValue.data[i];
na[low] = stack[--stackTop];
returnedValue.data = na;
} break;
case 33: {// Sleep for a bit
low = ((SmallInt) stack[--stackTop]).value;
try { Thread.sleep(low); } catch (Exception a) { }
} break;
case 34: { // thread kill
if (parentThread != null) parentThread.stop();
if (myThread != null) myThread.stop();
System.out.println("is there life after death?");
} break;
case 35: // return current context
returnedValue = context;
break;
case 36: // fast array creation
returnedValue = new SmallObject(ArrayClass, low);
for (int i = low-1; i >= 0; i--)
returnedValue.data[i] = stack[--stackTop];
break;
case 41: {// open file for output
try {
FileOutputStream of = new FileOutputStream(stack[--stackTop].toString());
PrintStream ps = new PrintStream(of);
returnedValue = new SmallJavaObject(stack[--stackTop], ps);
} catch (IOException e) {
throw new SmallException("I/O exception " + e, context);
} } break;
case 42: {// open file for input
try {
FileInputStream of = new FileInputStream(stack[--stackTop].toString());
DataInput ps = new DataInputStream(of);
returnedValue = new SmallJavaObject(stack[--stackTop], ps);
} catch (IOException e) {
throw new SmallException("I/O exception " + e, context);
} } break;
case 43: {// write a string
try {
PrintStream ps = (PrintStream)
((SmallJavaObject) stack[--stackTop]).value;
ps.print(stack[--stackTop]);
} catch (Exception e) {
throw new SmallException("I/O exception " + e, context);
} } break;
case 44: { // read a string
try {
DataInput di = (DataInput)
((SmallJavaObject) stack[--stackTop]).value;
String line = di.readLine();
if (line == null) {
--stackTop; returnedValue = nilObject;
}
else
returnedValue = new SmallByteArray(stack[--stackTop], line);
} catch (EOFException e) {
returnedValue = nilObject;
} catch (IOException f) {
throw new SmallException("I/O exception " + f, context);
} } break;
case 50: // integer into float
low = ((SmallInt) stack[--stackTop]).value;
returnedValue = new SmallJavaObject(stack[--stackTop], new Double((double) low));
break;
case 51:{ // addition of float
double a = ((Double)((SmallJavaObject) stack[--stackTop]).value).doubleValue();
double b = ((Double)((SmallJavaObject) stack[--stackTop]).value).doubleValue();
returnedValue = new SmallJavaObject(stack[--stackTop], new Double(a+b));
} break;
case 52:{ // subtraction of float
double a = ((Double)((SmallJavaObject) stack[--stackTop]).value).doubleValue();
double b = ((Double)((SmallJavaObject) stack[--stackTop]).value).doubleValue();
returnedValue = new SmallJavaObject(stack[--stackTop], new Double(a-b));
} break;
case 53:{ // multiplication of float
double a = ((Double)((SmallJavaObject) stack[--stackTop]).value).doubleValue();
double b = ((Double)((SmallJavaObject) stack[--stackTop]).value).doubleValue();
returnedValue = new SmallJavaObject(stack[--stackTop], new Double(a*b));
} break;
case 54:{ // division of float
double a = ((Double)((SmallJavaObject) stack[--stackTop]).value).doubleValue();
double b = ((Double)((SmallJavaObject) stack[--stackTop]).value).doubleValue();
returnedValue = new SmallJavaObject(stack[--stackTop], new Double(a/b));
} break;
case 55:{ // less than test of float
double a = ((Double)((SmallJavaObject) stack[--stackTop]).value).doubleValue();
double b = ((Double)((SmallJavaObject) stack[--stackTop]).value).doubleValue();
returnedValue = (a<b)?trueObject:falseObject;
} break;
case 56:{ // equality test of float
double a = ((Double)((SmallJavaObject) stack[--stackTop]).value).doubleValue();
double b = ((Double)((SmallJavaObject) stack[--stackTop]).value).doubleValue();
returnedValue = (a==b)?trueObject:falseObject;
} break;
case 57:{ // float to int
double a = ((Double)((SmallJavaObject) stack[--stackTop]).value).doubleValue();
returnedValue = newInteger((int) a);
} break;
case 58: // random float
returnedValue = new SmallJavaObject(stack[--stackTop], new Double(Math.random()));
break;
case 59: // print of float
returnedValue = (SmallJavaObject) stack[--stackTop];
returnedValue = new SmallByteArray(stack[--stackTop],
String.valueOf(((Double) ((SmallJavaObject) returnedValue).value).doubleValue()));
break;
case 60:{ // make window
JDialog jd = new JDialog();
jd.setVisible(false);
returnedValue = new SmallJavaObject(stack[--stackTop], jd);
} break;
case 61:{ // show/hide text window
returnedValue = stack[--stackTop];
SmallJavaObject jo = (SmallJavaObject) stack[--stackTop];
if (returnedValue == trueObject)
((JDialog) jo.value).setVisible(true);
else
((JDialog) jo.value).setVisible(false);
} break;
case 62:{ // set content pane
SmallJavaObject tc = (SmallJavaObject) stack[--stackTop];
returnedValue = stack[--stackTop];
SmallJavaObject jd = (SmallJavaObject) returnedValue;
((JDialog) jd.value).getContentPane().add((Component) tc.value);
} break;
case 63: // set size
low = ((SmallInt) stack[--stackTop]).value;
high = ((SmallInt) stack[--stackTop]).value;
returnedValue = stack[--stackTop];
{
SmallJavaObject wo = (SmallJavaObject) returnedValue;
((JDialog) wo.value).setSize(low, high);
} break;
case 64:{ // add menu to window
SmallJavaObject menu = (SmallJavaObject) stack[--stackTop];
returnedValue = stack[--stackTop];
SmallJavaObject jo = (SmallJavaObject) returnedValue;
JDialog jd = (JDialog) jo.value;
if (jd.getJMenuBar() == null) jd.setJMenuBar(new JMenuBar());
jd.getJMenuBar().add((JMenu) menu.value);
} break;
case 65:{ // set title
SmallObject title = stack[--stackTop];
returnedValue = stack[--stackTop];
SmallJavaObject jd = (SmallJavaObject) returnedValue;
((JDialog) jd.value).setTitle(title.toString());
} break;
case 66: { // repaint window
returnedValue = stack[--stackTop];
SmallJavaObject jd = (SmallJavaObject) returnedValue;
((JDialog) jd.value).repaint();
} break;
case 70:{ // new label panel
JLabel jl = new JLabel(stack[--stackTop].toString());
returnedValue = new SmallJavaObject(stack[--stackTop], new JScrollPane(jl));
} break;
case 71:{ // new button
final SmallObject action = stack[--stackTop];
final JButton jb = new JButton(stack[--stackTop].toString());
returnedValue = new SmallJavaObject(stack[--stackTop], jb);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new ActionThread(action, myThread).start();
}});
} break;
case 72: // new text line
returnedValue = new SmallJavaObject(stack[--stackTop], new JTextField());
break;
case 73: // new text area
returnedValue = new SmallJavaObject(stack[--stackTop], new JScrollPane(new JTextArea()));
break;
case 74:{ // new grid panel
SmallObject data = stack[--stackTop];
low = ((SmallInt) stack[--stackTop]).value;
high = ((SmallInt) stack[--stackTop]).value;
JPanel jp = new JPanel();
jp.setLayout(new GridLayout(low, high));
for (int i = 0; i < data.data.length; i++)
jp.add((Component)((SmallJavaObject)data.data[i]).value);
returnedValue = new SmallJavaObject(stack[--stackTop], jp);
}
break;
case 75:{ // new list panel
final SmallObject action = stack[--stackTop];
SmallObject data = stack[--stackTop];
returnedValue = stack[--stackTop];
final JList jl = new JList(data.data);
jl.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
returnedValue = new SmallJavaObject(returnedValue, new JScrollPane(jl));
jl.addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if ((!e.getValueIsAdjusting()) && (jl.getSelectedIndex() >= 0)) {
new ActionThread(action, myThread, jl.getSelectedIndex()+1).start();
//new ActionThread(action, myThread).start();
}}});
} break;
case 76:{ // new border panel
JPanel jp = new JPanel();
jp.setLayout(new BorderLayout());
returnedValue = stack[--stackTop];
if (returnedValue != nilObject)
jp.add("Center", (Component) ((SmallJavaObject) returnedValue).value);
returnedValue = stack[--stackTop];
if (returnedValue != nilObject)
jp.add("West", (Component) ((SmallJavaObject) returnedValue).value);
returnedValue = stack[--stackTop];
if (returnedValue != nilObject)
jp.add("East", (Component) ((SmallJavaObject) returnedValue).value);
returnedValue = stack[--stackTop];
if (returnedValue != nilObject)
jp.add("South", (Component) ((SmallJavaObject) returnedValue).value);
returnedValue = stack[--stackTop];
if (returnedValue != nilObject)
jp.add("North", (Component) ((SmallJavaObject) returnedValue).value);
returnedValue = new SmallJavaObject(stack[--stackTop], jp);
} break;
case 77:{ // set image on label
SmallJavaObject img = (SmallJavaObject) stack[--stackTop];
SmallJavaObject lab = (SmallJavaObject) stack[--stackTop];
Object jo = lab.value;
if (jo instanceof JScrollPane) jo = ((JScrollPane) jo).getViewport().getView();
if (jo instanceof JLabel) {
JLabel jlb = (JLabel) jo;
jlb.setIcon(new ImageIcon((Image) img.value));
jlb.setHorizontalAlignment(SwingConstants.LEFT);
jlb.setVerticalAlignment(SwingConstants.TOP);
jlb.repaint();
}} break;
case 79: {// repaint
returnedValue = stack[--stackTop];
SmallJavaObject jo = (SmallJavaObject) returnedValue;
((JComponent) jo.value).repaint();
} break;
case 80:{ // content of text area
SmallJavaObject jt = (SmallJavaObject) stack[--stackTop];
returnedValue = stack[--stackTop]; // class
Object jo = jt.value;
if (jo instanceof JScrollPane) jo = ((JScrollPane) jo).getViewport().getView();
if (jo instanceof JTextComponent)
returnedValue = new SmallByteArray(returnedValue,((JTextComponent) jo).getText());
else returnedValue = new SmallByteArray(returnedValue, "");
} break;
case 81: {// content of selected text area
SmallJavaObject jt = (SmallJavaObject) stack[--stackTop];
returnedValue = stack[--stackTop]; // class
Object jo = jt.value;
if (jo instanceof JScrollPane) jo = ((JScrollPane) jo).getViewport().getView();
if (jo instanceof JTextComponent)
returnedValue = new SmallByteArray(returnedValue,((JTextComponent) jo).getSelectedText());
else returnedValue = new SmallByteArray(returnedValue, "");
} break;
case 82:{ // set text area
returnedValue = stack[--stackTop];// text
SmallJavaObject jt = (SmallJavaObject) stack[--stackTop];
Object jo = jt.value;
if (jo instanceof JScrollPane) jo = ((JScrollPane) jo).getViewport().getView();
if (jo instanceof JTextComponent)
((JTextComponent) jo).setText(returnedValue.toString());
} break;
case 83:{ // get selected index
SmallJavaObject jo = (SmallJavaObject) stack[--stackTop];
Object jl = jo.value;
if (jl instanceof JScrollPane) jl = ((JScrollPane) jl).getViewport().getView();
if (jl instanceof JList)
returnedValue = newInteger(((JList) jl).getSelectedIndex()+1);
else if (jl instanceof JScrollBar)
returnedValue = newInteger(((JScrollBar) jl).getValue());
else returnedValue = newInteger(0);
} break;
case 84:{ // set list data
SmallObject data = stack[--stackTop];
returnedValue = stack[--stackTop];
SmallJavaObject jo = (SmallJavaObject) returnedValue;
Object jl = jo.value;
if (jl instanceof JScrollPane) jl = ((JScrollPane) jl).getViewport().getView();
if (jl instanceof JList) {
((JList) jl).setListData(data.data);
((JList) jl).repaint();
}} break;
case 85: { // new slider
final SmallObject action = stack[--stackTop];
int max = ((SmallInt) stack[--stackTop]).value+10; //why?
int min = ((SmallInt) stack[--stackTop]).value;
SmallObject orient = stack[--stackTop];
final JScrollBar bar = new JScrollBar(
((orient==trueObject)?JScrollBar.VERTICAL:JScrollBar.HORIZONTAL),
min, 10, min, max);
returnedValue = new SmallJavaObject(stack[--stackTop],bar);
if (action != nilObject)
bar.addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent ae) {
new ActionThread(action, myThread, ae.getValue()).start();
}});
} break;
case 86: { // onMouseDown b
final SmallObject action = stack[--stackTop];
SmallJavaObject pan = (SmallJavaObject) stack[--stackTop];
Object jo = pan.value;
if (jo instanceof JScrollPane) jo = (JComponent) ((JScrollPane) jo).getViewport().getView();
final JComponent jpan = (JComponent) jo;
jpan.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
new ActionThread(action, myThread, e.getX(), e.getY()).start();
}});
} break;
case 87:{ // onMouseUp b
final SmallObject action = stack[--stackTop];
SmallJavaObject pan = (SmallJavaObject) stack[--stackTop];
Object jo = pan.value;
if (jo instanceof JScrollPane) jo = (JComponent) ((JScrollPane) jo).getViewport().getView();
final JComponent jpan = (JComponent) jo;
jpan.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
new ActionThread(action, myThread, e.getX(), e.getY()).start();
}});
} break;
case 88:{ // onMouseMove b
final SmallObject action = stack[--stackTop];
SmallJavaObject pan = (SmallJavaObject) stack[--stackTop];
Object jo = pan.value;
if (jo instanceof JScrollPane) jo = (JComponent) ((JScrollPane) jo).getViewport().getView();
final JComponent jpan = (JComponent) jo;
jpan.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e)
{ new ActionThread(action, myThread, e.getX(), e.getY()).start(); }
public void mouseMoved(MouseEvent e) {
new ActionThread(action, myThread, e.getX(), e.getY()).start();
}});
} break;
case 90:{ // new menu
SmallObject title = stack[--stackTop]; // text
returnedValue = stack[--stackTop]; // class
JMenu menu = new JMenu(title.toString());
returnedValue = new SmallJavaObject(returnedValue, menu);
} break;