-
Notifications
You must be signed in to change notification settings - Fork 2
/
0001-Kernel-Classes.st
2192 lines (2047 loc) · 80.6 KB
/
0001-Kernel-Classes.st
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
'From Smalltalk 5.5k XM November 24 on 22 November 1980 at 2:57:08 am.'
"Object"
Class new title: 'Object'
subclassof: nil
fields: ''
declare: '';
asFollows
Object is the superclass of all classes. It is an abstract class, meaning that it has no state, and its main function is to provide a foundation message protocol for its subclasses. Three instances of this class are defined: nil, true, and false.
Comparison
≤ x [⇑self>x≡false]
≡ x [⇑self≡x "In case this is reached by perform:"] primitive: 4
≠ x [⇑self=x≡false]
≥ x [⇑self<x≡false]
= x [⇑self≡x]
and⦂ x [self⇒[⇑x eval] ⇑false]
and: x [self⇒[⇑x] ⇑false]
empty [⇑self length = 0]
eqv: x [x⇒[⇑self] ⇑self≡false]
or⦂ x [self⇒[⇑true] ⇑x eval]
or: x [self⇒[⇑true] ⇑x]
sameAs: object
[⇑self≡object]
xor: x [x⇒[⇑self≡false] ⇑self]
Classification
class [user croak] primitive: 27
is: x [⇑self class≡x]
Is: x "Is the class x a superclass or class of self"
[self class ≡ x ⇒[⇑true]
⇑self class Isa: x]
isArray
[⇑false]
isnt: x [⇑(self class≡x) ≡ false]
Isnt: x
[⇑(self Is: x)≡false]
isNumber
[⇑false]
Construction
, x | v
[v ← Vector new: 2.
v◦1 ← self. v◦2 ← x. ⇑v]
asParagraph [⇑self asString asParagraph]
asStream [⇑self asVector asStream]
asVector | v
[self≡nil⇒[⇑Vector new: 0]
v ← Vector new: 1. v◦1 ← self. ⇑v]
copy "create new copy of self"
[self is: Object⇒[⇑self]
⇑self class copy: self]
inVector | vec
["Return me as the sole element of a new Vector."
vec ← Vector new: 1.
vec◦1 ← self.
⇑vec]
recopy "recursively copy whole structure"
[self is: Object⇒[⇑self]
⇑self class recopy: self]
Aspects
asOop [user croak] primitive: 46
canunderstand: selector
[⇑self class canunderstand: selector]
error: s [⇑user notify: s]
fields
["Return an Array of all my field names or many of my subscripts."
self class is: VariableLengthClass⇒
[self length ≤ 50⇒ [⇑1 to: self length]
⇑ (1 to: 20) concat: (self length-20 to: self length)]
⇑self class instvars]
hash [user croak] primitive: 46
inspect
[user leaveTop; restartup: (InspectWindow new of: self)]
inspectfield: n "used by variable panes"
[self class is: VariableLengthClass⇒ [⇑self◦(self fields◦n)]
⇑self instfield: n]
instfield: n [user croak] primitive: 38
instfield: n ← val [user croak] primitive: 39
instfields | vec size i
["Return an Array of all my field values or many of my elements."
self class is: VariableLengthClass⇒ [⇑self◦self fields]
size ← self class instsize.
vec ← Vector new: size.
for⦂ i to: size do⦂ [vec◦i ← self instfield: i].
⇑vec]
itself
ref: index
[⇑FieldReference new object: self offset: index]
subError [self error: 'message not defined by subclass']
title
[⇑self class title + '.' + self asOop base8]
Printing
asFullString | strm
[strm ← (String new: 20) asStream.
self fullprinton: strm. ⇑strm contents]
asString | strm
[strm ← (String new: 16) asStream.
self printon: strm. ⇑strm contents]
filout | file
[⇑user displayoffwhile⦂
[file ← dp0 file: self title asFileName.
self fullprinton: file.
file close]]
fullprint | strm
[strm ← Stream default. self fullprinton: strm.
user show: strm contents]
fullprinton: strm
[self≡nil⇒ [strm append: 'nil']
self≡false⇒ [strm append: 'false']
self≡true⇒ [strm append: 'true']
self class print: self on: strm]
print
[user show: self asString]
printon: strm | t [
strm append: [self≡nil⇒ ['nil']; ≡false⇒ ['false']; ≡true⇒ ['true']
t ← self class title.
strm append: ['AEIO' has: t◦1⇒ ['an '] 'a '].
t]]
Compiler Defaults
ⓢ code
[⇑Generator new evaluate: code asStream in: false to: self notifying: self]
argsOff: stack
[self⇒ [stack pop: 1]]
asRemoteCode: generator
[⇑ParsedRemote new expr: self]
emitForEffect: code on: stack
emitForTruth: trueSkip falsity: falseSkip into: code on: stack
[self emitForValue: code on: stack.
(trueSkip jmpSize + falseSkip) emitBfp: code on: stack.
trueSkip emitJmp: code on: stack]
emitForValue: code on: stack
emitsLoad
[⇑false]
emittedReceiver
[⇑false]
emittedVariable
[⇑false]
findMacros: macros compilerTemps: compilerTemps
firstPush
[⇑¬1]
interactive
[⇑false]
isField
[⇑false]
notify: errorString at: position in: stream
[⇑self notify: errorString at: position in: stream for: self class]
notify: errorString at: position in: stream for: class | syntaxWindow
[NotifyFlag⇒
[syntaxWindow ← SyntaxWindow new of: errorString at: position in: stream for: class from: thisContext sender.
thisContext sender ← nil.
user restartup: syntaxWindow]
user notify: errorString. ⇑false]
printon: strm indent: level precedence: p forValue: v decompiler: decompiler
remote: generator
returns
[⇑false]
sizeForEffect: nextPush
[⇑0]
sizeForTruth: trueSkip falsity: falseSkip
| jump
[jump ← trueSkip jmpSize.
⇑self sizeForValue + (jump+falseSkip) bfpSize + jump]
sizeForValue
[⇑0]
System Primitives
error | sender op n args i "after compiling execute: nil installError. "
[sender ← thisContext sender.
op ← sender thisop.
n ← op numArgs.
args ← Vector new: n.
for⦂ i from: (n to: 1 by: ¬1) do⦂
[args◦i ← sender pop].
⇑self messageNotUnderstood: op withArgs: args from: sender]
installError | code old
[code ← Object md method: ↪error.
old ← SpecialOops◦1.
old asOop≠(mem◦3)⇒ [user notify: 'Object installError failed']
Top critical⦂
[mem◦3 ← code asOop.
SpecialOops◦1 ← code]]
messageNotUnderstood: op withArgs: args from: sender
[thisContext sender ← sender.
user notify: 'Message not understood: '+op]
nail [user croak] primitive: 31 "Nail me in core and return my core address"
perform: selector "Send the unary message, selector, to self"
[selector mustTake: 0. ⇑self performDangerously: selector]
perform: selector with: arg1 "Send the 1-argument message, selector, to self"
[selector mustTake: 1. ⇑self performDangerously: selector with: arg1]
perform: selector with: arg1 with: arg2 "Send the 2-argument message, selector, to self"
[selector mustTake: 2. ⇑self performDangerously: selector with: arg1 with: arg2]
perform: selector with: arg1 with: arg2 with: arg3 "Send the 3-argument message, selector, to self"
[selector mustTake: 3. ⇑self performDangerously: selector with: arg1 with: arg2 with: arg3]
perform: selector withArgs: vec
[selector mustTake: vec length.
⇑self performDangerously: selector withArgs: vec]
performDangerously: selector "Send self the message, selector; it had better be unary"
[user notify: 'can''t perform: nil'] primitive: 102
performDangerously: selector with: arg1 "selector had better take 1 arg"
[user notify: 'can''t perform: nil with:'] primitive: 102
performDangerously: selector with: arg1 with: arg2 "selector had better take 2 args"
[user notify: 'can''t perform: nil with:with:'] primitive: 102
performDangerously: selector with: arg1 with: arg2 with: arg3 "selector had better take 3 args"
[user notify: 'can''t perform: nil with:with:with:'] primitive: 102
performDangerously: selector withArgs: vec
[vec length=0⇒ [⇑self performDangerously: selector];
=1⇒ [⇑self performDangerously: selector with: vec◦1];
=2⇒ [⇑self performDangerously: selector with: vec◦1 with: vec◦2];
=3⇒ [⇑self performDangerously: selector with: vec◦1 with: vec◦2 with: vec◦3]
user notify: 'More than 3 args for perform:']
PTR [] primitive: 46
refct [user croak] primitive: 45
startup "loopless scheduling"
[self firsttime⇒
[while⦂ self eachtime do⦂ [].
⇑self lasttime]
⇑false]
swap⦂ variable | x "assign me to variable and return its old value"
[x ← variable value. variable value ← self. ⇑x]
unNail [user croak] primitive: 32 "Release me from being nailed"
SystemOrganization classify: ↪Object under: 'Kernel Classes'.
"Class"
Class new title: 'Class'
subclassof: Object
fields: 'title "<String> for identification, printing"
myinstvars "<String> partnames for compiling, printing"
instsize "<Integer> for storage management"
messagedict "<MessageDict> for communication, compiling"
classvars "<Dictionary/nil> compiler checks here"
superclass "<Class> for execution of inherited behavior"
environment "<Vector of SymbolTables> for external refs"
fieldtype'
declare: 'lastClass lastSelector lastParagraph ';
veryspecial: 1;
asFollows
Classes are the molecules of Smalltalk. The instance fields specify the number and naming of fields for each instance, and the messages define the protocol with which these objects may be communicated. Classes inherit the fields and message protocol of their superclass. Locally defined messages will override inherited ones of the same name, and overriden ones may be accessed through the use of super in place of self. A typical class definition looks like:
Class new title: 'CodeEditor';
subclassof: Window;
fields: 'pared class selector';
declare: 'editmenu'
This ordering is required, though the subclassof: and declare: messages are optional. A class definition may be re-executed but, if the fields: clause has changed, all instances of the old class will become obsolete (they will fail to respond to any messages).
Initialization
abstract
[self fields: nullString]
bytesize: n "non-pointer declaration"
[self≠self realself⇒[self realself bytesize: n]
fieldtype ← 32+ [n=8⇒ [8] 16]]
classInit "gets propagated to a dummy instance"
[self new classInit]
copyof: oldClass subclassof: newSubClass
[title ← oldClass title.
self subclassof: newSubClass.
classvars ← oldClass classvars.
environment ← oldClass environment.
self newFieldsForSubClass: oldClass myinstvars]
declare: v | var recom
[self≠self realself⇒[self realself declare: v]
[classvars≡nil⇒[classvars ← SymbolTable init]].
v is: String⇒[self declare: v asVector]
recom ← false.
[v is: Vector⇒
[for⦂ var from: v do⦂
[(Smalltalk has: var) or: (Undeclared has: var)⇒[recom ← true]]]
(Smalltalk has: v) or: (Undeclared has: v)⇒[recom ← true]].
[recom⇒
[user notify: 'Methods recompile if you proceed, global became local']].
[v is: Vector⇒
[for⦂ var from: v do⦂
[classvars insert: var with: nil]]
classvars insert: v with: nil].
recom⇒[self compileall]]
environment ← environment [] "for resetting to reread sharing clauses"
fields: myinstvars | r a b s h "list of instance variables"
[messagedict ← MessageDict init.
r ← self realself.
a ← self instvars.
h← HashSet init.
for⦂ s from: a do⦂
[h has: s⇒
[user notify: s+' is used already (maybe in superclass)']
h insert: s].
self=r⇒[self initClass]
a=(b← r instvars)⇒
[r environment← nil; myinstvars← myinstvars; subclassof: superclass]
[r howMany>0⇒[user notify: 'All '+title+'s become obsolete if you proceed...']].
classvars ← r classvars.
messagedict ← r md copy.
[a length≤b length or⦂ a◦(1 to: b length)≠b⇒ "just adding new inst fields"
[user notify: title+ ' methods recompile if you proceed...'.
self compileall]].
r md init.
self fixSubClassesOf: r.
r obsolete.
Smalltalk◦title unique ← self.
self initClass]
fixSubClassesOf: oldClass | n subClass
[for⦂ n from: user classNames do⦂
[subClass ← Smalltalk◦n.
subClass superclass≡oldClass⇒
[Class new copyof: subClass subclassof: self]]]
initClass
[fieldtype ← 16.
instsize ← self instvars length.
instsize>256⇒
[user notify: 'too many instance variables']
self organization]
myinstvars ← myinstvars
newFieldsForSubClass: myinstvars | r a b "list of instance variables"
[messagedict ← MessageDict init.
r ← self realself.
self=r⇒
[user notify: 'problem in class redefinition. See coment at end of method']
(a← self instvars)=(b← r instvars)⇒
[user notify: 'problem in class redefinition. See coment at end of method']
[r howMany>0⇒[user cr show: 'All '+title+'s are obsolete.']].
classvars ← r classvars.
messagedict ← r md copy.
r md init.
[a length≤b length or⦂ a◦(1 to: b length)≠b⇒ "changing inst fields"
[user cr show: title+ ' recompiled.'.
self compileall]].
self fixSubClassesOf: r.
r obsolete.
Smalltalk◦title unique ← self.
self initClass]
"Regarding the notifys in this method: It is my understanding
that this method will only be invoked when the conditions
leading to the notifys are false. If I'm available, I'd like to see
any case that results in notification.
Dave Robson"
obsolete "invalidate further communication"
[title ← 'AnObsolete'+title.
classvars ← nil. "recycle class variables"
messagedict close. "invalidate and recycle local messages"
environment ← self. "keep me around for old instances"
superclass ← Object. "invalidate superclass messages"]
realself [⇑Smalltalk◦title unique] "as opposed to possible filin ghost"
rename: newtitle
| name newname oldclass category
[name ← title unique. newname ← newtitle unique.
[Smalltalk has: newname⇒
[oldclass ← Smalltalk◦newname.
user notify: 'All ' + newtitle + 's will become obsolete if you proceed'.
oldclass obsolete]
category ← SystemOrganization invert: name.
AllClassNames ← AllClassNames insertSorted: newname.
SystemOrganization classify: newname under: category].
Smalltalk delete: name.
AllClassNames ← AllClassNames delete: name.
SystemOrganization delete: name.
title ← newtitle.
Smalltalk declare: newname as: self]
sharing: table
[self≠self realself⇒[self realself sharing: table]
environment ← environment asVector , table]
subclassof: superclass
[(superclass isnt: Class) and⦂ (superclass isnt: VariableLengthClass)⇒
[user notify: 'Superclass is not yet defined or not a Class']]
title: title
[self title: (title ← title unique) insystem: Smalltalk]
title: name insystem: system | cl
[superclass ← Object.
[system has: name⇒
[cl ← (system◦name) class.
cl≡self class⇒ [⇑self]
user notify: name + ' will change from a ' + cl title + ' to a ' + self class title + ' if you proceed...']].
system declare: name as: self.
AllClassNames ← AllClassNames insertSorted: name.
SystemOrganization classify: name under: 'As yet unclassified']
title: t subclassof: s fields: f declare: d
[t◦1≠((t◦1) asUppercase)⇒
[user notify: 'Please capitalize each word in class title: ' + t. ⇑false]
self title: t; subclassof: s; fields: f; declare: d]
veryspecial: n "inaccessible fields"
[instsize ← self instvars length + n]
Access to parts
◦x [⇑classvars◦x]
◦x ← val [⇑classvars◦x ← val]
fieldNamesInto: collector
[[superclass≡nil⇒ [] superclass fieldNamesInto: collector].
⇑(Reader new of: myinstvars) readInto: collector]
instsize
["Return the number of user accessable instance fields (self instvars length)."
⇑[fieldtype≥32⇒ [0]
self≡Class⇒ [instsize-1]
self≡VariableLengthClass ⇒[instsize-20]
instsize]]
instvars
[⇑self fieldNamesInto: FieldNameCollector default]
invertRef: refs "Refs may be a vector (to allow batching)"
| cl env source ref inv sym t
[refs isnt: Vector⇒ [⇑(self invert: refs inVector)◦1]
env ← (self wholeEnvironment concat: (Undeclared, Smalltalk)) asStream.
source ← Dictionary init.
⇑refs transform⦂ ref to⦂
[cl ← self. env reset.
until⦂
[(sym ← env next)≡false⇒ [inv ← 'unknown ' concat: ref asOop base8]
[cl≠nil and⦂ sym≡cl classvars⇒ [t ← cl title. cl ← cl superclass] t ← false].
(inv ← sym invertRef: ref)≡false⇒ [false]
[t⇒ []
t ← source lookup: sym⇒ []
source insert: sym with: (t ← Smalltalk invert: sym)].
inv ← (t concat: ' ') concat: inv]
do⦂ [].
inv]
]
Isa: x "is x on my superclass chain?"
[superclass ≡ x ⇒[⇑true]; ≡ nil ⇒[⇑false]
⇑superclass Isa: x]
md [⇑messagedict]
myinstvars
[⇑myinstvars]
superclass [⇑superclass]
title [⇑title]
Organization
classvars [⇑classvars]
clean | name "release unreferenced classvars"
[for⦂ name from: classvars do⦂
[name≠↪ClassOrganization and⦂ (classvars ref: name) refct=1⇒
[classvars delete: name]]]
environment
[⇑environment]
organization | o
[ [classvars ≡ nil⇒[self declare: ↪ClassOrganization]].
o ← classvars lookup: ↪ClassOrganization.
o is: ClassOrganizer⇒[⇑o]
o ← ClassOrganizer new init: messagedict contents sort.
classvars insert: ↪ClassOrganization with: o. ⇑o]
wholeEnvironment
[⇑(classvars asVector concat: environment asVector) concat:
[superclass≡nil⇒ [↪()] superclass wholeEnvironment]]
Editing
ed: selector | c s
[c← self code: selector. user clearshow: c.
while⦂ (s← user request: 'substitute: ') do⦂
[c ← c subst: s for: (user request: 'for: ').
user clearshow: c]
self understands: c]
edit: selector | para s v
[para ←
[selector=↪ClassOrganization⇒
[self organization asParagraph]
messagedict has: selector⇒[self code: selector]
nullString asParagraph].
self edit: selector para: para formerly: false]
edit: selector para: para formerly: oldpara
[user leaveTop.
user restartup: (CodeWindow new class: self selector: selector para: para formerly: oldpara)]
execute: code "disposable methods"
[self understands: 'doit [⇑' + code + ']'.
⇑self new doit]
Message access
archiveOn: file changesOnly: ch | org m [
"this should be called only by the system releaser
(via UserView file:classes:changesOnly:) !!!
if you want to archive your own classes (useful only if you have stable code
and intend to clean up afterwards with a vmem write), see Steve.
write comment and method text on a FileStream for some file.
ch⇒ [write only changes (non-remote String/Paraagraphs)] write everything"
user cr; show: title.
org ← self organization.
["org globalComment always yields a String, so a small kludge is in order"
ch and⦂ (org globalCommentItself is: RemoteParagraph)⇒ []
org globalComment ←
(RemoteParagraph new on: file) fromString: org globalComment].
"archive in category&alphabetical rather than hash order (messagedict)"
for⦂ m from: org do⦂ [
ch and⦂ ((messagedict code: m) is: RemoteParagraph)⇒ []
messagedict code: m ←
(RemoteParagraph new on: file) fromParagraph: (self code: m).
ch⇒ [user space; show: m]]]
bytesof: sel
[⇑(messagedict method: sel) asBytes]
canUnderstand: selector
[messagedict has: selector⇒ [⇑self]
superclass≡nil⇒ [⇑false]
⇑superclass canUnderstand: selector]
canunderstand: selector
[⇑messagedict has: selector]
code: sel [
"last paragraph returned is cached (mainly for NotifyWindows)"
[sel ≡ lastSelector and⦂ self ≡ lastClass ⇒ []
lastParagraph ← ([
sel = ↪ClassOrganization ⇒ [self organization]
"if left shift key is down, decompile"
user leftShiftKey⇒ [self decompile: sel]
"Paragraph or RemoteParagraph"
messagedict code: sel]) asParagraph.
lastClass ← self.
lastSelector ← sel].
⇑lastParagraph]
compileall | s c "does not modify code, just compiles it"
[for⦂ s from: messagedict do⦂
[c ← messagedict code: s.
self understands: c asParagraph.
messagedict code: s ← c "leave it as a remote paragraph"].
self≡Object⇒[nil installError]]
"to recompile the whole system (check out big changes) execute:
| n [for⦂ n from: AllClassNames do⦂
[user show: n; cr. (Smalltalk◦n) compileall.
Changes init. MessageDict new freeMethods]] "
copy: sel from: class
[self copy: sel from: class classified: nil]
copy: sel from: class classified: cat "Useful when modifying an existing class"
| s code
[sel is: Vector⇒ [for⦂ s from: sel do⦂ [self copy: s from: class classified: cat]]
sel is: String⇒ [self copy: (class organization category: sel) from: class classified: cat]
code ← class code: sel. code≡nil⇒ []
[cat≡nil⇒ [cat ← class organization invert: sel]].
[messagedict has: sel⇒
[code text=(self code: sel) text⇒ []
user notify: title+' '+sel+' will be redefined if you proceed.']].
self understands: code classified: cat]
decompile: t1
[⇑user displayoffwhile⦂ [Decompiler new decompile: t1 class: self]]
derstands: selector | c "overstands? undersits? - forget it"
[selector is: Vector⇒[for⦂ c from: selector do⦂ [self derstands: c]]
(messagedict has: selector)≡false⇒[]
messagedict ← messagedict delete: selector.
self organization delete: selector.
lastClass ← lastSelector ← lastParagraph ← nil.
[Changes has: (c←title+' '+selector)⇒ [Changes delete: c]].
Changes insert: (c←'~'+c).
⇑c]
describe: method on: strm | sel cls "append mclass and selector"
[cls ← self.
until⦂ [cls≡nil⇒ [cls←self. sel←↪?] sel ← cls md invert: method] do⦂
[cls ← cls superclass].
strm append: cls title; space; append: sel]
install: name method: method literals: literals
code: code backpointers: backpointers | c
[messagedict ← messagedict insert: name method: method
literals: literals code: code makeBoldPattern backpointers: backpointers.
lastClass ← self.
lastSelector ← name.
lastParagraph ← code.
Changes insert: (c←title+' '+name).
Changes has: (c←'~'+c)⇒[Changes delete: c]]
messages [⇑messagedict contents , ↪ClassOrganization]
method: sel
[⇑messagedict methodorfalse: sel]
notify: errorString at: position in: stream
[⇑self notify: errorString at: position in: stream for: self]
selectors "Return a Vector of all my selectors."
[⇑self messages]
shrink [messagedict ← messagedict shrink]
space | a s
[s ← 0. for⦂ a from: messagedict do⦂
[s ← s + (messagedict method: a) length]
⇑s]
textLocal | s [
"makes comment and methods local"
s ← self organization.
s globalComment ← s globalComment.
for⦂ s from: messagedict do⦂ [messagedict code: s ← self code: s]]
understands: code | selector old "install method"
[⇑self understands: code classified: 'As yet unclassified']
understands: code classified: heading "compile and install method"
[⇑Generator new compile: code asParagraph
in: self under: heading notifying: self]
whosends: selector | s l a
[s ← Stream default.
for⦂ a from: messagedict do⦂
[for⦂ l from: (messagedict literals: a) do⦂
[selector≡l⇒[s append: a; space]]]
⇑s contents]
Instance access
allInstances [⇑self allInstancesEver notNil]
allInstancesEver | indx vec PCLs i "returns a vector containing all instances of this class mixed with nils"
["Works for all classes. Some additional instances may be created after the
vector is filled but before you get to use it."
PCLs ← Vmem pclassesOf: self. "vector of PCLs"
vec ← Vector new: 128*PCLs length.
for⦂ i to: PCLs length do⦂
[(vec◦[i-1*128+1 to: i*128]) all← PCLs◦i].
thisContext destroyAndReturn: (self fromFreelist: Class instsize fill: vec)]
copy: inst | t i
[t ← self new.
for⦂ i to: self instsize do⦂
[t instfield: i ← inst instfield: i]
⇑t]
default
[⇑self new default]
fromFreelist: i fill: vec "i = zero order index of freelist in class instance.
vec = vector in pclasses of all possible instances."
[user croak] primitive: 60
howMany | v "how many instances of this class are in use now?"
[v ← self allInstancesEver.
thisContext destroyAndReturn: v length-(v count: nil)]
init "init and default get propagated to instances"
[⇑self new init]
init: n "init and default get propagated to instances"
[⇑self new init: n]
instfield: i "prevent user from getting freelist"
[i > Class instsize ⇒[user notify: 'arg too big']
⇑super instfield: i]
new [user croak] primitive: 28
new: length "To allow fixed-length classes to simulate variable-length ones"
[⇑self new init: length] "By convention"
print: inst on: strm | ivars i
[ivars ← self instvars.
strm append: '('; append: title; append: ' new '.
for⦂ i to: instsize do⦂
[strm append: ivars◦i; append: ': ';
print: (inst instfield: i); space]
strm append: ')']
printon: strm
[strm append: 'Class ' + title]
recopy: inst | t i
[t ← self new.
for⦂ i to: self instsize do⦂
[t instfield: i ← (inst instfield: i) recopy]
⇑t]
Filin and Filout
asFollows | s heading selector p [
self≠self realself⇒[self realself asFollows]
heading ← 'As yet unclassified'.
"handles Bravo or Press (Smalltalk generated) files"
while⦂ ((p ← FilinSource nextParagraph) and⦂ (s ← p text) ≠ '') do⦂ [
[s◦1 = 015⇒ [
"throw away initial cr before comment and headings"
s ← s copy: 2 to: s length]].
p runs◦2
= 2 "italic"⇒ [self organization globalComment ← s];
= 0121 "5, bold"⇒ [heading ← s]
self canunderstand: (
selector ← self understands: p classified: heading)⇒ [
user show: selector; space. messagedict purge: selector]
user show: '(an uncompiled method) ']] 7Bf0
changelist: cat [⇑title unique, (self organization category: cat)]
definition | strm "return a string that defines me (Class new title etc.)"
[strm ← (String new: 50) asStream.
self printdefon: strm.
⇑strm contents]
endCategoryOn: pstrm
endChangesOn: pstrm
[pstrm print: '' asParagraph]
filout [user displayoffwhile⦂ [
(dp0 file: title+'.st.') filoutclass: self.
self noChanges]]
filoutCategory: cat
[(dp0 file: (title+'-'+cat+'.st') asFileName) filout: (self changelist: cat)]
filoutOrganization "So we can merge separate work on organization"
[user show: title; cr.
user displayoffwhile⦂
[(dp0 file: title+'.org.')
append: title+' organization fromParagraph:'; cr;
append: self organization asParagraph text asString;
append: 'asParagraph'; close]]
noChanges | s t
[t← title+' *'.
for⦂ s from: Changes contents do⦂
[(s◦1=126 "~" and⦂ (t match: s◦(2 to: s length)))
or⦂ (t match: s)⇒
[Changes delete: s]]]
paraprinton: strm "Strm is actually a ParagraphPrinter"
| para frame s heading org
[para ← ('"'+title+'"') asParagraph.
para maskrunsunder: 0361 to: 0121. "Font ← 5, Bold"
frame ← strm defaultframe.
strm frame ← 15000⌾frame origin y rect: 20000⌾frame corner y.
strm print: para.
strm frame ← frame.
strm print: ((self definition+';
asFollows') asParagraph maskrunsunder: 0361 to: 0121).
org ← self organization.
strm print: ('
'+org globalComment) asParagraph allItalic.
for⦂ heading from: org categories do⦂
[self printCategory: heading on: strm]
self endChangesOn: strm.
strm print: ('SystemOrganization classify: ↪'+title+' under: '''+
(SystemOrganization invert: title unique)+'''.') asParagraph.
[self ≡ Class or: self ≡ VariableLengthClass⇒ [] self canunderstand: ↪classInit⇒
[strm print: (title+' classInit') asParagraph]].
]
printCategory: s on: pstrm | sel
[self startCategory: s on: pstrm.
for⦂ sel from: (self organization category: s) do⦂
[self printMethod: sel on: pstrm].
self endCategoryOn: pstrm]
printdefon: strm | s "print my definition on strm"
[strm append: self class title;
append: ' new title: ';
"title is probably unique, but make sure. then we want it as a 'String' "
print: title unique asString.
strm cr; tab; append: 'subclassof: ';
append: [superclass≡nil⇒['nil'] superclass title].
strm cr; tab; append: 'fields: '; print: myinstvars.
strm cr; tab; append: 'declare: '''.
for⦂ s from: classvars contents do⦂
[s=↪ClassOrganization⇒[]
strm append: s; space]
strm append: ''''.
[fieldtype=16⇒[]
strm semicrtab; append: 'bytesize: '; print: fieldtype-32].
[instsize = (s← self instvars) length⇒[]
strm semicrtab; append: 'veryspecial: '; print: instsize-s length].
[environment≡nil⇒[]
for⦂ s from: environment do⦂
[strm semicrtab; append: 'sharing: '; append: (Smalltalk invert: s)]]]
printMethod: sel on: pstrm
[pstrm print: (self code: sel).
messagedict purge: sel]
printout [user displayoffwhile⦂ [
(dp0 file: title+'.press.') printoutclass: self]]
printoutCategory: cat
[(dp0 file: (title+'-'+cat+'.press') asFileName) printout: (self changelist: cat)]
readfrom: strm [⇑self readfrom: strm format: nil]
readfrom: strm format: f [
⇑self new readfrom: strm format: f]
startCategory: s on: pstrm
[pstrm print: (('
'+s) asParagraph maskrunsunder: 0361 to: 0121). "Font 5, Bold"
]
startChangesOn: pstrm
[pstrm print: (('
'+title+' asFollows') asParagraph maskrunsunder: 0361 to: 0121). "Font 5, Bold"
]
System Organization
category [⇑SystemOrganization invert: self title unique]
category: cat
[cat is: String ⇒[SystemOrganization add: self title unique under: cat]
user notify: 'Category name must be a String']
moveFromCat: cat1 to: cat2
[(cat1 is: String) and⦂ (cat2 is: String) ⇒
[SystemOrganization move: self title unique from: cat1 to: cat2]
user notify: 'Category name must be a String']
SystemOrganization classify: ↪Class under: 'Kernel Classes'.
"Context"
Class new title: 'Context'
subclassof: Object
fields: 'sender "<Context> from which this message was sent"
receiver "<Object> to which this message was sent"
keep "<true, nil> nil means reclaimable"
method "<String>, the encoded method"
tempframe "<Vector> to hold temporaries and a stack"
pc "<Integer> marks progress of execution in method"
stackptr "<Integer> offset of stack top in tempframe"'
declare: 'arrayFld positionFld limitFld ';
asFollows
A context keeps track of the progress of a method
Initialization
cleancopy
[⇑Context new
sender: sender
receiver: receiver
method: method
tempframe: tempframe copy
pc: pc
stackptr: stackptr]
copy
[
⇑ Context new
sender: sender
receiver: receiver
method: method
tempframe: tempframe
pc: pc
stackptr: stackptr
]
sender: sender receiver: receiver
method: method tempframe: tempframe pc: pc stackptr: stackptr
Access to parts
caller [⇑sender]
getPT: i [ ⇑ tempframe◦i ]
mclass | selector mclass "return the class in which method was found"
[sender ≡ nil ⇒ [⇑receiver class]
selector ← self selector.
mclass ← receiver class.
until⦂ mclass ≡ nil do⦂
[(mclass method: selector) ≡ method ⇒ [⇑mclass]
mclass ← mclass superclass].
⇑receiver class]
method
[⇑method]
pc [⇑pc]
receiver
[⇑receiver]
selector | mclass selector "return the selector for my method"
[selector ← sender thisop.
selector◦(1~19)='performDangerously:'⇒
[mclass ← receiver class. "special work for perform"
until⦂ [mclass ≡ nil or⦂ (selector←mclass md invert: method)] do⦂
[mclass ← mclass superclass]
selector≡false⇒[⇑↪confused] ⇑selector]
⇑selector]
sender [⇑sender]
sender← sender []
setPT: i to: n [ tempframe◦i ← n ]
stackIndex "Return the subscript in tempframe of my top of stack."
[⇑stackptr+1]
swapSender: coroutine | oldSender
[oldSender ← sender. sender ← coroutine. ⇑oldSender]
tempframe
[⇑tempframe]
totalPT [⇑ (method◦5)+1 ]
Control structures
for⦂ var1 from: expr1 with⦂ var2 from: expr2 do⦂ stmt | s1 s2
[s1 ← expr1 asStream. s2 ← expr2 asStream.
while⦂ [(var1 value ← s1 next) and⦂ (var2 value ← s2 next)] do⦂ stmt eval]
Debugging
debug | t v
[self print.
while⦂ [user cr. t ← user request: '*'] do⦂ "until ctrl-d"
[v ← Generator new evaluate: t asStream in: false to: self notifying: nil.
↪debugret=v⇒[self print] v print]
⇑↪debugret]
printon: strm | mc
["Print the selector which invoked this Context
and the class in which code was found for that selector"
mc ← self mclass.
strm append: mc title. sender≡nil⇒ []
[receiver is: mc⇒[] strm append: '('+receiver class title+')'].
strm append: '⇒'; print: self selector]
restartWith: method
[tempframe ← tempframe copy: 1 to: method◦3.
self restart]
stack | a strm
["Return a Vector of me and all my derivative contexts."
strm ← (Vector new: 20) asStream.
strm next ← a ← self.
"when user notifty is fixed, a sender can become a caller"
until⦂ (a←a sender)≡nil do⦂ [strm next ← a].
⇑strm contents.]
thisop | a "return the message selector just sent"
[a ← method◦pc.
a≥0320⇒ [⇑self litof: a-0320]
a≥0260⇒ [⇑SpecialOops◦(10+a-0260)]
method◦(pc-1)=0214⇒ [⇑self litof: a]
⇑↪confused]
trace | strm a
[strm ← Stream default. self printon: strm.
a ← sender. until⦂ a≡nil do⦂
[strm cr. a printon: strm. a ← a sender]
⇑strm contents]
variableNamesInto: dest with: block | class selector parser
["For each method variable name, call
dest declaration: block name: string asArg: <true or false>
If cant find source code, call dest notify: "
class ← self mclass.
selector ← class md invert: method⇒
[parser ← Parser new from: (class code: selector) asStream to: dest.
parser pattern: block; temporaries: block; terminate]
dest notify: 'thisContext is not running a currently defined method']
verifyFrames | c "be sure frames on stack aren't nil"
[c ← self.
until⦂ c≡nil do⦂
[c tempframe≡nil⇒
[user notify: 'Sorry, that stack has been released -- proceeding is impossible'; restart]
c ← c sender]]
Simulation
docode: code toclass: class | i v
[[(i←code◦2)≠ 0 ⇒
[i = 1 ⇒ [⇑self];
= 30 ⇒ [v ← self pop. v push: self. ⇑v];
= 40 ⇒ [v ← self pop instfield: code◦5 + 1. ⇑self push: v];
= 41 ⇒ [user notify: 'Field← primitive unimplemented'];
= 87 ⇒ [⇑ receiver "PriorityInterrupt run: newContext"];
= 101 ⇒ [user notify: 'Doprimitive unimplemented'];
= 102 ⇒ [⇑self performing: code◦4 toclass: tempframe◦(stackptr+1)]
(v ← self doprimitive: code) ≡ ↪failed ⇒[]
stackptr ← stackptr-(code◦4+1). ⇑self push: v]].
⇑self newToRun: code]
dojump: displacement
[pc ← pc+displacement]
dopop
[stackptr ← stackptr-1]
doprimitive: code
[⇑↪failed] primitive: 101
doremotereturn | t f
[t ← self pop. f ← self pop. ⇑f push: t]
doreturn | t
[t ← tempframe◦(stackptr+1). tempframe ← nil. ⇑sender push: t]
dostore | byte
[byte ← method◦(pc← pc+1).
byte<020⇒ [self smashField: byte];
<040⇒ [self smashTemp: byte-020];
<0100⇒ [user notify: 'Store into literal'];
<0160⇒ [self smashLitInd: byte-0100];
<0170⇒ [self instfield: (byte-0157) ← tempframe◦(stackptr+1)];
=0210⇒ [self smashField: self nextByte];
=0211⇒ [self smashTemp: self nextByte];
=0213⇒ [self smashLitInd: self nextByte]
user notify: 'Illegal store'
]
dosuper | byte
[byte ← self nextByte.
[byte=0214⇒ [byte ← self nextByte+0320]].
byte<0260⇒ [user notify: 'non-selector after super']
⇑self sendmess: nil byte: byte toclass:
(self litof: method◦6-8/2) value superclass]
doUnique | r e "cause ◦ ← to be done for a UniqueString inside str: inside intern:"
[r ← tempframe◦(2+stackptr). "receiver"
"already know class is UniqueString"
e ← 'bad special message in super'.
(method ≡ (UniqueString md method: ↪str:)) ≡ false ⇒[user notify: e]
"ours, now put result of str: on the stack"
self push: (r ← r str: tempframe◦1 "arg").
method◦(method length) ≠ 0203 "return" ⇒[user notify: e]
"trick this method into returning"
pc ← method length -1.
⇑self]
help
["Here is how to use the Smalltalk simulator.
thisContext runsimulated⦂ [ place here the code you to simulate ].
Some classes in Smalltalk may not be subclassed. Some messages
may not be overridden. initSimulator tells the details. "]
initSimulator | i "these class variables of Context are used by instfield: to simulate what the microcode does to objects."
[" Context declare: 'positionFld arrayFld limitFld'. "
"Rules: There may not be a subclass of Integer.
(This is so +, -, <, >, ≤, ≥, =, ≠ may not be overridden).
The following messages may NOT be redefined by any class.
≡ (from class Object).
class (from class Object).
The following messages may NOT be redefined by any VariableLengthClass.
length (from Vector, String)"
i ← Stream instvars. "We need to peek inside instances of Stream"
positionFld ← i find: 'position'.
arrayFld ← i find: 'array'.
limitFld ← i find: 'limit'.
]
litof: a
[⇑(method word: a+4) asObject]
newToRun: code | r v i
[r ← self pop. v ← Vector new: code◦3.
for⦂ i from: (code◦4 to: 1 by: ¬1) do⦂ "Move args to new tframe"
[v◦i ← tempframe◦(2+(stackptr ← stackptr-1)).
tempframe◦(stackptr+2) ← nil "Nil args on caller's stack"
].
⇑self class new sender: self receiver: r method: code
tempframe: v pc: code◦6 stackptr: code◦5 - 1. "Allocate a new Context"
]
nextByte
[⇑method◦(pc ← pc+1)]
nonEmptyStack
[⇑(stackptr≠¬1)]
performing: nargs toclass: class | i sel
[i ← stackptr-nargs.
sel ← tempframe◦(i+1). "Selector is first arg"
while⦂ i<stackptr do⦂
[tempframe◦(i+1) ← tempframe◦(i+2).
i ← i+1].
stackptr ← stackptr-1.
⇑self sendmess: sel byte: 0320 toclass: class]
pop
[⇑tempframe◦(2+ (stackptr← stackptr-1))]
push: n "Push n on top of stack"
[tempframe◦(1+(stackptr←stackptr+1)) ← n]
pushField: i
[tempframe◦(1+(stackptr←stackptr+1)) ← receiver instfield: i+1]
pushLit: i
[tempframe◦(1+(stackptr←stackptr+1)) ← (method word: i+4) asObject]
pushLitInd: i
[tempframe◦(1+(stackptr←stackptr+1)) ← (method word: i+4) asObject value]
pushTemp: i
[tempframe◦(1+(stackptr←stackptr+1)) ← tempframe◦(i+1)]