Documentation
¶
Overview ¶
Package s4wave_function is the contract of PureFunctions: TypeScript modules that turn protobuf input into protobuf output under a memory and instruction budget, and run the same on a Go daemon and in a browser.
Index ¶
- Constants
- Variables
- func Compile(source string) (string, error)
- func CreatePureFunction(ctx context.Context, ws world.WorldState, key string, fn *PureFunction) error
- func SetPureFunction(ctx context.Context, ws world.WorldState, key string, fn *PureFunction) error
- type Budget
- func (m *Budget) CloneMessageVT() protobuf_go_lite.CloneMessage
- func (m *Budget) CloneVT() *Budget
- func (this *Budget) EqualMessageVT(thatMsg any) bool
- func (this *Budget) EqualVT(that *Budget) bool
- func (x *Budget) GetInterruptChecks() uint32
- func (b *Budget) GetInterruptLimit() uint32
- func (b *Budget) GetMemoryLimit() uint32
- func (x *Budget) GetMemoryLimitBytes() uint32
- func (x *Budget) MarshalJSON() ([]byte, error)
- func (x *Budget) MarshalProtoJSON(s *json.MarshalState)
- func (x *Budget) MarshalProtoText() string
- func (m *Budget) MarshalToSizedBufferVT(dAtA []byte) (int, error)
- func (m *Budget) MarshalToVT(dAtA []byte) (int, error)
- func (m *Budget) MarshalVT() (dAtA []byte, err error)
- func (*Budget) ProtoMessage()
- func (x *Budget) Reset()
- func (m *Budget) SizeVT() (n int)
- func (x *Budget) String() string
- func (x *Budget) UnmarshalJSON(b []byte) error
- func (x *Budget) UnmarshalProtoJSON(s *json.UnmarshalState)
- func (m *Budget) UnmarshalVT(dAtA []byte) error
- type PureFunction
- func (m *PureFunction) CloneMessageVT() protobuf_go_lite.CloneMessage
- func (m *PureFunction) CloneVT() *PureFunction
- func (this *PureFunction) EqualMessageVT(thatMsg any) bool
- func (this *PureFunction) EqualVT(that *PureFunction) bool
- func (x *PureFunction) GetBudget() *Budget
- func (x *PureFunction) GetInputType() string
- func (x *PureFunction) GetModule() string
- func (x *PureFunction) GetOutputType() string
- func (x *PureFunction) GetSource() string
- func (x *PureFunction) MarshalJSON() ([]byte, error)
- func (x *PureFunction) MarshalProtoJSON(s *json.MarshalState)
- func (x *PureFunction) MarshalProtoText() string
- func (m *PureFunction) MarshalToSizedBufferVT(dAtA []byte) (int, error)
- func (m *PureFunction) MarshalToVT(dAtA []byte) (int, error)
- func (m *PureFunction) MarshalVT() (dAtA []byte, err error)
- func (*PureFunction) ProtoMessage()
- func (x *PureFunction) Reset()
- func (m *PureFunction) SizeVT() (n int)
- func (x *PureFunction) String() string
- func (*PureFunction) TypeID() string
- func (x *PureFunction) UnmarshalJSON(b []byte) error
- func (x *PureFunction) UnmarshalProtoJSON(s *json.UnmarshalState)
- func (m *PureFunction) UnmarshalVT(dAtA []byte) error
- func (f *PureFunction) Validate() error
Constants ¶
const DefaultInterruptChecks = 1000
DefaultInterruptChecks is the interrupt check limit of a budget that sets none, about ten million operations.
const DefaultMemoryLimitBytes = 32 << 20
DefaultMemoryLimitBytes is the heap limit of a budget that sets none.
const ModuleName = "/function.js"
ModuleName is the name a runner gives the function's module.
const PureFunctionTypeID = "function/pure"
PureFunctionTypeID is the type ID of PureFunction.
Variables ¶
var ErrModuleRequired = errors.New("the function has no compiled module")
ErrModuleRequired is returned when a PureFunction has no compiled module.
var ErrTypeRequired = errors.New("the function needs an input and an output type")
ErrTypeRequired is returned when a PureFunction names no input or output type.
var FunctionTypes = []typeid.Type{ {ID: PureFunctionTypeID, New: func() any { return new(PureFunction) }}, }
FunctionTypes lists the typed messages of function.proto.
Functions ¶
func Compile ¶
Compile compiles the TypeScript source of a PureFunction to the ES module a runner evaluates. A PureFunction imports nothing, so an import fails.
func CreatePureFunction ¶
func CreatePureFunction(ctx context.Context, ws world.WorldState, key string, fn *PureFunction) error
CreatePureFunction creates the World object key as a function/pure holding fn.
func SetPureFunction ¶
func SetPureFunction(ctx context.Context, ws world.WorldState, key string, fn *PureFunction) error
SetPureFunction stores fn at key, creating the function/pure object when it is missing. An existing function is changed in place, so the edges that reach it remain.
Types ¶
type Budget ¶
type Budget struct {
// MemoryLimitBytes caps the JavaScript heap of a run. Zero uses 32 MiB.
MemoryLimitBytes uint32 `protobuf:"varint,1,opt,name=memory_limit_bytes,json=memoryLimitBytes,proto3" json:"memoryLimitBytes,omitempty"`
// InterruptChecks caps the interrupt checks of a run. QuickJS checks once
// every 10,000 bytecode branches and calls, so a run gets about that many
// operations per check, counted the same on a Go daemon and in a browser.
// Zero uses 1,000.
InterruptChecks uint32 `protobuf:"varint,2,opt,name=interrupt_checks,json=interruptChecks,proto3" json:"interruptChecks,omitempty"`
// contains filtered or unexported fields
}
Budget bounds the resources of one PureFunction run. A run past either bound fails.
func (*Budget) CloneMessageVT ¶
func (m *Budget) CloneMessageVT() protobuf_go_lite.CloneMessage
func (*Budget) EqualMessageVT ¶
func (*Budget) GetInterruptChecks ¶
func (*Budget) GetInterruptLimit ¶
GetInterruptLimit returns the interrupt check limit, applying the default.
func (*Budget) GetMemoryLimit ¶
GetMemoryLimit returns the heap limit in bytes, applying the default.
func (*Budget) GetMemoryLimitBytes ¶
func (*Budget) MarshalJSON ¶
MarshalJSON marshals the Budget to JSON.
func (*Budget) MarshalProtoJSON ¶
func (x *Budget) MarshalProtoJSON(s *json.MarshalState)
MarshalProtoJSON marshals the Budget message to JSON.
func (*Budget) MarshalProtoText ¶
func (*Budget) MarshalToSizedBufferVT ¶
func (*Budget) ProtoMessage ¶
func (*Budget) ProtoMessage()
func (*Budget) UnmarshalJSON ¶
UnmarshalJSON unmarshals the Budget from JSON.
func (*Budget) UnmarshalProtoJSON ¶
func (x *Budget) UnmarshalProtoJSON(s *json.UnmarshalState)
UnmarshalProtoJSON unmarshals the Budget message from JSON.
func (*Budget) UnmarshalVT ¶
type PureFunction ¶
type PureFunction struct {
// Source is the TypeScript source as written.
Source string `protobuf:"bytes,1,opt,name=source,proto3" json:"source,omitempty"`
// Module is the ES module compiled from Source, which a runner evaluates.
Module string `protobuf:"bytes,2,opt,name=module,proto3" json:"module,omitempty"`
// InputType is the full name of the input message, such as
// s4wave.prom.QueryResponse. The function reads it in protobuf JSON form.
InputType string `protobuf:"bytes,3,opt,name=input_type,json=inputType,proto3" json:"inputType,omitempty"`
// OutputType is the full name of the output message. The function returns
// it in protobuf JSON form.
OutputType string `protobuf:"bytes,4,opt,name=output_type,json=outputType,proto3" json:"outputType,omitempty"`
// Budget bounds each run.
Budget *Budget `protobuf:"bytes,5,opt,name=budget,proto3" json:"budget,omitempty"`
// contains filtered or unexported fields
}
PureFunction is the root of a function/pure object: a TypeScript module whose default export turns one protobuf message into another. It runs with no SDK, network or clock of its own, so the same input gives the same output wherever it runs.
func ReadPureFunction ¶
func ReadPureFunction(ctx context.Context, ws world.WorldState, key string) (*PureFunction, error)
ReadPureFunction reads the PureFunction key.
func (*PureFunction) CloneMessageVT ¶
func (m *PureFunction) CloneMessageVT() protobuf_go_lite.CloneMessage
func (*PureFunction) CloneVT ¶
func (m *PureFunction) CloneVT() *PureFunction
func (*PureFunction) EqualMessageVT ¶
func (this *PureFunction) EqualMessageVT(thatMsg any) bool
func (*PureFunction) EqualVT ¶
func (this *PureFunction) EqualVT(that *PureFunction) bool
func (*PureFunction) GetBudget ¶
func (x *PureFunction) GetBudget() *Budget
func (*PureFunction) GetInputType ¶
func (x *PureFunction) GetInputType() string
func (*PureFunction) GetModule ¶
func (x *PureFunction) GetModule() string
func (*PureFunction) GetOutputType ¶
func (x *PureFunction) GetOutputType() string
func (*PureFunction) GetSource ¶
func (x *PureFunction) GetSource() string
func (*PureFunction) MarshalJSON ¶
func (x *PureFunction) MarshalJSON() ([]byte, error)
MarshalJSON marshals the PureFunction to JSON.
func (*PureFunction) MarshalProtoJSON ¶
func (x *PureFunction) MarshalProtoJSON(s *json.MarshalState)
MarshalProtoJSON marshals the PureFunction message to JSON.
func (*PureFunction) MarshalProtoText ¶
func (x *PureFunction) MarshalProtoText() string
func (*PureFunction) MarshalToSizedBufferVT ¶
func (m *PureFunction) MarshalToSizedBufferVT(dAtA []byte) (int, error)
func (*PureFunction) MarshalToVT ¶
func (m *PureFunction) MarshalToVT(dAtA []byte) (int, error)
func (*PureFunction) MarshalVT ¶
func (m *PureFunction) MarshalVT() (dAtA []byte, err error)
func (*PureFunction) ProtoMessage ¶
func (*PureFunction) ProtoMessage()
func (*PureFunction) Reset ¶
func (x *PureFunction) Reset()
func (*PureFunction) SizeVT ¶
func (m *PureFunction) SizeVT() (n int)
func (*PureFunction) String ¶
func (x *PureFunction) String() string
func (*PureFunction) TypeID ¶
func (*PureFunction) TypeID() string
TypeID returns the type ID of PureFunction.
func (*PureFunction) UnmarshalJSON ¶
func (x *PureFunction) UnmarshalJSON(b []byte) error
UnmarshalJSON unmarshals the PureFunction from JSON.
func (*PureFunction) UnmarshalProtoJSON ¶
func (x *PureFunction) UnmarshalProtoJSON(s *json.UnmarshalState)
UnmarshalProtoJSON unmarshals the PureFunction message from JSON.
func (*PureFunction) UnmarshalVT ¶
func (m *PureFunction) UnmarshalVT(dAtA []byte) error
func (*PureFunction) Validate ¶
func (f *PureFunction) Validate() error
Validate checks that the function has a module and names its types.