ios - Blocks and messaging -


the question here more of educational one. began think of hour ago while flipping around lego block (silly, know).

a block object created on stack, understand.

let a object, means can do:

[a message]; 

based on that, if block object, do:

[block message]; 

am correct?

and when runtime sees that, call:

objc_msgsend(block, @selector(message), nil); 

so question is, how can send block message?

and if possible, imagine possible send block message arguments being blocks well?

and, if call block doing:

block(); 

does mean make block message (sel) well, because blocks have signature void (^)(void) resembles of method?

because if possible, following surprise me:

objc_msgsend(block, @selector(block), block); 

or:

objc_msgsend(block1, @selector(block2), block3); 

i hope imagination not running bit wild , understanding not off here (correct me, if is).

blocks objects purposes of storage , referencing. making them objects, blocks can retain/release'd , can, therefore, shoved arrays or other collection classes. respond copy.

that's it. block starts on stack largely compiler implementation detail.

when block's code invoked, not done through objc_msgsend(). if read source block runtime , llvm compiler, you'd find that:

  • a block c structure contains description of data has been captured (so can cleaned up) , pointer function -- chunk of code -- executable portion of block

  • the block function standard c function first argument must always reference block. rest of argument list arbitrary , works old c function or objective-c method.

so, manual calls objc_msgsend() treat block other random objc object and, thus, won't call code in block, nor, if did (and there spi can method... but, don't use it) pass argument list controllable.


one aside, though of relevance.

imp_implementationwithblock() takes block reference , returns imp can plugged objective-c class (see class_addmethod()) such when method invoked, block called.

the implementation of imp_implementationwithblock() takes advantage of layout of call site of objective-c method vs. block. block always:

blockfunc(blockref, ...) 

and objc method always:

methodfunc(selfref, sel, ...) 

because want imp_implementationwithblock() block take target object method first block parameter (i.e. self), imp_implementationwithblock() returns trampoline function when called (via objc_msgsend()):

- slides self reference slot selector (i.e. arg 0 -> arg 1) - finds implementing block puts pointer block arg 0 - jmps block's implementation pointer 

the finds implementing block bit kinda interesting, irrelevant question (hell, imp_implementationwithblock() bit irrelevant, too, may of interest).


thanks response. it's eye opener. part blocks calling not done thru objc_msgsend() tells me because blocks not part of normal object-hierachry (but coda's mentioning of nsblock seems refute understand far, because nsblock make part of object-hierachy). feel free take stab @ me, if understanding still off far. interested in hearing more followings 1: spi , way (how) call method. 2: underlying mechanisms of: sliding self reference slot. 3: finds implementing block , puts pointer block arg 0. if have time share , write bit more in detail, ears; find fascinating. in advance.

the blocks, themselves, standard objective-c object. block instance contains pointer executable code, captured state, , helpers used copy said state stack heap (if requested) , cleanup state on block's destruction.

the block's executable code not invoked method. block has other methods -- retain, release, copy, etc... -- can invoked directly other method, executable code not publicly 1 of methods.

the spi doesn't special; works blocks take no arguments , nothing more doing block().

if want know how whole argument slide thing works (and how enables tail calling through block), i'd suggest reading this or this. well, source blocks runtime, objc runtime, , llvm available.

that includes fun bit imp grabs block , shoves arg0.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -