]> git.sur5r.net Git - minitube/blob - lib/js/jsvm.cpp
New upstream version 3.9.3
[minitube] / lib / js / jsvm.cpp
1 #include "jsvm.h"
2
3 JSVM::JSVM(QQmlEngine *parent) : QObject{parent}, parentEngine(parent) {}
4
5 QJSValue JSVM::runInContext(QString code, QJSValue props) {
6     auto engine = QQmlEngine(this);
7
8     auto objectKeysFunction = parentEngine->evaluate("Object.keys");
9     auto keys = objectKeysFunction.call({props});
10     const int keyLength = keys.property("length").toInt();
11     for (int i = 0; i < keyLength; ++i) {
12         auto key = keys.property(i).toString();
13         auto value = props.property(key).toString();
14         qDebug() << "Setting property" << key << value;
15         engine.globalObject().setProperty(key, value);
16     }
17
18     auto res = engine.evaluate(code).toString();
19     return parentEngine->toScriptValue(res);
20 }