[gjs] dynamic class: Check if prototype is NULL



commit 8d85c940d6a275c53fca98f4697eed3112d64e30
Author: Philip Chimento <philip endlessm com>
Date:   Fri Jan 13 16:55:04 2017 -0800

    dynamic class: Check if prototype is NULL
    
    This fixes a regression introduced in commit
    a2fdf358207d075e9cbc6d78895f87b4e5a72bea.
    
    If the prototype passed to the dynamic class's constructor is NULL, then
    we have to call JS_NewObject() to get a default prototype, instead of
    JS_NewObjectWithGivenProto(). Otherwise the object has _no_ prototype at
    all.
    
    Includes a regression test that would have caught this bug.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=776966

 gjs/jsapi-dynamic-class.cpp                      |   20 ++++++++++++--------
 installed-tests/js/testEverythingEncapsulated.js |    5 +++++
 2 files changed, 17 insertions(+), 8 deletions(-)
---
diff --git a/gjs/jsapi-dynamic-class.cpp b/gjs/jsapi-dynamic-class.cpp
index e53efcf..57fdfc3 100644
--- a/gjs/jsapi-dynamic-class.cpp
+++ b/gjs/jsapi-dynamic-class.cpp
@@ -77,14 +77,18 @@ gjs_init_class_dynamic(JSContext              *context,
          JS_NewObjectForConstructor can find it
     */
 
-    /*
-     * JS_NewObject will try to search for clasp prototype in the global
-     * object if parent_proto is NULL, which is wrong, but it's not
-     * a problem because it will fallback to Object.prototype if the clasp's
-     * constructor is not found (and it won't be found, because we never call
-     * JS_InitClass).
-     */
-    prototype.set(JS_NewObjectWithGivenProto(context, clasp, parent_proto, global));
+    if (parent_proto) {
+        prototype.set(JS_NewObjectWithGivenProto(context, clasp,
+                                                 parent_proto, global));
+    } else {
+        /* JS_NewObject will try to search for clasp prototype in the
+         * global object, which is wrong, but it's not a problem because
+         * it will fallback to Object.prototype if the clasp's
+         * constructor is not found (and it won't be found, because we
+         * never call JS_InitClass).
+         */
+        prototype.set(JS_NewObject(context, clasp, JS::NullPtr(), global));
+    }
     if (!prototype)
         goto out;
 
diff --git a/installed-tests/js/testEverythingEncapsulated.js 
b/installed-tests/js/testEverythingEncapsulated.js
index 70e0bcb..c627c5e 100644
--- a/installed-tests/js/testEverythingEncapsulated.js
+++ b/installed-tests/js/testEverythingEncapsulated.js
@@ -224,4 +224,9 @@ describe('Introspected GObject', function () {
         'use strict';
         expect(() => obj.some_int8 = 41).toThrow();
     });
+
+    it('has normal Object methods', function () {
+        obj.ownprop = 'foo';
+        expect(obj.hasOwnProperty('ownprop')).toBeTruthy();
+    });
 });


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