Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I assume and handle SEL in Objective-C as a pointer to something?

I'm trying to interface Lua with Objective-C, and I think string conversion with NSSelectorFromString() has too big an overhead because Lua has to copy all strings to internalize them (although I'm not sure about this).

So I'm trying to find more lightweight way to represent a selector in Lua. An Objective-C selector is an abstracted type, but it's defined as a pointer to something:

typedef struct objc_selector    *SEL; 

So it looks safe to handle as a regular pointer, so I can pass it to Lua with lightuserdata. Is this fine?

like image 482
eonil Avatar asked Nov 04 '22 19:11

eonil


1 Answers

I don't believe it is safe to handle it as a pointer (even a void pointer), because if this ever changes in a future implementation or a different implementation of the language. I didn't see a formal Objective-C spec that tells what is implementation defines, but often when opaque types like this are used it means that you shouldn't have to know details about the underlying type is. In fact, the struct is forward-declared so that you can't access any of its members.

The other problem you might run into is implementing equality comparisons: are selectors references to a pool of constants or is each selector mutable. Once again, implementation defined.

Using C strings as suggested above is probably your best bet; ruby manages to use symbols for selectors and doesn't have too much of a performance penalty. Since the strings are const, lua doesn't need to copy them, but probably does anyway to be safe. If you can find a way to not copy the strings you might not take that much of a performance hit.

like image 56
RC Howe Avatar answered Nov 12 '22 10:11

RC Howe