fix isolate on wrong thread

This commit is contained in:
王劲鹏 2021-04-06 19:25:51 +08:00 committed by osborn
parent c01bd1e98c
commit ac39073551
2 changed files with 10 additions and 6 deletions

View File

@ -63,10 +63,12 @@ DoricJSEngine::DoricJSEngine(QObject *parent) : QObject(parent) {
// inject timer set & clear // inject timer set & clear
DoricTimerExtension *timerExtension = DoricTimerExtension *timerExtension =
new DoricTimerExtension([this](long timerId) { new DoricTimerExtension([this](long timerId) {
QVariantList arguments; auto result = QtConcurrent::run(&mJSThreadPool, [this, timerId] {
arguments.push_back(QVariant((int)timerId)); QVariantList arguments;
this->invokeDoricMethod(DoricConstant::DORIC_TIMER_CALLBACK, arguments.push_back(QVariant((int)timerId));
arguments); this->invokeDoricMethod(DoricConstant::DORIC_TIMER_CALLBACK,
arguments);
});
}); });
mJSE->injectGlobalJSFunction(DoricConstant::INJECT_TIMER_SET, mJSE->injectGlobalJSFunction(DoricConstant::INJECT_TIMER_SET,
timerExtension, "setTimer"); timerExtension, "setTimer");
@ -98,8 +100,7 @@ void DoricJSEngine::prepareContext(QString contextId, QString script,
mJSE->loadJS(packageContextScript(contextId, script), "Context://" + source); mJSE->loadJS(packageContextScript(contextId, script), "Context://" + source);
} }
void DoricJSEngine::invokeDoricMethod(QString method, void DoricJSEngine::invokeDoricMethod(QString method, QVariantList arguments) {
QVariantList arguments) {
return mJSE->invokeObject(DoricConstant::GLOBAL_DORIC, method, arguments); return mJSE->invokeObject(DoricConstant::GLOBAL_DORIC, method, arguments);
} }

View File

@ -61,6 +61,7 @@ v8::Local<v8::Value> String2JS(std::string string) {
v8::Local<v8::Value> Variant2JS(QVariant variant) { v8::Local<v8::Value> Variant2JS(QVariant variant) {
v8::Isolate *isolate = v8::Isolate::GetCurrent(); v8::Isolate *isolate = v8::Isolate::GetCurrent();
v8::EscapableHandleScope handle_scope(isolate); v8::EscapableHandleScope handle_scope(isolate);
v8::Local<v8::Context> context = isolate->GetEnteredOrMicrotaskContext(); v8::Local<v8::Context> context = isolate->GetEnteredOrMicrotaskContext();
@ -87,6 +88,8 @@ v8::Local<v8::Value> Variant2JS(QVariant variant) {
result.ToChecked(); result.ToChecked();
} }
jsValue = array; jsValue = array;
} else if (variant.type() == QVariant::Int) {
jsValue = v8::Number::New(isolate, variant.toDouble());
} }
return handle_scope.Escape(jsValue); return handle_scope.Escape(jsValue);
} }