From 2ac1f5b30f89953977aa7085c343c9e8073d47d9 Mon Sep 17 00:00:00 2001 From: visualfc Date: Sat, 9 Mar 2024 17:26:44 +0800 Subject: [PATCH] support load mixed go/gop project --- context.go | 1 + gopbuild/build.go | 6 ++++-- visit.go | 9 +++++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/context.go b/context.go index 687540de..e99e8231 100644 --- a/context.go +++ b/context.go @@ -55,6 +55,7 @@ const ( EnableNoStrict // Enable no strict mode ExperimentalSupportGC // experimental support runtime.GC SupportMultipleInterp // Support multiple interp, must manual release interp reflectx icall. + CheckGopOverloadFunc // Check and skip gop overload func ) // Loader types loader interface diff --git a/gopbuild/build.go b/gopbuild/build.go index bc3cabd0..f3a1da8c 100644 --- a/gopbuild/build.go +++ b/gopbuild/build.go @@ -144,6 +144,7 @@ func (p *Package) ToAst() *goast.File { type Context struct { ctx *igop.Context fset *token.FileSet + imp *igop.Importer gop igop.Loader } @@ -174,7 +175,8 @@ func NewContext(ctx *igop.Context) *Context { if ctx.IsEvalMode() { ctx = igop.NewContext(0) } - return &Context{ctx: ctx, fset: token.NewFileSet(), gop: igop.NewTypesLoader(ctx, 0)} + ctx.Mode |= igop.CheckGopOverloadFunc + return &Context{ctx: ctx, imp: igop.NewImporter(ctx), fset: token.NewFileSet(), gop: igop.NewTypesLoader(ctx, 0)} } func isGopPackage(path string) bool { @@ -190,7 +192,7 @@ func (c *Context) Import(path string) (*types.Package, error) { if isGopPackage(path) { return c.gop.Import(path) } - return c.ctx.Loader.Import(path) + return c.imp.Import(path) } func (c *Context) ParseDir(dir string) (*Package, error) { diff --git a/visit.go b/visit.go index 30df285f..b7bd7e38 100644 --- a/visit.go +++ b/visit.go @@ -93,8 +93,13 @@ func (visit *visitor) program() { sel := mset.At(i) obj := sel.Obj() // skip embed extern type method - if pkg := obj.Pkg(); pkg != nil && !chks[pkg.Path()] { - continue + if pkg := obj.Pkg(); pkg != nil { + if !chks[pkg.Path()] { + continue + } + if visit.intp.ctx.Mode&CheckGopOverloadFunc != 0 && obj.Pos() == token.NoPos { + continue + } } fn := visit.prog.MethodValue(sel) mmap[obj.Name()] = fn