1
0
mirror of https://github.com/thp/pyotherside.git synced 2025-01-28 23:52:55 +08:00

Don't pass QJSValue by value between different threads

This also avoids having additional signals when the callback isn't
a callable by not creating a QJSValue referencing the callback.
This commit is contained in:
Thomas Perl 2014-02-04 22:13:05 +01:00
parent a621f8c8ec
commit c765bf3d03
4 changed files with 46 additions and 36 deletions

View File

@ -45,15 +45,15 @@ QPython::QPython(QObject *parent)
QObject::connect(priv, SIGNAL(receive(QVariant)),
this, SLOT(receive(QVariant)));
QObject::connect(this, SIGNAL(process(QString,QVariant,QJSValue)),
worker, SLOT(process(QString,QVariant,QJSValue)));
QObject::connect(worker, SIGNAL(finished(QVariant,QJSValue)),
this, SLOT(finished(QVariant,QJSValue)));
QObject::connect(this, SIGNAL(process(QString,QVariant,QJSValue *)),
worker, SLOT(process(QString,QVariant,QJSValue *)));
QObject::connect(worker, SIGNAL(finished(QVariant,QJSValue *)),
this, SLOT(finished(QVariant,QJSValue *)));
QObject::connect(this, SIGNAL(import(QString,QJSValue)),
worker, SLOT(import(QString,QJSValue)));
QObject::connect(worker, SIGNAL(imported(bool,QJSValue)),
this, SLOT(imported(bool,QJSValue)));
QObject::connect(this, SIGNAL(import(QString,QJSValue *)),
worker, SLOT(import(QString,QJSValue *)));
QObject::connect(worker, SIGNAL(imported(bool,QJSValue *)),
this, SLOT(imported(bool,QJSValue *)));
thread.setObjectName("QPythonWorker");
thread.start();
@ -84,7 +84,11 @@ QPython::addImportPath(QString path)
void
QPython::importModule(QString name, QJSValue callback)
{
emit import(name, callback);
QJSValue *cb = 0;
if (!callback.isNull() && !callback.isUndefined() && callback.isCallable()) {
cb = new QJSValue(callback);
}
emit import(name, cb);
}
bool
@ -168,7 +172,11 @@ QPython::evaluate(QString expr)
void
QPython::call(QString func, QVariant args, QJSValue callback)
{
emit process(func, args, callback);
QJSValue *cb = 0;
if (!callback.isNull() && !callback.isUndefined() && callback.isCallable()) {
cb = new QJSValue(callback);
}
emit process(func, args, cb);
}
QVariant
@ -220,23 +228,21 @@ QPython::call_sync(QString func, QVariant args)
}
void
QPython::finished(QVariant result, QJSValue callback)
QPython::finished(QVariant result, QJSValue *callback)
{
if (!callback.isNull() && !callback.isUndefined() && callback.isCallable()) {
QJSValueList args;
QJSValue v = callback.engine()->toScriptValue(result);
args << v;
callback.call(args);
}
QJSValueList args;
QJSValue v = callback->engine()->toScriptValue(result);
args << v;
callback->call(args);
delete callback;
}
void
QPython::imported(bool result, QJSValue callback)
QPython::imported(bool result, QJSValue *callback)
{
if (!callback.isNull() && !callback.isUndefined() && callback.isCallable()) {
QJSValueList args;
QJSValue v = callback.engine()->toScriptValue(QVariant(result));
args << v;
callback.call(args);
}
QJSValueList args;
QJSValue v = callback->engine()->toScriptValue(QVariant(result));
args << v;
callback->call(args);
delete callback;
}

View File

@ -260,14 +260,14 @@ class QPython : public QObject {
void error(QString traceback);
/* For internal use only */
void process(QString func, QVariant args, QJSValue callback);
void import(QString name, QJSValue callback);
void process(QString func, QVariant args, QJSValue *callback);
void import(QString name, QJSValue *callback);
private slots:
void receive(QVariant data);
void finished(QVariant result, QJSValue callback);
void imported(bool result, QJSValue callback);
void finished(QVariant result, QJSValue *callback);
void imported(bool result, QJSValue *callback);
private:
static QPythonPriv *priv;

View File

@ -32,15 +32,19 @@ QPythonWorker::~QPythonWorker()
}
void
QPythonWorker::process(QString func, QVariant args, QJSValue callback)
QPythonWorker::process(QString func, QVariant args, QJSValue *callback)
{
QVariant result = qpython->call_sync(func, args);
emit finished(result, callback);
if (callback) {
emit finished(result, callback);
}
}
void
QPythonWorker::import(QString name, QJSValue callback)
QPythonWorker::import(QString name, QJSValue *callback)
{
bool result = qpython->importModule_sync(name);
emit imported(result, callback);
if (callback) {
emit imported(result, callback);
}
}

View File

@ -34,12 +34,12 @@ class QPythonWorker : public QObject {
~QPythonWorker();
public slots:
void process(QString func, QVariant args, QJSValue callback);
void import(QString func, QJSValue callback);
void process(QString func, QVariant args, QJSValue *callback);
void import(QString func, QJSValue *callback);
signals:
void finished(QVariant result, QJSValue callback);
void imported(bool result, QJSValue callback);
void finished(QVariant result, QJSValue *callback);
void imported(bool result, QJSValue *callback);
private:
QPython *qpython;