From d6a6734b6598e13a7ef1ab5f0b69fb27f485e4eb Mon Sep 17 00:00:00 2001 From: James Shubin Date: Sat, 16 Mar 2024 00:35:33 -0400 Subject: [PATCH] lang: Plumb the ctx deeper into the lang API --- lang/gapi/gapi.go | 6 +++--- lang/lang.go | 4 ++-- lang/lang_test.go | 14 +++++++------- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lang/gapi/gapi.go b/lang/gapi/gapi.go index 5f498aaa..bcfc48ca 100644 --- a/lang/gapi/gapi.go +++ b/lang/gapi/gapi.go @@ -427,7 +427,7 @@ func (obj *GAPI) Init(data *gapi.Data) error { } // LangInit is a wrapper around the lang Init method. -func (obj *GAPI) LangInit() error { +func (obj *GAPI) LangInit(ctx context.Context) error { if obj.lang != nil { return nil // already ran init, close first! } @@ -456,7 +456,7 @@ func (obj *GAPI) LangInit() error { obj.data.Logf(Name+": "+format, v...) }, } - if err := lang.Init(); err != nil { + if err := lang.Init(ctx); err != nil { return errwrap.Wrapf(err, "can't init the lang") } obj.lang = lang // once we can't fail, store the struct... @@ -553,7 +553,7 @@ func (obj *GAPI) Next() chan gapi.Next { // run up to these three but fail on err if e := obj.LangClose(); e != nil { // close any old lang err = e // pass through the err - } else if e := obj.LangInit(); e != nil { // init the new one! + } else if e := obj.LangInit(context.TODO()); e != nil { // init the new one! err = e // pass through the err // Always run LangClose after LangInit diff --git a/lang/lang.go b/lang/lang.go index 7b37ece2..33a06436 100644 --- a/lang/lang.go +++ b/lang/lang.go @@ -99,7 +99,7 @@ type Lang struct { // NOTE: The trick is that we need to get the list of funcs to watch AND start // watching them, *before* we pull their values, that way we'll know if they // changed from the values we wanted. -func (obj *Lang) Init() error { +func (obj *Lang) Init(ctx context.Context) error { if obj.Debug { obj.Logf("input: %s", obj.Input) tree, err := util.FsTree(obj.Fs, "/") // should look like gapi @@ -232,7 +232,7 @@ func (obj *Lang) Init() error { Debug: obj.Debug, Logf: logf, } - unifyErr := unifier.Unify(context.TODO()) + unifyErr := unifier.Unify(ctx) obj.Logf("type unification took: %s", time.Since(timing)) if unifyErr != nil { return errwrap.Wrapf(unifyErr, "could not unify types") diff --git a/lang/lang_test.go b/lang/lang_test.go index 945db07d..f2ff9569 100644 --- a/lang/lang_test.go +++ b/lang/lang_test.go @@ -128,23 +128,23 @@ func runInterpret(t *testing.T, code string) (_ *pgraph.Graph, reterr error) { } logf("tree:\n%s", tree) + wg := &sync.WaitGroup{} + defer wg.Wait() + + ctx, cancel := context.WithCancel(context.Background()) // TODO: get it from parent + defer cancel() + lang := &Lang{ Fs: fs, Input: "/" + interfaces.MetadataFilename, // start path in fs Debug: testing.Verbose(), // set via the -test.v flag to `go test` Logf: logf, } - if err := lang.Init(); err != nil { + if err := lang.Init(ctx); err != nil { return nil, errwrap.Wrapf(err, "init failed") } defer lang.Cleanup() - wg := &sync.WaitGroup{} - defer wg.Wait() - - ctx, cancel := context.WithCancel(context.Background()) // TODO: get it from parent - defer cancel() - wg.Add(1) go func() { defer wg.Done()