Documentation
¶
Overview ¶
Package nativegen reads the Go side of a native module, an interface that a platform's own language implements, and writes what each side needs from it: the Go constructors and proxies (<package>_native.go), and for Apple's platforms the Swift protocols, types and the @_cdecl functions Go calls (<Module>.golet.swift). See docs/native-modules.md.
Index ¶
- Constants
- Variables
- func AddLang(dir, lang string) error
- func GenerateGo(m *Module, langs []string, minMacOS string) ([]byte, error)
- func GenerateRust(m *Module) ([]byte, error)
- func GenerateSwift(m *Module) []byte
- func GenerateZig(m *Module) ([]byte, error)
- func GeneratedFile(m *Module, lang string) string
- func GeneratedGo(pkg string) string
- func HasLang(dir, lang string) bool
- func IsModule(dir string) bool
- func MinMacOS(dir string) string
- func ModuleLangs(dir string) []string
- func RustDir(m *Module) string
- func Scaffold(dir, pkg string, langs []string) error
- func SourceLine(hash string) string
- func Stale(dir string) ([]string, error)
- func SwiftDir(m *Module) string
- func SwiftFile(m *Module) string
- func UpperCamel(s string) string
- type Diag
- type Enum
- type EnumCase
- type ErrorVar
- type Errors
- type Field
- type Interface
- type Kind
- type Method
- type Module
- type Param
- type Result
- type Struct
- type Type
Constants ¶
const RustFile = "golet.rs"
RustFile is the generated Rust of a module, a module of its crate that the crate's lib.rs declares (mod golet;).
const Version = "2"
Version changes when the generated code changes for the same interface, so that `golet build` regenerates what an older generator wrote.
const ZigFile = "golet.zig"
ZigFile is the generated Zig of a module, the root of its library, which imports the implementation from src/<package>.zig.
Variables ¶
var Langs = []string{"swift", "rust", "zig"}
Langs are the languages a module may be implemented in, each in the directory of its name: a Swift package, a Rust crate or a Zig package.
Functions ¶
func AddLang ¶
AddLang adds a language's directory to the module in dir: a package whose every method says it is unsupported, so the module builds at once.
func GenerateGo ¶
GenerateGo writes the Go side: the module's description, a constructor per entry point and the values that call the implementation, which is in the languages langs.
func GenerateRust ¶
GenerateRust writes the Rust side: the traits the implementation implements, the structs, enums and errors of the Go side, and the functions Go calls, which call the factories of the crate's root (make_<entry point>).
func GenerateSwift ¶
GenerateSwift writes the Swift side: the protocols the implementation conforms to, the structs and enums of the Go side, the errors, and the functions Go calls.
func GenerateZig ¶
GenerateZig writes the Zig side: the interfaces the implementation implements, as values of a pointer and a table of its methods, the structs, enums and errors of the Go side, and the functions Go calls, which call the factories of the implementation (make<Entry point>).
func GeneratedFile ¶
GeneratedFile is the path of the generated file of a module in lang.
func GeneratedGo ¶
GeneratedGo is the name of the generated Go file of a package.
func IsModule ¶
IsModule reports whether dir holds a native module: a Go package whose generated file nativegen wrote.
func MinMacOS ¶
MinMacOS reads the oldest macOS the Swift package in dir supports from the platforms of its Package.swift, or "" when it names none.
func ModuleLangs ¶
ModuleLangs returns the languages the module in dir is implemented in.
func SourceLine ¶
SourceLine is the line of a generated file that names the declarations it came from, which `golet build` compares with the package's.
func Stale ¶
Stale returns the generated files of the module in dir that are older than its interface, or missing.
func UpperCamel ¶
UpperCamel is the name of a package in upper camel case, the name of its Swift module: language → Language, media_player → MediaPlayer.
Types ¶
type Enum ¶
type Enum struct {
Name string
Doc string
Pos token.Position
String bool
Basic string // the underlying Go type: string, int, uint8 …
Cases []*EnumCase
}
Enum is a named string or integer type with constants.
type EnumCase ¶
type EnumCase struct {
Name string // the Go name
Value string // its value, as a Go literal: "word", 3
Doc string
}
EnumCase is one constant of an Enum.
type Field ¶
type Field struct {
Name string // the Go name
JSON string // its key in JSON
Doc string
Type *Type
}
Field is an exported field of a struct.
type Interface ¶
type Interface struct {
Name string
Doc string
Pos token.Position
Entry bool // //golet:native: the package gets NewName
MainThread bool // //golet:mainthread: every method runs on the main thread
View bool // embeds native.View
Closer bool // embeds io.Closer or has Close() error
// ByGo is set for an interface that methods only take: Go implements
// it, and the platform calls it.
ByGo bool
Methods []*Method
// contains filtered or unexported fields
}
Interface is an interface of the module.
type Method ¶
type Method struct {
Name string
Doc string
Pos token.Position
Ctx bool // takes a context.Context first
MainThread bool
Params []*Param
// Result is the value it returns, nil for none. For a stream it is
// the element.
Result *Type
Err bool // returns an error, last
Stream int // 1: iter.Seq[Result]; 2: iter.Seq2[Result, error]
Closer bool // returns an io.Closer
// Close is the Close() error of an interface that is closed.
Close bool
}
Method is a method of an interface.
type Module ¶
type Module struct {
Dir string // the package's directory
Package string // the Go package name: language
Name string // the Swift module: Language
// Files are the package's own Go files, without the generated one.
Files []string
// Hash identifies the declarations the generated files came from.
Hash string
Interfaces []*Interface
Structs []*Struct
Enums []*Enum
Errors []*ErrorVar
// Imports are the packages the generated Go imports, besides native.
Imports map[string]bool
// Deps are the other native modules whose types the module uses, by
// the paths it imports them at, and Quals the names it imports them
// under.
Deps map[string]*Module
Quals map[string]string
}
Module is the Go package of a native module.
type Type ¶
type Type struct {
Kind Kind
// Go is the Go spelling: int64, []string, *Token.
Go string
// Bits and Unsigned describe Int and Float.
Bits int
Unsigned bool
Elem *Type // Slice, Map, Pointer
Name string // StructType, EnumType, InterfaceType
Iface *Interface // InterfaceType
Enum *Enum // EnumType
// Pkg is the other native module a StructType, EnumType or
// InterfaceType is of, nil for the module's own.
Pkg *Module
// Func's parameters and results.
Params []*Param
Result *Type
Err bool
}
Type is a type of a parameter, a result or a field.