Files
logos-cpp-sdk/cpp/module_proxy.cpp
Dario LipicarandClaude Opus 4.8 7b62ac2017 Per-event documentation + getPluginEvents introspection (#71)
* Add per-event documentation + getPluginEvents introspection

Mirror the per-method documentation pipeline for events. Events
(declared in a universal module's logos_events: section) now carry a
description parsed from their /// doc comments, and are introspectable
at runtime via a new getPluginEvents framework call.

- lidl_ast: EventDecl gains a description field.
- impl_header_parser: capture the event's doc comment (previously
  discarded) and an optional metadata.json events[].description.
- lidl_gen_provider: generated universal provider emits
  getEvents() override, mirroring getMethods() (name/signature/
  parameters/description; no returnType/isInvokable — events are void).
- logos_provider_object: default-empty virtual getEvents() so the
  legacy provider path and QtProviderObject inherit empty.
- module_proxy / qt_provider_object: intercept getPluginEvents next to
  the getPluginMethods special-case.
- docs: spec + README event-documentation notes.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* Add unit tests for event documentation + getEvents generation

Address review feedback (#71): cover the event-introspection paths that
previously only had method-side tests.

- impl_header_parser test: assert metadata.json events[].description is
  parsed; new documented_events fixture asserts `///` doc-comment
  capture on a logos_events: block (multi-line joined with \n,
  adjacent-only, plain // ignored).
- lidl_gen_provider test: assert the generated dispatch contains
  getEvents() emitting each event's name/signature/parameters and an
  escaped description, and that events carry no returnType/isInvokable.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* Fold event introspection into getMethods() to keep the provider ABI stable

The previous approach added a getEvents() virtual to LogosProviderObject,
which inserted a new vtable slot and shifted every later slot — an ABI
break that would misdispatch virtual calls whenever an old and new
host/module were mixed across the in-process plugin boundary.

Instead, report events INSIDE the existing getMethods() call: it now
returns the module's whole interface, with each entry tagged
type "method" or "event" (events omit returnType/isInvokable). The
provider vtable is therefore byte-for-byte unchanged, so old/new hosts
and modules stay binary-compatible — a new host reading an old module
sees no event entries (zero events), and an old host reading a new
module just ignores the "type" field (cosmetic). An entry with no
"type" is treated as a method.

- logos_provider_object.h: remove the getEvents() virtual; document that
  getMethods() carries both, and why.
- generator (lidl_gen_provider): emit events as type "event" entries
  inside getMethods(); tag methods type "method"; no getEvents() output.
- module_proxy / qt_provider_object: getPluginMethods()/getPluginEvents()
  are now type-filtered views of getMethods(), plus a new
  getPluginInterface() returning the whole list. (These are name-
  dispatched Q_INVOKABLEs, not vtable surface — adding them is safe.)
- tests: generator asserts events fold into getMethods() tagged "event";
  ModuleProxy asserts the three filtered views; parser tests unchanged.
- docs: spec/project/docs/README updated, incl. an ABI rationale note.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-04 17:06:12 -03:00

131 lines
4.5 KiB
C++

#include "module_proxy.h"
#include "logos_provider_object.h"
#include <QDebug>
#include <QJsonObject>
#include <QJsonValue>
ModuleProxy::ModuleProxy(LogosProviderObject* provider, QObject* parent)
: QObject(parent)
, m_provider(provider)
{
if (m_provider) {
m_provider->setEventListener([this](const QString& eventName, const QVariantList& data) {
qDebug() << "[LogosProviderObject] ModuleProxy: forwarding event" << eventName << "as Qt signal";
// Events may be fired from any thread (e.g. a module's worker/FFI
// thread), but this object is the QtRemoteObjects source and must be
// driven from its own thread. Emitting directly from a foreign
// thread runs QtRO's source serialization there, racing the source
// socket against a reply being sent from the source thread, which
// can silently drop the reply. AutoConnection keeps same-thread
// callers synchronous (the common case) and only queues the
// emission when it arrives from another thread, so events and
// replies stay serialized on the thread QtRO expects to own the
// source. Passing `this` as the context also cancels a queued
// emission if this object is destroyed first.
QMetaObject::invokeMethod(this, [this, eventName, data]() {
emit eventResponse(eventName, data);
}, Qt::AutoConnection);
});
qDebug() << "[LogosProviderObject] ModuleProxy: created, wrapping LogosProviderObject"
<< m_provider->providerName();
}
}
ModuleProxy::~ModuleProxy()
{
qDebug() << "ModuleProxy: destroyed";
}
bool ModuleProxy::saveToken(const QString& from_module_name, const QString& token)
{
if (from_module_name.isEmpty()) {
qWarning() << "ModuleProxy: Cannot save token with empty module name";
return false;
}
if (token.isEmpty()) {
qWarning() << "ModuleProxy: Cannot save empty token for module:" << from_module_name;
return false;
}
m_tokens[from_module_name] = token;
qDebug() << "ModuleProxy: Token saved for module:" << from_module_name;
return true;
}
QVariant ModuleProxy::callRemoteMethod(const QString& authToken, const QString& methodName, const QVariantList& args)
{
if (!m_provider) {
qWarning() << "ModuleProxy: Cannot call method on null provider:" << methodName;
return QVariant();
}
if (methodName.isEmpty()) {
qWarning() << "ModuleProxy: Method name cannot be empty";
return QVariant();
}
if (methodName == "getPluginMethods" && args.isEmpty()) {
return QVariant(getPluginMethods());
}
if (methodName == "getPluginEvents" && args.isEmpty()) {
return QVariant(getPluginEvents());
}
if (methodName == "getPluginInterface" && args.isEmpty()) {
return QVariant(getPluginInterface());
}
qDebug() << "ModuleProxy: callRemoteMethod" << methodName << "args:" << args;
return m_provider->callMethod(methodName, args);
}
bool ModuleProxy::informModuleToken(const QString& authToken, const QString& moduleName, const QString& token)
{
Q_UNUSED(authToken)
if (!m_provider) {
qWarning() << "ModuleProxy: Cannot inform token on null provider";
return false;
}
return m_provider->informModuleToken(moduleName, token);
}
namespace {
// getMethods() returns the module's full interface — both methods and events,
// each tagged with a "type" ("method"/"event"). Split it back out. An entry
// with no "type" counts as a method, so modules built against the pre-events
// SDK (whose getMethods() contains no events) report zero events, not a crash.
QJsonArray filterInterface(const QJsonArray& interface, bool keepEvents)
{
QJsonArray out;
for (const QJsonValue& v : interface) {
const bool isEvent =
v.toObject().value(QStringLiteral("type")).toString() == QStringLiteral("event");
if (isEvent == keepEvents) out.append(v);
}
return out;
}
} // namespace
QJsonArray ModuleProxy::getPluginInterface()
{
if (!m_provider) return QJsonArray();
qDebug() << "[LogosProviderObject] ModuleProxy: calling LogosProviderObject::getMethods()";
return m_provider->getMethods();
}
QJsonArray ModuleProxy::getPluginMethods()
{
return filterInterface(getPluginInterface(), /*keepEvents=*/false);
}
QJsonArray ModuleProxy::getPluginEvents()
{
return filterInterface(getPluginInterface(), /*keepEvents=*/true);
}
#include "moc_module_proxy.cpp"