summaryrefslogtreecommitdiffstats
path: root/bindings/perl/gobject_handling.c
blob: 6689029cc600dafe0a16ec1dc8158e9df7f2c9d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
 * Lasso - A free implementation of the Liberty Alliance specifications.
 *
 * Copyright (C) 2004-2007 Entr'ouvert
 * http://lasso.entrouvert.org
 *
 * Authors: See AUTHORS file in top-level directory.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <perl.h>
#include <glib.h>
#include <glib-object.h>
#include <lasso/xml/xml.h>

/*
 * Manipulate a pointer to indicate that an SV is undead.
 * Relies on SV pointers being word-aligned.
 */
#define IS_UNDEAD(x) (PTR2UV(x) & 1)
#define MAKE_UNDEAD(x) INT2PTR(void*, PTR2UV(x) | 1)
#define REVIVE_UNDEAD(x) INT2PTR(void*, PTR2UV(x) & ~1)

/* this code is copied / adapted from libglib-perl */
GHashTable *types_by_types;
GHashTable *types_by_package;
GQuark wrapper_quark;

extern int lasso_init();

static void
init_perl_lasso() {
	types_by_types = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
	types_by_package = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
	wrapper_quark = g_quark_from_static_string("PerlLasso::wrapper");
	lasso_init();
}

static const char *
gperl_object_package_from_type (GType gtype)
{
	gchar* package;
	const gchar* type_name;

	if (!g_type_is_a (gtype, G_TYPE_OBJECT) &&
		!g_type_is_a (gtype, G_TYPE_INTERFACE))
		return NULL;


	package = g_hash_table_lookup(types_by_types, (gconstpointer)gtype);
	if (package)
		return package;

	type_name = g_type_name(gtype);
	if (! type_name)
		return NULL;

	if (strncmp(type_name, "Lasso", 5) != 0)
		return NULL;

	package = g_strconcat("Lasso::", &type_name[5], NULL);
	g_hash_table_insert(types_by_types, (gpointer)gtype, (gpointer)package);
	g_hash_table_insert(types_by_package, g_strdup(package), (gpointer)gtype);

	return package;
}

static void
gobject_destroy_wrapper (SV *obj)
{
#ifdef NOISY
	warn ("gobject_destroy_wrapper (%p)[%d]\n", obj,
			SvREFCNT ((SV*)REVIVE_UNDEAD(obj)));
#endif
	obj = REVIVE_UNDEAD(obj);
	sv_unmagic (obj, PERL_MAGIC_ext);

	/* we might want to optimize away the call to DESTROY here for non-perl classes. */
	SvREFCNT_dec (obj);
}

static HV *
gperl_object_stash_from_type (GType gtype)
{
	const char * package = gperl_object_package_from_type (gtype);
	if (package)
		return gv_stashpv (package, TRUE);
	else
		return NULL;
}

static void
update_wrapper (GObject *object, gpointer obj)
{
#ifdef NOISY
	warn("update_wrapper [%p] (%p)\n", object, obj); */
#endif
        g_object_steal_qdata (object, wrapper_quark);
        g_object_set_qdata_full (object,
                                 wrapper_quark,
                                 obj,
                                 (GDestroyNotify)gobject_destroy_wrapper);
}

static SV *
gperl_new_object (GObject * object,
                  gboolean own)
{
	SV *obj;
	SV *sv;

	/* take the easy way out if we can */
	if (!object) {
		return &PL_sv_undef;
	}

	if (!LASSO_IS_NODE (object))
		croak ("object %p is not really a LassoNode", object);

	/* fetch existing wrapper_data */
	obj = (SV *)g_object_get_qdata (object, wrapper_quark);

	if (!obj) {
		/* create the perl object */
		GType gtype = G_OBJECT_TYPE (object);

		HV *stash = gperl_object_stash_from_type (gtype);

		/* We should only get NULL for the stash here if gtype is
		 * neither a GObject nor GInterface.  We filtered out all
		 * non-GObject types a few lines back. */
		g_assert (stash != NULL);

		/*
		 * Create the "object", a hash.
		 *
		 * This does not need to be a HV, the only problem is finding
		 * out what to use, and HV is certainly the way to go for any
		 * built-in objects.
		 */

		/* this increases the combined object's refcount. */
		obj = (SV *)newHV ();
		/* attach magic */
		sv_magic (obj, 0, PERL_MAGIC_ext, (const char *)object, 0);

		/* The SV has a ref to the C object.  If we are to own this
		 * object, then any other references will be taken care of
		 * below in take_ownership */
		g_object_ref (object);

		/* create the wrapper to return, the _noinc decreases the
		 * combined refcount by one. */
		sv = newRV_noinc (obj);

		/* bless into the package */
		sv_bless (sv, stash);

		/* attach it to the gobject */
		update_wrapper (object, obj);
		/* printf("creating new wrapper for [%p] (%p)\n", object, obj); */

		/* the noinc is so that the SV (initially) exists only as long
		 * as the perl code needs it.  When the DESTROY gets called, we
		 * check and see if the SV is the only referer to the C object,
		 * and if so remove both.  Otherwise, the SV will become
		 * "undead," to be either revived or destroyed with the C
		 * object */

#ifdef NOISY
		warn ("gperl_new_object%d %s(%p)[%d] => %s (%p) (NEW)\n", own,
				G_OBJECT_TYPE_NAME (object), object, object->ref_count,
				gperl_object_package_from_type (G_OBJECT_TYPE (object)),
				SvRV (sv));
#endif
	} else {
		/* create the wrapper to return, increases the combined
		 * refcount by one. */

		/* if the SV is undead, revive it */
		if (IS_UNDEAD(obj)) {
			g_object_ref (object);
			obj = REVIVE_UNDEAD(obj);
			update_wrapper (object, obj);
			sv = newRV_noinc (obj);
			/* printf("reviving undead wrapper for [%p] (%p)\n", object, obj); */
		} else {
			/* printf("reusing previous wrapper for %p\n", obj); */
			sv = newRV_inc (obj);
		}
	}

#ifdef NOISY
	warn ("gperl_new_object%d %s(%p)[%d] => %s (%p)[%d] (PRE-OWN)\n", own,
			G_OBJECT_TYPE_NAME (object), object, object->ref_count,
			gperl_object_package_from_type (G_OBJECT_TYPE (object)),
			SvRV (sv), SvREFCNT (SvRV (sv)));
#endif
	if (own)
		g_object_unref(object);

	return sv;
}

static GObject *
gperl_get_object (SV * sv)
{
	MAGIC *mg;

	if (!sv || !SvOK(sv) || !SvROK (sv) || !(mg = mg_find (SvRV (sv), PERL_MAGIC_ext)))
		return NULL;
	if (! mg->mg_ptr)
		return NULL;
	if (! G_IS_OBJECT(mg->mg_ptr))
		return NULL;
	return (GObject *) mg->mg_ptr;
}

static void
gperl_lasso_error(int error)
{
	dTHX;
	if (error != 0) {
		HV *hv;
		SV *sv;

		const char *desc = lasso_strerror(error);
		hv = newHV();
		(void)hv_store(hv, "code", 4, newSViv(error), 0);
		(void)hv_store(hv, "message", 7, newSVpv(desc, 0), 0);
		sv = sv_bless(newRV_noinc((SV*)hv), gv_stashpv("Lasso::Error", TRUE));
		sv_setsv(ERRSV, sv);
		Perl_croak (aTHX_ Nullch);
	}
}

/*
 * check_gobject:
 * @object: a #GObject object
 * @gtype: a #GType
 *
 * Check that a given pointer is really a pointer to a GObject of certain type.
 * Return value: TRUE or FALSE.
 */
static void
check_gobject(GObject *object, GType type) {
	if (! G_IS_OBJECT(object) || ! g_type_is_a(G_OBJECT_TYPE(object), type)) {
		gperl_lasso_error(LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ);
	}
}