theos / logos

Preprocessor that simplifies Objective-C hooking.
https://theos.dev/docs/logos
Other
206 stars 34 forks source link

%init of groups not compatible with ternary operators #50

Closed Nosskirneh closed 4 years ago

Nosskirneh commented 4 years ago

It would make sense to write the snippet

if (%c(SBHomeScreenBackdropView))
    %init(SwitcherBackdrop_iOS12);
else
    %init(SwitcherBackdrop_iOS11);

as a ternary statement,

%c(SBHomeScreenBackdropView) ? %init(SwitcherBackdrop_iOS12) : %init(SwitcherBackdrop_iOS11);

but that results in errors.

==> Compiling Tweak.xm (arm64)…
Tweak.xm:804:61: error: expected expression
    _logos_static_class_lookup$SBHomeScreenBackdropView() ? {Class _logos_class$SwitcherBackdrop_iOS12$...
                                                            ^
Tweak.xm:804:61: error: expected ':'
    _logos_static_class_lookup$SBHomeScreenBackdropView() ? {Class _logos_class$SwitcherBackdrop_iOS12$...
                                                            ^
                                                            : 
Tweak.xm:804:59: note: to match this '?'
    _logos_static_class_lookup$SBHomeScreenBackdropView() ? {Class _logos_class$SwitcherBackdrop_iOS12$...
                                                          ^
Tweak.xm:804:61: error: expected expression
    _logos_static_class_lookup$SBHomeScreenBackdropView() ? {Class _logos_class$SwitcherBackdrop_iOS12$...
                                                            ^
3 errors generated.

It seems like Logos outputs a mix between a normal if statement and a ternary operator.

kirb commented 4 years ago

I don’t believe this is possible without restructuring how %init is implemented. It would involve us splitting out the generated code you’re seeing there into its own inline function. That may be worth it anyway though, not just for this specific edge case but just to make the generated code slightly cleaner?

Nosskirneh commented 4 years ago

I see. I wished I could help more, but I've never touched Perl.

uroboro commented 4 years ago

This is somewhat related to item 2 of #5.

Can you try doing this in the mean time?

%c(SBHomeScreenBackdropView) ? (%init(SwitcherBackdrop_iOS12)) : (%init(SwitcherBackdrop_iOS11));
Nosskirneh commented 4 years ago

@uroboro Yes, that works fine, thank you!

uroboro commented 4 years ago

While it does indeed work, I don’t believe this is a good application of the ternary operator. I won’t be adding any specific support for this usage.