Skip to content

Commit e9b6365

Browse files
committed
extract call-time capability binding helpers
1 parent f7daedf commit e9b6365

3 files changed

Lines changed: 52 additions & 36 deletions

File tree

docs/architecture.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Key files:
2323
- `vibes/execution_assign.go` (assignment targets and member assignment flow)
2424
- `vibes/execution_script.go` (script call surface and call-time orchestration)
2525
- `vibes/execution_script_helpers.go` (compiled script lookup/order/ownership and call-time cloning helpers)
26+
- `vibes/execution_call_capabilities.go` (call-time capability binding and contract registration)
2627
- `vibes/execution_function_args.go` (function argument/default/type/ivar binding helpers)
2728
- `vibes/execution_calls.go` (callable dispatch + function invocation)
2829
- `vibes/execution_call_expr.go` (call expression target/args/kwargs/block evaluation)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package vibes
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
func bindCapabilitiesForCall(exec *Execution, root *Env, rebinder *callFunctionRebinder, capabilities []CapabilityAdapter) error {
9+
if len(capabilities) == 0 {
10+
return nil
11+
}
12+
13+
binding := CapabilityBinding{Context: exec.ctx, Engine: exec.engine}
14+
for _, adapter := range capabilities {
15+
if adapter == nil {
16+
continue
17+
}
18+
scope := &capabilityContractScope{
19+
contracts: map[string]CapabilityMethodContract{},
20+
}
21+
if provider, ok := adapter.(CapabilityContractProvider); ok {
22+
for methodName, contract := range provider.CapabilityContracts() {
23+
name := strings.TrimSpace(methodName)
24+
if name == "" {
25+
return fmt.Errorf("capability contract method name must be non-empty")
26+
}
27+
if _, exists := exec.capabilityContractsByName[name]; exists {
28+
return fmt.Errorf("duplicate capability contract for %s", name)
29+
}
30+
exec.capabilityContractsByName[name] = contract
31+
scope.contracts[name] = contract
32+
}
33+
}
34+
globals, err := adapter.Bind(binding)
35+
if err != nil {
36+
return err
37+
}
38+
for name, val := range globals {
39+
rebound := rebinder.rebindValue(val)
40+
root.Define(name, rebound)
41+
if len(scope.contracts) > 0 {
42+
scope.roots = append(scope.roots, rebound)
43+
}
44+
bindCapabilityContracts(rebound, scope, exec.capabilityContracts, exec.capabilityContractScopes)
45+
}
46+
}
47+
48+
return nil
49+
}

vibes/execution_script.go

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package vibes
33
import (
44
"context"
55
"fmt"
6-
"strings"
76
)
87

98
func (s *Script) Call(ctx context.Context, name string, args []Value, opts CallOptions) (Value, error) {
@@ -58,41 +57,8 @@ func (s *Script) Call(ctx context.Context, name string, args []Value, opts CallO
5857
allowRequire: opts.AllowRequire,
5958
}
6059

61-
if len(opts.Capabilities) > 0 {
62-
binding := CapabilityBinding{Context: exec.ctx, Engine: s.engine}
63-
for _, adapter := range opts.Capabilities {
64-
if adapter == nil {
65-
continue
66-
}
67-
scope := &capabilityContractScope{
68-
contracts: map[string]CapabilityMethodContract{},
69-
}
70-
if provider, ok := adapter.(CapabilityContractProvider); ok {
71-
for methodName, contract := range provider.CapabilityContracts() {
72-
name := strings.TrimSpace(methodName)
73-
if name == "" {
74-
return NewNil(), fmt.Errorf("capability contract method name must be non-empty")
75-
}
76-
if _, exists := exec.capabilityContractsByName[name]; exists {
77-
return NewNil(), fmt.Errorf("duplicate capability contract for %s", name)
78-
}
79-
exec.capabilityContractsByName[name] = contract
80-
scope.contracts[name] = contract
81-
}
82-
}
83-
globals, err := adapter.Bind(binding)
84-
if err != nil {
85-
return NewNil(), err
86-
}
87-
for name, val := range globals {
88-
rebound := rebinder.rebindValue(val)
89-
root.Define(name, rebound)
90-
if len(scope.contracts) > 0 {
91-
scope.roots = append(scope.roots, rebound)
92-
}
93-
bindCapabilityContracts(rebound, scope, exec.capabilityContracts, exec.capabilityContractScopes)
94-
}
95-
}
60+
if err := bindCapabilitiesForCall(exec, root, rebinder, opts.Capabilities); err != nil {
61+
return NewNil(), err
9662
}
9763

9864
if exec.strictEffects {

0 commit comments

Comments
 (0)