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
qharmonyjscontext.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
5#include <QtCore/private/qcore_ohos_p.h>
6#include <QtCore/private/qohoslogger_p.h>
7#include <QtCore/qharmonyappjscontext.h>
8
9QT_BEGIN_NAMESPACE
10
11namespace {
12
13QOhosJsState &jsState()
14{
15 return QOhosJsThreadOps::instance().jsState();
16}
17
18}
19
20/*!
21 \class QHarmonyJsContext
22 \inmodule QtCore
23 \brief The QHarmonyJsContext class gives code on the JS thread of a
24 HarmonyOS application access to the JS runtime.
25
26 On HarmonyOS, Qt runs the application on its own thread. The ArkTS
27 runtime and the platform UI run on the main thread of the process,
28 called the JS thread. Platform APIs are only available on the JS thread.
29 To run code there, use
30 QNativeInterface::QHarmonyApplication::runOnJsThread(). The code
31 receives a QHarmonyJsContext as the task argument. JS callbacks created
32 with \c napi_create_function() receive only a \c napi_env. They get the
33 context with fromEnv().
34
35 QHarmonyAppJsContext provides the state of the application in the
36 runtime, such as its default \c UIAbility instance. To get it, call
37 QHarmonyAppJsContext::fromJsContext().
38
39 All member functions must be called on the JS thread. A \c napi_value
40 that they return is valid until the task or callback that obtained it
41 returns. It must not be passed to another thread. To keep a JS object
42 longer, use a QHarmonyJsObjectRef.
43
44 A QHarmonyJsContext reference is valid as long as the \c napi_env of
45 the context, which is until the task or callback that received or
46 obtained it returns. Get the context again in each task or callback.
47
48 \sa {Qt for HarmonyOS Platform API}
49*/
50
51/*!
52 \internal
53*/
54QHarmonyJsContext::QHarmonyJsContext() = default;
55
56/*!
57 \internal
58*/
59QHarmonyJsContext::~QHarmonyJsContext() = default;
60
61/*!
62 Returns the context of \a env. \a env must be the environment of the JS
63 thread. The returned context is valid as long as \a env.
64
65 Use this function in a JS callback created with
66 \c napi_create_function(), which receives only the \c napi_env.
67*/
68QHarmonyJsContext &QHarmonyJsContext::fromEnv(napi_env env)
69{
70 return QHarmonyAppJsContext::fromEnv(env);
71}
72
73/*!
74 Returns the Node-API environment of the JS thread.
75*/
76napi_env QHarmonyJsContext::env()
77{
78 return jsState().env();
79}
80
81/*!
82 Returns the JS module \a moduleName, for example
83 \c {"@ohos.app.ability.AbilityConstant"}. If the module cannot be
84 loaded, the function logs the error and returns \nullptr. No exception
85 is left pending.
86
87 The module is loaded on first use and then kept. Further calls with the
88 same name return the same object, as with a JS \c import. Besides the
89 platform's \c {@ohos.*} modules, the kit modules that the ArkTS
90 application template bundled with Qt makes available to native code can
91 be imported, such as \c {@kit.ShareKit.systemShare}.
92*/
93napi_value QHarmonyJsContext::tryImportModule(const std::string &moduleName)
94{
95 try {
96 return jsState().tryGetModule(moduleName).value_or(QNapi::Object());
97 } catch (const Napi::Error &error) {
98 qOhosPrintfWarning("%s: importing '%s' failed: %s", Q_FUNC_INFO, moduleName.c_str(), error.what());
99 return nullptr;
100 }
101}
102
103QT_END_NAMESPACE