Skip to content
Permalink
Browse files

vm: Internalize waiting for flush and emiting dependencies

Now that we have a proper vm object we can hide things under the carpet a bit
better.
  • Loading branch information...
dlespiau committed May 10, 2019
1 parent 24b06c3 commit ce7a553b6ccfb88d861cf36c766776739cf5f1c7
Showing with 22 additions and 12 deletions.
  1. +1 −0 go.mod
  2. +2 −0 go.sum
  3. +0 −3 run.go
  4. +19 −9 vm.go
1 go.mod
@@ -5,6 +5,7 @@ require (
github.com/google/flatbuffers v1.10.0
github.com/hashicorp/hcl v1.0.0
github.com/jkcfg/v8worker2 v0.0.0-20190228210604-6677fe93c5c2
github.com/pkg/errors v0.8.1
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shurcooL/httpfs v0.0.0-20171119174359-809beceb2371 // indirect
github.com/shurcooL/vfsgen v0.0.0-20181020040650-a97a25d856ca
2 go.sum
@@ -14,6 +14,8 @@ github.com/jkcfg/v8worker2 v0.0.0-20190217191700-405919a64720 h1:YBgCfrSk4xZOH//
github.com/jkcfg/v8worker2 v0.0.0-20190217191700-405919a64720/go.mod h1:V1TBZ48loRvHpVCQEQSt39QYggAjIWgCaA63Z7NuGPI=
github.com/jkcfg/v8worker2 v0.0.0-20190228210604-6677fe93c5c2 h1:yroXZIO0q4uBioF+qxfrstz58/0kCKGJsBFd1Vd2OYg=
github.com/jkcfg/v8worker2 v0.0.0-20190228210604-6677fe93c5c2/go.mod h1:V1TBZ48loRvHpVCQEQSt39QYggAjIWgCaA63Z7NuGPI=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/ry/v8worker2 v0.0.0-20190118154056-6857a7739bc2 h1:nvqxIeI0eVhPV4d3UubaTiadSnAha1DaqMOBN0E3kqg=
3 run.go
@@ -123,7 +123,4 @@ func run(cmd *cobra.Command, args []string) {
if runErr != nil {
log.Fatal(runErr)
}

vm.Wait()
vm.Finish()
}
28 vm.go
@@ -8,12 +8,14 @@ import (
"os"
"path/filepath"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/jkcfg/jk/pkg/deferred"
"github.com/jkcfg/jk/pkg/record"
"github.com/jkcfg/jk/pkg/resolve"
"github.com/jkcfg/jk/pkg/std"
v8 "github.com/jkcfg/v8worker2"
"github.com/spf13/cobra"
)

type vmOptions struct {
@@ -158,16 +160,20 @@ func (vm *vm) resolver() *resolve.Resolver {

func (vm *vm) Run(specifier string, source string) error {
resolver := vm.resolver()
return vm.worker.LoadModule(specifier, source, resolver.ResolveModule)
if err := vm.worker.LoadModule(specifier, source, resolver.ResolveModule); err != nil {
return err
}
return vm.flush()
}

func (vm *vm) RunModule(specifier string, referrer string) error {
resolver := vm.resolver()
_, ret := resolver.ResolveModule(specifier, referrer)
if ret != 0 {
return fmt.Errorf("unable to load module %q", specifier)
err := fmt.Errorf("unable to load module %q", specifier)
return errors.Wrap(err, "run-module")
}
return nil
return vm.flush()
}

func (vm *vm) RunFile(filename string) error {
@@ -185,19 +191,23 @@ func (vm *vm) RunFile(filename string) error {
}

resolver := vm.resolver()
return vm.worker.LoadModule(filepath.Base(filename), string(input), resolver.ResolveModule)
if err := vm.worker.LoadModule(filepath.Base(filename), string(input), resolver.ResolveModule); err != nil {
return err
}

return vm.flush()
}

func (vm *vm) Wait() {
func (vm *vm) flush() error {
deferred.Wait() // TODO(michael): hide this in std?
}

func (vm *vm) Finish() {
if vm.recorder != nil {
data, err := json.MarshalIndent(vm.recorder, "", " ")
if err != nil {
log.Fatal("emit-dependencies:", err)
return errors.Wrap(err, "emit-dependencies")
}
fmt.Println(string(data))
}

return nil
}

0 comments on commit ce7a553

Please sign in to comment.
You can’t perform that action at this time.