Skip to content

Commit

Permalink
Adding Augmentor
Browse files Browse the repository at this point in the history
  • Loading branch information
grantnelson-wf committed Jan 8, 2025
1 parent 289ebba commit a0e0969
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
28 changes: 22 additions & 6 deletions build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,25 @@ type overrideInfo struct {
overrideSignature *ast.FuncDecl
}

type pkgOverrideInfo struct {
overrides map[string]overrideInfo
}

type Augmentor struct {
packages map[string]*pkgOverrideInfo
}

func (aug *Augmentor) Augment(fset *token.FileSet, filename string, src *ast.File) error {
pkgName := src.Name.Name

return nil
}

// parseAndAugment parses and returns all .go files of given pkg.
// Standard Go library packages are augmented with files in compiler/natives folder.
// If isTest is true and pkg.ImportPath has no _test suffix, package is built for running internal tests.
// If isTest is true and pkg.ImportPath has _test suffix, package is built for running external tests.
//
// If pkg.IsTest is true and pkg.ImportPath has no _test suffix, package is built for running internal tests.
// If pkg.IsTest is true and pkg.ImportPath has _test suffix, package is built for running external tests.
//
// The native packages are augmented by the contents of natives.FS in the following way.
// The file names do not matter except the usual `_test` suffix. The files for
Expand All @@ -164,7 +179,8 @@ type overrideInfo struct {
// - Otherwise for identifiers that exist in the original and the overrides,
// the original is removed.
// - New identifiers that don't exist in original package get added.
func parseAndAugment(xctx XContext, pkg *PackageData, isTest bool, fileSet *token.FileSet) ([]*ast.File, []JSFile, error) {
func parseAndAugment(xctx XContext, pkg *PackageData, fileSet *token.FileSet) ([]*ast.File, []JSFile, error) {
isTest := pkg.IsTest
jsFiles, overlayFiles := parseOverlayFiles(xctx, pkg, isTest, fileSet)

originalFiles, err := parserOriginalFiles(pkg, fileSet)
Expand All @@ -176,7 +192,7 @@ func parseAndAugment(xctx XContext, pkg *PackageData, isTest bool, fileSet *toke
for _, file := range overlayFiles {
augmentOverlayFile(file, overrides)
}
delete(overrides, "init")
delete(overrides, `init`)

for _, file := range originalFiles {
augmentOriginalImports(pkg.ImportPath, file)
Expand All @@ -194,8 +210,8 @@ func parseAndAugment(xctx XContext, pkg *PackageData, isTest bool, fileSet *toke
// parseOverlayFiles loads and parses overlay files
// to augment the original files with.
func parseOverlayFiles(xctx XContext, pkg *PackageData, isTest bool, fileSet *token.FileSet) ([]JSFile, []*ast.File) {
isXTest := strings.HasSuffix(pkg.ImportPath, "_test")
importPath := pkg.ImportPath
isXTest := strings.HasSuffix(importPath, "_test")
if isXTest {
importPath = importPath[:len(importPath)-5]
}
Expand Down Expand Up @@ -1004,7 +1020,7 @@ func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) {

// Existing archive is out of date or doesn't exist, let's build the package.
fileSet := token.NewFileSet()
files, overlayJsFiles, err := parseAndAugment(s.xctx, pkg, pkg.IsTest, fileSet)
files, overlayJsFiles, err := parseAndAugment(s.xctx, pkg, fileSet)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestNativesDontImportExtraPackages(t *testing.T) {

// Use parseAndAugment to get a list of augmented AST files.
fset := token.NewFileSet()
files, _, err := parseAndAugment(stdOnly, pkgVariant, pkgVariant.IsTest, fset)
files, _, err := parseAndAugment(stdOnly, pkgVariant, fset)
if err != nil {
t.Fatalf("github.com/gopherjs/gopherjs/build.parseAndAugment: %v", err)
}
Expand Down

0 comments on commit a0e0969

Please sign in to comment.