[Webkit-unassigned] [Bug 195574] [GLib] Returning G_TYPE_OBJECT from a method does not work

bugzilla-daemon at webkit.org bugzilla-daemon at webkit.org
Wed Mar 13 05:57:41 PDT 2019


https://bugs.webkit.org/show_bug.cgi?id=195574

--- Comment #3 from Carlos Garcia Campos <cgarcia at igalia.com> ---
Comment on attachment 364290
  --> https://bugs.webkit.org/attachment.cgi?id=364290
jscobj.c: Example code to reproduce the issue

>/*
> * Build with:
> *    gcc -o jscobj jscobj.c $(pkg-config javascriptcoregtk-4.0 gio-2.0 --cflags --libs)
> */
>
>#include <gio/gio.h>
>#include <jsc/jsc.h>
>
>static GFile *js_GFile_constructor(const char *path) {
>  GFile* ret =  g_file_new_for_path(path);
>  g_object_ref(ret);
>  return ret;
>}

You should not ref again here, the object ownership is transferred to the JSCClass.

>static GFile *js_GFile_id(GFile* self) {
>  return self;
>}

And here you need to g_object_ref(), the object will be unrefed after being converted to a JSCValue.

>static void setup_jsc_context(JSCContext *ctx) {
>  JSCClass *klass =
>      jsc_context_register_class(ctx, "GFile" /* name */, NULL /* parent */,
>                                 NULL /* vtable */, NULL /* destroy_notify */);

You should pass g_object_unref as destroy_notify, since the instance is owned by the JSCClass.

>  g_autoptr(JSCValue) ctor = jsc_class_add_constructor(
>      klass, NULL /* name: same as class */, G_CALLBACK(js_GFile_constructor),
>      NULL /* user_data */, NULL /* user_data destroy_notify */,
>      G_TYPE_OBJECT /* return type */, 1 /* n_params */, G_TYPE_STRING);
>  jsc_class_add_method(klass, "getPath" /* name */, G_CALLBACK(g_file_get_path),
>                       NULL /* user_data */,
>                       NULL /* user_data destroy_notify */,
>                       G_TYPE_STRING /* return type */, 0 /* n_params */);
>  jsc_class_add_method(klass, "id" /* name */, G_CALLBACK(js_GFile_id),
>                       NULL /* user_data */,
>                       NULL /* user_data destroy_notify */,
>                       G_TYPE_OBJECT /* return type */, 0 /* n_params */);
>
>  jsc_context_set_value(ctx, jsc_class_get_name(klass) /* name */,
>                        ctor /* value */);
>}
>
>int main(int argc, char *argv[]) {
>  g_autoptr(JSCContext) ctx = jsc_context_new();
>  setup_jsc_context(ctx);
>
>  g_autoptr(JSCValue) result =
>      jsc_context_evaluate(ctx, "(new GFile('.')).getPath()", -1);
>  g_autofree char *result_string = jsc_value_to_string(result);
>  g_print("Path: %s\n", result_string);
>
>  result =
>      jsc_context_evaluate(ctx, "var file = new GFile('.'); var file_copy = file.id(); file.getPath()", -1);
>  g_assert(jsc_value_is_string(result));
>
>  result_string = jsc_value_to_string(result);
>  g_print("Path after copy: %s\n", result_string);
>
>  return 0;
>}

-- 
You are receiving this mail because:
You are the assignee for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-unassigned/attachments/20190313/ea59bce1/attachment.html>


More information about the webkit-unassigned mailing list