MCPcopy Create free account
hub / github.com/cel-expr/cel-go / newProgram

Function newProgram

cel/program.go:310–457  ·  view source on GitHub ↗

newProgram creates a program instance with an environment, an ast, and an optional list of ProgramOption values. If the program cannot be configured the prog will be nil, with a non-nil error response.

(e *Env, a *ast.AST, opts []ProgramOption)

Source from the content-addressed store, hash-verified

308//
309// If the program cannot be configured the prog will be nil, with a non-nil error response.
310func newProgram(e *Env, a *ast.AST, opts []ProgramOption) (Program, error) {
311 // Build the env's function bindings and shared dispatcher once (pure functions of the
312 // env). The dispatcher holding the env's function bindings is identical across every
313 // Program() built from it and read-only during planning — so assemble it once per env
314 // and layer a thin child over it here for per-program Functions() isolation, rather than
315 // re-indexing overloads on every Program() call.
316 sharedDisp, hasAsync, err := e.initDispatcher()
317 if err != nil {
318 return nil, err
319 }
320 disp := interpreter.ExtendDispatcher(sharedDisp)
321
322 // Ensure the default attribute factory is set after the adapter and provider are
323 // configured.
324 p := &prog{
325 Env: e,
326 plannerOptions: []interpreter.PlannerOption{},
327 dispatcher: disp,
328 costOptions: []cost.TrackerOption{},
329 drainStrategy: async.DrainReady(100 * time.Microsecond),
330 hasAsync: hasAsync,
331 }
332
333 // Configure the program via the ProgramOption values.
334 for _, opt := range opts {
335 p, err = opt(p)
336 if err != nil {
337 return nil, err
338 }
339 }
340
341 // Set the attribute factory after the options have been set.
342 var attrFactory interpreter.AttributeFactory
343 attrFactorOpts := []interpreter.AttrFactoryOption{
344 interpreter.EnableErrorOnBadPresenceTest(p.HasFeature(featureEnableErrorOnBadPresenceTest)),
345 }
346 if a.SourceInfo().HasExtension("json_name", ast.NewExtensionVersion(1, 1)) {
347 if !e.HasFeature(featureJSONFieldNames) {
348 return nil, errors.New("the AST extension 'json_name' requires the option cel.JSONFieldNames(true)")
349 }
350 }
351 // Configure the type provider, considering whether the AST indicates whether it supports JSON field names
352 if p.evalOpts&OptPartialEval == OptPartialEval {
353 attrFactory = interpreter.NewPartialAttributeFactory(e.Container, e.adapter, e.provider, attrFactorOpts...)
354 } else {
355 attrFactory = interpreter.NewAttributeFactory(e.Container, e.adapter, e.provider, attrFactorOpts...)
356 }
357 interp := interpreter.NewInterpreter(disp, e.Container, e.provider, e.adapter, attrFactory)
358 p.interpreter = interp
359
360 // Translate the EvalOption flags into InterpretableDecorator instances.
361 plannerOptions := make([]interpreter.PlannerOption, len(p.plannerOptions))
362 copy(plannerOptions, p.plannerOptions)
363
364 // Enable interrupt checking if there's a non-zero check frequency
365 if p.interruptCheckFrequency > 0 {
366 plannerOptions = append(plannerOptions, interpreter.InterruptableEval())
367 }

Callers 1

PlanProgramMethod · 0.85

Calls 15

CloneMethod · 0.95
initInterpretableMethod · 0.95
ExtendDispatcherFunction · 0.92
DrainReadyFunction · 0.92
NewExtensionVersionFunction · 0.92
NewAttributeFactoryFunction · 0.92
NewInterpreterFunction · 0.92
InterruptableEvalFunction · 0.92
OptimizeFunction · 0.92
CompileRegexConstantsFunction · 0.92

Tested by

no test coverage detected