Remove excessive getContext calls

This commit is contained in:
ksuprynowicz 2023-11-29 22:41:20 +01:00
parent 78bc24cb22
commit 410848ed82
5 changed files with 126 additions and 93 deletions

View file

@ -31,7 +31,7 @@ ScriptValue vec3ToScriptValue(ScriptEngine* engine, const glm::vec3& vec3) {
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
auto context = engineV8->getContext();
v8::Context::Scope contextScope(engineV8->getContext());
v8::Context::Scope contextScope(context);
V8ScriptValue v8ScriptValue = proxy->toV8Value();
v8::Local<v8::Object> v8Object = v8::Local<v8::Object>::Cast(v8ScriptValue.get());
@ -106,7 +106,7 @@ bool vec3FromScriptValue(const ScriptValue& object, glm::vec3& vec3) {
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
auto context = engineV8->getContext();
v8::Context::Scope contextScope(engineV8->getContext());
v8::Context::Scope contextScope(context);
V8ScriptValue v8ScriptValue = proxy->toV8Value();
v8::Local<v8::Value> v8Value = v8ScriptValue.get();

View file

@ -307,7 +307,7 @@ void ScriptEngineV8::registerValue(const QString& valueName, V8ScriptValue value
v8::Isolate::Scope isolateScope(_v8Isolate);
v8::HandleScope handleScope(_v8Isolate);
v8::Local<v8::Context> context = getContext();
v8::Context::Scope contextScope(getContext());
v8::Context::Scope contextScope(context);
QStringList pathToValue = valueName.split(".");
int partsToGo = pathToValue.length();
v8::Local<v8::Object> partObject = context->Global();
@ -370,17 +370,17 @@ void ScriptEngineV8::registerGlobalObject(const QString& name, QObject* object,
Q_ASSERT(_v8Isolate->IsCurrent());
v8::Local<v8::Context> context = getContext();
v8::Context::Scope contextScope(context);
v8::Local<v8::Object> v8GlobalObject = getContext()->Global();
v8::Local<v8::Object> v8GlobalObject = context->Global();
v8::Local<v8::String> v8Name = v8::String::NewFromUtf8(_v8Isolate, name.toStdString().c_str()).ToLocalChecked();
if (!v8GlobalObject->Get(getContext(), v8Name).IsEmpty()) {
if (!v8GlobalObject->Get(context, v8Name).IsEmpty()) {
if (object) {
V8ScriptValue value = ScriptObjectV8Proxy::newQObject(this, object, ScriptEngine::QtOwnership);
if(!v8GlobalObject->Set(getContext(), v8Name, value.get()).FromMaybe(false)) {
if(!v8GlobalObject->Set(context, v8Name, value.get()).FromMaybe(false)) {
Q_ASSERT(false);
}
} else {
if(!v8GlobalObject->Set(getContext(), v8Name, v8::Null(_v8Isolate)).FromMaybe(false)) {
if(!v8GlobalObject->Set(context, v8Name, v8::Null(_v8Isolate)).FromMaybe(false)) {
Q_ASSERT(false);
}
}
@ -458,7 +458,8 @@ void ScriptEngineV8::registerGetterSetter(const QString& name, ScriptEngine::Fun
v8::Locker locker(_v8Isolate);
v8::Isolate::Scope isolateScope(_v8Isolate);
v8::HandleScope handleScope(_v8Isolate);
v8::Context::Scope contextScope(getContext());
auto context = getContext();
v8::Context::Scope contextScope(context);
ScriptValue setterFunction = newFunction(setter, 1);
ScriptValue getterFunction = newFunction(getter);
@ -482,7 +483,7 @@ void ScriptEngineV8::registerGetterSetter(const QString& name, ScriptEngine::Fun
} else {
v8ObjectToSetProperty = v8ParentObject;
}
if (!v8ObjectToSetProperty->DefineProperty(getContext(), v8propertyName, propertyDescriptor).FromMaybe(false)) {
if (!v8ObjectToSetProperty->DefineProperty(context, v8propertyName, propertyDescriptor).FromMaybe(false)) {
qCDebug(scriptengine_v8) << "DefineProperty failed for registerGetterSetter \"" << name << "\" for parent: \""
<< parent << "\"";
}
@ -493,7 +494,7 @@ void ScriptEngineV8::registerGetterSetter(const QString& name, ScriptEngine::Fun
} else {
v8::Local<v8::String> v8propertyName =
v8::String::NewFromUtf8(_v8Isolate, name.toStdString().c_str()).ToLocalChecked();
if (!getContext()->Global()->DefineProperty(getContext(), v8propertyName, propertyDescriptor).FromMaybe(false)) {
if (!context->Global()->DefineProperty(context, v8propertyName, propertyDescriptor).FromMaybe(false)) {
qCDebug(scriptengine_v8) << "DefineProperty failed for registerGetterSetter \"" << name << "\" for global object";
}
}
@ -557,7 +558,8 @@ ScriptValue ScriptEngineV8::evaluateInClosure(const ScriptValue& _closure,
ScriptProgramV8Wrapper* unwrappedProgram;
{
v8::Context::Scope contextScope(getContext());
auto context = getContext();
v8::Context::Scope contextScope(context);
unwrappedProgram = ScriptProgramV8Wrapper::unwrap(_program);
if (unwrappedProgram == nullptr) {
_evaluatingCounter--;
@ -588,7 +590,7 @@ ScriptValue ScriptEngineV8::evaluateInClosure(const ScriptValue& _closure,
closureObject = v8::Local<v8::Object>::Cast(closure.constGet());
qCDebug(scriptengine_v8) << "Closure object members:" << scriptValueDebugListMembersV8(closure);
v8::Local<v8::Object> testObject = v8::Object::New(_v8Isolate);
if(!testObject->Set(getContext(), v8::String::NewFromUtf8(_v8Isolate, "test_value").ToLocalChecked(), closureObject).FromMaybe(false)) {
if(!testObject->Set(context, v8::String::NewFromUtf8(_v8Isolate, "test_value").ToLocalChecked(), closureObject).FromMaybe(false)) {
Q_ASSERT(false);
}
qCDebug(scriptengine_v8) << "Test object members:" << scriptValueDebugListMembersV8(V8ScriptValue(this, testObject));
@ -770,12 +772,13 @@ ScriptValue ScriptEngineV8::evaluate(const QString& sourceCode, const QString& f
v8::Locker locker(_v8Isolate);
v8::Isolate::Scope isolateScope(_v8Isolate);
v8::HandleScope handleScope(_v8Isolate);
v8::Context::Scope contextScope(getContext());
auto context = getContext();
v8::Context::Scope contextScope(context);
v8::ScriptOrigin scriptOrigin(getIsolate(), v8::String::NewFromUtf8(getIsolate(), fileName.toStdString().c_str()).ToLocalChecked());
v8::Local<v8::Script> script;
{
v8::TryCatch tryCatch(getIsolate());
if (!v8::Script::Compile(getContext(), v8::String::NewFromUtf8(getIsolate(), sourceCode.toStdString().c_str()).ToLocalChecked(), &scriptOrigin).ToLocal(&script)) {
if (!v8::Script::Compile(context, v8::String::NewFromUtf8(getIsolate(), sourceCode.toStdString().c_str()).ToLocalChecked(), &scriptOrigin).ToLocal(&script)) {
QString errorMessage(QString("Error while compiling script: \"") + fileName + QString("\" ") + formatErrorMessageFromTryCatch(tryCatch));
if (_manager) {
_manager->scriptErrorMessage(errorMessage);
@ -790,7 +793,7 @@ ScriptValue ScriptEngineV8::evaluate(const QString& sourceCode, const QString& f
v8::Local<v8::Value> result;
v8::TryCatch tryCatchRun(getIsolate());
if (!script->Run(getContext()).ToLocal(&result)) {
if (!script->Run(context).ToLocal(&result)) {
Q_ASSERT(tryCatchRun.HasCaught());
auto runError = tryCatchRun.Message();
ScriptValue errorValue(new ScriptValueV8Wrapper(this, V8ScriptValue(this, runError->Get())));
@ -829,7 +832,8 @@ void ScriptEngineV8::setUncaughtException(const v8::TryCatch &tryCatch, const QS
v8::Locker locker(_v8Isolate);
v8::Isolate::Scope isolateScope(_v8Isolate);
v8::HandleScope handleScope(_v8Isolate);
v8::Context::Scope contextScope(getContext());
v8::Local<v8::Context> context = getContext();
v8::Context::Scope contextScope(context);
QString result("");
QString errorMessage = "";
@ -844,10 +848,10 @@ void ScriptEngineV8::setUncaughtException(const v8::TryCatch &tryCatch, const QS
v8::Local<v8::Message> exceptionMessage = tryCatch.Message();
if (!exceptionMessage.IsEmpty()) {
ex->errorLine = exceptionMessage->GetLineNumber(getContext()).FromJust();
ex->errorColumn = exceptionMessage->GetStartColumn(getContext()).FromJust();
ex->errorLine = exceptionMessage->GetLineNumber(context).FromJust();
ex->errorColumn = exceptionMessage->GetStartColumn(context).FromJust();
v8::Local<v8::Value> backtraceV8String;
if (tryCatch.StackTrace(getContext()).ToLocal(&backtraceV8String)) {
if (tryCatch.StackTrace(context).ToLocal(&backtraceV8String)) {
if (backtraceV8String->IsString()) {
if (v8::Local<v8::String>::Cast(backtraceV8String)->Length() > 0) {
v8::String::Utf8Value backtraceUtf8Value(getIsolate(), backtraceV8String);
@ -875,7 +879,8 @@ QString ScriptEngineV8::formatErrorMessageFromTryCatch(v8::TryCatch &tryCatch) {
v8::Locker locker(_v8Isolate);
v8::Isolate::Scope isolateScope(_v8Isolate);
v8::HandleScope handleScope(_v8Isolate);
v8::Context::Scope contextScope(getContext());
auto context = getContext();
v8::Context::Scope contextScope(context);
QString result("");
int errorColumnNumber = 0;
int errorLineNumber = 0;
@ -885,10 +890,10 @@ QString ScriptEngineV8::formatErrorMessageFromTryCatch(v8::TryCatch &tryCatch) {
errorMessage = QString(*utf8Value);
v8::Local<v8::Message> exceptionMessage = tryCatch.Message();
if (!exceptionMessage.IsEmpty()) {
errorLineNumber = exceptionMessage->GetLineNumber(getContext()).FromJust();
errorColumnNumber = exceptionMessage->GetStartColumn(getContext()).FromJust();
errorLineNumber = exceptionMessage->GetLineNumber(context).FromJust();
errorColumnNumber = exceptionMessage->GetStartColumn(context).FromJust();
v8::Local<v8::Value> backtraceV8String;
if (tryCatch.StackTrace(getContext()).ToLocal(&backtraceV8String)) {
if (tryCatch.StackTrace(context).ToLocal(&backtraceV8String)) {
if (backtraceV8String->IsString()) {
if (v8::Local<v8::String>::Cast(backtraceV8String)->Length() > 0) {
v8::String::Utf8Value backtraceUtf8Value(getIsolate(), backtraceV8String);
@ -1000,7 +1005,8 @@ Q_INVOKABLE ScriptValue ScriptEngineV8::evaluate(const ScriptProgramPointer& pro
v8::Locker locker(_v8Isolate);
v8::Isolate::Scope isolateScope(_v8Isolate);
v8::HandleScope handleScope(_v8Isolate);
v8::Context::Scope contextScope(getContext());
auto context = getContext();
v8::Context::Scope contextScope(context);
ScriptProgramV8Wrapper* unwrapped = ScriptProgramV8Wrapper::unwrap(program);
if (!unwrapped) {
setUncaughtEngineException("Could not unwrap program", "Compile error");
@ -1020,7 +1026,7 @@ Q_INVOKABLE ScriptValue ScriptEngineV8::evaluate(const ScriptProgramPointer& pro
const V8ScriptProgram& v8Program = unwrapped->toV8Value();
v8::TryCatch tryCatchRun(getIsolate());
if (!v8Program.constGet()->Run(getContext()).ToLocal(&result)) {
if (!v8Program.constGet()->Run(context).ToLocal(&result)) {
Q_ASSERT(tryCatchRun.HasCaught());
auto runError = tryCatchRun.Message();
errorValue = ScriptValue(new ScriptValueV8Wrapper(this, V8ScriptValue(this, runError->Get())));
@ -1252,7 +1258,8 @@ ScriptValue ScriptEngineV8::newFunction(ScriptEngine::FunctionSignature fun, int
v8::Locker locker(_v8Isolate);
v8::Isolate::Scope isolateScope(_v8Isolate);
v8::HandleScope handleScope(_v8Isolate);
v8::Context::Scope contextScope(getContext());
auto context = getContext();
v8::Context::Scope contextScope(context);
auto v8FunctionCallback = [](const v8::FunctionCallbackInfo<v8::Value>& info) {
//V8TODO: is using GetCurrentContext ok, or context wrapper needs to be added?
@ -1276,10 +1283,10 @@ ScriptValue ScriptEngineV8::newFunction(ScriptEngine::FunctionSignature fun, int
}
};
auto functionDataTemplate = getFunctionDataTemplate();
auto functionData = functionDataTemplate->NewInstance(getContext()).ToLocalChecked();
auto functionData = functionDataTemplate->NewInstance(context).ToLocalChecked();
functionData->SetAlignedPointerInInternalField(0, reinterpret_cast<void*>(fun));
functionData->SetAlignedPointerInInternalField(1, reinterpret_cast<void*>(this));
auto v8Function = v8::Function::New(getContext(), v8FunctionCallback, functionData, length).ToLocalChecked();
auto v8Function = v8::Function::New(context, v8FunctionCallback, functionData, length).ToLocalChecked();
V8ScriptValue result(this, v8Function);
return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
}
@ -1293,11 +1300,12 @@ bool ScriptEngineV8::setProperty(const char* name, const QVariant& value) {
v8::Locker locker(_v8Isolate);
v8::Isolate::Scope isolateScope(_v8Isolate);
v8::HandleScope handleScope(_v8Isolate);
v8::Context::Scope contextScope(getContext());
v8::Local<v8::Object> global = getContext()->Global();
v8::Local<v8::Context> context = getContext();
v8::Context::Scope contextScope(context);
v8::Local<v8::Object> global = context->Global();
auto v8Name = v8::String::NewFromUtf8(getIsolate(), name).ToLocalChecked();
V8ScriptValue v8Value = castVariantToValue(value);
return global->Set(getContext(), v8Name, v8Value.get()).FromMaybe(false);
return global->Set(context, v8Name, v8Value.get()).FromMaybe(false);
}
void ScriptEngineV8::setProcessEventsInterval(int interval) {
@ -1411,10 +1419,11 @@ void ScriptEngineV8::compileTest() {
v8::Locker locker(_v8Isolate);
v8::Isolate::Scope isolateScope(_v8Isolate);
v8::HandleScope handleScope(_v8Isolate);
v8::Context::Scope contextScope(getContext());
auto context = getContext();
v8::Context::Scope contextScope(context);
v8::Local<v8::Script> script;
v8::ScriptOrigin scriptOrigin(getIsolate(), v8::String::NewFromUtf8(getIsolate(),"test").ToLocalChecked());
if (v8::Script::Compile(getContext(), v8::String::NewFromUtf8(getIsolate(), "print(\"hello world\");").ToLocalChecked(), &scriptOrigin).ToLocal(&script)) {
if (v8::Script::Compile(context, v8::String::NewFromUtf8(getIsolate(), "print(\"hello world\");").ToLocalChecked(), &scriptOrigin).ToLocal(&script)) {
qCDebug(scriptengine_v8) << "Compile test successful";
} else {
qCDebug(scriptengine_v8) << "Compile test failed";
@ -1436,14 +1445,15 @@ QString ScriptEngineV8::scriptValueDebugListMembersV8(const V8ScriptValue &v8Val
v8::Locker locker(_v8Isolate);
v8::Isolate::Scope isolateScope(_v8Isolate);
v8::HandleScope handleScope(_v8Isolate);
v8::Context::Scope contextScope(getContext());
v8::Local<v8::Context> context = getContext();
v8::Context::Scope contextScope(context);
QString membersString("");
if (v8Value.constGet()->IsObject()) {
v8::Local<v8::String> membersStringV8;
v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value.constGet());
auto names = object->GetPropertyNames(getContext()).ToLocalChecked();
if (v8::JSON::Stringify(getContext(), names).ToLocal(&membersStringV8)) {
auto names = object->GetPropertyNames(context).ToLocalChecked();
if (v8::JSON::Stringify(context, names).ToLocal(&membersStringV8)) {
membersString = QString(*v8::String::Utf8Value(_v8Isolate, membersStringV8));
}
membersString = QString(*v8::String::Utf8Value(_v8Isolate, membersStringV8));
@ -1457,16 +1467,17 @@ QString ScriptEngineV8::scriptValueDebugDetailsV8(const V8ScriptValue &v8Value)
v8::Locker locker(_v8Isolate);
v8::Isolate::Scope isolateScope(_v8Isolate);
v8::HandleScope handleScope(_v8Isolate);
v8::Context::Scope contextScope(getContext());
v8::Local<v8::Context> context = getContext();
v8::Context::Scope contextScope(context);
QString parentValueQString("");
v8::Local<v8::String> parentValueString;
if (v8Value.constGet()->ToDetailString(getContext()).ToLocal(&parentValueString)) {
if (v8Value.constGet()->ToDetailString(context).ToLocal(&parentValueString)) {
parentValueQString = QString(*v8::String::Utf8Value(_v8Isolate, parentValueString));
}
QString JSONQString;
v8::Local<v8::String> JSONString;
if (v8::JSON::Stringify(getContext(), v8Value.constGet()).ToLocal(&JSONString)) {
if (v8::JSON::Stringify(context, v8Value.constGet()).ToLocal(&JSONString)) {
JSONQString = QString(*v8::String::Utf8Value(_v8Isolate, JSONString));
}
return parentValueQString + QString(" JSON: ") + JSONQString;

View file

@ -725,12 +725,12 @@ V8ScriptValue ScriptEngineV8::castVariantToValue(const QVariant& val) {
case QMetaType::QDateTime:
{
double timeMs = val.value<QDateTime>().currentMSecsSinceEpoch();
return V8ScriptValue(this, v8::Date::New(getContext(), timeMs).ToLocalChecked());
return V8ScriptValue(this, v8::Date::New(context, timeMs).ToLocalChecked());
}
case QMetaType::QDate:
{
double timeMs = val.value<QDate>().startOfDay().currentMSecsSinceEpoch();
return V8ScriptValue(this, v8::Date::New(getContext(), timeMs).ToLocalChecked());
return V8ScriptValue(this, v8::Date::New(context, timeMs).ToLocalChecked());
}
default:
// check to see if this is a pointer to a QObject-derived object

View file

@ -231,7 +231,8 @@ void ScriptObjectV8Proxy::investigate() {
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(_engine->getIsolate());
v8::Context::Scope contextScope(_engine->getContext());
auto context = _engine->getContext();
v8::Context::Scope contextScope(context);
const QMetaObject* metaObject = qobject->metaObject();
@ -348,7 +349,7 @@ void ScriptObjectV8Proxy::investigate() {
}
}
v8::Local<v8::Object> v8Object = objectTemplate->NewInstance(_engine->getContext()).ToLocalChecked();
v8::Local<v8::Object> v8Object = objectTemplate->NewInstance(context).ToLocalChecked();
v8Object->SetAlignedPointerInInternalField(0, const_cast<void*>(internalPointsToQObjectProxy));
v8Object->SetAlignedPointerInInternalField(1, reinterpret_cast<void*>(this));
@ -366,7 +367,7 @@ void ScriptObjectV8Proxy::investigate() {
for (auto i = _methods.begin(); i != _methods.end(); i++) {
V8ScriptValue method = ScriptMethodV8Proxy::newMethod(_engine, qobject, V8ScriptValue(_engine, v8Object),
i.value().methods, i.value().numMaxParams);
if(!propertiesObject->Set(_engine->getContext(), v8::String::NewFromUtf8(isolate, i.value().name.toStdString().c_str()).ToLocalChecked(), method.get()).FromMaybe(false)) {
if(!propertiesObject->Set(context, v8::String::NewFromUtf8(isolate, i.value().name.toStdString().c_str()).ToLocalChecked(), method.get()).FromMaybe(false)) {
Q_ASSERT(false);
}
}
@ -555,7 +556,7 @@ v8::Local<v8::Array> ScriptObjectV8Proxy::getPropertyNames() {
v8::Isolate::Scope isolateScope(isolate);
v8::EscapableHandleScope handleScope(_engine->getIsolate());
auto context = _engine->getContext();
v8::Context::Scope contextScope(_engine->getContext());
v8::Context::Scope contextScope(context);
//V8TODO: this is really slow. It could be cached if this is called often.
v8::Local<v8::Array> properties = v8::Array::New(isolate, _props.size() + _methods.size() + _signals.size());
@ -587,7 +588,8 @@ V8ScriptValue ScriptObjectV8Proxy::property(const V8ScriptValue& object, const V
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
v8::Context::Scope contextScope(_engine->getContext());
auto context = _engine->getContext();
v8::Context::Scope contextScope(context);
QObject* qobject = _object;
if (!qobject) {
_engine->getIsolate()->ThrowError("Referencing deleted native object");
@ -621,7 +623,7 @@ V8ScriptValue ScriptObjectV8Proxy::property(const V8ScriptValue& object, const V
}
} //V8TODO: is new method created during every call? It needs to be cached instead
v8::Local<v8::Value> property;
if(_v8Object.Get(isolate)->GetInternalField(2).As<v8::Object>()->Get(_engine->getContext(), name.constGet()).ToLocal(&property)) {
if(_v8Object.Get(isolate)->GetInternalField(2).As<v8::Object>()->Get(context, name.constGet()).ToLocal(&property)) {
if (!property->IsUndefined()) {
return V8ScriptValue(_engine, property);
}
@ -698,9 +700,10 @@ ScriptVariantV8Proxy::ScriptVariantV8Proxy(ScriptEngineV8* engine, const QVarian
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
v8::Context::Scope contextScope(engine->getContext());
auto context = engine->getContext();
v8::Context::Scope contextScope(context);
auto variantDataTemplate = _engine->getVariantDataTemplate();
auto variantData = variantDataTemplate->NewInstance(engine->getContext()).ToLocalChecked();
auto variantData = variantDataTemplate->NewInstance(context).ToLocalChecked();
variantData->SetAlignedPointerInInternalField(0, const_cast<void*>(internalPointsToQVariantInProxy));
// Internal field doesn't point directly to QVariant, because then alignment would need to be guaranteed in all compilers
variantData->SetAlignedPointerInInternalField(1, reinterpret_cast<void*>(this));
@ -723,7 +726,8 @@ V8ScriptValue ScriptVariantV8Proxy::newVariant(ScriptEngineV8* engine, const QVa
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
v8::Context::Scope contextScope(engine->getContext());
auto context = engine->getContext();
v8::Context::Scope contextScope(context);
ScriptObjectV8Proxy* protoProxy = ScriptObjectV8Proxy::unwrapProxy(proto);
if (!protoProxy) {
Q_ASSERT(protoProxy);
@ -734,7 +738,7 @@ V8ScriptValue ScriptVariantV8Proxy::newVariant(ScriptEngineV8* engine, const QVa
auto proxy = new ScriptVariantV8Proxy(engine, variant, proto, protoProxy);
auto variantProxyTemplate = engine->getVariantProxyTemplate();
auto variantProxy = variantProxyTemplate->NewInstance(engine->getContext()).ToLocalChecked();
auto variantProxy = variantProxyTemplate->NewInstance(context).ToLocalChecked();
variantProxy->SetAlignedPointerInInternalField(0, const_cast<void*>(internalPointsToQVariantProxy));
variantProxy->SetAlignedPointerInInternalField(1, reinterpret_cast<void*>(proxy));
return V8ScriptValue(engine, variantProxy);
@ -912,12 +916,13 @@ V8ScriptValue ScriptMethodV8Proxy::newMethod(ScriptEngineV8* engine, QObject* ob
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
v8::Context::Scope contextScope(engine->getContext());
auto context = engine->getContext();
v8::Context::Scope contextScope(context);
auto methodDataTemplate = engine->getMethodDataTemplate();
auto methodData = methodDataTemplate->NewInstance(engine->getContext()).ToLocalChecked();
auto methodData = methodDataTemplate->NewInstance(context).ToLocalChecked();
methodData->SetAlignedPointerInInternalField(0, const_cast<void*>(internalPointsToMethodProxy));
methodData->SetAlignedPointerInInternalField(1, reinterpret_cast<void*>(new ScriptMethodV8Proxy(engine, object, lifetime, metas, numMaxParams)));
auto v8Function = v8::Function::New(engine->getContext(), callback, methodData, numMaxParams).ToLocalChecked();
auto v8Function = v8::Function::New(context, callback, methodData, numMaxParams).ToLocalChecked();
return V8ScriptValue(engine, v8Function);
}
@ -964,7 +969,8 @@ void ScriptMethodV8Proxy::call(const v8::FunctionCallbackInfo<v8::Value>& argume
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
ContextScopeV8 contextScopeV8(_engine);
v8::Context::Scope contextScope(_engine->getContext());
auto context = _engine->getContext();
v8::Context::Scope contextScope(context);
QObject* qobject = _object;
if (!qobject) {
isolate->ThrowError("Referencing deleted native object");
@ -1057,7 +1063,7 @@ void ScriptMethodV8Proxy::call(const v8::FunctionCallbackInfo<v8::Value>& argume
if (isValidMetaSelected) {
// V8TODO: is this the correct wrapper?
ScriptContextV8Wrapper ourContext(_engine, &arguments, _engine->getContext(),
ScriptContextV8Wrapper ourContext(_engine, &arguments, context,
_engine->currentContext()->parentContext());
ScriptContextGuard guard(&ourContext);
const QMetaMethod& meta = _metas[bestMeta];
@ -1153,10 +1159,11 @@ ScriptSignalV8Proxy::ScriptSignalV8Proxy(ScriptEngineV8* engine, QObject* object
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
v8::Context::Scope contextScope(_engine->getContext());
auto context = _engine->getContext();
v8::Context::Scope contextScope(context);
_objectLifetime.Reset(isolate, lifetime.get());
_objectLifetime.SetWeak(this, weakHandleCallback, v8::WeakCallbackType::kParameter);
_v8Context.Reset(isolate, _engine->getContext());
_v8Context.Reset(isolate, context);
_engine->_signalProxySetLock.lockForWrite();
_engine->_signalProxySet.insert(this);
_engine->_signalProxySetLock.unlock();
@ -1313,7 +1320,8 @@ void ScriptSignalV8Proxy::connect(ScriptValue arg0, ScriptValue arg1) {
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
v8::Context::Scope contextScope(_engine->getContext());
auto context = _engine->getContext();
v8::Context::Scope contextScope(context);
QObject* qobject = _object;
if (!qobject) {
isolate->ThrowError("Referencing deleted native object");
@ -1362,7 +1370,7 @@ void ScriptSignalV8Proxy::connect(ScriptValue arg0, ScriptValue arg1) {
v8::Local<v8::Value> destData;
// V8TODO: I'm not sure which context to use here
//auto destFunctionContext = destFunction->CreationContext();
auto destFunctionContext = _engine->getContext();
auto destFunctionContext = context;
Q_ASSERT(thisObject().isObject());
V8ScriptValue v8ThisObject = ScriptValueV8Wrapper::fullUnwrap(_engine, thisObject());
Q_ASSERT(ScriptObjectV8Proxy::unwrapProxy(v8ThisObject));
@ -1435,7 +1443,8 @@ void ScriptSignalV8Proxy::disconnect(ScriptValue arg0, ScriptValue arg1) {
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
v8::Context::Scope contextScope(_engine->getContext());
auto context = _engine->getContext();
v8::Context::Scope contextScope(context);
// untangle the arguments
V8ScriptValue callback(_engine, v8::Null(isolate));
@ -1481,7 +1490,7 @@ void ScriptSignalV8Proxy::disconnect(ScriptValue arg0, ScriptValue arg1) {
v8::Local<v8::Value> destData;
//auto destFunctionContext = destFunction->CreationContext();
auto destFunctionContext = _engine->getContext();
auto destFunctionContext = context;
Q_ASSERT(thisObject().isObject());
V8ScriptValue v8ThisObject = ScriptValueV8Wrapper::fullUnwrap(_engine, thisObject());
Q_ASSERT(ScriptObjectV8Proxy::unwrapProxy(v8ThisObject));

View file

@ -95,11 +95,11 @@ ScriptValue ScriptValueV8Wrapper::call(const ScriptValue& thisObject, const Scri
if (v8This.get()->IsObject()) {
recv = v8This.get();
}else{
recv = _engine->getContext()->Global();
recv = context->Global();
}
lock.lockForRead();
auto maybeResult = v8Function->Call(_engine->getContext(), recv, args.length(), v8Args);
auto maybeResult = v8Function->Call(context, recv, args.length(), v8Args);
lock.unlock();
if (tryCatch.HasCaught()) {
QString errorMessage(QString("Function call failed: \"") + _engine->formatErrorMessageFromTryCatch(tryCatch));
@ -156,7 +156,8 @@ ScriptValue ScriptValueV8Wrapper::construct(const ScriptValueList& args) {
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
v8::Context::Scope contextScope(_engine->getContext());
auto context = _engine->getContext();
v8::Context::Scope contextScope(context);
Q_ASSERT(args.length() <= Q_METAMETHOD_INVOKE_MAX_ARGS);
v8::Local<v8::Value> v8Args[Q_METAMETHOD_INVOKE_MAX_ARGS];
int argIndex = 0;
@ -174,7 +175,7 @@ ScriptValue ScriptValueV8Wrapper::construct(const ScriptValueList& args) {
// V8TODO: I'm not sure if this is correct, maybe use CallAsConstructor instead?
// Maybe it's CallAsConstructor for function and NewInstance for class?
lock.lockForRead();
auto maybeResult = v8Function->NewInstance(_engine->getContext(), args.length(), v8Args);
auto maybeResult = v8Function->NewInstance(context, args.length(), v8Args);
lock.unlock();
v8::Local<v8::Object> result;
if (maybeResult.ToLocal(&result)) {
@ -207,13 +208,14 @@ ScriptValue ScriptValueV8Wrapper::data() const {
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
v8::Context::Scope contextScope(_engine->getContext());
auto context = _engine->getContext();
v8::Context::Scope contextScope(context);
// Private properties are an experimental feature for now on V8, so we are using regular value for now
if (_value.constGet()->IsObject()) {
auto v8Object = v8::Local<v8::Object>::Cast(_value.constGet());
v8::Local<v8::Value> data;
//bool createData = false;
if (!v8Object->Get(_engine->getContext(), v8::String::NewFromUtf8(isolate, "__data").ToLocalChecked()).ToLocal(&data)) {
if (!v8Object->Get(context, v8::String::NewFromUtf8(isolate, "__data").ToLocalChecked()).ToLocal(&data)) {
data = v8::Undefined(isolate);
Q_ASSERT(false);
//createData = true;
@ -268,7 +270,8 @@ bool ScriptValueV8Wrapper::hasProperty(const QString& name) const {
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
v8::Context::Scope contextScope(_engine->getContext());
auto context = _engine->getContext();
v8::Context::Scope contextScope(context);
//V8TODO: does function return true on IsObject too?
if (_value.constGet()->IsObject()) {
//V8TODO: what about flags?
@ -276,7 +279,7 @@ bool ScriptValueV8Wrapper::hasProperty(const QString& name) const {
v8::Local<v8::String> key = v8::String::NewFromUtf8(isolate, name.toStdString().c_str(),v8::NewStringType::kNormal).ToLocalChecked();
const v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(_value.constGet());
//V8TODO: Which context?
if (object->Get(_engine->getContext(), key).ToLocal(&resultLocal)) {
if (object->Get(context, key).ToLocal(&resultLocal)) {
return true;
} else {
return false;
@ -292,7 +295,8 @@ ScriptValue ScriptValueV8Wrapper::property(const QString& name, const ScriptValu
v8::Locker locker(_engine->getIsolate());
v8::Isolate::Scope isolateScope(_engine->getIsolate());
v8::HandleScope handleScope(isolate);
v8::Context::Scope contextScope(_engine->getContext());
auto context = _engine->getContext();
v8::Context::Scope contextScope(context);
if (_value.constGet()->IsNullOrUndefined()) {
return _engine->undefinedValue();
}
@ -303,14 +307,14 @@ ScriptValue ScriptValueV8Wrapper::property(const QString& name, const ScriptValu
const v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(_value.constGet());
//V8TODO: Which context?
lock.lockForRead();
if (object->Get(_engine->getContext(), key).ToLocal(&resultLocal)) {
if (object->Get(context, key).ToLocal(&resultLocal)) {
V8ScriptValue result(_engine, resultLocal);
lock.unlock();
return ScriptValue(new ScriptValueV8Wrapper(_engine, std::move(result)));
} else {
QString parentValueQString("");
v8::Local<v8::String> parentValueString;
if (_value.constGet()->ToDetailString(_engine->getContext()).ToLocal(&parentValueString)) {
if (_value.constGet()->ToDetailString(context).ToLocal(&parentValueString)) {
QString(*v8::String::Utf8Value(isolate, parentValueString));
}
qCDebug(scriptengine_v8) << "Failed to get property, parent of value: " << name << ", parent type: " << QString(*v8::String::Utf8Value(isolate, _value.constGet()->TypeOf(isolate))) << " parent value: " << parentValueQString;
@ -371,7 +375,8 @@ void ScriptValueV8Wrapper::setData(const ScriptValue& value) {
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
v8::Context::Scope contextScope(_engine->getContext());
auto context = _engine->getContext();
v8::Context::Scope contextScope(context);
V8ScriptValue unwrapped = fullUnwrap(value);
// Private properties are an experimental feature for now on V8, so we are using regular value for now
if (_value.constGet()->IsNullOrUndefined()) {
@ -380,7 +385,7 @@ void ScriptValueV8Wrapper::setData(const ScriptValue& value) {
}
if (_value.constGet()->IsObject()) {
auto v8Object = v8::Local<v8::Object>::Cast(_value.constGet());
if( !v8Object->Set(_engine->getContext(), v8::String::NewFromUtf8(isolate, "__data").ToLocalChecked(), unwrapped.constGet()).FromMaybe(false)) {
if( !v8Object->Set(context, v8::String::NewFromUtf8(isolate, "__data").ToLocalChecked(), unwrapped.constGet()).FromMaybe(false)) {
qCDebug(scriptengine_v8) << "ScriptValueV8Wrapper::data(): Data object couldn't be created";
Q_ASSERT(false);
}
@ -396,7 +401,8 @@ void ScriptValueV8Wrapper::setProperty(const QString& name, const ScriptValue& v
v8::Locker locker(_engine->getIsolate());
v8::Isolate::Scope isolateScope(_engine->getIsolate());
v8::HandleScope handleScope(isolate);
v8::Context::Scope contextScope(_engine->getContext());
auto context = _engine->getContext();
v8::Context::Scope contextScope(context);
V8ScriptValue unwrapped = fullUnwrap(value);
if (_value.constGet()->IsNullOrUndefined()) {
qCDebug(scriptengine_v8) << "ScriptValueV8Wrapper::setProperty() was called on a value that is null or undefined";
@ -415,7 +421,7 @@ void ScriptValueV8Wrapper::setProperty(const QString& name, const ScriptValue& v
} else {
v8::Local<v8::String> details;
QString detailsString("");
if(_value.get()->ToDetailString(_engine->getContext()).ToLocal(&details)) {
if(_value.get()->ToDetailString(context).ToLocal(&details)) {
v8::String::Utf8Value utf8Value(isolate,details);
detailsString = *utf8Value;
}
@ -431,7 +437,8 @@ void ScriptValueV8Wrapper::setProperty(quint32 arrayIndex, const ScriptValue& va
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
v8::Context::Scope contextScope(_engine->getContext());
auto context = _engine->getContext();
v8::Context::Scope contextScope(context);
V8ScriptValue unwrapped = fullUnwrap(value);
if (_value.constGet()->IsNullOrUndefined()) {
qCDebug(scriptengine_v8) << "ScriptValueV8Wrapper::setProperty() was called on a value that is null or undefined";
@ -441,7 +448,7 @@ void ScriptValueV8Wrapper::setProperty(quint32 arrayIndex, const ScriptValue& va
auto object = v8::Local<v8::Object>::Cast(_value.get());
//V8TODO: I don't know which context to use here
lock.lockForRead();
v8::Maybe<bool> retVal(object->Set(_engine->getContext(), arrayIndex, unwrapped.constGet()));
v8::Maybe<bool> retVal(object->Set(context, arrayIndex, unwrapped.constGet()));
lock.unlock();
if (retVal.IsJust() ? !retVal.FromJust() : true){
qCDebug(scriptengine_v8) << "Failed to set property";
@ -531,9 +538,10 @@ qint32 ScriptValueV8Wrapper::toInt32() const {
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
v8::Context::Scope contextScope(_engine->getContext());
auto context = _engine->getContext();
v8::Context::Scope contextScope(context);
v8::Local<v8::Integer> integer;
if (!_value.constGet()->ToInteger(_engine->getContext()).ToLocal(&integer)) {
if (!_value.constGet()->ToInteger(context).ToLocal(&integer)) {
Q_ASSERT(false);
}
return static_cast<int32_t>((integer)->Value());
@ -544,9 +552,10 @@ double ScriptValueV8Wrapper::toInteger() const {
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
v8::Context::Scope contextScope(_engine->getContext());
auto context = _engine->getContext();
v8::Context::Scope contextScope(context);
v8::Local<v8::Integer> integer;
if (!_value.constGet()->ToInteger(_engine->getContext()).ToLocal(&integer)) {
if (!_value.constGet()->ToInteger(context).ToLocal(&integer)) {
Q_ASSERT(false);
}
return (integer)->Value();
@ -557,9 +566,10 @@ double ScriptValueV8Wrapper::toNumber() const {
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
v8::Context::Scope contextScope(_engine->getContext());
auto context = _engine->getContext();
v8::Context::Scope contextScope(context);
v8::Local<v8::Number> number;
if (!_value.constGet()->ToNumber(_engine->getContext()).ToLocal(&number)) {
if (!_value.constGet()->ToNumber(context).ToLocal(&number)) {
Q_ASSERT(false);
}
return number->Value();
@ -581,9 +591,10 @@ quint16 ScriptValueV8Wrapper::toUInt16() const {
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
v8::Context::Scope contextScope(_engine->getContext());
auto context = _engine->getContext();
v8::Context::Scope contextScope(context);
v8::Local<v8::Uint32> integer;
if (!_value.constGet()->ToUint32(_engine->getContext()).ToLocal(&integer)) {
if (!_value.constGet()->ToUint32(context).ToLocal(&integer)) {
Q_ASSERT(false);
}
return static_cast<uint16_t>(integer->Value());
@ -594,9 +605,10 @@ quint32 ScriptValueV8Wrapper::toUInt32() const {
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
v8::Context::Scope contextScope(_engine->getContext());
auto context = _engine->getContext();
v8::Context::Scope contextScope(context);
v8::Local<v8::Uint32> integer;
if (!_value.constGet()->ToUint32(_engine->getContext()).ToLocal(&integer)) {
if (!_value.constGet()->ToUint32(context).ToLocal(&integer)) {
Q_ASSERT(false);
}
return integer->Value();
@ -632,16 +644,17 @@ bool ScriptValueV8Wrapper::equals(const ScriptValue& other) const {
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
v8::Context::Scope contextScope(_engine->getContext());
auto context = _engine->getContext();
v8::Context::Scope contextScope(context);
ScriptValueV8Wrapper* unwrappedOther = unwrap(other);
Q_ASSERT(_engine->getIsolate() == unwrappedOther->_engine->getIsolate());
if (!unwrappedOther) {
return false;
}else{
if (_value.constGet()->Equals(_engine->getContext(), unwrappedOther->toV8Value().constGet()).IsNothing()) {
if (_value.constGet()->Equals(context, unwrappedOther->toV8Value().constGet()).IsNothing()) {
return false;
} else {
return _value.constGet()->Equals(_engine->getContext(), unwrappedOther->toV8Value().constGet()).FromJust();
return _value.constGet()->Equals(context, unwrappedOther->toV8Value().constGet()).FromJust();
}
}
}
@ -670,7 +683,7 @@ bool ScriptValueV8Wrapper::isError() const {
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
auto context = _engine->getContext();
v8::Context::Scope contextScope(_engine->getContext());
v8::Context::Scope contextScope(context);
v8::Local<v8::Value> error;
if (!context->Global()->Get(context, v8::String::NewFromUtf8(isolate, "Error").ToLocalChecked()).ToLocal(&error)) {
Q_ASSERT(false);