Details
-
Bug
-
Resolution: Done
-
P2: Important
-
6.3.0
-
b201b1d0b3 (qt/qtdeclarative/dev)
Description
I have a QObject with:
Q_PROPERTY(QByteArray foo READ foo WRITE setFoo NOTIFY fooChanged) Q_PROPERTY(QByteArray bar READ bar WRITE setBar NOTIFY barChanged)
where foo() returns QByteArray("Some Text") and bar() returns a default constructed QByteArray.
In QML accessing bar.byteLength throws TypeError: Type error.
For instance the following QML code:
console.log(obj); console.log(obj.foo); console.log(obj.foo.byteLength); console.log(obj.bar); console.log(typeof obj.bar); console.log(obj.bar.constructor.name); console.log('byteLength' in obj.bar); try { console.log(obj.bar.byteLength); } catch (error) { console.error(error); } try { console.log(typeof obj.bar['byteLength']); } catch (error) { console.error(error); }
generates the following output:
qml: MyObject(0x5640bdc0a0e0)
qml: Some Text
qml: 9
qml:
qml: object
qml: ArrayBuffer
aml: true
qml: TypeError: Type error
qml: TypeError: Type error
Edit
I have made a simpler example using only QJSEngine:
void test(QJSEngine *engine, const QByteArray &ba) { qDebug() << "TEST with" << ba; auto value = engine->toScriptValue(ba); engine->globalObject().setProperty("myarray", value); QStringList trace; const auto result = engine->evaluate(R"( (function(){ return myarray.byteLength; })() )", "script.js", 1, &trace); if (result.isError()) { qDebug() << "ERROR" << result.toString(); qDebug() << "TRACE:"; qDebug().noquote() << trace.join("\n"); } else { qDebug() << "OK:" << result.toNumber(); } qDebug() << ""; }
Then calling
test(&engine, "Some Text");
test(&engine, {});
gives:
TEST with "Some Text" OK: 9 TEST with "" ERROR "TypeError: Type error" TRACE: :3:-1:[file:script.js|file:///script.js] %entry:2:-1:[file:script.js|file:///script.js]