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
113 if (!IS_THREADSAFE_INVOCATION(thread(), __FUNCTION__)) {113 if (!IS_THREADSAFE_INVOCATION(thread(), __FUNCTION__)) {
114 return nullValue();114 return nullValue();
115 }115 }
116 v8::HandleScope handleScope(_v8Isolate);
117 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
116 return nullValue();118 return nullValue();
117 119
118 //V8TODO120 //V8TODO
@@ -136,6 +138,8 @@ ScriptValue ScriptEngineV8::cheskScriptSyntax(ScriptProgramPointer program) { · 17 unmodified lines
136 if (!IS_THREADSAFE_INVOCATION(thread(), __FUNCTION__)) {138 if (!IS_THREADSAFE_INVOCATION(thread(), __FUNCTION__)) {
137 return nullValue();139 return nullValue();
138 }140 }
141 v8::HandleScope handleScope(_v8Isolate);
142 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
139 ScriptSyntaxCheckResultPointer syntaxCheck = program->checkSyntax();143 ScriptSyntaxCheckResultPointer syntaxCheck = program->checkSyntax();
140 //V8TODO144 //V8TODO
141 if (syntaxCheck->state() != ScriptSyntaxCheckResult::Valid) {145 if (syntaxCheck->state() != ScriptSyntaxCheckResult::Valid) {
@@ -190,6 +194,8 @@ ScriptValue ScriptEngineV8::cloneUncaughtException(const QString& extraDetail) { · 48 unmodified lines
190 if (!hasUncaughtException()) {194 if (!hasUncaughtException()) {
191 return nullValue();195 return nullValue();
192 }196 }
197 v8::HandleScope handleScope(_v8Isolate);
198 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
193 return nullValue();199 return nullValue();
194 //V8TODO200 //V8TODO
195 /*201 /*
@@ -284,6 +290,8 @@ bool ScriptEngineV8::maybeEmitUncaughtException(const QString& debugHint) { · 88 unmodified lines
284ScriptValue ScriptEngineV8::newLambdaFunction(std::function<V8ScriptValue(V8ScriptContext*, ScriptEngineV8*)> operation,290ScriptValue ScriptEngineV8::newLambdaFunction(std::function<V8ScriptValue(V8ScriptContext*, ScriptEngineV8*)> operation,
285 const V8ScriptValue& data,291 const V8ScriptValue& data,
286 const ValueOwnership& ownership) {292 const ValueOwnership& ownership) {
293 v8::HandleScope handleScope(_v8Isolate);
294 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
287 auto lambda = new Lambda(this, operation, data);295 auto lambda = new Lambda(this, operation, data);
288 auto object = newQObject(lambda, ownership);296 auto object = newQObject(lambda, ownership);
289 //V8TODO?297 //V8TODO?
@@ -294,6 +302,7 @@ ScriptValue ScriptEngineV8::newLambdaFunction(std::function<V8ScriptValue(V8Scri · 4 unmodified lines
294}302}
295QString Lambda::toString() const {303QString Lambda::toString() const {
296 v8::HandleScope handleScope(_engine->getIsolate());304 v8::HandleScope handleScope(_engine->getIsolate());
305 v8::Context::Scope contextScope(_engine->getContext());
297 v8::Local<v8::String> string;306 v8::Local<v8::String> string;
298 QString qString("");307 QString qString("");
299 if (_data.constGet()->ToString(_engine->getContext()).ToLocal(&string)) {308 if (_data.constGet()->ToString(_engine->getContext()).ToLocal(&string)) {
@@ -373,30 +382,14 @@ ScriptEngineV8::ScriptEngineV8(ScriptManager* scriptManager) : · 73 unmodified lines
373 v8::Isolate::CreateParams isolateParams;382 v8::Isolate::CreateParams isolateParams;
374 isolateParams.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();383 isolateParams.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
375 _v8Isolate = v8::Isolate::New(isolateParams);384 _v8Isolate = v8::Isolate::New(isolateParams);
385 _v8Isolate->Enter();
376 v8::HandleScope handleScope(_v8Isolate);386 v8::HandleScope handleScope(_v8Isolate);
377 v8::Local<v8::Context> context = v8::Context::New(_v8Isolate);387 v8::Local<v8::Context> context = v8::Context::New(_v8Isolate);
388 Q_ASSERT(!context.IsEmpty());
378 _v8Context = v8::UniquePersistent<v8::Context>(_v8Isolate, context);389 _v8Context = v8::UniquePersistent<v8::Context>(_v8Isolate, context);
379 390
380 registerSystemTypes();391 registerSystemTypes();
381 392
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
400 // V8TODO: dispose of isolate on ScriptEngineV8 destruction393 // V8TODO: dispose of isolate on ScriptEngineV8 destruction
401 //v8::UniquePersistent<v8::Value> null = v8::UniquePersistent<v8::Value>(_v8Isolate, v8::Null(_v8Isolate));394 //v8::UniquePersistent<v8::Value> null = v8::UniquePersistent<v8::Value>(_v8Isolate, v8::Null(_v8Isolate));
402 //_nullValue = ScriptValue(new ScriptValueV8Wrapper(this, std::move(null)));395 //_nullValue = ScriptValue(new ScriptValueV8Wrapper(this, std::move(null)));
@@ -410,6 +403,25 @@ ScriptEngineV8::ScriptEngineV8(ScriptManager* scriptManager) : · 7 unmodified lines
410 403
411 // V8TODO: 404 // V8TODO:
412 //QScriptEngine::setProcessEventsInterval(MSECS_PER_SECOND);405 //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 }
413}425}
414 426
415void ScriptEngineV8::registerEnum(const QString& enumName, QMetaEnum newEnum) {427void ScriptEngineV8::registerEnum(const QString& enumName, QMetaEnum newEnum) {
@@ -417,6 +429,8 @@ void ScriptEngineV8::registerEnum(const QString& enumName, QMetaEnum newEnum) { · 1 unmodified line
417 qCCritical(scriptengine) << "registerEnum called on invalid enum with name " << enumName;429 qCCritical(scriptengine) << "registerEnum called on invalid enum with name " << enumName;
418 return;430 return;
419 }431 }
432 v8::HandleScope handleScope(_v8Isolate);
433 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
420 434
421 for (int i = 0; i < newEnum.keyCount(); i++) {435 for (int i = 0; i < newEnum.keyCount(); i++) {
422 const char* keyName = newEnum.key(i);436 const char* keyName = newEnum.key(i);
@@ -426,8 +440,7 @@ void ScriptEngineV8::registerEnum(const QString& enumName, QMetaEnum newEnum) { · 3 unmodified lines
426}440}
427 441
428void ScriptEngineV8::registerValue(const QString& valueName, V8ScriptValue value) {442void ScriptEngineV8::registerValue(const QString& valueName, V8ScriptValue value) {
429 //V8TODO443 if (QThread::currentThread() != thread()) {
430 /*if (QThread::currentThread() != QScriptEngine::thread()) {
431#ifdef THREAD_DEBUGGING444#ifdef THREAD_DEBUGGING
432 qCDebug(scriptengine) << "*** WARNING *** ScriptEngineV8::registerValue() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "]";445 qCDebug(scriptengine) << "*** WARNING *** ScriptEngineV8::registerValue() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "]";
433#endif446#endif
@@ -435,8 +448,9 @@ void ScriptEngineV8::registerValue(const QString& valueName, V8ScriptValue value · 1 unmodified line
435 Q_ARG(const QString&, valueName),448 Q_ARG(const QString&, valueName),
436 Q_ARG(V8ScriptValue, value));449 Q_ARG(V8ScriptValue, value));
437 return;450 return;
438 }*/451 }
439 v8::HandleScope handleScope(_v8Isolate);452 v8::HandleScope handleScope(_v8Isolate);
453 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
440 QStringList pathToValue = valueName.split(".");454 QStringList pathToValue = valueName.split(".");
441 int partsToGo = pathToValue.length();455 int partsToGo = pathToValue.length();
442 v8::Local<v8::Object> partObject = _v8Context.Get(_v8Isolate)->Global();456 v8::Local<v8::Object> partObject = _v8Context.Get(_v8Isolate)->Global();
@@ -470,8 +484,7 @@ void ScriptEngineV8::registerValue(const QString& valueName, V8ScriptValue value · 27 unmodified lines
470}484}
471 485
472void ScriptEngineV8::registerGlobalObject(const QString& name, QObject* object) {486void ScriptEngineV8::registerGlobalObject(const QString& name, QObject* object) {
473 //V8TODO487 if (QThread::currentThread() != thread()) {
474 /*if (QThread::currentThread() != QScriptEngine::thread()) {
475#ifdef THREAD_DEBUGGING488#ifdef THREAD_DEBUGGING
476 qCDebug(scriptengine) << "*** WARNING *** ScriptEngineV8::registerGlobalObject() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] name:" << name;489 qCDebug(scriptengine) << "*** WARNING *** ScriptEngineV8::registerGlobalObject() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] name:" << name;
477#endif490#endif
@@ -482,8 +495,10 @@ void ScriptEngineV8::registerGlobalObject(const QString& name, QObject* object) · 4 unmodified lines
482 }495 }
483#ifdef THREAD_DEBUGGING496#ifdef THREAD_DEBUGGING
484 qCDebug(scriptengine) << "ScriptEngineV8::registerGlobalObject() called on thread [" << QThread::currentThread() << "] name:" << name;497 qCDebug(scriptengine) << "ScriptEngineV8::registerGlobalObject() called on thread [" << QThread::currentThread() << "] name:" << name;
485#endif*/498#endif
486 v8::HandleScope handleScope(_v8Isolate);499 v8::HandleScope handleScope(_v8Isolate);
500 Q_ASSERT(_v8Isolate->IsCurrent());
501 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
487 v8::Local<v8::Object> v8GlobalObject = getContext()->Global();502 v8::Local<v8::Object> v8GlobalObject = getContext()->Global();
488 v8::Local<v8::String> v8Name = v8::String::NewFromUtf8(_v8Isolate, name.toStdString().c_str()).ToLocalChecked();503 v8::Local<v8::String> v8Name = v8::String::NewFromUtf8(_v8Isolate, name.toStdString().c_str()).ToLocalChecked();
489 504
@@ -499,22 +514,23 @@ void ScriptEngineV8::registerGlobalObject(const QString& name, QObject* object) · 9 unmodified lines
499}514}
500 515
501void ScriptEngineV8::registerFunction(const QString& name, ScriptEngine::FunctionSignature functionSignature, int numArguments) {516void ScriptEngineV8::registerFunction(const QString& name, ScriptEngine::FunctionSignature functionSignature, int numArguments) {
502 //V8TODO517 if (QThread::currentThread() != thread()) {
503 /*if (QThread::currentThread() != QScriptEngine::thread()) {
504#ifdef THREAD_DEBUGGING518#ifdef THREAD_DEBUGGING
505 qCDebug(scriptengine) << "*** WARNING *** ScriptEngineV8::registerFunction() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] name:" << name;519 qCDebug(scriptengine) << "*** WARNING *** ScriptEngineV8::registerFunction() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] name:" << name;
506#endif520#endif
507 QMetaObject::invokeMethod(this, "registerFunction",521 QMetaObject::invokeMethod(this, "registerFunction",
508 Q_ARG(const QString&, name),522 Q_ARG(const QString&, name),
509 Q_ARG(QScriptEngine::FunctionSignature, functionSignature),523 Q_ARG(ScriptEngine::FunctionSignature, functionSignature),
510 Q_ARG(int, numArguments));524 Q_ARG(int, numArguments));
511 return;525 return;
512 }526 }
513#ifdef THREAD_DEBUGGING527#ifdef THREAD_DEBUGGING
514 qCDebug(scriptengine) << "ScriptEngineV8::registerFunction() called on thread [" << QThread::currentThread() << "] name:" << name;528 qCDebug(scriptengine) << "ScriptEngineV8::registerFunction() called on thread [" << QThread::currentThread() << "] name:" << name;
515#endif*/529#endif
516 530
517 //auto scriptFun = static_cast<ScriptValueV8Wrapper*>(newFunction(functionSignature, numArguments).ptr())->toV8Value().constGet();531 //auto scriptFun = static_cast<ScriptValueV8Wrapper*>(newFunction(functionSignature, numArguments).ptr())->toV8Value().constGet();
532 v8::HandleScope handleScope(_v8Isolate);
533 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
518 auto scriptFun = newFunction(functionSignature, numArguments);534 auto scriptFun = newFunction(functionSignature, numArguments);
519 535
520 //getContext()->Global().Set();536 //getContext()->Global().Set();
@@ -522,21 +538,22 @@ void ScriptEngineV8::registerFunction(const QString& name, ScriptEngine::Functio · 1 unmodified line
522}538}
523 539
524void ScriptEngineV8::registerFunction(const QString& parent, const QString& name, ScriptEngine::FunctionSignature functionSignature, int numArguments) {540void ScriptEngineV8::registerFunction(const QString& parent, const QString& name, ScriptEngine::FunctionSignature functionSignature, int numArguments) {
525 //V8TODO541 if (QThread::currentThread() != thread()) {
526 /*if (QThread::currentThread() != QScriptEngine::thread()) {
527#ifdef THREAD_DEBUGGING542#ifdef THREAD_DEBUGGING
528 qCDebug(scriptengine) << "*** WARNING *** ScriptEngineV8::registerFunction() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] parent:" << parent << "name:" << name;543 qCDebug(scriptengine) << "*** WARNING *** ScriptEngineV8::registerFunction() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] parent:" << parent << "name:" << name;
529#endif544#endif
530 QMetaObject::invokeMethod(this, "registerFunction",545 QMetaObject::invokeMethod(this, "registerFunction",
531 Q_ARG(const QString&, name),546 Q_ARG(const QString&, name),
532 Q_ARG(QScriptEngine::FunctionSignature, functionSignature),547 Q_ARG(ScriptEngine::FunctionSignature, functionSignature),
533 Q_ARG(int, numArguments));548 Q_ARG(int, numArguments));
534 return;549 return;
535 }550 }
536#ifdef THREAD_DEBUGGING551#ifdef THREAD_DEBUGGING
537 qCDebug(scriptengine) << "ScriptEngineV8::registerFunction() called on thread [" << QThread::currentThread() << "] parent:" << parent << "name:" << name;552 qCDebug(scriptengine) << "ScriptEngineV8::registerFunction() called on thread [" << QThread::currentThread() << "] parent:" << parent << "name:" << name;
538#endif*/553#endif
539 554
555 v8::HandleScope handleScope(_v8Isolate);
556 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
540 ScriptValue object = globalObject().property(parent);557 ScriptValue object = globalObject().property(parent);
541 if (object.isValid()) {558 if (object.isValid()) {
542 ScriptValue scriptFun = ScriptEngine::newFunction(functionSignature, numArguments);559 ScriptValue scriptFun = ScriptEngine::newFunction(functionSignature, numArguments);
@@ -546,8 +563,7 @@ void ScriptEngineV8::registerFunction(const QString& parent, const QString& name · 3 unmodified lines
546 563
547void ScriptEngineV8::registerGetterSetter(const QString& name, ScriptEngine::FunctionSignature getter,564void ScriptEngineV8::registerGetterSetter(const QString& name, ScriptEngine::FunctionSignature getter,
548 ScriptEngine::FunctionSignature setter, const QString& parent) {565 ScriptEngine::FunctionSignature setter, const QString& parent) {
549 //V8TODO566 if (QThread::currentThread() != thread()) {
550 /*if (QThread::currentThread() != QScriptEngine::thread()) {
551#ifdef THREAD_DEBUGGING567#ifdef THREAD_DEBUGGING
552 qCDebug(scriptengine) << "*** WARNING *** ScriptEngineV8::registerGetterSetter() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] "568 qCDebug(scriptengine) << "*** WARNING *** ScriptEngineV8::registerGetterSetter() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] "
553 " name:" << name << "parent:" << parent;569 " name:" << name << "parent:" << parent;
@@ -561,7 +577,10 @@ void ScriptEngineV8::registerGetterSetter(const QString& name, ScriptEngine::Fun · 7 unmodified lines
561 }577 }
562#ifdef THREAD_DEBUGGING578#ifdef THREAD_DEBUGGING
563 qCDebug(scriptengine) << "ScriptEngineV8::registerGetterSetter() called on thread [" << QThread::currentThread() << "] name:" << name << "parent:" << parent;579 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));
565 584
566 ScriptValue setterFunction = newFunction(setter, 1);585 ScriptValue setterFunction = newFunction(setter, 1);
567 ScriptValue getterFunction = newFunction(getter);586 ScriptValue getterFunction = newFunction(getter);
@@ -585,6 +604,7 @@ ScriptValue ScriptEngineV8::evaluateInClosure(const ScriptValue& _closure, · 17 unmodified lines
585 return nullValue();604 return nullValue();
586 }605 }
587 v8::HandleScope handleScope(_v8Isolate);606 v8::HandleScope handleScope(_v8Isolate);
607 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
588 ScriptProgramV8Wrapper* unwrappedProgram = ScriptProgramV8Wrapper::unwrap(_program);608 ScriptProgramV8Wrapper* unwrappedProgram = ScriptProgramV8Wrapper::unwrap(_program);
589 if (unwrappedProgram == nullptr) {609 if (unwrappedProgram == nullptr) {
590 return nullValue();610 return nullValue();
@@ -684,7 +704,7 @@ ScriptValue ScriptEngineV8::evaluateInClosure(const ScriptValue& _closure, · 93 unmodified lines
684 setGlobalObject(oldGlobal);704 setGlobalObject(oldGlobal);
685 }*/705 }*/
686 706
687 _v8Context.Get(_v8Isolate)->Enter();707 //_v8Context.Get(_v8Isolate)->Enter();
688 return result;708 return result;
689}709}
690 710
@@ -695,7 +715,7 @@ ScriptValue ScriptEngineV8::evaluate(const QString& sourceCode, const QString& f · 4 unmodified lines
695 715
696 //V8TODO716 //V8TODO
697 717
698 /*if (QThread::currentThread() != QScriptEngine::thread()) {718 if (QThread::currentThread() != thread()) {
699 ScriptValue result;719 ScriptValue result;
700#ifdef THREAD_DEBUGGING720#ifdef THREAD_DEBUGGING
701 qCDebug(scriptengine) << "*** WARNING *** ScriptEngineV8::evaluate() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] "721 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
706 Q_ARG(const QString&, sourceCode),726 Q_ARG(const QString&, sourceCode),
707 Q_ARG(const QString&, fileName));727 Q_ARG(const QString&, fileName));
708 return result;728 return result;
709 }*/729 }
710 // Compile and check syntax730 // Compile and check syntax
711 // V8TODO: Could these all be replaced with checkSyntax function from wrapper?731 // V8TODO: Could these all be replaced with checkSyntax function from wrapper?
712 v8::HandleScope handleScope(_v8Isolate);732 v8::HandleScope handleScope(_v8Isolate);
733 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
713 v8::TryCatch tryCatch(getIsolate());734 v8::TryCatch tryCatch(getIsolate());
714 v8::ScriptOrigin scriptOrigin(getIsolate(), v8::String::NewFromUtf8(getIsolate(), fileName.toStdString().c_str()).ToLocalChecked());735 v8::ScriptOrigin scriptOrigin(getIsolate(), v8::String::NewFromUtf8(getIsolate(), fileName.toStdString().c_str()).ToLocalChecked());
715 v8::Local<v8::Script> script;736 v8::Local<v8::Script> script;
@@ -775,8 +796,7 @@ Q_INVOKABLE ScriptValue ScriptEngineV8::evaluate(const ScriptProgramPointer& pro · 59 unmodified lines
775 return undefinedValue(); // bail early796 return undefinedValue(); // bail early
776 }797 }
777 798
778 //V8TODO799 if (QThread::currentThread() != thread()) {
779 /*if (QThread::currentThread() != QScriptEngine::thread()) {
780 ScriptValue result;800 ScriptValue result;
781#ifdef THREAD_DEBUGGING801#ifdef THREAD_DEBUGGING
782 qCDebug(scriptengine) << "*** WARNING *** ScriptEngineV8::evaluate() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] "802 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
786 Q_RETURN_ARG(ScriptValue, result),806 Q_RETURN_ARG(ScriptValue, result),
787 Q_ARG(const ScriptProgramPointer&, program));807 Q_ARG(const ScriptProgramPointer&, program));
788 return result;808 return result;
789 }*/809 }
790 v8::HandleScope handleScope(_v8Isolate);810 v8::HandleScope handleScope(_v8Isolate);
811 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
791 ScriptProgramV8Wrapper* unwrapped = ScriptProgramV8Wrapper::unwrap(program);812 ScriptProgramV8Wrapper* unwrapped = ScriptProgramV8Wrapper::unwrap(program);
792 if (!unwrapped) {813 if (!unwrapped) {
793 auto err = makeError(newValue("could not unwrap program"));814 auto err = makeError(newValue("could not unwrap program"));
@@ -843,6 +864,7 @@ void ScriptEngineV8::updateMemoryCost(const qint64& deltaSize) { · 49 unmodified lines
843 864
844ScriptValue ScriptEngineV8::globalObject() const {865ScriptValue ScriptEngineV8::globalObject() const {
845 v8::HandleScope handleScope(_v8Isolate);866 v8::HandleScope handleScope(_v8Isolate);
867 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
846 V8ScriptValue global(_v8Isolate, getConstContext()->Global());// = QScriptEngine::globalObject(); // can't cache the value as it may change868 V8ScriptValue global(_v8Isolate, getConstContext()->Global());// = QScriptEngine::globalObject(); // can't cache the value as it may change
847 return ScriptValue(new ScriptValueV8Wrapper(const_cast<ScriptEngineV8*>(this), std::move(global)));869 return ScriptValue(new ScriptValueV8Wrapper(const_cast<ScriptEngineV8*>(this), std::move(global)));
848}870}
@@ -852,12 +874,15 @@ ScriptManager* ScriptEngineV8::manager() const { · 3 unmodified lines
852}874}
853 875
854ScriptValue ScriptEngineV8::newArray(uint length) {876ScriptValue ScriptEngineV8::newArray(uint length) {
877 v8::HandleScope handleScope(_v8Isolate);
878 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
855 V8ScriptValue result(_v8Isolate, v8::Array::New(_v8Isolate, static_cast<int>(length)));879 V8ScriptValue result(_v8Isolate, v8::Array::New(_v8Isolate, static_cast<int>(length)));
856 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));880 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
857}881}
858 882
859ScriptValue ScriptEngineV8::newArrayBuffer(const QByteArray& message) {883ScriptValue ScriptEngineV8::newArrayBuffer(const QByteArray& message) {
860 v8::HandleScope handleScope(_v8Isolate);884 v8::HandleScope handleScope(_v8Isolate);
885 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
861 //V8TODO: this will leak memory886 //V8TODO: this will leak memory
862 std::shared_ptr<v8::BackingStore> backingStore(v8::ArrayBuffer::NewBackingStore(_v8Isolate, message.size()));887 std::shared_ptr<v8::BackingStore> backingStore(v8::ArrayBuffer::NewBackingStore(_v8Isolate, message.size()));
863 std::memcpy(backingStore.get()->Data(), message.constData(), message.size());888 std::memcpy(backingStore.get()->Data(), message.constData(), message.size());
@@ -873,12 +898,16 @@ ScriptValue ScriptEngineV8::newArrayBuffer(const QByteArray& message) { · 9 unmodified lines
873}898}
874 899
875ScriptValue ScriptEngineV8::newObject() {900ScriptValue ScriptEngineV8::newObject() {
901 v8::HandleScope handleScope(_v8Isolate);
902 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
876 V8ScriptValue result(_v8Isolate, v8::Object::New(_v8Isolate));903 V8ScriptValue result(_v8Isolate, v8::Object::New(_v8Isolate));
877 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));904 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
878}905}
879 906
880ScriptValue ScriptEngineV8::newMethod(QObject* object, V8ScriptValue lifetime,907ScriptValue ScriptEngineV8::newMethod(QObject* object, V8ScriptValue lifetime,
881 const QList<QMetaMethod>& metas, int numMaxParams) {908 const QList<QMetaMethod>& metas, int numMaxParams) {
909 v8::HandleScope handleScope(_v8Isolate);
910 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
882 V8ScriptValue result(ScriptMethodV8Proxy::newMethod(this, object, lifetime, metas, numMaxParams));911 V8ScriptValue result(ScriptMethodV8Proxy::newMethod(this, object, lifetime, metas, numMaxParams));
883 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));912 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
884}913}
@@ -893,33 +922,43 @@ ScriptProgramPointer ScriptEngineV8::newProgram(const QString& sourceCode, const · 8 unmodified lines
893ScriptValue ScriptEngineV8::newQObject(QObject* object,922ScriptValue ScriptEngineV8::newQObject(QObject* object,
894 ScriptEngine::ValueOwnership ownership,923 ScriptEngine::ValueOwnership ownership,
895 const ScriptEngine::QObjectWrapOptions& options) {924 const ScriptEngine::QObjectWrapOptions& options) {
925 v8::HandleScope handleScope(_v8Isolate);
926 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
896 V8ScriptValue result = ScriptObjectV8Proxy::newQObject(this, object, ownership, options);927 V8ScriptValue result = ScriptObjectV8Proxy::newQObject(this, object, ownership, options);
897 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));928 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
898}929}
899 930
900ScriptValue ScriptEngineV8::newValue(bool value) {931ScriptValue ScriptEngineV8::newValue(bool value) {
932 v8::HandleScope handleScope(_v8Isolate);
933 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
901 V8ScriptValue result(_v8Isolate, v8::Boolean::New(_v8Isolate, value));934 V8ScriptValue result(_v8Isolate, v8::Boolean::New(_v8Isolate, value));
902 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));935 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
903}936}
904 937
905ScriptValue ScriptEngineV8::newValue(int value) {938ScriptValue ScriptEngineV8::newValue(int value) {
939 v8::HandleScope handleScope(_v8Isolate);
940 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
906 V8ScriptValue result(_v8Isolate, v8::Integer::New(_v8Isolate, value));941 V8ScriptValue result(_v8Isolate, v8::Integer::New(_v8Isolate, value));
907 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));942 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
908}943}
909 944
910ScriptValue ScriptEngineV8::newValue(uint value) {945ScriptValue ScriptEngineV8::newValue(uint value) {
946 v8::HandleScope handleScope(_v8Isolate);
947 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
911 V8ScriptValue result(_v8Isolate, v8::Uint32::New(_v8Isolate, value));948 V8ScriptValue result(_v8Isolate, v8::Uint32::New(_v8Isolate, value));
912 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));949 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
913}950}
914 951
915ScriptValue ScriptEngineV8::newValue(double value) {952ScriptValue ScriptEngineV8::newValue(double value) {
916 v8::HandleScope handleScope(_v8Isolate);953 v8::HandleScope handleScope(_v8Isolate);
954 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
917 V8ScriptValue result(_v8Isolate, v8::Number::New(_v8Isolate, value));955 V8ScriptValue result(_v8Isolate, v8::Number::New(_v8Isolate, value));
918 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));956 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
919}957}
920 958
921ScriptValue ScriptEngineV8::newValue(const QString& value) {959ScriptValue ScriptEngineV8::newValue(const QString& value) {
922 v8::HandleScope handleScope(_v8Isolate);960 v8::HandleScope handleScope(_v8Isolate);
961 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
923 v8::Local<v8::String> valueV8 = v8::String::NewFromUtf8(_v8Isolate, value.toStdString().c_str(), v8::NewStringType::kNormal).ToLocalChecked();962 v8::Local<v8::String> valueV8 = v8::String::NewFromUtf8(_v8Isolate, value.toStdString().c_str(), v8::NewStringType::kNormal).ToLocalChecked();
924 V8ScriptValue result(_v8Isolate, valueV8);963 V8ScriptValue result(_v8Isolate, valueV8);
925 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));964 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
@@ -927,6 +966,7 @@ ScriptValue ScriptEngineV8::newValue(const QString& value) { · 1 unmodified line
927 966
928ScriptValue ScriptEngineV8::newValue(const QLatin1String& value) {967ScriptValue ScriptEngineV8::newValue(const QLatin1String& value) {
929 v8::HandleScope handleScope(_v8Isolate);968 v8::HandleScope handleScope(_v8Isolate);
969 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
930 v8::Local<v8::String> valueV8 = v8::String::NewFromUtf8(_v8Isolate, value.latin1(), v8::NewStringType::kNormal).ToLocalChecked();970 v8::Local<v8::String> valueV8 = v8::String::NewFromUtf8(_v8Isolate, value.latin1(), v8::NewStringType::kNormal).ToLocalChecked();
931 V8ScriptValue result(_v8Isolate, valueV8);971 V8ScriptValue result(_v8Isolate, valueV8);
932 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));972 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
@@ -934,12 +974,15 @@ ScriptValue ScriptEngineV8::newValue(const QLatin1String& value) { · 1 unmodified line
934 974
935ScriptValue ScriptEngineV8::newValue(const char* value) {975ScriptValue ScriptEngineV8::newValue(const char* value) {
936 v8::HandleScope handleScope(_v8Isolate);976 v8::HandleScope handleScope(_v8Isolate);
977 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
937 v8::Local<v8::String> valueV8 = v8::String::NewFromUtf8(_v8Isolate, value, v8::NewStringType::kNormal).ToLocalChecked();978 v8::Local<v8::String> valueV8 = v8::String::NewFromUtf8(_v8Isolate, value, v8::NewStringType::kNormal).ToLocalChecked();
938 V8ScriptValue result(_v8Isolate, valueV8);979 V8ScriptValue result(_v8Isolate, valueV8);
939 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));980 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
940}981}
941 982
942ScriptValue ScriptEngineV8::newVariant(const QVariant& value) {983ScriptValue ScriptEngineV8::newVariant(const QVariant& value) {
984 v8::HandleScope handleScope(_v8Isolate);
985 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
943 V8ScriptValue result = castVariantToValue(value);986 V8ScriptValue result = castVariantToValue(value);
944 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));987 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
945}988}
@@ -1005,12 +1048,14 @@ ScriptValue ScriptEngineV8::newFunction(ScriptEngine::FunctionSignature fun, int · 59 unmodified lines
1005 //V8TODO: is using GetCurrentContext ok, or context wrapper needs to be added?1048 //V8TODO: is using GetCurrentContext ok, or context wrapper needs to be added?
1006 v8::HandleScope handleScope(info.GetIsolate());1049 v8::HandleScope handleScope(info.GetIsolate());
1007 auto context = info.GetIsolate()->GetCurrentContext();1050 auto context = info.GetIsolate()->GetCurrentContext();
1051 v8::Context::Scope contextScope(context);
1052 auto object = v8::Local<v8::Object>::Cast(info.Data());
1008 auto function = reinterpret_cast<ScriptEngine::FunctionSignature>1053 auto function = reinterpret_cast<ScriptEngine::FunctionSignature>
1009 (info.Data()->ToObject(context).ToLocalChecked()->GetAlignedPointerFromInternalField(0));1054 (object->GetAlignedPointerFromInternalField(0));
1010 ScriptContext *scriptContext = reinterpret_cast<ScriptContext*>1055 ScriptContext *scriptContext = reinterpret_cast<ScriptContext*>
1011 (info.Data()->ToObject(context).ToLocalChecked()->GetAlignedPointerFromInternalField(1));;1056 (object->GetAlignedPointerFromInternalField(1));;
1012 ScriptEngineV8 *scriptEngine = reinterpret_cast<ScriptEngineV8*>1057 ScriptEngineV8 *scriptEngine = reinterpret_cast<ScriptEngineV8*>
1013 (info.Data()->ToObject(context).ToLocalChecked()->GetAlignedPointerFromInternalField(2));;1058 (object->GetAlignedPointerFromInternalField(2));;
1014 ScriptValue result = function(scriptContext, scriptEngine);1059 ScriptValue result = function(scriptContext, scriptEngine);
1015 ScriptValueV8Wrapper* unwrapped = ScriptValueV8Wrapper::unwrap(result);1060 ScriptValueV8Wrapper* unwrapped = ScriptValueV8Wrapper::unwrap(result);
1016 info.GetReturnValue().Set(unwrapped->toV8Value().constGet());1061 info.GetReturnValue().Set(unwrapped->toV8Value().constGet());
@@ -1019,6 +1064,7 @@ ScriptValue ScriptEngineV8::newFunction(ScriptEngine::FunctionSignature fun, int · 2 unmodified lines
1019 //auto functionData = v8::Object::New(_v8Isolate);1064 //auto functionData = v8::Object::New(_v8Isolate);
1020 //functionData->setIn1065 //functionData->setIn
1021 v8::HandleScope handleScope(_v8Isolate);1066 v8::HandleScope handleScope(_v8Isolate);
1067 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
1022 auto functionDataTemplate = v8::ObjectTemplate::New(_v8Isolate);1068 auto functionDataTemplate = v8::ObjectTemplate::New(_v8Isolate);
1023 functionDataTemplate->SetInternalFieldCount(3);1069 functionDataTemplate->SetInternalFieldCount(3);
1024 auto functionData = functionDataTemplate->NewInstance(getContext()).ToLocalChecked();1070 auto functionData = functionDataTemplate->NewInstance(getContext()).ToLocalChecked();
@@ -1044,6 +1090,7 @@ void ScriptEngineV8::setObjectName(const QString& name) { · 19 unmodified lines
1044//V8TODO1090//V8TODO
1045bool ScriptEngineV8::setProperty(const char* name, const QVariant& value) {1091bool ScriptEngineV8::setProperty(const char* name, const QVariant& value) {
1046 v8::HandleScope handleScope(_v8Isolate);1092 v8::HandleScope handleScope(_v8Isolate);
1093 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
1047 v8::Local<v8::Object> global = getContext()->Global();1094 v8::Local<v8::Object> global = getContext()->Global();
1048 auto v8Name = v8::String::NewFromUtf8(getIsolate(), name).ToLocalChecked();1095 auto v8Name = v8::String::NewFromUtf8(getIsolate(), name).ToLocalChecked();
1049 V8ScriptValue v8Value = castVariantToValue(value);1096 V8ScriptValue v8Value = castVariantToValue(value);
@@ -1056,13 +1103,23 @@ void ScriptEngineV8::setProcessEventsInterval(int interval) { · 6 unmodified lines
1056}1103}
1057 1104
1058QThread* ScriptEngineV8::thread() const {1105QThread* ScriptEngineV8::thread() const {
1059 return _currentThread;1106 return QObject::thread();
1060}1107}
1061 1108
1062void ScriptEngineV8::setThread(QThread* thread) {1109void ScriptEngineV8::setThread(QThread* thread) {
1110 qDebug() << "Moved script engine " << objectName() << " to different thread";
1111 _v8Isolate->Exit();
1063 moveToThread(thread);1112 moveToThread(thread);
1113 QMetaObject::invokeMethod(this, "enterIsolateOnThisThread");
1114}
1115
1116void ScriptEngineV8::enterIsolateOnThisThread() {
1117 Q_ASSERT(thread() == QThread::currentThread());
1118 _v8Isolate->Enter();
1119 qDebug() << "Script engine " << objectName() << " entered isolate on a new thread";
1064}1120}
1065 1121
1122
1066ScriptValue ScriptEngineV8::uncaughtException() const {1123ScriptValue ScriptEngineV8::uncaughtException() const {
1067 //V8TODO1124 //V8TODO
1068 //V8ScriptValue result = QScriptEngine::uncaughtException();1125 //V8ScriptValue result = QScriptEngine::uncaughtException();
@@ -1088,12 +1145,16 @@ bool ScriptEngineV8::raiseException(const ScriptValue& exception) { · 19 unmodified lines
1088}1145}
1089 1146
1090ScriptValue ScriptEngineV8::create(int type, const void* ptr) {1147ScriptValue ScriptEngineV8::create(int type, const void* ptr) {
1148 v8::HandleScope handleScope(_v8Isolate);
1149 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
1091 QVariant variant(type, ptr);1150 QVariant variant(type, ptr);
1092 V8ScriptValue scriptValue = castVariantToValue(variant);1151 V8ScriptValue scriptValue = castVariantToValue(variant);
1093 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(scriptValue)));1152 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(scriptValue)));
1094}1153}
1095 1154
1096QVariant ScriptEngineV8::convert(const ScriptValue& value, int typeId) {1155QVariant ScriptEngineV8::convert(const ScriptValue& value, int typeId) {
1156 v8::HandleScope handleScope(_v8Isolate);
1157 v8::Context::Scope contextScope(_v8Context.Get(_v8Isolate));
1097 ScriptValueV8Wrapper* unwrapped = ScriptValueV8Wrapper::unwrap(value);1158 ScriptValueV8Wrapper* unwrapped = ScriptValueV8Wrapper::unwrap(value);
1098 if (unwrapped == nullptr) {1159 if (unwrapped == nullptr) {
1099 return QVariant();1160 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