nativegen

package
v0.1.18 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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;).

View Source
const Version = "2"

Version changes when the generated code changes for the same interface, so that `golet build` regenerates what an older generator wrote.

View Source
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

View Source
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

func AddLang(dir, lang string) error

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

func GenerateGo(m *Module, langs []string, minMacOS string) ([]byte, error)

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

func GenerateRust(m *Module) ([]byte, error)

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

func GenerateSwift(m *Module) []byte

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

func GenerateZig(m *Module) ([]byte, error)

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

func GeneratedFile(m *Module, lang string) string

GeneratedFile is the path of the generated file of a module in lang.

func GeneratedGo

func GeneratedGo(pkg string) string

GeneratedGo is the name of the generated Go file of a package.

func HasLang

func HasLang(dir, lang string) bool

HasLang reports whether the module in dir is implemented in lang.

func IsModule

func IsModule(dir string) bool

IsModule reports whether dir holds a native module: a Go package whose generated file nativegen wrote.

func MinMacOS

func MinMacOS(dir string) string

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

func ModuleLangs(dir string) []string

ModuleLangs returns the languages the module in dir is implemented in.

func RustDir

func RustDir(m *Module) string

RustDir is the directory of a module's Rust sources.

func Scaffold

func Scaffold(dir, pkg string, langs []string) error

Scaffold writes a new module named pkg in dir, implemented in the languages given.

func SourceLine

func SourceLine(hash string) string

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

func Stale(dir string) ([]string, error)

Stale returns the generated files of the module in dir that are older than its interface, or missing.

func SwiftDir

func SwiftDir(m *Module) string

SwiftDir is the directory of a module's Swift sources.

func SwiftFile

func SwiftFile(m *Module) string

SwiftFile is the name of the generated Swift file of a module.

func UpperCamel

func UpperCamel(s string) string

UpperCamel is the name of a package in upper camel case, the name of its Swift module: language → Language, media_player → MediaPlayer.

Types

type Diag

type Diag struct {
	Pos token.Position
	Msg string
}

Diag is a problem with the Go side of a module, at a line of it.

func (Diag) String

func (d Diag) String() string

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 ErrorVar

type ErrorVar struct {
	Name string // ErrNoEmbedding
	Code string // no_embedding
	Doc  string
}

ErrorVar is an error of the package, made with native.NewError.

func (*ErrorVar) SwiftName

func (e *ErrorVar) SwiftName() string

SwiftName is the Swift name of an error's case: ErrNoEmbedding → noEmbedding.

type Errors

type Errors []Diag

Errors are the problems Load found, each at its line.

func (Errors) Error

func (e Errors) Error() string

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 Kind

type Kind int

Kind is the kind of a type that crosses between Go and the platform.

const (
	Bool Kind = iota
	Int
	Float
	String
	Bytes
	Time
	Duration
	Slice
	Map
	Pointer
	StructType
	EnumType
	InterfaceType
	Func
	Window
	Rect
	View
	Buffer
	Texture
	Closer
)

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.

func (*Method) Async

func (m *Method) Async() bool

Async reports whether the method is asynchronous on the platform's side.

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.

func Load

func Load(dir string) (*Module, error)

Load reads the module in dir, and the native modules it uses. Its errors are Errors, one per problem, at the line of the declaration.

type Param

type Param struct {
	Name string
	Type *Type
}

Param is a parameter of a method or a callback.

type Result

type Result struct {
	Module  *Module
	Written []string // the files that changed
}

Result is what Generate wrote.

func Generate

func Generate(dir string) (*Result, error)

Generate loads the module in dir and writes its generated files, leaving those that are up to date.

type Struct

type Struct struct {
	Name   string
	Doc    string
	Pos    token.Position
	Fields []*Field
}

Struct is a struct type of the package.

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.

Jump to

Keyboard shortcuts

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