Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with Proc & GObject::Callback #61

Open
GeopJr opened this issue Aug 12, 2022 · 5 comments
Open

Issues with Proc & GObject::Callback #61

GeopJr opened this issue Aug 12, 2022 · 5 comments
Labels
bug Something isn't working

Comments

@GeopJr
Copy link
Contributor

GeopJr commented Aug 12, 2022

Hi, I'm having some issues with JavaScriptCore bindings (https://github.com/GeopJr/JavaScriptCore.cr):

  • Proc

For example, JavaScriptCore::ClassVTable#get_property= accepts JavaScriptCore::ClassGetPropertyFunction (which is an alias for Proc(JavaScriptCore::Class, JavaScriptCore::Context, Pointer(Void)?, JavaScriptCore::Value)) generates:

def get_property=(value : JavaScriptCore::ClassGetPropertyFunction)
      _var = (to_unsafe + 0).as(Pointer(Void*))
      _var.copy_from(value.to_unsafe, sizeof(LibJavaScriptCore::ClassVTable))
      value
end

but Proc#to_unsafe is not a method.

  • GObject::Callback

Almost all callbacks are of type GObject::Callback which I'm unsure how to work with - even after some manual editing, I couldn't get past Error: argument 'getter' of 'LibJavaScriptCore#jsc_class_add_property' must be Pointer(Void) or invalid memory access.

Example code for the first issue:

require "javascriptcore"

JavaScriptCore::ClassVTable.new(
    get_property: JavaScriptCore::ClassGetPropertyFunction.new { |x, y, _| JavaScriptCore::Value.new_null(y) }
)

I understand if this is out of the scope of gi-crystal and I'll probably end up writing bindings manually if closed (as the api is not that big) 😅

Also surprised getter and setter work as method parameters

@hugopl
Copy link
Owner

hugopl commented Aug 12, 2022

Does JSCore have GIR information? GIR information for non-gobject based libraries are usually not good, maybe would be easier to try to use GJS instead?

@GeopJr
Copy link
Contributor Author

GeopJr commented Aug 12, 2022

It does have gir information (it's part of webkit2gtk / gobject based afaik), here's a copy of it https://gist.github.com/GeopJr/aae89878ad6b83f3c6233191a109e0eb

The shard works fine as per its readme, just some methods generated by gi-crystal dont. The crystal docs also turned out alright https://javascriptcore.geopjr.dev/JavaScriptCore/Context.html

@hugopl hugopl added the bug Something isn't working label Sep 29, 2022
@hugopl
Copy link
Owner

hugopl commented Sep 29, 2022

I didn't check this issue in detail yet, but maybe cccab76 can help with it, it fixes callback signature generation.

@GeopJr
Copy link
Contributor Author

GeopJr commented Sep 30, 2022

Unfortunately both issues still exist from what I've gathered

@hugopl
Copy link
Owner

hugopl commented Feb 8, 2023

When passing a Proc to C libraries we usually need to put the Proc inside a Box, then store this Box object in the GICrystal::ClosureManager to avoid GC to delete it. Usually the problem is to know when to remove the Box from ClosureManager to let GC take care of the memory.

With the current implementation of signals we have a similar problem... if you connect an object to some signal and never disconnect this signal the object will never be collected by GC because there will always be a reference to it in the ClosureManager.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants