jnigen

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2026 License: CC0-1.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateVtableHelpers

func GenerateVtableHelpers(funcs []MergedCapiFunc) string

GenerateVtableHelpers produces C code for cgo_vtable_dispatch.h. Each function gets a static inline C helper that dereferences the vtable.

func Render

func Render(merged *MergedSpec, templatesDir, outputDir string) error

Render executes templates with MergedSpec data and writes output files.

Types

type CapiArgInfo

type CapiArgInfo struct {
	Name       string // param name (for explicit params)
	GoType     string // Go type
	CType      string // original C type from overlay (e.g., "jboolean*", "const char*")
	IsVariadic bool   // whether this is a variadic param
	IsImplicit bool   // true if this is an implicit param
	Implicit   string // Go expression for implicit params (e.g., "len(buf)")
}

CapiArgInfo holds information needed to generate a single capi call argument.

type CapiParam

type CapiParam struct {
	CType      string
	CName      string
	VtableCast string // if set, cast to this type in the vtable call
}

CapiParam is a parameter in the capi layer.

type CapiRenderData

type CapiRenderData struct {
	EnvFunctions []MergedCapiFunc
	VMFunctions  []MergedCapiFunc
	AllFunctions []MergedCapiFunc
	Types        []MergedRefType
	OpaqueTypes  []MergedOpaqueType
	Constants    []MergedConstant
	Primitives   []Primitive
}

CapiRenderData holds all data needed for capi template rendering.

func BuildCapiData

func BuildCapiData(merged *MergedSpec) *CapiRenderData

BuildCapiData prepares data for capi templates.

type Constant

type Constant struct {
	Name   string `yaml:"name"`
	Value  string `yaml:"value"`
	GoType string `yaml:"go_type"`
}

Constant represents a JNI constant.

type EnvRenderData

type EnvRenderData struct {
	Methods []RenderMethod
}

EnvRenderData holds all data needed for env.go.tmpl rendering.

func BuildEnvData

func BuildEnvData(merged *MergedSpec) *EnvRenderData

BuildEnvData prepares data for the env.go.tmpl template.

type ErrorCode

type ErrorCode struct {
	GoName      string
	Value       string
	Description string
}

ErrorCode holds data for a single JNI error constant.

type ErrorRenderData

type ErrorRenderData struct {
	ErrorCodes []ErrorCode
}

ErrorRenderData holds data for errors.go.tmpl rendering.

func BuildErrorData

func BuildErrorData(merged *MergedSpec) *ErrorRenderData

BuildErrorData prepares data for the errors.go.tmpl template.

type FamilyOverlay

type FamilyOverlay struct {
	GoPattern      string         `yaml:"go_pattern"`
	CheckException bool           `yaml:"check_exception"`
	Params         []ParamOverlay `yaml:"params"`
	// Returns can be a map (keyed by "primitive", "Object", "Void") or a string.
	// We use a custom unmarshaler.
	Returns interface{} `yaml:"returns,omitempty"`
}

FamilyOverlay defines overlay for a typed function family.

func (*FamilyOverlay) FamilyReturnMap

func (fo *FamilyOverlay) FamilyReturnMap() map[string]string

FamilyReturnMap returns the returns field as a map if it is one, or nil.

type FuncOverlay

type FuncOverlay struct {
	GoName         string         `yaml:"go_name"`
	CheckException bool           `yaml:"check_exception"`
	Params         []ParamOverlay `yaml:"params,omitempty"`
	Returns        *ReturnOverlay `yaml:"returns,omitempty"`
	Skip           bool           `yaml:"skip,omitempty"`
}

FuncOverlay holds per-function overlay data.

type Function

type Function struct {
	Name      string   `yaml:"name"`
	Params    []string `yaml:"params,omitempty"`
	Returns   string   `yaml:"returns,omitempty"`
	Exception bool     `yaml:"exception,omitempty"`
}

Function represents an individual JNI function.

type FunctionFamily

type FunctionFamily struct {
	Pattern   string   `yaml:"pattern"`
	Vtable    string   `yaml:"vtable"`
	Params    []string `yaml:"params"`
	Returns   string   `yaml:"returns"`
	Expand    []string `yaml:"expand"`
	Exception bool     `yaml:"exception"`
}

FunctionFamily represents a typed function family that expands across types.

type GoParam

type GoParam struct {
	Name       string
	GoType     string
	CType      string // original C type from overlay (e.g., "jboolean*", "const char*")
	IsVariadic bool
}

GoParam is a parameter in the idiomatic Go API.

type GoReturn

type GoReturn struct {
	GoType    string
	IsError   bool
	Transform string
}

GoReturn is a return value in the idiomatic Go API.

type MergedCapiFunc

type MergedCapiFunc struct {
	CName      string
	HelperName string
	Vtable     string
	Params     []CapiParam
	Returns    string
	IsVoid     bool
}

MergedCapiFunc represents a function in the capi layer.

type MergedConstant

type MergedConstant struct {
	CName  string
	GoName string
	Value  string
	GoType string
}

MergedConstant is a constant with Go name and type.

type MergedMethod

type MergedMethod struct {
	GoName         string
	Receiver       string
	Params         []GoParam     // Only non-implicit params (visible in Go signature)
	AllParams      []CapiArgInfo // All params in C order (for capi call generation)
	Returns        []GoReturn
	CapiCall       string
	CheckException bool
	Transforms     []string
	Defers         []string
}

MergedMethod represents a method in the idiomatic layer.

type MergedOpaqueType

type MergedOpaqueType struct {
	CType    string
	GoType   string
	CapiType string // CamelCase of CType, for use in capi layer
}

MergedOpaqueType is an opaque type with Go name.

type MergedRefType

type MergedRefType struct {
	CType    string
	GoType   string
	Parent   string
	IsArray  bool
	ElemType string
}

MergedRefType is a reference type with both C and Go names.

type MergedSpec

type MergedSpec struct {
	Primitives     []Primitive
	ReferenceTypes []MergedRefType
	OpaqueTypes    []MergedOpaqueType
	Constants      []MergedConstant
	CapiFunctions  []MergedCapiFunc
	EnvMethods     []MergedMethod
	VMMethods      []MergedMethod
	TypeMethods    map[string][]MergedMethod
}

MergedSpec is the fully resolved specification ready for template rendering.

func Merge

func Merge(spec *Spec, overlay *Overlay) (*MergedSpec, error)

Merge combines a Spec and Overlay into a MergedSpec.

type OpaqueType

type OpaqueType struct {
	CType  string `yaml:"c_type"`
	GoType string `yaml:"go_type,omitempty"`
}

OpaqueType represents an opaque JNI type (method/field IDs).

type Overlay

type Overlay struct {
	TypeRenames     map[string]string         `yaml:"type_renames"`
	Receivers       map[string]string         `yaml:"receivers"`
	Functions       map[string]FuncOverlay    `yaml:"functions"`
	FamilyOverlays  map[string]FamilyOverlay  `yaml:"family_overlays"`
	ParamTransforms map[string]ParamTransform `yaml:"param_transforms"`
}

Overlay is the top-level structure of spec/overlays/jni.yaml.

func LoadOverlay

func LoadOverlay(path string) (*Overlay, error)

LoadOverlay reads and parses an overlay YAML file.

type ParamOverlay

type ParamOverlay struct {
	Name      string `yaml:"name"`
	CType     string `yaml:"c_type"`
	GoType    string `yaml:"go_type"`
	Transform string `yaml:"transform,omitempty"`
	Implicit  string `yaml:"implicit,omitempty"`
}

ParamOverlay defines a Go-friendly parameter override.

type ParamTransform

type ParamTransform struct {
	Description string `yaml:"description"`
	GoType      string `yaml:"go_type"`
	CType       string `yaml:"c_type"`
	ToC         string `yaml:"to_c,omitempty"`
	CArg        string `yaml:"c_arg,omitempty"`
	FromC       string `yaml:"from_c,omitempty"`
}

ParamTransform defines how to convert between Go and C types.

type Primitive

type Primitive struct {
	CType  string `yaml:"c_type"`
	GoType string `yaml:"go_type"`
	Suffix string `yaml:"suffix"`
}

Primitive represents a JNI primitive type.

type ReferenceType

type ReferenceType struct {
	CType  string  `yaml:"c_type"`
	Parent *string `yaml:"parent"`
}

ReferenceType represents a JNI reference type in the hierarchy.

type RenderMethod

type RenderMethod struct {
	GoName            string
	GoParamList       string
	GoReturnList      string
	Transforms        []string
	HasReturn         bool
	CapiName          string
	CapiArgs          string
	PostTransforms    []string
	CheckException    bool
	ZeroReturn        string
	ReturnConversion  string
	ReturnsError      bool
	JNIErrorTransform bool // true if the only return is error with jni_error transform
	Doc               string
}

RenderMethod is a pre-computed method ready for template rendering.

type ReturnOverlay

type ReturnOverlay struct {
	GoType    string `yaml:"go_type"`
	Transform string `yaml:"transform,omitempty"`
}

ReturnOverlay defines a Go-friendly return type override.

type Spec

type Spec struct {
	Primitives       []Primitive      `yaml:"primitives"`
	ReferenceTypes   []ReferenceType  `yaml:"reference_types"`
	OpaqueTypes      []OpaqueType     `yaml:"opaque_types"`
	FunctionFamilies []FunctionFamily `yaml:"function_families"`
	EnvFunctions     []Function       `yaml:"env_functions"`
	VMFunctions      []Function       `yaml:"vm_functions"`
	Constants        []Constant       `yaml:"constants"`
}

Spec is the top-level structure of spec/jni.yaml.

func LoadSpec

func LoadSpec(path string) (*Spec, error)

LoadSpec reads and parses a spec YAML file.

type TypeFileRenderData

type TypeFileRenderData struct {
	Types []*TypeRenderData
}

TypeFileRenderData holds all types for a single generated file.

type TypeParent

type TypeParent struct {
	GoName string
}

TypeParent holds parent type information for embedding.

type TypeRenderData

type TypeRenderData struct {
	GoName   string
	CType    string
	CapiType string
	Parent   *TypeParent
	IsArray  bool
	ElemType string
}

TypeRenderData holds data for rendering a single reference type file.

func BuildTypeData

func BuildTypeData(rt MergedRefType) *TypeRenderData

BuildTypeData prepares data for a single reference type.

type TypesRenderData

type TypesRenderData struct {
	VersionConstants []VersionConstant
}

TypesRenderData holds data for types.go.tmpl rendering.

func BuildTypesData

func BuildTypesData(merged *MergedSpec) *TypesRenderData

BuildTypesData prepares data for the types.go.tmpl template.

type VMRenderData

type VMRenderData struct {
	Methods []RenderMethod
}

VMRenderData holds all data needed for vm.go.tmpl rendering.

func BuildVMData

func BuildVMData(merged *MergedSpec) *VMRenderData

BuildVMData prepares data for the vm.go.tmpl template.

type ValuePrimitive

type ValuePrimitive struct {
	GoName  string // e.g., "Int"
	GoType  string // e.g., "int32"
	CGoType string // e.g., "capi.Jint" — the capi alias for the C type
}

ValuePrimitive holds data for a single typed Value constructor.

type ValueRenderData

type ValueRenderData struct {
	Primitives []ValuePrimitive
}

ValueRenderData holds data for jvalue.go.tmpl rendering.

func BuildValueData

func BuildValueData(merged *MergedSpec) *ValueRenderData

BuildValueData prepares data for the jvalue.go.tmpl template.

type VersionConstant

type VersionConstant struct {
	GoName string
}

VersionConstant holds a JNI version constant for re-export.

Jump to

Keyboard shortcuts

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