-
Notifications
You must be signed in to change notification settings - Fork 76
/
vm.interpreter.js
1924 lines (1894 loc) · 96 KB
/
vm.interpreter.js
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
"use strict";
/*
* Copyright (c) 2013-2024 Vanessa Freudenberg
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
Object.subclass('Squeak.Interpreter',
'initialization', {
initialize: function(image, display, options) {
console.log('squeak: initializing interpreter ' + Squeak.vmVersion + ' (' + Squeak.vmDate + ')');
this.Squeak = Squeak; // store locally to avoid dynamic lookup in Lively
this.image = image;
this.image.vm = this;
this.options = options || {};
this.primHandler = new Squeak.Primitives(this, display);
this.loadImageState();
this.initVMState();
this.loadInitialContext();
this.hackImage();
this.initCompiler();
console.log('squeak: ready');
},
loadImageState: function() {
this.specialObjects = this.image.specialObjectsArray.pointers;
this.specialSelectors = this.specialObjects[Squeak.splOb_SpecialSelectors].pointers;
this.nilObj = this.specialObjects[Squeak.splOb_NilObject];
this.falseObj = this.specialObjects[Squeak.splOb_FalseObject];
this.trueObj = this.specialObjects[Squeak.splOb_TrueObject];
this.hasClosures = this.image.hasClosures;
this.getGlobals = this.globalsGetter();
// hack for old image that does not support Unix files
if (!this.hasClosures && !this.findMethod("UnixFileDirectory class>>pathNameDelimiter"))
this.primHandler.emulateMac = true;
// pre-release image has inverted colors
if (this.image.version == 6501)
this.primHandler.reverseDisplay = true;
},
initVMState: function() {
this.byteCodeCount = 0;
this.sendCount = 0;
this.interruptCheckCounter = 0;
this.interruptCheckCounterFeedBackReset = 1000;
this.interruptChecksEveryNms = 3;
this.lowSpaceThreshold = 1000000;
this.signalLowSpace = false;
this.nextPollTick = 0;
this.nextWakeupTick = 0;
this.lastTick = 0;
this.interruptKeycode = 2094; //"cmd-."
this.interruptPending = false;
this.pendingFinalizationSignals = 0;
this.freeContexts = this.nilObj;
this.freeLargeContexts = this.nilObj;
this.reclaimableContextCount = 0;
this.nRecycledContexts = 0;
this.nAllocatedContexts = 0;
this.methodCacheSize = 1024;
this.methodCacheMask = this.methodCacheSize - 1;
this.methodCacheRandomish = 0;
this.methodCache = [];
for (var i = 0; i < this.methodCacheSize; i++)
this.methodCache[i] = {lkupClass: null, selector: null, method: null, primIndex: 0, argCount: 0, mClass: null};
this.breakOutOfInterpreter = false;
this.breakOutTick = 0;
this.breakOnMethod = null; // method to break on
this.breakOnNewMethod = false;
this.breakOnMessageNotUnderstood = false;
this.breakOnContextChanged = false;
this.breakOnContextReturned = null; // context to break on
this.messages = {};
this.startupTime = Date.now(); // base for millisecond clock
},
loadInitialContext: function() {
var schedAssn = this.specialObjects[Squeak.splOb_SchedulerAssociation];
var sched = schedAssn.pointers[Squeak.Assn_value];
var proc = sched.pointers[Squeak.ProcSched_activeProcess];
this.activeContext = proc.pointers[Squeak.Proc_suspendedContext];
this.activeContext.dirty = true;
this.fetchContextRegisters(this.activeContext);
this.reclaimableContextCount = 0;
},
globalsGetter: function() {
// Globals (more specifically the pointers we are interested in) might
// change during execution, because a Dictionary needs growing for example.
// Therefore answer a getter function to access the actual globals (pointers).
// This getter can be used, even if the Dictionary has grown (and thereby the
// underlying Array is replaced by a larger one), because it uses the reference
// to the 'outer' Dictionary instead of the pointers to the values.
var smalltalk = this.specialObjects[Squeak.splOb_SmalltalkDictionary],
smalltalkClass = smalltalk.sqClass.className();
if (smalltalkClass === "Association") {
smalltalk = smalltalk.pointers[1];
smalltalkClass = smalltalk.sqClass.className();
}
if (smalltalkClass === "SystemDictionary")
return function() { return smalltalk.pointers[1].pointers; };
if (smalltalkClass === "SmalltalkImage") {
var globals = smalltalk.pointers[0],
globalsClass = globals.sqClass.className();
if (globalsClass === "SystemDictionary")
return function() { return globals.pointers[1].pointers; };
if (globalsClass === "Environment")
return function() { return globals.pointers[2].pointers[1].pointers; };
}
console.warn("cannot find global dict");
return function() { return []; };
},
initCompiler: function() {
if (!Squeak.Compiler)
return console.warn("Squeak.Compiler not loaded, using interpreter only");
// some JS environments disallow creating functions at runtime (e.g. FireFox OS apps)
try {
if (new Function("return 42")() !== 42)
return console.warn("function constructor not working, disabling JIT");
} catch (e) {
return console.warn("disabling JIT: " + e);
}
// disable JIT on slow machines, which are likely memory-limited
var kObjPerSec = this.image.oldSpaceCount / (this.startupTime - this.image.startupTime);
if (kObjPerSec < 10)
return console.warn("Slow machine detected (loaded " + (kObjPerSec*1000|0) + " objects/sec), using interpreter only");
// compiler might decide to not handle current image
try {
console.log("squeak: initializing JIT compiler");
var compiler = new Squeak.Compiler(this);
if (compiler.compile) this.compiler = compiler;
} catch(e) {
console.warn("Compiler: " + e);
}
if (!this.compiler) {
console.warn("SqueakJS will be running in interpreter mode only (slow)");
}
},
hackImage: function() {
// hack methods to make work / speed up
var returnSelf = 256,
returnTrue = 257,
returnFalse = 258,
returnNil = 259,
opts = typeof location === 'object' ? location.hash : "",
sista = this.method.methodSignFlag();
[
// Etoys fallback for missing translation files is hugely inefficient.
// This speeds up opening a viewer by 10x (!)
// Remove when we added translation files.
//{method: "String>>translated", primitive: returnSelf, enabled: true},
//{method: "String>>translatedInAllDomains", primitive: returnSelf, enabled: true},
// 64 bit Squeak does not flush word size on snapshot
{method: "SmalltalkImage>>wordSize", literal: {index: 1, old: 8, hack: 4}, enabled: true},
// Squeak 5.3 disable wizard by replacing #open send with pop
{method: "ReleaseBuilder class>>prepareEnvironment", bytecode: {pc: 28, old: 0xD8, hack: 0x87}, enabled: opts.includes("wizard=false")},
// Squeak source file should use UTF8 not MacRoman (both V3 and Sista)
{method: "Latin1Environment class>>systemConverterClass", bytecode: {pc: 53, old: 0x45, hack: 0x49}, enabled: !this.image.isSpur},
{method: "Latin1Environment class>>systemConverterClass", bytecode: {pc: 38, old: 0x16, hack: 0x13}, enabled: this.image.isSpur && sista},
{method: "Latin1Environment class>>systemConverterClass", bytecode: {pc: 50, old: 0x44, hack: 0x48}, enabled: this.image.isSpur && !sista},
].forEach(function(each) {
try {
var m = each.enabled && this.findMethod(each.method);
if (m) {
var prim = each.primitive,
byte = each.bytecode,
lit = each.literal,
hacked = true;
if (prim) m.pointers[0] |= prim;
else if (byte && m.bytes[byte.pc] === byte.old) m.bytes[byte.pc] = byte.hack;
else if (byte && m.bytes[byte.pc] === byte.hack) hacked = false; // already there
else if (lit && m.pointers[lit.index].pointers[1] === lit.old) m.pointers[lit.index].pointers[1] = lit.hack;
else if (lit && m.pointers[lit.index].pointers[1] === lit.hack) hacked = false; // already there
else { hacked = false; console.warn("Not hacking " + each.method); }
if (hacked) console.warn("Hacking " + each.method);
}
} catch (error) {
console.error("Failed to hack " + each.method + " with error " + error);
}
}, this);
},
},
'interpreting', {
interpretOne: function(singleStep) {
if (this.method.compiled) {
if (singleStep) {
if (!this.compiler.enableSingleStepping(this.method)) {
this.method.compiled = null;
return this.interpretOne(singleStep);
}
this.breakNow();
}
this.method.compiled(this);
return;
}
if (this.method.methodSignFlag()) {
return this.interpretOneSistaWithExtensions(singleStep, 0, 0);
}
var Squeak = this.Squeak; // avoid dynamic lookup of "Squeak" in Lively
var b, b2;
this.byteCodeCount++;
b = this.nextByte();
switch (b) { /* The Main V3 Bytecode Dispatch Loop */
// load receiver variable
case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
case 0x08: case 0x09: case 0x0A: case 0x0B: case 0x0C: case 0x0D: case 0x0E: case 0x0F:
this.push(this.receiver.pointers[b&0xF]); return;
// load temporary variable
case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
case 0x18: case 0x19: case 0x1A: case 0x1B: case 0x1C: case 0x1D: case 0x1E: case 0x1F:
this.push(this.homeContext.pointers[Squeak.Context_tempFrameStart+(b&0xF)]); return;
// loadLiteral
case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
case 0x28: case 0x29: case 0x2A: case 0x2B: case 0x2C: case 0x2D: case 0x2E: case 0x2F:
case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: case 0x35: case 0x36: case 0x37:
case 0x38: case 0x39: case 0x3A: case 0x3B: case 0x3C: case 0x3D: case 0x3E: case 0x3F:
this.push(this.method.methodGetLiteral(b&0x1F)); return;
// loadLiteralIndirect
case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: case 0x46: case 0x47:
case 0x48: case 0x49: case 0x4A: case 0x4B: case 0x4C: case 0x4D: case 0x4E: case 0x4F:
case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
case 0x58: case 0x59: case 0x5A: case 0x5B: case 0x5C: case 0x5D: case 0x5E: case 0x5F:
this.push((this.method.methodGetLiteral(b&0x1F)).pointers[Squeak.Assn_value]); return;
// storeAndPop rcvr, temp
case 0x60: case 0x61: case 0x62: case 0x63: case 0x64: case 0x65: case 0x66: case 0x67:
this.receiver.dirty = true;
this.receiver.pointers[b&7] = this.pop(); return;
case 0x68: case 0x69: case 0x6A: case 0x6B: case 0x6C: case 0x6D: case 0x6E: case 0x6F:
this.homeContext.pointers[Squeak.Context_tempFrameStart+(b&7)] = this.pop(); return;
// Quick push
case 0x70: this.push(this.receiver); return;
case 0x71: this.push(this.trueObj); return;
case 0x72: this.push(this.falseObj); return;
case 0x73: this.push(this.nilObj); return;
case 0x74: this.push(-1); return;
case 0x75: this.push(0); return;
case 0x76: this.push(1); return;
case 0x77: this.push(2); return;
// Quick return
case 0x78: this.doReturn(this.receiver); return;
case 0x79: this.doReturn(this.trueObj); return;
case 0x7A: this.doReturn(this.falseObj); return;
case 0x7B: this.doReturn(this.nilObj); return;
case 0x7C: this.doReturn(this.pop()); return;
case 0x7D: this.doReturn(this.pop(), this.activeContext.pointers[Squeak.BlockContext_caller]); return; // blockReturn
case 0x7E: this.nono(); return;
case 0x7F: this.nono(); return;
// Sundry
case 0x80: this.extendedPush(this.nextByte()); return;
case 0x81: this.extendedStore(this.nextByte()); return;
case 0x82: this.extendedStorePop(this.nextByte()); return;
// singleExtendedSend
case 0x83: b2 = this.nextByte(); this.send(this.method.methodGetSelector(b2&31), b2>>5, false); return;
case 0x84: this.doubleExtendedDoAnything(this.nextByte()); return;
// singleExtendedSendToSuper
case 0x85: b2= this.nextByte(); this.send(this.method.methodGetSelector(b2&31), b2>>5, true); return;
// secondExtendedSend
case 0x86: b2= this.nextByte(); this.send(this.method.methodGetSelector(b2&63), b2>>6, false); return;
case 0x87: this.pop(); return; // pop
case 0x88: this.push(this.top()); return; // dup
// thisContext
case 0x89: this.push(this.exportThisContext()); return;
// Closures
case 0x8A: this.pushNewArray(this.nextByte()); // create new temp vector
return;
case 0x8B: this.callPrimBytecode(0x81);
return;
case 0x8C: b2 = this.nextByte(); // remote push from temp vector
this.push(this.homeContext.pointers[Squeak.Context_tempFrameStart+this.nextByte()].pointers[b2]);
return;
case 0x8D: b2 = this.nextByte(); // remote store into temp vector
var vec = this.homeContext.pointers[Squeak.Context_tempFrameStart+this.nextByte()];
vec.pointers[b2] = this.top();
vec.dirty = true;
return;
case 0x8E: b2 = this.nextByte(); // remote store and pop into temp vector
var vec = this.homeContext.pointers[Squeak.Context_tempFrameStart+this.nextByte()];
vec.pointers[b2] = this.pop();
vec.dirty = true;
return;
case 0x8F: this.pushClosureCopy(); return;
// Short jmp
case 0x90: case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97:
this.pc += (b&7)+1; return;
// Short conditional jump on false
case 0x98: case 0x99: case 0x9A: case 0x9B: case 0x9C: case 0x9D: case 0x9E: case 0x9F:
this.jumpIfFalse((b&7)+1); return;
// Long jump, forward and back
case 0xA0: case 0xA1: case 0xA2: case 0xA3: case 0xA4: case 0xA5: case 0xA6: case 0xA7:
b2 = this.nextByte();
this.pc += (((b&7)-4)*256 + b2);
if ((b&7)<4) // check for process switch on backward jumps (loops)
if (this.interruptCheckCounter-- <= 0) this.checkForInterrupts();
return;
// Long conditional jump on true
case 0xA8: case 0xA9: case 0xAA: case 0xAB:
this.jumpIfTrue((b&3)*256 + this.nextByte()); return;
// Long conditional jump on false
case 0xAC: case 0xAD: case 0xAE: case 0xAF:
this.jumpIfFalse((b&3)*256 + this.nextByte()); return;
// Arithmetic Ops... + - < > <= >= = ~= * / @ lshift: lxor: land: lor:
case 0xB0: this.success = true; this.resultIsFloat = false;
if (!this.pop2AndPushNumResult(this.stackIntOrFloat(1) + this.stackIntOrFloat(0))) this.sendSpecial(b&0xF); return; // PLUS +
case 0xB1: this.success = true; this.resultIsFloat = false;
if (!this.pop2AndPushNumResult(this.stackIntOrFloat(1) - this.stackIntOrFloat(0))) this.sendSpecial(b&0xF); return; // MINUS -
case 0xB2: this.success = true;
if (!this.pop2AndPushBoolResult(this.stackIntOrFloat(1) < this.stackIntOrFloat(0))) this.sendSpecial(b&0xF); return; // LESS <
case 0xB3: this.success = true;
if (!this.pop2AndPushBoolResult(this.stackIntOrFloat(1) > this.stackIntOrFloat(0))) this.sendSpecial(b&0xF); return; // GRTR >
case 0xB4: this.success = true;
if (!this.pop2AndPushBoolResult(this.stackIntOrFloat(1) <= this.stackIntOrFloat(0))) this.sendSpecial(b&0xF); return; // LEQ <=
case 0xB5: this.success = true;
if (!this.pop2AndPushBoolResult(this.stackIntOrFloat(1) >= this.stackIntOrFloat(0))) this.sendSpecial(b&0xF); return; // GEQ >=
case 0xB6: this.success = true;
if (!this.pop2AndPushBoolResult(this.stackIntOrFloat(1) === this.stackIntOrFloat(0))) this.sendSpecial(b&0xF); return; // EQU =
case 0xB7: this.success = true;
if (!this.pop2AndPushBoolResult(this.stackIntOrFloat(1) !== this.stackIntOrFloat(0))) this.sendSpecial(b&0xF); return; // NEQ ~=
case 0xB8: this.success = true; this.resultIsFloat = false;
if (!this.pop2AndPushNumResult(this.stackIntOrFloat(1) * this.stackIntOrFloat(0))) this.sendSpecial(b&0xF); return; // TIMES *
case 0xB9: this.success = true;
if (!this.pop2AndPushIntResult(this.quickDivide(this.stackInteger(1),this.stackInteger(0)))) this.sendSpecial(b&0xF); return; // Divide /
case 0xBA: this.success = true;
if (!this.pop2AndPushIntResult(this.mod(this.stackInteger(1),this.stackInteger(0)))) this.sendSpecial(b&0xF); return; // MOD \
case 0xBB: this.success = true;
if (!this.primHandler.primitiveMakePoint(1, true)) this.sendSpecial(b&0xF); return; // MakePt int@int
case 0xBC: this.success = true;
if (!this.pop2AndPushIntResult(this.safeShift(this.stackInteger(1),this.stackInteger(0)))) this.sendSpecial(b&0xF); return; // bitShift:
case 0xBD: this.success = true;
if (!this.pop2AndPushIntResult(this.div(this.stackInteger(1),this.stackInteger(0)))) this.sendSpecial(b&0xF); return; // Divide //
case 0xBE: this.success = true;
if (!this.pop2AndPushIntResult(this.stackInteger(1) & this.stackInteger(0))) this.sendSpecial(b&0xF); return; // bitAnd:
case 0xBF: this.success = true;
if (!this.pop2AndPushIntResult(this.stackInteger(1) | this.stackInteger(0))) this.sendSpecial(b&0xF); return; // bitOr:
// at:, at:put:, size, next, nextPut:, ...
case 0xC0: case 0xC1: case 0xC2: case 0xC3: case 0xC4: case 0xC5: case 0xC6: case 0xC7:
case 0xC8: case 0xC9: case 0xCA: case 0xCB: case 0xCC: case 0xCD: case 0xCE: case 0xCF:
if (!this.primHandler.quickSendOther(this.receiver, b&0xF))
this.sendSpecial((b&0xF)+16); return;
// Send Literal Selector with 0, 1, and 2 args
case 0xD0: case 0xD1: case 0xD2: case 0xD3: case 0xD4: case 0xD5: case 0xD6: case 0xD7:
case 0xD8: case 0xD9: case 0xDA: case 0xDB: case 0xDC: case 0xDD: case 0xDE: case 0xDF:
this.send(this.method.methodGetSelector(b&0xF), 0, false); return;
case 0xE0: case 0xE1: case 0xE2: case 0xE3: case 0xE4: case 0xE5: case 0xE6: case 0xE7:
case 0xE8: case 0xE9: case 0xEA: case 0xEB: case 0xEC: case 0xED: case 0xEE: case 0xEF:
this.send(this.method.methodGetSelector(b&0xF), 1, false); return;
case 0xF0: case 0xF1: case 0xF2: case 0xF3: case 0xF4: case 0xF5: case 0xF6: case 0xF7:
case 0xF8: case 0xF9: case 0xFA: case 0xFB: case 0xFC: case 0xFD: case 0xFE: case 0xFF:
this.send(this.method.methodGetSelector(b&0xF), 2, false); return;
}
throw Error("not a bytecode: " + b);
},
interpretOneSistaWithExtensions: function(singleStep, extA, extB) {
var Squeak = this.Squeak; // avoid dynamic lookup of "Squeak" in Lively
var b, b2;
this.byteCodeCount++;
b = this.nextByte();
switch (b) { /* The Main Sista Bytecode Dispatch Loop */
// 1 Byte Bytecodes
// load receiver variable
case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
case 0x08: case 0x09: case 0x0A: case 0x0B: case 0x0C: case 0x0D: case 0x0E: case 0x0F:
this.push(this.receiver.pointers[b&0xF]); return;
// load literal variable
case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
case 0x18: case 0x19: case 0x1A: case 0x1B: case 0x1C: case 0x1D: case 0x1E: case 0x1F:
this.push((this.method.methodGetLiteral(b&0xF)).pointers[Squeak.Assn_value]); return;
// load literal constant
case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
case 0x28: case 0x29: case 0x2A: case 0x2B: case 0x2C: case 0x2D: case 0x2E: case 0x2F:
case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: case 0x35: case 0x36: case 0x37:
case 0x38: case 0x39: case 0x3A: case 0x3B: case 0x3C: case 0x3D: case 0x3E: case 0x3F:
this.push(this.method.methodGetLiteral(b&0x1F)); return;
// load temporary variable
case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: case 0x46: case 0x47:
this.push(this.homeContext.pointers[Squeak.Context_tempFrameStart+(b&0x7)]); return;
case 0x48: case 0x49: case 0x4A: case 0x4B:
this.push(this.homeContext.pointers[Squeak.Context_tempFrameStart+(b&0x3)+8]); return;
case 0x4C: this.push(this.receiver); return;
case 0x4D: this.push(this.trueObj); return;
case 0x4E: this.push(this.falseObj); return;
case 0x4F: this.push(this.nilObj); return;
case 0x50: this.push(0); return;
case 0x51: this.push(1); return;
case 0x52:
if (extB == 0) {
this.push(this.exportThisContext()); return;
} else {
this.nono(); return;
}
case 0x53: this.push(this.top()); return;
case 0x54: case 0x55: case 0x56: case 0x57: this.nono(); return; // unused
case 0x58: this.doReturn(this.receiver); return;
case 0x59: this.doReturn(this.trueObj); return;
case 0x5A: this.doReturn(this.falseObj); return;
case 0x5B: this.doReturn(this.nilObj); return;
case 0x5C: this.doReturn(this.pop()); return;
case 0x5D: this.doReturn(this.nilObj, this.activeContext.pointers[Squeak.BlockContext_caller]); return; // blockReturn nil
case 0x5E:
if (extA == 0) {
this.doReturn(this.pop(), this.activeContext.pointers[Squeak.BlockContext_caller]); return; // blockReturn
} else {
this.nono(); return;
}
case 0x5F:
return; // nop
// Arithmetic Ops... + - < > <= >= = ~= * / @ lshift: lxor: land: lor:
case 0x60: this.success = true; this.resultIsFloat = false;
if (!this.pop2AndPushNumResult(this.stackIntOrFloat(1) + this.stackIntOrFloat(0))) this.sendSpecial(b&0xF); return; // PLUS +
case 0x61: this.success = true; this.resultIsFloat = false;
if (!this.pop2AndPushNumResult(this.stackIntOrFloat(1) - this.stackIntOrFloat(0))) this.sendSpecial(b&0xF); return; // MINUS -
case 0x62: this.success = true;
if (!this.pop2AndPushBoolResult(this.stackIntOrFloat(1) < this.stackIntOrFloat(0))) this.sendSpecial(b&0xF); return; // LESS <
case 0x63: this.success = true;
if (!this.pop2AndPushBoolResult(this.stackIntOrFloat(1) > this.stackIntOrFloat(0))) this.sendSpecial(b&0xF); return; // GRTR >
case 0x64: this.success = true;
if (!this.pop2AndPushBoolResult(this.stackIntOrFloat(1) <= this.stackIntOrFloat(0))) this.sendSpecial(b&0xF); return; // LEQ <=
case 0x65: this.success = true;
if (!this.pop2AndPushBoolResult(this.stackIntOrFloat(1) >= this.stackIntOrFloat(0))) this.sendSpecial(b&0xF); return; // GEQ >=
case 0x66: this.success = true;
if (!this.pop2AndPushBoolResult(this.stackIntOrFloat(1) === this.stackIntOrFloat(0))) this.sendSpecial(b&0xF); return; // EQU =
case 0x67: this.success = true;
if (!this.pop2AndPushBoolResult(this.stackIntOrFloat(1) !== this.stackIntOrFloat(0))) this.sendSpecial(b&0xF); return; // NEQ ~=
case 0x68: this.success = true; this.resultIsFloat = false;
if (!this.pop2AndPushNumResult(this.stackIntOrFloat(1) * this.stackIntOrFloat(0))) this.sendSpecial(b&0xF); return; // TIMES *
case 0x69: this.success = true;
if (!this.pop2AndPushIntResult(this.quickDivide(this.stackInteger(1),this.stackInteger(0)))) this.sendSpecial(b&0xF); return; // Divide /
case 0x6A: this.success = true;
if (!this.pop2AndPushIntResult(this.mod(this.stackInteger(1),this.stackInteger(0)))) this.sendSpecial(b&0xF); return; // MOD \
case 0x6B: this.success = true;
if (!this.primHandler.primitiveMakePoint(1, true)) this.sendSpecial(b&0xF); return; // MakePt int@int
case 0x6C: this.success = true;
if (!this.pop2AndPushIntResult(this.safeShift(this.stackInteger(1),this.stackInteger(0)))) this.sendSpecial(b&0xF); return; // bitShift:
case 0x6D: this.success = true;
if (!this.pop2AndPushIntResult(this.div(this.stackInteger(1),this.stackInteger(0)))) this.sendSpecial(b&0xF); return; // Divide //
case 0x6E: this.success = true;
if (!this.pop2AndPushIntResult(this.stackInteger(1) & this.stackInteger(0))) this.sendSpecial(b&0xF); return; // bitAnd:
case 0x6F: this.success = true;
if (!this.pop2AndPushIntResult(this.stackInteger(1) | this.stackInteger(0))) this.sendSpecial(b&0xF); return; // bitOr:
// at:, at:put:, size, next, nextPut:, ...
case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77:
case 0x78: case 0x79: case 0x7A: case 0x7B: case 0x7C: case 0x7D: case 0x7E: case 0x7F:
if (!this.primHandler.quickSendOther(this.receiver, b&0xF))
this.sendSpecial((b&0xF)+16); return;
// Send Literal Selector with 0, 1, and 2 args
case 0x80: case 0x81: case 0x82: case 0x83: case 0x84: case 0x85: case 0x86: case 0x87:
case 0x88: case 0x89: case 0x8A: case 0x8B: case 0x8C: case 0x8D: case 0x8E: case 0x8F:
this.send(this.method.methodGetSelector(b&0xF), 0, false); return;
case 0x90: case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97:
case 0x98: case 0x99: case 0x9A: case 0x9B: case 0x9C: case 0x9D: case 0x9E: case 0x9F:
this.send(this.method.methodGetSelector(b&0xF), 1, false); return;
case 0xA0: case 0xA1: case 0xA2: case 0xA3: case 0xA4: case 0xA5: case 0xA6: case 0xA7:
case 0xA8: case 0xA9: case 0xAA: case 0xAB: case 0xAC: case 0xAD: case 0xAE: case 0xAF:
this.send(this.method.methodGetSelector(b&0xF), 2, false); return;
// Short jmp
case 0xB0: case 0xB1: case 0xB2: case 0xB3: case 0xB4: case 0xB5: case 0xB6: case 0xB7:
this.pc += (b&7)+1; return;
// Short conditional jump on true
case 0xB8: case 0xB9: case 0xBA: case 0xBB: case 0xBC: case 0xBD: case 0xBE: case 0xBF:
this.jumpIfTrue((b&7)+1); return;
// Short conditional jump on false
case 0xC0: case 0xC1: case 0xC2: case 0xC3: case 0xC4: case 0xC5: case 0xC6: case 0xC7:
this.jumpIfFalse((b&7)+1); return;
// storeAndPop rcvr, temp
case 0xC8: case 0xC9: case 0xCA: case 0xCB: case 0xCC: case 0xCD: case 0xCE: case 0xCF:
this.receiver.dirty = true;
this.receiver.pointers[b&7] = this.pop(); return;
case 0xD0: case 0xD1: case 0xD2: case 0xD3: case 0xD4: case 0xD5: case 0xD6: case 0xD7:
this.homeContext.pointers[Squeak.Context_tempFrameStart+(b&7)] = this.pop(); return;
case 0xD8: this.pop(); return; // pop
case 0xD9: this.nono(); return; // FIXME: Unconditional trap
case 0xDA: case 0xDB: case 0xDC: case 0xDD: case 0xDE: case 0xDF:
this.nono(); return; // unused
// 2 Byte Bytecodes
case 0xE0:
b2 = this.nextByte(); this.interpretOneSistaWithExtensions(singleStep, (extA << 8) + b2, extB); return;
case 0xE1:
b2 = this.nextByte(); this.interpretOneSistaWithExtensions(singleStep, extA, (extB << 8) + (b2 < 128 ? b2 : b2-256)); return;
case 0xE2:
b2 = this.nextByte(); this.push(this.receiver.pointers[b2 + (extA << 8)]); return;
case 0xE3:
b2 = this.nextByte(); this.push((this.method.methodGetLiteral(b2 + (extA << 8))).pointers[Squeak.Assn_value]); return;
case 0xE4:
b2 = this.nextByte(); this.push(this.method.methodGetLiteral(b2 + (extA << 8))); return;
case 0xE5:
b2 = this.nextByte(); this.push(this.homeContext.pointers[Squeak.Context_tempFrameStart+b2]); return;
case 0xE6: this.nono(); return; // unused
case 0xE7: this.pushNewArray(this.nextByte()); return; // create new temp vector
case 0xE8: b2 = this.nextByte(); this.push(b2 + (extB << 8)); return; // push SmallInteger
case 0xE9: b2 = this.nextByte(); this.push(this.image.getCharacter(b2 + (extB << 8))); return; // push Character
case 0xEA:
b2 = this.nextByte();
this.send(this.method.methodGetSelector((b2 >> 3) + (extA << 5)), (b2 & 7) + (extB << 3), false); return;
case 0xEB:
b2 = this.nextByte();
var literal = this.method.methodGetSelector((b2 >> 3) + (extA << 5));
if (extB >= 64) {
this.sendSuperDirected(literal, (b2 & 7) + ((extB & 63) << 3)); return;
} else {
this.send(literal, (b2 & 7) + (extB << 3), true); return;
}
case 0xEC: this.nono(); return; // unused
case 0xED: // long jump, forward and back
var offset = this.nextByte() + (extB << 8);
this.pc += offset;
if (offset < 0) // check for process switch on backward jumps (loops)
if (this.interruptCheckCounter-- <= 0) this.checkForInterrupts();
return;
case 0xEE: // long conditional jump on true
this.jumpIfTrue(this.nextByte() + (extB << 8)); return;
case 0xEF: // long conditional jump on false
this.jumpIfFalse(this.nextByte() + (extB << 8)); return;
case 0xF0: // pop into receiver
this.receiver.dirty = true;
this.receiver.pointers[this.nextByte() + (extA << 8)] = this.pop();
return;
case 0xF1: // pop into literal
var assoc = this.method.methodGetLiteral(this.nextByte() + (extA << 8));
assoc.dirty = true;
assoc.pointers[Squeak.Assn_value] = this.pop();
return;
case 0xF2: // pop into temp
this.homeContext.pointers[Squeak.Context_tempFrameStart + this.nextByte()] = this.pop();
return;
case 0xF3: // store into receiver
this.receiver.dirty = true;
this.receiver.pointers[this.nextByte() + (extA << 8)] = this.top();
return;
case 0xF4: // store into literal
var assoc = this.method.methodGetLiteral(this.nextByte() + (extA << 8));
assoc.dirty = true;
assoc.pointers[Squeak.Assn_value] = this.top();
return;
case 0xF5: // store into temp
this.homeContext.pointers[Squeak.Context_tempFrameStart + this.nextByte()] = this.top();
return;
case 0xF6: case 0xF7: this.nono(); return; // unused
// 3 Byte Bytecodes
case 0xF8: this.callPrimBytecode(0xF5); return;
case 0xF9: this.pushFullClosure(extA); return;
case 0xFA: this.pushClosureCopyExtended(extA, extB); return;
case 0xFB: b2 = this.nextByte(); // remote push from temp vector
this.push(this.homeContext.pointers[Squeak.Context_tempFrameStart+this.nextByte()].pointers[b2]);
return;
case 0xFC: b2 = this.nextByte(); // remote store into temp vector
var vec = this.homeContext.pointers[Squeak.Context_tempFrameStart+this.nextByte()];
vec.pointers[b2] = this.top();
vec.dirty = true;
return;
case 0xFD: b2 = this.nextByte(); // remote store and pop into temp vector
var vec = this.homeContext.pointers[Squeak.Context_tempFrameStart+this.nextByte()];
vec.pointers[b2] = this.pop();
vec.dirty = true;
return;
case 0xFE: case 0xFF: this.nono(); return; // unused
}
throw Error("not a bytecode: " + b);
},
interpret: function(forMilliseconds, thenDo) {
// run for a couple milliseconds (but only until idle or break)
// answer milliseconds to sleep (until next timer wakeup)
// or 'break' if reached breakpoint
// call thenDo with that result when done
if (this.frozen) return 'frozen';
this.isIdle = false;
this.breakOutOfInterpreter = false;
this.breakAfter(forMilliseconds || 500);
while (this.breakOutOfInterpreter === false)
if (this.method.compiled) {
this.method.compiled(this);
} else {
this.interpretOne();
}
// this is to allow 'freezing' the interpreter and restarting it asynchronously. See freeze()
if (typeof this.breakOutOfInterpreter == "function")
return this.breakOutOfInterpreter(thenDo);
// normally, we answer regularly
var result = this.breakOutOfInterpreter == 'break' ? 'break'
: !this.isIdle ? 0
: !this.nextWakeupTick ? 'sleep' // all processes waiting
: Math.max(1, this.nextWakeupTick - this.primHandler.millisecondClockValue());
if (thenDo) thenDo(result);
return result;
},
goIdle: function() {
// make sure we tend to pending delays
var hadTimer = this.nextWakeupTick !== 0;
this.forceInterruptCheck();
this.checkForInterrupts();
var hasTimer = this.nextWakeupTick !== 0;
// go idle unless a timer just expired
this.isIdle = hasTimer || !hadTimer;
this.breakOut();
},
freeze: function(frozenDo) {
// Stop the interpreter. Answer a function that can be
// called to continue interpreting.
// Optionally, frozenDo is called asynchronously when frozen
var continueFunc;
this.frozen = true;
this.breakOutOfInterpreter = function(thenDo) {
if (!thenDo) throw Error("need function to restart interpreter");
continueFunc = thenDo;
return "frozen";
}.bind(this);
var unfreeze = function() {
this.frozen = false;
if (!continueFunc) throw Error("no continue function");
continueFunc(0); //continue without timeout
}.bind(this);
if (frozenDo) self.setTimeout(function(){frozenDo(unfreeze)}, 0);
return unfreeze;
},
breakOut: function() {
this.breakOutOfInterpreter = this.breakOutOfInterpreter || true; // do not overwrite break string
},
nextByte: function() {
return this.method.bytes[this.pc++];
},
nono: function() {
throw Error("Oh No!");
},
forceInterruptCheck: function() {
this.interruptCheckCounter = -1000;
},
checkForInterrupts: function() {
//Check for interrupts at sends and backward jumps
var now = this.primHandler.millisecondClockValue();
if (now < this.lastTick) { // millisecond clock wrapped
this.nextPollTick = now + (this.nextPollTick - this.lastTick);
this.breakOutTick = now + (this.breakOutTick - this.lastTick);
if (this.nextWakeupTick !== 0)
this.nextWakeupTick = now + (this.nextWakeupTick - this.lastTick);
}
//Feedback logic attempts to keep interrupt response around 3ms...
if (this.interruptCheckCounter > -100) { // only if not a forced check
if ((now - this.lastTick) < this.interruptChecksEveryNms) { //wrapping is not a concern
this.interruptCheckCounterFeedBackReset += 10;
} else { // do a thousand sends even if we are too slow for 3ms
if (this.interruptCheckCounterFeedBackReset <= 1000)
this.interruptCheckCounterFeedBackReset = 1000;
else
this.interruptCheckCounterFeedBackReset -= 12;
}
}
this.interruptCheckCounter = this.interruptCheckCounterFeedBackReset; //reset the interrupt check counter
this.lastTick = now; //used to detect wraparound of millisecond clock
if (this.signalLowSpace) {
this.signalLowSpace = false; // reset flag
var sema = this.specialObjects[Squeak.splOb_TheLowSpaceSemaphore];
if (!sema.isNil) this.primHandler.synchronousSignal(sema);
}
// if (now >= nextPollTick) {
// ioProcessEvents(); //sets interruptPending if interrupt key pressed
// nextPollTick= now + 500; } //msecs to wait before next call to ioProcessEvents"
if (this.interruptPending) {
this.interruptPending = false; //reset interrupt flag
var sema = this.specialObjects[Squeak.splOb_TheInterruptSemaphore];
if (!sema.isNil) this.primHandler.synchronousSignal(sema);
}
if ((this.nextWakeupTick !== 0) && (now >= this.nextWakeupTick)) {
this.nextWakeupTick = 0; //reset timer interrupt
var sema = this.specialObjects[Squeak.splOb_TheTimerSemaphore];
if (!sema.isNil) this.primHandler.synchronousSignal(sema);
}
if (this.pendingFinalizationSignals > 0) { //signal any pending finalizations
var sema = this.specialObjects[Squeak.splOb_TheFinalizationSemaphore];
this.pendingFinalizationSignals = 0;
if (!sema.isNil) this.primHandler.synchronousSignal(sema);
}
if (this.primHandler.semaphoresToSignal.length > 0)
this.primHandler.signalExternalSemaphores(); // signal pending semaphores, if any
// if this is a long-running do-it, compile it
if (!this.method.compiled) this.compileIfPossible(this.method);
// have to return to web browser once in a while
if (now >= this.breakOutTick)
this.breakOut();
},
extendedPush: function(nextByte) {
var lobits = nextByte & 63;
switch (nextByte>>6) {
case 0: this.push(this.receiver.pointers[lobits]);break;
case 1: this.push(this.homeContext.pointers[Squeak.Context_tempFrameStart+lobits]); break;
case 2: this.push(this.method.methodGetLiteral(lobits)); break;
case 3: this.push(this.method.methodGetLiteral(lobits).pointers[Squeak.Assn_value]); break;
}
},
extendedStore: function( nextByte) {
var lobits = nextByte & 63;
switch (nextByte>>6) {
case 0:
this.receiver.dirty = true;
this.receiver.pointers[lobits] = this.top();
break;
case 1:
this.homeContext.pointers[Squeak.Context_tempFrameStart+lobits] = this.top();
break;
case 2:
this.nono();
break;
case 3:
var assoc = this.method.methodGetLiteral(lobits);
assoc.dirty = true;
assoc.pointers[Squeak.Assn_value] = this.top();
break;
}
},
extendedStorePop: function(nextByte) {
var lobits = nextByte & 63;
switch (nextByte>>6) {
case 0:
this.receiver.dirty = true;
this.receiver.pointers[lobits] = this.pop();
break;
case 1:
this.homeContext.pointers[Squeak.Context_tempFrameStart+lobits] = this.pop();
break;
case 2:
this.nono();
break;
case 3:
var assoc = this.method.methodGetLiteral(lobits);
assoc.dirty = true;
assoc.pointers[Squeak.Assn_value] = this.pop();
break;
}
},
doubleExtendedDoAnything: function(byte2) {
var byte3 = this.nextByte();
switch (byte2>>5) {
case 0: this.send(this.method.methodGetSelector(byte3), byte2&31, false); break;
case 1: this.send(this.method.methodGetSelector(byte3), byte2&31, true); break;
case 2: this.push(this.receiver.pointers[byte3]); break;
case 3: this.push(this.method.methodGetLiteral(byte3)); break;
case 4: this.push(this.method.methodGetLiteral(byte3).pointers[Squeak.Assn_value]); break;
case 5: this.receiver.dirty = true; this.receiver.pointers[byte3] = this.top(); break;
case 6: this.receiver.dirty = true; this.receiver.pointers[byte3] = this.pop(); break;
case 7: var assoc = this.method.methodGetLiteral(byte3);
assoc.dirty = true;
assoc.pointers[Squeak.Assn_value] = this.top(); break;
}
},
jumpIfTrue: function(delta) {
var top = this.pop();
if (top.isTrue) {this.pc += delta; return;}
if (top.isFalse) return;
this.push(top); //Uh-oh it's not even a boolean (that we know of ;-). Restore stack...
this.send(this.specialObjects[Squeak.splOb_SelectorMustBeBoolean], 0, false);
},
jumpIfFalse: function(delta) {
var top = this.pop();
if (top.isFalse) {this.pc += delta; return;}
if (top.isTrue) return;
this.push(top); //Uh-oh it's not even a boolean (that we know of ;-). Restore stack...
this.send(this.specialObjects[Squeak.splOb_SelectorMustBeBoolean], 0, false);
},
sendSpecial: function(lobits) {
this.send(this.specialSelectors[lobits*2],
this.specialSelectors[(lobits*2)+1],
false); //specialSelectors is {...sel,nArgs,sel,nArgs,...)
},
callPrimBytecode: function(extendedStoreBytecode) {
this.pc += 2; // skip over primitive number
if (this.primFailCode) {
if (this.method.bytes[this.pc] === extendedStoreBytecode)
this.stackTopPut(this.getErrorObjectFromPrimFailCode());
this.primFailCode = 0;
}
},
getErrorObjectFromPrimFailCode: function() {
var primErrTable = this.specialObjects[Squeak.splOb_PrimErrTableIndex];
if (primErrTable && primErrTable.pointers) {
var errorObject = primErrTable.pointers[this.primFailCode - 1];
if (errorObject) return errorObject;
}
return this.primFailCode;
},
},
'closures', {
pushNewArray: function(nextByte) {
var popValues = nextByte > 127,
count = nextByte & 127,
array = this.instantiateClass(this.specialObjects[Squeak.splOb_ClassArray], count);
if (popValues) {
for (var i = 0; i < count; i++)
array.pointers[i] = this.stackValue(count - i - 1);
this.popN(count);
}
this.push(array);
},
pushClosureCopy: function() {
// The compiler has pushed the values to be copied, if any. Find numArgs and numCopied in the byte following.
// Create a Closure with space for the copiedValues and pop numCopied values off the stack into the closure.
// Set numArgs as specified, and set startpc to the pc following the block size and jump over that code.
var numArgsNumCopied = this.nextByte(),
numArgs = numArgsNumCopied & 0xF,
numCopied = numArgsNumCopied >> 4,
blockSizeHigh = this.nextByte(),
blockSize = blockSizeHigh * 256 + this.nextByte(),
initialPC = this.encodeSqueakPC(this.pc, this.method),
closure = this.newClosure(numArgs, initialPC, numCopied);
closure.pointers[Squeak.Closure_outerContext] = this.activeContext;
this.reclaimableContextCount = 0; // The closure refers to thisContext so it can't be reclaimed
if (numCopied > 0) {
for (var i = 0; i < numCopied; i++)
closure.pointers[Squeak.Closure_firstCopiedValue + i] = this.stackValue(numCopied - i - 1);
this.popN(numCopied);
}
this.pc += blockSize;
this.push(closure);
},
pushClosureCopyExtended: function(extA, extB) {
var byteA = this.nextByte();
var byteB = this.nextByte();
var numArgs = (byteA & 7) + this.mod(extA, 16) * 8,
numCopied = (byteA >> 3 & 0x7) + this.div(extA, 16) * 8,
blockSize = byteB + (extB << 8),
initialPC = this.encodeSqueakPC(this.pc, this.method),
closure = this.newClosure(numArgs, initialPC, numCopied);
closure.pointers[Squeak.Closure_outerContext] = this.activeContext;
this.reclaimableContextCount = 0; // The closure refers to thisContext so it can't be reclaimed
if (numCopied > 0) {
for (var i = 0; i < numCopied; i++)
closure.pointers[Squeak.Closure_firstCopiedValue + i] = this.stackValue(numCopied - i - 1);
this.popN(numCopied);
}
this.pc += blockSize;
this.push(closure);
},
pushFullClosure: function(extA) {
var byteA = this.nextByte();
var byteB = this.nextByte();
var literalIndex = byteA + (extA << 8);
var numCopied = byteB & 63;
var context;
if ((byteB >> 6 & 1) == 1) {
context = this.vm.nilObj;
} else {
context = this.activeContext;
}
var compiledBlock = this.method.methodGetLiteral(literalIndex);
var closure = this.newFullClosure(context, numCopied, compiledBlock);
if ((byteB >> 7 & 1) == 1) {
throw Error("on-stack receiver not yet supported");
} else {
closure.pointers[Squeak.ClosureFull_receiver] = this.receiver;
}
this.reclaimableContextCount = 0; // The closure refers to thisContext so it can't be reclaimed
if (numCopied > 0) {
for (var i = 0; i < numCopied; i++)
closure.pointers[Squeak.ClosureFull_firstCopiedValue + i] = this.stackValue(numCopied - i - 1);
this.popN(numCopied);
}
this.push(closure);
},
newClosure: function(numArgs, initialPC, numCopied) {
var closure = this.instantiateClass(this.specialObjects[Squeak.splOb_ClassBlockClosure], numCopied);
closure.pointers[Squeak.Closure_startpc] = initialPC;
closure.pointers[Squeak.Closure_numArgs] = numArgs;
return closure;
},
newFullClosure: function(context, numCopied, compiledBlock) {
var closure = this.instantiateClass(this.specialObjects[Squeak.splOb_ClassFullBlockClosure], numCopied);
closure.pointers[Squeak.Closure_outerContext] = context;
closure.pointers[Squeak.ClosureFull_method] = compiledBlock;
closure.pointers[Squeak.Closure_numArgs] = compiledBlock.methodNumArgs();
return closure;
},
},
'sending', {
send: function(selector, argCount, doSuper) {
var newRcvr = this.stackValue(argCount);
var lookupClass;
if (doSuper) {
lookupClass = this.method.methodClassForSuper();
lookupClass = lookupClass.pointers[Squeak.Class_superclass];
} else {
lookupClass = this.getClass(newRcvr);
}
var entry = this.findSelectorInClass(selector, argCount, lookupClass);
if (entry.primIndex) {
//note details for verification of at/atput primitives
this.verifyAtSelector = selector;
this.verifyAtClass = lookupClass;
}
this.executeNewMethod(newRcvr, entry.method, entry.argCount, entry.primIndex, entry.mClass, selector);
},
sendSuperDirected: function(selector, argCount) {
var lookupClass = this.pop().pointers[Squeak.Class_superclass];
var newRcvr = this.stackValue(argCount);
var entry = this.findSelectorInClass(selector, argCount, lookupClass);
if (entry.primIndex) {
//note details for verification of at/atput primitives
this.verifyAtSelector = selector;
this.verifyAtClass = lookupClass;
}
this.executeNewMethod(newRcvr, entry.method, entry.argCount, entry.primIndex, entry.mClass, selector);
},
sendAsPrimitiveFailure: function(rcvr, method, argCount) {
this.executeNewMethod(rcvr, method, argCount, 0);
},
/**
* @param {*} trueArgCount The number of arguments for the method to be found
* @param {*} argCount The number of arguments currently on the stack (may be different from trueArgCount in the context of primitive 84 etc.)
*/
findSelectorInClass: function(selector, trueArgCount, startingClass, argCount = trueArgCount) {
this.currentSelector = selector; // for primitiveInvokeObjectAsMethod
var cacheEntry = this.findMethodCacheEntry(selector, startingClass);
if (cacheEntry.method) return cacheEntry; // Found it in the method cache
var currentClass = startingClass;
var mDict;
while (!currentClass.isNil) {
mDict = currentClass.pointers[Squeak.Class_mdict];
if (mDict.isNil) {
// MethodDict pointer is nil (hopefully due a swapped out stub)
// -- send #cannotInterpret:
var cantInterpSel = this.specialObjects[Squeak.splOb_SelectorCannotInterpret],
cantInterpMsg = this.createActualMessage(selector, trueArgCount, startingClass);
this.popNandPush(argCount + 1, cantInterpMsg);
return this.findSelectorInClass(cantInterpSel, 1, currentClass.superclass());
}
var newMethod = this.lookupSelectorInDict(mDict, selector);
if (!newMethod.isNil) {
cacheEntry.method = newMethod;
if (newMethod.isMethod()) {
cacheEntry.primIndex = newMethod.methodPrimitiveIndex();
cacheEntry.argCount = newMethod.methodNumArgs();
} else {
// if method is not actually a CompiledMethod, let primitiveInvokeObjectAsMethod (576) handle it
cacheEntry.primIndex = 576;
cacheEntry.argCount = trueArgCount;
}
cacheEntry.mClass = currentClass;
return cacheEntry;
}
currentClass = currentClass.superclass();
}
//Cound not find a normal message -- send #doesNotUnderstand:
var dnuSel = this.specialObjects[Squeak.splOb_SelectorDoesNotUnderstand];
if (selector === dnuSel) // Cannot find #doesNotUnderstand: -- unrecoverable error.
throw Error("Recursive not understood error encountered");
var dnuMsg = this.createActualMessage(selector, trueArgCount, startingClass); // The argument to doesNotUnderstand:
if (this.breakOnMessageNotUnderstood) {
var receiver = this.stackValue(argCount);
this.breakNow("Message not understood: " + receiver + " " + startingClass.className() + ">>" + selector.bytesAsString());
}
this.popNandPush(argCount, dnuMsg);
return this.findSelectorInClass(dnuSel, 1, startingClass);
},
lookupSelectorInDict: function(mDict, messageSelector) {
//Returns a method or nilObject
var dictSize = mDict.pointersSize();
var mask = (dictSize - Squeak.MethodDict_selectorStart) - 1;
var index = (mask & messageSelector.hash) + Squeak.MethodDict_selectorStart;
// If there are no nils (should always be), then stop looping on second wrap.
var hasWrapped = false;
while (true) {
var nextSelector = mDict.pointers[index];
if (nextSelector === messageSelector) {
var methArray = mDict.pointers[Squeak.MethodDict_array];