static JSBool gjs_define_static_methods(JSContext *context, JSObject *constructor, GType gtype, GIStructInfo *boxed_info) { int i; int n_methods; n_methods = g_struct_info_get_n_methods(boxed_info); for (i = 0; i < n_methods; i++) { GIFunctionInfo *meth_info; GIFunctionInfoFlags flags; meth_info = g_struct_info_get_method (boxed_info, i); flags = g_function_info_get_flags (meth_info); /* Anything that isn't a method we put on the prototype of the * constructor. This includes <constructor> introspection * methods, as well as the forthcoming "static methods" * support. We may want to change this to use * GI_FUNCTION_IS_CONSTRUCTOR and GI_FUNCTION_IS_STATIC or the * like in the near future. */ if (!(flags & GI_FUNCTION_IS_METHOD)) { gjs_define_function(context, constructor, gtype, (GICallableInfo *)meth_info); } g_base_info_unref((GIBaseInfo*) meth_info); } return JS_TRUE; }
/* * Set up prototype and constructor for structs. Same semantics as objects * except * for the type. */ static void seed_gi_importer_handle_struct(JSContextRef ctx, JSObjectRef namespace_ref, GIStructInfo* info, JSValueRef* exception) { JSObjectRef struct_ref; JSObjectRef proto; gint i, n_methods; GIFunctionInfo* finfo; struct_ref = JSObjectMake(ctx, seed_struct_constructor_class, info); g_base_info_ref(info); n_methods = g_struct_info_get_n_methods(info); for (i = 0; i < n_methods; i++) { GIFunctionInfoFlags flags; finfo = g_struct_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, struct_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, struct_ref, fname, constructor); fname = "c_new"; } seed_object_set_property(ctx, struct_ref, fname, constructor); } else if (flags & GI_FUNCTION_IS_METHOD) g_base_info_unref((GIBaseInfo*) finfo); else seed_gobject_define_property_from_function_info(ctx, finfo, struct_ref, FALSE); } proto = seed_struct_prototype(ctx, (GIBaseInfo*) info); seed_object_set_property(ctx, struct_ref, "prototype", proto); seed_object_set_property(ctx, namespace_ref, g_base_info_get_name((GIBaseInfo*) info), struct_ref); }
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; }
static void boxed_fill_prototype_info(Boxed *priv) { int i, n_methods; int first_constructor = -1; priv->gtype = g_registered_type_info_get_g_type( (GIRegisteredTypeInfo*) priv->info); priv->zero_args_constructor = -1; priv->default_constructor = -1; if (priv->gtype != G_TYPE_NONE) { /* If the structure is registered as a boxed, we can create a new instance by * looking for a zero-args constructor and calling it; constructors don't * really make sense for non-boxed types, since there is no memory management * for the return value. */ n_methods = g_struct_info_get_n_methods(priv->info); for (i = 0; i < n_methods; ++i) { GIFunctionInfo *func_info; GIFunctionInfoFlags flags; func_info = g_struct_info_get_method(priv->info, i); flags = g_function_info_get_flags(func_info); if ((flags & GI_FUNCTION_IS_CONSTRUCTOR) != 0) { if (first_constructor < 0) first_constructor = i; if (priv->zero_args_constructor < 0 && g_callable_info_get_n_args((GICallableInfo*) func_info) == 0) priv->zero_args_constructor = i; if (priv->default_constructor < 0 && strcmp(g_base_info_get_name ((GIBaseInfo*) func_info), "new") == 0) priv->default_constructor = i; } g_base_info_unref((GIBaseInfo*) func_info); } if (priv->default_constructor < 0) priv->default_constructor = priv->zero_args_constructor; if (priv->default_constructor < 0) priv->default_constructor = first_constructor; } }