lgi-devs / lgi

Dynamic Lua binding to GObject libraries using GObject-Introspection
MIT License
440 stars 70 forks source link

Handling foreign structure pointers returned from a function call #109

Open dannote opened 9 years ago

dannote commented 9 years ago

First of all, thank you for starting this awesome project!

I'm currently working on a service using WebKitGTK+ (It's WebKit1 interface) and I faced with the problem of JavaScriptCore GTK bindings which simply do not exist. As there is gir file describing two foreign structures - JavaScriptCore.GlobalContext and JavaScriptCore.Value - I decided to try to use LuaJIT's FFI to operate with this structures directly.

So, I'm calling web_view:get_main_frame():get_global_context() to get JSGlobalContextRef but the returning pointer value is slightly different from the value returned by direct FFI call ffi.C.webkit_web_frame_get_global_context(web_view:get_main_frame()._native). It looks like JavaScriptCore.GlobalContext is boxed but I'm unable to find a proper way to unbox it. I've already tried some GValue magic but it seems I'm doing it wrong. Here is what I always get:

cdata<struct OpaqueJSContext *>: 0x7fb0dc1df9b0
lgi.rec 0x7ffc9c0d1bf0:JavaScriptCore.GlobalContext

What is the recommended way to handle foreign pointers in this case?

pavouk commented 9 years ago

lgi normally uses _native to unbox both objects and records. Does web_view:get_main_frame():get_global_context()._native work?

dannote commented 9 years ago

Yes, it does. But if then I pass this pointer to JSEvaluateScript, I get a segfault. I'm currently using ffi.C.webkit_web_frame_get_global_context(web_view:get_main_frame()._native) for workaround.

dannote commented 9 years ago

https://gist.github.com/dannote/ec83a263188e04f84a9b

RossComputerGuy commented 3 years ago

Wait, if I push a native GObjec pointert to _native in a table using the native Lua API then am I able to make my C code work with LGI?

psychon commented 3 years ago

@RossComputerGuy I'm not quite sure what you are asking. Perhaps https://github.com/pavouk/lgi/blob/master/docs/guide.md#8-interoperability-with-native-code already answers your question?