Functions connected to signals now use context that was active during connecting

This commit is contained in:
ksuprynowicz 2023-01-28 19:11:29 +01:00
parent 044cd506a6
commit 3a5b927979
5 changed files with 18 additions and 7 deletions

View file

@ -1122,6 +1122,7 @@ void ScriptManager::stopTimer(QTimer *timer) {
}
QUrl ScriptManager::resolvePath(const QString& include) const {
//qDebug(scriptengine) << "ScriptManager::resolvePath: getCurrentScriptURLs: " << _engine->getCurrentScriptURLs();
QUrl url(include);
// first lets check to see if it's already a full URL -- or a Windows path like "c:/"
if (include.startsWith("/") || url.scheme().length() == 1) {
@ -1139,7 +1140,7 @@ QUrl ScriptManager::resolvePath(const QString& include) const {
do {
auto contextInfo = context->functionContext();
parentURL = QUrl(contextInfo->fileName());
qDebug(scriptengine) << "ScriptManager::resolvePath: URL get: " << parentURL << " backtrace: " << context->backtrace() << " " << _engine->getCurrentScriptURLs();
//qDebug(scriptengine) << "ScriptManager::resolvePath: URL get: " << parentURL << " backtrace: " << context->backtrace() << " " << _engine->getCurrentScriptURLs();
parentContext = context->parentContext();
context = parentContext.get();
} while (parentURL.isRelative() && context);

View file

@ -1111,7 +1111,7 @@ QString ScriptEngineV8::formatErrorMessageFromTryCatch(v8::TryCatch &tryCatch) {
return result;
}
ScriptContextV8Pointer ScriptEngineV8::pushContext(v8::Local<v8::Context> &context) {
ScriptContextV8Pointer ScriptEngineV8::pushContext(v8::Local<v8::Context> context) {
Q_ASSERT(!_contexts.isEmpty());
ScriptContextPointer parent = _contexts.last();
_contexts.append(std::make_shared<ScriptContextV8Wrapper>(this, context, ScriptContextPointer()));

View file

@ -199,7 +199,7 @@ public: // not for public use, but I don't like how Qt strings this along with p
// V8TODO add a V8 callback that removes pointer from the map so that it gets deleted
QMap<QObject*, QSharedPointer<ScriptObjectV8Proxy>> _qobjectWrapperMapV8;
ScriptContextV8Pointer pushContext(v8::Local<v8::Context> &context);
ScriptContextV8Pointer pushContext(v8::Local<v8::Context> context);
void popContext();
protected:

View file

@ -1094,9 +1094,11 @@ int ScriptSignalV8Proxy::qt_metacall(QMetaObject::Call call, int id, void** argu
Q_ASSERT(conn.callback.get()->IsFunction());
{
v8::Local<v8::Function> callback = v8::Local<v8::Function>::Cast(conn.callback.get());
auto functionContext = callback->CreationContext();
_engine->pushContext(functionContext);
v8::Context::Scope functionContextScope(functionContext);
//auto functionContext = callback->CreationContext();
//_engine->pushContext(functionContext);
_engine->pushContext(_v8Context.Get(_engine->getIsolate()));
//v8::Context::Scope functionContextScope(functionContext);
v8::Context::Scope functionContextScope(_v8Context.Get(_engine->getIsolate()));
v8::Local<v8::Value> v8This;
if (conn.thisValue.get()->IsObject()) {
v8This = conn.thisValue.get();

View file

@ -221,7 +221,13 @@ private: // storage
public: // construction
inline ScriptSignalV8Proxy(ScriptEngineV8* engine, QObject* object, V8ScriptValue lifetime, const QMetaMethod& meta) :
_engine(engine), _object(object), _objectLifetime(lifetime), _meta(meta), _metaCallId(discoverMetaCallIdx()) {}
_engine(engine), _object(object), _objectLifetime(lifetime), _meta(meta), _metaCallId(discoverMetaCallIdx()) {
v8::Locker locker(_engine->getIsolate());
v8::Isolate::Scope isolateScope(_engine->getIsolate());
v8::HandleScope handleScope(_engine->getIsolate());
v8::Context::Scope contextScope(_engine->getContext());
_v8Context.Reset(_engine->getIsolate(), _engine->getContext());
}
private: // implementation
virtual int qt_metacall(QMetaObject::Call call, int id, void** arguments) override;
@ -245,6 +251,8 @@ private: // storage
const int _metaCallId;
ConnectionList _connections;
bool _isConnected{ false };
// Context in which it was created
v8::UniquePersistent<v8::Context> _v8Context;
Q_DISABLE_COPY(ScriptSignalV8Proxy)
};