compiler

package
v1.19.0-beta2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 19, 2025 License: BSD-2-Clause Imports: 37 Imported by: 118

Documentation

Overview

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.

Index

Constants

View Source
const GoVersion = 19

GoVersion is the current Go 1.x version that GopherJS is compatible with.

View Source
const Version = "1.19.0-beta2+go1.19.13"

Version is the GopherJS compiler version string.

Variables

This section is empty.

Functions

func CheckGoVersion added in v1.17.2

func CheckGoVersion(goroot string) error

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 GoRelease added in v1.17.2

func GoRelease(goroot string) string

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 PrepareAllSources

func PrepareAllSources(allSources []*sources.Sources, importer sources.Importer, tContext *types.Context) error

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 WriteArchive

func WriteArchive(a *Archive, buildTime time.Time, w io.Writer) error

WriteArchive writes compiled package archive on disk for later reuse.

The passed in buildTime is used to determine if the archive is out-of-date. Typically it should be set to the srcModTime or time.Now() but it is exposed for testing purposes.

func WritePkgCode

func WritePkgCode(pkg *Archive, dceSelection map[*Decl]struct{}, gls linkname.GoLinknameSet, minify bool, w *SourceMapFilter) error

func WriteProgramCode

func WriteProgramCode(pkgs []*Archive, w *SourceMapFilter, goVersion string) error

Types

type Archive

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 []byte
	// 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
}

Archive contains intermediate build outputs of a single package.

This is a logical equivalent of an object file in traditional compilers.

func Compile

func Compile(srcs *sources.Sources, tContext *types.Context, minify bool) (_ *Archive, err error)

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 ImportDependencies

func ImportDependencies(archive *Archive, importPkg func(string) (*Archive, error)) ([]*Archive, error)

func ReadArchive

func ReadArchive(importPath string, r io.Reader, srcModTime time.Time, imports map[string]*types.Package) (*Archive, time.Time, error)

ReadArchive reads serialized compiled archive of the importPath package.

The given srcModTime is used to determine if the archive is out-of-date. If the archive is out-of-date, the returned archive is nil. If there was not an error, the returned time is when the archive was built.

The imports map is used to resolve package dependencies and may modify the map to include the package from the read archive. See gcexportdata.Read.

func (Archive) String added in v1.17.2

func (a Archive) String() string

type Decl

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
}

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.

func (*Decl) Dce

func (d *Decl) Dce() *dce.Info

Dce gets the information for dead-code elimination.

type Dependency

type Dependency struct {
	Pkg    string
	Type   string
	Method string
}

type FatalError added in v1.17.2

type FatalError struct {
	// contains filtered or unexported fields
}

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.

func (FatalError) Error added in v1.17.2

func (b FatalError) Error() string

func (FatalError) Unwrap added in v1.17.2

func (b FatalError) Unwrap() error

func (*FatalError) Write added in v1.17.2

func (b *FatalError) Write(p []byte) (n int, err error)

Write implements io.Writer and can be used to store free-form debugging clues.

type SourceMapFilter

type SourceMapFilter struct {
	Writer          io.Writer
	MappingCallback func(generatedLine, generatedColumn int, originalPos token.Position)
	// contains filtered or unexported fields
}

func (*SourceMapFilter) Write

func (f *SourceMapFilter) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL