lang: core, funcs: Port some functions to CallableFunc API

Some modern features of our function engine and language might require
this new API, so port what we can and figure out the rest later.
This commit is contained in:
James Shubin
2025-03-16 23:23:57 -04:00
parent f313380480
commit 642c6b952f
29 changed files with 702 additions and 291 deletions

View File

@@ -92,7 +92,7 @@ type PrintfFunc struct {
init *interfaces.Init
last types.Value // last value received to use for diff
result *string // last calculated output
result types.Value // last calculated output
}
// String returns a simple name for this function. This is needed so this struct
@@ -325,40 +325,57 @@ func (obj *PrintfFunc) Stream(ctx context.Context) error {
}
obj.last = input // store for next
format := input.Struct()[printfArgNameFormat].Str()
values := []types.Value{}
for _, name := range obj.Type.Ord {
if name == printfArgNameFormat { // skip format arg
continue
}
x := input.Struct()[name]
values = append(values, x)
}
result, err := compileFormatToString(format, values)
args, err := interfaces.StructToCallableArgs(input) // []types.Value, error)
if err != nil {
return err // no errwrap needed b/c helper func
return err
}
if obj.result != nil && *obj.result == result {
result, err := obj.Call(ctx, args)
if err != nil {
return err
}
// if the result is still the same, skip sending an update...
if obj.result != nil && result.Cmp(obj.result) == nil {
continue // result didn't change
}
obj.result = &result // store new result
obj.result = result // store new result
case <-ctx.Done():
return nil
}
select {
case obj.init.Output <- &types.StrValue{
V: *obj.result,
}:
case obj.init.Output <- obj.result: // send
// pass
case <-ctx.Done():
return nil
}
}
}
// Call this function with the input args and return the value if it is possible
// to do so at this time.
func (obj *PrintfFunc) Call(ctx context.Context, args []types.Value) (types.Value, error) {
format := args[0].Str()
values := []types.Value{}
for i, x := range args {
if i == 0 { // skip format arg
continue
}
values = append(values, x)
}
result, err := compileFormatToString(format, values)
if err != nil {
return nil, err // no errwrap needed b/c helper func
}
return &types.StrValue{
V: result,
}, nil
}
// valueToString prints our values how we expect for printf.
// FIXME: if this turns out to be useful, add it to the types package.
func valueToString(value types.Value) string {