qtbridge-runtime 0.1.4

Qt Bridge: bridging code to be run in applications.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only

#include "dynamicmetaobjectbuilder.h"
#include "rustconv.h"
#include "rustobjectgetter.h"
#include <QMetaType>
#include <QObject>
#include <QScopedPointer>
#include <QSpan>
#include <QVariant>
#include <private/qobject_p.h>
#include <private/qmetaobjectbuilder_p.h>
#include <cassert>
#include <map>
#include <optional>
#include <stdexcept>

using namespace std::string_literals;

class DynamicMetaObjectBuilder::Impl : public QDynamicMetaObjectData
{
public:
    using PropertyId = int;
    using SignalId = int;
    using SlotId = int;

    Impl(const QMetaObject* staticMetaObj, const QByteArray& className)
        : m_mob(std::make_unique<QMetaObjectBuilder>())
    {
        m_mob->setSuperClass(staticMetaObj); // TODO: check without this
        m_mob->setClassName(className.isEmpty() ? QByteArray(staticMetaObj->className()) : className);
    }

    void addClassInfo(const QByteArray& name, const QByteArray& value) {
        m_mob->addClassInfo(name, value);
    }

    // TODO: assume that
    //      notifySignal = name + "Changed"; ?
    void registerProperty(const QByteArray& name, const QMetaType& metaType, PropertyGetterFn getter, std::optional<PropertySetterFn> setter, bool isConstant, const QByteArray& notifySignal)
    {
        std::optional<int> signal;
        if (!notifySignal.isEmpty())
        {
            signal = getSignalIndexByName(notifySignal);
            if (!signal)
                throw std::runtime_error("Failed to find signal by name");
        }

        doRegisterProperty(name, metaType, std::move(getter), std::move(setter), isConstant, signal);
    }

    void registerSignal(const QByteArray& name, QSpan<const QMetaType> argMetaTypes)
    {
        if (!m_mob)
            throw std::runtime_error("Signal registration must be done before endMetaRegistration() call");

        for (const QMetaType& type: argMetaTypes)
            type.registerType();

        QByteArray signature = generateFuncSignature(name, argMetaTypes);
        QMetaMethodBuilder builder = m_mob->addSignal(signature);
        const int localId = builder.index();
        auto [_, added] = m_signals.emplace(localId, SignalInfo{ name });
        if (!added)
            throw std::runtime_error("Failed to register signal");
    }

    void registerSlot(const QByteArray& name, QSpan<const QMetaType> argMetaTypes, const QMetaType& returnMetaType, SlotCallbackFn&& func)
    {
        if (!m_mob)
            throw std::runtime_error("Slot registration must be done before endMetaRegistration() call");

        for (const QMetaType& type: argMetaTypes)
            type.registerType();

        QByteArray signature = generateFuncSignature(name, argMetaTypes);
        QMetaMethodBuilder builder = m_mob->addSlot(signature);
        if (returnMetaType.isValid())
            builder.setReturnType(returnMetaType.name());
        const int localId = builder.index();

        m_slots.emplace(localId, SlotInfo{ std::move(func) });
    }

    void endMetaRegistration()
    {
        if (m_mob)
        {
            m_mo.reset(m_mob->toMetaObject());
            m_mob.reset();
        }
        else
        {
            assert(false && "The function is called more than once");
        }
    }

    void emitSignal(QObject& obj, const QByteArray& name, rust::Slice<const uint8_t* const> argv)
    {
        if (auto idx = getSignalIndexByName(name))
            doEmitSignal(obj, *idx, argv);
        else
            throw std::runtime_error("Failed to find signal by name");
    }

    const QMetaObject* getDynamicQMetaObject()
    {
        if (!m_mo)
            endMetaRegistration();

        return m_mo.get();
    }

private:
    void doRegisterProperty(const QByteArray& name, const QMetaType& metaType, PropertyGetterFn getter, std::optional<PropertySetterFn> setter, bool isConstant, std::optional<int> signalIndex)
    {
        if (!m_mob)
            throw std::runtime_error("Property registration must be done before endMetaRegistration() call");

        if (!metaType.isValid())
            throw std::runtime_error("Invalid property type");

        const bool writable = setter.has_value();
        metaType.registerType();

        //TODO: pass notifierId argument to addProperty?
        QMetaPropertyBuilder builder = m_mob->addProperty(name, metaType.name());
        builder.setReadable(true);
        builder.setWritable(writable);
        builder.setConstant(!writable && isConstant);

        if (signalIndex)
        {
            const int idx = *signalIndex;
            if (!m_signals.count(idx))
                throw std::runtime_error("Unknown property change signal");
            builder.setNotifySignal(m_mob->method(idx));
        }

        const auto localId = builder.index();
        auto [_, added] = m_properties.emplace(localId, PropertyInfo{ metaType, std::move(getter), std::move(*setter) });
        if (!added)
            throw std::runtime_error("Failed to register property");
    }

    void doEmitSignal(QObject& obj, SignalId id, void** params)
    {
        if (!m_mo)
            throw std::logic_error(__func__ + " called before endMetaRegistration()"s);
        QMetaObject::activate(&obj, m_mo.get(), id, params);
    }

     void doEmitSignal(QObject& obj, SignalId id, rust::Slice<const uint8_t* const> argv)
     {
        doEmitSignal(obj, id, reinterpret_cast<void**>(const_cast<uint8_t**>(argv.data())));
    }

    void objectDestroyed(QObject *) override
    {
        // Do nothing here unlike QDynamicMetaObjectData
        // to avoid double deletion
    }

    QMetaObject* toDynamicMetaObject(QObject* /*o*/) override
    {
        if (!m_mo)
            endMetaRegistration();

        return m_mo.get();
    }

    int metaCall(QObject* o, QMetaObject::Call call, int id, void** argv) override
    {
        if (!m_mo)
            throw std::logic_error(__func__ + " called before endMetaRegistration()"s);

        auto rustPtrGetter = dynamic_cast<const RustObjectGetter*>(o);
        if (!rustPtrGetter)
            throw std::runtime_error("Failed to get pointer to rust object");
        uint8_t* rustPtr = rustPtrGetter->getRustObject();

        if (rustPtr)
        {
            switch (call)
            {
                case QMetaObject::InvokeMetaMethod:
                    if (handleMetaCallInvoke(o, rustPtr, id, argv))
                        return -1;
                break;
                case QMetaObject::ReadProperty:
                    if (handleMetaCallReadProperty(rustPtr, id, argv))
                        return -1;
                break;
                case QMetaObject::WriteProperty:
                    if (handleMetaCallWriteProperty(rustPtr, id, argv))
                        return -1;
                break;
                default:
                break;
            }
        }

        return o->qt_metacall(call, id, argv);
    }

    bool handleMetaCallInvoke(QObject* o, uint8_t* clientPtr, int id, void** argv)
    {
        const int methodId = id - m_mo->methodOffset();
        if (methodId < 0 || methodId >= m_mo->methodCount())
            return false;

        QMetaMethod method = m_mo->method(id);
        switch (method.methodType())
        {
            case QMetaMethod::Signal:
            {
                if (!m_signals.count(methodId))
                    return false;
                doEmitSignal(*o, methodId, argv);
                return true;
            }
            break;
            case QMetaMethod::Slot:
            {
                auto slotIt = m_slots.find(methodId);
                if (slotIt == m_slots.end())
                    return false;

                const int paramCount = method.parameterCount();
                const QMetaType returnType = method.returnMetaType();
                if ((paramCount > 0 || returnType.isValid()) && !argv)
                    throw std::runtime_error("Input meta params are null");

                uint8_t* const* u8Argv = reinterpret_cast<uint8_t* const*>(argv);
                const uint8_t* const* inputsBegin = u8Argv + 1;
                rust::Slice inputsSlice(inputsBegin, static_cast<size_t>(paramCount));
                auto outputSlice = returnType.isValid() ?
                    rust::Slice<uint8_t* const>(u8Argv, 1) :
                    rust::Slice<uint8_t* const>();
                slotIt->second.m_callback(clientPtr, inputsSlice, outputSlice);
                return true;
            }
            break;
            default:
            break;
        }

        return false;
    }

    bool handleMetaCallReadProperty(uint8_t* clientPtr, int id, void** argv)
    {
        const int propId = id - m_mo->propertyOffset();
        if (propId < 0 || propId >= m_mo->propertyCount())
            return false;

        void* dstArg = argv[0];
        if (!dstArg)
            return false;

        auto propIt = m_properties.find(propId);
        if (propIt == m_properties.end())
            return false;

        auto& getterFunc = propIt->second.m_getter;

        const QMetaProperty property = m_mo->property(id);
        const QVariant result = getterFunc(clientPtr);
        if (!QMetaType::convert(result.metaType(), result.data(), property.metaType(), dstArg))
            throw std::logic_error("Property type mismatch");

        return true;
    }

    bool handleMetaCallWriteProperty(uint8_t* clientPtr, int id, void** argv)
    {
        const int propId = id - m_mo->propertyOffset();
        if (propId < 0 || propId >= m_mo->propertyCount())
            return false;

        void* arg = argv[0];
        if (!arg)
            return false;

        auto propIt = m_properties.find(propId);
        if (propIt == m_properties.end())
            return false;

        auto& setterFunc = propIt->second.m_setter;
        const QMetaProperty property = m_mo->property(id);
        const auto v = QVariant::fromMetaType(property.metaType(), arg);
        setterFunc(clientPtr, v);

        return true;
    }

    static QByteArray generateFuncSignature(const QByteArray& name, const QSpan<const QMetaType>& argMetaTypes)
    {
        QString paramStr;
        for (const auto& type : argMetaTypes)
        {
            if (!type.isValid())
                throw std::runtime_error("Unspecified argument type");

            if (!paramStr.isEmpty())
                paramStr.append(',');
            paramStr += type.name();
        }

        QString sign = name + '(' + paramStr + ')';
        return QMetaObject::normalizedSignature(sign.toStdString().c_str());
    }

    QMetaMethod getMetaMethod(int id) const
    {
        if (!m_mo)
            throw std::logic_error(__func__ + " called before endMetaRegistration()"s);

        const int methodOffset = m_mo->methodOffset();
        const QMetaMethod method = m_mo->method(id + methodOffset);
        return method;
    }

    std::optional<int> getSignalIndexByName(const QByteArray& name) const
    {
        for (const auto& [idx, signalInfo] : m_signals)
        {
            if (signalInfo.m_name == name)
                return idx;
        }
        return std::nullopt;
    }

private:
    struct PropertyInfo
    {
        QMetaType m_type;
        PropertyGetterFn m_getter;
        PropertySetterFn m_setter;
    };

    struct SignalInfo
    {
        QByteArray m_name;
    };

    struct SlotInfo
    {
        SlotCallbackFn m_callback;
    };

private:
    std::unique_ptr<QMetaObjectBuilder> m_mob;
    std::unique_ptr<QMetaObject, QScopedPointerPodDeleter> m_mo;
    std::map<PropertyId, PropertyInfo> m_properties;
    std::map<SignalId, SignalInfo> m_signals;
    std::map<SlotId, SlotInfo> m_slots;
};


DynamicMetaObjectBuilder::DynamicMetaObjectBuilder(const QMetaObject* staticMetaObj, rust::Str className)
    : m_impl(std::make_unique<Impl>(staticMetaObj, RustStrToQByteArray(className)))
{}

void DynamicMetaObjectBuilder::setToQObject(QObject& dst) const
{
    QObjectPrivate::get(&dst)->metaObject = m_impl.get();
}

const QMetaObject* DynamicMetaObjectBuilder::getDynamicQMetaObject() const
{
    return m_impl->getDynamicQMetaObject();
}

void DynamicMetaObjectBuilder::addClassInfo(rust::Str name, rust::Str value)
{
    m_impl->addClassInfo(RustStrToQByteArray(name), RustStrToQByteArray(value));
}

void DynamicMetaObjectBuilder::registerProperty(rust::Str name, const QMetaType& metaType, PropertyGetterFn getter, PropertySetterFn setter, rust::Str notifySignal)
{
    m_impl->registerProperty(RustStrToQByteArray(name), metaType, std::move(getter), std::move(setter), false, RustStrToQByteArray(notifySignal));
}

void DynamicMetaObjectBuilder::registerPropertyReadOnly(rust::Str name, const QMetaType& metaType, PropertyGetterFn getter, bool isConstant, rust::Str notifySignal)
{
    m_impl->registerProperty(RustStrToQByteArray(name), metaType, std::move(getter), std::nullopt, isConstant, RustStrToQByteArray(notifySignal));
}

void DynamicMetaObjectBuilder::registerSignal(rust::Str name, rust::Slice<const QMetaType> argMetaTypes)
{
    m_impl->registerSignal(RustStrToQByteArray(name), RustSliceToQSpan(argMetaTypes));
}

void DynamicMetaObjectBuilder::registerSlot(rust::Str name, rust::Slice<const QMetaType> argMetaTypes, const QMetaType& returnMetaType, SlotCallbackFn callback)
{
    m_impl->registerSlot(RustStrToQByteArray(name), RustSliceToQSpan(argMetaTypes), returnMetaType, std::move(callback));
}

void DynamicMetaObjectBuilder::endMetaRegistration()
{
    m_impl->endMetaRegistration();
}

void DynamicMetaObjectBuilder::emitSignal(QObject& obj, rust::Str name, rust::Slice<const uint8_t* const> argv) const
{
    m_impl->emitSignal(obj, RustStrToQByteArray(name), argv);
}

DynamicMetaObjectBuilder *createDynamicMetaObjectBuilder(rust::Str rustStructName, const QMetaObject& staticMeta)
{
    return new DynamicMetaObjectBuilder(&staticMeta, rustStructName);
}