-
Notifications
You must be signed in to change notification settings - Fork 11
/
ActivationMirrors.ns
913 lines (884 loc) · 33.1 KB
/
ActivationMirrors.ns
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
Newspeak3
'Mirrors'
class ActivationMirrors usingPlatform: platform vmMirror: vmm = (
(* Mirrors for method activations and processes.
Copyright (c) 2009 Peter von der Ahe
Copyright (c) 2011 Gilad Bracha, Ryan Macnak and Cadence Design Systems
Copyright (c) 2012 Cadence Design Systems
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. *)
|
private OutOfScopeNotification = platform squeak OutOfScopeNotification.
private SyntaxErrorNotification = platform squeak SyntaxErrorNotification.
private Project = platform squeak Project.
private DebuggerMethodMap = platform squeak DebuggerMethodMap.
private Map = platform collections Map.
private vmmirror = vmm.
private mirrors = platform mirrors.
private ObjectMirror = platform mirrors ObjectMirror.
private MixinBuilder = platform mirrors MixinBuilder.
private MixinMirror = platform mirrors MixinMirror.
private MethodMirror = platform mirrors MethodMirror.
|) (
class ActivationMirror onContext: ctxt <MethodContext> = (|
public (*BOGUS*) context_slot <MethodContext> = ctxt.
public (*BOGUS*) sender_slot <ActivationMirror>
protected home_slot <ActivationMirror>
protected localNames_slot <List[Symbol]>
protected methodMirror_slot <MethodMirror>
|#ACCESSBOGUS) (
public = anotherMirror = (
(anotherMirror isKindOfActivationMirror)
ifFalse: [ ^false].
^context_slot == anotherMirror context_slot.
)
public activationsDo: action <[:ActivationMirror]> = (
| currentMirror |
currentMirror:: self.
[currentMirror isNil] whileFalse:
[action value: currentMirror.
currentMirror:: currentMirror sender]
)
public activationsUpTo: activation <ActivationMirror> = (
(* Answer an Array of activations up to, but not including, activationOrNil *)
^Array streamContents:
[:s | | current |
current:: self.
[s nextPut: current.
activation isSenderOf: current] whileFalse:
[current:: current sender]]
)
bciForHighlightingIn: thread <ThreadMirror> ^ <Integer|nil> = (
^(pcForHighlightingIn: thread) - homeMethod initialPC + 1
)
public compile: newSource ifFail: onFail ^<MethodMirror> = (
self subclassResponsibility.
)
public errorReportOn: stream = (
context_slot errorReportOn: stream
)
public evaluate: expression <String>
ifCompilerError: compilationErrorHandler <[:Error]>
ifError: evalErrorHandler <[:Error]> ^<ObjectMirror> = (
subclassResponsibility
)
public evaluate: expression <String>
with: scope <Map[Symbol, ObjectMirror]>
ifCompilerError: compilationErrorHandler <[:Error]>
ifError: evalErrorHandler <[:Error]> ^<ObjectMirror> = (
(* Most languages don't need extra blackMarket during debugging *)
^self evaluate: expression asString
ifCompilerError: compilationErrorHandler
ifError: evalErrorHandler
)
public hasActivation: activation <ActivationMirror> ^ <Boolean> = (
^context_slot hasSender: activation context_slot
)
public hash = (
^context_slot hash.
)
public home ^<ActivationMirror> = (
^isBlockActivation
ifTrue:
[home_slot ifNil:
[context_slot home ifNotNil:
[:it | home_slot:: class onContext: it]].
home_slot]
ifFalse:
[self]
)
public homeInThread ^ <ActivationMirror> = (
isBlockActivation ifFalse: [^self].
home ifNotNil:
[:homeMirror <ActivationMirror> |
(hasActivation: homeMirror) ifTrue:
[activationsDo:
[:each <ActivationMirror> |
each context_slot == homeMirror context_slot ifTrue: [^each]]]].
^nil
)
public homeMethod ^ <CompiledMethod> = (
^home ifNotNil:
[:it |
self == it
ifTrue: [context_slot method]
ifFalse: [it homeMethod]]
)
public isBlockActivation ^<Boolean> = (
^context_slot closure notNil
)
public isKindOfActivationMirror = (
^true
)
private isMessageNotUnderstood ^ <Boolean> = (
^context_slot selector = #doesNotUnderstand:
)
public isSenderOf: activation <ActivationMirror> ^ <Boolean> = (
^(self == activation sender_slot)
or: [context_slot == activation context_slot sender]
)
localNames ^ <List[String]> = (
subclassResponsibility
)
public localNamesWithValuesDo: action <[:String :ObjectMirror]> = (
localNames doWithIndex:
[:localName :i |
action
value: localName
value: (ObjectMirror reflecting: ([home context_slot at: i] ifError: ['<error getting value>']))]
)
public localVariableValueMirror: name <String> = (
| index |
index:: localNames indexOf: name.
^ObjectMirror reflecting: ([home context_slot at: index] ifError: ['<error getting value>'])
)
public method ^<MethodMirror> = (
methodMirror_slot ifNil: [methodMirror_slot:: MethodMirror reflecting: homeMethod].
^methodMirror_slot
)
public notUnderstoodArguments = (
^(context_slot tempAt: 1) arguments
)
public notUnderstoodMessage ^ <Symbol> = (
^(context_slot tempAt: 1) selector
)
pc = (
^context_slot pc
)
pcForHighlightingIn: thread <ThreadMirror> ^ <Integer|nil> = (
(* The pc to use for highlighting depends on whther the context is the active (top) contex or not.
The top context's pc is its effective pc. But other contexts' pcs are those following a send,
since they are suspended after a send. Hence their highlighting pc is the pc previous to the current pc. *)
^context_slot pc ifNotNil:
[:ctxtpc |
(thread isActiveContext: context_slot)
ifTrue: [ctxtpc]
ifFalse: [context_slot method pcPreviousTo: ctxtpc]]
)
public printOn: stream = (
isBlockActivation
ifTrue:
[home ifNotNil:
[:it |
stream nextPutAll: '[] in '.
it printOn: stream.
^self].
stream nextPutAll: '<<Orphaned block>>']
ifFalse:
[homeMethod ifNotNil:
[:it | | receiverMixin methodMixin |
receiverMixin:: receiver getClass mixin.
stream nextPutAll: receiverMixin simpleName.
methodMixin:: method definingMixin.
(receiverMixin = methodMixin)
ifFalse:
[stream nextPut: "(".
stream nextPutAll: methodMixin simpleName.
stream nextPut: ")"].
stream space.
stream nextPutAll: (it selector ifNil: [it defaultSelector]).
isMessageNotUnderstood ifTrue:
[stream space.
notUnderstoodMessage printOn: stream].
^self].
stream nextPutAll: '<<Anonymous method>>']
)
public receiver ^<ObjectMirror> = (
^ObjectMirror reflecting: context_slot receiver
)
public selector ^<Symbol> = (
^context_slot selector
)
public selectorForParsing = (
^#methodDecl
)
public sender ^<ActivationMirror> = (
sender_slot ifNil:
[context_slot sender ifNotNil:
[:it | sender_slot:: (ActivationMirror reflecting: it)]].
^sender_slot
)
public setLocalVariable: variableName <String> toExpression: exprString <String> ifFail: onFail <[:String]> ^<Object> = (
| index newValue <ObjectMirror> |
#BOGUS. (* Should take an ObjectMirror for the new value and call should choose with version of evaluate. *)
index:: localNames indexOf: variableName ifAbsent:
[variableName asInteger asString = variableName asString
ifTrue: [variableName asInteger + localNames size]].
(index isNil or: [index < 1 or: [index > stackDepth]])
ifTrue: [^onFail valueWithPossibleArgument: 'Not found: ', variableName].
newValue:: self
evaluate: exprString
ifCompilerError: [:e | ^onFail cull: e description]
ifError: [:e | ^onFail cull: e description].
home context_slot at: index put: newValue reflectee.
)
public setLocalVariable: variableName <String> toValue: newValue ifFail: onFail <[:String]> ^<Object> = (
| index |
index:: localNames indexOf: variableName ifAbsent:
[variableName asInteger asString = variableName asString
ifTrue: [variableName asInteger + localNames size]].
(index isNil or: [index < 1 or: [index > stackDepth]])
ifTrue: [^onFail valueWithPossibleArgument: 'Not found: ', variableName].
home context_slot at: index put: newValue.
)
public sourceAvailable ^<Boolean> = (
#ACCESSBOGUS. (* We should have decided not to colorize earlier when the method gave us nil source. *)
^(homeMethod properties includesKey: #source) or: [homeMethod trailer hasSourcePointer]
)
public sourceRangeIn: thread <ThreadMirror> ^ <Interval> = (
subclassResponsibility
)
stackDepth ^ <Integer> = (
isBlockActivation ifTrue: [^context_slot size].
^home context_slot size
)
public stackIndicesWithValuesDo: action <[:Integer :ObjectMirror]> = (
| tempFrameSize |
tempFrameSize: localNames size.
stackDepth to: tempFrameSize + 1 by: -1 do:
[:i |
action
value: i - localNames size
value: (ObjectMirror reflecting: (context_slot at: i))]
)
public withAllActivations = (
^Array streamContents:
[:s | activationsDo:
[:each | s nextPut: each]].
)
) : (
public current = (
^ActivationMirror reflecting: vmmirror currentActivation sender
)
public reflecting: c <ContexPart> ^ <Instance> = (
^c methodClass language activationMirrorFor: c using: outer ActivationMirrors
)
)
public class ActivationMirrorCompiledByNewspeak onContext: c <ContextPart> = ActivationMirror onContext: c (
(* A mirror on activations whose code was produced by the Newspeak compiler (NS3 and above). *)
|
private debugInfo_slot
private debugMapper_slot
|) (
public compile: newSource ifFail: onFail ^<MethodMirror> = (
| builder newSelector declMirror mixinMirror |
builder:: MixinBuilder reflecting: method definingMixin reflectee.
newSelector:: (builder methods addFromSource: newSource) simpleName.
declMirror:: builder declaration install.
mixinMirror:: builder isMeta
ifTrue: [declMirror classSide]
ifFalse: [declMirror instanceSide].
^mixinMirror methods findMirrorNamed: newSelector.
)
debugInfo ^<DebugInfo> = (
(* This can only handle regular methods and do-its. It does not handle factories and other synthetic methods. *)
debugInfo_slot ifNil: [
| imethod <IntermediateMethod> |
(homeMethod properties includesKey: #debugInfo)
ifTrue: [^debugInfo_slot:: homeMethod properties at: #debugInfo].
imethod:: mirrors compiler
compileMethodSource: method source within: method definingMixin.
debugInfo_slot:: imethod debugInfo.
].
^debugInfo_slot
)
debugMapper = (
debugMapper_slot ifNil: [
debugMapper_slot:: debugInfo mapperForContext: context_slot
].
^debugMapper_slot
)
public evaluate: expression <String>
ifCompilerError: compilationErrorHandler <[:Error]>
ifError: evalErrorHandler <[:Error]> ^<ObjectMirror> = (
^self evaluate: expression with: Map new ifCompilerError: compilationErrorHandler ifError: evalErrorHandler
)
public evaluate: expression <String>
with: scope <Map[Symbol, ObjectMirror]>
ifCompilerError: compilationErrorHandler <[:Error]>
ifError: evalErrorHandler <[:Error]> ^<ObjectMirror> = (
(* Evaluate [expression] with the reflectee as the receiver. Names in scope in the current activation will be available, with the exception that from a closure activation any setters for temporaries not written to in that closure will only update the value seen in that closure, not all enclosing contexts where that temporary is in scope. Allow [`] as an identifier, bound to [blackMarket]. If [expression] cannot be compiled, activate [compilationErrorHandler] with the exception. If the evaluation of [expression] raises an error, activate [evalErrorHandler] with the error. Otherwise return an ObjectMirror on the result of [expression]. *)
| rawScope mxnMirror mtdMirror result |
rawScope:: Map new.
scope keysAndValuesDo: [:key :value | rawScope at: key put: value reflectee].
[mxnMirror:: method definingMixin. (* Super, outer and implicit receiver sends should behave like the expression is in the method class, not the receiver class. *)
mtdMirror:: mirrors compiler
compileExpressionSource: expression asString
debugMapper: debugMapper
withScope: rawScope
inMixin: mxnMirror.
mtdMirror category: #DoIts.
mtdMirror pragmas: {#isExpression -> true}.
mtdMirror methodMixin: mxnMirror reflectee.
mtdMirror compiledMethod properties
at: #source put: expression;
at: #debugInfo put: mtdMirror debugInfo]
on: Error
do: [:e | ^compilationErrorHandler cull: e ].
[result:: vmmirror
object: context_slot receiver
executeMethod: mtdMirror compiledMethod
with: {context_slot. rawScope}
ifFail: [ ^compilationErrorHandler cull: (Error description: 'primitiveExecuteMethod failed') ]]
on: Error
do: [:e | ^evalErrorHandler cull: e].
^mirrors ObjectMirror reflecting: result
)
public isInitializer ^ <Boolean> = (
^method isInitializer
)
localNames ^ <List[String]> = (
sourceAvailable ifFalse: [^{}].
localNames_slot ifNil: [
localNames_slot:: debugMapper localNames
].
^localNames_slot
)
localNamesWithIndicesDo: action <[:String :Integer]> = (
localNames doWithIndex: [:localName :index | action value: localName value: index].
)
public localNamesWithValuesDo: action <[:String :ObjectMirror]> = (
localNames do: [:ea | action value: ea value:
(ObjectMirror reflecting: ([debugMapper getValueOf: ea]
ifError: ['<error getting value>']))].
)
public localVariableValueMirror: name <String> = (
^ObjectMirror reflecting: (debugMapper getValueOf: name)
)
localVariableValueMirrorForIndex: desiredIndex <Number> = (
localNames doWithIndex:
[:localName :index |
index = desiredIndex ifTrue:
[^ObjectMirror reflecting: (debugMapper getValueOf: localName)]]
)
public sourceRangeIn: thread <ThreadMirror> ^ <Interval> = (
sourceAvailable ifFalse: [^nil].
^debugInfo bciSourceMapping at: (bciForHighlightingIn: thread) ifAbsent: [nil].
)
public stackIndicesWithValuesDo: action <[:Integer :ObjectMirror]> = (
| tempFrameSize |
#BOGUS yourself.
(* Need to specialize. The operand stack isn't simply after the number of locals in scope because remote temps don't take one slot each on the stacks of nested-closures. *)
tempFrameSize: localNames size (* + hidden itvs, setters, pushnils *).
stackDepth to: tempFrameSize + 1 by: -1 do:
[:i |
action
value: i - tempFrameSize
value: (ObjectMirror reflecting: (context_slot at: i))]
)
) : (
)
public class ActivationMirrorCompiledBySqueak onContext: ctxt = ActivationMirror onContext: ctxt (
(* A mirror that operates on activations whose code was compiled by the Squeak back-end. *)
|
protected methodMap_slot <DebuggerMethodMap>
|) (
public compile: newSource ifFail: onFail ^<MethodMirror> = (
| klass newSelector newCM |
klass:: method reflectee methodClass.
newSelector:: klass
compile: newSource
classified: 'as-yet-unclassified'
notifying: nil.
newCM:: klass compiledMethodAt: newSelector.
^MethodMirror reflecting: newCM
)
public evaluate: expression <String>
ifCompilerError: compilationErrorHandler <[:Error]>
ifError: evalErrorHandler <[:Error]> ^<ObjectMirror> = (
[
| newValue |
newValue:: method reflectee methodClass language compiler new
evaluate: expression asString
in: context_slot
to: receiver reflectee
notifying: (* requestor *) nil
ifFail: [^compilationErrorHandler valueWithPossibleArgument: 'Error'].
^ObjectMirror reflecting: newValue
] on: SyntaxErrorNotification do: [:ex | ^compilationErrorHandler cull: ex]
on: OutOfScopeNotification do: [:ex | ex resume: true]
)
localNames ^ <SequenceableCollection[String]> = (
localNames_slot ifNil: [
(* localNames_slot:: methodNode ifNotNil: [:it | it tempNames] *)
localNames_slot:: methodMap tempNamesForContext: context_slot
].
^localNames_slot ifNil: [{}]
)
localNamesWithIndicesDo: action <[:String :Number]> = (
localNames doWithIndex: [:localName :index | action value: localName value: index].
)
public localNamesWithValuesDo: action <[:String :ObjectMirror]> = (
localNames doWithIndex:
[:localName :i |
action
value: localName
value: (ObjectMirror reflecting: (methodMap namedTempAt: i in: context_slot))]
)
public localVariableValueMirror: name <String> = (
| index |
index:: localNames indexOf: name.
^ObjectMirror reflecting: ([methodMap namedTempAt: index in: context_slot] ifError: ['<error getting value>'])
)
localVariableValueMirrorForIndex: desiredIndex <Number> = (
localNames doWithIndex:
[:localName :index|
index = desiredIndex ifTrue:
[^ObjectMirror reflecting: ([methodMap namedTempAt: index in: context_slot] ifError: ['<error getting value>'])]].
)
methodMap ^<DebuggerMethodMap> = (
methodMap_slot ifNil: [methodMap_slot:: DebuggerMethodMap forMethod: homeMethod].
^methodMap_slot
)
public sourceRangeIn: thread <ThreadMirror> ^ <Interval> = (
^context_slot debuggerMap
rangeForPC: pc
contextIsActiveContext: (thread isActiveContext: context_slot)
)
) : (
)
public class NSActivationMirror on: anActivationMirror thread: aThreadMirror = (
(* Represents a higher level activation mirror. It has subclasses like NSMethodActivationMIrror and NSTransientSlotMirror*)
|
public lowLevelActivationMirror = anActivationMirror.
threadMirror = aThreadMirror.
senderSlot
homeSlot
|) (
public = anotherMirror = (
(anotherMirror isKindOfNSActivationMirror)
ifFalse: [ ^false].
^lowLevelActivationMirror = anotherMirror lowLevelActivationMirror.
)
public activationsDo: action = (
| currentMirror |
currentMirror:: self.
[currentMirror isNil] whileFalse:
[action value: currentMirror.
currentMirror:: currentMirror sender]
)
public compile: newSource ifFail: onFail ^<Mirror> = (
self subclassResponsibility.
)
computeHome = (
^threadMirror activationFromLowLevelActivation: lowLevelActivationMirror home.
)
computeSender = (
subclassResponsibility.
)
public context_slot = (
^lowLevelActivationMirror context_slot.
)
definingMixin = (
^lowLevelActivationMirror method definingMixin
)
public errorReportOn: stream = (
context_slot errorReportOn: stream
)
public evaluate: expression <String>
ifCompilerError: compilationErrorHandler <[:Error]>
ifError: evalErrorHandler <[:Error]> ^<ObjectMirror> = (
^lowLevelActivationMirror evaluate: expression asString
ifCompilerError: compilationErrorHandler
ifError: evalErrorHandler
)
public evaluate: expression <String>
with: scope <Map[Symbol, ObjectMirror]>
ifCompilerError: compilationErrorHandler <[:Error]>
ifError: evalErrorHandler <[:Error]> ^<ObjectMirror> = (
^lowLevelActivationMirror evaluate: expression asString
with: scope
ifCompilerError: compilationErrorHandler
ifError: evalErrorHandler
)
public hash = (
^lowLevelActivationMirror hash.
)
public home = (
homeSlot ifNotNil: [:it | ^it ].
homeSlot:: computeHome.
^homeSlot.
)
public homeInThread = (
^threadMirror activationFromLowLevelActivation: lowLevelActivationMirror homeInThread.
)
public homeMethod = (
^lowLevelActivationMirror homeMethod.
)
public isBlockActivation = (
^lowLevelActivationMirror isBlockActivation.
)
public isKindOfNSActivationMirror = (
^true.
)
public localNamesWithValuesDo: action <[:String :ObjectMirror]> = (
^lowLevelActivationMirror localNamesWithValuesDo: action.
)
public localVariableValueMirror: name <String> = (
^lowLevelActivationMirror localVariableValueMirror: name.
)
public method = (
^lowLevelActivationMirror method.
)
public name = (
subclassResponsibility
)
public notUnderstoodMessage ^ <Symbol> = (
^lowLevelActivationMirror notUnderstoodMessage.
)
public receiver = (
^lowLevelActivationMirror receiver.
)
public restart = (
^threadMirror restartInMethodActivation: self.
)
public selectorForParsing = (
self subclassResponsiblity
)
public sender = (
senderSlot ifNotNil: [:it | ^it ].
senderSlot:: computeSender.
^senderSlot.
)
public setLocalVariable: variableName <String> toExpression: exprString <String> ifFail: onFail = (
^lowLevelActivationMirror setLocalVariable: variableName toExpression: exprString ifFail: onFail.
)
public setLocalVariable: variableName <String> toValue: newValue ifFail: onFail <[:String]> ^<Object> = (
^lowLevelActivationMirror setLocalVariable: variableName toValue: newValue ifFail: onFail.
)
public sourceAvailable ^<Boolean> = (
subclassResponsibility.
)
public sourceRange ^ <Interval> = (
subclassResponsibility
)
public stackIndicesWithValuesDo: action <[:Integer :ObjectMirror]> = (
lowLevelActivationMirror stackIndicesWithValuesDo: action.
)
public stepIntoBlock = (
^threadMirror stepIntoBlockInMethodActivation: self.
)
public withAllActivations = (
^Array streamContents:
[:s | activationsDo:
[:each | s nextPut: each]].
)
) : (
)
public class NSMethodActivationMirror on: anActivationMirror thread: aThreadMirror = NSActivationMirror on: anActivationMirror thread: aThreadMirror (
) (
public compile: newSource ifFail: onFail ^<Mirror> = (
^lowLevelActivationMirror compile: newSource ifFail: onFail.
)
computeSender = (
| lowLevelSender |
lowLevelSender:: lowLevelActivationMirror sender.
lowLevelSender ifNil: [ ^nil].
^threadMirror activationFromLowLevelActivation: lowLevelSender.
)
public method = (
|mirror|
mirror:: definingMixin methods findMirrorNamed: name.
mirror ifNil: [ mirror:: super method. ].
^mirror.
)
public name = (
^lowLevelActivationMirror selector.
)
public notUnderstoodArguments = (
^lowLevelActivationMirror notUnderstoodArguments
)
public selectorForParsing = (
^#methodDecl
)
public sourceAvailable = (
^lowLevelActivationMirror sourceAvailable.
)
public sourceRange ^ <Interval> = (
^lowLevelActivationMirror sourceRangeIn: threadMirror lowLevelThreadMirror.
)
) : (
public handleActivation: aLowLevelActivation <AcivationMirror> whenCreated: createdBlock whenContextUpdated: contextUpdatedBlock on: aThreadMirror = (
^createdBlock value: (self on: aLowLevelActivation thread: aThreadMirror).
)
public handles: aLowLevelActivation <ActivationMirror> = (
| mixinMirror |
aLowLevelActivation isBlockActivation ifTrue: [^true].
mixinMirror:: aLowLevelActivation method definingMixin.
^mixinMirror methods includesMirrorNamed: aLowLevelActivation method name.
)
)
class NSThreadMirror on: aLowLevelThreadMirror <ThreadMirror> = (
(* Represents a high level Thread mirror (it is at the newspeak level). It wraps the standard ThreadlMirror and is in charge of taking more advanced decisions in terms of which kind activation mirror shoud be created on every step. See #activationFromLowLevelActivation: . This class was introduced in the process of adding transient slots to the language *)
|
public lowLevelThreadMirror = aLowLevelThreadMirror.
|) (
public activationFromLowLevelActivation: aLowLevelActivation <ActivationMirror> ^ <NSActivationMirror> = (
(* It decides which kind kind of activation mirror should be created given a low level activation Mirror. It tries to match a low level activation with a suitable handler. If no handler matches, it defaults to a NSMethodActivationMIrror *)
| possibleHandlers matchedHandler |
possibleHandlers:: { NSMethodActivationMirror . NSTransientSlotActivationMirror }.
matchedHandler:: possibleHandlers detect: [:each | each handles: aLowLevelActivation] ifNone: [ NSMethodActivationMirror ].
^matchedHandler
handleActivation: aLowLevelActivation
whenCreated: [:newActivation | newActivation]
whenContextUpdated: [:updatedLowLevelActivation | activationFromLowLevelActivation: updatedLowLevelActivation]
on: self.
)
public createActivationFromContext: context <ContextPart> ^ <NSActivationMirror> = (
^activationFromLowLevelActivation: (lowLevelThreadMirror createActivationFromContext: context).
)
public install: method <MethodMirror> restart: activation <ActivationMirror> ifFail: onFail = (
| newActivation |
newActivation:: lowLevelThreadMirror
install: method
restart: activation lowLevelActivationMirror
ifFail: onFail.
^activationFromLowLevelActivation: newActivation.
)
public isActiveContext: aContext <MethodContext> = (
^lowLevelThreadMirror isActiveContext: aContext.
)
public isActiveThread = (
^lowLevelThreadMirror isActiveThread.
)
public name = (
^lowLevelThreadMirror name.
)
public printOn: stream = (
^lowLevelThreadMirror printOn: stream.
)
public restart: activation <NSActivationMirror> ^<NSActivationMirror> = (
^activationFromLowLevelActivation: (lowLevelThreadMirror restart: activation lowLevelActivationMirror).
)
public resume = (
^lowLevelThreadMirror resume.
)
public return: mirror <ObjectMirror> from: activation <ActivationMirror> ^<ActivationMirror> = (
^activationFromLowLevelActivation: (lowLevelThreadMirror return: mirror from: activation lowLevelActivationMirror).
)
public squeakProcess = (
^lowLevelThreadMirror squeakProcess.
)
public stepInto: activation <NSActivationMirror> ^<NSActivationMirror> = (
^activationFromLowLevelActivation: (lowLevelThreadMirror stepInto: activation lowLevelActivationMirror).
)
public stepIntoBlock: activation <NSActivationMirror> ^<NSActivationMirror> = (
^activationFromLowLevelActivation: (lowLevelThreadMirror stepIntoBlock: activation lowLevelActivationMirror).
)
public stepOver: activation <NSActivationMirror> ^ <NSActivationMirror> = (
^activationFromLowLevelActivation: (lowLevelThreadMirror stepOver: activation lowLevelActivationMirror).
)
public summary = (
^lowLevelThreadMirror summary.
)
public suspend = (
^lowLevelThreadMirror suspend.
)
public terminate = (
^lowLevelThreadMirror terminate.
)
public unwindRecursion: activation <ActivationMirror> ^<ActivationMirror> = (
^activationFromLowLevelActivation: (lowLevelThreadMirror unwindRecursion: activation lowLevelActivationMirror).
)
) : (
)
public class NSTransientSlotActivationMirror on: anActivationMirror thread: aThreadMirror = NSActivationMirror on: anActivationMirror thread: aThreadMirror (
(*
Represent that activation mirror for a transient slot. It takes care of computer the right sender, providing the current sourceRange considering the particular grammar , providing access to compiling the transient slot code etc
*)
) (
public compile: newSource ifFail: onFail ^<Mirror> = (
[ | builder newMB |
builder:: MixinBuilder reflecting: definingMixin reflectee.
newMB:: builder transientSlots addFromSource: newSource.
builder declaration install.
^definingMixin transientSlots findMirrorNamed: newMB simpleName. ]
on: Error
do: [:ex | ^onFail value: ex description].
)
computeSender = (
| lowLevelSender |
lowLevelSender:: lowLevelActivationMirror sender sender. (* the first sender skips the main method for the transient slot. It should not show in the debugger *)
lowLevelSender ifNil: [ ^nil].
^threadMirror activationFromLowLevelActivation: lowLevelSender.
)
public localNamesWithValuesDo: action <[:String :ObjectMirror]> = (
(* transient slots do not have local variables (temp or parameters) . It's just an expression*)
^{}
)
public method = (
^definingMixin transientSlots findMirrorNamed: name.
)
public name = (
^lowLevelActivationMirror sender selector.
)
public selectorForParsing = (
^#transientSlotDecl
)
public sourceAvailable = (
^true.
)
public sourceRange ^ <Interval> = (
| methodRange |
methodRange:: lowLevelActivationMirror sourceRangeIn: threadMirror lowLevelThreadMirror.
methodRange ifNil: [ ^nil].
^method rangeFromTransientSlotExpressionToMethod: methodRange.
)
) : (
public handleActivation: aLowLevelActivation <AcivationMirror> whenCreated: createdBlock whenContextUpdated: contextUpdatedBlock on: aThreadMirror = (
| isMainMethod mixinMirror tsMirror methodName isCachedValueEmpty newActivation |
methodName:: aLowLevelActivation method name.
mixinMirror:: aLowLevelActivation method definingMixin.
isMainMethod:: mixinMirror transientSlots includesMirrorNamed: methodName.
(* setter *)
(methodName endsWith: ':') ifTrue:
[
newActivation:: aThreadMirror lowLevelThreadMirror complete: aLowLevelActivation.
^contextUpdatedBlock value: newActivation.
].
(* getter *)
isMainMethod
ifTrue: [
tsMirror:: mixinMirror transientSlots findMirrorNamed: methodName.
isCachedValueEmpty:: (aLowLevelActivation receiver getTransientSlot: methodName) reflectee isNil.
newActivation:: isCachedValueEmpty
ifTrue: [ aThreadMirror lowLevelThreadMirror step: aLowLevelActivation untilReturnOrMethodOnTopNamed: tsMirror initMethodName ]
ifFalse: [ aThreadMirror lowLevelThreadMirror complete: aLowLevelActivation].
^contextUpdatedBlock value: newActivation]
ifFalse: [
^createdBlock value: (self on: aLowLevelActivation thread: aThreadMirror) ].
)
public handles: aLowLevelActivation <ActivationMirror> = (
| mixinMirror |
mixinMirror:: aLowLevelActivation method definingMixin.
^mixinMirror isMethodMirrorForTransientSlot: aLowLevelActivation method.
)
)
class ThreadMirror on: process <Process> = (
(* Represents a thread of control. On every step, the internal squeak context is evaluated and a new ActivationMirror is created, even if the underlying context object is exactly the same. The sender method in ActivatiolnMirror creates a new ActivationMirror for the sender (and saves it for further requests). This two things may look a bit of an overkill, but really simplifies situations where the ActivationMirror sender chain need to be updated. In this case the entire ActivationMirror chain will be reacreated on every context step. The #= method in ActivationMirror is defined so two elements are equal if they have the same underlying context, The debugger IDE relies on this operator *)
|
public (* BOGUS *) squeakProcess = process.
protected wasMorphic = Project uiProcess == process.
|#ACCESSBOGUS) (
public complete: activation <ActivationMirror> ^ <ActivationMirror> = (
| newContext |
newContext:: squeakProcess complete: activation context_slot.
^createActivationFromContext: squeakProcess stepToSendOrReturn.
)
public createActivationFromContext: context <ContextPart> ^ <ActivationMirror> = (
assert: [context isNil not] message: 'context is nil'.
^ActivationMirror reflecting: context
)
public install: method <NS2MethodMirror> restart: activation <ActivationMirror> ifFail: onFail = (
| newContext |
activation sender. (* Remember sender *)
activation ifNil: [^onFail value: 'Block''s method not on stack'].
newContext:: squeakProcess popTo: activation context_slot.
newContext == activation context_slot ifFalse:
[onFail value: 'Method saved, but error during unwind.'.
^activationFor: newContext relativeTo: activation].
squeakProcess
restartTopWith: method reflectee;
stepToSendOrReturn.
^createActivationFromContext: newContext.
)
public isActiveContext: aContext <MethodContext> = (
^squeakProcess suspendedContext == aContext
)
public isActiveThread = (
^squeakProcess isActiveProcess
)
public name = (
^wasMorphic
ifTrue: ['UI event loop']
ifFalse: ['thread ', squeakProcess name].
)
public printOn: stream = (
stream
nextPutAll: 'ThreadMirror:';
nextPutAll: name
)
public restart: activation <ActivationMirror> ^<ActivationMirror> = (
| newContext |
newContext:: squeakProcess popTo: activation context_slot.
newContext == activation context_slot ifTrue:
[newContext:: squeakProcess restartTop; stepToSendOrReturn].
^createActivationFromContext: newContext.
)
public resume = (
| oldUiProcess |
wasMorphic
ifTrue:
[oldUiProcess:: Project uiProcess.
Project resumeProcess: squeakProcess]
ifFalse:
[squeakProcess resume].
wasMorphic ifTrue: [oldUiProcess terminate]
)
public return: mirror <ObjectMirror> from: activation <ActivationMirror> ^<ActivationMirror> = (
| newContext |
newContext:: squeakProcess popTo: activation sender context_slot value: mirror reflectee.
^createActivationFromContext: newContext.
)
public step: activation <ActivationMirror> untilReturnOrMethodOnTopNamed: aMethodName <String> ^<ActivationMirror> = (
|newContext|
[ newContext:: squeakProcess step]
doWhileTrue: [ (activation context_slot isDead not) and: [newContext selector ~= aMethodName asSymbol]].
newContext:: squeakProcess stepToSendOrReturn.
^createActivationFromContext: newContext.
)
public stepInto: activation <ActivationMirror> ^<ActivationMirror> = (
squeakProcess step: activation context_slot.
^createActivationFromContext: squeakProcess stepToSendOrReturn.
)
public stepIntoBlock: activation <ActivationMirror> ^<ActivationMirror> = (
squeakProcess stepToHome: activation context_slot.
^createActivationFromContext: squeakProcess stepToSendOrReturn.
)
public stepOver: activation <ActivationMirror> ^ <ActivationMirror> = (
| newContext |
newContext:: squeakProcess completeStep: activation context_slot.
newContext == activation context_slot ifTrue:
[newContext:: squeakProcess stepToSendOrReturn].
^createActivationFromContext: newContext.
)
public summary = (
^name, ' @ priority ', squeakProcess priority asString
)
public suspend = (
(* Special case for the Morphic UI thread. NsFFISessionManager takes care of native UI thread. *)
Project spawnNewProcessIfThisIsUI: squeakProcess.
squeakProcess suspend.
)
public terminate = (
Project spawnNewProcessIfThisIsUI: squeakProcess.
squeakProcess terminate
)
public unwindRecursion: activation <ActivationMirror> ^<ActivationMirror> = (
| newContext |
newContext:: squeakProcess popTo: activation context_slot findSecondToOldestSimilarSender.
^createActivationFromContext: newContext.
)
) : (
)
public onProcess: process <Process> ^ <ThreadMirror> = (
assert: [process isNil not] message: 'process is nil'.
^NSThreadMirror on: (ThreadMirror on: process).
(* ^(ThreadMirror on: process). *)
)
) : (
)