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
|
From: =?utf-8?b?Ik1hcmNvIFRyZXZpc2FuIChUcmV2acOxbyki?= <mail@3v1n0.net>
Date: Thu, 11 Sep 2025 23:51:56 +0200
Subject: overrides/Gio: Add wrappers for platform-specific Gio functions
GLib will not expose anymore platform specific functions in Gio
namespace as it used to do when GI Repository 1.0 was used, in order to
keep retro-compatibility in gjs applications, generate wrappers for
Gio platform-specific definitions that we used to provide inside the
main Gio object, but warn the users of these APIs that they should
migrate to GioUnix or GioWin32 instead.
---
installed-tests/js/testGio.js | 88 +++++++++++++++++++++++++++++++++++++++++++
modules/core/overrides/Gio.js | 35 +++++++++++++++++
2 files changed, 123 insertions(+)
diff --git a/installed-tests/js/testGio.js b/installed-tests/js/testGio.js
index 908f906..e74059a 100644
--- a/installed-tests/js/testGio.js
+++ b/installed-tests/js/testGio.js
@@ -4,6 +4,11 @@
const {GLib, Gio, GObject} = imports.gi;
+let GioUnix;
+try {
+ GioUnix = imports.gi.GioUnix;
+} catch {}
+
const Foo = GObject.registerClass({
Properties: {
boolval: GObject.ParamSpec.boolean('boolval', '', '',
@@ -390,6 +395,89 @@ describe('Gio.FileEnumerator overrides', function () {
});
});
+describe('Gio.DesktopAppInfo fallback', function () {
+ let writerFunc;
+ const requiredVersion =
+ GLib.MAJOR_VERSION > 2 ||
+ (GLib.MAJOR_VERSION === 2 && GLib.MINOR_VERSION >= 86);
+ let keyFile;
+ const desktopFileContent = `[Desktop Entry]
+Version=1.0
+Type=Application
+Name=Some Application
+Exec=${GLib.find_program_in_path('sh')}
+`;
+ beforeAll(function () {
+ // Set up log writer for tests to override
+ writerFunc = jasmine.createSpy('parsed writer func');
+ const writerFuncWrapper = jasmine.createSpy('log writer func', (level, fields) => {
+ const decoder = new TextDecoder('utf-8');
+ const domain = decoder.decode(fields?.GLIB_DOMAIN);
+ const message = `${decoder.decode(fields?.MESSAGE)}`;
+ if (level < GLib.LogLevelFlags.LEVEL_WARNING) {
+ level |= GLib.LogLevelFlags.FLAG_RECURSION;
+ GLib.log_default_handler(domain, level, `${message}\n`, null);
+ }
+ writerFunc(domain, level, message);
+ return GLib.LogWriterOutput.HANDLED;
+ });
+ writerFuncWrapper.and.callThrough();
+ GLib.log_set_writer_func(writerFuncWrapper);
+
+ keyFile = new GLib.KeyFile();
+ keyFile.load_from_data(desktopFileContent, desktopFileContent.length,
+ GLib.KeyFileFlags.NONE);
+ });
+
+ afterAll(function () {
+ GLib.log_set_writer_default();
+ });
+
+ beforeEach(function () {
+ if (!GioUnix)
+ pending('Not supported platform');
+
+ writerFunc.calls.reset();
+ });
+
+ it('can be created using GioUnix', function () {
+ expect(GioUnix.DesktopAppInfo.new_from_keyfile(keyFile)).not.toBeNull();
+ expect(writerFunc).not.toHaveBeenCalled();
+ });
+
+ it('can be created using Gio wrapper', function () {
+ expect(Gio.DesktopAppInfo.new_from_keyfile(keyFile)).not.toBeNull();
+ expect(writerFunc).toHaveBeenCalledWith('Cjs-Console',
+ GLib.LogLevelFlags.LEVEL_WARNING,
+ 'Gio.DesktopAppInfo is deprecated, please use GioUnix.DesktopAppInfo instead');
+
+ writerFunc.calls.reset();
+ expect(Gio.DesktopAppInfo.new_from_keyfile(keyFile)).not.toBeNull();
+ expect(writerFunc).not.toHaveBeenCalled();
+ });
+
+ describe('provides platform-independent functions', function () {
+ [Gio, GioUnix].forEach(ns => it(`when created from ${ns.__name__}`, function () {
+ if (!requiredVersion)
+ pending('Installed Gio is not new enough for this test');
+
+ const appInfo = ns.DesktopAppInfo.new_from_keyfile(keyFile);
+ expect(appInfo.get_name()).toBe('Some Application');
+ }));
+ });
+
+ describe('provides unix-only functions', function () {
+ [Gio, GioUnix].forEach(ns => it(`when created from ${ns.__name__}`, function () {
+ if (!requiredVersion)
+ pending('Installed Gio is not new enough for this test');
+
+ const appInfo = ns.DesktopAppInfo.new_from_keyfile(keyFile);
+ expect(appInfo.has_key('Name')).toBeTrue();
+ expect(appInfo.get_string('Name')).toBe('Some Application');
+ }));
+ });
+});
+
describe('Non-introspectable file attribute overrides', function () {
let numExpectedWarnings, file, info;
const flags = [Gio.FileQueryInfoFlags.NONE, null];
diff --git a/modules/core/overrides/Gio.js b/modules/core/overrides/Gio.js
index 836d14a..6bcc20f 100644
--- a/modules/core/overrides/Gio.js
+++ b/modules/core/overrides/Gio.js
@@ -479,9 +479,44 @@ function _warnNotIntrospectable(funcName, replacement) {
function _init() {
Gio = this;
+ let GioPlatform = {};
Gio.Application.prototype.runAsync = GLib.MainLoop.prototype.runAsync;
+ // Redefine Gio functions with platform-specific implementations to be
+ // backward compatible with gi-repository 1.0, however when possible we
+ // notify a deprecation warning, to ensure that the surrounding code is
+ // updated.
+ try {
+ GioPlatform = imports.gi.GioUnix;
+ } catch {
+ try {
+ GioPlatform = imports.gi.GioWin32;
+ } catch {}
+ }
+
+ Object.entries(Object.getOwnPropertyDescriptors(GioPlatform)).forEach(([prop, desc]) => {
+ if (Object.hasOwn(Gio, prop)) {
+ console.debug(`Gio already contains property ${prop}`);
+ Gio[prop] = GioPlatform[prop];
+ return;
+ }
+
+ const newDesc = {
+ enumerable: true,
+ configurable: false,
+ get() {
+ if (!newDesc._deprecationWarningDone) {
+ console.warn(`Gio.${prop} is deprecated, please use ` +
+ `${GioPlatform.__name__}.${prop} instead`);
+ newDesc._deprecationWarningDone = true;
+ }
+ return desc.get?.() ?? desc.value;
+ },
+ };
+ Object.defineProperty(Gio, prop, newDesc);
+ });
+
Gio.DBus = {
// Namespace some functions
get: Gio.bus_get,
|