Documentation
¶
Overview ¶
Package scriptudf registers a function written in ClassAd Script as a user-defined function, so an ordinary ClassAd expression can call it.
It is the script counterpart of wasmudf, and deliberately the same shape: a module exports functions by name, registration picks one of those exports and puts it in classad's registry, and a caller wiring extensions into a process hands either kind of module to the same adapter.
Two things differ, and both are things a script can do that a wasm guest cannot:
A `def` STATES ITS OWN ARITY, so Register takes no min/max. There is no arity for a caller to supply and get wrong.
PURITY IS CHECKED. A wasm guest is pure because the ABI hands it values and nothing else. A script function can read the subject ad -- inside a script that is the point of it -- so whether a particular one may be registered is decided by the compiler and reported here as vm.ErrReadsAd. A registered function may not read the ad.
Example ¶
Example registers a function written in ClassAd Script and calls it from an ordinary expression.
The module is the same `pctUsed` the Go and WebAssembly examples implement, written as statements instead: the two absence cases are separate `if`s rather than a condition, which is what a statement language buys. After registration the name behaves exactly as a built-in does -- it folds case, it is compiled in place, and the three answers a function can give are values.
package main
import (
"fmt"
"github.com/PelicanPlatform/classad/classad"
"github.com/PelicanPlatform/classad/collections/scriptudf"
"github.com/PelicanPlatform/classad/collections/vm"
)
func main() {
mod, err := vm.ParseScriptModule(`
# What fraction of a resource is in use, as a percentage.
def examplePct(used, total):
# Undefined in, undefined out: an attribute the ad does not carry is not a mistake, and a
# Requirements expression that treated it as one would reject the machine rather than ignore
# the term.
if used is undefined:
return undefined
if total is undefined:
return undefined
# A division with no answer. Report it by RETURNING error.
if total <= 0:
return error
return used * 100.0 / total
`)
if err != nil {
panic(err)
}
fmt.Println("exports:", mod.Functions())
// Compiling comes first and registration second: a name is only ever registered against a
// function that compiled and that the compiler agrees reads nothing but its arguments.
f, err := scriptudf.Register(mod, scriptudf.ClassAdRegistry(), "examplePct")
if err != nil {
panic(err)
}
// The arity is the def's own, so there is no min/max to pass and none to get wrong.
fmt.Println("arity:", f.Arity())
ad, err := classad.Parse(`[ Used = 3; Total = 8; Label = "eight" ]`)
if err != nil {
panic(err)
}
for _, src := range []string{
`examplePct(Used, Total)`, // both numbers
`EXAMPLEPCT(Used, Total)`, // names fold, as every function name does
`examplePct(Used, Absent)`, // an attribute the ad does not carry
`examplePct(Used, Label)`, // a string where a number belongs
} {
expr, perr := classad.ParseExpr(src)
if perr != nil {
panic(perr)
}
fmt.Printf("%-27s %s\n", src, expr.Eval(ad).String())
}
fmt.Println("compilable:", classad.StrictFunction("examplePct", 2))
// A registered function may not read the ad. Inside a script `Optimistic` would be an
// attribute of the subject ad; a registration has no subject, so this is refused rather than
// registered and left to answer UNDEFINED forever.
impure, err := vm.ParseScriptModule("def exampleFits(requested, cap):\n return Optimistic\n")
if err != nil {
panic(err)
}
_, err = scriptudf.Register(impure, scriptudf.ClassAdRegistry(), "exampleFits")
fmt.Println("reaches the ad:", err != nil)
}
Output: exports: [examplePct] arity: 2 examplePct(Used, Total) 37.5 EXAMPLEPCT(Used, Total) 37.5 examplePct(Used, Absent) undefined examplePct(Used, Label) error compilable: true reaches the ad: true
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Register ¶
func Register(mod *vm.ScriptModule, reg Registry, name string) (*vm.ScriptFunc, error)
Register compiles the module's function called name and registers it, so an expression calling name reaches it.
COMPILATION COMES FIRST AND REGISTRATION SECOND, which is the order that matters and the same order wasmudf uses. Every static problem with the function -- a syntax error in its body, recursion, and above all reaching the ad -- is a returned error here, and a name is only ever registered against a function that compiled and passed the purity gate. A registration cannot be withdrawn: compiled programs capture the decision that a name was a UDF, and they are cached and shared, so registering something unproven is not a mistake that can be taken back.
The errors a loader will want to tell apart are the registry's own sentinels, not new ones: classad.ErrBuiltinName for a name that is already a built-in, classad.ErrFunctionExists for a name already registered. vm.ErrReadsAd is the one failure that has no counterpart in a Go or wasm registration, because only a script can express it.
Types ¶
type Registry ¶
type Registry interface {
RegisterFunction(name string, minArgs, maxArgs int, fn func(args []classad.Value) classad.Value) error
}
Registry is the part of classad's user-defined function registry this package needs: a name, an arity range, and a pure function of argument values.
It is the interface wasmudf declares, redeclared rather than imported. Go interfaces are structural, so an adapter written for one satisfies the other, and a caller can hand a script module and a wasm module to the same registry; importing wasmudf's copy would instead make every program that registers a script function link a WebAssembly runtime.
The method takes an UNNAMED func type so that it is satisfied by a method declared over classad.Func too.
func ClassAdRegistry ¶
func ClassAdRegistry() Registry
ClassAdRegistry returns the process-wide registry in package classad, which is what makes a registered function reachable from an expression.
Use it for anything other than a test. The interface exists so a test can pass a registry it can inspect, since classad's registry is process-wide and append-only and a test cannot undo a registration in it.