Added debug function to list object members

This commit is contained in:
ksuprynowicz 2023-01-09 20:24:49 +01:00
parent ed968e1ebd
commit 5f2c13da52
4 changed files with 33 additions and 2 deletions

View file

@ -128,7 +128,7 @@ public:
virtual void requestCollectGarbage() = 0;
virtual void compileTest() = 0;
virtual QString scriptValueDebugDetails(const ScriptValue &value) = 0;
virtual QString scriptValueDebugListMembers(const ScriptValue &value) = 0;
public:
// helper to detect and log warnings when other code invokes QScriptEngine/BaseScriptEngine in thread-unsafe ways
bool IS_THREADSAFE_INVOCATION(const QString& method);

View file

@ -1342,7 +1342,7 @@ ScriptValue ScriptManager::newModule(const QString& modulePath, const ScriptValu
// module.require is a bound version of require that always resolves relative to that module's path
auto boundRequire = _engine->evaluate("(function(id) { return Script.require(Script.require.resolve(id, this.filename)); })", "(boundRequire)");
module.setProperty("require", boundRequire, READONLY_PROP_FLAGS);
qDebug() << "Module object contents" << _engine->scriptValueDebugListMembers(module);
return module;
}

View file

@ -821,6 +821,9 @@ ScriptValue ScriptEngineV8::evaluateInClosure(const ScriptValue& _closure,
ScriptValue result;
//auto context = pushContext();
// V8TODO: a lot of functions rely on _v8Context, which was not updated here
// It might cause trouble
{
v8::Context::Scope contextScope(closureContext);
const V8ScriptValue& closure = unwrappedClosure->toV8Value();
@ -1527,6 +1530,32 @@ QString ScriptEngineV8::scriptValueDebugDetails(const ScriptValue &value) {
return scriptValueDebugDetailsV8(v8Value);
}
QString ScriptEngineV8::scriptValueDebugListMembers(const ScriptValue &value) {
V8ScriptValue v8Value = ScriptValueV8Wrapper::fullUnwrap(this, value);
return scriptValueDebugDetailsV8(v8Value);
}
QString ScriptEngineV8::scriptValueDebugListMembersV8(const V8ScriptValue &v8Value) {
v8::Locker locker(_v8Isolate);
v8::Isolate::Scope isolateScope(_v8Isolate);
v8::HandleScope handleScope(_v8Isolate);
v8::Context::Scope contextScope(getContext());
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(), object).ToLocal(&membersStringV8)) {
membersString = QString(*v8::String::Utf8Value(_v8Isolate, membersStringV8));
}
membersString = QString(*v8::String::Utf8Value(_v8Isolate, membersStringV8));
} else {
membersString = QString(" Is not an object");
}
return membersString;
}
QString ScriptEngineV8::scriptValueDebugDetailsV8(const V8ScriptValue &v8Value) {
v8::Locker locker(_v8Isolate);
v8::Isolate::Scope isolateScope(_v8Isolate);

View file

@ -129,6 +129,8 @@ public: // ScriptEngine implementation
virtual void compileTest() override;
virtual QString scriptValueDebugDetails(const ScriptValue &value) override;
QString scriptValueDebugDetailsV8(const V8ScriptValue &value);
virtual QString scriptValueDebugListMembers(const ScriptValue &value) override;
QString scriptValueDebugListMembersV8(const V8ScriptValue &v8Value);
// helper to detect and log warnings when other code invokes QScriptEngine/BaseScriptEngine in thread-unsafe ways
inline bool IS_THREADSAFE_INVOCATION(const QString& method) { return ScriptEngine::IS_THREADSAFE_INVOCATION(method); }