-
Notifications
You must be signed in to change notification settings - Fork 175
/
main.go
190 lines (148 loc) · 3.79 KB
/
main.go
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
package main
import (
"bytes"
"fmt"
"go/format"
"os"
"path/filepath"
"strings"
)
const header = `// AUTO-GENERATED BY %s. DO NOT MODIFY.
package environment
import (
"github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/fvm/state"
"github.com/onflow/flow-go/module/trace"
)
func parseRestricted(
txnState *state.TransactionState,
spanName trace.SpanName,
) error {
if txnState.IsParseRestricted() {
return errors.NewParseRestrictedModeInvalidAccessFailure(spanName)
}
return nil
}
// Utility functions used for checking unexpected operation access while
// cadence is parsing programs.
//
// The generic functions are of the form
// parseRestrict<x>Arg<y>Ret(txnState, spanName, callback, arg1, ..., argX)
// where the callback expects <x> number of arguments, and <y> number of
// return values (not counting error). If the callback expects no argument,
// <x>Arg is omitted, and similarly for return value.`
func generateWrapper(numArgs int, numRets int, content *FileContent) {
l := content.Line
push := content.PushIndent
pop := content.PopIndent
argsFuncSuffix := ""
if numArgs > 0 {
argsFuncSuffix = fmt.Sprintf("%dArg", numArgs)
}
argTypes := []string{}
argNames := []string{}
for i := 0; i < numArgs; i++ {
argTypes = append(argTypes, fmt.Sprintf("Arg%dT", i))
argNames = append(argNames, fmt.Sprintf("arg%d", i))
}
retsFuncSuffix := ""
if numRets > 0 {
retsFuncSuffix = fmt.Sprintf("%dRet", numRets)
}
retTypes := []string{}
retNames := []string{}
for i := 0; i < numRets; i++ {
retTypes = append(retTypes, fmt.Sprintf("Ret%dT", i))
retNames = append(retNames, fmt.Sprintf("value%d", i))
}
//
// Generate function signature
//
l("")
l("func parseRestrict%s%s[", argsFuncSuffix, retsFuncSuffix)
push()
for _, typeName := range append(argTypes, retTypes...) {
l("%s any,", typeName)
}
pop()
l("](")
push()
l("txnState *state.TransactionState,")
l("spanName trace.SpanName,")
callbackRet := "error"
if numRets > 0 {
callbackRet = "(" + strings.Join(append(retTypes, "error"), ", ") + ")"
}
l("callback func(%s) %s,", strings.Join(argTypes, ", "), callbackRet)
for i, argType := range argTypes {
l("%s %s,", argNames[i], argType)
}
pop()
if numRets == 0 {
l(") error {")
} else {
l(") (")
push()
for _, retType := range retTypes {
l("%s,", retType)
}
l("error,")
pop()
l(") {")
}
push()
//
// Generate parse restrict check
//
l("err := parseRestricted(txnState, spanName)")
l("if err != nil {")
push()
for i, retType := range retTypes {
l("var %s %s", retNames[i], retType)
}
l("return %s", strings.Join(append(retNames, "err"), ", "))
pop()
l("}")
//
// Generate callback invocation
//
l("")
l("return callback(%s)", strings.Join(argNames, ", "))
pop()
l("}")
}
func main() {
if len(os.Args) != 2 {
fmt.Printf("USAGE: %s <output file>\n", filepath.Base(os.Args[0]))
os.Exit(1)
}
cmd := append([]string{filepath.Base(os.Args[0])}, os.Args[1:]...)
content := NewFileContent()
content.Section(header, strings.Join(cmd, " "))
for numArgs := 1; numArgs < 4; numArgs++ {
generateWrapper(numArgs, 0, content)
}
for _, numArgs := range []int{0, 1, 2, 3, 4, 6} {
generateWrapper(numArgs, 1, content)
}
generateWrapper(1, 2, content)
buffer := &bytes.Buffer{}
_, err := content.WriteTo(buffer)
if err != nil {
panic(err) // This should never happen
}
source, formatErr := format.Source(buffer.Bytes())
// NOTE: formatting error can occur if the generated code has syntax
// errors. We still want to write out the unformatted source for debugging
// purpose.
if formatErr != nil {
source = buffer.Bytes()
}
writeErr := os.WriteFile(os.Args[1], source, 0644)
if writeErr != nil {
panic(writeErr)
}
if formatErr != nil {
panic(formatErr)
}
}