Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Represent :Capability type as *capnp.Client. #232

Merged
merged 8 commits into from
May 9, 2022
70 changes: 55 additions & 15 deletions capnpc-go/capnpc-go.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,14 +369,29 @@ func (g *generator) Value(rel *node, t schema.Type, v schema.Value) (string, err
case schema.Type_Which_anyPointer:
data, _ := v.AnyPointer()
var buf bytes.Buffer

sd, err := g.data.copyData(data)
if err != nil {
return "", err
}

switch t.AnyPointer().Which() {
// Unconstrained?
case schema.Type_anyPointer_Which_unconstrained:

switch t.AnyPointer().Unconstrained().Which() {
// Capability type?
case schema.Type_anyPointer_unconstrained_Which_capability:
return "nil", nil
lthibault marked this conversation as resolved.
Show resolved Hide resolved
}
}

// Fall back to default case => generic pointer value
err = templates.ExecuteTemplate(&buf, "pointerValue", pointerValueParams{
G: g,
Value: sd,
})

return buf.String(), err

case schema.Type_Which_list:
Expand Down Expand Up @@ -590,6 +605,20 @@ func (g *generator) defineField(n *node, f field) (err error) {
return err
}
}

switch t.AnyPointer().Which() {
// Unconstrained?
lthibault marked this conversation as resolved.
Show resolved Hide resolved
case schema.Type_anyPointer_Which_unconstrained:

switch t.AnyPointer().Unconstrained().Which() {
// Capability type?
case schema.Type_anyPointer_unconstrained_Which_capability:
// params.FieldType = "*capnp.Client"
return g.r.Render(structCapabilityFieldParams(params))
}
}

// Fall back to default case => generic pointer value
return g.r.Render(structPointerFieldParams{
structFieldParams: params,
Default: defref,
Expand Down Expand Up @@ -647,21 +676,20 @@ func makeNodeTypeRef(n, rel *node) (typeRef, error) {

var (
staticTypeRefs = map[schema.Type_Which]typeRef{
schema.Type_Which_void: {},
schema.Type_Which_bool: {name: "bool"},
schema.Type_Which_int8: {name: "int8"},
schema.Type_Which_int16: {name: "int16"},
schema.Type_Which_int32: {name: "int32"},
schema.Type_Which_int64: {name: "int64"},
schema.Type_Which_uint8: {name: "uint8"},
schema.Type_Which_uint16: {name: "uint16"},
schema.Type_Which_uint32: {name: "uint32"},
schema.Type_Which_uint64: {name: "uint64"},
schema.Type_Which_float32: {name: "float32"},
schema.Type_Which_float64: {name: "float64"},
schema.Type_Which_text: {name: "string"},
schema.Type_Which_data: {name: "[]byte"},
schema.Type_Which_anyPointer: {name: "Pointer", imp: capnpImportSpec},
schema.Type_Which_void: {},
schema.Type_Which_bool: {name: "bool"},
schema.Type_Which_int8: {name: "int8"},
schema.Type_Which_int16: {name: "int16"},
schema.Type_Which_int32: {name: "int32"},
schema.Type_Which_int64: {name: "int64"},
schema.Type_Which_uint8: {name: "uint8"},
schema.Type_Which_uint16: {name: "uint16"},
schema.Type_Which_uint32: {name: "uint32"},
schema.Type_Which_uint64: {name: "uint64"},
schema.Type_Which_float32: {name: "float32"},
schema.Type_Which_float64: {name: "float64"},
schema.Type_Which_text: {name: "string"},
schema.Type_Which_data: {name: "[]byte"},
}
staticListTypeRefs = map[schema.Type_Which]typeRef{
// TODO(light): omitting newfunc since it doesn't have a similar type signature (no errors).
Expand Down Expand Up @@ -726,6 +754,18 @@ func makeTypeRef(t schema.Type, rel *node, nodes nodeMap) (typeRef, error) {
case schema.Type_Which_anyPointer, schema.Type_Which_list, schema.Type_Which_interface:
return typeRef{name: "PointerList", newfunc: "NewPointerList", imp: capnpImportSpec}, nil
}
case schema.Type_Which_anyPointer:
switch t.AnyPointer().Which() {
case schema.Type_anyPointer_Which_unconstrained:

switch t.AnyPointer().Unconstrained().Which() {
case schema.Type_anyPointer_unconstrained_Which_capability:
return typeRef{name: "Client", imp: capnpImportSpec}, nil
lthibault marked this conversation as resolved.
Show resolved Hide resolved
}
}

return typeRef{name: "Ptr", imp: capnpImportSpec}, nil

}
return typeRef{}, fmt.Errorf("unable to reference type %v", t.Which())
}
Expand Down
13 changes: 7 additions & 6 deletions capnpc-go/templateparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ type structFieldParams struct {
}

type (
structFloatFieldParams structUintFieldParams
structInterfaceFieldParams structFieldParams
structVoidFieldParams structFieldParams
structListFieldParams structObjectFieldParams
structPointerFieldParams structObjectFieldParams
structStructFieldParams structObjectFieldParams
structFloatFieldParams structUintFieldParams
structInterfaceFieldParams structFieldParams
structCapabilityFieldParams structFieldParams
structVoidFieldParams structFieldParams
structListFieldParams structObjectFieldParams
structPointerFieldParams structObjectFieldParams
structStructFieldParams structObjectFieldParams
)

type structBoolFieldParams struct {
Expand Down
18 changes: 18 additions & 0 deletions capnpc-go/templates/structCapabilityField
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
func (s {{.Node.Name}}) {{.Field.Name|title}}() *{{.FieldType}} {
{{template "_checktag" . -}}
p, _ := s.Struct.Ptr({{.Field.Slot.Offset}})
return p.Interface().Client()
}

{{template "_hasfield" .}}

func (s {{.Node.Name}}) Set{{.Field.Name|title}}(c *{{.FieldType}}) error {
{{template "_settag" . -}}
if !c.IsValid() {
return s.Struct.SetPtr({{.Field.Slot.Offset}}, capnp.Ptr{})
}
seg := s.Segment()
in := {{.G.Capnp}}.NewInterface(seg, seg.Message().AddCap(c))
return s.Struct.SetPtr({{.Field.Slot.Offset}}, in.ToPtr())
}

14 changes: 10 additions & 4 deletions internal/aircraftlib/aircraft.capnp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions pogs/pogs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1227,11 +1227,8 @@ func zfill(c air.Z, g *Z) error {
case air.Z_Which_anyList:
return c.SetAnyList(g.AnyList.ToPtr())
case air.Z_Which_anyCapability:
if g.AnyCapability == nil {
return c.SetAnyCapability(capnp.Ptr{})
}
cap := capnp.NewInterface(c.Segment(), c.Message().AddCap(g.AnyCapability))
return c.SetAnyCapability(cap.ToPtr())
return c.SetAnyCapability(cap.Client())
lthibault marked this conversation as resolved.
Show resolved Hide resolved
default:
return fmt.Errorf("zfill: unknown type: %v", g.Which)
}
Expand Down
14 changes: 10 additions & 4 deletions rpc/internal/testcapnp/test.capnp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions rpc/receiveranswer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ func (me *capArgsTest) Self(ctx context.Context, p testcapnp.CapArgsTest_self) e

func (me *capArgsTest) Call(ctx context.Context, p testcapnp.CapArgsTest_call) error {
defer close(me.Errs)
cap, err := p.Args().Cap()
chkfatal(err)
client := cap.Interface().Client()
client := p.Args().Cap()
chkfatal(client.Resolve(ctx))
brand, ok := server.IsServer(client.State().Brand)
if !ok {
Expand Down Expand Up @@ -81,7 +79,7 @@ func TestBootstrapReceiverAnswerRpc(t *testing.T) {

res, rel := c.Call(ctx, func(p testcapnp.CapArgsTest_call_Params) error {
capId := p.Message().AddCap(c.Client.AddRef())
p.SetCap(capnp.NewInterface(p.Segment(), capId).ToPtr())
p.SetCap(capnp.NewInterface(p.Segment(), capId).Client())
lthibault marked this conversation as resolved.
Show resolved Hide resolved
return nil
})
defer rel()
Expand Down Expand Up @@ -126,7 +124,7 @@ func TestCallReceiverAnswerRpc(t *testing.T) {
self := selfRes.Self()
callRes, rel := self.Call(ctx, func(p testcapnp.CapArgsTest_call_Params) error {
capId := p.Message().AddCap(self.Client.AddRef())
p.SetCap(capnp.NewInterface(p.Segment(), capId).ToPtr())
p.SetCap(capnp.NewInterface(p.Segment(), capId).Client())
lthibault marked this conversation as resolved.
Show resolved Hide resolved
return nil
})
self.Release()
Expand Down