Documentation
¶
Overview ¶
Package protoc provides a Go wrapper for running protoc via WASI/wazero.
Index ¶
Constants ¶
const ( // ExportProtocInit initializes the protoc reactor. // Creates CLI instance and registers all built-in generators. // Signature: protoc_init() -> i32 // Returns: 0 on success, non-zero on error ExportProtocInit = "protoc_init" // ExportProtocRun runs protoc with the given arguments. // protoc_init() must be called first. // Signature: protoc_run(argc: i32, argv: i32) -> i32 // Returns: protoc exit code (0 on success) ExportProtocRun = "protoc_run" // ExportProtocDestroy destroys the protoc reactor and frees resources. // Signature: protoc_destroy() -> void ExportProtocDestroy = "protoc_destroy" )
Protoc reactor exports
const ( // ExportMalloc allocates memory in WASM linear memory. // Signature: malloc(size: i32) -> i32 (pointer) ExportMalloc = "malloc" // ExportFree frees memory in WASM linear memory. // Signature: free(ptr: i32) -> void ExportFree = "free" // ExportRealloc reallocates memory in WASM linear memory. // Signature: realloc(ptr: i32, size: i32) -> i32 (pointer) ExportRealloc = "realloc" // ExportCalloc allocates zeroed memory in WASM linear memory. // Signature: calloc(nmemb: i32, size: i32) -> i32 (pointer) ExportCalloc = "calloc" )
Memory management exports
const ( // ImportModuleProtoc is the import module name for protoc host functions. ImportModuleProtoc = "protoc" // ImportPluginCommunicate is the host function for plugin subprocess IPC. // This allows protoc to spawn plugin processes on the host. ImportPluginCommunicate = "plugin_communicate" )
Host import module name for plugin subprocess communication
const ProtocWASMFilename = "protoc.wasm"
ProtocWASMFilename is the filename for ProtocWASM.
Variables ¶
var ProtocWASM []byte
ProtocWASM contains the binary contents of the protoc WASI reactor build.
This is a reactor-model WASM that exports the protoc compiler API for reentrant execution in host environments. The reactor model allows multiple compilations per instance without reloading the module.
Functions ¶
func CompileProtoc ¶
CompileProtoc compiles the embedded protoc WASM module. The compiled module can be reused across multiple Protoc instances.
Types ¶
type Config ¶
type Config struct {
// Stdin is the standard input for protoc. Default: empty.
Stdin io.Reader
// Stdout is the standard output for protoc. Default: discard.
Stdout io.Writer
// Stderr is the standard error for protoc. Default: discard.
Stderr io.Writer
// FS is the filesystem for reading .proto files and writing output.
// Default: no filesystem access.
FS fs.FS
// FSConfig allows configuring the wazero filesystem.
// If set, FS is ignored.
FSConfig wazero.FSConfig
// PluginHandler handles spawning plugin processes.
// Default: DefaultPluginHandler (uses os/exec).
PluginHandler PluginHandler
}
Config holds configuration for creating a new Protoc instance.
type DefaultPluginHandler ¶
type DefaultPluginHandler struct{}
DefaultPluginHandler spawns plugin processes using os/exec.
func (*DefaultPluginHandler) Communicate ¶
func (h *DefaultPluginHandler) Communicate(ctx context.Context, program string, searchPath bool, input []byte) ([]byte, error)
Communicate spawns a plugin and communicates via stdin/stdout.
type PluginHandler ¶
type PluginHandler interface {
// Communicate spawns a plugin process and handles IPC.
// program: plugin program name (e.g., "protoc-gen-go")
// searchPath: if true, search PATH for the program
// input: serialized CodeGeneratorRequest
// Returns: serialized CodeGeneratorResponse, or error
Communicate(ctx context.Context, program string, searchPath bool, input []byte) ([]byte, error)
}
PluginHandler handles spawning and communicating with protoc plugins. The default implementation uses os/exec to spawn native processes.
type Protoc ¶
type Protoc struct {
// contains filtered or unexported fields
}
Protoc wraps a protoc WASI reactor module providing a high-level API for Protocol Buffer compilation.
func NewProtoc ¶
NewProtoc creates a new Protoc instance using the embedded WASM reactor. Call Close() when done to release resources.
func NewProtocWithModule ¶
func NewProtocWithModule(ctx context.Context, r wazero.Runtime, compiled wazero.CompiledModule, cfg *Config) (*Protoc, error)
NewProtocWithModule creates a new Protoc instance using a pre-compiled module.