[gjs: 1/2] legacy: Allow ES6 classes to inherit from abstract Lang.Class class



commit cf551d9ded996738c849294c22f50cb1864ad1a8
Author: Florian Müllner <fmuellner gnome org>
Date:   Sat Oct 28 17:59:33 2017 +0200

    legacy: Allow ES6 classes to inherit from abstract Lang.Class class
    
    Currently Lang.Class uses a distinct constructor for abstract classes,
    which just throws when instantiated. This works as Lang.Class classes
    chain up to _init rather than the real constructor, which isn't the
    case for ES6 classes. In order to allow ES6 classes to directly inherit
    from an abstract Lang.Class class, check whether an abstract class
    has been instantiated directly inside the regular constructor.

 installed-tests/js/testLegacyClass.js |  7 +++++++
 modules/_legacy.js                    | 16 ++++++----------
 2 files changed, 13 insertions(+), 10 deletions(-)
---
diff --git a/installed-tests/js/testLegacyClass.js b/installed-tests/js/testLegacyClass.js
index ac59e8e..e691abb 100644
--- a/installed-tests/js/testLegacyClass.js
+++ b/installed-tests/js/testLegacyClass.js
@@ -292,6 +292,13 @@ describe('Class framework', function () {
         expect(newAbstract.foo).toEqual(42);
     });
 
+    it('allows ES6 classes to inherit from abstract base classes', function() {
+        class AbstractImpl extends AbstractBase {};
+
+        let newAbstract = new AbstractImpl();
+        expect(newAbstract.foo).toEqual(42);
+    });
+
     it('lets methods call other methods without clobbering __caller__', function () {
         let newMagic = new Magic();
         let buffer = [];
diff --git a/modules/_legacy.js b/modules/_legacy.js
index 99cef8c..a89420c 100644
--- a/modules/_legacy.js
+++ b/modules/_legacy.js
@@ -105,18 +105,14 @@ Class.prototype._construct = function(params) {
     if (!parent)
         parent = _Base;
 
-    let newClass;
-    if (params.Abstract) {
-        newClass = function() {
+    let newClass = function() {
+        if (params.Abstract && new.target.name === name)
             throw new TypeError('Cannot instantiate abstract class ' + name);
-        };
-    } else {
-        newClass = function() {
-            this.__caller__ = null;
 
-            return this._construct.apply(this, arguments);
-        };
-    }
+        this.__caller__ = null;
+
+        return this._construct(...arguments);
+    };
 
     // Since it's not possible to create a constructor with
     // a custom [[Prototype]], we have to do this to make


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