libobjc2 icon indicating copy to clipboard operation
libobjc2 copied to clipboard

objc_direct methods

Open digital-pers0n opened this issue 4 years ago • 3 comments

GNU runtime doesn't support direct calls at this time Aren't they just simple C-functions?

digital-pers0n avatar Jul 02 '20 15:07 digital-pers0n

Not quite. They also need to ensure that the +initialize method is invoked. There's a comment in the Mac version that describes some of this:

  // Generate:
  //
  // /* for class methods only to force class lazy initialization */
  // self = [self self];
  //
  // /* unless the receiver is never NULL */
  // if (self == nil) {
  //     return (ReturnType){ };
  // }
  //
  // _cmd = @selector(...)

I think this is actually wrong, because if you do objc_allocateInstance and then send it a message, it won't call +initialize and they've broken the language semantics.

davidchisnall avatar Jul 02 '20 15:07 davidchisnall

I don't really like requiring a message send here, because it largely defeats the point of direct messages for efficiency. I'm tempted to add an __objc_initialize function to guarantee that +initialize has been called. I'm also tempted to make the caller responsible for calling it - if a static method is called from within another method on the same class (or a subclass) it can be elided, and a little bit of analysis should allow it to be elided elsewhere as well.

davidchisnall avatar Apr 02 '21 10:04 davidchisnall

Looks like they know about this problem. several entry points for direct methods unnecessary class initialization in direct methods Also in the recent Apple's objc runtime there are new runtime functions to bypass objc_msgSend for rarely overridden methods such as self, new, class, isKindOfClass and respondsToSelector the functions are prefixed with objc_opt_* NSObject.mm

digital-pers0n avatar May 02 '21 17:05 digital-pers0n

It looks as if the code for the objc_opt_ stuff is not in the public LLVM, only in Apple's fork.

davidchisnall avatar Jan 06 '24 10:01 davidchisnall