History

Added simple compile test function

ksuprynowicz committed 4 years ago

Commit 827ddf51 parent 38064b97 files · +83 −19Browse files

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

Unified · Split

libraries/script-engine/src/v8/ScriptEngineV8.cpp+38−11
@@ -134,7 +134,7 @@ ScriptValue ScriptEngineV8::makeError(const ScriptValue& _other, const QString& · 133 unmodified lines
134}134}
135 135
136// check syntax and when there are issues returns an actual "SyntaxError" with the details136// check syntax and when there are issues returns an actual "SyntaxError" with the details
137ScriptValue ScriptEngineV8::cheskScriptSyntax(ScriptProgramPointer program) {137ScriptValue ScriptEngineV8::checkScriptSyntax(ScriptProgramPointer program) {
138 if (!IS_THREADSAFE_INVOCATION(thread(), __FUNCTION__)) {138 if (!IS_THREADSAFE_INVOCATION(thread(), __FUNCTION__)) {
139 return nullValue();139 return nullValue();
140 }140 }
@@ -375,10 +375,13 @@ ScriptEngineV8::ScriptEngineV8(ScriptManager* scriptManager) : · 234 unmodified lines
375 //_arrayBufferClass(new ArrayBufferClass(this))375 //_arrayBufferClass(new ArrayBufferClass(this))
376{376{
377 _v8InitMutex.lock();377 _v8InitMutex.lock();
378 std::call_once ( _v8InitOnceFlag, [ ]{ 378 std::call_once ( _v8InitOnceFlag, [ ]{
379 v8::V8::InitializeExternalStartupData("");
380 //V8TODO might cause crashes if it's too much
381 v8::V8::SetFlagsFromString("--stack-size=900000");
379 v8::Platform* platform = getV8Platform();382 v8::Platform* platform = getV8Platform();
380 v8::V8::InitializePlatform(platform);383 v8::V8::InitializePlatform(platform);
381 v8::V8::Initialize();384 v8::V8::Initialize(); qCDebug(scriptengine) << "V8 platform initialized";
382 } );385 } );
383 _v8InitMutex.unlock();386 _v8InitMutex.unlock();
384 {387 {
@@ -391,7 +394,8 @@ ScriptEngineV8::ScriptEngineV8(ScriptManager* scriptManager) : · 6 unmodified lines
391 Q_ASSERT(!context.IsEmpty());394 Q_ASSERT(!context.IsEmpty());
392 v8::Context::Scope contextScope(context);395 v8::Context::Scope contextScope(context);
393 _v8Context = v8::UniquePersistent<v8::Context>(_v8Isolate, context);396 _v8Context = v8::UniquePersistent<v8::Context>(_v8Isolate, context);
394 397
398
395 V8ScriptValue nullScriptValue(_v8Isolate, v8::Null(_v8Isolate));399 V8ScriptValue nullScriptValue(_v8Isolate, v8::Null(_v8Isolate));
396 _nullValue = ScriptValue(new ScriptValueV8Wrapper(this, nullScriptValue));400 _nullValue = ScriptValue(new ScriptValueV8Wrapper(this, nullScriptValue));
397 401
@@ -427,7 +431,7 @@ ScriptEngineV8::ScriptEngineV8(ScriptManager* scriptManager) : · 29 unmodified lines
427 }431 }
428 }, Qt::DirectConnection);*/432 }, Qt::DirectConnection);*/
429 //moveToThread(scriptManager->thread());433 //moveToThread(scriptManager->thread());
430 setThread(scriptManager->thread());434 //setThread(scriptManager->thread());
431 }435 }
432}436}
433 437
@@ -789,6 +793,7 @@ ScriptValue ScriptEngineV8::evaluate(const QString& sourceCode, const QString& f · 355 unmodified lines
789 }793 }
790 // Compile and check syntax794 // Compile and check syntax
791 // V8TODO: Could these all be replaced with checkSyntax function from wrapper?795 // V8TODO: Could these all be replaced with checkSyntax function from wrapper?
796 Q_ASSERT(!_v8Isolate->IsDead());
792 v8::HandleScope handleScope(_v8Isolate);797 v8::HandleScope handleScope(_v8Isolate);
793 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));798 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
794 v8::TryCatch tryCatch(getIsolate());799 v8::TryCatch tryCatch(getIsolate());
@@ -799,7 +804,8 @@ ScriptValue ScriptEngineV8::evaluate(const QString& sourceCode, const QString& f · 4 unmodified lines
799 int errorLineNumber = 0;804 int errorLineNumber = 0;
800 QString errorMessage = "";805 QString errorMessage = "";
801 QString errorBacktrace = "";806 QString errorBacktrace = "";
802 v8::String::Utf8Value utf8Value(getIsolate(), tryCatch.Exception());807 //v8::String::Utf8Value utf8Value(getIsolate(), tryCatch.Exception());
808 v8::String::Utf8Value utf8Value(getIsolate(), tryCatch.Message()->Get());
803 errorMessage = QString(*utf8Value);809 errorMessage = QString(*utf8Value);
804 v8::Local<v8::Message> exceptionMessage = tryCatch.Message();810 v8::Local<v8::Message> exceptionMessage = tryCatch.Message();
805 if (!exceptionMessage.IsEmpty()) {811 if (!exceptionMessage.IsEmpty()) {
@@ -811,12 +817,14 @@ ScriptValue ScriptEngineV8::evaluate(const QString& sourceCode, const QString& f · 5 unmodified lines
811 v8::String::Utf8Value backtraceUtf8Value(getIsolate(), backtraceV8String);817 v8::String::Utf8Value backtraceUtf8Value(getIsolate(), backtraceV8String);
812 errorBacktrace = *backtraceUtf8Value;818 errorBacktrace = *backtraceUtf8Value;
813 }819 }
820 qCDebug(scriptengine) << "Compiling script \"" << fileName << "\" failed on line " << errorLineNumber << " column " << errorColumnNumber << " with message: \"" << errorMessage <<"\" backtrace: " << errorBacktrace;
814 }821 }
815 auto err = makeError(newValue(errorMessage));822 auto err = makeError(newValue(errorMessage));
816 raiseException(err);823 raiseException(err);
817 maybeEmitUncaughtException("compile");824 maybeEmitUncaughtException("compile");
818 return err;825 return err;
819 }826 }
827 qCDebug(scriptengine) << "Script compilation succesful: " << fileName;
820 828
821 //V8TODO829 //V8TODO
822 /*auto syntaxError = lintScript(sourceCode, fileName);830 /*auto syntaxError = lintScript(sourceCode, fileName);
@@ -1170,16 +1178,20 @@ QThread* ScriptEngineV8::thread() const { · 347 unmodified lines
1170}1178}
1171 1179
1172void ScriptEngineV8::setThread(QThread* thread) {1180void ScriptEngineV8::setThread(QThread* thread) {
1173 qDebug() << "Moved script engine " << objectName() << " to different thread";1181 if (_v8Isolate->IsCurrent()) {
1174 _v8Isolate->Exit();1182 _v8Isolate->Exit();
1183 qDebug() << "Script engine " << objectName() << " exited isolate";
1184 }
1175 moveToThread(thread);1185 moveToThread(thread);
1176 QMetaObject::invokeMethod(this, "enterIsolateOnThisThread");1186 qDebug() << "Moved script engine " << objectName() << " to different thread";
1177}1187}
1178 1188
1179void ScriptEngineV8::enterIsolateOnThisThread() {1189void ScriptEngineV8::enterIsolateOnThisThread() {
1180 Q_ASSERT(thread() == QThread::currentThread());1190 Q_ASSERT(thread() == QThread::currentThread());
1181 _v8Isolate->Enter();1191 if (!_v8Isolate->IsCurrent()) {
1182 qDebug() << "Script engine " << objectName() << " entered isolate on a new thread";1192 _v8Isolate->Enter();
1193 qDebug() << "Script engine " << objectName() << " entered isolate on a new thread";
1194 }
1183}1195}
1184 1196
1185 1197
@@ -1238,3 +1250,18 @@ QVariant ScriptEngineV8::convert(const ScriptValue& value, int typeId) { · 52 unmodified lines
1238 return var;1250 return var;
1239 return QVariant();1251 return QVariant();
1240}1252}
1253
1254void ScriptEngineV8::compileTest() {
1255 v8::Locker locker(_v8Isolate);
1256 v8::Isolate::Scope isolateScope(_v8Isolate);
1257 v8::HandleScope handleScope(_v8Isolate);
1258 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
1259 v8::Local<v8::Script> script;
1260 v8::ScriptOrigin scriptOrigin(getIsolate(), v8::String::NewFromUtf8(getIsolate(),"test").ToLocalChecked());
1261 if (v8::Script::Compile(getContext(), v8::String::NewFromUtf8(getIsolate(), "print(\"hello world\");").ToLocalChecked(), &scriptOrigin).ToLocal(&script)) {
1262 qCDebug(scriptengine) << "Compile test succesful";
1263 } else {
1264 qCDebug(scriptengine) << "Compile test failed";
1265 Q_ASSERT(false);
1266 }
1267}

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