Permalink
Browse files

Make ObjectByName work with Go types too.

Fixes #91.
  • Loading branch information...
1 parent 2fcb6e2 commit ea1e5fb719535f7c1a9281b0801960a918d7672a @niemeyer niemeyer committed Sep 5, 2014
Showing with 26 additions and 5 deletions.
  1. +6 −2 datatype.go
  2. +10 −2 qml.go
  3. +10 −1 qml_test.go
View
@@ -153,11 +153,15 @@ func unpackDataValue(dvalue *C.DataValue, engine *Engine) interface{} {
var c uint32 = *(*uint32)(datap)
return color.RGBA{byte(c >> 16), byte(c >> 8), byte(c), byte(c >> 24)}
case C.DTGoAddr:
- return (*(**valueFold)(datap)).gvalue
+ // ObjectByName also does this fold conversion, to have access
+ // to the cvalue. Perhaps the fold should be returned.
+ fold := (*(**valueFold)(datap))
+ ensureEngine(engine.addr, unsafe.Pointer(fold))
+ return fold.gvalue
case C.DTInvalid:
return nil
case C.DTObject:
- // TODO Would be good to preserve identity on the Go side. See ensureEngine as well.
+ // TODO Would be good to preserve identity on the Go side. See initGoType as well.
obj := &Common{
engine: engine,
addr: *(*unsafe.Pointer)(datap),
View
@@ -639,13 +639,21 @@ func (obj *Common) Map(property string) *Map {
func (obj *Common) ObjectByName(objectName string) Object {
cname, cnamelen := unsafeStringData(objectName)
var dvalue C.DataValue
+ var object Object
RunMain(func() {
qname := C.newString(cname, cnamelen)
defer C.delString(qname)
C.objectFindChild(obj.addr, qname, &dvalue)
+ value := unpackDataValue(&dvalue, obj.engine)
+ if dvalue.dataType == C.DTGoAddr {
+ datap := unsafe.Pointer(&dvalue.data)
+ fold := (*(**valueFold)(datap))
+ object = &Common{fold.cvalue, obj.engine}
+ } else {
+ object, _ = value.(Object)
+ }
})
- object, ok := unpackDataValue(&dvalue, obj.engine).(Object)
- if !ok {
+ if object == nil {
panic(fmt.Sprintf("cannot find descendant with objectName == %q", objectName))
}
return object
View
@@ -595,7 +595,7 @@ var tests = []struct {
QMLLog: "Identical: true",
},
{
- Summary: "Object finding via objectName",
+ Summary: "Object finding via ObjectByName",
QML: `Item { Item { objectName: "subitem"; property string s: "<found>" } }`,
Done: func(c *TestData) {
obj := c.root.ObjectByName("subitem")
@@ -604,6 +604,15 @@ var tests = []struct {
},
},
{
+ Summary: "Object finding via ObjectByName on GoType",
+ QML: `Item { GoType { objectName: "subitem"; property string s: "<found>" } }`,
+ Done: func(c *TestData) {
+ obj := c.root.ObjectByName("subitem")
+ c.Check(obj.String("s"), Equals, "<found>")
+ c.Check(func() { c.root.ObjectByName("foo") }, Panics, `cannot find descendant with objectName == "foo"`)
+ },
+ },
+ {
Summary: "Register Go type",
QML: `GoType { objectName: "test"; Component.onCompleted: console.log("String is", stringValue) }`,
QMLLog: "String is <initial>",

0 comments on commit ea1e5fb

Please sign in to comment.