luapower / objc

Objective-C & Cocoa Bridge
http://luapower.com/objc
25 stars 7 forks source link

Calling static methods? #6

Open ducktype opened 4 years ago

ducktype commented 4 years ago

Hi, i'm testing around ffi and osx for my first time so try to be patient with me, could be i'm missing something obvious :)

Basically i want to take a screenshot of the whole screen, to do that seems i need to call an objc function in the CoreGraphics osx framework, the function signature is CGImage CGDisplayCreateImage(uint display_id).

To me seems that lua try to call the CGDisplayCreateImage method of objc passing some lua table as first argument, probably passing self as in methods calls could be i need to call it statically can i do that?

Also as a side problem i need to read an objc constant kCGDirectMainDisplay that should be valued at 1 but i expect it to be in objc.kCGDirectMainDisplay after objc.load'CoreGraphics' but is not here, i've no clue

i've also seen this issue: https://github.com/luapower/objc/issues/5 searchig for a warning "[objc] cdef size of C type is unknown or too large at line 4" i get during objc.load'CoreGraphics', cold be this the cause of the problem? in this case can i hope to dig inside objc.lua and fix the xml bridge parsing and fix it?

PS: Your work on luapower is very cool i love the design principles that guide the project, so much better than luarocks and others i've seen while choosing which lua enviroment to try, so even if it no so much.... a BIG THANK YOU given you seems the only committer

test code:

local inspect = require'inspect'
local objc = require'objc'
objc.load'CoreGraphics'
inspect(objc.CGDisplayCreateImage)
inspect(objc.kCGDirectMainDisplay)
--objc.kCGDirectMainDisplay is nill but it should be defined as 1
local ss = objc:CGDisplayCreateImage(1)

output:

[objc] cdef             size of C type is unknown or too large at line 4
        struct CGRect {
        struct CGPoint origin;
        struct CGSize size;
}
<function 1>
nil
/luapower-all-master/bin/osx64/luajit-bin: ./objc.lua:2227: bad argument #1 to 'func' (cannot convert 'table' to 'unsigned int')
stack traceback:
        [C]: in function 'func'
        ./objc.lua:2227: in function 'CGDisplayCreateImage'
        ss_test.lua:16: in main chunk
        [C]: at 0x0100000b90
capr commented 4 years ago

Hi,

If you got errors from bridgesupport, you won't have those types available so yes, that could be the problem.

If you want to fix the parser, I will assist with any questions you might have (coding-wise my plate is full these days). Note that even if you fix the parser, the limitations remain due to LuaJIT ffi.

You might also want to research how Swift bridges with Cocoa, maybe they enhanced the C RTTI API or maybe they introduced a new way to access the metadata provided by bridgesupport, dunno, but surely that API, whatever it is, they won't screw with, since their new shiny language sits on it.

capr commented 4 years ago

Generating AST of ObjC headers with clang and then parsing that might be one way to do it (basically you'd be reimplementing the buggy gen_bridge_support), see https://stackoverflow.com/questions/10649594/how-to-extract-ast-from-objective-c-code