andrewplummer / Sugar

A Javascript library for working with native objects.
https://sugarjs.com/
MIT License
4.53k stars 306 forks source link

QML problems with Qt 5.7 #551

Closed bgr closed 7 years ago

bgr commented 8 years ago

Hey, QML problems have come up again.

They've changed something in Qt 5.7 (and maybe 5.6 as well, I skipped that version). Running this minimal example:

// example.qml
import QtQuick 2.7
import "sugar.js" as Sugar

Item {
    Component.onCompleted: {
        Sugar.Sugar.extend();
    }
}

causes the following error: sugar.js:798: TypeError: Type error

(of course, the exact line will vary depending on the build, this one is from the QML build from today's master; the same error happens with regular downloaded sugar.js build as well as the months-old and proven QML build I've been using so far)

I briefly inspected what's going on, seems like it's due to undefined being passed as arg to sugarNamespace.extend(arg); up in the call stack, but I might be wrong.

To run the example you can download Qt 5.7 here and run it using qmlscene example.qml (assumed that "sugar.js" file is in the same directory; qmlscene binary can be found in e.g. .../Qt/Qt5.7.0/5.7/<compiler>/bin)

andrewplummer commented 8 years ago

Well, it would appear that an update to QML has made native all native methods configurable: false, meaning that any attempt to override them results in a TypeError. Sugar 1.4.1 does this to allow enhanced Array methods. Sugar 2.0.0 does it as well, however in making native extension opt-in, this issue was anticipated, so fortunately you can just do this:

// example.qml
import QtQuick 2.7
import "sugar.js" as Sugar

Item {
    Component.onCompleted: {
        Sugar.Sugar.extend({
          enhance: false
        });
    }
}

This should take care of the issue for you. Overriding native methods being controversial as it is, this flag was added. However, the changes QML have instituted are not part of spec, and could potentially interfere with polyfills, so should be treated as a bug. Maybe filing a bug report would make sense?

bgr commented 8 years ago

Thanks. I've filed the issue.

The enhance: false fix looks promising, the impact seems relatively small. I'll keep this issue open for now, other Qt-related problems might come out once I fix this one.

andrewplummer commented 8 years ago

Essentially what it means is that you won't be able to take advantage of enhanced methods. At the moment this is essentially just shortcuts for every, filter, map, etc.

Note however, that the "enhanced" functionality will still work with the new static methods or chainables, so you can still take advantage of it there.