[gjs] repo: Warn when importing a namespace with >1 version
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs] repo: Warn when importing a namespace with >1 version
- Date: Fri, 7 Apr 2017 15:47:09 +0000 (UTC)
commit 0154c9d5b87f789b5f8810cf12a77fa15fbbb41e
Author: Colin Walters <walters verbum org>
Date: Tue Dec 4 14:51:05 2012 -0500
repo: Warn when importing a namespace with >1 version
In introspection, libraries are a pair of (namespace, version). But
it's possible to request "the latest" version of a namespace, and this
is in fact what many projects do.
The problem is that for say GTK+ which has 2.0, 3.0, and is just releasing
4.0, most people's scripts imported gi.Gtk and expected to receive 3.0, and
will explode when installing 4.0.
Let's start warning about unversioned imports when there are multiple
versions available, and get people to do:
imports.gi.versions.Gtk = '3.0';
const Gtk = imports.gi.Gtk;
(Original patch by Colin Walters; revised by Philip Chimento.)
https://bugzilla.gnome.org/show_bug.cgi?id=689654
examples/gettext.js | 2 +-
examples/gtk.js | 1 +
examples/webkit.js | 2 +
gi/repo.cpp | 33 ++++++++++++++++++++++++-------
installed-tests/js/testGObjectClass.js | 1 +
5 files changed, 30 insertions(+), 9 deletions(-)
---
diff --git a/examples/gettext.js b/examples/gettext.js
index 44377fc..3c67be7 100644
--- a/examples/gettext.js
+++ b/examples/gettext.js
@@ -1,4 +1,4 @@
-
+imports.gi.versions.Gtk = '3.0';
const Gettext = imports.gettext;
const Gtk = imports.gi.Gtk;
diff --git a/examples/gtk.js b/examples/gtk.js
index 5a2195d..2357c40 100644
--- a/examples/gtk.js
+++ b/examples/gtk.js
@@ -1,3 +1,4 @@
+imports.gi.versions.Gtk = '3.0';
const Gtk = imports.gi.Gtk;
// This is a callback function. The data arguments are ignored
diff --git a/examples/webkit.js b/examples/webkit.js
index 7e042c2..04e61d7 100644
--- a/examples/webkit.js
+++ b/examples/webkit.js
@@ -1,3 +1,5 @@
+imports.gi.versions.Gtk = '3.0';
+imports.gi.versions.WebKit2 = '4.0';
const Gtk = imports.gi.Gtk;
const WebKit = imports.gi.WebKit2;
diff --git a/gi/repo.cpp b/gi/repo.cpp
index 4e780fe..1fc12cf 100644
--- a/gi/repo.cpp
+++ b/gi/repo.cpp
@@ -98,6 +98,17 @@ resolve_namespace_object(JSContext *context,
return false;
repo = g_irepository_get_default();
+ GList *versions = g_irepository_enumerate_versions(repo, ns_name);
+ unsigned nversions = g_list_length(versions);
+ if (nversions != 1 && !version &&
+ !g_irepository_is_registered(repo, ns_name, NULL)) {
+ GjsAutoChar warn_text = g_strdup_printf("Requiring %s but it has %u "
+ "versions available; use "
+ "imports.gi.versions to pick one",
+ ns_name, nversions);
+ JS_ReportWarning(context, warn_text);
+ }
+ g_list_free_full(versions, g_free);
error = NULL;
g_irepository_require(repo, ns_name, version, (GIRepositoryLoadFlags) 0, &error);
@@ -297,19 +308,25 @@ repo_new(JSContext *context)
gjs_object_define_property(context, repo, GJS_STRING_GI_VERSIONS,
versions, JSPROP_PERMANENT);
+ /* GLib/GObject/Gio are fixed at 2.0, since we depend on them
+ * internally.
+ */
+ JS::RootedString two_point_oh(context, JS_NewStringCopyZ(context, "2.0"));
+ if (!JS_DefineProperty(context, versions, "GLib", two_point_oh,
+ JSPROP_PERMANENT))
+ return nullptr;
+ if (!JS_DefineProperty(context, versions, "GObject", two_point_oh,
+ JSPROP_PERMANENT))
+ return nullptr;
+ if (!JS_DefineProperty(context, versions, "Gio", two_point_oh,
+ JSPROP_PERMANENT))
+ return nullptr;
+
JS::RootedObject private_ns(context, JS_NewObject(context, NULL, global));
gjs_object_define_property(context, repo,
GJS_STRING_PRIVATE_NS_MARKER, private_ns,
JSPROP_PERMANENT);
- /* FIXME - hack to make namespaces load, since
- * gobject-introspection does not yet search a path properly.
- */
- {
- JS::RootedValue value(context);
- JS_GetProperty(context, repo, "GLib", &value);
- }
-
return repo;
}
diff --git a/installed-tests/js/testGObjectClass.js b/installed-tests/js/testGObjectClass.js
index c967550..0d81b96 100644
--- a/installed-tests/js/testGObjectClass.js
+++ b/installed-tests/js/testGObjectClass.js
@@ -1,4 +1,5 @@
// -*- mode: js; indent-tabs-mode: nil -*-
+imports.gi.versions.Gtk = '3.0';
const Lang = imports.lang;
const GObject = imports.gi.GObject;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]