compiler
import "github.com/gopherjs/gopherjs/compiler"
Package compiler implements GopherJS compiler logic.
WARNING: This package's API is treated as internal and currently doesn't provide any API stability guarantee, use it at your own risk. If you need a stable interface, prefer invoking the gopherjs CLI tool as a subprocess.
Constants
GoVersion is the current Go 1.x version that GopherJS is compatible with.
Source: compiler/version_check.go:18
const GoVersion = 21Version is the GopherJS compiler version string.
Source: compiler/version_check.go:15
const Version = "1.21.0+go1.21.13"Functions
func CheckGoVersion(goroot string) error
Source: compiler/version_check.go:23
CheckGoVersion checks the version of the Go distribution at goroot, and reports an error if it's not compatible with this version of the GopherJS compiler.
func Compile(srcs *sources.Sources, tContext *types.Context, minify bool) (_ *Archive, err error)
Source: compiler/package.go:165
Compile the provided Go sources as a single package.
Provided sources must be prepared so that the type information has been determined, and the source files have been sorted by name to ensure reproducible JavaScript output.
func DefaultFilter(w io.Writer) *sourcemapx.Filter
Source: compiler/compiler.go:127
DefaultFilter creates a new sourcemapx.Filter that writes to the given writer. This is not in the internal package so that tools like the playground can use it when calling things like the exported WriteProgramCode function.
func GoRelease(goroot string) string
Source: compiler/version_check.go:63
GoRelease does a best-effort to identify the Go release we are building with. If unable to determine the precise version for the given GOROOT, falls back to the best guess available.
func ImportDependencies(archive *Archive, importPkg func(string) (*Archive, error)) ([]*Archive, error)
func PrepareAllSources(allSources []*sources.Sources, importer sources.Importer, tContext *types.Context) error
Source: compiler/package.go:243
PrepareAllSources prepares all sources for compilation by parsing go linknames, type checking, sorting, simplifying, and performing cross package analysis. The results are stored in the provided sources.
All sources must be given at the same time for cross package analysis to work correctly. For consistency, the sources should be sorted by import path.
func WritePkgCode(pkg *Archive, dceSelection map[*Decl]struct{}, gls linkname.GoLinknameSet, minify bool, w *sourcemapx.Filter) error
func WriteProgramCode(pkgs []*Archive, w *sourcemapx.Filter, goVersion, testBinary string) error
Types
type Archive
Source: compiler/compiler.go:53
Archive contains intermediate build outputs of a single package.
This is a logical equivalent of an object file in traditional compilers.
type Archive struct {
// Package's full import path, e.g. "some/package/name".
ImportPath string
// Package's name as per "package" statement at the top of a source file.
// Usually matches the last component of import path, but may differ in
// certain cases (e.g. main or test packages).
Name string
// A list of full package import paths that the current package imports across
// all source files. See go/types.Package.Imports().
Imports []string
// The package information is used by the compiler to type-check packages
// that import this one. See [gcexportdata.Write].
Package *types.Package
// Compiled package-level symbols.
Declarations []*Decl
// Concatenated contents of all raw .inc.js of the package.
IncJSCode []incjs.File
// The file set containing the source code locations for various symbols
// (e.g. for sourcemap generation). See [token.FileSet.Write].
FileSet *token.FileSet
// Whether or not the package was compiled with minification enabled.
Minified bool
// A list of go:linkname directives encountered in the package.
GoLinknames []linkname.GoLinkname
}Fields
ImportPath stringPackage's full import path, e.g. "some/package/name".
Name stringPackage's name as per "package" statement at the top of a source file. Usually matches the last component of import path, but may differ in certain cases (e.g. main or test packages).
Imports []stringA list of full package import paths that the current package imports across all source files. See go/types.Package.Imports().
Package *types.PackageThe package information is used by the compiler to type-check packages that import this one. See [gcexportdata.Write].
Declarations []*DeclCompiled package-level symbols.
IncJSCode []incjs.FileConcatenated contents of all raw .inc.js of the package.
FileSet *token.FileSetThe file set containing the source code locations for various symbols (e.g. for sourcemap generation). See [token.FileSet.Write].
Minified boolWhether or not the package was compiled with minification enabled.
GoLinknames []linkname.GoLinknameA list of go:linkname directives encountered in the package.
func String() string
type Decl
Decl represents a package-level symbol (e.g. a function, variable or type).
It contains code generated by the compiler for this specific symbol, which is grouped by the execution stage it belongs to in the JavaScript runtime.
When adding new fields to this struct, make sure the field is exported so that it Gob serializes correctly for the archive cache.
type Decl struct {
// The package- or receiver-type-qualified name of function or method obj.
// See go/types.Func.FullName().
FullName string
// A logical equivalent of a symbol name in an object file in the traditional
// Go compiler/linker toolchain. Used by GopherJS to support go:linkname
// directives. Must be set for decls that are supported by go:linkname
// implementation.
LinkingName symbol.Name
// A list of package-level JavaScript variable names this symbol needs to declare.
Vars []string
// A JS expression by which the object represented by this decl may be
// referenced within the package context. Empty if the decl represents no such
// object.
RefExpr string
// NamedRecvType is method named recv declare.
NamedRecvType string
// JavaScript code that declares a local variable for an imported package.
ImportCode []byte
// JavaScript code that declares basic information about a named type symbol.
// It configures basic information about the type and its identity.
TypeDeclCode []byte
// JavaScript code that assigns exposed named types to the package.
ExportTypeCode []byte
// JavaScript code that declares basic information about an anonymous type.
// It configures basic information about the type.
// This is added to the finish setup phase to have access to all packages.
AnonTypeDeclCode []byte
// JavaScript code that declares basic information about a function or
// method symbol. This contains the function's or method's compiled body.
// This is added to the finish setup phase to have access to all packages.
FuncDeclCode []byte
// JavaScript code that assigns exposed functions to the package.
// This is added to the finish setup phase to have access to all packages.
ExportFuncCode []byte
// JavaScript code that initializes reflection metadata about a type's method list.
// This is added to the finish setup phase to have access to all packages.
MethodListCode []byte
// JavaScript code that initializes the rest of reflection metadata about a type
// (e.g. struct fields, array type sizes, element types, etc.).
// This is added to the finish setup phase to have access to all packages.
TypeInitCode []byte
// JavaScript code that needs to be executed during the package init phase to
// set the symbol up (e.g. initialize package-level variable value).
InitCode []byte
// DCEInfo stores the information for dead-code elimination.
DCEInfo dce.Info
// Set to true if a function performs a blocking operation (I/O or
// synchronization). The compiler will have to generate function code such
// that it can be resumed after a blocking operation completes without
// blocking the main thread in the meantime.
Blocking bool
}Fields
FullName stringThe package- or receiver-type-qualified name of function or method obj. See go/types.Func.FullName().
LinkingName symbol.NameA logical equivalent of a symbol name in an object file in the traditional Go compiler/linker toolchain. Used by GopherJS to support go:linkname directives. Must be set for decls that are supported by go:linkname implementation.
Vars []stringA list of package-level JavaScript variable names this symbol needs to declare.
RefExpr stringA JS expression by which the object represented by this decl may be referenced within the package context. Empty if the decl represents no such object.
NamedRecvType stringNamedRecvType is method named recv declare.
ImportCode []byteJavaScript code that declares a local variable for an imported package.
TypeDeclCode []byteJavaScript code that declares basic information about a named type symbol. It configures basic information about the type and its identity.
ExportTypeCode []byteJavaScript code that assigns exposed named types to the package.
AnonTypeDeclCode []byteJavaScript code that declares basic information about an anonymous type. It configures basic information about the type. This is added to the finish setup phase to have access to all packages.
FuncDeclCode []byteJavaScript code that declares basic information about a function or method symbol. This contains the function's or method's compiled body. This is added to the finish setup phase to have access to all packages.
ExportFuncCode []byteJavaScript code that assigns exposed functions to the package. This is added to the finish setup phase to have access to all packages.
MethodListCode []byteJavaScript code that initializes reflection metadata about a type's method list. This is added to the finish setup phase to have access to all packages.
TypeInitCode []byteJavaScript code that initializes the rest of reflection metadata about a type (e.g. struct fields, array type sizes, element types, etc.). This is added to the finish setup phase to have access to all packages.
InitCode []byteJavaScript code that needs to be executed during the package init phase to set the symbol up (e.g. initialize package-level variable value).
DCEInfo dce.InfoDCEInfo stores the information for dead-code elimination.
Blocking boolSet to true if a function performs a blocking operation (I/O or synchronization). The compiler will have to generate function code such that it can be resumed after a blocking operation completes without blocking the main thread in the meantime.
func Dce() *dce.Info
Dce gets the information for dead-code elimination.
type Dependency
Source: compiler/compiler.go:83
type Dependency struct {
Pkg string
Type string
Method string
}Fields
Pkg stringType stringMethod string
type FatalError
FatalError is an error compiler panics with when it encountered a fatal error.
FatalError implements io.Writer, which can be used to record any free-form debugging details for human consumption. This information will be included into String() result along with the rest.
type FatalError struct {
// contains filtered or unexported fields
}func Error) Error() string
func Unwrap() error
func Write(p []byte) (n int, err error)
Write implements io.Writer and can be used to store free-form debugging clues.
