History

Added V8 Locker for moving script engine to a new thread

ksuprynowicz committed 4 years ago

Commit abfcbb71 parent 827ddf53 files · +39 −3Browse files

Showing the changes to libraries/script-engine/src/v8/ScriptEngineV8.cpp (1 of 3 files). Show all

Unified · Split

libraries/script-engine/src/v8/ScriptEngineV8.cpp+35−2
@@ -800,6 +800,7 @@ ScriptValue ScriptEngineV8::evaluate(const QString& sourceCode, const QString& f · 799 unmodified lines
800 v8::ScriptOrigin scriptOrigin(getIsolate(), v8::String::NewFromUtf8(getIsolate(), fileName.toStdString().c_str()).ToLocalChecked());800 v8::ScriptOrigin scriptOrigin(getIsolate(), v8::String::NewFromUtf8(getIsolate(), fileName.toStdString().c_str()).ToLocalChecked());
801 v8::Local<v8::Script> script;801 v8::Local<v8::Script> script;
802 if (!v8::Script::Compile(getContext(), v8::String::NewFromUtf8(getIsolate(), sourceCode.toStdString().c_str()).ToLocalChecked(), &scriptOrigin).ToLocal(&script)) {802 if (!v8::Script::Compile(getContext(), v8::String::NewFromUtf8(getIsolate(), sourceCode.toStdString().c_str()).ToLocalChecked(), &scriptOrigin).ToLocal(&script)) {
803 //V8TODO replace this with external function
803 int errorColumnNumber = 0;804 int errorColumnNumber = 0;
804 int errorLineNumber = 0;805 int errorLineNumber = 0;
805 QString errorMessage = "";806 QString errorMessage = "";
@@ -851,6 +852,7 @@ ScriptValue ScriptEngineV8::evaluate(const QString& sourceCode, const QString& f · 45 unmodified lines
851 Q_ASSERT(tryCatchRun.HasCaught());852 Q_ASSERT(tryCatchRun.HasCaught());
852 auto runError = tryCatchRun.Message();853 auto runError = tryCatchRun.Message();
853 ScriptValue errorValue(new ScriptValueV8Wrapper(this, V8ScriptValue(_v8Isolate, runError->Get())));854 ScriptValue errorValue(new ScriptValueV8Wrapper(this, V8ScriptValue(_v8Isolate, runError->Get())));
855 qCDebug(scriptengine) << "Running script: \"" << fileName << "\" " << formatErrorMessageFromTryCatch(tryCatchRun);
854 //V8TODO856 //V8TODO
855 //raiseException(errorValue);857 //raiseException(errorValue);
856 //maybeEmitUncaughtException("evaluate");858 //maybeEmitUncaughtException("evaluate");
@@ -860,6 +862,31 @@ ScriptValue ScriptEngineV8::evaluate(const QString& sourceCode, const QString& f · 3 unmodified lines
860 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(resultValue)));862 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(resultValue)));
861}863}
862 864
865QString ScriptEngineV8::formatErrorMessageFromTryCatch(v8::TryCatch &tryCatch) {
866 QString result("");
867 int errorColumnNumber = 0;
868 int errorLineNumber = 0;
869 QString errorMessage = "";
870 QString errorBacktrace = "";
871 //v8::String::Utf8Value utf8Value(getIsolate(), tryCatch.Exception());
872 v8::String::Utf8Value utf8Value(getIsolate(), tryCatch.Message()->Get());
873 errorMessage = QString(*utf8Value);
874 v8::Local<v8::Message> exceptionMessage = tryCatch.Message();
875 if (!exceptionMessage.IsEmpty()) {
876 errorLineNumber = exceptionMessage->GetLineNumber(getContext()).FromJust();
877 errorColumnNumber = exceptionMessage->GetStartColumn(getContext()).FromJust();
878 v8::Local<v8::Value> backtraceV8String;
879 if (tryCatch.StackTrace(getContext()).ToLocal(&backtraceV8String) && backtraceV8String->IsString() &&
880 v8::Local<v8::String>::Cast(backtraceV8String)->Length() > 0) {
881 v8::String::Utf8Value backtraceUtf8Value(getIsolate(), backtraceV8String);
882 errorBacktrace = *backtraceUtf8Value;
883 }
884 QTextStream resultStream(&result);
885 resultStream << "failed on line " << errorLineNumber << " column " << errorColumnNumber << " with message: \"" << errorMessage <<"\" backtrace: " << errorBacktrace;
886 }
887 return result;
888}
889
863Q_INVOKABLE ScriptValue ScriptEngineV8::evaluate(const ScriptProgramPointer& program) {890Q_INVOKABLE ScriptValue ScriptEngineV8::evaluate(const ScriptProgramPointer& program) {
864 if (_scriptManager && _scriptManager->isStopped()) {891 if (_scriptManager && _scriptManager->isStopped()) {
865 return undefinedValue(); // bail early892 return undefinedValue(); // bail early
@@ -1182,12 +1209,18 @@ void ScriptEngineV8::setThread(QThread* thread) { · 316 unmodified lines
1182 _v8Isolate->Exit();1209 _v8Isolate->Exit();
1183 qDebug() << "Script engine " << objectName() << " exited isolate";1210 qDebug() << "Script engine " << objectName() << " exited isolate";
1184 }1211 }
1212 Q_ASSERT(QObject::thread() == QThread::currentThread());
1213 if (_v8Locker) {
1214 _v8Locker.reset();
1215 }
1185 moveToThread(thread);1216 moveToThread(thread);
1186 qDebug() << "Moved script engine " << objectName() << " to different thread";1217 qDebug() << "Moved script engine " << objectName() << " to different thread";
1187}1218}
1188 1219
1189void ScriptEngineV8::enterIsolateOnThisThread() {1220void ScriptEngineV8::enterIsolateOnThisThread() {
1190 Q_ASSERT(thread() == QThread::currentThread());1221 Q_ASSERT(thread() == QThread::currentThread());
1222 Q_ASSERT(!_v8Locker);
1223 _v8Locker.reset(new v8::Locker(_v8Isolate));
1191 if (!_v8Isolate->IsCurrent()) {1224 if (!_v8Isolate->IsCurrent()) {
1192 _v8Isolate->Enter();1225 _v8Isolate->Enter();
1193 qDebug() << "Script engine " << objectName() << " entered isolate on a new thread";1226 qDebug() << "Script engine " << objectName() << " entered isolate on a new thread";
@@ -1252,8 +1285,8 @@ QVariant ScriptEngineV8::convert(const ScriptValue& value, int typeId) { · 58 unmodified lines
1252}1285}
1253 1286
1254void ScriptEngineV8::compileTest() {1287void ScriptEngineV8::compileTest() {
1255 v8::Locker locker(_v8Isolate);1288 //v8::Locker locker(_v8Isolate);
1256 v8::Isolate::Scope isolateScope(_v8Isolate);1289 //v8::Isolate::Scope isolateScope(_v8Isolate);
1257 v8::HandleScope handleScope(_v8Isolate);1290 v8::HandleScope handleScope(_v8Isolate);
1258 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));1291 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
1259 v8::Local<v8::Script> script;1292 v8::Local<v8::Script> script;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t