complete context init & build
This commit is contained in:
@@ -13,7 +13,7 @@ public:
|
||||
|
||||
virtual void injectGlobalJSFunction(QString name, QObject *function, QString property) = 0;
|
||||
|
||||
virtual QJSValue invokeObject(QString objectName, QString functionName, QJSValueList arguments) = 0;
|
||||
virtual QJSValue invokeObject(QString objectName, QString functionName, QVariantList arguments) = 0;
|
||||
};
|
||||
|
||||
#endif // INTERFACE_JSE_H
|
||||
|
@@ -10,6 +10,7 @@
|
||||
#include "../utils/constant.h"
|
||||
#include "native_log.h"
|
||||
#include "native_empty.h"
|
||||
#include "native_require.h"
|
||||
#include "timer_extension.h"
|
||||
#include "bridge_extension.h"
|
||||
#include "../utils/utils.h"
|
||||
@@ -51,11 +52,14 @@ JSEngine::JSEngine(QObject *parent) : QObject(parent)
|
||||
NativeEmpty *nativeEmpty = new NativeEmpty();
|
||||
mJSE->injectGlobalJSFunction(Constant::INJECT_EMPTY, nativeEmpty, "function");
|
||||
|
||||
// inject require
|
||||
NativeRequire *nativeRequire = new NativeRequire();
|
||||
mJSE->injectGlobalJSFunction(Constant::INJECT_REQUIRE, nativeRequire, "function");
|
||||
|
||||
// inject timer set & clear
|
||||
std::function<void(void)> func = [](){};
|
||||
TimerExtension *timerExtension = new TimerExtension([this](long timerId){
|
||||
QJSValueList arguments;
|
||||
arguments.append(QJSValue((int)timerId));
|
||||
QVariantList arguments;
|
||||
arguments.push_back(QVariant((int)timerId));
|
||||
this->invokeDoricMethod(Constant::DORIC_TIMER_CALLBACK, arguments);
|
||||
});
|
||||
mJSE->injectGlobalJSFunction(Constant::INJECT_TIMER_SET, timerExtension, "setTimer");
|
||||
@@ -75,7 +79,7 @@ JSEngine::JSEngine(QObject *parent) : QObject(parent)
|
||||
});
|
||||
}
|
||||
|
||||
QJSValue JSEngine::invokeDoricMethod(QString method, QJSValueList arguments)
|
||||
QJSValue JSEngine::invokeDoricMethod(QString method, QVariantList arguments)
|
||||
{
|
||||
return mJSE->invokeObject(Constant::GLOBAL_DORIC, method, arguments);
|
||||
}
|
||||
|
@@ -22,7 +22,7 @@ public:
|
||||
|
||||
~JSEngine();
|
||||
|
||||
QJSValue invokeDoricMethod(QString method, QJSValueList arguments);
|
||||
QJSValue invokeDoricMethod(QString method, QVariantList arguments);
|
||||
void prepareContext(QString contextId, QString script, QString source);
|
||||
};
|
||||
|
||||
|
@@ -35,9 +35,35 @@ void NativeJSE::injectGlobalJSFunction(QString name, QObject *function, QString
|
||||
mJSEngine.globalObject().setProperty(name, functionObject.property(property));
|
||||
}
|
||||
|
||||
QJSValue NativeJSE::invokeObject(QString objectName, QString functionName, QJSValueList arguments)
|
||||
QJSValue NativeJSE::invokeObject(QString objectName, QString functionName, QVariantList arguments)
|
||||
{
|
||||
QJSValue object = mJSEngine.evaluate(objectName);
|
||||
QJSValue function = object.property(functionName);
|
||||
return function.call(arguments);
|
||||
|
||||
QJSValueList args;
|
||||
foreach(QVariant variant, arguments) {
|
||||
if (variant.type() == QVariant::String) {
|
||||
args.push_back(QJSValue(variant.toString()));
|
||||
} else if (variant.type() == QVariant::Map) {
|
||||
QJSValue arg = mJSEngine.newObject();
|
||||
QMap<QString, QVariant> map = variant.toMap();
|
||||
foreach (QString key, map.keys()) {
|
||||
QVariant value = map.value(key);
|
||||
if (value.type() == QVariant::String) {
|
||||
arg.setProperty(key, value.toString());
|
||||
} else if (value.type() == QVariant::Int) {
|
||||
arg.setProperty(key, value.toInt());
|
||||
}
|
||||
}
|
||||
args.push_back(arg);
|
||||
}
|
||||
}
|
||||
|
||||
QJSValue result = function.call(args);
|
||||
if (result.isError())
|
||||
qDebug()
|
||||
<< "Uncaught exception at line"
|
||||
<< result.property("lineNumber").toInt()
|
||||
<< ":" << result.toString();
|
||||
return result;
|
||||
}
|
||||
|
@@ -17,7 +17,7 @@ public:
|
||||
|
||||
void injectGlobalJSFunction(QString name, QObject *function, QString property) override;
|
||||
|
||||
QJSValue invokeObject(QString objectName, QString functionName, QJSValueList arguments) override;
|
||||
QJSValue invokeObject(QString objectName, QString functionName, QVariantList arguments) override;
|
||||
};
|
||||
|
||||
#endif // NATIVE_JSE_H
|
||||
|
8
doric-Qt/doric/engine/native_require.cpp
Normal file
8
doric-Qt/doric/engine/native_require.cpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include "native_require.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
Q_INVOKABLE QJSValue NativeRequire::function(QString name) {
|
||||
qDebug() << "nativeRequire";
|
||||
return QJSValue::NullValue;
|
||||
}
|
18
doric-Qt/doric/engine/native_require.h
Normal file
18
doric-Qt/doric/engine/native_require.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef NATIVE_REQUIRE_H
|
||||
#define NATIVE_REQUIRE_H
|
||||
|
||||
|
||||
#include <QObject>
|
||||
#include <QJSValue>
|
||||
|
||||
class NativeRequire : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
NativeRequire(QObject *parent = nullptr) : QObject(parent) {}
|
||||
|
||||
Q_INVOKABLE QJSValue function(QString name);
|
||||
};
|
||||
|
||||
|
||||
#endif // NATIVE_REQUIRE_H
|
Reference in New Issue
Block a user