-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
subroutine.go
279 lines (256 loc) · 8.05 KB
/
subroutine.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
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
package interpreter
import (
"strings"
"github.com/pkg/errors"
"github.com/ysugimoto/falco/ast"
"github.com/ysugimoto/falco/interpreter/context"
"github.com/ysugimoto/falco/interpreter/exception"
"github.com/ysugimoto/falco/interpreter/process"
"github.com/ysugimoto/falco/interpreter/value"
"github.com/ysugimoto/falco/interpreter/variable"
)
func (i *Interpreter) ProcessSubroutine(sub *ast.SubroutineDeclaration, ds DebugState) (State, error) {
i.process.Flows = append(i.process.Flows, process.NewFlow(i.ctx, sub))
// Store the current values and restore after subroutine has ended
regex := i.ctx.RegexMatchedValues
local := i.localVars
i.ctx.RegexMatchedValues = make(map[string]*value.String)
i.localVars = variable.LocalVariables{}
defer func() {
i.ctx.RegexMatchedValues = regex
i.localVars = local
i.ctx.SubroutineCalls[sub.Name.Value]++
}()
// Try to extract fastly reserved subroutine macro
if err := i.extractBoilerplateMacro(sub); err != nil {
return NONE, errors.WithStack(err)
}
statements, err := i.resolveIncludeStatement(sub.Block.Statements, false)
if err != nil {
return NONE, errors.WithStack(err)
}
// Ignore debug status and must return state, not a value
_, state, _, err := i.ProcessBlockStatement(statements, ds, false)
return state, err
}
// nolint: gocognit
func (i *Interpreter) ProcessFunctionSubroutine(sub *ast.SubroutineDeclaration, ds DebugState) (value.Value, State, error) {
i.process.Flows = append(i.process.Flows, process.NewFlow(i.ctx, sub))
// Store the current values and restore after subroutine has ended
regex := i.ctx.RegexMatchedValues
local := i.localVars
i.ctx.RegexMatchedValues = make(map[string]*value.String)
i.localVars = variable.LocalVariables{}
defer func() {
i.ctx.RegexMatchedValues = regex
i.localVars = local
i.ctx.SubroutineCalls[sub.Name.Value]++
}()
var err error
var debugState DebugState = ds
for _, stmt := range sub.Block.Statements {
// Call debugger
if debugState != DebugStepOut {
debugState = i.Debugger.Run(stmt)
}
switch t := stmt.(type) {
// Common logic statements (nothing to change state)
case *ast.DeclareStatement:
err = i.ProcessDeclareStatement(t)
case *ast.SetStatement:
err = i.ProcessSetStatement(t)
case *ast.UnsetStatement:
err = i.ProcessUnsetStatement(t)
case *ast.RemoveStatement:
err = i.ProcessRemoveStatement(t)
case *ast.AddStatement:
err = i.ProcessAddStatement(t)
case *ast.LogStatement:
err = i.ProcessLogStatement(t)
case *ast.SyntheticStatement:
err = i.ProcessSyntheticStatement(t)
case *ast.SyntheticBase64Statement:
err = i.ProcessSyntheticBase64Statement(t)
// case *ast.GotoStatement:
// err = i.ProcessGotoStatement(t)
// case *ast.GotoDestinationStatement:
// err = i.ProcessGotoDesticationStatement(t)
// Probably change status statements
case *ast.BlockStatement:
var val value.Value
var state State
val, state, _, err = i.ProcessBlockStatement(t.Statements, ds, true)
if val != value.Null {
return val, NONE, nil
}
if state != NONE {
return value.Null, state, nil
}
case *ast.FunctionCallStatement:
var state State
// Enable breakpoint if current debug state is step-in
if debugState == DebugStepIn {
state, err = i.ProcessFunctionCallStatement(t, DebugStepIn)
} else {
state, err = i.ProcessFunctionCallStatement(t, DebugStepOut)
}
if state != NONE {
return value.Null, state, nil
}
case *ast.CallStatement:
var state State
// Enable breakpoint if current debug state is step-in
if debugState == DebugStepIn {
state, err = i.ProcessCallStatement(t, DebugStepIn)
} else {
state, err = i.ProcessCallStatement(t, DebugStepOut)
}
if state != NONE {
return value.Null, state, nil
}
case *ast.IfStatement:
var val value.Value
var state State
// If statement inside functional subroutine could return value
val, state, err = i.ProcessIfStatement(t, debugState, true)
if val != value.Null {
return val, NONE, nil
}
if state != NONE {
return value.Null, state, nil
}
case *ast.SwitchStatement:
var val value.Value
var state State
val, state, err = i.ProcessSwitchStatement(t, debugState, true)
if val != value.Null {
return val, NONE, nil
}
if state != NONE {
return value.Null, state, nil
}
case *ast.RestartStatement:
// restart statement force change state to RESTART
return value.Null, RESTART, nil
case *ast.ReturnStatement:
var val value.Value
var state State
val, state, err = i.ProcessExpressionReturnStatement(t)
if err != nil {
return val, state, errors.WithStack(err)
}
// Functional subroutine can return state
// https://developer.fastly.com/reference/vcl/subroutines/#returning-a-state
if state != NONE {
return value.Null, state, nil
}
// Check return value type is the same
if string(val.Type()) != sub.ReturnType.Value {
return val, NONE, exception.Runtime(
&t.GetMeta().Token,
"Invalid return type, expects=%s, but got=%s",
sub.ReturnType.Value,
val.Type(),
)
}
return val, NONE, nil
case *ast.ErrorStatement:
// error statement force change state to ERROR
err = i.ProcessErrorStatement(t)
if err == nil {
return value.Null, ERROR, nil
}
case *ast.EsiStatement:
if err := i.ProcessEsiStatement(t); err != nil {
return value.Null, ERROR, nil
}
}
if err != nil {
return value.Null, INTERNAL_ERROR, errors.WithStack(err)
}
}
return value.Null, NONE, exception.Runtime(
&sub.GetMeta().Token,
"Functional subroutine %s did not return any values",
sub.Name.Value,
)
}
func (i *Interpreter) ProcessExpressionReturnStatement(stmt *ast.ReturnStatement) (value.Value, State, error) {
val, err := i.ProcessExpression(stmt.ReturnExpression, false)
if err != nil {
return value.Null, NONE, errors.WithStack(err)
}
if !val.IsLiteral() {
return value.Null, NONE, exception.Runtime(
&stmt.GetMeta().Token,
"Functional subroutine only can return value only accepts a literal value",
)
}
switch t := val.(type) {
case *value.Ident:
if v, ok := stateMap[t.Value]; ok {
return value.Null, v, nil
}
return value.Null, NONE, exception.Runtime(
&stmt.GetMeta().Token,
"Unexpected return state value: %s", t.Value,
)
default:
return val, NONE, nil
}
}
func (i *Interpreter) extractBoilerplateMacro(sub *ast.SubroutineDeclaration) error {
if i.ctx.FastlySnippets == nil {
return nil
}
// If subroutine name is fastly subroutine, find and extract boilerplate macro
macro, ok := context.FastlyReservedSubroutine[sub.Name.Value]
if !ok {
return nil
}
snippets, ok := i.ctx.FastlySnippets.ScopedSnippets[macro]
if !ok || len(snippets) == 0 {
return nil
}
macroName := strings.ToUpper("fastly " + macro)
var resolved []ast.Statement
// Find "FASTLY [macro]" comment and extract in infix comment of block statement
if hasFastlyBoilerplateMacro(sub.Block.Infix, macroName) {
for _, s := range snippets {
statements, err := loadStatementVCL(s.Name, s.Data)
if err != nil {
return errors.WithStack(err)
}
resolved = append(resolved, statements...)
}
// Prevent to block statements
sub.Block.Statements = append(resolved, sub.Block.Statements...)
return nil
}
// Find "FASTLY [macro]" comment and extract inside block statement
var found bool // guard flag, embedding macro should do only once
for _, stmt := range sub.Block.Statements {
if hasFastlyBoilerplateMacro(stmt.GetMeta().Leading, macroName) && !found {
for _, s := range snippets {
statements, err := loadStatementVCL(s.Name, s.Data)
if err != nil {
return errors.WithStack(err)
}
resolved = append(resolved, statements...)
}
found = true // guard for only once
}
resolved = append(resolved, stmt) // don't forget to append original statement
}
sub.Block.Statements = resolved
return nil
}
func hasFastlyBoilerplateMacro(cs ast.Comments, macroName string) bool {
for _, c := range cs {
line := strings.TrimLeft(c.String(), " */#")
if strings.HasPrefix(strings.ToUpper(line), macroName) {
return true
}
}
return false
}