Skip to content

Commit

Permalink
chore: add objc class property Type() and Attributes() getters
Browse files Browse the repository at this point in the history
  • Loading branch information
blacktop committed Dec 13, 2023
1 parent 625bf9e commit bc9049c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 46 deletions.
6 changes: 3 additions & 3 deletions objc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1268,9 +1268,9 @@ func (f *File) GetObjCProperties(vmaddr uint64) ([]objc.Property, error) {
return nil, fmt.Errorf("failed to read prop attributes cstring: %v", err)
}
objcProperties = append(objcProperties, objc.Property{
PropertyT: prop,
Name: name,
Attributes: attrib,
PropertyT: prop,
Name: name,
EncodedAttributes: attrib,
})
}

Expand Down
4 changes: 2 additions & 2 deletions types/objc/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ func (c *Class) dump(verbose, addrs bool) string {
}
for _, prop := range c.Props {
if verbose {
props += fmt.Sprintf("@property %s%s;\n", getPropertyAttributeTypes(prop.Attributes), prop.Name)
props += fmt.Sprintf("@property %s%s%s;\n", prop.Attributes(), prop.Type(), prop.Name)
} else {
props += fmt.Sprintf("@property (%s) %s;\n", prop.Attributes, prop.Name)
props += fmt.Sprintf("@property (%s) %s;\n", prop.EncodedAttributes, prop.Name)
}
}
props += "\n"
Expand Down
11 changes: 9 additions & 2 deletions types/objc/objc.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,15 @@ type PropertyT struct {

type Property struct {
PropertyT
Name string
Attributes string
Name string
EncodedAttributes string
}

func (p *Property) Type() string {
return getPropertyType(p.EncodedAttributes)
}
func (p *Property) Attributes() string {
return getPropertyAttributeTypes(p.EncodedAttributes)
}

// CFString object in a 64-bit MachO file
Expand Down
4 changes: 2 additions & 2 deletions types/objc/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ func (p *Protocol) dump(verbose, addrs bool) string {
if len(p.InstanceProperties) > 0 {
for _, prop := range p.InstanceProperties {
if verbose {
props += fmt.Sprintf("@property %s%s;\n", getPropertyAttributeTypes(prop.Attributes), prop.Name)
props += fmt.Sprintf("@property %s%s%s;\n", prop.Attributes(), prop.Type(), prop.Name)
} else {
props += fmt.Sprintf("@property (%s) %s;\n", prop.Attributes, prop.Name)
props += fmt.Sprintf("@property (%s) %s;\n", prop.EncodedAttributes, prop.Name)
}
}
}
Expand Down
83 changes: 46 additions & 37 deletions types/objc/type_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,62 +150,71 @@ func getMethodWithArgs(method, returnType string, args []string) string {
return fmt.Sprintf("(%s)%s;", returnType, method)
}

func getPropertyAttributeTypes(attrs string) string {
// var ivarStr string
var typeStr string
var attrsStr string
var attrsList []string
func getPropertyType(attrs string) string {
var typ string

for _, attr := range strings.Split(attrs, ",") {
if strings.HasPrefix(attr, propertyType) {
attr = strings.TrimPrefix(attr, propertyType)
if strings.HasPrefix(attr, "@\"") {
typeStr = strings.Trim(attr, "@\"") + " *"
typ = strings.Trim(attr, "@\"") + " *"
} else {
if val, ok := typeEncoding[attr]; ok {
typeStr = val + " "
typ = val + " "
}
}
} else if strings.HasPrefix(attr, propertyIVar) {
break
}
}

return typ
}

func getPropertyAttributeTypes(attrs string) string {
// var ivarStr string
var attrsStr string
var attrsList []string

for _, attr := range strings.Split(attrs, ",") {
if strings.HasPrefix(attr, propertyIVar) {
// found ivar name
// ivarStr = strings.TrimPrefix(attr, propertyIVar)
continue
} else {
// TODO: handle the following cases
// @property struct YorkshireTeaStruct structDefault; ==> T{YorkshireTeaStruct="pot"i"lady"c},VstructDefault
// @property int (*functionPointerDefault)(char *); ==> T^?,VfunctionPointerDefault
switch attr {
case propertyGetter:
attr = strings.TrimPrefix(attr, propertyGetter)
attrsList = append(attrsList, fmt.Sprintf("getter=%s", attr))
case propertySetter:
attr = strings.TrimPrefix(attr, propertySetter)
attrsList = append(attrsList, fmt.Sprintf("setter=%s", attr))
case propertyReadOnly:
attrsList = append(attrsList, "readonly")
case propertyNonAtomic:
attrsList = append(attrsList, "nonatomic")
case propertyAtomic:
attrsList = append(attrsList, "atomic")
case propertyBycopy:
attrsList = append(attrsList, "copy")
case propertyByref:
attrsList = append(attrsList, "retain")
case propertyWeak:
attrsList = append(attrsList, "weak")
case propertyDynamic:
attrsList = append(attrsList, "dynamic")
case propertyStrong:
attrsList = append(attrsList, "collectable")
}
}
// TODO: handle the following cases
// @property struct YorkshireTeaStruct structDefault; ==> T{YorkshireTeaStruct="pot"i"lady"c},VstructDefault
// @property int (*functionPointerDefault)(char *); ==> T^?,VfunctionPointerDefault
switch attr {
case propertyGetter:
attr = strings.TrimPrefix(attr, propertyGetter)
attrsList = append(attrsList, fmt.Sprintf("getter=%s", attr))
case propertySetter:
attr = strings.TrimPrefix(attr, propertySetter)
attrsList = append(attrsList, fmt.Sprintf("setter=%s", attr))
case propertyReadOnly:
attrsList = append(attrsList, "readonly")
case propertyNonAtomic:
attrsList = append(attrsList, "nonatomic")
case propertyAtomic:
attrsList = append(attrsList, "atomic")
case propertyBycopy:
attrsList = append(attrsList, "copy")
case propertyByref:
attrsList = append(attrsList, "retain")
case propertyWeak:
attrsList = append(attrsList, "weak")
case propertyDynamic:
attrsList = append(attrsList, "dynamic")
case propertyStrong:
attrsList = append(attrsList, "collectable")
}
}

if len(attrsList) > 0 {
attrsStr = fmt.Sprintf("(%s) ", strings.Join(attrsList, ", "))
}

return fmt.Sprintf("%s%s", attrsStr, typeStr)
return attrsStr
}

func getIVarType(ivType string) string {
Expand Down

0 comments on commit bc9049c

Please sign in to comment.