Skip to content

compiler: remove dependency on legacy gcimporter15 #803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
compiler: remove dependency on legacy gcimporter15.
This is a follow up to #789 that removes the sticking plaster in that
PR, and switches the compiler to using
golang.org/x/tools/go/gcexportdata as directed here
https://github.com/golang/tools/blob/14d5b80f954f510ba9bcd241821b6f4828498e1a/go/gcimporter15/gcimporter.go#L13-L14.

This means we can also remove the temporary vendor of gcimporter15.
  • Loading branch information
myitcv committed Apr 23, 2018
commit a6543abb280bbc0e061eacd6485a8daa3724e148
5 changes: 3 additions & 2 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"strings"

"github.com/gopherjs/gopherjs/compiler/prelude"
"golang.org/x/tools/go/gcimporter15"
"golang.org/x/tools/go/gcexportdata"
)

var sizes32 = &types.StdSizes{WordSize: 4, MaxAlign: 8}
Expand Down Expand Up @@ -243,7 +243,8 @@ func ReadArchive(filename, path string, r io.Reader, packages map[string]*types.
}

var err error
_, packages[path], err = gcimporter.BImportData(token.NewFileSet(), packages, a.ExportData, path)
b := bytes.NewReader(a.ExportData)
packages[path], err = gcexportdata.Read(b, token.NewFileSet(), packages, path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

b is not a descriptive variable name. That's often okay in short functions, but it doesn't work well in this context. It's also typically associated with []byte type, not io.Reader. I see r is already taken and I don't think it's a good idea to reuse it.

I think it's better to either use a longer descriptive name here:

exportData := bytes.NewReader(a.ExportData)
packages[path], err = gcexportdata.Read(exportData, token.NewFileSet(), packages, path)

Which would mirror the gcexportdata.Write code below.

But I think this line is not that long, that it's even better to just inline it:

packages[path], err = gcexportdata.Read(bytes.NewReader(a.ExportData), token.NewFileSet(), packages, path)

Either of those is okay.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if err != nil {
return nil, err
}
Expand Down
9 changes: 6 additions & 3 deletions compiler/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

"github.com/gopherjs/gopherjs/compiler/analysis"
"github.com/neelance/astrewrite"
"golang.org/x/tools/go/gcimporter15"
"golang.org/x/tools/go/gcexportdata"
"golang.org/x/tools/go/types/typeutil"
)

Expand Down Expand Up @@ -164,7 +164,10 @@ func Compile(importPath string, files []*ast.File, fileSet *token.FileSet, impor
}
importContext.Packages[importPath] = typesPkg

exportData := gcimporter.BExportData(nil, typesPkg)
exportData := new(bytes.Buffer)
if err := gcexportdata.Write(exportData, nil, typesPkg); err != nil {
return nil, fmt.Errorf("failed to write export data: %v", err)
}
encodedFileSet := bytes.NewBuffer(nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line uses a very outdated style of initializing a new bytes.Buffer, no one writes it like that in anymore. bytes.NewBuffer documentation hints at this:

In most cases, new(Buffer) (or just declaring a Buffer variable) is sufficient to initialize a Buffer.

It's not surprising because this line was written 4 years ago.

I know it's not your code, but it's the line directly following your code, and I think it's not nice how just 4 lines above it is a different style is used to do the same thing:

exportData := new(bytes.Buffer)
// ... 3 lines
encodedFileSet := bytes.NewBuffer(nil)

Let's make this better by updating it so they both use the same style.

I would slightly prefer the var buf bytes.Buffer (and using & operator) style over buf := new(bytes.Buffer):

var exportData bytes.Buffer
if err := gcexportdata.Write(&exportData, nil, typesPkg); err != nil {
	return nil, fmt.Errorf("failed to write export data: %v", err)
}
var encodedFileSet bytes.Buffer
if err := fileSet.Write(json.NewEncoder(&encodedFileSet).Encode); err != nil {

But if you feel strongly about using new(bytes.Buffer) for both variables (and not using & operator), I'm okay with that too.

Also, I know this file has yet another instance of bytes.NewBuffer(nil), but it's fine to leave that alone, it's too far from the affected code and hence out of scope of this PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if err := fileSet.Write(json.NewEncoder(encodedFileSet).Encode); err != nil {
return nil, err
Expand Down Expand Up @@ -541,7 +544,7 @@ func Compile(importPath string, files []*ast.File, fileSet *token.FileSet, impor
ImportPath: importPath,
Name: typesPkg.Name(),
Imports: importedPaths,
ExportData: exportData,
ExportData: exportData.Bytes(),
Declarations: allDecls,
FileSet: encodedFileSet.Bytes(),
Minified: minify,
Expand Down
97 changes: 97 additions & 0 deletions compiler/vendor/golang.org/x/tools/go/gcexportdata/gcexportdata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions compiler/vendor/golang.org/x/tools/go/gcexportdata/importer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions compiler/vendor/golang.org/x/tools/go/gcexportdata/main.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.