// Code generated by hostgen. DO NOT EDIT. package testpkg import ( "context" "encoding/json" extism "github.com/extism/go-sdk" ) // MathAddResponse is the response type for Math.Add. type MathAddResponse struct { Result int32 `json:"result,omitempty"` Error string `json:"error,omitempty"` } // RegisterMathHostFunctions registers Math service host functions. // The returned host functions should be added to the plugin's configuration. func RegisterMathHostFunctions(service MathService) []extism.HostFunction { return []extism.HostFunction{ newMathAddHostFunction(service), } } func newMathAddHostFunction(service MathService) extism.HostFunction { return extism.NewHostFunctionWithStack( "math_add", func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) { // Read parameters from stack a := extism.DecodeI32(stack[0]) b := extism.DecodeI32(stack[1]) // Call the service method result, err := service.Add(ctx, a, b) if err != nil { mathWriteError(p, stack, err) return } // Write JSON response to plugin memory resp := MathAddResponse{ Result: result, } mathWriteResponse(p, stack, resp) }, []extism.ValueType{extism.ValueTypeI32, extism.ValueTypeI32}, []extism.ValueType{extism.ValueTypePTR}, ) } // mathWriteResponse writes a JSON response to plugin memory. func mathWriteResponse(p *extism.CurrentPlugin, stack []uint64, resp any) { respBytes, err := json.Marshal(resp) if err != nil { mathWriteError(p, stack, err) return } respPtr, err := p.WriteBytes(respBytes) if err != nil { stack[0] = 0 return } stack[0] = respPtr } // mathWriteError writes an error response to plugin memory. func mathWriteError(p *extism.CurrentPlugin, stack []uint64, err error) { errResp := struct { Error string `json:"error"` }{Error: err.Error()} respBytes, _ := json.Marshal(errResp) respPtr, _ := p.WriteBytes(respBytes) stack[0] = respPtr }