Skip to content
This repository was archived by the owner on Jul 12, 2025. It is now read-only.

Commit 44f3a14

Browse files
authored
Merge pull request #848 from goby-lang/refactor-builtin-method-yield
Make t.builtinMethodYield return an Object instead of a Pointer
2 parents 283b108 + e6c3641 commit 44f3a14

File tree

9 files changed

+30
-34
lines changed

9 files changed

+30
-34
lines changed

vm/array.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ var builtinArrayClassMethods = []*BuiltinMethodObject{
4444

4545
if blockFrame != nil && !blockIsEmpty(blockFrame) {
4646
for i := range elems {
47-
elems[i] = t.builtinMethodYield(blockFrame, t.vm.InitIntegerObject(i)).Target
47+
elems[i] = t.builtinMethodYield(blockFrame, t.vm.InitIntegerObject(i))
4848
}
4949
} else {
5050
var elem Object
@@ -413,7 +413,7 @@ var builtinArrayInstanceMethods = []*BuiltinMethodObject{
413413
for _, obj := range arr.Elements {
414414
result := t.builtinMethodYield(blockFrame, obj)
415415

416-
if result.Target.isTruthy() {
416+
if result.isTruthy() {
417417
return TRUE
418418
}
419419
}
@@ -541,7 +541,7 @@ var builtinArrayInstanceMethods = []*BuiltinMethodObject{
541541

542542
for _, obj := range arr.Elements {
543543
result := t.builtinMethodYield(blockFrame, obj)
544-
if result.Target.isTruthy() {
544+
if result.isTruthy() {
545545
count++
546546
}
547547
}
@@ -930,12 +930,12 @@ var builtinArrayInstanceMethods = []*BuiltinMethodObject{
930930
switch len(args) {
931931
case 0:
932932
for _, obj := range a.Elements {
933-
hash[obj.ToString()] = t.builtinMethodYield(blockFrame, obj).Target
933+
hash[obj.ToString()] = t.builtinMethodYield(blockFrame, obj)
934934
}
935935
case 1:
936936
arg := args[0]
937937
for _, obj := range a.Elements {
938-
switch b := t.builtinMethodYield(blockFrame, obj).Target; b.(type) {
938+
switch b := t.builtinMethodYield(blockFrame, obj); b.(type) {
939939
case *NullObject:
940940
hash[obj.ToString()] = arg
941941
default:
@@ -1104,8 +1104,7 @@ var builtinArrayInstanceMethods = []*BuiltinMethodObject{
11041104
}
11051105
} else {
11061106
for i, obj := range arr.Elements {
1107-
result := t.builtinMethodYield(blockFrame, obj)
1108-
elements[i] = result.Target
1107+
elements[i] = t.builtinMethodYield(blockFrame, obj)
11091108
}
11101109
}
11111110

@@ -1225,8 +1224,7 @@ var builtinArrayInstanceMethods = []*BuiltinMethodObject{
12251224
}
12261225

12271226
for i := start; i < len(arr.Elements); i++ {
1228-
result := t.builtinMethodYield(blockFrame, prev, arr.Elements[i])
1229-
prev = result.Target
1227+
prev = t.builtinMethodYield(blockFrame, prev, arr.Elements[i])
12301228
}
12311229

12321230
return prev
@@ -1405,7 +1403,7 @@ var builtinArrayInstanceMethods = []*BuiltinMethodObject{
14051403

14061404
for _, obj := range arr.Elements {
14071405
result := t.builtinMethodYield(blockFrame, obj)
1408-
if result.Target.isTruthy() {
1406+
if result.isTruthy() {
14091407
elements = append(elements, obj)
14101408
}
14111409
}

vm/block.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ var builtinBlockInstanceMethods = []*BuiltinMethodObject{
115115
c.self = block.self
116116
c.isBlock = true
117117

118-
return t.builtinMethodYield(c, args...).Target
118+
return t.builtinMethodYield(c, args...)
119119
},
120120
},
121121
}

vm/class.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,9 +1199,8 @@ var builtinClassCommonInstanceMethods = []*BuiltinMethodObject{
11991199
}
12001200

12011201
blockFrame.self = receiver
1202-
result := t.builtinMethodYield(blockFrame)
12031202

1204-
return result.Target
1203+
return t.builtinMethodYield(blockFrame)
12051204

12061205
},
12071206
},

vm/concurrent_rw_lock.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ var builtinConcurrentRWLockInstanceMethods = []*BuiltinMethodObject{
170170

171171
lockObject.mutex.RLock()
172172

173-
blockReturnValue := t.builtinMethodYield(blockFrame).Target
173+
blockReturnValue := t.builtinMethodYield(blockFrame)
174174

175175
lockObject.mutex.RUnlock()
176176

@@ -204,7 +204,7 @@ var builtinConcurrentRWLockInstanceMethods = []*BuiltinMethodObject{
204204

205205
lockObject.mutex.Lock()
206206

207-
blockReturnValue := t.builtinMethodYield(blockFrame).Target
207+
blockReturnValue := t.builtinMethodYield(blockFrame)
208208

209209
lockObject.mutex.Unlock()
210210

vm/hash.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ var builtinHashInstanceMethods = []*BuiltinMethodObject{
216216
return NULL
217217
}
218218

219-
if result.Target.isTruthy() {
219+
if result.isTruthy() {
220220
return TRUE
221221
}
222222
}
@@ -384,13 +384,13 @@ var builtinHashInstanceMethods = []*BuiltinMethodObject{
384384
objectKey := t.vm.InitStringObject(stringKey)
385385
result := t.builtinMethodYield(blockFrame, objectKey, value)
386386

387-
booleanResult, isResultBoolean := result.Target.(*BooleanObject)
387+
booleanResult, isResultBoolean := result.(*BooleanObject)
388388

389389
if isResultBoolean {
390390
if booleanResult.value {
391391
delete(hash.Pairs, stringKey)
392392
}
393-
} else if result.Target != NULL {
393+
} else if result != NULL {
394394
delete(hash.Pairs, stringKey)
395395
}
396396
}
@@ -704,7 +704,7 @@ var builtinHashInstanceMethods = []*BuiltinMethodObject{
704704
}
705705

706706
if blockFrame != nil {
707-
return t.builtinMethodYield(blockFrame, key).Target
707+
return t.builtinMethodYield(blockFrame, key)
708708
}
709709
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, "The value was not found, and no block has been provided")
710710
},
@@ -746,7 +746,7 @@ var builtinHashInstanceMethods = []*BuiltinMethodObject{
746746

747747
if !ok {
748748
if blockFrame != nil {
749-
value = t.builtinMethodYield(blockFrame, objectKey).Target
749+
value = t.builtinMethodYield(blockFrame, objectKey)
750750
blockFramePopped = true
751751
} else {
752752
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, "There is no value for the key `%s`, and no block has been provided", stringKey.value)
@@ -910,7 +910,7 @@ var builtinHashInstanceMethods = []*BuiltinMethodObject{
910910
}
911911

912912
for k, v := range h.Pairs {
913-
result[k] = t.builtinMethodYield(blockFrame, v).Target
913+
result[k] = t.builtinMethodYield(blockFrame, v)
914914
}
915915
return t.vm.InitHashObject(result)
916916

@@ -1005,7 +1005,7 @@ var builtinHashInstanceMethods = []*BuiltinMethodObject{
10051005
objectKey := t.vm.InitStringObject(stringKey)
10061006
result := t.builtinMethodYield(blockFrame, objectKey, value)
10071007

1008-
if result.Target.isTruthy() {
1008+
if result.isTruthy() {
10091009
destinationPairs[stringKey] = value
10101010
}
10111011
}
@@ -1179,8 +1179,7 @@ var builtinHashInstanceMethods = []*BuiltinMethodObject{
11791179

11801180
resultHash := make(map[string]Object)
11811181
for k, v := range h.Pairs {
1182-
result := t.builtinMethodYield(blockFrame, v)
1183-
resultHash[k] = result.Target
1182+
resultHash[k] = t.builtinMethodYield(blockFrame, v)
11841183
}
11851184
return t.vm.InitHashObject(resultHash)
11861185

vm/http.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,11 @@ var builtinHTTPClassMethods = []*BuiltinMethodObject{
168168

169169
result := t.builtinMethodYield(blockFrame, gobyClient)
170170

171-
if err, ok := result.Target.(*Error); ok {
171+
if err, ok := result.(*Error); ok {
172172
return err //an Error object
173173
}
174174

175-
return result.Target
175+
return result
176176

177177
},
178178
},

vm/range.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ var builtinRangeInstanceMethods = []*BuiltinMethodObject{
119119

120120
result := t.builtinMethodYield(blockFrame, t.vm.InitIntegerObject(mid))
121121

122-
switch r := result.Target.(type) {
122+
switch r := result.(type) {
123123
case *BooleanObject:
124124
if r.value {
125125
pivot = mid
@@ -303,7 +303,7 @@ var builtinRangeInstanceMethods = []*BuiltinMethodObject{
303303
el = append(el, NULL)
304304
} else {
305305
obj := t.vm.InitIntegerObject(i)
306-
el = append(el, t.builtinMethodYield(blockFrame, obj).Target)
306+
el = append(el, t.builtinMethodYield(blockFrame, obj))
307307
}
308308

309309
return nil

vm/simple_server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func newHandler(t *Thread, blockFrame *normalCallFrame) func(http.ResponseWriter
152152
req := initRequest(t, w, r)
153153
result := thread.builtinMethodYield(blockFrame, req, res)
154154

155-
if err, ok := result.Target.(*Error); ok {
155+
if err, ok := result.(*Error); ok {
156156
log.Printf("Error: %s", err.message)
157157
res.InstanceVariableSet("@status", t.vm.InitIntegerObject(500))
158158
}

vm/thread.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ func (t *Thread) execInstruction(cf *normalCallFrame, i *bytecode.Instruction) {
240240
}
241241

242242
// Yield to a call frame
243-
func (t *Thread) Yield(args ...Object) *Pointer {
243+
func (t *Thread) Yield(args ...Object) Object {
244244
return t.builtinMethodYield(t.currentFrame.BlockFrame(), args...)
245245
}
246246

@@ -249,9 +249,9 @@ func (t *Thread) BlockGiven() bool {
249249
return t.currentFrame.BlockFrame() != nil
250250
}
251251

252-
func (t *Thread) builtinMethodYield(blockFrame *normalCallFrame, args ...Object) *Pointer {
252+
func (t *Thread) builtinMethodYield(blockFrame *normalCallFrame, args ...Object) Object {
253253
if blockFrame.IsRemoved() {
254-
return &Pointer{Target: NULL}
254+
return NULL
255255
}
256256

257257
c := newNormalCallFrame(blockFrame.instructionSet, blockFrame.FileName(), blockFrame.sourceLine)
@@ -269,10 +269,10 @@ func (t *Thread) builtinMethodYield(blockFrame *normalCallFrame, args ...Object)
269269
t.startFromTopFrame()
270270

271271
if blockFrame.IsRemoved() {
272-
return &Pointer{Target: NULL}
272+
return NULL
273273
}
274274

275-
return t.Stack.top()
275+
return t.Stack.top().Target
276276
}
277277

278278
func (t *Thread) retrieveBlock(fileName, blockFlag string, sourceLine int) (blockFrame *normalCallFrame) {

0 commit comments

Comments
 (0)