better error message and padding support (#83)
This commit improves the error message when the Ivar and struct field are not at the same offset. If the parent class has fields its fields are located before the child's in the struct. This causes issues since there is no way to know the internal structure of the class so padding is required. This commit enables the use of padding by using the underscore (_) identifier with an array of bytes that matches the parent's field size. Still, if any fields are misaligned an error is produced.
- commit
- b4ba55de7c488d0483dd40f45e8dbd6be4b8ea44
- author
- TotallyGamerJet · · 2022-12-17 18:34 UTC
- committer
- GitHub · · 2022-12-17 18:34 UTC
- parent
- 7b60891
- signature
- GPG Unverified
4AEE18F83AFDEB23The key is unknown
1 file · +4 -1
M objc/objc_runtime_darwin.go +4 -1
| @@ -286,6 +286,9 @@ func RegisterClass(object Selector) (Class, error) { | |||
| 286 | 286 | // Start at 1 because we skip the class object which is first | |
| 287 | 287 | for i := 1; i < strct.NumField(); i++ { | |
| 288 | 288 | f := strct.Field(i) | |
| 289 | + | if f.Name == "_" { | |
| 290 | + | continue | |
| 291 | + | } | |
| 289 | 292 | size := f.Type.Size() | |
| 290 | 293 | alignment := uint8(math.Log2(float64(f.Type.Align()))) | |
| 291 | 294 | enc, err := encodeType(f.Type, false) | |
| @@ -296,7 +299,7 @@ func RegisterClass(object Selector) (Class, error) { | |||
| 296 | 299 | return 0, fmt.Errorf("objc: couldn't add Ivar %s", f.Name) | |
| 297 | 300 | } | |
| 298 | 301 | if offset := class.InstanceVariable(f.Name).Offset(); offset != f.Offset { | |
| 299 | - | return 0, fmt.Errorf("objc: couldn't add Ivar %s", f.Name) | |
| 302 | + | return 0, fmt.Errorf("objc: couldn't add Ivar %s because offset (%d != %d)", f.Name, offset, f.Offset) | |
| 300 | 303 | } | |
| 301 | 304 | } | |
| 302 | 305 | objc_registerClassPair(class) | |
| @@ -286,6 +286,9 @@ func RegisterClass(object Selector) (Class, error) { | |||||
| 286 | // Start at 1 because we skip the class object which is first | 286 | // Start at 1 because we skip the class object which is first | ||
| 287 | for i := 1; i < strct.NumField(); i++ { | 287 | for i := 1; i < strct.NumField(); i++ { | ||
| 288 | f := strct.Field(i) | 288 | f := strct.Field(i) | ||
| 289 | + | if f.Name == "_" { | |||
| 290 | + | continue | |||
| 291 | + | } | |||
| 289 | size := f.Type.Size() | 292 | size := f.Type.Size() | ||
| 290 | alignment := uint8(math.Log2(float64(f.Type.Align()))) | 293 | alignment := uint8(math.Log2(float64(f.Type.Align()))) | ||
| 291 | enc, err := encodeType(f.Type, false) | 294 | enc, err := encodeType(f.Type, false) | ||
| @@ -296,7 +299,7 @@ func RegisterClass(object Selector) (Class, error) { | |||||
| 296 | return 0, fmt.Errorf("objc: couldn't add Ivar %s", f.Name) | 299 | return 0, fmt.Errorf("objc: couldn't add Ivar %s", f.Name) | ||
| 297 | } | 300 | } | ||
| 298 | if offset := class.InstanceVariable(f.Name).Offset(); offset != f.Offset { | 301 | if offset := class.InstanceVariable(f.Name).Offset(); offset != f.Offset { | ||
| 299 | - | return 0, fmt.Errorf("objc: couldn't add Ivar %s", f.Name) | 302 | + | return 0, fmt.Errorf("objc: couldn't add Ivar %s because offset (%d != %d)", f.Name, offset, f.Offset) |
| 300 | } | 303 | } | ||
| 301 | } | 304 | } | ||
| 302 | objc_registerClassPair(class) | 305 | objc_registerClassPair(class) | ||