History

Added context scopes

ksuprynowicz committed 4 years ago

Commit a4c69301 parent 2b06c0910 files · +300 −95Browse files

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

Unified · Split

libraries/script-engine/src/v8/ScriptEngineV8.cpp+106−45
@@ -113,6 +113,8 @@ ScriptValue ScriptEngineV8::makeError(const ScriptValue& _other, const QString& · 112 unmodified lines
113113 if (!IS_THREADSAFE_INVOCATION(thread(), __FUNCTION__)) {
114114 return nullValue();
115115 }
116+ v8::HandleScope handleScope(_v8Isolate);
117+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
116118 return nullValue();
117119
118120 //V8TODO
@@ -136,6 +138,8 @@ ScriptValue ScriptEngineV8::cheskScriptSyntax(ScriptProgramPointer program) { · 17 unmodified lines
136138 if (!IS_THREADSAFE_INVOCATION(thread(), __FUNCTION__)) {
137139 return nullValue();
138140 }
141+ v8::HandleScope handleScope(_v8Isolate);
142+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
139143 ScriptSyntaxCheckResultPointer syntaxCheck = program->checkSyntax();
140144 //V8TODO
141145 if (syntaxCheck->state() != ScriptSyntaxCheckResult::Valid) {
@@ -190,6 +194,8 @@ ScriptValue ScriptEngineV8::cloneUncaughtException(const QString& extraDetail) { · 48 unmodified lines
190194 if (!hasUncaughtException()) {
191195 return nullValue();
192196 }
197+ v8::HandleScope handleScope(_v8Isolate);
198+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
193199 return nullValue();
194200 //V8TODO
195201 /*
@@ -284,6 +290,8 @@ bool ScriptEngineV8::maybeEmitUncaughtException(const QString& debugHint) { · 88 unmodified lines
284290 ScriptValue ScriptEngineV8::newLambdaFunction(std::function<V8ScriptValue(V8ScriptContext*, ScriptEngineV8*)> operation,
285291 const V8ScriptValue& data,
286292 const ValueOwnership& ownership) {
293+ v8::HandleScope handleScope(_v8Isolate);
294+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
287295 auto lambda = new Lambda(this, operation, data);
288296 auto object = newQObject(lambda, ownership);
289297 //V8TODO?
@@ -294,6 +302,7 @@ ScriptValue ScriptEngineV8::newLambdaFunction(std::function<V8ScriptValue(V8Scri · 4 unmodified lines
294302 }
295303 QString Lambda::toString() const {
296304 v8::HandleScope handleScope(_engine->getIsolate());
305+ v8::Context::Scope contextScope(_engine->getContext());
297306 v8::Local<v8::String> string;
298307 QString qString("");
299308 if (_data.constGet()->ToString(_engine->getContext()).ToLocal(&string)) {
@@ -373,30 +382,14 @@ ScriptEngineV8::ScriptEngineV8(ScriptManager* scriptManager) : · 73 unmodified lines
373382 v8::Isolate::CreateParams isolateParams;
374383 isolateParams.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
375384 _v8Isolate = v8::Isolate::New(isolateParams);
385+ _v8Isolate->Enter();
376386 v8::HandleScope handleScope(_v8Isolate);
377387 v8::Local<v8::Context> context = v8::Context::New(_v8Isolate);
388+ Q_ASSERT(!context.IsEmpty());
378389 _v8Context = v8::UniquePersistent<v8::Context>(_v8Isolate, context);
379390
380391 registerSystemTypes();
381392
382− _currentThread = QThread::currentThread();
383− // V8TODO: port to V8
384− /*
385− if (_scriptManager) {
386− connect(this, &QScriptEngine::signalHandlerException, this, [this](const V8ScriptValue& exception) {
387− if (hasUncaughtException()) {
388− // the engine's uncaughtException() seems to produce much better stack traces here
389− emit _scriptManager->unhandledException(cloneUncaughtException("signalHandlerException"));
390− clearExceptions();
391− } else {
392− // ... but may not always be available -- so if needed we fallback to the passed exception
393− V8ScriptValue thrown = makeError(exception);
394− emit _scriptManager->unhandledException(ScriptValue(new ScriptValueV8Wrapper(this, std::move(thrown))));
395− }
396− }, Qt::DirectConnection);
397− moveToThread(scriptManager->thread());
398− }*/
399−
400393 // V8TODO: dispose of isolate on ScriptEngineV8 destruction
401394 //v8::UniquePersistent<v8::Value> null = v8::UniquePersistent<v8::Value>(_v8Isolate, v8::Null(_v8Isolate));
402395 //_nullValue = ScriptValue(new ScriptValueV8Wrapper(this, std::move(null)));
@@ -410,6 +403,25 @@ ScriptEngineV8::ScriptEngineV8(ScriptManager* scriptManager) : · 7 unmodified lines
410403
411404 // V8TODO:
412405 //QScriptEngine::setProcessEventsInterval(MSECS_PER_SECOND);
406+
407+ //_currentThread = QThread::currentThread();
408+
409+ if (_scriptManager) {
410+ // V8TODO: port to V8
411+ /*connect(this, &QScriptEngine::signalHandlerException, this, [this](const V8ScriptValue& exception) {
412+ if (hasUncaughtException()) {
413+ // the engine's uncaughtException() seems to produce much better stack traces here
414+ emit _scriptManager->unhandledException(cloneUncaughtException("signalHandlerException"));
415+ clearExceptions();
416+ } else {
417+ // ... but may not always be available -- so if needed we fallback to the passed exception
418+ V8ScriptValue thrown = makeError(exception);
419+ emit _scriptManager->unhandledException(ScriptValue(new ScriptValueV8Wrapper(this, std::move(thrown))));
420+ }
421+ }, Qt::DirectConnection);*/
422+ //moveToThread(scriptManager->thread());
423+ setThread(scriptManager->thread());
424+ }
413425 }
414426
415427 void ScriptEngineV8::registerEnum(const QString& enumName, QMetaEnum newEnum) {
@@ -417,6 +429,8 @@ void ScriptEngineV8::registerEnum(const QString& enumName, QMetaEnum newEnum) { · 1 unmodified line
417429 qCCritical(scriptengine) << "registerEnum called on invalid enum with name " << enumName;
418430 return;
419431 }
432+ v8::HandleScope handleScope(_v8Isolate);
433+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
420434
421435 for (int i = 0; i < newEnum.keyCount(); i++) {
422436 const char* keyName = newEnum.key(i);
@@ -426,8 +440,7 @@ void ScriptEngineV8::registerEnum(const QString& enumName, QMetaEnum newEnum) { · 3 unmodified lines
426440 }
427441
428442 void ScriptEngineV8::registerValue(const QString& valueName, V8ScriptValue value) {
429− //V8TODO
430− /*if (QThread::currentThread() != QScriptEngine::thread()) {
443+ if (QThread::currentThread() != thread()) {
431444 #ifdef THREAD_DEBUGGING
432445 qCDebug(scriptengine) << "*** WARNING *** ScriptEngineV8::registerValue() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "]";
433446 #endif
@@ -435,8 +448,9 @@ void ScriptEngineV8::registerValue(const QString& valueName, V8ScriptValue value · 1 unmodified line
435448 Q_ARG(const QString&, valueName),
436449 Q_ARG(V8ScriptValue, value));
437450 return;
438− }*/
451+ }
439452 v8::HandleScope handleScope(_v8Isolate);
453+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
440454 QStringList pathToValue = valueName.split(".");
441455 int partsToGo = pathToValue.length();
442456 v8::Local<v8::Object> partObject = _v8Context.Get(_v8Isolate)->Global();
@@ -470,8 +484,7 @@ void ScriptEngineV8::registerValue(const QString& valueName, V8ScriptValue value · 27 unmodified lines
470484 }
471485
472486 void ScriptEngineV8::registerGlobalObject(const QString& name, QObject* object) {
473− //V8TODO
474− /*if (QThread::currentThread() != QScriptEngine::thread()) {
487+ if (QThread::currentThread() != thread()) {
475488 #ifdef THREAD_DEBUGGING
476489 qCDebug(scriptengine) << "*** WARNING *** ScriptEngineV8::registerGlobalObject() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] name:" << name;
477490 #endif
@@ -482,8 +495,10 @@ void ScriptEngineV8::registerGlobalObject(const QString& name, QObject* object) · 4 unmodified lines
482495 }
483496 #ifdef THREAD_DEBUGGING
484497 qCDebug(scriptengine) << "ScriptEngineV8::registerGlobalObject() called on thread [" << QThread::currentThread() << "] name:" << name;
485−#endif*/
498+#endif
486499 v8::HandleScope handleScope(_v8Isolate);
500+ Q_ASSERT(_v8Isolate->IsCurrent());
501+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
487502 v8::Local<v8::Object> v8GlobalObject = getContext()->Global();
488503 v8::Local<v8::String> v8Name = v8::String::NewFromUtf8(_v8Isolate, name.toStdString().c_str()).ToLocalChecked();
489504
@@ -499,22 +514,23 @@ void ScriptEngineV8::registerGlobalObject(const QString& name, QObject* object) · 9 unmodified lines
499514 }
500515
501516 void ScriptEngineV8::registerFunction(const QString& name, ScriptEngine::FunctionSignature functionSignature, int numArguments) {
502− //V8TODO
503− /*if (QThread::currentThread() != QScriptEngine::thread()) {
517+ if (QThread::currentThread() != thread()) {
504518 #ifdef THREAD_DEBUGGING
505519 qCDebug(scriptengine) << "*** WARNING *** ScriptEngineV8::registerFunction() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] name:" << name;
506520 #endif
507521 QMetaObject::invokeMethod(this, "registerFunction",
508522 Q_ARG(const QString&, name),
509− Q_ARG(QScriptEngine::FunctionSignature, functionSignature),
523+ Q_ARG(ScriptEngine::FunctionSignature, functionSignature),
510524 Q_ARG(int, numArguments));
511525 return;
512526 }
513527 #ifdef THREAD_DEBUGGING
514528 qCDebug(scriptengine) << "ScriptEngineV8::registerFunction() called on thread [" << QThread::currentThread() << "] name:" << name;
515−#endif*/
529+#endif
516530
517531 //auto scriptFun = static_cast<ScriptValueV8Wrapper*>(newFunction(functionSignature, numArguments).ptr())->toV8Value().constGet();
532+ v8::HandleScope handleScope(_v8Isolate);
533+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
518534 auto scriptFun = newFunction(functionSignature, numArguments);
519535
520536 //getContext()->Global().Set();
@@ -522,21 +538,22 @@ void ScriptEngineV8::registerFunction(const QString& name, ScriptEngine::Functio · 1 unmodified line
522538 }
523539
524540 void ScriptEngineV8::registerFunction(const QString& parent, const QString& name, ScriptEngine::FunctionSignature functionSignature, int numArguments) {
525− //V8TODO
526− /*if (QThread::currentThread() != QScriptEngine::thread()) {
541+ if (QThread::currentThread() != thread()) {
527542 #ifdef THREAD_DEBUGGING
528543 qCDebug(scriptengine) << "*** WARNING *** ScriptEngineV8::registerFunction() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] parent:" << parent << "name:" << name;
529544 #endif
530545 QMetaObject::invokeMethod(this, "registerFunction",
531546 Q_ARG(const QString&, name),
532− Q_ARG(QScriptEngine::FunctionSignature, functionSignature),
547+ Q_ARG(ScriptEngine::FunctionSignature, functionSignature),
533548 Q_ARG(int, numArguments));
534549 return;
535550 }
536551 #ifdef THREAD_DEBUGGING
537552 qCDebug(scriptengine) << "ScriptEngineV8::registerFunction() called on thread [" << QThread::currentThread() << "] parent:" << parent << "name:" << name;
538−#endif*/
553+#endif
539554
555+ v8::HandleScope handleScope(_v8Isolate);
556+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
540557 ScriptValue object = globalObject().property(parent);
541558 if (object.isValid()) {
542559 ScriptValue scriptFun = ScriptEngine::newFunction(functionSignature, numArguments);
@@ -546,8 +563,7 @@ void ScriptEngineV8::registerFunction(const QString& parent, const QString& name · 3 unmodified lines
546563
547564 void ScriptEngineV8::registerGetterSetter(const QString& name, ScriptEngine::FunctionSignature getter,
548565 ScriptEngine::FunctionSignature setter, const QString& parent) {
549− //V8TODO
550− /*if (QThread::currentThread() != QScriptEngine::thread()) {
566+ if (QThread::currentThread() != thread()) {
551567 #ifdef THREAD_DEBUGGING
552568 qCDebug(scriptengine) << "*** WARNING *** ScriptEngineV8::registerGetterSetter() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] "
553569 " name:" << name << "parent:" << parent;
@@ -561,7 +577,10 @@ void ScriptEngineV8::registerGetterSetter(const QString& name, ScriptEngine::Fun · 7 unmodified lines
561577 }
562578 #ifdef THREAD_DEBUGGING
563579 qCDebug(scriptengine) << "ScriptEngineV8::registerGetterSetter() called on thread [" << QThread::currentThread() << "] name:" << name << "parent:" << parent;
564−#endif*/
580+#endif
581+
582+ v8::HandleScope handleScope(_v8Isolate);
583+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
565584
566585 ScriptValue setterFunction = newFunction(setter, 1);
567586 ScriptValue getterFunction = newFunction(getter);
@@ -585,6 +604,7 @@ ScriptValue ScriptEngineV8::evaluateInClosure(const ScriptValue& _closure, · 17 unmodified lines
585604 return nullValue();
586605 }
587606 v8::HandleScope handleScope(_v8Isolate);
607+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
588608 ScriptProgramV8Wrapper* unwrappedProgram = ScriptProgramV8Wrapper::unwrap(_program);
589609 if (unwrappedProgram == nullptr) {
590610 return nullValue();
@@ -684,7 +704,7 @@ ScriptValue ScriptEngineV8::evaluateInClosure(const ScriptValue& _closure, · 93 unmodified lines
684704 setGlobalObject(oldGlobal);
685705 }*/
686706
687− _v8Context.Get(_v8Isolate)->Enter();
707+ //_v8Context.Get(_v8Isolate)->Enter();
688708 return result;
689709 }
690710
@@ -695,7 +715,7 @@ ScriptValue ScriptEngineV8::evaluate(const QString& sourceCode, const QString& f · 4 unmodified lines
695715
696716 //V8TODO
697717
698− /*if (QThread::currentThread() != QScriptEngine::thread()) {
718+ if (QThread::currentThread() != thread()) {
699719 ScriptValue result;
700720 #ifdef THREAD_DEBUGGING
701721 qCDebug(scriptengine) << "*** WARNING *** ScriptEngineV8::evaluate() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] "
@@ -706,10 +726,11 @@ ScriptValue ScriptEngineV8::evaluate(const QString& sourceCode, const QString& f · 4 unmodified lines
706726 Q_ARG(const QString&, sourceCode),
707727 Q_ARG(const QString&, fileName));
708728 return result;
709− }*/
729+ }
710730 // Compile and check syntax
711731 // V8TODO: Could these all be replaced with checkSyntax function from wrapper?
712732 v8::HandleScope handleScope(_v8Isolate);
733+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
713734 v8::TryCatch tryCatch(getIsolate());
714735 v8::ScriptOrigin scriptOrigin(getIsolate(), v8::String::NewFromUtf8(getIsolate(), fileName.toStdString().c_str()).ToLocalChecked());
715736 v8::Local<v8::Script> script;
@@ -775,8 +796,7 @@ Q_INVOKABLE ScriptValue ScriptEngineV8::evaluate(const ScriptProgramPointer& pro · 59 unmodified lines
775796 return undefinedValue(); // bail early
776797 }
777798
778− //V8TODO
779− /*if (QThread::currentThread() != QScriptEngine::thread()) {
799+ if (QThread::currentThread() != thread()) {
780800 ScriptValue result;
781801 #ifdef THREAD_DEBUGGING
782802 qCDebug(scriptengine) << "*** WARNING *** ScriptEngineV8::evaluate() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] "
@@ -786,8 +806,9 @@ Q_INVOKABLE ScriptValue ScriptEngineV8::evaluate(const ScriptProgramPointer& pro · 3 unmodified lines
786806 Q_RETURN_ARG(ScriptValue, result),
787807 Q_ARG(const ScriptProgramPointer&, program));
788808 return result;
789− }*/
809+ }
790810 v8::HandleScope handleScope(_v8Isolate);
811+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
791812 ScriptProgramV8Wrapper* unwrapped = ScriptProgramV8Wrapper::unwrap(program);
792813 if (!unwrapped) {
793814 auto err = makeError(newValue("could not unwrap program"));
@@ -843,6 +864,7 @@ void ScriptEngineV8::updateMemoryCost(const qint64& deltaSize) { · 49 unmodified lines
843864
844865 ScriptValue ScriptEngineV8::globalObject() const {
845866 v8::HandleScope handleScope(_v8Isolate);
867+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
846868 V8ScriptValue global(_v8Isolate, getConstContext()->Global());// = QScriptEngine::globalObject(); // can't cache the value as it may change
847869 return ScriptValue(new ScriptValueV8Wrapper(const_cast<ScriptEngineV8*>(this), std::move(global)));
848870 }
@@ -852,12 +874,15 @@ ScriptManager* ScriptEngineV8::manager() const { · 3 unmodified lines
852874 }
853875
854876 ScriptValue ScriptEngineV8::newArray(uint length) {
877+ v8::HandleScope handleScope(_v8Isolate);
878+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
855879 V8ScriptValue result(_v8Isolate, v8::Array::New(_v8Isolate, static_cast<int>(length)));
856880 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
857881 }
858882
859883 ScriptValue ScriptEngineV8::newArrayBuffer(const QByteArray& message) {
860884 v8::HandleScope handleScope(_v8Isolate);
885+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
861886 //V8TODO: this will leak memory
862887 std::shared_ptr<v8::BackingStore> backingStore(v8::ArrayBuffer::NewBackingStore(_v8Isolate, message.size()));
863888 std::memcpy(backingStore.get()->Data(), message.constData(), message.size());
@@ -873,12 +898,16 @@ ScriptValue ScriptEngineV8::newArrayBuffer(const QByteArray& message) { · 9 unmodified lines
873898 }
874899
875900 ScriptValue ScriptEngineV8::newObject() {
901+ v8::HandleScope handleScope(_v8Isolate);
902+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
876903 V8ScriptValue result(_v8Isolate, v8::Object::New(_v8Isolate));
877904 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
878905 }
879906
880907 ScriptValue ScriptEngineV8::newMethod(QObject* object, V8ScriptValue lifetime,
881908 const QList<QMetaMethod>& metas, int numMaxParams) {
909+ v8::HandleScope handleScope(_v8Isolate);
910+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
882911 V8ScriptValue result(ScriptMethodV8Proxy::newMethod(this, object, lifetime, metas, numMaxParams));
883912 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
884913 }
@@ -893,33 +922,43 @@ ScriptProgramPointer ScriptEngineV8::newProgram(const QString& sourceCode, const · 8 unmodified lines
893922 ScriptValue ScriptEngineV8::newQObject(QObject* object,
894923 ScriptEngine::ValueOwnership ownership,
895924 const ScriptEngine::QObjectWrapOptions& options) {
925+ v8::HandleScope handleScope(_v8Isolate);
926+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
896927 V8ScriptValue result = ScriptObjectV8Proxy::newQObject(this, object, ownership, options);
897928 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
898929 }
899930
900931 ScriptValue ScriptEngineV8::newValue(bool value) {
932+ v8::HandleScope handleScope(_v8Isolate);
933+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
901934 V8ScriptValue result(_v8Isolate, v8::Boolean::New(_v8Isolate, value));
902935 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
903936 }
904937
905938 ScriptValue ScriptEngineV8::newValue(int value) {
939+ v8::HandleScope handleScope(_v8Isolate);
940+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
906941 V8ScriptValue result(_v8Isolate, v8::Integer::New(_v8Isolate, value));
907942 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
908943 }
909944
910945 ScriptValue ScriptEngineV8::newValue(uint value) {
946+ v8::HandleScope handleScope(_v8Isolate);
947+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
911948 V8ScriptValue result(_v8Isolate, v8::Uint32::New(_v8Isolate, value));
912949 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
913950 }
914951
915952 ScriptValue ScriptEngineV8::newValue(double value) {
916953 v8::HandleScope handleScope(_v8Isolate);
954+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
917955 V8ScriptValue result(_v8Isolate, v8::Number::New(_v8Isolate, value));
918956 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
919957 }
920958
921959 ScriptValue ScriptEngineV8::newValue(const QString& value) {
922960 v8::HandleScope handleScope(_v8Isolate);
961+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
923962 v8::Local<v8::String> valueV8 = v8::String::NewFromUtf8(_v8Isolate, value.toStdString().c_str(), v8::NewStringType::kNormal).ToLocalChecked();
924963 V8ScriptValue result(_v8Isolate, valueV8);
925964 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
@@ -927,6 +966,7 @@ ScriptValue ScriptEngineV8::newValue(const QString& value) { · 1 unmodified line
927966
928967 ScriptValue ScriptEngineV8::newValue(const QLatin1String& value) {
929968 v8::HandleScope handleScope(_v8Isolate);
969+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
930970 v8::Local<v8::String> valueV8 = v8::String::NewFromUtf8(_v8Isolate, value.latin1(), v8::NewStringType::kNormal).ToLocalChecked();
931971 V8ScriptValue result(_v8Isolate, valueV8);
932972 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
@@ -934,12 +974,15 @@ ScriptValue ScriptEngineV8::newValue(const QLatin1String& value) { · 1 unmodified line
934974
935975 ScriptValue ScriptEngineV8::newValue(const char* value) {
936976 v8::HandleScope handleScope(_v8Isolate);
977+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
937978 v8::Local<v8::String> valueV8 = v8::String::NewFromUtf8(_v8Isolate, value, v8::NewStringType::kNormal).ToLocalChecked();
938979 V8ScriptValue result(_v8Isolate, valueV8);
939980 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
940981 }
941982
942983 ScriptValue ScriptEngineV8::newVariant(const QVariant& value) {
984+ v8::HandleScope handleScope(_v8Isolate);
985+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
943986 V8ScriptValue result = castVariantToValue(value);
944987 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
945988 }
@@ -1005,12 +1048,14 @@ ScriptValue ScriptEngineV8::newFunction(ScriptEngine::FunctionSignature fun, int · 59 unmodified lines
10051048 //V8TODO: is using GetCurrentContext ok, or context wrapper needs to be added?
10061049 v8::HandleScope handleScope(info.GetIsolate());
10071050 auto context = info.GetIsolate()->GetCurrentContext();
1051+ v8::Context::Scope contextScope(context);
1052+ auto object = v8::Local<v8::Object>::Cast(info.Data());
10081053 auto function = reinterpret_cast<ScriptEngine::FunctionSignature>
1009− (info.Data()->ToObject(context).ToLocalChecked()->GetAlignedPointerFromInternalField(0));
1054+ (object->GetAlignedPointerFromInternalField(0));
10101055 ScriptContext *scriptContext = reinterpret_cast<ScriptContext*>
1011− (info.Data()->ToObject(context).ToLocalChecked()->GetAlignedPointerFromInternalField(1));;
1056+ (object->GetAlignedPointerFromInternalField(1));;
10121057 ScriptEngineV8 *scriptEngine = reinterpret_cast<ScriptEngineV8*>
1013− (info.Data()->ToObject(context).ToLocalChecked()->GetAlignedPointerFromInternalField(2));;
1058+ (object->GetAlignedPointerFromInternalField(2));;
10141059 ScriptValue result = function(scriptContext, scriptEngine);
10151060 ScriptValueV8Wrapper* unwrapped = ScriptValueV8Wrapper::unwrap(result);
10161061 info.GetReturnValue().Set(unwrapped->toV8Value().constGet());
@@ -1019,6 +1064,7 @@ ScriptValue ScriptEngineV8::newFunction(ScriptEngine::FunctionSignature fun, int · 2 unmodified lines
10191064 //auto functionData = v8::Object::New(_v8Isolate);
10201065 //functionData->setIn
10211066 v8::HandleScope handleScope(_v8Isolate);
1067+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
10221068 auto functionDataTemplate = v8::ObjectTemplate::New(_v8Isolate);
10231069 functionDataTemplate->SetInternalFieldCount(3);
10241070 auto functionData = functionDataTemplate->NewInstance(getContext()).ToLocalChecked();
@@ -1044,6 +1090,7 @@ void ScriptEngineV8::setObjectName(const QString& name) { · 19 unmodified lines
10441090 //V8TODO
10451091 bool ScriptEngineV8::setProperty(const char* name, const QVariant& value) {
10461092 v8::HandleScope handleScope(_v8Isolate);
1093+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
10471094 v8::Local<v8::Object> global = getContext()->Global();
10481095 auto v8Name = v8::String::NewFromUtf8(getIsolate(), name).ToLocalChecked();
10491096 V8ScriptValue v8Value = castVariantToValue(value);
@@ -1056,13 +1103,23 @@ void ScriptEngineV8::setProcessEventsInterval(int interval) { · 6 unmodified lines
10561103 }
10571104
10581105 QThread* ScriptEngineV8::thread() const {
1059− return _currentThread;
1106+ return QObject::thread();
10601107 }
10611108
10621109 void ScriptEngineV8::setThread(QThread* thread) {
1110+ qDebug() << "Moved script engine " << objectName() << " to different thread";
1111+ _v8Isolate->Exit();
10631112 moveToThread(thread);
1113+ QMetaObject::invokeMethod(this, "enterIsolateOnThisThread");
1114+}
1115+
1116+void ScriptEngineV8::enterIsolateOnThisThread() {
1117+ Q_ASSERT(thread() == QThread::currentThread());
1118+ _v8Isolate->Enter();
1119+ qDebug() << "Script engine " << objectName() << " entered isolate on a new thread";
10641120 }
10651121
1122+
10661123 ScriptValue ScriptEngineV8::uncaughtException() const {
10671124 //V8TODO
10681125 //V8ScriptValue result = QScriptEngine::uncaughtException();
@@ -1088,12 +1145,16 @@ bool ScriptEngineV8::raiseException(const ScriptValue& exception) { · 19 unmodified lines
10881145 }
10891146
10901147 ScriptValue ScriptEngineV8::create(int type, const void* ptr) {
1148+ v8::HandleScope handleScope(_v8Isolate);
1149+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
10911150 QVariant variant(type, ptr);
10921151 V8ScriptValue scriptValue = castVariantToValue(variant);
10931152 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(scriptValue)));
10941153 }
10951154
10961155 QVariant ScriptEngineV8::convert(const ScriptValue& value, int typeId) {
1156+ v8::HandleScope handleScope(_v8Isolate);
1157+ v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
10971158 ScriptValueV8Wrapper* unwrapped = ScriptValueV8Wrapper::unwrap(value);
10981159 if (unwrapped == nullptr) {
10991160 return QVariant();

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