History

Code cleanup

ksuprynowicz committed 3 years ago

Commit 5050cc91 parent d24038629 files · +57 −951Browse files

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

Unified · Split

libraries/script-engine/src/v8/ScriptEngineV8.cpp+7−345
@@ -85,7 +85,7 @@ ScriptValue ScriptEngineV8::makeError(const ScriptValue& _other, const QString& · 84 unmodified lines
8585 return nullValue();
8686 }
8787
88− //V8TODO
88+ //V8TODO: do not remove until ScriptEngineV8::makeError is implemented
8989 /*
9090 auto other = _other;
9191 if (_other.constGet()->IsString()) {
@@ -130,14 +130,12 @@ ScriptValue ScriptEngineV8::checkScriptSyntax(ScriptProgramPointer program) { · 38 unmodified lines
130130 v8::HandleScope handleScope(_v8Isolate);
131131 v8::Context::Scope contextScope(getContext());
132132 ScriptSyntaxCheckResultPointer syntaxCheck = program->checkSyntax();
133− //V8TODO
134133 if (syntaxCheck->state() != ScriptSyntaxCheckResult::Valid) {
135134 auto err = globalObject().property("SyntaxError").construct(ScriptValueList({ newValue(syntaxCheck->errorMessage()) }));
136135 err.setProperty("fileName", program->fileName());
137136 err.setProperty("lineNumber", syntaxCheck->errorLineNumber());
138137 err.setProperty("expressionBeginOffset", syntaxCheck->errorColumnNumber());
139138 err.setProperty("stack", syntaxCheck->errorBacktrace());
140− //err.setProperty("stack", currentContext()->backtrace().join(ScriptManager::SCRIPT_BACKTRACE_SEP));
141139 {
142140 const auto error = syntaxCheck->errorMessage();
143141 const auto line = QString::number(syntaxCheck->errorLineNumber());
@@ -150,32 +148,6 @@ ScriptValue ScriptEngineV8::checkScriptSyntax(ScriptProgramPointer program) { · 6 unmodified lines
150148 }
151149 return undefinedValue();
152150 }
153−/*ScriptValue ScriptEngineV8::lintScript(const QString& sourceCode, const QString& fileName, const int lineNumber) {
154− if (!IS_THREADSAFE_INVOCATION(thread(), __FUNCTION__)) {
155− return nullValue();
156− }
157− //V8TODO
158− const auto syntaxCheck = checkSyntax(sourceCode);
159− if (syntaxCheck.state() != QScriptSyntaxCheckResult::Valid) {
160− auto err = QScriptEngine::globalObject().property("SyntaxError").construct(V8ScriptValueList({ syntaxCheck.errorMessage() }));
161− err.setProperty("fileName", fileName);
162− err.setProperty("lineNumber", syntaxCheck.errorLineNumber());
163− err.setProperty("expressionBeginOffset", syntaxCheck.errorColumnNumber());
164− err.setProperty("stack", currentContext()->backtrace().join(ScriptManager::SCRIPT_BACKTRACE_SEP));
165− {
166− const auto error = syntaxCheck.errorMessage();
167− const auto line = QString::number(syntaxCheck.errorLineNumber());
168− const auto column = QString::number(syntaxCheck.errorColumnNumber());
169− // for compatibility with legacy reporting
170− const auto message = QString("[SyntaxError] %1 in %2:%3(%4)").arg(error, fileName, line, column);
171− err.setProperty("formatted", message);
172− }
173− return ScriptValue(new ScriptValueV8Wrapper(this, std::move(err)));
174− }
175− return undefinedValue();
176−}*/
177−
178−
179151
180152 // Lambda
181153 /*ScriptValue ScriptEngineV8::newLambdaFunction(std::function<V8ScriptValue(V8ScriptContext*, ScriptEngineV8*)> operation,
@@ -264,8 +236,7 @@ v8::Platform* ScriptEngineV8::getV8Platform() { · 82 unmodified lines
264236 }
265237
266238 ScriptEngineV8::ScriptEngineV8(ScriptManager *manager) : ScriptEngine(manager), _evaluatingCounter(0)
267− //V8TODO
268− //_arrayBufferClass(new ArrayBufferClass(this))
239+ //V8TODO _arrayBufferClass(new ArrayBufferClass(this))
269240 {
270241 _v8InitMutex.lock();
271242 std::call_once ( _v8InitOnceFlag, [ ]{
@@ -289,7 +260,6 @@ ScriptEngineV8::ScriptEngineV8(ScriptManager *manager) : ScriptEngine(manager), · 17 unmodified lines
289260 #else
290261 v8::V8::SetFlagsFromString("--stack-size=256");
291262 #endif
292− //v8::V8::SetFlagsFromString("--stack-size=256 --single-threaded");
293263 v8::Platform* platform = getV8Platform();
294264 v8::V8::InitializePlatform(platform);
295265 v8::V8::Initialize(); qCDebug(scriptengine_v8) << "V8 platform initialized";
@@ -317,18 +287,10 @@ ScriptEngineV8::ScriptEngineV8(ScriptManager *manager) : ScriptEngine(manager), · 21 unmodified lines
317287 registerSystemTypes();
318288
319289 // V8TODO: dispose of isolate on ScriptEngineV8 destruction
320− //v8::UniquePersistent<v8::Value> null = v8::UniquePersistent<v8::Value>(_v8Isolate, v8::Null(_v8Isolate));
321− //_nullValue = ScriptValue(new ScriptValueV8Wrapper(this, std::move(null)));
322−
323− //V8ScriptValue undefined = v8::UniquePersistent<v8::Value>(_v8Isolate,v8::Undefined(_v8Isolate));
324− //_undefinedValue = ScriptValue(new ScriptValueV8Wrapper(this, std::move(undefined)));
325−
326290 // V8TODO:
327291 //QScriptEngine::setProcessEventsInterval(MSECS_PER_SECOND);
328292 }
329293
330− //_currentThread = QThread::currentThread();
331−
332294 //if (_scriptManager) {
333295 // V8TODO: port to V8
334296 /*connect(this, &QScriptEngine::signalHandlerException, this, [this](const V8ScriptValue& exception) {
@@ -342,8 +304,6 @@ ScriptEngineV8::ScriptEngineV8(ScriptManager *manager) : ScriptEngine(manager), · 7 unmodified lines
342304 emit _scriptManager->unhandledException(ScriptValue(new ScriptValueV8Wrapper(this, std::move(thrown))));
343305 }
344306 }, Qt::DirectConnection);*/
345− //moveToThread(scriptManager->thread());
346− //setThread(scriptManager->thread());
347307 //}
348308 }
349309
@@ -396,16 +356,11 @@ void ScriptEngineV8::registerValue(const QString& valueName, V8ScriptValue value · 46 unmodified lines
396356 }
397357 if (createProperty) {
398358 if (partsToGo > 0) {
399− //This was commented out
400− //QObject *object = new QObject;
401− v8::Local<v8::Object> partValue = v8::Object::New(_v8Isolate); //newQObject(object, QScriptEngine::ScriptOwnership);
402− //V8ScriptValue partValue = QScriptEngine::newArray(); //newQObject(object, QScriptEngine::ScriptOwnership);
359+ v8::Local<v8::Object> partValue = v8::Object::New(_v8Isolate);
403360 if (!partObject->Set(context, pathPartV8, partValue).FromMaybe(false)) {
404361 Q_ASSERT(false);
405362 }
406363 } else {
407− //partObject = currentPath->ToObject();
408− //V8TODO: do these still happen if asserts are disabled?
409364 if (!partObject->Set(context, pathPartV8, value.constGet()).FromMaybe(false)) {
410365 Q_ASSERT(false);
411366 }
@@ -440,14 +395,6 @@ void ScriptEngineV8::registerGlobalObject(const QString& name, QObject* object) · 28 unmodified lines
440395 #ifdef THREAD_DEBUGGING
441396 qCDebug(scriptengine_v8) << "ScriptEngineV8::registerGlobalObject() called on thread [" << QThread::currentThread() << "] name:" << name;
442397 #endif
443− /*bool is_isolate_exit_needed = false;
444− if(!_v8Isolate->IsCurrent() && !_v8Locker) {
445− // V8TODO: Theoretically only script thread should access this, so it should be safe
446− _v8Locker.reset(new v8::Locker(_v8Isolate));
447− _v8Isolate->Enter();
448− is_isolate_exit_needed = true;
449− }*/
450− //{
451398 v8::Locker locker(_v8Isolate);
452399 v8::Isolate::Scope isolateScope(_v8Isolate);
453400 v8::HandleScope handleScope(_v8Isolate);
@@ -457,7 +404,6 @@ void ScriptEngineV8::registerGlobalObject(const QString& name, QObject* object) · 3 unmodified lines
457404 v8::Local<v8::Object> v8GlobalObject = getContext()->Global();
458405 v8::Local<v8::String> v8Name = v8::String::NewFromUtf8(_v8Isolate, name.toStdString().c_str()).ToLocalChecked();
459406
460− // V8TODO: Is IsEmpty check enough or IsValid is needed too?
461407 if (!v8GlobalObject->Get(getContext(), v8Name).IsEmpty()) {
462408 if (object) {
463409 V8ScriptValue value = ScriptObjectV8Proxy::newQObject(this, object, ScriptEngine::QtOwnership);
@@ -470,16 +416,9 @@ void ScriptEngineV8::registerGlobalObject(const QString& name, QObject* object) · 6 unmodified lines
470416 }
471417 }
472418 }
473− //}
474− /*if (is_isolate_exit_needed) {
475− _v8Isolate->Exit();
476− _v8Locker.reset(nullptr);
477− }*/
478419 }
479420
480421 void ScriptEngineV8::registerFunction(const QString& name, ScriptEngine::FunctionSignature functionSignature, int numArguments) {
481− //if (QThread::currentThread() != ) {
482− //}
483422 if (QThread::currentThread() != thread()) {
484423 #ifdef THREAD_DEBUGGING
485424 qCDebug(scriptengine_v8) << "*** WARNING *** ScriptEngineV8::registerFunction() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] name:" << name;
@@ -494,28 +433,13 @@ void ScriptEngineV8::registerFunction(const QString& name, ScriptEngine::Functio · 8 unmodified lines
494433 qCDebug(scriptengine_v8) << "ScriptEngineV8::registerFunction() called on thread [" << QThread::currentThread() << "] name:" << name;
495434 #endif
496435
497− /*bool is_isolate_exit_needed = false;
498− if(!_v8Isolate->IsCurrent() && !_v8Locker) {
499− // V8TODO: Theoretically only script thread should access this, so it should be safe
500− _v8Locker.reset(new v8::Locker(_v8Isolate));
501− _v8Isolate->Enter();
502− is_isolate_exit_needed = true;
503− }
504− {*/
505− //auto scriptFun = static_cast<ScriptValueV8Wrapper*>(newFunction(functionSignature, numArguments).ptr())->toV8Value().constGet();
506436 v8::Locker locker(_v8Isolate);
507437 v8::Isolate::Scope isolateScope(_v8Isolate);
508438 v8::HandleScope handleScope(_v8Isolate);
509439 v8::Context::Scope contextScope(getContext());
510440 auto scriptFun = newFunction(functionSignature, numArguments);
511441
512− //getContext()->Global().Set();
513442 globalObject().setProperty(name, scriptFun);
514− /*}
515− if (is_isolate_exit_needed) {
516− _v8Isolate->Exit();
517− _v8Locker.reset(nullptr);
518− }*/
519443 }
520444
521445 void ScriptEngineV8::registerFunction(const QString& parent, const QString& name, ScriptEngine::FunctionSignature functionSignature, int numArguments) {
@@ -533,14 +457,6 @@ void ScriptEngineV8::registerFunction(const QString& parent, const QString& name · 11 unmodified lines
533457 qCDebug(scriptengine_v8) << "ScriptEngineV8::registerFunction() called on thread [" << QThread::currentThread() << "] parent:" << parent << "name:" << name;
534458 #endif
535459
536− /*bool is_isolate_exit_needed = false;
537− if(!_v8Isolate->IsCurrent() && !_v8Locker) {
538− // V8TODO: Theoretically only script thread should access this, so it should be safe
539− _v8Locker.reset(new v8::Locker(_v8Isolate));
540− _v8Isolate->Enter();
541− is_isolate_exit_needed = true;
542− }
543− {*/
544460 v8::Locker locker(_v8Isolate);
545461 v8::Isolate::Scope isolateScope(_v8Isolate);
546462 v8::HandleScope handleScope(_v8Isolate);
@@ -550,11 +466,6 @@ void ScriptEngineV8::registerFunction(const QString& parent, const QString& name · 3 unmodified lines
550466 ScriptValue scriptFun = newFunction(functionSignature, numArguments);
551467 object.setProperty(name, scriptFun);
552468 }
553− /*}
554− if (is_isolate_exit_needed) {
555− _v8Isolate->Exit();
556− _v8Locker.reset(nullptr);
557− }*/
558469 }
559470
560471 void ScriptEngineV8::registerGetterSetter(const QString& name, ScriptEngine::FunctionSignature getter,
@@ -575,60 +486,17 @@ void ScriptEngineV8::registerGetterSetter(const QString& name, ScriptEngine::Fun · 14 unmodified lines
575486 qCDebug(scriptengine_v8) << "ScriptEngineV8::registerGetterSetter() called on thread [" << QThread::currentThread() << "] name:" << name << "parent:" << parent;
576487 #endif
577488
578− /*bool is_isolate_exit_needed = false;
579− if(!_v8Isolate->IsCurrent() && !_v8Locker) {
580− // V8TODO: Theoretically only script thread should access this, so it should be safe
581− _v8Locker.reset(new v8::Locker(_v8Isolate));
582− _v8Isolate->Enter();
583− is_isolate_exit_needed = true;
584− }
585− {*/
586489 v8::Locker locker(_v8Isolate);
587490 v8::Isolate::Scope isolateScope(_v8Isolate);
588491 v8::HandleScope handleScope(_v8Isolate);
589492 v8::Context::Scope contextScope(getContext());
590493
591− /*auto getterFunction = [](v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info) {
592− //V8TODO: is using GetCurrentContext ok, or context wrapper needs to be added?
593− v8::HandleScope handleScope(info.GetIsolate());
594− auto context = info.GetIsolate()->GetCurrentContext();
595− v8::Context::Scope contextScope(context);
596− auto object = v8::Local<v8::Object>::Cast(info.Data());
597− Q_ASSERT(object->InternalFieldCount() == 2);
598− auto function = reinterpret_cast<ScriptEngine::FunctionSignature>
599− (object->GetAlignedPointerFromInternalField(0));
600− ScriptEngineV8 *scriptEngine = reinterpret_cast<ScriptEngineV8*>
601− (object->GetAlignedPointerFromInternalField(1));
602− ScriptContextV8Wrapper scriptContext(scriptEngine, &info);
603− //V8TODO: this scriptContext needs to have FunctionCallbackInfo added
604− ScriptValue result = function(&scriptContext, scriptEngine);
605− ScriptValueV8Wrapper* unwrapped = ScriptValueV8Wrapper::unwrap(result);
606− info.GetReturnValue().Set(unwrapped->toV8Value().constGet());
607− };
608− auto setterFunction = [](v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info) {
609− //V8TODO: is using GetCurrentContext ok, or context wrapper needs to be added?
610− v8::HandleScope handleScope(info.GetIsolate());
611− auto context = info.GetIsolate()->GetCurrentContext();
612− v8::Context::Scope contextScope(context);
613− auto object = v8::Local<v8::Object>::Cast(info.Data());
614− Q_ASSERT(object->InternalFieldCount() == 2);
615− auto function = reinterpret_cast<ScriptEngine::FunctionSignature>
616− (object->GetAlignedPointerFromInternalField(0));
617− ScriptEngineV8 *scriptEngine = reinterpret_cast<ScriptEngineV8*>
618− (object->GetAlignedPointerFromInternalField(1));
619− ScriptContextV8Wrapper scriptContext(scriptEngine, &info);
620− //V8TODO: this scriptContext needs to have FunctionCallbackInfo added
621− ScriptValue result = function(&scriptContext, scriptEngine);
622− ScriptValueV8Wrapper* unwrapped = ScriptValueV8Wrapper::unwrap(result);
623− };*/
624−
625494 ScriptValue setterFunction = newFunction(setter, 1);
626495 ScriptValue getterFunction = newFunction(getter);
627496 V8ScriptValue unwrappedGetter = ScriptValueV8Wrapper::fullUnwrap(this, getterFunction);
628497 V8ScriptValue unwrappedSetter = ScriptValueV8Wrapper::fullUnwrap(this, setterFunction);
629498 v8::PropertyDescriptor propertyDescriptor(unwrappedGetter.get(), unwrappedSetter.get());
630499
631− //V8TODO: Getters/setters are probably done in a different way in V8. Maybe object template is needed?
632500 if (!parent.isNull() && !parent.isEmpty()) {
633501 ScriptValue object = globalObject().property(parent);
634502 if (object.isValid()) {
@@ -649,8 +517,6 @@ void ScriptEngineV8::registerGetterSetter(const QString& name, ScriptEngine::Fun · 14 unmodified lines
649517 qCDebug(scriptengine_v8) << "DefineProperty failed for registerGetterSetter \"" << name << "\" for parent: \""
650518 << parent << "\"";
651519 }
652− //object.setProperty(name, setterFunction, ScriptValue::PropertySetter);
653− //object.setProperty(name, getterFunction, ScriptValue::PropertyGetter);
654520 } else {
655521 qCDebug(scriptengine_v8) << "Parent object \"" << parent << "\" for registerGetterSetter \"" << name
656522 << "\" is not valid: ";
@@ -661,14 +527,7 @@ void ScriptEngineV8::registerGetterSetter(const QString& name, ScriptEngine::Fun · 4 unmodified lines
661527 if (!getContext()->Global()->DefineProperty(getContext(), v8propertyName, propertyDescriptor).FromMaybe(false)) {
662528 qCDebug(scriptengine_v8) << "DefineProperty failed for registerGetterSetter \"" << name << "\" for global object";
663529 }
664− //globalObject().setProperty(name, setterFunction, ScriptValue::PropertySetter);
665− //globalObject().setProperty(name, getterFunction, ScriptValue::PropertyGetter);
666530 }
667− /*}
668− if (is_isolate_exit_needed) {
669− _v8Isolate->Exit();
670− _v8Locker.reset(nullptr);
671− }*/
672531 }
673532
674533 v8::Local<v8::Context> ScriptEngineV8::getContext() {
@@ -721,11 +580,9 @@ ScriptValue ScriptEngineV8::evaluateInClosure(const ScriptValue& _closure, · 46 unmodified lines
721580 storeGlobalObjectContents();
722581
723582 v8::Local<v8::Object> closureObject;
724− //v8::Local<v8::Value> oldGlobal;
725583 v8::Local<v8::Value> closureGlobal;
726584 ScriptValueV8Wrapper* unwrappedClosure;
727585 ScriptProgramV8Wrapper* unwrappedProgram;
728− //v8::Local<v8::Context> oldContext = getContext();
729586
730587 {
731588 v8::Context::Scope contextScope(getContext());
@@ -736,7 +593,6 @@ ScriptValue ScriptEngineV8::evaluateInClosure(const ScriptValue& _closure, · 4 unmodified lines
736593 Q_ASSERT(false);
737594 return nullValue();
738595 }
739− // V8TODO: is another context switch necessary after unwrapping closure?
740596
741597 const auto fileName = unwrappedProgram->fileName();
742598 const auto shortName = QUrl(fileName).fileName();
@@ -750,7 +606,6 @@ ScriptValue ScriptEngineV8::evaluateInClosure(const ScriptValue& _closure, · 7 unmodified lines
750606 }
751607
752608 const V8ScriptValue& closure = unwrappedClosure->toV8Value();
753− //const V8ScriptProgram& program = unwrappedProgram->toV8Value();
754609 if (!closure.constGet()->IsObject()) {
755610 _evaluatingCounter--;
756611 qCDebug(scriptengine_v8) << "Unwrapped closure is not an object";
@@ -773,28 +628,13 @@ ScriptValue ScriptEngineV8::evaluateInClosure(const ScriptValue& _closure, · 16 unmodified lines
773628 Q_ASSERT(false);
774629 return nullValue();
775630 }
776− //qCDebug(scriptengine_v8) << "Closure global details:" << scriptValueDebugDetailsV8(V8ScriptValue(_v8Isolate, closureGlobal));
777631 }
778− //oldGlobal = _v8Context.Get(_v8Isolate)->Global();
779632 v8::Local<v8::Context> closureContext;
780633
781− // V8TODO V8 cannot use arbitrary objects as global objects
782− /*if (closureGlobal->IsObject()) {
783−#ifdef DEBUG_JS
784− qCDebug(shared) << " setting global = closure.global" << shortName;
785−#endif
786− closureContext = v8::Context::New(_v8Isolate, nullptr, v8::Local<v8::ObjectTemplate>(), closureGlobal);
787− closureContext = v8::Context::New(_v8Isolate, nullptr, v8::Local<v8::ObjectTemplate>(), closureGlobal);
788− //setGlobalObject(global);
789− } else {
790− closureContext = v8::Context::New(_v8Isolate);
791− }*/
792634 closureContext = v8::Context::New(_v8Isolate);
793635 pushContext(closureContext);
794636
795637 ScriptValue result;
796− //auto context = pushContext();
797−
798638 // V8TODO: a lot of functions rely on _v8Context, which was not updated here
799639 // It might cause trouble
800640 {
@@ -809,27 +649,6 @@ ScriptValue ScriptEngineV8::evaluateInClosure(const ScriptValue& _closure, · 8 unmodified lines
809649 const V8ScriptProgram& program = unwrappedProgram->toV8Value();
810650
811651 v8::Local<v8::Value> thiz;
812− // V8TODO: not sure if "this" is used at all here
813− /*if (!closureObject->Get(closure.constGetContext(), v8::String::NewFromUtf8(_v8Isolate, "this").ToLocalChecked())
814− .ToLocal(&thiz)) {
815− _evaluatingCounter--;
816− qCDebug(scriptengine_v8) << "Empty this object in closure";
817− Q_ASSERT(false);
818− return nullValue();
819− }*/
820− //thiz = closure.property("this");
821− //qCDebug(scriptengine_v8) << "Closure this details:" << scriptValueDebugDetailsV8(V8ScriptValue(_v8Isolate, thiz));
822− // V8TODO:
823− /*if (thiz->IsObject()) {
824−#ifdef DEBUG_JS
825− qCDebug(shared) << " setting this = closure.this" << shortName;
826−#endif
827− //V8TODO I don't know how to do this in V8, will adding "this" to global object work?
828− closureContext->Global()->Set(closureContext, v8::String::NewFromUtf8(_v8Isolate, "this").ToLocalChecked(), thiz);
829− //context->setThisObject(thiz);
830− }*/
831−
832− //context->pushScope(closure);
833652 #ifdef DEBUG_JS
834653 qCDebug(shared) << QString("[%1] evaluateInClosure %2").arg(isEvaluating()).arg(shortName);
835654 #endif
@@ -846,51 +665,22 @@ ScriptValue ScriptEngineV8::evaluateInClosure(const ScriptValue& _closure, · 10 unmodified lines
846665 }
847666 qCDebug(scriptengine_v8) << "ScriptEngineV8::evaluateInClosure: " << globalMemberNames->Length() << " objects added to global";
848667
849− /*auto oldGlobalMemberNames = oldContext->Global()->GetPropertyNames(oldContext).ToLocalChecked();
850− //auto oldGlobalMemberNames = oldContext->Global()->GetPropertyNames(closureContext).ToLocalChecked();
851− for (size_t i = 0; i < oldGlobalMemberNames->Length(); i++) {
852− auto name = oldGlobalMemberNames->Get(closureContext, i).ToLocalChecked();
853− //auto name = oldGlobalMemberNames->Get(oldContext, i).ToLocalChecked();
854− if(!closureContext->Global()->Set(closureContext, name, oldContext->Global()->Get(oldContext, name).ToLocalChecked()).FromMaybe(false)) {
855− //if(!closureContext->Global()->Set(closureContext, name, oldContext->Global()->Get(closureContext, name).ToLocalChecked()).FromMaybe(false)) {
856− Q_ASSERT(false);
857− }
858− }*/
859668 // Objects from closure need to be copied to global object too
860669 // V8TODO: I'm not sure which context to use with Get
861670 auto closureMemberNames = closureObject->GetPropertyNames(closureContext).ToLocalChecked();
862− //auto closureMemberNames = closureObject->GetPropertyNames(oldContext).ToLocalChecked();
863671 for (size_t i = 0; i < closureMemberNames->Length(); i++) {
864672 auto name = closureMemberNames->Get(closureContext, i).ToLocalChecked();
865− //auto name = closureMemberNames->Get(oldContext, i).ToLocalChecked();
866673 if(!closureContext->Global()->Set(closureContext, name, closureObject->Get(closureContext, name).ToLocalChecked()).FromMaybe(false)) {
867− //if(!closureContext->Global()->Set(closureContext, name, closureObject->Get(oldContext, name).ToLocalChecked()).FromMaybe(false)) {
868674 Q_ASSERT(false);
869675 }
870676 }
871− // List members of closure global object
872− //QString membersString("");
873− /*if (closureContext->Global()->IsObject()) {
874− v8::Local<v8::String> membersStringV8;
875− v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(closureContext->Global());
876− auto names = object->GetPropertyNames(closureContext).ToLocalChecked();
877− if (v8::JSON::Stringify(closureContext, names).ToLocal(&membersStringV8)) {
878− membersString = QString(*v8::String::Utf8Value(_v8Isolate, membersStringV8));
879− }
880− membersString = QString(*v8::String::Utf8Value(_v8Isolate, membersStringV8));
881− } else {
882− membersString = QString(" Is not an object");
883− }*/
884− //qCDebug(scriptengine_v8) << "Closure global before run:" << membersString;
885677 auto maybeResult = program.constGet()->GetUnboundScript()->BindToCurrentContext()->Run(closureContext);
886− //qCDebug(scriptengine_v8) << "Closure after run:" << scriptValueDebugDetailsV8(closure);
887678 v8::Local<v8::Value> v8Result;
888679 if (!maybeResult.ToLocal(&v8Result)) {
889680 v8::String::Utf8Value utf8Value(getIsolate(), tryCatch.Exception());
890681 QString errorMessage = QString(*utf8Value);
891682 qCWarning(scriptengine_v8) << __FUNCTION__ << "---------- hasCaught:" << errorMessage;
892683 qCWarning(scriptengine_v8) << __FUNCTION__ << "---------- tryCatch details:" << formatErrorMessageFromTryCatch(tryCatch);
893− //V8TODO: better error reporting
894684 }
895685
896686 if (hasUncaughtException()) {
@@ -908,20 +698,12 @@ ScriptValue ScriptEngineV8::evaluateInClosure(const ScriptValue& _closure, · 11 unmodified lines
908698 #endif
909699 popContext();
910700 }
911− //This is probably unnecessary in V8
912− /*if (oldGlobal.isValid()) {
913−#ifdef DEBUG_JS
914− qCDebug(shared) << " restoring global" << shortName;
915−#endif
916− setGlobalObject(oldGlobal);
917− }*/
918701
919702 _evaluatingCounter--;
920703 return result;
921704 }
922705
923706 ScriptValue ScriptEngineV8::evaluate(const QString& sourceCode, const QString& fileName) {
924− //V8TODO
925707
926708 if (QThread::currentThread() != thread()) {
927709 ScriptValue result;
@@ -936,7 +718,6 @@ ScriptValue ScriptEngineV8::evaluate(const QString& sourceCode, const QString& f · 8 unmodified lines
936718 return result;
937719 }
938720 // Compile and check syntax
939− // V8TODO: Could these all be replaced with checkSyntax function from wrapper?
940721 Q_ASSERT(!_v8Isolate->IsDead());
941722 _evaluatingCounter++;
942723 v8::Locker locker(_v8Isolate);
@@ -953,26 +734,6 @@ ScriptValue ScriptEngineV8::evaluate(const QString& sourceCode, const QString& f · 10 unmodified lines
953734 return nullValue();
954735 }
955736 }
956− //qCDebug(scriptengine_v8) << "Script compilation succesful: " << fileName;
957−
958− //V8TODO
959− /*auto syntaxError = lintScript(sourceCode, fileName);
960− if (syntaxError.isError()) {
961− if (!isEvaluating()) {
962− syntaxError.setProperty("detail", "evaluate");
963− }
964− raiseException(syntaxError);
965− maybeEmitUncaughtException("lint");
966− return syntaxError;
967− }*/
968− //V8TODO
969− /*if (script->IsNull()) {
970− // can this happen?
971− auto err = makeError(newValue("could not create V8ScriptProgram for " + fileName));
972− raiseException(err);
973− maybeEmitUncaughtException("compile");
974− return err;
975− }*/
976737
977738 v8::Local<v8::Value> result;
978739 v8::TryCatch tryCatchRun(getIsolate());
@@ -981,11 +742,6 @@ ScriptValue ScriptEngineV8::evaluate(const QString& sourceCode, const QString& f · 2 unmodified lines
981742 auto runError = tryCatchRun.Message();
982743 ScriptValue errorValue(new ScriptValueV8Wrapper(this, V8ScriptValue(this, runError->Get())));
983744 qCDebug(scriptengine_v8) << "Running script: \"" << fileName << "\" " << formatErrorMessageFromTryCatch(tryCatchRun);
984− //V8TODO
985−
986−
987− //raiseException(errorValue);
988− //maybeEmitUncaughtException("evaluate");
989745 setUncaughtException(tryCatchRun, "script evaluation");
990746
991747 _evaluatingCounter--;
@@ -1020,7 +776,6 @@ void ScriptEngineV8::setUncaughtException(const v8::TryCatch &tryCatch, const QS · 28 unmodified lines
1020776
1021777 QString errorMessage = "";
1022778 QString errorBacktrace = "";
1023− //v8::String::Utf8Value utf8Value(getIsolate(), tryCatch.Exception());
1024779 v8::String::Utf8Value utf8Value(getIsolate(), tryCatch.Message()->Get());
1025780
1026781 ex->errorMessage = QString(*utf8Value);
@@ -1068,7 +823,6 @@ QString ScriptEngineV8::formatErrorMessageFromTryCatch(v8::TryCatch &tryCatch) { · 41 unmodified lines
1068823 int errorLineNumber = 0;
1069824 QString errorMessage = "";
1070825 QString errorBacktrace = "";
1071− //v8::String::Utf8Value utf8Value(getIsolate(), tryCatch.Exception());
1072826 v8::String::Utf8Value utf8Value(getIsolate(), tryCatch.Message()->Get());
1073827 errorMessage = QString(*utf8Value);
1074828 v8::Local<v8::Message> exceptionMessage = tryCatch.Message();
@@ -1181,13 +935,6 @@ Q_INVOKABLE ScriptValue ScriptEngineV8::evaluate(const ScriptProgramPointer& pro · 106 unmodified lines
1181935 return result;
1182936 }
1183937 _evaluatingCounter++;
1184− /*bool is_isolate_exit_needed = false;
1185− if(!_v8Isolate->IsCurrent() && !_v8Locker) {
1186− // V8TODO: Theoretically only script thread should access this, so it should be safe
1187− _v8Locker.reset(new v8::Locker(_v8Isolate));
1188− _v8Isolate->Enter();
1189− is_isolate_exit_needed = true;
1190− }*/
1191938 ScriptValue errorValue;
1192939 ScriptValue resultValue;
1193940 bool hasFailed = false;
@@ -1213,14 +960,6 @@ Q_INVOKABLE ScriptValue ScriptEngineV8::evaluate(const ScriptProgramPointer& pro · 19 unmodified lines
1213960 v8::Local<v8::Value> result;
1214961 if(!hasFailed) {
1215962 const V8ScriptProgram& v8Program = unwrapped->toV8Value();
1216− // V8TODO
1217− /*if (qProgram.isNull()) {
1218− // can this happen?
1219− auto err = makeError(newValue("requested program is empty"));
1220− raiseException(err);
1221− maybeEmitUncaughtException("compile");
1222− return err;
1223− }*/
1224963
1225964 v8::TryCatch tryCatchRun(getIsolate());
1226965 if (!v8Program.constGet()->Run(getContext()).ToLocal(&result)) {
@@ -1240,10 +979,6 @@ Q_INVOKABLE ScriptValue ScriptEngineV8::evaluate(const ScriptProgramPointer& pro · 13 unmodified lines
1240979 }
1241980 }
1242981 _evaluatingCounter--;
1243− /*if (is_isolate_exit_needed) {
1244− _v8Isolate->Exit();
1245− _v8Locker.reset(nullptr);
1246− }*/
1247982 if (hasFailed) {
1248983 return errorValue;
1249984 } else {
@@ -1293,6 +1028,7 @@ ScriptValue ScriptEngineV8::newArrayBuffer(const QByteArray& message) { · 43 unmodified lines
12931028 std::shared_ptr<v8::BackingStore> backingStore(v8::ArrayBuffer::NewBackingStore(_v8Isolate, message.size()));
12941029 std::memcpy(backingStore.get()->Data(), message.constData(), message.size());
12951030 auto arrayBuffer = v8::ArrayBuffer::New(_v8Isolate, backingStore);
1031+ //V8TODO: this needs to be finished and tested
12961032 /*V8ScriptValue data = QScriptEngine::newVariant(QVariant::fromValue(message));
12971033 V8ScriptValue ctor = QScriptEngine::globalObject().property("ArrayBuffer");
12981034 auto array = qscriptvalue_cast<ArrayBufferClass*>(ctor.data());
@@ -1304,13 +1040,6 @@ ScriptValue ScriptEngineV8::newArrayBuffer(const QByteArray& message) { · 5 unmodified lines
13041040 }
13051041
13061042 ScriptValue ScriptEngineV8::newObject() {
1307− /*bool is_isolate_exit_needed = false;
1308− if(!_v8Isolate->IsCurrent() && !_v8Locker) {
1309− // V8TODO: Theoretically only script thread should access this, so it should be safe
1310− _v8Locker.reset(new v8::Locker(_v8Isolate));
1311− _v8Isolate->Enter();
1312− is_isolate_exit_needed = true;
1313− }*/
13141043 ScriptValue result;
13151044 {
13161045 v8::Locker locker(_v8Isolate);
@@ -1320,10 +1049,6 @@ ScriptValue ScriptEngineV8::newObject() { · 3 unmodified lines
13201049 V8ScriptValue resultV8 = V8ScriptValue(this, v8::Object::New(_v8Isolate));
13211050 result = ScriptValue(new ScriptValueV8Wrapper(this, std::move(resultV8)));
13221051 }
1323− /*if (is_isolate_exit_needed) {
1324− _v8Isolate->Exit();
1325− _v8Locker.reset(nullptr);
1326− }*/
13271052 return result;
13281053 }
13291054
@@ -1338,10 +1063,6 @@ ScriptValue ScriptEngineV8::newMethod(QObject* object, V8ScriptValue lifetime, · 8 unmodified lines
13381063 }
13391064
13401065 ScriptProgramPointer ScriptEngineV8::newProgram(const QString& sourceCode, const QString& fileName) {
1341− //V8TODO: is it used between isolates?
1342− //V8TODO: should it be compiled on creation?
1343− //V8ScriptProgram result(sourceCode, fileName);
1344−
13451066 v8::Locker locker(_v8Isolate);
13461067 v8::Isolate::Scope isolateScope(_v8Isolate);
13471068 v8::HandleScope handleScope(_v8Isolate);
@@ -1453,20 +1174,8 @@ void ScriptEngineV8::clearExceptions() { · 105 unmodified lines
14531174 }
14541175
14551176 ScriptContext* ScriptEngineV8::currentContext() const {
1456− //V8TODO
1457− /*V8ScriptContext* localCtx = QScriptEngine::currentContext();
1458− if (!localCtx) {
1459− return nullptr;
1460− }
1461− if (!_currContext || _currContext->toV8Value() != localCtx) {
1462− _currContext = std::make_shared<ScriptContextV8Wrapper>(const_cast<ScriptEngineV8*>(this), localCtx);
1463− }*/
1464− //_currContext = std::make_shared<ScriptContextV8Wrapper>(const_cast<ScriptEngineV8*>(this), localCtx);
1465− /*if (!_currContext) {
1466− // I'm not sure how to do this without discarding const
1467− _currContext = std::make_shared<ScriptContextV8Wrapper>(const_cast<ScriptEngineV8*>(this));
1468− }*/
14691177 // V8TODO: add FunctionCallbackInfo or PropertyCallbackInfo when necessary
1178+ // Is it needed?
14701179 return _contexts.last().get();
14711180 }
14721181
@@ -1482,16 +1191,6 @@ bool ScriptEngineV8::isEvaluating() const { · 9 unmodified lines
14821191
14831192 ScriptValue ScriptEngineV8::newFunction(ScriptEngine::FunctionSignature fun, int length) {
14841193 //V8TODO is callee() used for anything?
1485− /*auto innerFunc = [](V8ScriptContext* _context, QScriptEngine* _engine) -> V8ScriptValue {
1486− auto callee = _context->callee();
1487− QVariant funAddr = callee.property("_func").toVariant();
1488− ScriptEngine::FunctionSignature fun = reinterpret_cast<ScriptEngine::FunctionSignature>(funAddr.toULongLong());
1489− ScriptEngineV8* engine = static_cast<ScriptEngineV8*>(_engine);
1490− ScriptContextV8Wrapper context(engine, _context);
1491− ScriptValue result = fun(&context, engine);
1492− ScriptValueV8Wrapper* unwrapped = ScriptValueV8Wrapper::unwrap(result);
1493− return unwrapped ? unwrapped->toV8Value() : V8ScriptValue();
1494− };*/
14951194
14961195 v8::Locker locker(_v8Isolate);
14971196 v8::Isolate::Scope isolateScope(_v8Isolate);
@@ -1518,23 +1217,12 @@ ScriptValue ScriptEngineV8::newFunction(ScriptEngine::FunctionSignature fun, int · 20 unmodified lines
15181217 info.GetReturnValue().Set(unwrapped->toV8Value().constGet());
15191218 }
15201219 };
1521− //auto functionTemplate = v8::FunctionTemplate::New(_v8Isolate, v8FunctionCallback, v8::Local<v8::Value>(), v8::Local<v8::Signature>(), length);
1522− //auto functionData = v8::Object::New(_v8Isolate);
1523− //functionData->setIn
15241220 auto functionDataTemplate = getFunctionDataTemplate();
1525− //auto functionDataTemplate = v8::ObjectTemplate::New(_v8Isolate);
1526− //functionDataTemplate->SetInternalFieldCount(2);
15271221 auto functionData = functionDataTemplate->NewInstance(getContext()).ToLocalChecked();
15281222 functionData->SetAlignedPointerInInternalField(0, reinterpret_cast<void*>(fun));
15291223 functionData->SetAlignedPointerInInternalField(1, reinterpret_cast<void*>(this));
1530− //functionData->SetInternalField(3, v8::Null(_v8Isolate));
15311224 auto v8Function = v8::Function::New(getContext(), v8FunctionCallback, functionData, length).ToLocalChecked();
1532− //auto functionObjectTemplate = functionTemplate->InstanceTemplate();
1533− //auto function =
1534− V8ScriptValue result(this, v8Function); // = QScriptEngine::newFunction(innerFunc, length);
1535− //auto funAddr = QScriptEngine::newVariant(QVariant(reinterpret_cast<qulonglong>(fun)));
1536− // V8TODO
1537− //result.setProperty("_func", funAddr, V8ScriptValue::PropertyFlags(V8ScriptValue::ReadOnly + V8ScriptValue::Undeletable + V8ScriptValue::SkipInEnumeration));
1225+ V8ScriptValue result(this, v8Function);
15381226 return ScriptValue(new ScriptValueV8Wrapper(this, std::move(result)));
15391227 }
15401228
@@ -1543,7 +1231,6 @@ void ScriptEngineV8::setObjectName(const QString& name) { · 2 unmodified lines
15431231 QObject::setObjectName(name);
15441232 }
15451233
1546−//V8TODO
15471234 bool ScriptEngineV8::setProperty(const char* name, const QVariant& value) {
15481235 v8::Locker locker(_v8Isolate);
15491236 v8::Isolate::Scope isolateScope(_v8Isolate);
@@ -1570,24 +1257,10 @@ void ScriptEngineV8::setThread(QThread* thread) { · 20 unmodified lines
15701257 qCDebug(scriptengine_v8) << "Script engine " << objectName() << " exited isolate";
15711258 }
15721259 Q_ASSERT(QObject::thread() == QThread::currentThread());
1573− /*if (_v8Locker) {
1574− _v8Locker.reset();
1575− }*/
15761260 moveToThread(thread);
15771261 qCDebug(scriptengine_v8) << "Moved script engine " << objectName() << " to different thread";
15781262 }
15791263
1580−/*void ScriptEngineV8::enterIsolateOnThisThread() {
1581− Q_ASSERT(thread() == QThread::currentThread());
1582− Q_ASSERT(!_v8Locker);
1583− _v8Locker.reset(new v8::Locker(_v8Isolate));
1584− if (!_v8Isolate->IsCurrent()) {
1585− _v8Isolate->Enter();
1586− qCDebug(scriptengine_v8) << "Script engine " << objectName() << " entered isolate on a new thread";
1587− }
1588−}*/
1589−
1590−
15911264 std::shared_ptr<ScriptException> ScriptEngineV8::uncaughtException() const {
15921265 if (_uncaughtException) {
15931266 return _uncaughtException->clone();
@@ -1601,8 +1274,7 @@ bool ScriptEngineV8::raiseException(const QString& error, const QString &reason) · 7 unmodified lines
16011274 }
16021275
16031276 bool ScriptEngineV8::raiseException(const ScriptValue& exception, const QString &reason) {
1604− //V8TODO
1605− //Q_ASSERT(false);
1277+ //V8TODO Im not sure how to finish these
16061278 // qCCritical(scriptengine_v8) << "Script exception occurred: " << exception.toString();
16071279 // ScriptValueV8Wrapper* unwrapped = ScriptValueV8Wrapper::unwrap(exception);
16081280 // V8ScriptValue qException = unwrapped ? unwrapped->toV8Value() : QScriptEngine::newVariant(exception.toVariant());
@@ -1626,7 +1298,6 @@ bool ScriptEngineV8::raiseException(const V8ScriptValue& exception) { · 17 unmodified lines
16261298
16271299 _v8Isolate->ThrowException(exception.constGet());
16281300
1629−
16301301 /*if (QScriptEngine::currentContext()) {
16311302 // we have an active context / JS stack frame so throw the exception per usual
16321303 QScriptEngine::currentContext()->throwValue(makeError(exception));
@@ -1741,15 +1412,6 @@ QString ScriptEngineV8::scriptValueDebugDetailsV8(const V8ScriptValue &v8Value) · 108 unmodified lines
17411412 return parentValueQString + QString(" JSON: ") + JSONQString;
17421413 }
17431414
1744−/*QStringList ScriptEngineV8::getCurrentStackTrace() {
1745− v8::Local<v8::StackTrace> stackTrace = v8::StackTrace::CurrentStackTrace(_v8Isolate, 100);
1746− QStringList backtrace;
1747− for (int n = 0; n < stackTrace->GetFrameCount(); n++) {
1748− v8::Local<v8::StackFrame> stackFrame = stackTrace->GetFrame(_v8Isolate, n);
1749−
1750− }
1751−}*/
1752−
17531415 void ScriptEngineV8::logBacktrace(const QString &title) {
17541416 QStringList backtrace = currentContext()->backtrace();
17551417 qCDebug(scriptengine_v8) << title;