Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime): move semantic analysis to the finalize step #2946

Merged
merged 1 commit into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ jobs:
test-race:
docker:
- image: quay.io/influxdb/flux-build:latest
resource_class: large
environment:
GOPATH: /tmp/go
GOFLAGS: -p=8
Expand Down
49 changes: 32 additions & 17 deletions runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var Default = &runtime{}
// runtime contains the flux runtime for interpreting and
// executing queries.
type runtime struct {
astPkgs map[string]*ast.Package
pkgs map[string]*semantic.Package
builtins map[string]map[string]values.Value
finalized bool
Expand Down Expand Up @@ -56,32 +57,19 @@ func (r *runtime) RegisterPackage(pkg *ast.Package) error {
return errors.New(codes.Internal, "already finalized, cannot register builtin package")
}

if r.pkgs == nil {
r.pkgs = make(map[string]*semantic.Package)
if r.astPkgs == nil {
r.astPkgs = make(map[string]*ast.Package)
}

if _, ok := r.pkgs[pkg.Path]; ok {
if _, ok := r.astPkgs[pkg.Path]; ok {
return errors.Newf(codes.Internal, "duplicate builtin package %q", pkg.Path)
}

if ast.Check(pkg) > 0 {
err := ast.GetError(pkg)
return errors.Wrapf(err, codes.Inherit, "failed to parse builtin package %q", pkg.Path)
}

bs, err := json.Marshal(pkg)
if err != nil {
return err
}
hdl, err := r.JSONToHandle(bs)
if err != nil {
return err
}
root, err := AnalyzePackage(hdl)
if err != nil {
return err
}
r.pkgs[pkg.Path] = root
r.astPkgs[pkg.Path] = pkg
return nil
}

Expand Down Expand Up @@ -192,11 +180,38 @@ func (r *runtime) Stdlib() interpreter.Importer {
return &importer{r: r}
}

func (r *runtime) compilePackages() error {
pkgs := make(map[string]*semantic.Package)
for _, pkg := range r.astPkgs {
bs, err := json.Marshal(pkg)
if err != nil {
return err
}
hdl, err := r.JSONToHandle(bs)
if err != nil {
return err
}
root, err := AnalyzePackage(hdl)
if err != nil {
return err
}
pkgs[pkg.Path] = root
}
r.pkgs = pkgs
r.astPkgs = nil
return nil
}

func (r *runtime) Finalize() error {
if r.finalized {
return errors.New(codes.Internal, "already finalized")
}
r.finalized = true

if err := r.compilePackages(); err != nil {
return err
}

for path, pkg := range r.builtins {
semPkg, ok := r.pkgs[path]
if !ok {
Expand Down