[gjs] GError: pretend that GBoxed wrapping GError is a JS GError



commit aa1db67a815b3e9faaeb1392a232c130308feb5c
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Sun Dec 2 20:16:47 2012 +0100

    GError: pretend that GBoxed wrapping GError is a JS GError
    
    gjs_error_from_gerror() will return a JS object with a plain Boxed JS class
    in case it doesn't find the errordomain metadata. Later, gjs_typecheck_gerror()
    would choke on those, so they would be effectively useless.
    Instead, recognize GError wrappers with a boxed JS class and delegate
    typecheck and structure access.
    Credits to Colin Walters <walters verbum org> for the test.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=689482

 Makefile-test.am               |   22 ++++++++++++++++++++--
 gi/gerror.c                    |    9 +++++++++
 test/js/testEverythingBasic.js |   10 ++++++++++
 3 files changed, 39 insertions(+), 2 deletions(-)
---
diff --git a/Makefile-test.am b/Makefile-test.am
index 2bac21a..59b5370 100644
--- a/Makefile-test.am
+++ b/Makefile-test.am
@@ -69,7 +69,7 @@ EXTRA_DIST +=			\
 
 # noinst_ always builds a static library
 if ENABLE_TESTS
-testlib_LTLIBRARIES = libregress.la libgimarshallingtests.la
+testlib_LTLIBRARIES = libregress.la libwarnlib.la libgimarshallingtests.la
 testlibdir = $(prefix)/unused
 install-testlibLTLIBRARIES: # prevent it from being installed
 endif
@@ -77,6 +77,9 @@ endif
 nodist_libregress_la_SOURCES = $(GI_DATADIR)/tests/regress.c $(GI_DATADIR)/tests/regress.h
 libregress_la_CFLAGS = $(GJS_CFLAGS) $(GJS_CAIRO_CFLAGS)
 libregress_la_LDFLAGS = -avoid-version $(GJS_LIBS) $(GJS_CAIRO_LIBS)
+nodist_libwarnlib_la_SOURCES = $(GI_DATADIR)/tests/warnlib.c $(GI_DATADIR)/tests/warnlib.h
+libwarnlib_la_CFLAGS = $(GJS_CFLAGS)
+libwarnlib_la_LDFLAGS = -avoid-version $(GJS_LIBS)
 nodist_libgimarshallingtests_la_SOURCES = $(GI_DATADIR)/tests/gimarshallingtests.c $(GI_DATADIR)/tests/gimarshallingtests.h
 libgimarshallingtests_la_CFLAGS = $(GJS_CFLAGS)
 libgimarshallingtests_la_LDFLAGS = -avoid-version $(GJS_LIBS)
@@ -95,6 +98,21 @@ Regress-1.0.typelib: Regress-1.0.gir Makefile
 
 CLEANFILES += Regress-1.0.gir Regress-1.0.typelib
 
+# g-i doesn't ship these as shared libraries anymore; we build them here
+WarnLib-1.0.gir: libwarnlib.la Makefile
+	$(AM_V_GEN) g-ir-scanner --include=cairo-1.0 --include=Gio-2.0 \
+	--namespace=WarnLib --nsversion=1.0 \
+	--identifier-prefix=WarnLib_ \
+	--symbol-prefix=warnlib_ \
+	--library=libwarnlib.la \
+	--libtool="$(top_builddir)/libtool" \
+	--output $@ \
+	$(nodist_libwarnlib_la_SOURCES)
+WarnLib-1.0.typelib: WarnLib-1.0.gir Makefile
+	$(AM_V_GEN) g-ir-compiler $< -o $@
+
+CLEANFILES += WarnLib-1.0.gir WarnLib-1.0.typelib
+
 GIMarshallingTests-1.0.gir: libgimarshallingtests.la Makefile
 	$(AM_V_GEN) LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):$(FIREFOX_JS_LIBDIR)" g-ir-scanner --include=Gio-2.0 \
 	--namespace=GIMarshallingTests --nsversion=1.0 --symbol-prefix=gi_marshalling_tests \
@@ -121,7 +139,7 @@ TESTS_ENVIRONMENT =							\
 	LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):$(FIREFOX_JS_LIBDIR)"	\
 	G_FILENAME_ENCODING=latin1	# ensure filenames are not utf8
 
-tests_dependencies = $(gjsnative_LTLIBRARIES) ${TEST_PROGS} Regress-1.0.typelib GIMarshallingTests-1.0.typelib
+tests_dependencies = $(gjsnative_LTLIBRARIES) ${TEST_PROGS} Regress-1.0.typelib WarnLib-1.0.typelib GIMarshallingTests-1.0.typelib
 
 test: $(tests_dependencies)
 	@test -z "${TEST_PROGS}" || ${GTESTER} --verbose ${TEST_PROGS} ${TEST_PROGS_OPTIONS}
diff --git a/gi/gerror.c b/gi/gerror.c
index ff3a369..2f40d7d 100644
--- a/gi/gerror.c
+++ b/gi/gerror.c
@@ -617,6 +617,12 @@ gjs_gerror_from_error(JSContext    *context,
     if (obj == NULL)
         return NULL;
 
+    /* If this is a plain GBoxed (i.e. a GError without metadata),
+       delegate marshalling.
+    */
+    if (gjs_typecheck_boxed (context, obj, NULL, G_TYPE_ERROR, JS_FALSE))
+        return gjs_c_struct_from_boxed (context, obj);
+
     priv = priv_from_js(context, obj);
 
     if (priv == NULL)
@@ -638,5 +644,8 @@ gjs_typecheck_gerror (JSContext *context,
                       JSObject  *obj,
                       JSBool     throw)
 {
+    if (gjs_typecheck_boxed (context, obj, NULL, G_TYPE_ERROR, JS_FALSE))
+        return TRUE;
+
     return do_base_typecheck(context, obj, throw);
 }
diff --git a/test/js/testEverythingBasic.js b/test/js/testEverythingBasic.js
index 99c35b9..4581966 100644
--- a/test/js/testEverythingBasic.js
+++ b/test/js/testEverythingBasic.js
@@ -1,6 +1,7 @@
 // application/javascript;version=1.8
 // This used to be called "Everything"
 const Everything = imports.gi.Regress;
+const WarnLib = imports.gi.WarnLib;
 if (!('assertEquals' in this)) { /* allow running this test standalone */
     imports.lang.copyPublicProperties(imports.jsUnit, this);
     gjstestRun = function() { return imports.jsUnit.gjstestRun(window); };
@@ -548,6 +549,15 @@ function testGError() {
 	assertEquals(Gio.IOErrorEnum.PERMISSION_DENIED, e.code);
 	assertEquals('regression test owned error', e.message);
     });
+
+    // Calling matches() on an unpaired error used to assert:
+    // https://bugzilla.gnome.org/show_bug.cgi?id=689482
+    try {
+	WarnLib.throw_unpaired();
+	assertTrue(false);
+    } catch (e) {
+	assertFalse(e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_FOUND));
+    }
 }
 
 function testWrongClassGObject() {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]