This repository was archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 635
Expand file tree
/
Copy pathgenerator.go
More file actions
126 lines (110 loc) · 3.65 KB
/
generator.go
File metadata and controls
126 lines (110 loc) · 3.65 KB
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
// Copyright 2016 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package grumpy
import (
"reflect"
"sync"
)
var (
// GeneratorType is the object representing the Python 'generator' type.
GeneratorType = newBasisType("generator", reflect.TypeOf(Generator{}), toGeneratorUnsafe, ObjectType)
)
type generatorState int
const (
generatorStateCreated generatorState = iota
generatorStateReady
generatorStateRunning
generatorStateDone
)
// Generator represents Python 'generator' objects.
type Generator struct {
Object
mutex sync.Mutex
state generatorState
frame *Frame
fn func(*Object) (*Object, *BaseException)
}
// NewGenerator returns a new Generator object that runs the given Block b.
func NewGenerator(f *Frame, fn func(*Object) (*Object, *BaseException)) *Generator {
f.taken = true // Claim the frame from being returned.
// The code generator basically gives us the Frame, so we can tare it
// off and prevent a parasitic `taken` from creeping up the frames.
f.back = nil
return &Generator{Object: Object{typ: GeneratorType}, frame: f, fn: fn}
}
func toGeneratorUnsafe(o *Object) *Generator {
return (*Generator)(o.toPointer())
}
func (g *Generator) resume(f *Frame, sendValue *Object) (*Object, *BaseException) {
var raised *BaseException
g.mutex.Lock()
oldState := g.state
switch oldState {
case generatorStateCreated:
if sendValue != None {
raised = f.RaiseType(TypeErrorType, "can't send non-None value to a just-started generator")
} else {
g.state = generatorStateRunning
}
case generatorStateReady:
g.state = generatorStateRunning
case generatorStateRunning:
raised = f.RaiseType(ValueErrorType, "generator already executing")
case generatorStateDone:
raised = f.Raise(StopIterationType.ToObject(), nil, nil)
}
g.mutex.Unlock()
// Concurrent attempts to transition to running state will raise here
// so it's guaranteed that only one thread will proceed to execute the
// block below.
if raised != nil {
return nil, raised
}
g.frame.pushFrame(f)
result, raised := g.fn(sendValue)
g.mutex.Lock()
if result == nil && raised == nil {
raised = f.Raise(StopIterationType.ToObject(), nil, nil)
}
if raised == nil {
g.frame.PopCheckpoint()
g.state = generatorStateReady
} else {
g.state = generatorStateDone
}
g.mutex.Unlock()
return result, raised
}
// ToObject upcasts g to an Object.
func (g *Generator) ToObject() *Object {
return &g.Object
}
func generatorIter(f *Frame, o *Object) (*Object, *BaseException) {
return o, nil
}
func generatorNext(f *Frame, o *Object) (*Object, *BaseException) {
return toGeneratorUnsafe(o).resume(f, None)
}
func generatorSend(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
if raised := checkMethodArgs(f, "send", args, GeneratorType, ObjectType); raised != nil {
return nil, raised
}
return toGeneratorUnsafe(args[0]).resume(f, args[1])
}
func initGeneratorType(dict map[string]*Object) {
dict["send"] = newBuiltinFunction("send", generatorSend).ToObject()
GeneratorType.flags &= ^(typeFlagBasetype | typeFlagInstantiable)
GeneratorType.slots.Iter = &unaryOpSlot{generatorIter}
GeneratorType.slots.Next = &unaryOpSlot{generatorNext}
}