History

Further work on evaluateInClosure

ksuprynowicz committed 3 years ago

Commit ed968e11 parent 70d4a435 files · +63 −10Browse files

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

Unified · Split

libraries/script-engine/src/v8/ScriptEngineV8.cpp+17−8
@@ -800,20 +800,24 @@ ScriptValue ScriptEngineV8::evaluateInClosure(const ScriptValue& _closure, · 799 unmodified lines
800 Q_ASSERT(false);800 Q_ASSERT(false);
801 return nullValue();801 return nullValue();
802 }802 }
803 //qDebug() << "Closure global details:" << scriptValueDebugDetailsV8(V8ScriptValue(_v8Isolate, closureGlobal));
803 }804 }
804 //_v8Context.Get(_v8Isolate)->DetachGlobal();805 //_v8Context.Get(_v8Isolate)->DetachGlobal();
805 //oldGlobal = _v8Context.Get(_v8Isolate)->Global();806 //oldGlobal = _v8Context.Get(_v8Isolate)->Global();
806 v8::Local<v8::Context> closureContext;807 v8::Local<v8::Context> closureContext;
807 808
808 if (closureGlobal->IsObject()) {809 // V8TODO
810 /*if (closureGlobal->IsObject()) {
809#ifdef DEBUG_JS811#ifdef DEBUG_JS
810 qCDebug(shared) << " setting global = closure.global" << shortName;812 qCDebug(shared) << " setting global = closure.global" << shortName;
811#endif813#endif
814 closureContext = v8::Context::New(_v8Isolate, nullptr, v8::Local<v8::ObjectTemplate>(), closureGlobal);
812 closureContext = v8::Context::New(_v8Isolate, nullptr, v8::Local<v8::ObjectTemplate>(), closureGlobal);815 closureContext = v8::Context::New(_v8Isolate, nullptr, v8::Local<v8::ObjectTemplate>(), closureGlobal);
813 //setGlobalObject(global);816 //setGlobalObject(global);
814 } else {817 } else {
815 closureContext = v8::Context::New(_v8Isolate);818 closureContext = v8::Context::New(_v8Isolate);
816 }819 }*/
820 closureContext = v8::Context::New(_v8Isolate, nullptr, v8::Local<v8::ObjectTemplate>(), closureObject);
817 821
818 ScriptValue result;822 ScriptValue result;
819 //auto context = pushContext();823 //auto context = pushContext();
@@ -828,23 +832,25 @@ ScriptValue ScriptEngineV8::evaluateInClosure(const ScriptValue& _closure, · 8 unmodified lines
828 const V8ScriptProgram& program = unwrappedProgram->toV8Value();832 const V8ScriptProgram& program = unwrappedProgram->toV8Value();
829 833
830 v8::Local<v8::Value> thiz;834 v8::Local<v8::Value> thiz;
831 // V8TODO: not sure if "this" doesn't exist or is empty in some cases835 // V8TODO: not sure if "this" is used at all here
832 if (!closureObject->Get(closure.constGetContext(), v8::String::NewFromUtf8(_v8Isolate, "this").ToLocalChecked())836 /*if (!closureObject->Get(closure.constGetContext(), v8::String::NewFromUtf8(_v8Isolate, "this").ToLocalChecked())
833 .ToLocal(&thiz)) {837 .ToLocal(&thiz)) {
834 _evaluatingCounter--;838 _evaluatingCounter--;
835 qDebug(scriptengine) << "Empty this object in closure";839 qDebug(scriptengine) << "Empty this object in closure";
836 Q_ASSERT(false);840 Q_ASSERT(false);
837 return nullValue();841 return nullValue();
838 }842 }*/
839 //thiz = closure.property("this");843 //thiz = closure.property("this");
840 if (thiz->IsObject()) {844 //qDebug() << "Closure this details:" << scriptValueDebugDetailsV8(V8ScriptValue(_v8Isolate, thiz));
845 // V8TODO:
846 /*if (thiz->IsObject()) {
841#ifdef DEBUG_JS847#ifdef DEBUG_JS
842 qCDebug(shared) << " setting this = closure.this" << shortName;848 qCDebug(shared) << " setting this = closure.this" << shortName;
843#endif849#endif
844 //V8TODO I don't know how to do this in V8, will adding "this" to global object work?850 //V8TODO I don't know how to do this in V8, will adding "this" to global object work?
845 closureContext->Global()->Set(closureContext, v8::String::NewFromUtf8(_v8Isolate, "this").ToLocalChecked(), thiz);851 closureContext->Global()->Set(closureContext, v8::String::NewFromUtf8(_v8Isolate, "this").ToLocalChecked(), thiz);
846 //context->setThisObject(thiz);852 //context->setThisObject(thiz);
847 }853 }*/
848 854
849 //context->pushScope(closure);855 //context->pushScope(closure);
850#ifdef DEBUG_JS856#ifdef DEBUG_JS
@@ -852,12 +858,15 @@ ScriptValue ScriptEngineV8::evaluateInClosure(const ScriptValue& _closure, · 1 unmodified line
852#endif858#endif
853 {859 {
854 v8::TryCatch tryCatch(getIsolate());860 v8::TryCatch tryCatch(getIsolate());
861 //qDebug(scriptengine) << "Closure before run:" << scriptValueDebugDetailsV8(closure);
855 auto maybeResult = program.constGet()->Run(closureContext);862 auto maybeResult = program.constGet()->Run(closureContext);
863 //qDebug(scriptengine) << "Closure after run:" << scriptValueDebugDetailsV8(closure);
856 v8::Local<v8::Value> v8Result;864 v8::Local<v8::Value> v8Result;
857 if (!maybeResult.ToLocal(&v8Result)) {865 if (!maybeResult.ToLocal(&v8Result)) {
858 v8::String::Utf8Value utf8Value(getIsolate(), tryCatch.Exception());866 v8::String::Utf8Value utf8Value(getIsolate(), tryCatch.Exception());
859 QString errorMessage = QString(*utf8Value);867 QString errorMessage = QString(*utf8Value);
860 qWarning(scriptengine) << __FUNCTION__ << "---------- hasCaught:" << errorMessage;868 qWarning(scriptengine) << __FUNCTION__ << "---------- hasCaught:" << errorMessage;
869 qWarning(scriptengine) << __FUNCTION__ << "---------- tryCatch details:" << formatErrorMessageFromTryCatch(tryCatch);
861 //V8TODO: better error reporting870 //V8TODO: better error reporting
862 }871 }
863 872
@@ -1513,7 +1522,7 @@ void ScriptEngineV8::compileTest() { · 649 unmodified lines
1513 }1522 }
1514}1523}
1515 1524
1516QString ScriptEngineV8::scriptValueDebugDetails(ScriptValue &value) {1525QString ScriptEngineV8::scriptValueDebugDetails(const ScriptValue &value) {
1517 V8ScriptValue v8Value = ScriptValueV8Wrapper::fullUnwrap(this, value);1526 V8ScriptValue v8Value = ScriptValueV8Wrapper::fullUnwrap(this, value);
1518 return scriptValueDebugDetailsV8(v8Value);1527 return scriptValueDebugDetailsV8(v8Value);
1519}1528}