Added facility for defining top-level commands from invokables
This commit is contained in:
parent
ba6d42acbb
commit
f1cdf6dd93
|
@ -174,6 +174,20 @@ func TestInst_Eval_WithSubEnv(t *testing.T) {
|
||||||
want1: "hello",
|
want1: "hello",
|
||||||
want2: "world",
|
want2: "world",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
descr: "exporting procs 1",
|
||||||
|
eval1: `export say_hello { "hello" } ; hook { say_hello }`,
|
||||||
|
eval2: `hook { say_hello }`,
|
||||||
|
want1: "hello",
|
||||||
|
want2: "hello",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
descr: "exporting procs 2",
|
||||||
|
eval1: `$a = "hello" ; export say_hello { $a = "world"; $a } ; hook { say_hello }`,
|
||||||
|
eval2: `$a = "other" ; hook { say_hello }`,
|
||||||
|
want1: "world",
|
||||||
|
want2: "world",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
@ -193,6 +207,19 @@ func TestInst_Eval_WithSubEnv(t *testing.T) {
|
||||||
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
})
|
})
|
||||||
|
inst.SetBuiltin("export", func(ctx context.Context, args ucl.CallArgs) (any, error) {
|
||||||
|
var (
|
||||||
|
name string
|
||||||
|
hookProc ucl.Invokable
|
||||||
|
)
|
||||||
|
|
||||||
|
if err := args.Bind(&name, &hookProc); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
inst.SetBuiltinInvokable(name, hookProc)
|
||||||
|
|
||||||
|
return nil, nil
|
||||||
|
})
|
||||||
|
|
||||||
res, err := inst.Eval(ctx, strings.NewReader(tt.eval1), ucl.WithSubEnv())
|
res, err := inst.Eval(ctx, strings.NewReader(tt.eval1), ucl.WithSubEnv())
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
|
@ -81,6 +81,10 @@ func (inst *Inst) SetBuiltin(name string, fn BuiltinHandler) {
|
||||||
inst.rootEC.addCmd(name, userBuiltin{fn: fn})
|
inst.rootEC.addCmd(name, userBuiltin{fn: fn})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (inst *Inst) SetBuiltinInvokable(name string, fn Invokable) {
|
||||||
|
inst.rootEC.addCmd(name, fn.inv)
|
||||||
|
}
|
||||||
|
|
||||||
type userBuiltin struct {
|
type userBuiltin struct {
|
||||||
fn func(ctx context.Context, args CallArgs) (any, error)
|
fn func(ctx context.Context, args CallArgs) (any, error)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue