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
DoricTimerExtension *timerExtension =
new DoricTimerExtension([this](long timerId) {
QVariantList arguments;
arguments.push_back(QVariant((int)timerId));
this->invokeDoricMethod(DoricConstant::DORIC_TIMER_CALLBACK,
arguments);
auto result = QtConcurrent::run(&mJSThreadPool, [this, timerId] {
QVariantList arguments;
arguments.push_back(QVariant((int)timerId));
this->invokeDoricMethod(DoricConstant::DORIC_TIMER_CALLBACK,
arguments);
});
});
mJSE->injectGlobalJSFunction(DoricConstant::INJECT_TIMER_SET,
timerExtension, "setTimer");
@ -98,8 +100,7 @@ void DoricJSEngine::prepareContext(QString contextId, QString script,
mJSE->loadJS(packageContextScript(contextId, script), "Context://" + source);
}
void DoricJSEngine::invokeDoricMethod(QString method,
QVariantList arguments) {
void DoricJSEngine::invokeDoricMethod(QString method, QVariantList 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::Isolate *isolate = v8::Isolate::GetCurrent();
v8::EscapableHandleScope handle_scope(isolate);
v8::Local<v8::Context> context = isolate->GetEnteredOrMicrotaskContext();
@ -87,6 +88,8 @@ v8::Local<v8::Value> Variant2JS(QVariant variant) {
result.ToChecked();
}
jsValue = array;
} else if (variant.type() == QVariant::Int) {
jsValue = v8::Number::New(isolate, variant.toDouble());
}
return handle_scope.Escape(jsValue);
}