[gjs: 1/2] Gio: Throws errors when trying to set/read readonly/writeonly properties



commit 56d1db2f759006387ae481fef9d570a48cb2f6dd
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Mon Jun 17 18:01:53 2019 +0200

    Gio: Throws errors when trying to set/read readonly/writeonly properties
    
    Currently when setting a read-only property or reading a write-only one no
    error is prompted on the JS side, while the values might be changed locally.
    
    Avoid this to happen by throwing errors in case an invalid operation is
    requested by checking the properties flags and respecting them.

 installed-tests/js/testGDBus.js | 11 +++++++----
 modules/overrides/Gio.js        | 24 ++++++++++++++++++++----
 2 files changed, 27 insertions(+), 8 deletions(-)
---
diff --git a/installed-tests/js/testGDBus.js b/installed-tests/js/testGDBus.js
index 36d71c47..c6cff2db 100644
--- a/installed-tests/js/testGDBus.js
+++ b/installed-tests/js/testGDBus.js
@@ -628,8 +628,8 @@ describe('Exported DBus object', function () {
             GLib.Variant.new_string(PROP_READ_WRITE_INITIAL_VALUE.toString()));
     });
 
-    it('reading writeonly property returns null', function () {
-        expect(proxy.PropWriteOnly).toBeNull();
+    it('reading writeonly throws an error', function () {
+        expect(() => proxy.PropWriteOnly).toThrowError('Property PropWriteOnly is not readable');
     });
 
     it('Setting a readwrite property works', function () {
@@ -649,13 +649,16 @@ describe('Exported DBus object', function () {
             proxy.PropWriteOnly = testValue;
         }).not.toThrow();
 
+        expect(() => proxy.PropWriteOnly).toThrow();
         expect(waitForServerProperty('_propWriteOnly', testValue)).toEqual(testValue);
     });
 
-    it('Setting a readonly property does not throw', function () {
+    it('Setting a readonly property throws an error', function () {
         let testValue = Math.random().toString();
         expect(() => {
             proxy.PropReadOnly = testValue;
-        }).not.toThrow();
+        }).toThrowError('Property PropReadOnly is not writable');
+
+        expect(proxy.PropReadOnly).toBe(PROP_READ_ONLY_INITIAL_VALUE);
     });
 });
diff --git a/modules/overrides/Gio.js b/modules/overrides/Gio.js
index d4cb5199..4ab135fa 100644
--- a/modules/overrides/Gio.js
+++ b/modules/overrides/Gio.js
@@ -211,10 +211,26 @@ function _addDBusConvenience() {
     for (i = 0; i < properties.length; i++) {
         let name = properties[i].name;
         let signature = properties[i].signature;
-        Object.defineProperty(this, name, { get: _propertyGetter.bind(this, name),
-                                            set: _propertySetter.bind(this, name, signature),
-                                            configurable: false,
-                                            enumerable: true });
+        let flags = properties[i].flags;
+        let getter = () => {
+            throw new Error(`Property ${name} is not readable`);
+        };
+        let setter = () => {
+            throw new Error(`Property ${name} is not writable`);
+        };
+
+        if (flags & Gio.DBusPropertyInfoFlags.READABLE)
+            getter = _propertyGetter.bind(this, name);
+
+        if (flags & Gio.DBusPropertyInfoFlags.WRITABLE)
+            setter = _propertySetter.bind(this, name, signature);
+
+        Object.defineProperty(this, name, {
+            get: getter,
+            set: setter,
+            configurable: false,
+            enumerable: true
+        });
     }
 }
 


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