Skip to content

Commit e73454f

Browse files
replaced ParsedPackage with sources
1 parent eac0953 commit e73454f

File tree

7 files changed

+196
-160
lines changed

7 files changed

+196
-160
lines changed

build/build.go

Lines changed: 44 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import (
2828
"github.com/gopherjs/gopherjs/compiler"
2929
"github.com/gopherjs/gopherjs/compiler/astutil"
3030
"github.com/gopherjs/gopherjs/compiler/errorList"
31+
"github.com/gopherjs/gopherjs/compiler/jsFile"
32+
"github.com/gopherjs/gopherjs/compiler/sources"
3133
"github.com/gopherjs/gopherjs/internal/testmain"
3234
log "github.com/sirupsen/logrus"
3335

@@ -164,7 +166,7 @@ type overrideInfo struct {
164166
// - Otherwise for identifiers that exist in the original and the overrides,
165167
// the original is removed.
166168
// - New identifiers that don't exist in original package get added.
167-
func parseAndAugment(xctx XContext, pkg *PackageData, isTest bool, fileSet *token.FileSet) ([]*ast.File, []JSFile, error) {
169+
func parseAndAugment(xctx XContext, pkg *PackageData, isTest bool, fileSet *token.FileSet) ([]*ast.File, []jsFile.JSFile, error) {
168170
jsFiles, overlayFiles := parseOverlayFiles(xctx, pkg, isTest, fileSet)
169171

170172
originalFiles, err := parserOriginalFiles(pkg, fileSet)
@@ -193,7 +195,7 @@ func parseAndAugment(xctx XContext, pkg *PackageData, isTest bool, fileSet *toke
193195

194196
// parseOverlayFiles loads and parses overlay files
195197
// to augment the original files with.
196-
func parseOverlayFiles(xctx XContext, pkg *PackageData, isTest bool, fileSet *token.FileSet) ([]JSFile, []*ast.File) {
198+
func parseOverlayFiles(xctx XContext, pkg *PackageData, isTest bool, fileSet *token.FileSet) ([]jsFile.JSFile, []*ast.File) {
197199
isXTest := strings.HasSuffix(pkg.ImportPath, "_test")
198200
importPath := pkg.ImportPath
199201
if isXTest {
@@ -623,18 +625,11 @@ func (o *Options) PrintSuccess(format string, a ...interface{}) {
623625
fmt.Fprintf(os.Stderr, format, a...)
624626
}
625627

626-
// JSFile represents a *.inc.js file metadata and content.
627-
type JSFile struct {
628-
Path string // Full file path for the build context the file came from.
629-
ModTime time.Time
630-
Content []byte
631-
}
632-
633628
// PackageData is an extension of go/build.Package with additional metadata
634629
// GopherJS requires.
635630
type PackageData struct {
636631
*build.Package
637-
JSFiles []JSFile
632+
JSFiles []jsFile.JSFile
638633
// IsTest is true if the package is being built for running tests.
639634
IsTest bool
640635
SrcModTime time.Time
@@ -744,43 +739,6 @@ func (p *PackageData) InstallPath() string {
744739
return p.PkgObj
745740
}
746741

747-
// ParsedPackage is the results from building a package before it is compiled.
748-
type ParsedPackage struct {
749-
ImportPath string // import path of package ("" if unknown)
750-
Dir string // directory containing package sources
751-
752-
// GoFiles is the parsed and augmented Go AST files for the package.
753-
GoFiles []*ast.File
754-
FileSet *token.FileSet
755-
JSFiles []JSFile
756-
}
757-
758-
// Imports calculates the import paths of the package's dependencies
759-
// based on all the imports in the augmented files.
760-
//
761-
// The given skip paths will not be returned in the results.
762-
// This will not return any `*_test` packages in the results.
763-
func (p *ParsedPackage) Imports(skip ...string) []string {
764-
seen := make(map[string]struct{})
765-
for _, s := range skip {
766-
seen[s] = struct{}{}
767-
}
768-
imports := []string{}
769-
for _, file := range p.GoFiles {
770-
for _, imp := range file.Imports {
771-
path := strings.Trim(imp.Path.Value, `"`)
772-
if _, ok := seen[path]; !ok {
773-
if !strings.HasSuffix(path, "_test") {
774-
imports = append(imports, path)
775-
}
776-
seen[path] = struct{}{}
777-
}
778-
}
779-
}
780-
sort.Strings(imports)
781-
return imports
782-
}
783-
784742
// Session manages internal state GopherJS requires to perform a build.
785743
//
786744
// This is the main interface to GopherJS build system. Session lifetime is
@@ -794,10 +752,11 @@ type Session struct {
794752
// compilation when we're looking up parsed packages.
795753
importPaths map[string]map[string]string
796754

797-
// parsePackage is a map of parsed packages that have been built and augmented.
755+
// sources is a map of parsed packages that have been built and augmented.
798756
// This is keyed using resolved import paths. This is used to avoid
799757
// rebuilding and augmenting packages that are imported by several packages.
800-
parsedPackages map[string]*ParsedPackage
758+
// These sources haven't been sorted nor simplified yet.
759+
sources map[string]*sources.Sources
801760

802761
// Binary archives produced during the current session and assumed to be
803762
// up to date with input sources and dependencies. In the -w ("watch") mode
@@ -814,7 +773,7 @@ func NewSession(options *Options) (*Session, error) {
814773
s := &Session{
815774
options: options,
816775
importPaths: make(map[string]map[string]string),
817-
parsedPackages: make(map[string]*ParsedPackage),
776+
sources: make(map[string]*sources.Sources),
818777
UpToDateArchives: make(map[string]*compiler.Archive),
819778
}
820779
s.xctx = NewBuildContext(s.InstallSuffix(), s.options.BuildTags)
@@ -932,7 +891,7 @@ func (s *Session) BuildFiles(filenames []string, pkgObj string, cwd string) erro
932891
if err != nil {
933892
return fmt.Errorf("failed to stat %s: %w", file, err)
934893
}
935-
pkg.JSFiles = append(pkg.JSFiles, JSFile{
894+
pkg.JSFiles = append(pkg.JSFiles, jsFile.JSFile{
936895
Path: filepath.Join(pkg.Dir, filepath.Base(file)),
937896
ModTime: info.ModTime(),
938897
Content: content,
@@ -955,13 +914,13 @@ func (s *Session) BuildProject(pkg *PackageData) (*compiler.Archive, error) {
955914
// ensure that runtime for gopherjs is imported
956915
pkg.Imports = append(pkg.Imports, `runtime`)
957916

958-
// Build the project to get the parsed packages.
959-
var parsed *ParsedPackage
917+
// Build the project to get the sources for the parsed packages.
918+
var srcs *sources.Sources
960919
var err error
961920
if pkg.IsTest {
962-
parsed, err = s.buildTestPackage(pkg)
921+
srcs, err = s.buildTestPackage(pkg)
963922
} else {
964-
parsed, err = s.buildPackages(pkg)
923+
srcs, err = s.buildPackages(pkg)
965924
}
966925
if err != nil {
967926
return nil, err
@@ -973,10 +932,10 @@ func (s *Session) BuildProject(pkg *PackageData) (*compiler.Archive, error) {
973932
// flatten, blocking, etc. information and check types to get the type info
974933
// with all the instances for all generics in the whole project.
975934

976-
return s.compilePackages(parsed)
935+
return s.compilePackages(srcs)
977936
}
978937

979-
func (s *Session) buildTestPackage(pkg *PackageData) (*ParsedPackage, error) {
938+
func (s *Session) buildTestPackage(pkg *PackageData) (*sources.Sources, error) {
980939
_, err := s.buildPackages(pkg.TestPackage())
981940
if err != nil {
982941
return nil, err
@@ -995,30 +954,30 @@ func (s *Session) buildTestPackage(pkg *PackageData) (*ParsedPackage, error) {
995954
return nil, fmt.Errorf("failed to generate testmain package for %s: %w", pkg.ImportPath, err)
996955
}
997956

998-
// Create a parsed package for the testmain package.
999-
parsed := &ParsedPackage{
957+
// Create the sources for parsed package for the testmain package.
958+
srcs := &sources.Sources{
1000959
ImportPath: mainPkg.ImportPath,
1001960
Dir: mainPkg.Dir,
1002-
GoFiles: []*ast.File{mainFile},
961+
Files: []*ast.File{mainFile},
1003962
FileSet: fset,
1004963
}
1005964

1006965
// Import dependencies for the testmain package.
1007-
for _, importedPkgPath := range parsed.Imports() {
966+
for _, importedPkgPath := range srcs.Imports() {
1008967
_, _, err := s.buildImportPathWithSrcDir(importedPkgPath, pkg.Dir)
1009968
if err != nil {
1010969
return nil, err
1011970
}
1012971
}
1013972

1014-
return parsed, nil
973+
return srcs, nil
1015974
}
1016975

1017-
// buildImportPathWithSrcDir builds the parsed package specified by the import path.
976+
// buildImportPathWithSrcDir builds the sources for a package specified by the import path.
1018977
//
1019978
// Relative import paths are interpreted relative to the passed srcDir. If
1020979
// srcDir is empty, current working directory is assumed.
1021-
func (s *Session) buildImportPathWithSrcDir(path, srcDir string) (*PackageData, *ParsedPackage, error) {
980+
func (s *Session) buildImportPathWithSrcDir(path, srcDir string) (*PackageData, *sources.Sources, error) {
1022981
pkg, err := s.xctx.Import(path, srcDir, 0)
1023982
if s.Watcher != nil && pkg != nil { // add watch even on error
1024983
s.Watcher.Add(pkg.Dir)
@@ -1027,13 +986,13 @@ func (s *Session) buildImportPathWithSrcDir(path, srcDir string) (*PackageData,
1027986
return nil, nil, err
1028987
}
1029988

1030-
parsed, err := s.buildPackages(pkg)
989+
srcs, err := s.buildPackages(pkg)
1031990
if err != nil {
1032991
return nil, nil, err
1033992
}
1034993

1035994
s.cacheImportPath(path, srcDir, pkg.ImportPath)
1036-
return pkg, parsed, nil
995+
return pkg, srcs, nil
1037996
}
1038997

1039998
// cacheImportPath stores the import path for the build package so we can look
@@ -1074,9 +1033,9 @@ var getExeModTime = func() func() time.Time {
10741033
}
10751034
}()
10761035

1077-
func (s *Session) buildPackages(pkg *PackageData) (*ParsedPackage, error) {
1078-
if parsed, ok := s.parsedPackages[pkg.ImportPath]; ok {
1079-
return parsed, nil
1036+
func (s *Session) buildPackages(pkg *PackageData) (*sources.Sources, error) {
1037+
if srcs, ok := s.sources[pkg.ImportPath]; ok {
1038+
return srcs, nil
10801039
}
10811040

10821041
if exeModTime := getExeModTime(); exeModTime.After(pkg.SrcModTime) {
@@ -1115,52 +1074,52 @@ func (s *Session) buildPackages(pkg *PackageData) (*ParsedPackage, error) {
11151074
files = append(files, embed)
11161075
}
11171076

1118-
parsed := &ParsedPackage{
1077+
srcs := &sources.Sources{
11191078
ImportPath: pkg.ImportPath,
11201079
Dir: pkg.Dir,
1121-
GoFiles: files,
1080+
Files: files,
11221081
FileSet: fileSet,
11231082
JSFiles: append(pkg.JSFiles, overlayJsFiles...),
11241083
}
1125-
s.parsedPackages[pkg.ImportPath] = parsed
1084+
s.sources[pkg.ImportPath] = srcs
11261085

11271086
// Import dependencies from the augmented files,
11281087
// whilst skipping any that have been already imported.
1129-
for _, importedPkgPath := range parsed.Imports(pkg.Imports...) {
1088+
for _, importedPkgPath := range srcs.Imports(pkg.Imports...) {
11301089
_, _, err := s.buildImportPathWithSrcDir(importedPkgPath, pkg.Dir)
11311090
if err != nil {
11321091
return nil, err
11331092
}
11341093
}
11351094

1136-
return parsed, nil
1095+
return srcs, nil
11371096
}
11381097

1139-
func (s *Session) compilePackages(pkg *ParsedPackage) (*compiler.Archive, error) {
1140-
if archive, ok := s.UpToDateArchives[pkg.ImportPath]; ok {
1098+
func (s *Session) compilePackages(srcs *sources.Sources) (*compiler.Archive, error) {
1099+
if archive, ok := s.UpToDateArchives[srcs.ImportPath]; ok {
11411100
return archive, nil
11421101
}
11431102

11441103
importContext := &compiler.ImportContext{
11451104
Packages: s.Types,
1146-
ImportArchive: s.ImportResolverFor(pkg.Dir),
1105+
ImportArchive: s.ImportResolverFor(srcs.Dir),
11471106
}
1148-
archive, err := compiler.Compile(pkg.ImportPath, pkg.GoFiles, pkg.FileSet, importContext, s.options.Minify)
1107+
archive, err := compiler.Compile(*srcs, importContext, s.options.Minify)
11491108
if err != nil {
11501109
return nil, err
11511110
}
11521111

1153-
for _, jsFile := range pkg.JSFiles {
1112+
for _, jsFile := range srcs.JSFiles {
11541113
archive.IncJSCode = append(archive.IncJSCode, []byte("\t(function() {\n")...)
11551114
archive.IncJSCode = append(archive.IncJSCode, jsFile.Content...)
11561115
archive.IncJSCode = append(archive.IncJSCode, []byte("\n\t}).call($global);\n")...)
11571116
}
11581117

11591118
if s.options.Verbose {
1160-
fmt.Println(pkg.ImportPath)
1119+
fmt.Println(srcs.ImportPath)
11611120
}
11621121

1163-
s.UpToDateArchives[pkg.ImportPath] = archive
1122+
s.UpToDateArchives[srcs.ImportPath] = archive
11641123

11651124
return archive, nil
11661125
}
@@ -1198,12 +1157,12 @@ func (s *Session) ImportResolverFor(srcDir string) func(string) (*compiler.Archi
11981157
return archive, nil
11991158
}
12001159

1201-
// The archive hasn't been compiled yet so compile it with the parsed package.
1202-
if parsed, ok := s.parsedPackages[importPath]; ok {
1203-
return s.compilePackages(parsed)
1160+
// The archive hasn't been compiled yet so compile it with the sources.
1161+
if srcs, ok := s.sources[importPath]; ok {
1162+
return s.compilePackages(srcs)
12041163
}
12051164

1206-
return nil, fmt.Errorf(`parsed package for %q not found`, importPath)
1165+
return nil, fmt.Errorf(`sources for %q not found`, importPath)
12071166
}
12081167
}
12091168

build/context.go

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"go/build"
66
"go/token"
7-
"io"
87
"net/http"
98
"os"
109
"os/exec"
@@ -16,6 +15,7 @@ import (
1615
_ "github.com/gopherjs/gopherjs/build/versionhack" // go/build release tags hack.
1716
"github.com/gopherjs/gopherjs/compiler"
1817
"github.com/gopherjs/gopherjs/compiler/gopherjspkg"
18+
"github.com/gopherjs/gopherjs/compiler/jsFile"
1919
"github.com/gopherjs/gopherjs/compiler/natives"
2020
"golang.org/x/tools/go/buildutil"
2121
)
@@ -91,7 +91,7 @@ func (sc simpleCtx) Import(importPath string, srcDir string, mode build.ImportMo
9191
if err != nil {
9292
return nil, err
9393
}
94-
jsFiles, err := jsFilesFromDir(&sc.bctx, pkg.Dir)
94+
jsFiles, err := jsFile.JSFilesFromDir(&sc.bctx, pkg.Dir)
9595
if err != nil {
9696
return nil, fmt.Errorf("failed to enumerate .inc.js files in %s: %w", pkg.Dir, err)
9797
}
@@ -440,40 +440,3 @@ func updateImports(sources []string, importPos map[string][]token.Position) (new
440440
sort.Strings(newImports)
441441
return newImports, newImportPos
442442
}
443-
444-
// jsFilesFromDir finds and loads any *.inc.js packages in the build context
445-
// directory.
446-
func jsFilesFromDir(bctx *build.Context, dir string) ([]JSFile, error) {
447-
files, err := buildutil.ReadDir(bctx, dir)
448-
if err != nil {
449-
return nil, err
450-
}
451-
var jsFiles []JSFile
452-
for _, file := range files {
453-
if !strings.HasSuffix(file.Name(), ".inc.js") || file.IsDir() {
454-
continue
455-
}
456-
if file.Name()[0] == '_' || file.Name()[0] == '.' {
457-
continue // Skip "hidden" files that are typically ignored by the Go build system.
458-
}
459-
460-
path := buildutil.JoinPath(bctx, dir, file.Name())
461-
f, err := buildutil.OpenFile(bctx, path)
462-
if err != nil {
463-
return nil, fmt.Errorf("failed to open %s from %v: %w", path, bctx, err)
464-
}
465-
defer f.Close()
466-
467-
content, err := io.ReadAll(f)
468-
if err != nil {
469-
return nil, fmt.Errorf("failed to read %s from %v: %w", path, bctx, err)
470-
}
471-
472-
jsFiles = append(jsFiles, JSFile{
473-
Path: path,
474-
ModTime: file.ModTime(),
475-
Content: content,
476-
})
477-
}
478-
return jsFiles, nil
479-
}

compiler/compiler_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"golang.org/x/tools/go/packages"
1313

1414
"github.com/gopherjs/gopherjs/compiler/internal/dce"
15+
"github.com/gopherjs/gopherjs/compiler/sources"
1516
"github.com/gopherjs/gopherjs/internal/srctesting"
1617
)
1718

@@ -693,8 +694,14 @@ func compileProject(t *testing.T, root *packages.Package, minify bool) map[strin
693694
}
694695
importContext.Packages[path] = pkg.Types
695696

697+
srcs := sources.Sources{
698+
ImportPath: path,
699+
Files: pkg.Syntax,
700+
FileSet: pkg.Fset,
701+
}
702+
696703
// compile package
697-
a, err := Compile(path, pkg.Syntax, pkg.Fset, importContext, minify)
704+
a, err := Compile(srcs, importContext, minify)
698705
if err != nil {
699706
return nil, err
700707
}

0 commit comments

Comments
 (0)