-
Notifications
You must be signed in to change notification settings - Fork 67
/
VMBlockTest.class.st
301 lines (232 loc) · 9.8 KB
/
VMBlockTest.class.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
Class {
#name : #VMBlockTest,
#superclass : #VMInterpreterTests,
#pools : [
'VMClassIndices',
'VMObjectIndices'
],
#category : #'VMMakerTests-InterpreterTests'
}
{ #category : #supports }
VMBlockTest >> anEmptyMethod [
]
{ #category : #'as yet unclassified' }
VMBlockTest >> evaluatingABlock [
[^1] value
]
{ #category : #helpers }
VMBlockTest >> installFullBlockClosureClass [
| aClass |
aClass := self
newClassInOldSpaceWithSlots: 0
instSpec: FullBlockClosure instSpec.
memory setHashBitsOf: aClass to: ClassFullBlockClosureCompactIndex.
memory
storePointer: ClassFullBlockClosureCompactIndex
ofObject: memory classTableFirstPage
withValue: aClass
]
{ #category : #supports }
VMBlockTest >> methodReturningABlock [
<compilerOptions: #(- #optionConstantBlockClosure) >
^ []
]
{ #category : #'as yet unclassified' }
VMBlockTest >> methodReturningABlockInsideABlockWithLocal [
true ifTrue: [
| anArgument |
anArgument := 2.
^ [ anArgument ] ]
]
{ #category : #supports }
VMBlockTest >> methodReturningABlockWithTwoArguments [
^ [:a :b]
]
{ #category : #supports }
VMBlockTest >> methodWithLocalReturningABlock [
| a |
a:= 1.
^ [ a ]
]
{ #category : #running }
VMBlockTest >> setUp [
super setUp.
self initializeSpecialSelectors
]
{ #category : #'as yet unclassified' }
VMBlockTest >> testCreatingABlockCapturesReceiver [
| methodReturning initialMethod |
initialMethod := self createMethodOopFromHostMethod:
self class >> #anEmptyMethod.
methodReturning := self createMethodOopFromHostMethod:
self class >> #methodReturningABlockInsideABlockWithLocal.
self installFullBlockClosureClass.
"We want to avoid baseFrameReturn (base frame initialisation)"
stackBuilder addNewFrame method: initialMethod.
stackBuilder addNewFrame
method: methodReturning;
receiver: memory trueObject.
stackBuilder buildStack.
interpreter setMethod: stackBuilder lastFrame method.
interpreter instructionPointer: (methodBuilder bytecodeAt: 0 forMethod: methodReturning).
interpreter interpretWhile: [ interpreter method = methodReturning ].
self assert: (memory fetchPointer: FullClosureReceiverIndex ofObject: interpreter stackTop) equals: memory trueObject
]
{ #category : #testing }
VMBlockTest >> testCreatingABlockClosureKnowsCompiledBlock [
| methodReturning initialMethod placeTakenByLiterals closure blockInitialPC compiledBlock |
initialMethod := self createMethodOopFromHostMethod:
self class >> #anEmptyMethod.
methodReturning := self createMethodOopFromHostMethod:
self class >> #methodReturningABlock.
self installFullBlockClosureClass.
"We want to avoid baseFrameReturn (base frame initialisation)"
stackBuilder addNewFrame method: initialMethod.
stackBuilder addNewFrame
method: methodReturning;
receiver: memory trueObject.
stackBuilder buildStack.
interpreter setMethod: stackBuilder lastFrame method.
"Bytecode index is 1-based, use 0 to set IP just before the first bytecode.
The first thing interpretWhile: dows is to fetch a new bytecode"
interpreter instructionPointer:
(methodBuilder bytecodeAt: 0 forMethod: methodReturning).
interpreter interpretWhile: [ interpreter method = methodReturning ].
"The start pc is after the two literals of the block"
blockInitialPC := memory wordSize * 2.
compiledBlock := memory
fetchPointer: FullClosureCompiledBlockIndex
ofObject: interpreter stackTop.
self
assert: (interpreter startPCOfMethod: compiledBlock)
equals: blockInitialPC
]
{ #category : #testing }
VMBlockTest >> testCreatingABlockClosureShouldCopyUsedMethodVariable [
| methodReturning initialMethod |
initialMethod := self createMethodOopFromHostMethod:
self class >> #anEmptyMethod.
methodReturning := self createMethodOopFromHostMethod:
self class >> #methodReturningABlockInsideABlockWithLocal.
self installFullBlockClosureClass.
"We want to avoid baseFrameReturn (base frame initialisation)"
stackBuilder addNewFrame method: initialMethod.
stackBuilder addNewFrame
method: methodReturning;
receiver: memory trueObject.
stackBuilder buildStack.
interpreter setMethod: stackBuilder lastFrame method.
interpreter instructionPointer: (methodBuilder bytecodeAt: 0 forMethod: methodReturning).
interpreter interpretWhile: [ interpreter method = methodReturning ].
"We assign 1 to the local variable of the method, see methodWithLocalReturningABlock"
self
assert: (memory fetchPointer: FullClosureFirstCopiedValueIndex ofObject: interpreter stackTop) equals: (memory integerObjectOf: 2)
]
{ #category : #testing }
VMBlockTest >> testCreatingABlockClosureShouldHaveOuterContextObject [
| methodReturning initialMethod |
initialMethod := self createMethodOopFromHostMethod:
self class >> #anEmptyMethod.
methodReturning := self createMethodOopFromHostMethod:
self class >> #methodReturningABlock.
self installFullBlockClosureClass.
"We want to avoid baseFrameReturn (base frame initialisation)"
stackBuilder addNewFrame method: initialMethod.
stackBuilder addNewFrame
method: methodReturning;
receiver: memory trueObject.
stackBuilder buildStack.
interpreter setMethod: stackBuilder lastFrame method.
interpreter instructionPointer: (methodBuilder bytecodeAt: 0 forMethod: methodReturning).
interpreter interpretWhile: [ interpreter method = methodReturning ].
"The current frame returned, the context is widowed, but the VM does not mark what are the alive frames all the time"
self deny: (interpreter isWidowedContext: (memory outerContextOf: interpreter stackTop)).
"From time to time, the VM will mark the frames that are alive, and then be able to recognize that a context is widowed"
interpreter writeBackHeadFramePointers.
self assert: (interpreter isWidowedContext: (memory outerContextOf: interpreter stackTop))
]
{ #category : #'as yet unclassified' }
VMBlockTest >> testCreatingABlockInsideABlockClosureShouldCopyUsedBlockVariable [
| methodReturning initialMethod |
initialMethod := self createMethodOopFromHostMethod:
self class >> #anEmptyMethod.
methodReturning := self createMethodOopFromHostMethod:
self class >> #methodReturningABlockInsideABlockWithLocal.
self installFullBlockClosureClass.
"We want to avoid baseFrameReturn (base frame initialisation)"
stackBuilder addNewFrame method: initialMethod.
stackBuilder addNewFrame
method: methodReturning;
receiver: memory trueObject.
stackBuilder buildStack.
interpreter setMethod: stackBuilder lastFrame method.
interpreter instructionPointer: (methodBuilder bytecodeAt: 0 forMethod: methodReturning).
interpreter interpretWhile: [ interpreter method = methodReturning ].
"We assign 2 to the local variable of the block, see methodReturningABlockInsideABlock"
self assert: (memory fetchPointer: FullClosureFirstCopiedValueIndex ofObject: interpreter stackTop) equals: (memory integerObjectOf: 2).
]
{ #category : #testing }
VMBlockTest >> testCreatingABlockWithoutArgumentsClosureShouldHaveNoArgument [
| methodReturning initialMethod |
initialMethod := self createMethodOopFromHostMethod:
self class >> #anEmptyMethod.
methodReturning := self createMethodOopFromHostMethod:
self class >> #methodReturningABlock.
self installFullBlockClosureClass.
"We want to avoid baseFrameReturn (base frame initialisation)"
stackBuilder addNewFrame method: initialMethod.
stackBuilder addNewFrame
method: methodReturning;
receiver: memory trueObject.
stackBuilder buildStack.
interpreter setMethod: stackBuilder lastFrame method.
interpreter instructionPointer: (methodBuilder bytecodeAt: 0 forMethod: methodReturning).
interpreter interpretWhile: [ interpreter method = methodReturning ].
self
assert: (memory fetchPointer: FullClosureNumArgsIndex ofObject: interpreter stackTop) equals: (memory integerObjectOf: 0).
]
{ #category : #'as yet unclassified' }
VMBlockTest >> testEvaluatingABlock [
| methodReturning initialMethod |
initialMethod := self createMethodOopFromHostMethod:
self class >> #anEmptyMethod.
methodReturning := self createMethodOopFromHostMethod:
self class >> #evaluatingABlock.
self installFullBlockClosureClass.
"We want to avoid baseFrameReturn (base frame initialisation)"
stackBuilder addNewFrame method: initialMethod.
stackBuilder addNewFrame
method: methodReturning;
receiver: memory trueObject.
stackBuilder buildStack.
interpreter setMethod: stackBuilder lastFrame method.
interpreter instructionPointer: (methodBuilder bytecodeAt: 0 forMethod: methodReturning).
"Interpret while we are in the correct method, and until the value message to the block"
interpreter interpretWhile: [
interpreter method = methodReturning
and: [ interpreter currentBytecode ~= 121 ] ].
"The top of the stack should be the fullblock closure object"
self
assert: (memory fetchClassOf: interpreter stackTop)
equals: (memory classAtIndex: ClassFullBlockClosureCompactIndex)
]
{ #category : #testing }
VMBlockTest >> testPushClosureBytecodePushesClosure [
| methodReturning initialMethod |
initialMethod := self createMethodOopFromHostMethod:
self class >> #anEmptyMethod.
methodReturning := self createMethodOopFromHostMethod:
self class >> #methodReturningABlock.
self installFullBlockClosureClass.
"We want to avoid baseFrameReturn (base frame initialisation)"
stackBuilder addNewFrame method: initialMethod.
stackBuilder addNewFrame
method: methodReturning;
receiver: memory trueObject.
stackBuilder buildStack.
interpreter setMethod: stackBuilder lastFrame method.
interpreter interpretWhile: [ interpreter method = methodReturning ].
self
assert: (memory fetchClassOf: interpreter stackTop)
equals: (memory classAtIndex: ClassFullBlockClosureCompactIndex)
]