Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qharmonynativeinterface.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <QtCore/qcoreapplication_platform.h>
5
6#include <QtCore/private/qcore_ohos_p.h>
7#include <QtCore/private/qnativeinterface_p.h>
8#include <QtCore/private/qohosjstools_p.h>
9#include <functional>
10
11QT_BEGIN_NAMESPACE
12
13namespace {
14
16 QOhosJsState &jsState, const char *callerContextName,
17 const std::function<void(QHarmonyJsContext &)> &task)
18{
19 runQOhosJsCodeLoggingExceptions(
20 jsState.env(), callerContextName,
21 [&]() {
22 task(QHarmonyJsContext::fromEnv(jsState.env()));
23 });
24}
25
26}
27
28/*!
29 \class QNativeInterface::QHarmonyApplication
30 \brief Native interface to a core application on HarmonyOS.
31
32 Accessed through QCoreApplication::nativeInterface().
33
34 On HarmonyOS, the ArkTS runtime and the platform APIs run on the main
35 thread of the process, called the JS thread. The Qt application runs on
36 a separate thread, called the Qt thread. This is the thread of the
37 QCoreApplication object. This interface runs application code on the JS
38 thread, where the code receives a QHarmonyJsContext. It also brings
39 results back to the Qt thread.
40
41 Code that runs on the JS thread must follow these rules:
42 \list
43 \li It must not wait for the Qt thread, which may itself be waiting in
44 runOnJsThreadAndWait(). Send results back asynchronously, with
45 runOnQtThread() or through QPromise.
46 \li It must not access Qt-thread objects directly, not even through a
47 QPointer. A QPointer is created, checked, and dereferenced only on
48 the thread of its object. The JS thread only passes copies of it
49 along. The functor posted to the Qt thread checks the QPointer
50 there.
51 \li A JS callback may run after the Qt event loop has finished. Tasks
52 that it queues with runOnQtThread() then never run.
53 \li A \c napi_value that it obtains is valid until the task returns, and
54 must not leave the JS thread. To keep a JS object longer, use a
55 QHarmonyJsObjectRef.
56 \li Resources bound to the JS engine, such as references and listeners,
57 must be released on the JS thread, unless their documentation
58 states otherwise.
59 \li The JS thread also runs the platform UI, so tasks must be short.
60 \endlist
61
62 Each task runs inside its own Node-API handle scope. A C++ exception
63 that a task throws and a JS exception that it leaves pending are caught
64 and logged on the JS thread. A task that needs to react to a failure
65 must handle it itself.
66
67 \inmodule QtCore
68 \inheaderfile QCoreApplication
69 \ingroup native-interfaces
70 \ingroup native-interfaces-qcoreapplication
71
72 \sa {Qt for HarmonyOS Platform API}
73*/
75
76/*!
77 Queues \a task for execution on the JS thread and returns immediately.
78 You can call this function from any thread. This is the primary way to
79 reach the platform APIs. \a task is left empty, and the task it held,
80 with everything it captures, is destroyed on the JS thread.
81
82 Tasks queued with runOnJsThread() and runOnJsThreadAndWait() run one at a
83 time, in the order in which they were queued.
84*/
85void QNativeInterface::QHarmonyApplication::runOnJsThread(std::function<void(QHarmonyJsContext &)> &&task)
86{
87 const char *funcInfo = Q_FUNC_INFO;
88
89 QOhosJsThreadGateway::invoke(
90 [sharedTask = QtOhos::takeFunctionToSharedPtr(task), funcInfo](QOhosJsState &jsState) {
91 const auto takenTask = std::exchange(*sharedTask, nullptr);
92 runTaskLoggingExceptions(jsState, funcInfo, takenTask);
93 });
94}
95
96/*!
97 Runs \a task on the JS thread and blocks the calling thread until
98 \a task returns. You can call this function from any thread. \a task
99 runs after the tasks queued before it with runOnJsThread() or
100 runOnJsThreadAndWait(). When called on the JS thread, it runs \a task
101 directly, before any task that is still queued. \a task must not wait for
102 the calling thread.
103*/
104void QNativeInterface::QHarmonyApplication::runOnJsThreadAndWait(const std::function<void(QHarmonyJsContext &)> &task)
105{
106 const char *funcInfo = Q_FUNC_INFO;
107
108 QOhosJsThreadGateway::runAndWait(
109 [&](QOhosJsState &jsState) {
110 runTaskLoggingExceptions(jsState, funcInfo, task);
111 },
112 funcInfo);
113}
114
115/*!
116 \fn template <typename Func, typename Result> Result QNativeInterface::QHarmonyApplication::runOnJsThreadAndWait(Func &&task)
117
118 Runs \a task on the JS thread, blocks the calling thread until \a task
119 returns, and returns its result. \a task is ordered like in the overload
120 without a result. If \a task throws an exception or leaves a JS
121 exception pending, the exception is logged on the JS thread and a
122 default-constructed value is returned. The result must not be a
123 \c napi_value or a \c napi_ref. To bring JS data back, convert it with
124 the QtHarmony functions or keep it in a QHarmonyJsObjectRef.
125*/
126
127/*!
128 Queues \a task for execution on the Qt thread, the thread of the
129 QCoreApplication object, and returns immediately. \a task is left empty.
130 You can call this function from any thread at any time. Use it to bring
131 results back from the JS thread. Tasks queued with runOnQtThread() run
132 one at a time, in the order in which they were queued. A task queued
133 after the Qt event loop has finished for good never runs.
134*/
135void QNativeInterface::QHarmonyApplication::runOnQtThread(std::function<void()> &&task)
136{
137 QtOhos::invokeInQtThread(std::move(task));
138}
139
140QT_END_NAMESPACE
void runTaskLoggingExceptions(QOhosJsState &jsState, const char *callerContextName, const std::function< void(QHarmonyJsContext &)> &task)
QT_DEFINE_NATIVE_INTERFACE(QHarmonyApplication)