File: jsvm.cpp

package info (click to toggle)
minitube 3.9.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,600 kB
  • sloc: cpp: 19,800; xml: 68; sh: 18; makefile: 11
file content (20 lines) | stat: -rw-r--r-- 726 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "jsvm.h"

JSVM::JSVM(QQmlEngine *parent) : QObject{parent}, parentEngine(parent) {}

QJSValue JSVM::runInContext(QString code, QJSValue props) {
    auto engine = QQmlEngine(this);

    auto objectKeysFunction = parentEngine->evaluate("Object.keys");
    auto keys = objectKeysFunction.call({props});
    const int keyLength = keys.property("length").toInt();
    for (int i = 0; i < keyLength; ++i) {
        auto key = keys.property(i).toString();
        auto value = props.property(key).toString();
        qDebug() << "Setting property" << key << value;
        engine.globalObject().setProperty(key, value);
    }

    auto res = engine.evaluate(code).toString();
    return parentEngine->toScriptValue(res);
}