/* Find the first constructor */ static GIFunctionInfo * find_fundamental_constructor(JSContext *context, GIObjectInfo *info, jsid *constructor_name) { int i, n_methods; n_methods = g_object_info_get_n_methods(info); for (i = 0; i < n_methods; ++i) { GIFunctionInfo *func_info; GIFunctionInfoFlags flags; func_info = g_object_info_get_method(info, i); flags = g_function_info_get_flags(func_info); if ((flags & GI_FUNCTION_IS_CONSTRUCTOR) != 0) { const char *name; name = g_base_info_get_name((GIBaseInfo *) func_info); *constructor_name = gjs_intern_string_to_id(context, name); return func_info; } g_base_info_unref((GIBaseInfo *) func_info); } return NULL; }
static PyObject * _get_methods (PyGIBaseInfo *self, GIInfoType info_type) { gssize n_infos; PyObject *infos; gssize i; switch (info_type) { case GI_INFO_TYPE_STRUCT: n_infos = g_struct_info_get_n_methods ( (GIStructInfo *) self->info); break; case GI_INFO_TYPE_OBJECT: n_infos = g_object_info_get_n_methods ( (GIObjectInfo *) self->info); break; default: g_assert_not_reached(); } infos = PyTuple_New (n_infos); if (infos == NULL) { return NULL; } for (i = 0; i < n_infos; i++) { GIBaseInfo *info; PyObject *py_info; switch (info_type) { case GI_INFO_TYPE_STRUCT: info = (GIBaseInfo *) g_struct_info_get_method ( (GIStructInfo *) self->info, i); break; case GI_INFO_TYPE_OBJECT: info = (GIBaseInfo *) g_object_info_get_method ( (GIObjectInfo *) self->info, i); break; default: g_assert_not_reached(); } g_assert (info != NULL); py_info = _pygi_info_new (info); g_base_info_unref (info); if (py_info == NULL) { Py_CLEAR (infos); break; } PyTuple_SET_ITEM (infos, i, py_info); } return infos; }
/* * Handle setting up the prototype and constructor for a GObject type * Namespace.Type will be the constructor and Namespace.Type.prototype is * the prototype object. Namespace.Type.type will be the GType. */ static void seed_gi_importer_handle_object(JSContextRef ctx, JSObjectRef namespace_ref, GIObjectInfo* info, JSValueRef* exception) { GType type; JSClassRef class_ref; type = g_registered_type_info_get_g_type((GIRegisteredTypeInfo*) info); if (type != 0) { GIFunctionInfo* finfo; GIFunctionInfoFlags flags; JSObjectRef constructor_ref; guint i, n_methods; class_ref = seed_gobject_get_class_for_gtype(ctx, type); constructor_ref = JSObjectMake(ctx, gobject_constructor_class, (gpointer) type); seed_object_set_property(ctx, constructor_ref, "type", seed_value_from_long(ctx, type, exception)); n_methods = g_object_info_get_n_methods(info); for (i = 0; i < n_methods; i++) { finfo = g_object_info_get_method(info, i); flags = g_function_info_get_flags(finfo); if (flags & GI_FUNCTION_IS_CONSTRUCTOR) { JSObjectRef constructor = JSObjectMake(ctx, gobject_named_constructor_class, finfo); const gchar* fname = g_base_info_get_name((GIBaseInfo*) finfo); if (g_strrstr(fname, "new_") == fname) { // To be compatible with gjs, we need to have a method with // new_, too. seed_object_set_property(ctx, constructor_ref, fname, constructor); fname += 4; } else if (!g_strcmp0(fname, "new")) { // To be compatible with gjs, we need to have new as // function, too. seed_object_set_property(ctx, constructor_ref, fname, constructor); fname = "c_new"; } seed_object_set_property(ctx, constructor_ref, fname, constructor); } else if (!(flags & GI_FUNCTION_IS_METHOD)) { seed_gobject_define_property_from_function_info(ctx, finfo, constructor_ref, FALSE); } else { g_base_info_unref((GIBaseInfo*) finfo); } } seed_object_set_property(ctx, namespace_ref, g_base_info_get_name((GIBaseInfo*) info), constructor_ref); seed_object_set_property(ctx, constructor_ref, "prototype", seed_gobject_get_prototype_for_gtype(type)); } }