[gjs: 1/2] object: Warn if passing more than one argument to default constructor



commit fe49c62c076789cfc33d9fcfe064465fb3357392
Author: Philip Chimento <philip chimento gmail com>
Date:   Sun Oct 27 22:18:28 2019 -0700

    object: Warn if passing more than one argument to default constructor
    
    The default constructor of GObjects takes one optional object which is
    treated as a property hash. Any subsequent arguments are ignored, as is
    normal in JavaScript.
    
    However, since we do log a warning about extra arguments to
    GObject-introspected functions being ignored, it seems reasonable that
    we should also log a warning when extra arguments to the default GObject
    constructor are ignored.
    
    Also adds some tests for the functionality that was already there:
    throwing if something that's not an object is passed in, and allowing an
    object that's not a plain JavaScript object.
    
    Closes: #63.

 gi/object.cpp                          |  7 +++++++
 installed-tests/js/testGObjectClass.js | 18 ++++++++++++++++++
 2 files changed, 25 insertions(+)
---
diff --git a/gi/object.cpp b/gi/object.cpp
index 9cd9c40a..8472f481 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -1437,6 +1437,13 @@ ObjectInstance::init_impl(JSContext              *context,
 
     g_assert(gtype() != G_TYPE_NONE);
 
+    if (args.length() > 1) {
+        JS_ReportWarningUTF8(context,
+                             "Too many arguments to the constructor of %s: "
+                             "expected 1, got %u",
+                             name(), args.length());
+    }
+
     std::vector<const char *> names;
     AutoGValueVector values;
 
diff --git a/installed-tests/js/testGObjectClass.js b/installed-tests/js/testGObjectClass.js
index 92d1ffdf..ced92ad3 100644
--- a/installed-tests/js/testGObjectClass.js
+++ b/installed-tests/js/testGObjectClass.js
@@ -181,6 +181,24 @@ describe('GObject class with decorator', function () {
         expect(myInstance2.construct).toEqual('asdf');
     });
 
+    it('warns if more than one argument passed to the default constructor', function () {
+        GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE,
+            '*Too many arguments*');
+
+        new MyObject({readwrite: 'baz'}, 'this is ignored', 123);
+
+        GLib.test_assert_expected_messages_internal('Gjs', 'testGObjectClass.js', 0,
+            'testGObjectClassTooManyArguments');
+    });
+
+    it('throws an error if the first argument to the default constructor is not a property hash', function 
() {
+        expect(() => new MyObject('this is wrong')).toThrow();
+    });
+
+    it('accepts a property hash that is not a plain object', function () {
+        expect(() => new MyObject(new GObject.Object())).not.toThrow();
+    });
+
     const ui = `<interface>
                   <object class="Gjs_MyObject" id="MyObject">
                     <property name="readwrite">baz</property>


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