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.
58 \li The JS thread also runs the platform UI, so tasks must be short.
59 \endlist
60
61 Each task runs inside its own Node-API handle scope. A C++ exception
62 that a task throws and a JS exception that it leaves pending are caught
63 and logged on the JS thread. A task that needs to react to a failure
64 must handle it itself.
65
66 \inmodule QtCore
67 \inheaderfile QCoreApplication
68 \ingroup native-interfaces
69 \ingroup native-interfaces-qcoreapplication
70
71 \sa {Qt for HarmonyOS Platform API}
72*/
74
75/*!
76 Queues \a task for execution on the JS thread and returns immediately.
77 You can call this function from any thread. This is the primary way to
78 reach the platform APIs. The task, with everything it captures, is
79 destroyed on the JS thread.
80
81 Tasks queued with runOnJsThread() and runOnJsThreadAndWait() run one at a
82 time, in the order in which they were queued.
83*/
84void QNativeInterface::QHarmonyApplication::runOnJsThread(std::function<void(QHarmonyJsContext &)> task)
85{
86 const char *funcInfo = Q_FUNC_INFO;
87
88 QOhosJsThreadGateway::invoke(
89 [task = std::move(task), funcInfo](QOhosJsState &jsState) {
90 runTaskLoggingExceptions(jsState, funcInfo, task);
91 });
92}
93
94/*!
95 Runs \a task on the JS thread and blocks the calling thread until
96 \a task returns. You can call this function from any thread. \a task
97 runs after the tasks queued before it with runOnJsThread() or
98 runOnJsThreadAndWait(). When called on the JS thread, it runs \a task
99 directly, before any task that is still queued. \a task must not wait for
100 the calling thread.
101*/
102void QNativeInterface::QHarmonyApplication::runOnJsThreadAndWait(const std::function<void(QHarmonyJsContext &)> &task)
103{
104 const char *funcInfo = Q_FUNC_INFO;
105
106 QOhosJsThreadGateway::runAndWait(
107 [&](QOhosJsState &jsState) {
108 runTaskLoggingExceptions(jsState, funcInfo, task);
109 },
110 funcInfo);
111}
112
113/*!
114 \fn template <typename Func, typename Result> Result QNativeInterface::QHarmonyApplication::runOnJsThreadAndWait(Func &&task)
115
116 Runs \a task on the JS thread, blocks the calling thread until \a task
117 returns, and returns its result. \a task is ordered like in the overload
118 without a result. If \a task throws an exception or leaves a JS
119 exception pending, the exception is logged on the JS thread and a
120 default-constructed value is returned. The result must not be a
121 \c napi_value or a \c napi_ref. To bring JS data back, convert it with
122 the QtHarmony functions or keep it in a QHarmonyJsObjectRef.
123*/
124
125/*!
126 Queues \a task for execution on the Qt thread, the thread of the
127 QCoreApplication object, and returns immediately. You can call this
128 function from any thread at any time. Use it to bring results back from
129 the JS thread. Tasks queued with runOnQtThread() run one at a time, in
130 the order in which they were queued. A task queued after the Qt event
131 loop has finished for good never runs.
132*/
133void QNativeInterface::QHarmonyApplication::runOnQtThread(std::function<void()> task)
134{
135 QtOhos::invokeInQtThread(std::move(task));
136}
137
138QT_END_NAMESPACE
void runTaskLoggingExceptions(QOhosJsState &jsState, const char *callerContextName, const std::function< void(QHarmonyJsContext &)> &task)
QT_DEFINE_NATIVE_INTERFACE(QHarmonyApplication)