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
800800 v8::ScriptOrigin scriptOrigin(getIsolate(), v8::String::NewFromUtf8(getIsolate(), fileName.toStdString().c_str()).ToLocalChecked());
801801 v8::Local<v8::Script> script;
802802 if (!v8::Script::Compile(getContext(), v8::String::NewFromUtf8(getIsolate(), sourceCode.toStdString().c_str()).ToLocalChecked(), &scriptOrigin).ToLocal(&script)) {
803+ //V8TODO replace this with external function
803804 int errorColumnNumber = 0;
804805 int errorLineNumber = 0;
805806 QString errorMessage = "";
@@ -851,6 +852,7 @@ ScriptValue ScriptEngineV8::evaluate(const QString& sourceCode, const QString& f · 45 unmodified lines
851852 Q_ASSERT(tryCatchRun.HasCaught());
852853 auto runError = tryCatchRun.Message();
853854 ScriptValue errorValue(new ScriptValueV8Wrapper(this, V8ScriptValue(_v8Isolate, runError->Get())));
855+ qCDebug(scriptengine) << "Running script: \"" << fileName << "\" " << formatErrorMessageFromTryCatch(tryCatchRun);
854856 //V8TODO
855857 //raiseException(errorValue);
856858 //maybeEmitUncaughtException("evaluate");
@@ -860,6 +862,31 @@ ScriptValue ScriptEngineV8::evaluate(const QString& sourceCode, const QString& f · 3 unmodified lines
860862 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(resultValue)));
861863 }
862864
865+QString 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+
863890 Q_INVOKABLE ScriptValue ScriptEngineV8::evaluate(const ScriptProgramPointer& program) {
864891 if (_scriptManager && _scriptManager->isStopped()) {
865892 return undefinedValue(); // bail early
@@ -1182,12 +1209,18 @@ void ScriptEngineV8::setThread(QThread* thread) { · 316 unmodified lines
11821209 _v8Isolate->Exit();
11831210 qDebug() << "Script engine " << objectName() << " exited isolate";
11841211 }
1212+ Q_ASSERT(QObject::thread() == QThread::currentThread());
1213+ if (_v8Locker) {
1214+ _v8Locker.reset();
1215+ }
11851216 moveToThread(thread);
11861217 qDebug() << "Moved script engine " << objectName() << " to different thread";
11871218 }
11881219
11891220 void ScriptEngineV8::enterIsolateOnThisThread() {
11901221 Q_ASSERT(thread() == QThread::currentThread());
1222+ Q_ASSERT(!_v8Locker);
1223+ _v8Locker.reset(new v8::Locker(_v8Isolate));
11911224 if (!_v8Isolate->IsCurrent()) {
11921225 _v8Isolate->Enter();
11931226 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
12521285 }
12531286
12541287 void ScriptEngineV8::compileTest() {
1255− v8::Locker locker(_v8Isolate);
1256− v8::Isolate::Scope isolateScope(_v8Isolate);
1288+ //v8::Locker locker(_v8Isolate);
1289+ //v8::Isolate::Scope isolateScope(_v8Isolate);
12571290 v8::HandleScope handleScope(_v8Isolate);
12581291 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
12591292 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