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
qohosjsenv_p.h
Go to the documentation of this file.
1
// Copyright (C) 2025 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
#
ifndef
QOHOSJSENV_H
5
#
define
QOHOSJSENV_H
6
7
//
8
// W A R N I N G
9
// -------------
10
//
11
// This file is not part of the Qt API. It exists purely as an
12
// implementation detail. This header file may change from version to
13
// version without notice, or even be removed.
14
//
15
// We mean it.
16
//
17
18
#
include
<
QtCore
/
qjsonarray
.
h
>
19
#
include
<
QtCore
/
qjsonobject
.
h
>
20
#
include
<
QtCore
/
qjsonvalue
.
h
>
21
#
include
<
QtCore
/
qstring
.
h
>
22
#
include
<
QtCore
/
private
/
qnapi_p
.
h
>
23
#
include
<
QtCore
/
private
/
qohoslogger_p
.
h
>
24
25
#
include
<
algorithm
>
26
#
include
<
array
>
27
#
include
<
cstdint
>
28
#
include
<
iterator
>
29
#
include
<
limits
>
30
#
include
<
memory
>
31
#
include
<
type_traits
>
32
33
#
include
<
napi
.
h
>
34
#
include
<
napi
/
native_api
.
h
>
35
36
QT_BEGIN_NAMESPACE
37
38
namespace
QtHarmonyExtras
::
Private
{
39
40
struct
QOhosJsEnv
41
{
42
template
<
typename
T,
typename
Enable =
void
>
43
struct
QNapiValue
{
44
static
QNapi
::
Value
create
(
napi_env
/*env*/
, T
/*value*/
) {
45
static_assert
(
sizeof
(T) == 0,
"Unsupported type - provide proper QOhosJsEnv::QNapiValue"
46
" overload"
);
47
}
48
static
Napi
::
Maybe
<
T
>
get
(
const
QNapi::Value &
/*value*/
) {
49
static_assert
(
sizeof
(T) == 0,
"Unsupported type - provide proper QOhosJsEnv::QNapiValue"
50
" overload"
);
51
}
52
};
53
54
template
<
typename
T>
55
static
QNapi
::
Value
toNapiValue
(napi_env env, T &&value) {
56
return
QNapiValue<
typename
std::decay<T>::type>::create(env, std::forward<T>(value));
57
}
58
59
template
<
typename
ReturnType>
60
static
ReturnType
fromNapiValue
(
const
QNapi::Value &value)
61
{
62
return
QNapiValue<
typename
std::decay<ReturnType>::type>::get(value).UnwrapOr(ReturnType());
63
}
64
};
65
66
//
67
// overloads of QNapiValue to support various types
68
//
69
70
template
<>
71
struct
QOhosJsEnv
::
QNapiValue
<
QList
<
QJsonValue
>>
72
{
73
static
QNapi
::
Value
create
(
napi_env
env
,
const
QList
<
QJsonValue
> &
inputValue
);
74
static
Napi
::
Maybe
<
QList
<
QJsonValue
>>
get
(
const
QNapi
::
Value
&
inputValue
);
75
};
76
77
template
<>
78
struct
QOhosJsEnv
::
QNapiValue
<
QJsonValue
>
79
{
80
static
QNapi
::
Value
create
(
napi_env
env
,
const
QJsonValue
&
inputValue
);
81
static
Napi
::
Maybe
<
QJsonValue
>
get
(
const
QNapi
::
Value
&
inputValue
);
82
};
83
84
template
<>
85
struct
QOhosJsEnv
::
QNapiValue
<
QJsonArray
>
86
{
87
static
QNapi
::
Value
create
(
napi_env
env
,
const
QJsonArray
&
inputValue
);
88
static
Napi
::
Maybe
<
QJsonArray
>
get
(
const
QNapi
::
Value
&
inputValue
);
89
};
90
91
template
<>
92
struct
QOhosJsEnv
::
QNapiValue
<
QJsonObject
>
93
{
94
static
QNapi
::
Value
create
(
napi_env
env
,
const
QJsonObject
&
inputValue
);
95
static
Napi
::
Maybe
<
QJsonObject
>
get
(
const
QNapi
::
Value
&
inputValue
);
96
};
97
98
inline
QNapi
::
Value
QOhosJsEnv
::
QNapiValue
<
QList
<
QJsonValue
>>::
create
(
napi_env
env
,
const
QList
<
QJsonValue
> &
inputValue
)
99
{
100
return
QNapi
::
runEscapingHandleScope
<
QNapi
::
Array
>(
101
env
,
102
[&]() {
103
auto
outputArray
=
QNapi
::
Array
::
New
(
env
,
inputValue
.
length
());
104
for
(
int
i
= 0;
i
<
inputValue
.
length
(); ++
i
)
105
outputArray
.
Set
(
i
,
QNapiValue
<
QJsonValue
>::
create
(
env
,
inputValue
[
i
]));
106
return
outputArray
;
107
});
108
}
109
110
inline
Napi
::
Maybe
<
QList
<
QJsonValue
>>
QOhosJsEnv
::
QNapiValue
<
QList
<
QJsonValue
>>::
get
(
const
QNapi
::
Value
&
inputValue
)
111
{
112
if
(!
inputValue
.
IsArray
())
113
return
Napi
::
Nothing
<
QList
<
QJsonValue
>>();
114
115
auto
inputArray
=
QNapi
::
checkedCast
<
QNapi
::
Array
>(
inputValue
);
116
std
::
uint32_t
rawInputArrayLength
=
inputArray
.
Length
();
117
if
(
rawInputArrayLength
>
std
::
numeric_limits
<
int
>::
max
())
118
return
Napi
::
Nothing
<
QList
<
QJsonValue
>>();
119
int
inputArrayLength
=
static_cast
<
int
>(
rawInputArrayLength
);
120
121
QList
<
QJsonValue
>
outputList
;
122
outputList
.
reserve
(
inputArrayLength
);
123
124
for
(
int
i
= 0;
i
<
inputArrayLength
; ++
i
) {
125
Napi
::
HandleScope
inputElementScope
{
inputValue
.
Env
()};
126
auto
optOutputElem
=
QNapiValue
<
QJsonValue
>::
get
(
inputArray
.
Get
(
i
));
127
if
(
optOutputElem
.
IsNothing
())
128
break
;
129
outputList
.
append
(
optOutputElem
.
Unwrap
());
130
}
131
132
return
outputList
.
length
() ==
inputArrayLength
133
?
Napi
::
Just
(
outputList
)
134
:
Napi
::
Nothing
<
QList
<
QJsonValue
>>();
135
}
136
137
inline
QNapi
::
Value
QOhosJsEnv
::
QNapiValue
<
QJsonValue
>::
create
(
napi_env
env
,
const
QJsonValue
&
inputValue
)
138
{
139
switch
(
inputValue
.
type
()) {
140
case
QJsonValue
::
Type
::
Array
:
141
return
QNapiValue
<
QJsonArray
>::
create
(
env
,
inputValue
.
toArray
());
142
case
QJsonValue
::
Type
::
Bool
:
143
return
QNapi
::
Boolean
::
New
(
env
,
inputValue
.
toBool
());
144
case
QJsonValue
::
Type
::
Double
:
145
return
QNapi
::
Number
::
New
(
env
,
inputValue
.
toDouble
());
146
case
QJsonValue
::
Type
::
Null
:
147
case
QJsonValue
::
Type
::
Undefined
:
148
return
Napi
::
Env
(
env
).
Null
();
149
case
QJsonValue
::
Type
::
Object
:
150
return
QNapiValue
<
QJsonObject
>::
create
(
env
,
inputValue
.
toObject
());
151
case
QJsonValue
::
Type
::
String
:
152
return
QNapi
::
String
::
New
(
env
,
inputValue
.
toString
().
toStdString
());
153
}
154
155
throw
Napi
::
Error
::
New
(
env
,
"Got unsupported (impossible) QJsonValue"
);
156
}
157
158
inline
Napi
::
Maybe
<
QJsonValue
>
QOhosJsEnv
::
QNapiValue
<
QJsonValue
>::
get
(
const
QNapi
::
Value
&
inputValue
)
159
{
160
if
(
inputValue
.
IsArray
()) {
161
const
auto
arrayValue
=
QNapiValue
<
QJsonArray
>::
get
(
inputValue
);
162
return
arrayValue
.
IsJust
() ?
Napi
::
Just
<
QJsonValue
>(
arrayValue
.
Unwrap
()) :
Napi
::
Nothing
<
QJsonValue
>();
163
}
else
if
(
inputValue
.
IsBoolean
()) {
164
const
auto
boolValue
=
QNapi
::
checkedCast
<
QNapi
::
Boolean
>(
inputValue
).
Value
();
165
return
Napi
::
Just
(
QJsonValue
(
boolValue
));
166
}
else
if
(
inputValue
.
IsNumber
()) {
167
const
auto
numberValue
=
QNapi
::
checkedCast
<
QNapi
::
Number
>(
inputValue
).
DoubleValue
();
168
return
Napi
::
Just
(
QJsonValue
(
numberValue
));
169
}
else
if
(
inputValue
.
IsObject
()) {
170
const
auto
objectValue
=
QNapiValue
<
QJsonObject
>::
get
(
inputValue
);
171
return
objectValue
.
IsJust
() ?
Napi
::
Just
<
QJsonValue
>(
objectValue
.
Unwrap
()) :
Napi
::
Nothing
<
QJsonValue
>();
172
}
else
if
(
inputValue
.
IsString
()) {
173
const
auto
stringValue
=
QString
::
fromStdString
(
QNapi
::
checkedCast
<
QNapi
::
String
>(
inputValue
));
174
return
Napi
::
Just
<
QJsonValue
>(
stringValue
);
175
}
else
{
176
return
Napi
::
Nothing
<
QJsonValue
>();
177
}
178
}
179
180
inline
QNapi
::
Value
QOhosJsEnv
::
QNapiValue
<
QJsonArray
>::
create
(
napi_env
env
,
const
QJsonArray
&
inputValue
)
181
{
182
QList
<
QJsonValue
>
listInputValue
;
183
listInputValue
.
reserve
(
inputValue
.
size
());
184
std
::
copy
(
inputValue
.
begin
(),
inputValue
.
end
(),
std
::
back_inserter
(
listInputValue
));
185
return
QNapiValue
<
QList
<
QJsonValue
>>::
create
(
env
,
listInputValue
);
186
}
187
188
inline
Napi
::
Maybe
<
QJsonArray
>
QOhosJsEnv
::
QNapiValue
<
QJsonArray
>::
get
(
const
QNapi
::
Value
&
inputValue
)
189
{
190
const
auto
transform
= [](
const
QList
<
QJsonValue
> &
input
) ->
QJsonArray
{
191
QJsonArray
result
;
192
std
::
copy
(
input
.
begin
(),
input
.
end
(),
std
::
back_inserter
(
result
));
193
return
result
;
194
};
195
196
const
auto
listValue
=
QNapiValue
<
QList
<
QJsonValue
>>::
get
(
inputValue
);
197
return
listValue
.
IsJust
()
198
?
Napi
::
Just
(
transform
(
listValue
.
Unwrap
()))
199
:
Napi
::
Nothing
<
QJsonArray
>();
200
}
201
202
inline
QNapi
::
Value
QOhosJsEnv
::
QNapiValue
<
QJsonObject
>::
create
(
napi_env
env
,
const
QJsonObject
&
inputValue
)
203
{
204
return
QNapi
::
runEscapingHandleScope
<
QNapi
::
Object
>(
205
env
,
206
[&]() {
207
auto
outputObject
=
QNapi
::
Object
::
New
(
env
);
208
for
(
auto
inputValueIter
=
inputValue
.
begin
();
inputValueIter
!=
inputValue
.
end
(); ++
inputValueIter
) {
209
outputObject
.
Set
(
210
inputValueIter
.
key
().
toStdString
(),
211
QNapiValue
<
QJsonValue
>::
create
(
env
,
inputValueIter
.
value
()));
212
}
213
return
outputObject
;
214
});
215
}
216
217
inline
Napi
::
Maybe
<
QJsonObject
>
QOhosJsEnv
::
QNapiValue
<
QJsonObject
>::
get
(
const
QNapi
::
Value
&
inputValue
)
218
{
219
if
(!
inputValue
.
IsObject
())
220
return
Napi
::
Nothing
<
QJsonObject
>();
221
222
auto
inputObject
=
QNapi
::
checkedCast
<
QNapi
::
Object
>(
inputValue
);
223
224
QJsonObject
result
;
225
bool
allElementsSet
=
true
;
226
227
for
(
const
auto
&
inputElement
:
inputObject
) {
228
if
(
inputElement
.
first
.
IsString
()) {
229
const
auto
key
=
QString
::
fromStdString
(
QNapi
::
checkedCast
<
QNapi
::
String
>(
inputElement
.
first
));
230
231
auto
optPropValue
=
QNapiValue
<
QJsonValue
>::
get
(
inputElement
.
second
);
232
if
(
optPropValue
.
IsNothing
()) {
233
allElementsSet
=
false
;
234
break
;
235
}
236
237
result
[
key
] =
optPropValue
.
Unwrap
();
238
}
239
}
240
241
return
allElementsSet
242
?
Napi
::
Just
(
result
)
243
:
Napi
::
Nothing
<
QJsonObject
>();
244
}
245
246
}
247
248
QT_END_NAMESPACE
249
250
#
endif
// QOHOSJSENV_H
QtHarmonyExtras::Private
Definition
qohosappbundleinfo_p.h:42
QtHarmonyExtras
Definition
qohosabilitycontext.cpp:36
QtHarmonyExtras::Private::QOhosJsEnv::QNapiValue
Definition
qohosjsenv_p.h:43
QtHarmonyExtras::Private::QOhosJsEnv::QNapiValue::create
static QNapi::Value create(napi_env, T)
Definition
qohosjsenv_p.h:44
QtHarmonyExtras::Private::QOhosJsEnv::QNapiValue::get
static Napi::Maybe< T > get(const QNapi::Value &)
Definition
qohosjsenv_p.h:48
QtHarmonyExtras::Private::QOhosJsEnv
Definition
qohosjsenv_p.h:41
QtHarmonyExtras::Private::QOhosJsEnv::fromNapiValue
static ReturnType fromNapiValue(const QNapi::Value &value)
Definition
qohosjsenv_p.h:60
QtHarmonyExtras::Private::QOhosJsEnv::toNapiValue
static QNapi::Value toNapiValue(napi_env env, T &&value)
Definition
qohosjsenv_p.h:55
qtbase
src
plugins
platforms
ohos
extras
qohosjsenv_p.h
Generated on
for Qt by
1.16.1