0

i will try to use QScriptEngine to write function to Read Array and write array to change Trame for example with qtscript user. i have read lot of documentation.

i will try this: for the moment, i have QByteArray in my software, to check trameProtocol by script.

QByteArray l_ByteArray ="01";
QVariant l_varByteArray = l_ByteArray;
QScriptValue l_var = m_engine.newVariant(l_varByteArray);
QScriptValue m_fct_checksum = m_engine.evaluate("( \n function cal_crc(pTrame) \n { return pTrame[0]; \n } \n)");
qDebug() << m_fct_checksum.call(QScriptValue(),l_var).toInteger();
/* works  can read if i return element of array, but not can t modify by script , not have properti length in script not work
if call with newArray(), not have to access to property length */

i don't understand i will write custom class like this https://doc.qt.io/archives/qt-4.8/qt-script-customclass-example.html

or i have automatic conversion not works?

other test:

"( \n function cal_crc(pTrame) \n { return pTrame.length; \n } \n)"

QScriptValue l_var = m_engine.newArray(2);
l_var.setProperty( 0,QScriptValue('0') );
l_var.setProperty( 1,QScriptValue('1') );
qDebug() << l_var.toVariant();
qDebug() << m_fct_checksum.call(QScriptValue(),l_var).toInteger();

//console
QVariant(QVariantList, (QVariant(int, 48), QVariant(int, 49)))
0
like writing previously, don't modify pTrame in script
    ("(function cal_crc(pTrame){pTrame[0]=5;return pTrame[0];})");
   //same result
   QVariant(QVariantList, (QVariant(int, 48), QVariant(int, 49)))
    0 //not 5

if i return pTrame in script:

  "(function cal_crc(pTrame){pTrame[0]=5;return pTrame;})"
   qDebug() << m_fct_checksum.call(QScriptValue(),l_var).toVariant();
   QVariant(QVariantList, (QVariant(int, 48), QVariant(int, 49)))
    QVariant(int, 48)

or

 "function cal_crc(pTrame){return pTrame})"
  QVariant(QVariantList, (QVariant(int, 48), QVariant(int, 49)))
  QVariant(int, 48)
//return only the first,, it's strange because en i work in arg but not in return and can't modify i repeat..

any idea?

thanks

anyone
  • 1
  • 1

1 Answers1

0

if i register pTrame is ok and not use in arg //with this script:

 "(function(){pTrame[0]=5;return pTrame})"

QScriptValue l_var = m_engine.newArray(2);
  l_var.setProperty( 0,QScriptValue('0') );
  l_var.setProperty( 1,QScriptValue('1') );
 m_engine.globalObject().setProperty("pTrame", l_var);


//console
QVariant(QVariantList, (QVariant(int, 48), QVariant(int, 49)))
QVariant(QVariantList, (QVariant(int, 5), QVariant(int, 49)))

any idea to do better?

anyone
  • 1
  • 1