Documentation
¶
Overview ¶
Package evmbind builds a reusable EVM host schema from Go declarations.
Index ¶
- Constants
- func Catch(name string, err *error)
- func ConcurrentInterfaceArgument[I any](f *Frame, name string, v values.Value) I
- func Field(v values.Value, offset uint64, aggregate bool) values.Value
- func Generate(path, nativeImport, outputPackage string, files fs.FS, opt Options) ([]byte, error)
- func GenerateMethods(outputPackage string, methods []Method) ([]byte, error)
- func ModelValueArgument[T any](f *Frame, name string, v values.Value) T
- func ModelValueResult[T any](f *Frame, name string, x T) values.Value
- func NativeSliceData(v values.Value, element reflect.Type) unsafe.Pointer
- func ReadFunctionMetadata(source fs.FS) (map[string]FunctionMetadata, error)
- func RegisterMethods(pkg *evm.Package, methods []Method, opt MethodOptions) error
- func StoreModelValue[T any](f *Frame, name string, p unsafe.Pointer, x T)
- type DirectFactory
- type Discovery
- type Frame
- func (f *Frame) BoxedFunction(v values.Value) *evm.Callable
- func (f *Frame) Callback(v values.Value) func()
- func (f *Frame) CellField(name string, v values.Value, index int) values.Value
- func (f *Frame) DecodeAny(v values.Value) any
- func (f *Frame) DecodeBorrowedCallback(name string, v values.Value, domain string) any
- func (f *Frame) DecodeError(v values.Value) error
- func (f *Frame) DecodeInterface(v values.Value) any
- func (f *Frame) DecodeNativeCallback(name string, v values.Value) any
- func (f *Frame) DecodeNativeFunction(name string, v values.Value) any
- func (f *Frame) DecodeTypedInterface(name string, v values.Value) any
- func (f *Frame) EncodeAny(x any) values.Value
- func (f *Frame) EncodeBorrowedIterator(name string, x any, domain string) values.Value
- func (f *Frame) EncodeFunction(name string, fn *evm.Callable) values.Value
- func (f *Frame) EncodeInterface(x any) values.Value
- func (f *Frame) EncodeNativeFunction(name string, x any) values.Value
- func (f *Frame) EncodeTypedInterface(name string, x any) values.Value
- func (f *Frame) Function(v values.Value) *evm.Callable
- func (f *Frame) ModelArgument(name string, v values.Value) any
- func (f *Frame) ModelResult(name string, x any) values.Value
- func (f *Frame) NativeModelSliceResult(name string, data unsafe.Pointer, length, capacity int) values.Value
- func (f *Frame) New(cells uint64) unsafe.Pointer
- func (f *Frame) PackCell(name string, fields [2]values.Value) values.Value
- func (f *Frame) SetBorrowedBytesResult(index int, data []byte)
- func (f *Frame) String(x string) values.Value
- func (f *Frame) Time(t time.Time) values.Value
- type FunctionInstance
- type FunctionMetadata
- type Implementation
- type Instance
- type Method
- type MethodFlags
- type MethodOptions
- type Mode
- type MutableSlots
- type Options
- type PackageExposure
- type Schema
- func (s *Schema) Bind(host any) evm.Package
- func (s *Schema) NativeFields(t reflect.Type) *nativebind.Struct
- func (s *Schema) NativePointer(x any) (values.Value, error)
- func (s *Schema) NativeType(t reflect.Type) *values.Type
- func (s *Schema) PackInterface(p *evm.Program, x any) (values.Value, error)
- func (s *Schema) Package() evm.Package
- func (s *Schema) Type(name string) *values.Type
- func (s *Schema) UnpackInterface(p *evm.Program, v values.Value) (any, error)
- func (s *Schema) UsesStatic() bool
- type Static
- type StaticMethod
- type StaticMethods
- type ValueCodec
- func (c *ValueCodec[T]) Pack(x T) values.Value
- func (c *ValueCodec[T]) PackIn(vm *evm.VM, x T) (v values.Value, err error)
- func (c *ValueCodec[T]) Type() *values.Type
- func (c *ValueCodec[T]) Unpack(v values.Value) (T, error)
- func (c *ValueCodec[T]) UnpackIn(vm *evm.VM, v values.Value) (x T, err error)
- type Wrapper
Constants ¶
const ( Automatic = nativebind.Automatic StaticOnly = nativebind.StaticOnly )
Variables ¶
This section is empty.
Functions ¶
func Catch ¶
Catch converts a panic from a generated conversion or Go method into the same source-positioned failure as the reflection binding.
func ConcurrentInterfaceArgument ¶
ConcurrentInterfaceArgument constructs the host adapter for a verified receiver.
func Generate ¶
Generate emits direct Go wrappers for the schema New builds from the same source and options, including Selective, Only and Omit. nativeImport is the Go import path of the bound package; runtime binding options are ignored.
func GenerateMethods ¶
GenerateMethods emits MethodBindings for explicit registrations. Helpers must be exported package functions (not closures, method values, or generic instances). The output package must differ from each helper's package. Run this from a small go:generate program importing the registration list, then supply the result to RegisterMethods. Model and script names are not inferred from Go names.
func ModelValueArgument ¶
ModelValueArgument avoids boxing a generated native value argument in any.
func ModelValueResult ¶
ModelValueResult retains the ordinary Go argument copy as the row storage.
func NativeSliceData ¶
NativeSliceData checks a native view before passing its backing array to Go. Ordinary native slice carriers keep their existing direct-storage contract.
func ReadFunctionMetadata ¶
func ReadFunctionMetadata(source fs.FS) (map[string]FunctionMetadata, error)
ReadFunctionMetadata reads package functions from Go source files.
func RegisterMethods ¶
func RegisterMethods(pkg *evm.Package, methods []Method, opt MethodOptions) error
RegisterMethods adds methods to the package owning their model receivers. The package is changed only after all registrations have been validated. Automatic falls back to reflection; StaticOnly rejects missing/stale adapters.
Types ¶
type DirectFactory ¶
type DirectFactory func(host any) evm.DirectFunc
DirectFactory binds a generated register wrapper once per external receiver.
type Discovery ¶
type Discovery struct {
Options Options
Packages []PackageExposure
// Constants are exposed in the function package under their map keys.
// A bool, int, float64 or string value becomes an untyped constant; a
// value of another scalar type keeps that type.
Constants map[string]any
}
Discovery supplies roots whose reachable types are exposed without source. Existing schemas in Options.Imports retain their declarations and identities.
type Frame ¶
func (*Frame) BoxedFunction ¶
BoxedFunction retains a signature-polymorphic callable and its captures. The receiving host checks its signature. Ordinary any stays ordinary any.
func (*Frame) DecodeBorrowedCallback ¶
DecodeBorrowedCallback adapts a method callback whose []byte arguments are borrowed.
func (*Frame) DecodeError ¶
DecodeError preserves native and guest error identity across the binding.
func (*Frame) DecodeNativeCallback ¶
DecodeNativeCallback binds a typed callback to the originating VM.
func (*Frame) DecodeNativeFunction ¶
DecodeNativeFunction converts a script callable to its declared native type.
func (*Frame) DecodeTypedInterface ¶
DecodeTypedInterface uses the schema that owns the declared interface.
func (*Frame) EncodeBorrowedIterator ¶
EncodeBorrowedIterator adapts the []byte arguments of a returned iterator's yield function.
func (*Frame) EncodeFunction ¶
EncodeFunction checks program ownership and the declared result signature.
func (*Frame) EncodeNativeFunction ¶
EncodeNativeFunction makes a native Go function callable by the script.
func (*Frame) EncodeTypedInterface ¶
func (*Frame) ModelArgument ¶
ModelArgument converts a typed immutable row to the native declaration named by the generated binding. Native-backed pointers retain identity; encoded records require an explicit conversion before this call.
func (*Frame) ModelResult ¶
ModelResult exposes the native declaration as its immutable model row.
func (*Frame) NativeModelSliceResult ¶
func (f *Frame) NativeModelSliceResult(name string, data unsafe.Pointer, length, capacity int) values.Value
NativeModelSliceResult preserves the backing of a generated native result.
func (*Frame) SetBorrowedBytesResult ¶
SetBorrowedBytesResult returns a slice backed by a declared borrow source.
type FunctionInstance ¶
FunctionInstance binds one compiled native specialization. Args are explicit: reflection cannot recover a function's generic type arguments.
type FunctionMetadata ¶
FunctionMetadata retains source names and comments for reflection-based bindings. Consumers decide which comment annotations to interpret or publish.
type Implementation ¶
type Implementation struct {
// contains filtered or unexported fields
}
Implementation binds one concrete Go pointer type to its program-local ID. The conversion functions preserve the pointer without allocating an interface cell.
func Pointer ¶
func Pointer[T any]() Implementation
func PointerForCall ¶
func PointerForCall[T any](bind func(*T, *evm.VM) *T) Implementation
PointerForCall lets a native receiver use the current external call (for example to narrow a budget). The interface value still carries the original pointer. The receiver returned by bind is used only for this method call.
type Instance ¶
Instance explicitly associates a source generic struct with a native instance. Args are required because reflection cannot recover generic type arguments.
type Method ¶
type Method struct {
// contains filtered or unexported fields
}
Method exposes an ordinary Go function as a method of a logical model. Registration does not depend on the record's storage representation.
func RecordMethod ¶
RecordMethod declares an immutable record method. fn takes kvo.Record first, followed by scalar arguments, and returns zero or more scalars. Use GenerateMethods for reflection-free calls; no Go name prefix is stripped.
type MethodFlags ¶
type MethodFlags uint8
MethodFlags declare trusted host effects. Omit them for conservative ownership.
const ( NoRetain MethodFlags = 1 << iota NoInvalidate // BorrowOnly promises that the helper neither retains borrowed arguments nor // invalidates their storage (including through callbacks or transaction calls). BorrowOnly = NoRetain | NoInvalidate )
func (MethodFlags) String ¶
func (f MethodFlags) String() string
type MethodOptions ¶
type MethodOptions struct {
Mode Mode
Static StaticMethods
}
type Mode ¶
type Mode = nativebind.Mode
type MutableSlots ¶
type MutableSlots struct{ Params, Results []int }
MutableSlots selects zero-based explicit parameters and results, excluding method receivers and the host-only *evm.VM argument. Only direct pointers to modeled native structs are supported.
type Options ¶
type Options struct {
// InterfaceAdapters builds native implementations for concurrent interface
// parameters. Each factory receives the checked methods bound to one guest receiver.
InterfaceAdapters map[reflect.Type]func(map[string]evm.Callable) any
// Selective exposes only what the options name, instead of everything the
// source exports: the types in Only (every exported type when Only is nil),
// the methods in Methods, the functions in Functions and GenericFunctions,
// and the typed constants of exposed types. Use it to pick declarations
// out of a large native package.
Selective bool
// Only lists the types a selective schema exposes, together with the local
// types they reference. A non-nil list implies Selective.
Only []string
// Omit leaves exported declarations out of a schema that exposes everything.
// Entries name a type, function or constant, or a method as Type.Method.
Omit []string
// CellStructs replaces value-copy storage while retaining native struct fields.
CellStructs map[string]*values.CellStruct
// Mutable exposes selected native model pointer slots as editable rows.
// Keys are Function or Type.Method, using native declaration names.
Mutable map[string]MutableSlots
// TypeNames renames nongeneric native type declarations in the guest package.
// Fields, Methods and native lookup keep their original declaration names.
// Method signatures and memory layout keep their original native types.
// Model rows cannot be renamed here; configure their model row names.
TypeNames map[string]string
// GenericFunctions supplies compiled native instances of generic package functions.
GenericFunctions map[string][]FunctionInstance
// Instances selects concrete generic struct instantiations. Unlisted
// instantiations are rejected; field and method selections use template names.
Instances []Instance
// Values binds exported package variables to their native Go values. Only
// bound variables are exposed; the source must declare their types.
// Scripts read a variable and use its methods; they cannot assign it.
Values map[string]any
// Functions binds package functions to their actual native Go values.
// A selective schema exposes exactly these functions. Otherwise every
// exported function in the source needs a binding here, in
// GenericFunctions or as a Host method.
Functions map[string]any
// Models supplies the shared model package. Native model structs use its
// immutable row types instead of separate native struct declarations.
Models *evm.Package
// NativePackage identifies the original Go package when its script path differs.
// If omitted, selected native Types supply the package identity.
NativePackage string
// Methods selects the exposed native methods. A nil map exposes every
// exported method, unless the schema is selective. Otherwise, for
// interfaces, a listed type selects direct methods or whole embedded
// interfaces by name; an empty list exposes none, an absent type keeps all.
// For concrete types, unlisted methods remain outside the source API.
Methods map[string][]string
// Fields selects the exposed direct fields of a native struct. An empty
// list exposes none; an absent type keeps the default of exposing all fields.
// Fields and OmitFields cannot both configure the same type.
Fields map[string][]string
// OmitFields hides selected native struct fields from script declarations.
// Native memory and offsets remain unchanged.
OmitFields map[string][]string
// Imports maps native Go import paths to shared script declaration schemas.
// Imported types retain the dependency's nominal identity and native layout.
Imports map[string]*Schema
Mode Mode
Static *Static
// Host is the concrete receiver of package functions. Its methods take a
// leading *evm.VM for access to the session and source position.
Host reflect.Type
// Types associate guest names with native Go declarations.
Types []reflect.Type
// Implementations are pointer types admitted into guest interfaces.
Implementations []Implementation
}
type PackageExposure ¶
type PackageExposure struct {
Pattern string
// Package replaces the guest import path. Empty preserves the native path.
Package string
// Methods includes all exported native methods of discovered types.
Methods bool
// Exclude rejects newly discovered types in this package.
Exclude bool
}
PackageExposure configures discovered native types. Pattern is an exact Go import path or a path ending in /... (including that path itself). The most specific match wins; exact matches beat descendant rules.
type Schema ¶
type Schema struct {
// contains filtered or unexported fields
}
func Discover ¶
Discover binds functions in path and recursively exposes their concrete types. Options.Types adds roots, including concrete values otherwise hidden by any. Returned packages include newly discovered dependency bindings and must all be linked alongside the caller-owned packages from Options.Imports. Go reflection cannot enumerate package globals, functions or constants.
func New ¶
New builds a schema from the Go source of a package. It exposes every exported type with its exported methods, every exported package function and every exported constant. Function bodies, variables, unexported declarations and unexported struct fields are ignored, so the package's own source can be embedded and supplied as is.
Go cannot look a function or type up by name, so the host supplies them: Options.Functions binds package functions, and Options.Types adds native types that no bound signature or struct field reaches. A declaration with no native counterpart, or with a type the binding does not support, fails schema construction; Options.Omit leaves it out. Options.Selective and Options.Only expose a chosen part of a larger package instead.
Cache the returned schema and reuse it across environments; parsing and type checking happen only here.
func (*Schema) NativeFields ¶
func (s *Schema) NativeFields(t reflect.Type) *nativebind.Struct
NativeFields returns the shared live-field binding for an exposed Go struct. Generated mode and reflection use the same operations available to KVO.
func (*Schema) NativePointer ¶
NativePointer borrows a registered data struct pointer for a guest argument. The VM keeps the original object alive. Guest writes and native methods act on that object immediately; concurrent access requires caller synchronization.
func (*Schema) NativeType ¶
NativeType returns the existing value-type identity for an exposed Go type.
func (*Schema) PackInterface ¶
PackInterface creates a program-local R+P argument. P is the original Go pointer, including a nil pointer in a non-nil interface.
func (*Schema) UnpackInterface ¶
UnpackInterface recovers a native pointer without a running call.
func (*Schema) UsesStatic ¶
type Static ¶
type Static struct {
Fields map[string]*nativebind.GeneratedFields
Direct map[string]DirectFactory
Types map[string]reflect.Type
Fingerprint string
Calls map[string]Wrapper
}
type StaticMethod ¶
type StaticMethod struct {
Signature string
Call evm.Native
Direct evm.DirectBinding
}
StaticMethod is a generated adapter for one Go function. Several model methods may share it. Signature protects against stale generated output.
type StaticMethods ¶
type StaticMethods map[string]StaticMethod
type ValueCodec ¶
type ValueCodec[T any] struct { // contains filtered or unexported fields }
ValueCodec converts one Go type declared by a binding schema.
func Codec ¶
func Codec[T any](s *Schema) *ValueCodec[T]
Codec resolves the schema type once. It panics if the conversion is unavailable or ambiguous.
func TryCodec ¶
func TryCodec[T any](s *Schema) (*ValueCodec[T], error)
TryCodec resolves the schema type once and returns any setup error.
func (*ValueCodec[T]) Pack ¶
func (c *ValueCodec[T]) Pack(x T) values.Value
Pack wraps a Go pointer without copying it. It panics if the codec requires a VM.
func (*ValueCodec[T]) PackIn ¶
PackIn converts x using the VM's linked types and charges any metered allocation.
func (*ValueCodec[T]) Type ¶
func (c *ValueCodec[T]) Type() *values.Type
Source Files
¶
- collection_types.go
- discover.go
- frame.go
- function_metadata.go
- generate.go
- generate_direct.go
- methods.go
- methods_generate.go
- model_mutable.go
- model_slices.go
- model_source.go
- model_values.go
- native_function.go
- omitted_fields.go
- reflect.go
- schema.go
- signature.go
- source.go
- source_aliases.go
- source_generic_functions.go
- source_generics.go
- source_imports.go
- type_source.go
- value_codec.go
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
evmbind
command
Command evmbind generates a package of native wrappers for an EVM API.
|
Command evmbind generates a package of native wrappers for an EVM API. |
|
internal
|
|
|
appfixture
Package appfixture is an ordinary Go package whose own source is bound whole.
|
Package appfixture is an ordinary Go package whose own source is bound whole. |
|
methodfixture
Code generated by kvo.
|
Code generated by kvo. |
|
methodfixture/gen
command
|
|