Skip to content

Commit 074e48b

Browse files
moved linkname to its own package
1 parent 5740abf commit 074e48b

File tree

7 files changed

+37
-34
lines changed

7 files changed

+37
-34
lines changed

compiler/compiler.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"time"
1919

2020
"github.com/gopherjs/gopherjs/compiler/internal/dce"
21+
"github.com/gopherjs/gopherjs/compiler/linkname"
2122
"github.com/gopherjs/gopherjs/compiler/prelude"
2223
"golang.org/x/tools/go/gcexportdata"
2324
)
@@ -59,7 +60,7 @@ type Archive struct {
5960
// Whether or not the package was compiled with minification enabled.
6061
Minified bool
6162
// A list of go:linkname directives encountered in the package.
62-
GoLinknames []GoLinkname
63+
GoLinknames []linkname.GoLinkname
6364
}
6465

6566
func (a Archive) String() string {
@@ -112,7 +113,7 @@ func WriteProgramCode(pkgs []*Archive, w *SourceMapFilter, goVersion string) err
112113
minify := mainPkg.Minified
113114

114115
// Aggregate all go:linkname directives in the program together.
115-
gls := goLinknameSet{}
116+
gls := linkname.GoLinknameSet{}
116117
for _, pkg := range pkgs {
117118
gls.Add(pkg.GoLinknames)
118119
}
@@ -164,7 +165,7 @@ func WriteProgramCode(pkgs []*Archive, w *SourceMapFilter, goVersion string) err
164165
return nil
165166
}
166167

167-
func WritePkgCode(pkg *Archive, dceSelection map[*Decl]struct{}, gls goLinknameSet, minify bool, w *SourceMapFilter) error {
168+
func WritePkgCode(pkg *Archive, dceSelection map[*Decl]struct{}, gls linkname.GoLinknameSet, minify bool, w *SourceMapFilter) error {
168169
if w.MappingCallback != nil && pkg.FileSet != nil {
169170
w.fileSet = pkg.FileSet
170171
}
@@ -264,7 +265,7 @@ type serializableArchive struct {
264265
IncJSCode []byte
265266
FileSet []byte
266267
Minified bool
267-
GoLinknames []GoLinkname
268+
GoLinknames []linkname.GoLinkname
268269
BuildTime time.Time
269270
}
270271

compiler/compiler_test.go

Lines changed: 2 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/linkname"
1516
"github.com/gopherjs/gopherjs/compiler/sources"
1617
"github.com/gopherjs/gopherjs/internal/srctesting"
1718
)
@@ -782,7 +783,7 @@ func renderPackage(t *testing.T, archive *Archive, minify bool) string {
782783

783784
buf := &bytes.Buffer{}
784785

785-
if err := WritePkgCode(archive, selection, goLinknameSet{}, minify, &SourceMapFilter{Writer: buf}); err != nil {
786+
if err := WritePkgCode(archive, selection, linkname.GoLinknameSet{}, minify, &SourceMapFilter{Writer: buf}); err != nil {
786787
t.Fatal(err)
787788
}
788789

compiler/linkname.go renamed to compiler/linkname/linkname.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package compiler
1+
package linkname
22

33
import (
44
"fmt"
@@ -22,7 +22,7 @@ type GoLinkname struct {
2222
Reference symbol.Name
2323
}
2424

25-
// parseGoLinknames processed comments in a source file and extracts //go:linkname
25+
// ParseGoLinknames processed comments in a source file and extracts //go:linkname
2626
// compiler directive from the comments.
2727
//
2828
// The following directive format is supported:
@@ -38,7 +38,7 @@ type GoLinkname struct {
3838
// - The local function referenced by the directive must have no body (in other
3939
// words, it can only "import" an external function implementation into the
4040
// local scope).
41-
func parseGoLinknames(fset *token.FileSet, pkgPath string, file *ast.File) ([]GoLinkname, error) {
41+
func ParseGoLinknames(fset *token.FileSet, pkgPath string, file *ast.File) ([]GoLinkname, error) {
4242
var errs errorList.ErrorList = nil
4343
var directives []GoLinkname
4444

@@ -108,23 +108,28 @@ func parseGoLinknames(fset *token.FileSet, pkgPath string, file *ast.File) ([]Go
108108
for _, cg := range file.Comments {
109109
for _, c := range cg.List {
110110
if err := processComment(c); err != nil {
111-
errs = append(errs, ErrorAt(err, fset, c.Pos()))
111+
errs = append(errs, errorAt(err, fset, c.Pos()))
112112
}
113113
}
114114
}
115115

116116
return directives, errs.ErrOrNil()
117117
}
118118

119-
// goLinknameSet is a utility that enables quick lookup of whether a decl is
119+
// errorAt annotates an error with a position in the source code.
120+
func errorAt(err error, fset *token.FileSet, pos token.Pos) error {
121+
return fmt.Errorf("%s: %w", fset.Position(pos), err)
122+
}
123+
124+
// GoLinknameSet is a utility that enables quick lookup of whether a decl is
120125
// affected by any go:linkname directive in the program.
121-
type goLinknameSet struct {
126+
type GoLinknameSet struct {
122127
byImplementation map[symbol.Name][]GoLinkname
123128
byReference map[symbol.Name]GoLinkname
124129
}
125130

126131
// Add more GoLinkname directives into the set.
127-
func (gls *goLinknameSet) Add(entries []GoLinkname) error {
132+
func (gls *GoLinknameSet) Add(entries []GoLinkname) error {
128133
if gls.byImplementation == nil {
129134
gls.byImplementation = map[symbol.Name][]GoLinkname{}
130135
}
@@ -144,15 +149,15 @@ func (gls *goLinknameSet) Add(entries []GoLinkname) error {
144149

145150
// IsImplementation returns true if there is a directive referencing this symbol
146151
// as an implementation.
147-
func (gls *goLinknameSet) IsImplementation(sym symbol.Name) bool {
152+
func (gls *GoLinknameSet) IsImplementation(sym symbol.Name) bool {
148153
_, found := gls.byImplementation[sym]
149154
return found
150155
}
151156

152157
// FindImplementation returns a symbol name, which provides the implementation
153158
// for the given symbol. The second value indicates whether the implementation
154159
// was found.
155-
func (gls *goLinknameSet) FindImplementation(sym symbol.Name) (symbol.Name, bool) {
160+
func (gls *GoLinknameSet) FindImplementation(sym symbol.Name) (symbol.Name, bool) {
156161
directive, found := gls.byReference[sym]
157162
return directive.Implementation, found
158163
}

compiler/linkname_test.go renamed to compiler/linkname/linkname_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package compiler
1+
package linkname
22

33
import (
44
"go/ast"
@@ -151,7 +151,7 @@ func TestParseGoLinknames(t *testing.T) {
151151
for _, test := range tests {
152152
t.Run(test.desc, func(t *testing.T) {
153153
file, fset := parseSource(t, test.src)
154-
directives, err := parseGoLinknames(fset, "testcase", file)
154+
directives, err := ParseGoLinknames(fset, "testcase", file)
155155

156156
if test.wantError != "" {
157157
if err == nil {

compiler/package.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -211,18 +211,6 @@ func (ic *ImportContext) Import(path string) (*types.Package, error) {
211211
return ic.Packages[a.ImportPath], nil
212212
}
213213

214-
// parseAllGoLinknames extracts all //go:linkname compiler directive from the sources.
215-
func parseAllGoLinknames(s sources.Sources) ([]GoLinkname, error) {
216-
goLinknames := []GoLinkname{}
217-
var errs errorList.ErrorList
218-
for _, file := range s.Files {
219-
found, err := parseGoLinknames(s.FileSet, s.ImportPath, file)
220-
errs = errs.Append(err)
221-
goLinknames = append(goLinknames, found...)
222-
}
223-
return goLinknames, errs.ErrOrNil()
224-
}
225-
226214
// Compile the provided Go sources as a single package.
227215
//
228216
// Import path must be the absolute import path for a package. Provided sources
@@ -256,7 +244,7 @@ func Compile(srcs sources.Sources, importContext *ImportContext, minify bool) (_
256244
importContext.Packages[srcs.ImportPath] = typesPkg
257245

258246
// Extract all go:linkname compiler directives from the package source.
259-
goLinknames, err := parseAllGoLinknames(srcs)
247+
goLinknames, err := srcs.ParseGoLinknames()
260248
if err != nil {
261249
return nil, err
262250
}

compiler/sources/sources.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
"github.com/gopherjs/gopherjs/compiler/jsFile"
11+
"github.com/gopherjs/gopherjs/compiler/linkname"
1112
"github.com/gopherjs/gopherjs/internal/errorList"
1213
"github.com/neelance/astrewrite"
1314
)
@@ -144,3 +145,15 @@ func (s Sources) UnresolvedImports(skip ...string) []string {
144145
sort.Strings(imports)
145146
return imports
146147
}
148+
149+
// ParseGoLinknames extracts all //go:linkname compiler directive from the sources.
150+
func (s Sources) ParseGoLinknames() ([]linkname.GoLinkname, error) {
151+
goLinknames := []linkname.GoLinkname{}
152+
var errs errorList.ErrorList
153+
for _, file := range s.Files {
154+
found, err := linkname.ParseGoLinknames(s.FileSet, s.ImportPath, file)
155+
errs = errs.Append(err)
156+
goLinknames = append(goLinknames, found...)
157+
}
158+
return goLinknames, errs.ErrOrNil()
159+
}

compiler/utils.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -933,11 +933,6 @@ func formatJSStructTagVal(jsTag string) string {
933933
return "." + jsTag
934934
}
935935

936-
// ErrorAt annotates an error with a position in the source code.
937-
func ErrorAt(err error, fset *token.FileSet, pos token.Pos) error {
938-
return fmt.Errorf("%s: %w", fset.Position(pos), err)
939-
}
940-
941936
// FatalError is an error compiler panics with when it encountered a fatal error.
942937
//
943938
// FatalError implements io.Writer, which can be used to record any free-form

0 commit comments

Comments
 (0)