github.com/pygolin/runtime@v0.0.0-20201208210830-a62e3cd39798/callableiter.go (about) 1 // Copyright 2018 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package runtime 16 17 import ( 18 "reflect" 19 "sync" 20 ) 21 22 var ( 23 callableIteratorType = newBasisType("callable-iterator", reflect.TypeOf(callableIterator{}), toCallableIteratorUnsafe, ObjectType) 24 ) 25 26 type callableIterator struct { 27 Object 28 callable *Object 29 sentinel *Object 30 mutex sync.Mutex 31 } 32 33 func newCallableIterator(callable *Object, sentinel *Object) *Object { 34 iter := &callableIterator{Object: Object{typ: callableIteratorType}, callable: callable, sentinel: sentinel} 35 return &iter.Object 36 } 37 38 func toCallableIteratorUnsafe(o *Object) *callableIterator { 39 return (*callableIterator)(o.toPointer()) 40 } 41 42 func callableIteratorIter(f *Frame, o *Object) (*Object, *BaseException) { 43 return o, nil 44 } 45 46 func callableIteratorNext(f *Frame, o *Object) (item *Object, raised *BaseException) { 47 i := toCallableIteratorUnsafe(o) 48 i.mutex.Lock() 49 defer i.mutex.Unlock() 50 if i.callable == nil { 51 raised = f.Raise(StopIterationType.ToObject(), nil, nil) 52 } else if item, raised = i.callable.Call(f, Args{}, nil); raised == nil { 53 var eq *Object 54 if eq, raised = Eq(f, item, i.sentinel); raised == nil && eq == True.ToObject() { 55 i.callable = nil 56 item = nil 57 raised = f.Raise(StopIterationType.ToObject(), nil, nil) 58 } 59 } 60 return item, raised 61 } 62 63 func initCallableIteratorType(map[string]*Object) { 64 callableIteratorType.flags &= ^(typeFlagBasetype | typeFlagInstantiable) 65 callableIteratorType.slots.Iter = &unaryOpSlot{callableIteratorIter} 66 callableIteratorType.slots.Next = &unaryOpSlot{callableIteratorNext} 67 }