#include <QScriptEngine>
#include <QCoreApplication>
#include <QDebug>
static void addToEngine( QScriptEngine& engine, const QString& varName, const QVector<float>& val )
{
QScriptValue result = engine.newArray( val.size() );
for ( int idx = 0; idx < val.size(); idx++ ) {
result.setProperty( idx, val[ idx ] );
}
engine.globalObject().setProperty( varName, result );
}
int main( int argc, char** argv )
{
QVector<float> a( 5 ), aa( 5 );
for ( int idx = 0; idx < 5; idx++ ) {
a[ idx ] = idx;
aa[ idx ] = idx + 10;
}
QScriptEngine engine;
addToEngine( engine, "a", a );
addToEngine( engine, "aa", aa );
#if 1
QScriptValue result = engine.evaluate( "d = a * aa;" );
#else
QScriptValue result = engine.evaluate( "\n\
var d = new Array; \n\
for ( var idx=0; idx < a.length; idx++ ) {\n\
d[idx] = a[idx] * aa[idx]; \n\
}" );
#endif
qDebug() << result.toString();
QScriptValue dVal = engine.globalObject().property( "d" );
qDebug() << dVal.toString();
}
#include <QScriptEngine>
#include <QCoreApplication>
#include <QDebug>
static void addToEngine( QScriptEngine& engine, const QString& varName, const QVector<float>& val )
{
QScriptValue result = engine.newArray( val.size() );
for ( int idx = 0; idx < val.size(); idx++ ) {
result.setProperty( idx, val[ idx ] );
}
engine.globalObject().setProperty( varName, result );
}
int main( int argc, char** argv )
{
QCoreApplication app( argc, argv );
QVector<float> a( 5 ), aa( 5 );
for ( int idx = 0; idx < 5; idx++ ) {
a[ idx ] = idx;
aa[ idx ] = idx + 10;
}
QScriptEngine engine;
addToEngine( engine, "a", a );
addToEngine( engine, "aa", aa );
#if 1
QScriptValue result = engine.evaluate( "d = a * aa;" );
#else
QScriptValue result = engine.evaluate( "\n\
var d = new Array; \n\
for ( var idx=0; idx < a.length; idx++ ) {\n\
d[idx] = a[idx] * aa[idx]; \n\
}" );
#endif
qDebug() << result.toString();
QScriptValue dVal = engine.globalObject().property( "d" );
qDebug() << dVal.toString();
}
To copy to clipboard, switch view to plain text mode
Bookmarks