Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic Invocation #19

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Dynamic Invocation #19

wants to merge 6 commits into from

Conversation

steipete
Copy link
Owner

@steipete steipete commented Jun 18, 2020

With using objc_msgForward, we can add hooks without offering a type signature.

_ = try testObj.hook(#selector(TestClass.executeBlock)) { bSelf in
  print("Before Interposing Dynamic Hook for \(bSelf)")
}

This is slightly slower than the typed call, but way more convenient

This is achieved with a few tricks:

  1. We install the subclass
  2. The hooked method gets a super trampoline
  3. we add a interpose_origMethod and copy the implementation (which is the super trampoline)
  4. change orig impl to objc_msgForward
  5. Custom forward handler calls hooks, changes invocation selector to interpose_
  6. super handler in interpose changes _cmd back to expected and calls super

TODO:

  • class-based not just objects (can't easily fix the selector then...
  • pass on invocation + helpers to get parameters
  • copy block parameters as needed
  • check block type signature
  • Generalize interpose _cmd filter

@codecov
Copy link

codecov bot commented Jun 18, 2020

Codecov Report

Merging #19 into master will increase coverage by 1.58%.
The diff coverage is 86.51%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #19      +/-   ##
==========================================
+ Coverage   83.09%   84.67%   +1.58%     
==========================================
  Files          15       18       +3     
  Lines         899     1057     +158     
==========================================
+ Hits          747      895     +148     
- Misses        152      162      +10     
Impacted Files Coverage Δ
Sources/InterposeKit/InterposeError.swift 55.17% <50.00%> (-0.39%) ⬇️
Sources/InterposeKit/InterposeKit.swift 89.18% <62.50%> (+0.45%) ⬆️
Sources/InterposeKit/ObjectHook.swift 66.66% <62.50%> (+1.52%) ⬆️
Sources/InterposeKit/InterposeRuntime.swift 83.78% <83.78%> (ø)
Sources/SuperBuilder/src/ITKSuperBuilder.m 87.71% <85.71%> (-0.34%) ⬇️
Sources/InterposeKit/InterposeSubclass.swift 91.20% <86.95%> (-3.24%) ⬇️
Sources/InterposeKit/DynamicHook.swift 91.80% <91.80%> (ø)
Sources/InterposeKit/AnyHook.swift 75.60% <100.00%> (+1.25%) ⬆️
Sources/InterposeKit/ClassHook.swift 83.33% <100.00%> (-0.88%) ⬇️
Sources/InterposeKit/HookFinder.swift 96.77% <100.00%> (+0.34%) ⬆️
... and 5 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7a9367e...89f32cc. Read the comment docs.

@crsantos
Copy link

Hey Peter, hope you're fine.

Honest question, was there any work left to do on this PR?
Because this would be an awesome feature ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants