evmbind

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: 0BSD Imports: 29 Imported by: ? 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.

README

Source-defined host bindings

evmbind.New(guestPath, source, options) builds a script-facing schema directly from a Go package's own source files. Supply the source as an fs.FS containing the package's .go files—usually through //go:embed:

//go:embed app.go
var source embed.FS

schema, err := evmbind.New("app", source, evmbind.Options{
    Functions: map[string]any{"Pluralize": Pluralize, "Discount": Discount},
    Types:     []reflect.Type{reflect.TypeFor[Store](), reflect.TypeFor[User](), reflect.TypeFor[Cart]()},
})

By default, the schema exposes everything the source exports: every type with its exported fields and methods, every package function, and every constant. Function bodies, unbound variables, unexported declarations, unexported struct fields, and _test.go files are ignored, so you can embed real implementation files as they are. The source supplies declarations, parameter names, and doc comments without ever being executed. See the runnable example.

Because Go cannot look up functions or types by name at run time, the host provides the Go values:

  • Options.Functions maps each exported package function to its Go value. Alternatively, you can implement package functions as methods on an Options.Host type taking a leading *evm.VM, and supply the receiver when calling schema.Bind(host).
  • Options.Types registers native Go types that no bound function, method, or struct field reaches on its own (reachable types are discovered automatically by reflection). Register concrete context types with Pointer[T]().
  • Options.Values maps exported package variables to their Go values. Only explicitly bound variables are exposed, and the source must declare their types. Scripts can read these variables and call their methods, but cannot reassign them.

Schema construction fails—naming the offending declaration—if an exported function has no binding, a type has no native Go type, or a declaration uses an unsupported type. To trim the API, use Options.Omit (by name, or Type.Method for methods—types still referenced by exposed declarations are kept), Options.Methods (methods per type), and Options.Fields or Options.OmitFields (fields per type).

Compile scripts against schema.Package(), and link each module against schema.Bind(receiver) (or schema.Bind(nil) when there is no host receiver). You can acquire concurrent VMs from a linked program with program.AcquireVM(ctx). Remember that a schema owns nominal type identities, while a program owns the numeric dynamic type IDs used during execution.

Store each schema in a host-level sync.OnceValue. Parsing, type checking, enum extraction, and native layout binding then happen once per binary, and every environment shares schema.Package() and its type identities.

Expose part of a package

When you want to expose only a slice of a larger Go package without duplicating its declarations, set Options.Only. The schema then includes:

  • The listed types and any local types they reference.
  • Methods listed in Options.Methods.
  • Functions bound in Options.Functions and Options.GenericFunctions.
  • Typed constants belonging to exposed types.

Options.Selective works the same way, starting from every exported type. Complete constant blocks are type-checked so iota, omitted expressions, and aliases keep their exact native values, while constants of unrelated types stay hidden. Struct pointers keep their native layout, and exported embedded structs and struct pointers preserve Go field promotion, value copying, and pointer aliasing. Options.Omit cannot be combined with selective mode—list only what you want to expose.

Concrete generic structs

Use Options.Instances to bind concrete instantiations of a generic source struct:

Instances: []evmbind.Instance{{
    Type:   "Box",
    Native: reflect.TypeFor[Box[int]](),
    Args:   []reflect.Type{reflect.TypeFor[int]()},
}},

The source template supplies fields, constraints, and selected method signatures; scripts write Box[int] and the binding preserves that instance's native layout. Every reachable instantiation must be registered explicitly because reflection cannot recover generic type arguments (mismatches fail registration).

Field omissions and method selections use the template name and apply to every instance. Native generic aliases and non-struct generic templates are not supported. Generic struct instances currently use reflection, so StaticOnly rejects them.

Consolidated native type aliases

A source package can re-export an imported native type using a Go type alias when both schemas share the same guest package path and keep the same type name. The alias reuses the dependency's type, pointer shape, and native layout without emitting a second guest declaration, and Schema.Type resolves the alias name.

Same-name constant re-exports share the owning declaration as long as their Go types and exact values match (conflicts fail schema construction). Merge the dependency and consumer packages before compiling or linking, and register native methods, field omissions, and interface implementations on the owning schema. Renamed aliases and aliases across different guest packages are not supported by this path.

Native model arguments and results

When selecting native model source with Options.Only, pass the shared model package in Options.Models. The model metadata already knows its native Go types, so you do not need a separate Go-type-to-model registry.

  • Set NativePackage if the original Go package path differs from the script path. It can also be inferred from Types, Implementations, or Static.Types; set it explicitly when those roots include unrelated packages that happen to share a local type name.
  • Model struct fields are stripped before dependency selection. Their script type is the model package's immutable row type, not a duplicate native struct declaration.

Import that schema into callable host schemas through Options.Imports and link the shared model package alongside it:

  • Native pointers keep their identity and nil value; native values use ordinary Go struct assignment.
  • Encoded rows and rows backed by a different native struct require explicit conversion before being passed to a native pointer parameter—bindings never decode, cache, or copy changes back implicitly.
  • Native-backed immutable rows are live views: host code can still mutate the underlying Go object.
Borrowing byte slices from native methods

A native method that returns a single []byte can lend its backing bytes to the guest instead of copying them. Annotate its Go declaration with:

//evm:borrow-bytes domain [nativeMethod]

The method must keep its arguments local and must not invalidate outstanding borrows; the receiver supplies the borrow scope. If the public Go method returns an owned copy, you can pass an optional nativeMethod (with the same signature) that supplies the borrowed bytes instead. Both generated and reflection bindings expose the original guest method name. Guest reads use the lent storage directly, while any write, mutable address, or escape triggers a single writable copy shared across all slices of that result.

Methods can also lend bytes to a guest callback. Name the callback parameter on a method that calls it directly, or annotate the method that returns an iterator:

//evm:borrow-callback-bytes edb.tx visit
func (c *Cursor) Visit(visit func(key, value []byte) bool)

//evm:borrow-yield-bytes edb.tx
func (c *Cursor) RawValues() func(yield func(key, value []byte) bool)

Each []byte argument of that callback borrows its backing storage for the call. Guest reads use those bytes directly. Writes detach the slice and its subslice aliases. Saving a slice past the call also detaches it. Native callbacks and guest functions compiled without tracking that domain receive owned bytes. Unannotated callbacks keep their ordinary Go slice semantics.

Editable model slots

To expose selected native model pointer parameters or results as editable rows in a callable schema, list them in Options.Mutable:

opt := evmbind.Options{
    Imports: imports,
    Mutable: map[string]evmbind.MutableSlots{
        "GetCustomer":    {Results: []int{0}},
        "UpdateCustomer": {Params: []int{0}},
    },
}

Slot indices are zero-based and exclude the method receiver and any host-only *evm.VM (method keys use Type.Method). Pass the same Mutable options to Generate. Unknown names, out-of-range indices, or non-model / non-pointer slots fail schema construction. Each annotation applies only to that call site; ordinary model pointers and any results stay immutable by default.

For a native GetCustomer() *Customer, an annotated result becomes *db.CustomerEdit in scripts:

  • Writing customer.name = "Ada" mutates the underlying Go struct immediately; native callbacks see the change without a flush, and pointer equality and interface boxing preserve identity.
  • Editable encoded database records cannot be passed as native pointers without explicit conversion, and persistence remains the host's responsibility.
  • Child reads keep native pointers (including nil), and whole-child replacement remains unsupported.
  • Native slices keep their backing arrays (including nested scalar slices and slices of modeled pointers), while assigning a VM-cell slice to a native field requires explicit conversion.
  • Native model maps require an explicitly bound native map layout; optional collection wrappers and slices of model struct values are not adapted implicitly.

Under the hood, editable row handles use two VM words to distinguish native pointers from encoded editors. Native scalar writes allocate no editor, and string writes follow the native setter's ownership rules. Programs that expose native model slices use tagged addresses and general slice operations where cell-only fast paths would be unsafe, while ordinary guest allocations need no per-object registry.

Registered concrete generic models keep their shared model identity (only registered instantiations may cross the model boundary), aliases resolve to their original native types, and generated and reflected wrappers follow the same rules. Local model source requires a selective schema; callable sources import that shared schema.

Omit host-only fields

Use Options.OmitFields to hide specific exported fields from scripts, mapped by local type name. Omitted fields are removed before dependency selection, so hiding a context or service field prevents its interface from being pulled into the script API. Unknown types, unknown fields, and duplicates fail schema construction (unexported fields are always omitted automatically). Pass the same omissions to Generate.

Omitting a field changes only the script declaration, never the Go struct layout. Native value copies keep omitted fields intact in an inaccessible shallow snapshot (allocations count against the VM budget) and copy them on assignment. Fresh native storage starts with omitted fields zeroed, whereas assigning a fresh script literal over existing native storage updates only the exposed fields and preserves the destination's omitted fields. Script field operations see only exposed fields, while equality (==) and map hashing include omitted fields via the snapshot.

Share declarations across packages

Options.Imports maps native Go import paths to previously built schemas. The native source keeps its ordinary Go imports, while script signatures and fields reuse the imported schema's guest package and type identity. Link every dependency package alongside the consuming schema so local and imported structs keep native pointer aliasing across transitive dependencies.

A few rules keep shared schemas clean:

  • Each native import path must resolve to a single shared schema instance across the import graph. If multiple consumers need different subsets of a package, combine them into that one schema.
  • Standard declarations are shared across schemas and initialized once. Several native packages may also emit into one guest package without losing Go type identity.
  • Pass the same Imports map to Generate so generated code imports the real native Go packages.
  • Imported layouts stay owned by the dependency schema and are never mutated by consumers. Imported interfaces reuse the owning schema's registered implementations and conversions (including typed nils).
  • Link the owning schema's Bind(nil) (or Bind(host) if it has package functions) to install its methods. Scripts do not need to import that package just to receive an implementation through another package's API.

Generic native functions

Register compiled instantiations of generic package functions with Options.GenericFunctions:

GenericFunctions: map[string][]evmbind.FunctionInstance{
    "Var": {
        {Args: []reflect.Type{reflect.TypeFor[string]()}, Function: forms.Var[string]},
        {Args: []reflect.Type{reflect.TypeFor[int64]()}, Function: forms.Var[int64]},
    },
},

Scripts call the original generic signature with inferred or explicit type arguments (forms.Var(&name) or forms.Var[string](&name)), and native pointer arguments keep their storage. The host must register every supported instantiation—unregistered types fail at compile or link time rather than specializing at run time. Constraints and signatures are validated during schema setup.

Register any generic structs that appear in those signatures (such as Binding[string]) through Options.Instances as well. Type arguments must match registered VM type identities (a script-defined named type does not match its underlying native scalar). Generic function instances currently use reflected calls, so generated bindings and StaticOnly reject them. Generic function types (such as Validator[T]) are separate and not yet supported.

Generate bindings

To generate direct, reflection-free Go wrappers for a whole package into a separate bindings package, run:

go run github.com/andreyvit/evm/evmbind/cmd/evmbind \
    -dir ../api -path demo -native example.org/project/api \
    -package apibindings -o bindings_gen.go

For selective schemas or custom options (Only, Selective, Omit, Imports, and so on), call evmbind.Generate(path, nativeImport, outputPackage, source, options) from a small generator program using the same Options as New.

Supply the generated bindings via Options.Static: &apibindings.Bindings:

  • evmbind.Automatic (default) uses generated wrappers when both the source fingerprint and the full set of wrapper names match, and falls back to reflection when bindings are missing or stale.
  • evmbind.StaticOnly enforces freshness: New panics on a mismatch, and RegisterMethods returns an error.
  • The evmreflect build tag excludes generated files from a build, letting you rename or remove native APIs before regenerating (supply a zero Static value under that tag when a package builds in both modes).
Variadic functions and methods

Native package functions and methods support variadic parameters, including through interface dispatch and bound method values. Calls follow Go semantics: omitted arguments produce an empty tail, and values... keeps the supplied slice's backing array. Generated wrappers use Go's ... call syntax, while reflected wrappers use reflect.Value.CallSlice. Variadic calls use the ordinary call window rather than direct-call opcodes. Variadic native function fields are not yet supported.

Native values

Typed codecs (evmbind.Codec[T])

Resolve a bound Go type once during setup with evmbind.Codec[T](schema):

customerCodec := evmbind.Codec[*Customer](shopSchema)
tagsCodec := evmbind.Codec[map[string][]string](shopSchema)

Codec panics if it cannot resolve a unique conversion. Use evmbind.TryCodec[T](schema) to handle setup errors.

  • codec.Type() returns the bound *values.Type for program.LookupFunction.
  • For ordinary bound native struct pointers, codec.Pack(ptr) and codec.Unpack(v) convert without copying or reflection. They preserve the live Go address and map a typed nil pointer to/from the zero values.Value{}.
  • For slices, maps, structs, and other bound types (or metered conversions in general), call codec.PackIn(vm, x) and codec.UnpackIn(vm, v) so allocations and bounds are checked against the active *evm.VM budget. On a non-pointer codec, Pack panics and Unpack returns an error pointing to PackIn / UnpackIn.
  • Codecs follow the schema's declared types and static-only policy: unsupported or unregistered Go types fail at setup rather than registering at run time. Because values.Value slots carry no runtime type tag, callers must match each argument slot to its parameter type in the checked signature.
Interfaces

Pass a host interface value across the Go boundary with schema.PackInterface(program, ptr) and recover it with schema.UnpackInterface(program, v). The concrete type must be registered and implement a declared interface. In the EVM value, R carries the program's nonzero type index and P holds the original GC-visible Go pointer—a typed nil preserves its type index with P == nil, whereas untyped nil packs as the zero value. (program.PackInterface and program.UnpackInterface wrap and unwrap already-packed EVM values.)

  • Both generated and reflected paths expose only declared interface methods, and generated code dispatches through the native Go interface directly.
  • Context values passed back to native code must come from a registered host implementation; repack them for the destination program when crossing programs.
  • Use PointerForCall when a receiver needs the active *evm.VM (for example, to open or close a budget scope): it may return a shallow receiver copy for that call while leaving the original pointer in the guest interface.
  • Embedded host interfaces can be passed wherever a smaller method set is expected.
  • evmbind binds registered native Go implementations into script interfaces (Go → script); it does not synthesize runtime Go proxies for arbitrary script-defined types. For built-in guest-defined error bridging and the precompiled Go adapter pattern for arbitrary interfaces, see Scripts implementing Go host interfaces.
Data structs, pointers, and collections

The data boundary supports booleans, integers, floats, strings, named scalars, nested data structs with exported fields, and pointers to data structs or scalars:

  • Data structs cross by value. Host pointers keep their Go address so guest fields, aliases, escaped field addresses, and native methods all operate on the same object. Prefer evmbind.Codec[*T](schema) (codec.Pack(ptr)) for entry arguments; schema.NativePointer(ptr) provides an untyped check when the Go type is held as any (the host must synchronize concurrent access).
  • Guest allocations and package variables of registered data structs and scalars use native Go storage too. Other guest addresses backed by VM cells use the call-scoped temporary and copy-back boundary.
  • Typed slices and maps of supported native types—including map[string]any and nested collections—keep their Go backing storage, element mutations, and aliases (slice growth may replace the backing array, as in Go). Numbers retain their Go types.
  • Native method-interface fields, slice elements, and map values preserve the concrete native object and typed nils. Only registered native implementations can be stored in those slots (reading an unregistered concrete type—including an error implementation—fails with an unregistered-type error rather than synthesizing a proxy). Interface map keys and unsupported declarations fail schema creation.
any, model rows, and error
  • any: Works in arguments, results, struct fields, and typed slices. Registered native values keep their Go types and identity; guest-only types reach Go as *evm.GuestValue (carrying their owning program and exact VM value, and rejected by any other program). No JSON conversion happens. Schemas exposing any automatically link their registered concrete and primitive scalar types.
  • Model rows through any: When an immutable model row is linked, a registered native model pointer inside any becomes that row type and keeps its original pointer (including typed nils) on return. Multiple Go types can share one model; if one Go type belongs to several linked models, pass a kvo.Record with its model to disambiguate. Modeled kvo.Record values keep their representation (encoded rows return without decoding), and guest-created nil rows return as missing records with the model attached. Model rows take precedence over a separately exposed native struct type. This applies to pointers and known-model records, not native struct values or unmodeled records.
  • error: Built-in error works in arguments, results, struct fields, and map values. Native and guest errors preserve identity, nil, and Error() through EVM's error bridge in both generated and reflected bindings (see Scripts implementing Go host interfaces for OpenCallbackScope after Run or Release). Native []error storage is not yet supported.

Native package functions

Use Options.Functions to bind source declarations directly to Go function values or closures:

schema, err := evmbind.New("db", edb.TransactionBindingSource, evmbind.Options{
    Only:      []string{"RawRange"},
    Functions: map[string]any{"RawPrefix": edb.RawPrefix},
})

In a selective schema, only bound functions enter the guest package, and the types they reference are pulled in recursively while function bodies are ignored. Binding validates argument and result types against the supplied Go function, calls it directly without a synthetic *evm.VM or host receiver, and preserves native reference sharing on return.

Generate uses the same options and selection, invoking the registered function value directly. Because the fingerprint distinguishes package functions from host methods, generated wrappers for one form can never silently serve the other.

Retained functions and callbacks

Use *evm.FunctionOf[S] to retain a guest callback. Declare the parameter as *evm.ConcurrentFunctionOf[S] when the host may invoke the callback concurrently.

Typed function handles (*evm.FunctionOf[S])
  • *evm.FunctionOf[func(int) int] (type-aliased to evm.Callable) accepts named guest functions, closures, bound methods, and native functions; Go sees *evm.Callable. Use FunctionOf for single-run, per-request, or host-serialized callbacks where the compiler does not need to prove read-only captures. The type argument supplies the guest signature so incompatible functions fail compilation, and both generated and reflected bindings accept and return these callables as script function values without boxing them in any. Returned callables must belong to the current program and match the declared signature (nil round-trips as nil). Retaining a closure keeps its captures and owning *evm.Program alive; invoke it with vm.Run(*fn, slots) on an acquired *evm.VM (or synchronously on the active *evm.VM inside a binding). If a host requires a named, non-capturing guest function, check fn.Name() and !fn.HasCaptures().
    • The signature may include nested function parameters and slices (such as func(func([]string), func([]any)) for a header/row producer). Those values stay in VM storage, so invoke the callable with program-owned native callbacks.
    • Struct fields may also use FunctionOf: reads, writes, address-taking, and struct copies translate through the current program (foreign callables fail before invocation), stores meter the retained callable, and Go keeps an ordinary typed pointer field.
Untyped (host-validated) handles (*evm.FunctionOf[any])

Use *evm.FunctionOf[any] (input-only; scripts see an any parameter) when the host itself validates the signature at registration (for example, a single-request GoX renderer whose data type is declared by the script). It accepts named guest functions, closures, bound methods, and native functions, returning *evm.Callable. Both untyped nil and typed nil functions yield a nil pointer, while non-functions fail at the call boundary. Validate the contract with fn.CheckSignature(want) (and inspect fn.Signature(), fn.Name(), or fn.HasCaptures()) before publishing or invoking.

Concurrent callback parameters (*evm.ConcurrentFunctionOf[S])

When a host registers a callback that may run concurrently across pooled VMs, declare the registration parameter as *evm.ConcurrentFunctionOf[S] instead of *evm.FunctionOf[S]. Like FunctionOf, ConcurrentFunctionOf aliases evm.Callable (Go sees *evm.Callable), and S is either a non-variadic function signature or any for host-checked registrations:

type InitContext interface {
    AddBlock(renderer *evm.ConcurrentFunctionOf[any])
    AddAdminHandler(path, method string, handler *evm.ConcurrentFunctionOf[func(HandlerContext) any], options *AdminHandler)
}

evmbind handles both halves of the contract automatically:

  • Compile-time verification (Package.ConcurrentCallbackParams): evmbind records each *evm.ConcurrentFunctionOf[S] parameter's zero-based explicit index (excluding any receiver) in Package.ConcurrentCallbackParams. During compilation, the EVM compiler scans statically visible calls to those functions and methods, verifying that any capturing closure or bound method uses its captures and receiver read-only (with *concurrency.Cache methods allowed for synchronized shared state). Scripts pass c.Handle or a closure directly with no wrapper call.
  • Native-boundary enforcement (fn.CheckConcurrentUse()): Every generated and reflected evmbind wrapper checks every *evm.ConcurrentFunctionOf[S] argument before calling the host function or method. nil passes through as nil (for optional callbacks); any non-nil value must pass fn.CheckConcurrentUse(), accepting plain non-capturing guest functions and compiler-verified closures/bound methods and rejecting unverified capturing callbacks and native callables with evm.ErrFunction.
// Allowed: plugin passes c.Handle directly; c.prefix is read-only, c.tokens is a concurrency.Cache.
type Client struct { prefix string; tokens *concurrency.Cache[string, string] }
func (c *Client) Handle(h bh.HandlerContext) any {
    e, err := c.tokens.GetOrLoad("k", func() (string, time.Time, error) {
        var exp time.Time
        return c.prefix + h.Method(), exp, nil
    })
    if err != nil { panic(err) }
    return e.Value
}
// ctx.AddAdminHandler("token", "GET", c.Handle, opts) // passes compile + boundary check

// Forbidden:
// - mutating c.prefix or a captured map/slice inside Handle -> compile error
// - returning an alias to a captured map/slice -> compile error
// - passing a dynamic function parameter to AddAdminHandler -> compile error
// - calling indirectly (add := ctx.AddAdminHandler; add(..., c.Handle, opts))
//   -> skips static check, rejected at the evmbind boundary by CheckConcurrentUse()

Keep two boundaries in mind:

  • Indirect registration calls and dynamic callbacks: Passing a dynamic function variable or parameter to a *evm.ConcurrentFunctionOf[S] parameter fails compilation. Calling the registration method indirectly (for example, through a function variable add := ctx.AddAdminHandler; add(...)) is not statically visible to the compiler, so evmbind rejects unverified capturing callbacks at run time at the native boundary.
  • Writes after registration: The compiler checks the builder too. Once a receiver or capture is borrowed, later writes through tracked aliases fail, including after a conditional registration. Host-owned pointers can still have other writers. See Concurrent callbacks and caches for the full verification rules and concurrency.Cache limits.
Concurrent interface parameters

Declare //evm:concurrent impl on a host function or method taking impl Foo. Supply a factory for reflect.TypeFor[Foo]() in Options.InterfaceAdapters. It receives the bound methods of one checked guest receiver and returns a Go implementation of Foo. See the embedding guide.

Native Go function types and callback scopes

Ordinary Go function types can also appear in data fields and method signatures. Native calls and callbacks run synchronously on the active VM's goroutine with its globals, context, and remaining budget, and must be serialized like any other use of that VM (EVM provides no blanket guarantee of guest race safety on shared captures or native pointers). Guest panics unwind native defers and remain recoverable by guest callers.

  • Idle and post-release callbacks (OpenCallbackScope): Native Go function wrappers and script-defined errors remember their originating VM's CallbackScope. While a host binding is actively running, they re-enter that VM directly (no OpenCallbackScope needed). If host code needs to invoke them after vm.Run returns, open scope, err := vm.OpenCallbackScope(limits) while vm is idle and defer scope.Close(). Every callback invoked outside an active host binding—whether vm is idle or already released—acquires a fresh VM lease from the same program and Go context with fresh package globals and a fresh per-call budget of limits (zero quotas are unlimited, matching vm.SetBudget; pass program.DefaultLimits() to reuse the program's defaults). Without an open scope, idle calls panic with ErrCallbackInactive, and vm.Release() expires them (ErrCallbackExpired). Keep ctx valid until scope.Close() and serialize calls sharing one scope. Native Go errors and native-created functions are unaffected.
  • Identity without wrapper stacking: Native callable values remember their original Go function value, and guest-created functions read back from native storage recover their program-owned handle (tracked weakly and cleaned up after the Go wrapper is collected), so round-tripping never accumulates wrapper layers. Reading a native function field copies the function value, so later field writes do not alter an already-loaded reference. Variadic native function types are not supported.

Direct calls and fields

A generated wrapper qualifies for a direct-call instruction when it is:

  • a function with up to two arguments, or
  • a pointer method with up to one explicit argument,

returning zero or one result over words (including named scalars and floats), strings, and true pointers. Host functions and registered record methods share the same signature classifier and scalar conversion generator. The compiler emits the specialized opcode matching the wrapper's signature; reflection and older wrappers stay on the general call ABI, and interface methods use virtual dispatch. For hand-written bindings, Direct alone also handles function values and deferred calls (supply Call alongside it to override the window wrapper).

A generated pointer wrapper passes native addresses directly—only VM-cell arguments need a temporary and copy-back. Returned strings are charged once and keep their Go data pointer visible to the GC. In a hand-written direct binding that returns a KVO row, construct its typed pair with VM.RecordValue. Direct callbacks may return conversion or execution errors; expose fallible application operations through Native.

Generated output also provides typed struct field access:

  • schema.NativeFields(goType) returns the shared nativebind.Struct, and schema.NativeType(goType) returns its nominal values.Type.
  • kvo.Binding.WithNativeSchema and Projection.WithNativeSchema reuse both, so KVO property access and host methods operate on the exact same Go object.
  • KVO's record codec generator shares the same field generator for types that need persistence bindings without script exposure.
  • Generated field access (including escaped scalar addresses) uses typed Go operations, while fallback bindings share nativebind's reflection path. Regenerate whenever bound declarations change.

Explicit record methods

You can expose ordinary Go helpers as methods on a specific KVO model. The Go function name, model, and script method name are completely independent—there is no naming convention or prefix stripping.

For example, in a native model package:

func Customer_PointBalance(r kvo.Record) int64 {
    return r.Get(PointsEarned) - r.Get(PointsRedeemed) - r.Get(PointsExpired)
}

Declare the script-facing mapping in a separate package:

func RecordMethods() []evmbind.Method {
    return []evmbind.Method{
        evmbind.RecordMethod(nm.MCustomerStatsOverall, "PointBalance",
            nm.Customer_PointBalance, evmbind.BorrowOnly),
    }
}

A small go:generate program imports that list, calls evmbind.GenerateMethods("bindings", RecordMethods()), and writes the generated Go file to package bindings (which must be a different package from the helpers themselves). Register the generated MethodBindings before compiling scripts or publishing the package:

pkg := must(evm.ModelPackage("m", nm.Schema))
ensure(evmbind.RegisterMethods(&pkg, RecordMethods(), evmbind.MethodOptions{
    Mode:   evmbind.StaticOnly,
    Static: bindings.MethodBindings,
}))

must and ensure panic on error. Go callers invoke nm.Customer_PointBalance(r) while scripts write stats.PointBalance(). Both run the same helper over the original 16-byte record—whether it is Fast2, Compact2, MessagePack, a native struct, or decoded memory—without decoding or changing its representation.

Rules and limits:

  • Methods attach only to explicitly registered immutable model types (editors have their own method set). One helper can be registered on multiple models, and registration rejects shadowed properties, existing methods, or duplicates without modifying the package.
  • BorrowOnly is a trusted promise that the helper neither retains its arguments nor invalidates their backing storage (directly or transitively), allowing ownership analysis to skip unnecessary copies. Omit it for conservative behavior, or pass NoRetain and NoInvalidate separately. EVM does not infer these properties from Go code.
  • Generated adapters call Go directly with no reflection or record boxing. StaticOnly rejects missing or stale adapters, while Automatic falls back to reflective calls (which allocate). Regenerate whenever a helper's signature changes.
  • Generation requires exported top-level functions taking kvo.Record first, followed by scalar arguments and scalar results (bool, integers other than uintptr, floats, and strings, including named Go types—guest signatures use their underlying scalar types). Variadic functions, collections, record results, closures, and bound Go methods are not supported by the generator. Signatures beyond the direct-call limits use the ordinary call window; bound methods, method expressions, interface calls, and deferred calls all work.
  • String results are copied and charged to the VM allocation budget so they can safely outlive borrowed storage; numeric and boolean calculations can stay allocation-free (the helper's own allocations remain its responsibility).
  • Generated files respect the evmreflect build tag; pass an empty StaticMethods with Automatic under that tag if you want reflection-only development builds.

See record-method benchmarks, the runnable generator in internal/methodfixture/gen, and the hand-written bindings guide for void calls, metadata reuse, and manual Shape / DirectBinding declarations.

Reflection-based discovery

When you do not have embedded source available, evmbind.Discover exposes native functions and every type they reach using reflection. Prefer New whenever source is available, because reflection has no parameter names, doc comments, or constants. Exported fields are discovered recursively, unexported fields stay hidden while keeping their native storage, and recursive references preserve pointer identity.

_, packages, err := evmbind.Discover("app", evmbind.Discovery{
    Options: evmbind.Options{
        Functions: map[string]any{"Foo": Foo}, // Foo returns *Bar
        Imports:   existingSchemas,
    },
    Packages: []evmbind.PackageExposure{
        {Pattern: "example.org/app/...", Methods: true},
        {Pattern: "example.org/app/models/...", Package: "app/models", Methods: true},
        {Pattern: "example.org/app/internal/secrets", Exclude: true},
    },
})

Pass the returned packages to Compile and Link alongside your existing bound packages:

  • Types mapped into the function package share it (app.Foo and app.Bar sit side by side), and each returned path appears once.
  • Add named constants with Discovery.Constants, and extra type roots (such as concrete types passed through any) with Options.Types. The returned schema's NativePointer accepts any discovered struct pointer.
  • By default, discovered types keep their native import paths with exported fields and no concrete methods (interfaces keep their method sets). Package rules can enable all exported methods, remap types into an umbrella package, or exclude a path. Rules match exact paths or /... subtrees (including the parent); longest match wins, exact beats /... at the same path, later rules break ties, and a matching rule replaces broader settings. Name collisions under an umbrella package fail explicitly.
  • Existing schemas in Options.Imports keep their identities and API selections without being broadened (their referenced types must already be bound).
  • Discovery uses reflection adapters and requires no generated wrappers. Because reflection cannot enumerate package globals, functions, or constants, register functions and constants explicitly (and use ExternalValue for host-owned globals). Concrete generic instances still require source bindings, and unsupported native shapes fail schema construction rather than being silently dropped. As with source bindings, pointers and supported collections keep their native storage and mutations.

Documentation

Overview

Package evmbind builds a reusable EVM host schema from Go declarations.

Index

Constants

View Source
const (
	Automatic  = nativebind.Automatic
	StaticOnly = nativebind.StaticOnly
)

Variables

This section is empty.

Functions

func Catch

func Catch(name string, err *error)

Catch converts a panic from a generated conversion or Go method into the same source-positioned failure as the reflection binding.

func ConcurrentInterfaceArgument

func ConcurrentInterfaceArgument[I any](f *Frame, name string, v values.Value) I

ConcurrentInterfaceArgument constructs the host adapter for a verified receiver.

func Field

func Field(v values.Value, offset uint64, aggregate bool) values.Value

func Generate

func Generate(path, nativeImport, outputPackage string, files fs.FS, opt Options) ([]byte, error)

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

func GenerateMethods(outputPackage string, methods []Method) ([]byte, error)

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

func ModelValueArgument[T any](f *Frame, name string, v values.Value) T

ModelValueArgument avoids boxing a generated native value argument in any.

func ModelValueResult

func ModelValueResult[T any](f *Frame, name string, x T) values.Value

ModelValueResult retains the ordinary Go argument copy as the row storage.

func NativeSliceData

func NativeSliceData(v values.Value, element reflect.Type) unsafe.Pointer

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.

func StoreModelValue

func StoreModelValue[T any](f *Frame, name string, p unsafe.Pointer, x T)

StoreModelValue writes a native value snapshot into its aggregate cell.

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

type Frame struct {
	*evm.VM
	Host, Receiver any
	// contains filtered or unexported fields
}

func (*Frame) BoxedFunction

func (f *Frame) BoxedFunction(v values.Value) *evm.Callable

BoxedFunction retains a signature-polymorphic callable and its captures. The receiving host checks its signature. Ordinary any stays ordinary any.

func (*Frame) Callback

func (f *Frame) Callback(v values.Value) func()

Callback binds a guest func() to its originating VM.

func (*Frame) CellField

func (f *Frame) CellField(name string, v values.Value, index int) values.Value

CellField reads a logical field from a compact struct value.

func (*Frame) DecodeAny

func (f *Frame) DecodeAny(v values.Value) any

func (*Frame) DecodeBorrowedCallback

func (f *Frame) DecodeBorrowedCallback(name string, v values.Value, domain string) any

DecodeBorrowedCallback adapts a method callback whose []byte arguments are borrowed.

func (*Frame) DecodeError

func (f *Frame) DecodeError(v values.Value) error

DecodeError preserves native and guest error identity across the binding.

func (*Frame) DecodeInterface

func (f *Frame) DecodeInterface(v values.Value) any

func (*Frame) DecodeNativeCallback

func (f *Frame) DecodeNativeCallback(name string, v values.Value) any

DecodeNativeCallback binds a typed callback to the originating VM.

func (*Frame) DecodeNativeFunction

func (f *Frame) DecodeNativeFunction(name string, v values.Value) any

DecodeNativeFunction converts a script callable to its declared native type.

func (*Frame) DecodeTypedInterface

func (f *Frame) DecodeTypedInterface(name string, v values.Value) any

DecodeTypedInterface uses the schema that owns the declared interface.

func (*Frame) EncodeAny

func (f *Frame) EncodeAny(x any) values.Value

func (*Frame) EncodeBorrowedIterator

func (f *Frame) EncodeBorrowedIterator(name string, x any, domain string) values.Value

EncodeBorrowedIterator adapts the []byte arguments of a returned iterator's yield function.

func (*Frame) EncodeFunction

func (f *Frame) EncodeFunction(name string, fn *evm.Callable) values.Value

EncodeFunction checks program ownership and the declared result signature.

func (*Frame) EncodeInterface

func (f *Frame) EncodeInterface(x any) values.Value

func (*Frame) EncodeNativeFunction

func (f *Frame) EncodeNativeFunction(name string, x any) values.Value

EncodeNativeFunction makes a native Go function callable by the script.

func (*Frame) EncodeTypedInterface

func (f *Frame) EncodeTypedInterface(name string, x any) values.Value

func (*Frame) Function

func (f *Frame) Function(v values.Value) *evm.Callable

Function retains a typed callable, including its captured state.

func (*Frame) ModelArgument

func (f *Frame) ModelArgument(name string, v values.Value) any

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

func (f *Frame) ModelResult(name string, x any) values.Value

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) New

func (f *Frame) New(cells uint64) unsafe.Pointer

func (*Frame) PackCell

func (f *Frame) PackCell(name string, fields [2]values.Value) values.Value

PackCell constructs a compact struct value from its logical fields.

func (*Frame) SetBorrowedBytesResult

func (f *Frame) SetBorrowedBytesResult(index int, data []byte)

SetBorrowedBytesResult returns a slice backed by a declared borrow source.

func (*Frame) String

func (f *Frame) String(x string) values.Value

func (*Frame) Time

func (f *Frame) Time(t time.Time) values.Value

Time preserves an exact native time value and accounts for its owned storage.

type FunctionInstance

type FunctionInstance struct {
	Args     []reflect.Type
	Function any
}

FunctionInstance binds one compiled native specialization. Args are explicit: reflection cannot recover a function's generic type arguments.

type FunctionMetadata

type FunctionMetadata struct {
	ParamNames []string
	Doc        string
}

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

type Instance struct {
	Type   string
	Native reflect.Type
	Args   []reflect.Type
}

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

func RecordMethod(model *kvo.Model, name string, fn any, flags ...MethodFlags) Method

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.

func (Method) Model

func (m Method) Model() *kvo.Model

Model is the logical receiver. Environments exposing only part of a schema can use it to select applicable registrations before calling RegisterMethods.

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

func Discover(path string, config Discovery) (*Schema, []evm.Package, error)

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

func New(path string, files fs.FS, opt Options) (schema *Schema, err error)

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) Bind

func (s *Schema) Bind(host any) evm.Package

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

func (s *Schema) NativePointer(x any) (values.Value, error)

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

func (s *Schema) NativeType(t reflect.Type) *values.Type

NativeType returns the existing value-type identity for an exposed Go type.

func (*Schema) PackInterface

func (s *Schema) PackInterface(p *evm.Program, x any) (values.Value, error)

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) Package

func (s *Schema) Package() evm.Package

func (*Schema) Type

func (s *Schema) Type(name string) *values.Type

func (*Schema) UnpackInterface

func (s *Schema) UnpackInterface(p *evm.Program, v values.Value) (any, error)

UnpackInterface recovers a native pointer without a running call.

func (*Schema) UsesStatic

func (s *Schema) UsesStatic() bool

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

func (c *ValueCodec[T]) PackIn(vm *evm.VM, x T) (v values.Value, err error)

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

func (*ValueCodec[T]) Unpack

func (c *ValueCodec[T]) Unpack(v values.Value) (T, error)

Unpack reads a Go pointer from v. It returns an error if the codec requires a VM or the pointer representation is incompatible.

func (*ValueCodec[T]) UnpackIn

func (c *ValueCodec[T]) UnpackIn(vm *evm.VM, v values.Value) (x T, err error)

UnpackIn converts v back to Go using the VM's linked types and conversion budget.

type Wrapper

type Wrapper func(*Frame)

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.

Jump to

Keyboard shortcuts

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