@@ -54,7 +54,7 @@ type Execution struct {
5454 moduleLoading map [string ]bool
5555 moduleLoadStack []string
5656 moduleStack []moduleContext
57- capabilityContracts map [string ]CapabilityMethodContract
57+ capabilityContracts map [* Builtin ]CapabilityMethodContract
5858 receiverStack []Value
5959 envStack []* Env
6060 strictEffects bool
@@ -607,7 +607,7 @@ func (exec *Execution) invokeCallable(callee Value, receiver Value, args []Value
607607 return exec .callFunction (callee .Function (), receiver , args , kwargs , block , pos )
608608 case KindBuiltin :
609609 builtin := callee .Builtin ()
610- contract , hasContract := exec .capabilityContracts [builtin . Name ]
610+ contract , hasContract := exec .capabilityContracts [builtin ]
611611 if hasContract && contract .ValidateArgs != nil {
612612 if err := contract .ValidateArgs (args , kwargs , block ); err != nil {
613613 return NewNil (), exec .wrapError (err , pos )
@@ -3400,7 +3400,7 @@ func (s *Script) Call(ctx context.Context, name string, args []Value, opts CallO
34003400 moduleLoading : make (map [string ]bool ),
34013401 moduleLoadStack : make ([]string , 0 , 8 ),
34023402 moduleStack : make ([]moduleContext , 0 , 8 ),
3403- capabilityContracts : make (map [string ]CapabilityMethodContract ),
3403+ capabilityContracts : make (map [* Builtin ]CapabilityMethodContract ),
34043404 receiverStack : make ([]Value , 0 , 8 ),
34053405 envStack : make ([]* Env , 0 , 8 ),
34063406 strictEffects : s .engine .config .StrictEffects ,
@@ -3409,28 +3409,33 @@ func (s *Script) Call(ctx context.Context, name string, args []Value, opts CallO
34093409
34103410 if len (opts .Capabilities ) > 0 {
34113411 binding := CapabilityBinding {Context : exec .ctx , Engine : s .engine }
3412+ knownCapabilityContracts := make (map [string ]CapabilityMethodContract )
34123413 for _ , adapter := range opts .Capabilities {
34133414 if adapter == nil {
34143415 continue
34153416 }
3417+ adapterContracts := map [string ]CapabilityMethodContract {}
34163418 if provider , ok := adapter .(CapabilityContractProvider ); ok {
34173419 for methodName , contract := range provider .CapabilityContracts () {
34183420 name := strings .TrimSpace (methodName )
34193421 if name == "" {
34203422 return NewNil (), fmt .Errorf ("capability contract method name must be non-empty" )
34213423 }
3422- if _ , exists := exec . capabilityContracts [name ]; exists {
3424+ if _ , exists := knownCapabilityContracts [name ]; exists {
34233425 return NewNil (), fmt .Errorf ("duplicate capability contract for %s" , name )
34243426 }
3425- exec .capabilityContracts [name ] = contract
3427+ knownCapabilityContracts [name ] = contract
3428+ adapterContracts [name ] = contract
34263429 }
34273430 }
34283431 globals , err := adapter .Bind (binding )
34293432 if err != nil {
34303433 return NewNil (), err
34313434 }
34323435 for name , val := range globals {
3433- root .Define (name , rebinder .rebindValue (val ))
3436+ rebound := rebinder .rebindValue (val )
3437+ root .Define (name , rebound )
3438+ bindCapabilityContracts (rebound , adapterContracts , exec .capabilityContracts )
34343439 }
34353440 }
34363441 }
0 commit comments