Documentation
¶
Overview ¶
Package goxmod answers two questions about an XGo module:
- What does its module description say? That is gox.mod (with gop.mod as a legacy fallback), enriched with information that only exists in go.mod. See LoadModfile.
- Which classfile extensions are in effect for it, and what are their projects? See ImportClasses and Classes.
A classfile is not decided by the file itself. Whether a file such as main.gsh, foo_test.gox or Sprite1.spx is a classfile depends on the built-in projects, the projects declared by the module's own gox.mod, and the projects declared by the dependency modules that the module marks with //xgo:class (or the legacy //gop:class) on a require line in go.mod. goxmod resolves all of this into a single, read-only lookup table.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // GshProject is the built-in project for the XGo shell (".gsh") classfile. // It is a package-level value shared by every Classes, so callers must // treat it, and every *Project obtained from a Classes, as read-only. GshProject = &Project{ Ext: ".gsh", Class: "App", PkgPaths: []string{"github.com/qiniu/x/gsh", "math"}, } // TestProject is the built-in project for the XGo test ("_test.gox") // classfile, with the "Case" work class. It is a package-level value shared // by every Classes, so callers must treat it, and every *Project obtained // from a Classes, as read-only. TestProject = &Project{ Ext: "_test.gox", Class: "App", PkgPaths: []string{"github.com/goplus/xgo/test", "testing"}, Works: []*modfile.Class{{Ext: "_test.gox", Class: "Case"}}, } )
var ErrNotClassFileMod = errors.New("not a classfile module")
ErrNotClassFileMod reports that a module marked as a class module declares no projects, so it contributes no classfile. See ImportClasses.
Functions ¶
func LoadModfile ¶
LoadModfile reads and parses the gox.mod module description at goxmod, enriched with information that only exists in go.mod. It is LoadModfileEx with os.ReadFile; see that function for the full contract.
func LoadModfileEx ¶
func LoadModfileEx(gomod *gomodfile.File, goxmod string, readFile func(string) ([]byte, error)) (*modfile.File, error)
LoadModfileEx reads and parses the gox.mod module description at goxmod, enriched with information that only exists in go.mod. The readFile hook lets editors and tests supply file contents from memory; LoadModfile passes os.ReadFile. gomod is the parsed go.mod of the same module and may be nil.
It performs these steps, in order:
- Read goxmod (a path ending in "gox.mod"). If that fails and the name ends in "gox.mod", read "gop.mod" in the same directory instead.
- If a file was read, parse it with modfile.ParseLax. A parse error is returned to the caller.
- If no file could be read, the result is an empty module description with the default XGo version, and no error is returned. A module without gox.mod is a normal case, not a failure. Any read error leads here, not only "file not found".
- If gomod is not nil, two things from go.mod are folded into the result: every require whose trailing comment starts with "xgo:class" (or the legacy "gop:class") is appended to ClassMods, and a trailing comment on the go directive that starts with "llgo " or "tinygo " (for example "go 1.18 // llgo 0.9") sets Compiler. The first matching comment wins, and a compiler found in go.mod replaces any compiler set by gox.mod.
The result is therefore the union of what gox.mod declares and what go.mod adds, so callers never have to merge the two themselves.
Types ¶
type Class ¶
Class is a work class statement, re-exported from github.com/goplus/mod/modfile so callers do not have to import modfile just to name the type.
type Classes ¶
type Classes struct {
// contains filtered or unexported fields
}
Classes represents a collection of classfiles, indexed by classfile extension. It is a read-only lookup table: it is complete once ImportClasses returns, and all of its methods are safe for concurrent use.
func DefaultClasses ¶
func DefaultClasses() *Classes
DefaultClasses returns the classfiles that are known without any module: the built-in projects TestProject ("_test.gox") and GshProject (".gsh"), in that registration order. The result is a fresh collection, so it is safe to use like any other *Classes.
GshProject and TestProject are package-level values shared by every Classes, so callers must treat them, and every *Project obtained from a Classes, as read-only.
func ImportClasses ¶
func ImportClasses(goxmod *modfile.File, lookupModVer LookupModVer) (p *Classes, err error)
ImportClasses builds a *Classes from the module description returned by LoadModfile. Projects are registered in this order, and when two projects use the same extension the later one replaces the earlier one:
- TestProject (built-in).
- GshProject (built-in).
- Every project in goxmod.Projects (declared by the module itself).
- For each module path in goxmod.ClassMods, in order, the projects of that module.
A project is indexed by its own extension and by the extension of every class in its Works list, so a lookup by any of those extensions reaches the same *Project.
For each class module, lookupModVer(modPath) resolves the path to a version. If it reports ok == false, the module is skipped and no error is raised. A module with a version whose go.mod is not yet in the module cache is fetched first; a module without a version is a local directory and is not fetched. The module's gox.mod (or gop.mod) is then loaded with go.mod == nil, because only the projects matter. A class module that resolves but declares no projects yields ErrNotClassFileMod.
Any error (module cache lookup, fetch, parse, or ErrNotClassFileMod) stops the import and is returned. The *Classes returned together with an error is partial and must not be used.
lookupModVer must not be nil when goxmod.ClassMods is not empty. GshProject and TestProject are package-level values shared by every Classes, so callers must treat them, and every *Project obtained from a Classes, as read-only.
func (*Classes) ClassInfo ¶
ClassInfo reports whether fname is a known classfile.
It takes the classfile extension of fname (see modfile.ClassExt). If the extension is known, ClassInfo returns the project's auto-lambda map, whether fname is the project (main) file of its class family as opposed to a work file, and ok == true. For an unknown file it returns nil, false, false.
See https://github.com/goplus/xgo/issues/2828 to learn about auto lambda.
type LookupModVer ¶
LookupModVer looks up a module by its path, returning the selected version and whether such a module exists. [Workspace.LookupModule] has this shape; it already applies replace directives and the selected versions, so a class module replaced by a local directory is found there, without a version, and is not fetched.
type Project ¶
Project is a project statement, re-exported from github.com/goplus/mod/modfile so callers do not have to import modfile just to name the type.