Skip to content

Commit

Permalink
fix: objc optional property attribute parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
blacktop committed Apr 6, 2024
1 parent d10da4c commit c8a3f8b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
7 changes: 6 additions & 1 deletion types/objc/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,12 @@ func (c *Class) dump(verbose, addrs bool) string {
}
for _, prop := range c.Props {
if verbose {
props += fmt.Sprintf("@property %s%s%s;\n", prop.Attributes(), prop.Type(), prop.Name)
attrs, optional := prop.Attributes()
var optionalStr string
if optional {
optionalStr = "@optional\n"
}
props += fmt.Sprintf("%s@property %s%s%s;\n", optionalStr, attrs, prop.Type(), prop.Name)
} else {
props += fmt.Sprintf("@property (%s) %s;\n", prop.EncodedAttributes, prop.Name)
}
Expand Down
2 changes: 1 addition & 1 deletion types/objc/objc.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ type Property struct {
func (p *Property) Type() string {
return getPropertyType(p.EncodedAttributes)
}
func (p *Property) Attributes() string {
func (p *Property) Attributes() (string, bool) {
return getPropertyAttributeTypes(p.EncodedAttributes)
}

Expand Down
7 changes: 6 additions & 1 deletion types/objc/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ func (p *Protocol) dump(verbose, addrs bool) string {
props += "\n"
for _, prop := range p.InstanceProperties {
if verbose {
props += fmt.Sprintf("@property %s%s%s;\n", prop.Attributes(), prop.Type(), prop.Name)
attrs, optional := prop.Attributes()
var optionalStr string
if optional {
optionalStr = "@optional\n"
}
props += fmt.Sprintf("%s@property %s%s%s;\n", optionalStr, attrs, prop.Type(), prop.Name)
} else {
props += fmt.Sprintf("@property (%s) %s;\n", prop.EncodedAttributes, prop.Name)
}
Expand Down
12 changes: 7 additions & 5 deletions types/objc/type_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const (
propertyStrong = "P" // property GC'able
propertyAtomic = "A" // property atomic
propertyNonAtomic = "N" // property non-atomic
propertyOptional = "?" // property optional // TODO: correct this when Apple releases the macOS 14.4 ObjC source code
propertyOptional = "?" // property optional
)

type methodEncodedArg struct {
Expand Down Expand Up @@ -170,8 +170,8 @@ func getPropertyType(attrs string) (typ string) {
case propertyStrong:
case propertyAtomic:
case propertyNonAtomic:
case propertyType:
case propertyOptional:
case propertyType:
typParts = append([]string{strings.TrimPrefix(sub, propertyType)}, typParts...)
attr = strings.Join(typParts, ",")
default:
Expand All @@ -193,11 +193,13 @@ func getPropertyType(attrs string) (typ string) {
return typ
}

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

isOptional := false

for _, attr := range strings.Split(attrs, ",") {
if strings.HasPrefix(attr, propertyIVar) {
// found ivar name
Expand Down Expand Up @@ -231,15 +233,15 @@ func getPropertyAttributeTypes(attrs string) string {
case propertyStrong:
attrsList = append(attrsList, "collectable")
case propertyOptional:
attrsList = append(attrsList, "optional")
isOptional = true
}
}

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

return attrsStr
return attrsStr, isOptional
}

func getIVarType(ivType string) string {
Expand Down

0 comments on commit c8a3f8b

Please sign in to comment.