Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qstdweb_p.h
Go to the documentation of this file.
1
// Copyright (C) 2021 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
// Qt-Security score:significant reason:default
4
5
#
ifndef
QSTDWEB_P_H
6
#
define
QSTDWEB_P_H
7
8
//
9
// W A R N I N G
10
// -------------
11
//
12
// This file is not part of the Qt API. It exists purely as an
13
// implementation detail. This header file may change from version to
14
// version without notice, or even be removed.
15
//
16
// We mean it.
17
//
18
19
#
include
<
private
/
qglobal_p
.
h
>
20
#
include
<
QtCore
/
qglobal
.
h
>
21
#
include
"QtCore/qhash.h"
22
#
include
"QtCore/qiodevice.h"
23
#
include
"QtCore/private/qwasmsuspendresumecontrol_p.h"
24
25
#
include
<
emscripten
/
val
.
h
>
26
27
#
include
<
cstdint
>
28
#
include
<
functional
>
29
#
include
<
initializer_list
>
30
#
include
<
memory
>
31
#
include
<
string
>
32
#
include
<
utility
>
33
34
#
if
QT_CONFIG
(
thread
)
35
#
include
<
emscripten
/
proxying
.
h
>
36
#
include
<
emscripten
/
threading
.
h
>
37
#
endif
// #if QT_CONFIG(thread)
38
39
#
if
QT_CONFIG
(
wasm_jspi
)
40
#
define
QT_WASM_EMSCRIPTEN_ASYNC
,
emscripten
::
async
(
)
41
#
else
42
#
define
QT_WASM_EMSCRIPTEN_ASYNC
43
#
endif
44
45
QT_BEGIN_NAMESPACE
46
47
class
QMimeData;
48
49
namespace
qstdweb
{
50
extern
const
char
makeContextfulPromiseFunctionName
[];
51
52
// DOM API in C++, implemented using emscripten val.h and bind.h.
53
// This is private API and can be extended and changed as needed.
54
// The API mirrors that of the native API, with some extensions
55
// to ease usage from C++ code.
56
57
class
ArrayBuffer;
58
class
Blob;
59
class
File;
60
class
FileList;
61
class
FileReader;
62
class
Uint8Array;
63
class
EventCallback;
64
65
class
Q_CORE_EXPORT
ArrayBuffer
{
66
public
:
67
explicit
ArrayBuffer
(
uint32_t
size
);
68
explicit
ArrayBuffer
(
const
emscripten
::
val
&
arrayBuffer
);
69
uint32_t
byteLength
()
const
;
70
ArrayBuffer
slice
(
uint32_t
begin
,
uint32_t
end
)
const
;
71
emscripten
::
val
val
()
const
;
72
73
private
:
74
friend
class
Uint8Array
;
75
emscripten
::
val
m_arrayBuffer
=
emscripten
::
val
::
undefined
();
76
};
77
78
class
Q_CORE_EXPORT
Blob
{
79
public
:
80
explicit
Blob
(
const
emscripten
::
val
&
blob
);
81
static
Blob
fromArrayBuffer
(
const
ArrayBuffer
&
arrayBuffer
);
82
uint32_t
size
()
const
;
83
static
Blob
copyFrom
(
const
char
*
buffer
,
uint32_t
size
,
std
::
string
mimeType
);
84
static
Blob
copyFrom
(
const
char
*
buffer
,
uint32_t
size
);
85
Blob
slice
(
uint32_t
begin
,
uint32_t
end
)
const
;
86
ArrayBuffer
arrayBuffer_sync
()
const
;
87
emscripten
::
val
val
()
const
;
88
std
::
string
type
()
const
;
89
90
private
:
91
friend
class
FileReader
;
92
emscripten
::
val
m_blob
=
emscripten
::
val
::
undefined
();
93
};
94
95
class
Q_CORE_EXPORT
File
{
96
public
:
97
File
() =
default
;
98
explicit
File
(
const
emscripten
::
val
&
file
);
99
~
File
();
100
101
File
(
const
File
&
other
);
102
File
(
File
&&
other
);
103
File
&
operator
=(
const
File
&
other
);
104
File
&
operator
=(
File
&&
other
);
105
106
Blob
slice
(
uint64_t
begin
,
uint64_t
end
)
const
;
107
std
::
string
name
()
const
;
108
uint64_t
size
()
const
;
109
std
::
string
type
()
const
;
110
void
stream
(
uint32_t
offset
,
uint32_t
length
,
char
*
buffer
,
111
std
::
function
<
void
()>
completed
)
const
;
112
void
stream
(
char
*
buffer
,
std
::
function
<
void
()>
completed
)
const
;
113
emscripten
::
val
val
()
const
;
114
void
fileUrlRegistration
()
const
;
115
const
QString
&
fileUrlPath
()
const
{
return
m_urlPath
; }
116
emscripten
::
val
file
()
const
{
return
m_file
; }
117
118
private
:
119
emscripten
::
val
m_file
=
emscripten
::
val
::
undefined
();
120
QString
m_urlPath
;
121
};
122
123
class
Q_CORE_EXPORT
FileUrlRegistration
124
{
125
public
:
126
explicit
FileUrlRegistration
(
File
file
);
127
~
FileUrlRegistration
();
128
129
FileUrlRegistration
(
const
FileUrlRegistration
&
other
) =
delete
;
130
FileUrlRegistration
(
FileUrlRegistration
&&
other
);
131
FileUrlRegistration
&
operator
=(
const
FileUrlRegistration
&
other
) =
delete
;
132
FileUrlRegistration
&
operator
=(
FileUrlRegistration
&&
other
);
133
134
const
QString
&
path
()
const
{
return
m_path
; }
135
136
private
:
137
QString
m_path
;
138
};
139
140
using
FileUrlRegistrations
=
std
::
vector
<
std
::
unique_ptr
<
FileUrlRegistration
>>;
141
142
class
Q_CORE_EXPORT
FileList
{
143
public
:
144
FileList
() =
default
;
145
explicit
FileList
(
const
emscripten
::
val
&
fileList
);
146
147
int
length
()
const
;
148
File
item
(
int
index
)
const
;
149
File
operator
[](
int
index
)
const
;
150
emscripten
::
val
val
()
const
;
151
152
private
:
153
emscripten
::
val
m_fileList
=
emscripten
::
val
::
undefined
();
154
};
155
156
class
Q_CORE_EXPORT
FileReader
{
157
public
:
158
ArrayBuffer
result
()
const
;
159
void
readAsArrayBuffer
(
const
Blob
&
blob
)
const
;
160
161
void
onLoad
(
const
std
::
function
<
void
(
emscripten
::
val
)> &
onLoad
);
162
void
onError
(
const
std
::
function
<
void
(
emscripten
::
val
)> &
onError
);
163
void
onAbort
(
const
std
::
function
<
void
(
emscripten
::
val
)> &
onAbort
);
164
emscripten
::
val
val
()
const
;
165
166
private
:
167
emscripten
::
val
m_fileReader
=
emscripten
::
val
::
global
(
"FileReader"
).
new_
();
168
std
::
unique_ptr
<
EventCallback
>
m_onLoad
;
169
std
::
unique_ptr
<
EventCallback
>
m_onError
;
170
std
::
unique_ptr
<
EventCallback
>
m_onAbort
;
171
};
172
173
class
Q_CORE_EXPORT
Uint8Array
{
174
public
:
175
explicit
Uint8Array
(
const
emscripten
::
val
&
uint8Array
);
176
explicit
Uint8Array
(
const
ArrayBuffer
&
buffer
);
177
explicit
Uint8Array
(
uint32_t
size
);
178
Uint8Array
(
const
ArrayBuffer
&
buffer
,
uint32_t
offset
,
uint32_t
length
);
179
Uint8Array
(
const
char
*
buffer
,
uint32_t
size
);
180
181
ArrayBuffer
buffer
()
const
;
182
uint32_t
length
()
const
;
183
void
set
(
const
Uint8Array
&
source
);
184
Uint8Array
subarray
(
uint32_t
begin
,
uint32_t
end
);
185
186
void
copyTo
(
char
*
destination
)
const
;
187
QByteArray
copyToQByteArray
()
const
;
188
189
static
void
copy
(
char
*
destination
,
const
Uint8Array
&
source
);
190
static
Uint8Array
copyFrom
(
const
char
*
buffer
,
uint32_t
size
);
191
static
Uint8Array
copyFrom
(
const
QByteArray
&
buffer
);
192
emscripten
::
val
val
()
const
;
193
194
private
:
195
static
emscripten
::
val
constructor_
();
196
emscripten
::
val
m_uint8Array
=
emscripten
::
val
::
undefined
();
197
};
198
199
class
Q_CORE_EXPORT
FileSystemWritableFileStream
{
200
public
:
201
FileSystemWritableFileStream
() =
default
;
202
explicit
FileSystemWritableFileStream
(
const
emscripten
::
val
&
writableStream
);
203
emscripten
::
val
val
()
const
;
204
205
private
:
206
emscripten
::
val
m_writableStream
=
emscripten
::
val
::
undefined
();
207
};
208
209
class
Q_CORE_EXPORT
FileSystemFileHandle
{
210
public
:
211
FileSystemFileHandle
() =
default
;
212
explicit
FileSystemFileHandle
(
const
emscripten
::
val
&
fileHandle
);
213
214
std
::
string
name
()
const
;
215
std
::
string
kind
()
const
;
216
217
emscripten
::
val
val
()
const
;
218
219
private
:
220
emscripten
::
val
m_fileHandle
=
emscripten
::
val
::
undefined
();
221
};
222
223
// EventCallback here for source compatibility; prefer using QWasmEventHandler directly
224
class
Q_CORE_EXPORT
EventCallback
:
public
QWasmEventHandler
225
{
226
public
:
227
EventCallback
() =
default
;
228
EventCallback
(
EventCallback
const
&) =
delete
;
229
EventCallback
&
operator
=(
EventCallback
const
&) =
delete
;
230
EventCallback
(
emscripten
::
val
element
,
const
std
::
string
&
name
,
231
const
std
::
function
<
void
(
emscripten
::
val
)> &
fn
);
232
};
233
234
struct
PromiseCallbacks
235
{
236
std
::
function
<
void
(
emscripten
::
val
)>
thenFunc
= [](
emscripten
::
val
) {};
237
std
::
function
<
void
(
emscripten
::
val
)>
catchFunc
= [](
emscripten
::
val
) {};
238
std
::function<
void
()>
finallyFunc
= []() {};
239
};
240
241
// Note: it is ok for the Promise object to go out of scope,
242
// the resources will be cleaned up in the finally handler.
243
class
Q_CORE_EXPORT
Promise
{
244
public
:
245
template
<
typename
...
Args
>
246
Promise
(
emscripten
::
val
target
,
QString
methodName
,
Args
...
args
) {
247
m_state
=
std
::
make_shared
<
State
>();
248
m_state
->
m_promise
=
target
.
call
<
emscripten
::
val
>(
249
methodName
.
toStdString
().
c_str
(),
std
::
forward
<
Args
>(
args
)...);
250
if
(
m_state
->
m_promise
.
isUndefined
() ||
m_state
->
m_promise
[
"constructor"
][
"name"
].
as
<
std
::
string
>() !=
"Promise"
) {
251
qFatal
(
"This function did not return a promise"
);
252
}
253
addFinallyFunction
([](){});
254
}
255
256
template
<
typename
...
Args
>
257
Promise
(
emscripten
::
val
target
,
const
char
*
methodName
,
Args
...
args
) {
258
m_state
=
std
::
make_shared
<
State
>();
259
m_state
->
m_promise
=
target
.
call
<
emscripten
::
val
>(
260
methodName
,
std
::
forward
<
Args
>(
args
)...);
261
if
(
m_state
->
m_promise
.
isUndefined
() ||
m_state
->
m_promise
[
"constructor"
][
"name"
].
as
<
std
::
string
>() !=
"Promise"
) {
262
qFatal
(
"This function did not return a promise"
);
263
}
264
addFinallyFunction
([](){});
265
}
266
267
Promise
(
emscripten
::
val
promise
) {
268
m_state
=
std
::
make_shared
<
State
>();
269
m_state
->
m_promise
=
promise
;
270
if
(
m_state
->
m_promise
.
isUndefined
() ||
m_state
->
m_promise
[
"constructor"
][
"name"
].
as
<
std
::
string
>() !=
"Promise"
) {
271
qFatal
(
"This function did not return a promise"
);
272
}
273
addFinallyFunction
([](){});
274
}
275
276
Promise
(
const
std
::
vector
<
Promise
> &
promises
) {
277
std
::
vector
<
emscripten
::
val
>
all
;
278
all
.
reserve
(
promises
.
size
());
279
for
(
const
auto
&
p
:
promises
)
280
all
.
push_back
(
p
.
getPromise
());
281
282
auto
arr
=
emscripten
::
val
::
array
(
all
);
283
m_state
=
std
::
make_shared
<
State
>();
284
m_state
->
m_promise
=
emscripten
::
val
::
global
(
"Promise"
).
call
<
emscripten
::
val
>(
"all"
,
arr
);
285
addFinallyFunction
([](){});
286
}
287
288
Promise
&
addThenFunction
(
std
::
function
<
void
(
emscripten
::
val
)>
thenFunc
);
289
Promise
&
addCatchFunction
(
std
::
function
<
void
(
emscripten
::
val
)>
catchFunc
);
290
Promise
&
addFinallyFunction
(
std
::
function
<
void
()>
finallyFunc
);
291
292
void
suspendExclusive
();
293
294
struct
AwaitResult
{
295
emscripten
::
val
value
=
emscripten
::
val
::
undefined
();
296
emscripten
::
val
error
=
emscripten
::
val
::
undefined
();
297
};
298
AwaitResult
awaitExclusive
();
299
300
emscripten
::
val
getPromise
()
const
;
301
302
public
:
303
class
State
{
304
private
:
305
friend
class
Promise
;
306
307
State
(
const
State
&) =
delete
;
308
State
(
State
&&) =
delete
;
309
State
&
operator
=(
const
State
&) =
delete
;
310
State
&
operator
=(
State
&&) =
delete
;
311
312
public
:
313
State
() { ++
s_numInstances
; }
314
~
State
() { --
s_numInstances
; }
315
static
size_t
numInstances
() {
return
s_numInstances
; }
316
317
private
:
318
emscripten
::
val
m_promise
=
emscripten
::
val
::
undefined
();
319
QList
<
uint32_t
>
m_handlers
;
320
static
size_t
s_numInstances
;
321
};
322
323
private
:
324
std
::
shared_ptr
<
State
>
m_state
;
325
326
public
:
327
// Deprecated: To be backwards compatible
328
static
uint32_t
Q_CORE_EXPORT
adoptPromise
(
emscripten
::
val
promise
,
PromiseCallbacks
callbacks
,
QList
<
uint32_t
> *
handlers
=
nullptr
);
329
330
template
<
typename
...
Args
>
331
static
uint32_t
make
(
emscripten
::
val
target
,
332
QString
methodName
,
333
PromiseCallbacks
callbacks
,
334
Args
...
args
)
335
{
336
emscripten
::
val
promiseObject
=
target
.
call
<
emscripten
::
val
>(
337
methodName
.
toStdString
().
c_str
(),
std
::
forward
<
Args
>(
args
)...);
338
if
(
promiseObject
.
isUndefined
() ||
promiseObject
[
"constructor"
][
"name"
].
as
<
std
::
string
>() !=
"Promise"
) {
339
qFatal
(
"This function did not return a promise"
);
340
}
341
342
return
adoptPromise
(
std
::
move
(
promiseObject
),
std
::
move
(
callbacks
));
343
}
344
345
template
<
typename
...
Args
>
346
static
void
make
(
347
QList
<
uint32_t
> &
handlers
,
348
emscripten
::
val
target
,
349
QString
methodName
,
350
PromiseCallbacks
callbacks
,
351
Args
...
args
)
352
{
353
emscripten
::
val
promiseObject
=
target
.
call
<
emscripten
::
val
>(
354
methodName
.
toStdString
().
c_str
(),
std
::
forward
<
Args
>(
args
)...);
355
if
(
promiseObject
.
isUndefined
() ||
promiseObject
[
"constructor"
][
"name"
].
as
<
std
::
string
>() !=
"Promise"
) {
356
qFatal
(
"This function did not return a promise"
);
357
}
358
359
adoptPromise
(
std
::
move
(
promiseObject
),
std
::
move
(
callbacks
), &
handlers
);
360
}
361
362
static
void
Q_CORE_EXPORT
suspendExclusive
(
QList
<
uint32_t
>
handlerIndices
);
363
static
void
Q_CORE_EXPORT
all
(
std
::
vector
<
emscripten
::
val
>
promises
,
PromiseCallbacks
callbacks
);
364
};
365
366
template
<
class
F>
367
decltype
(
auto
)
bindForever
(F wrappedCallback)
368
{
369
return
wrappedCallback;
370
}
371
372
class
Q_CORE_EXPORT
BlobIODevice
:
public
QIODevice
373
{
374
public
:
375
BlobIODevice
(
Blob
blob
);
376
bool
open
(
QIODeviceBase
::
OpenMode
mode
)
override
;
377
bool
isSequential
()
const
override
;
378
qint64
size
()
const
override
;
379
bool
seek
(
qint64
pos
)
override
;
380
381
protected
:
382
qint64
readData
(
char
*
data
,
qint64
maxSize
)
override
;
383
qint64
writeData
(
const
char
*,
qint64
)
override
;
384
385
private
:
386
Blob
m_blob
;
387
};
388
389
class
Uint8ArrayIODevice
:
public
QIODevice
390
{
391
public
:
392
Uint8ArrayIODevice
(Uint8Array array);
393
bool
open
(QIODevice::OpenMode mode)
override
;
394
bool
isSequential
()
const
override
;
395
qint64
size
()
const
override
;
396
bool
seek
(qint64 pos)
override
;
397
398
protected
:
399
qint64
readData
(
char
*data, qint64 maxSize)
override
;
400
qint64
writeData
(
const
char
*data, qint64 size)
override
;
401
402
private
:
403
Uint8Array m_array;
404
};
405
406
class
Q_CORE_EXPORT
FileSystemWritableFileStreamIODevice
:
public
QIODevice
407
{
408
public
:
409
FileSystemWritableFileStreamIODevice
(
FileSystemWritableFileStream
stream
);
410
bool
open
(
QIODevice
::
OpenMode
mode
)
override
;
411
void
close
()
override
;
412
bool
isSequential
()
const
override
;
413
qint64
size
()
const
override
;
414
bool
seek
(
qint64
pos
)
override
;
415
416
protected
:
417
qint64
readData
(
char
*
data
,
qint64
maxSize
)
override
;
418
qint64
writeData
(
const
char
*
data
,
qint64
size
)
override
;
419
420
private
:
421
FileSystemWritableFileStream
m_stream
;
422
qint64
m_size
= 0;
423
};
424
425
class
Q_CORE_EXPORT
FileSystemFileIODevice
:
public
QIODevice
426
{
427
public
:
428
FileSystemFileIODevice
(
FileSystemFileHandle
fileHandle
);
429
bool
open
(
QIODevice
::
OpenMode
mode
)
override
;
430
void
close
()
override
;
431
bool
isSequential
()
const
override
;
432
qint64
size
()
const
override
;
433
bool
seek
(
qint64
pos
)
override
;
434
435
protected
:
436
qint64
readData
(
char
*
data
,
qint64
maxSize
)
override
;
437
qint64
writeData
(
const
char
*
data
,
qint64
size
)
override
;
438
439
private
:
440
FileSystemFileHandle
m_fileHandle
;
441
std
::
unique_ptr
<
BlobIODevice
>
m_blobDevice
;
442
std
::
unique_ptr
<
FileSystemWritableFileStreamIODevice
>
m_writableDevice
;
443
qint64
m_size
= 0;
444
};
445
446
inline
emscripten
::
val
window
()
447
{
448
static
emscripten::val savedWindow = emscripten::val::global(
"window"
);
449
return
savedWindow;
450
}
451
452
bool
Q_CORE_EXPORT
haveAsyncify
();
453
bool
Q_CORE_EXPORT
haveJspi
();
454
bool
canBlockCallingThread
();
455
}
456
457
QT_END_NAMESPACE
458
459
#
endif
qstdweb::ArrayBuffer
Definition
qstdweb_p.h:65
qstdweb::BlobIODevice
Definition
qstdweb_p.h:373
qstdweb::Blob
Definition
qstdweb_p.h:78
qstdweb::EventCallback
Definition
qstdweb_p.h:225
qstdweb::FileList
Definition
qstdweb_p.h:142
qstdweb::FileReader
Definition
qstdweb_p.h:156
qstdweb::FileSystemFileHandle
Definition
qstdweb_p.h:209
qstdweb::FileSystemFileIODevice
Definition
qstdweb_p.h:426
qstdweb::FileSystemWritableFileStreamIODevice
Definition
qstdweb_p.h:407
qstdweb::FileSystemWritableFileStream
Definition
qstdweb_p.h:199
qstdweb::FileUrlRegistration
Definition
qstdweb_p.h:124
qstdweb::File
Definition
qstdweb_p.h:95
qstdweb::Promise
Definition
qstdweb_p.h:243
qstdweb::Uint8ArrayIODevice
Definition
qstdweb_p.h:390
qstdweb::Uint8ArrayIODevice::size
qint64 size() const override
For open random-access devices, this function returns the size of the device.
Definition
qstdweb.cpp:733
qstdweb::Uint8ArrayIODevice::seek
bool seek(qint64 pos) override
For random-access devices, this function sets the current position to pos, returning true on success,...
Definition
qstdweb.cpp:738
qstdweb::Uint8ArrayIODevice::Uint8ArrayIODevice
Uint8ArrayIODevice(Uint8Array array)
Definition
qstdweb.cpp:717
qstdweb::Uint8ArrayIODevice::isSequential
bool isSequential() const override
Returns true if this device is sequential; otherwise returns false.
Definition
qstdweb.cpp:728
qstdweb::Uint8ArrayIODevice::writeData
qint64 writeData(const char *data, qint64 size) override
Writes up to maxSize bytes from data to the device.
Definition
qstdweb.cpp:755
qstdweb::Uint8ArrayIODevice::open
bool open(QIODevice::OpenMode mode) override
Definition
qstdweb.cpp:723
qstdweb::Uint8ArrayIODevice::readData
qint64 readData(char *data, qint64 maxSize) override
Reads up to maxSize bytes from the device into data, and returns the number of bytes read or -1 if an...
Definition
qstdweb.cpp:745
qstdweb::Uint8Array
Definition
qstdweb_p.h:173
qstdweb
Definition
qstdweb.cpp:29
qstdweb::canBlockCallingThread
bool canBlockCallingThread()
Definition
qstdweb.cpp:665
qstdweb::window
emscripten::val window()
Definition
qstdweb_p.h:446
qstdweb::uint53_t
double uint53_t
Definition
qstdweb.cpp:46
qstdweb::haveAsyncify
bool haveAsyncify()
Definition
qstdweb.cpp:659
qstdweb::haveJspi
bool haveJspi()
Definition
qstdweb.cpp:653
qstdweb::makeContextfulPromiseFunctionName
const char makeContextfulPromiseFunctionName[]
qstdweb::usePotentialyUnusedSymbols
static void usePotentialyUnusedSymbols()
Definition
qstdweb.cpp:31
qstdweb::bindForever
decltype(auto) bindForever(F wrappedCallback)
Definition
qstdweb_p.h:367
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
qstdweb::PromiseCallbacks
Definition
qstdweb_p.h:235
qstdweb::PromiseCallbacks::thenFunc
std::function< void(emscripten::val)> thenFunc
Definition
qstdweb_p.h:236
qstdweb::PromiseCallbacks::finallyFunc
std::function< void()> finallyFunc
Definition
qstdweb_p.h:238
qstdweb::PromiseCallbacks::catchFunc
std::function< void(emscripten::val)> catchFunc
Definition
qstdweb_p.h:237
qtbase
src
corelib
platform
wasm
qstdweb_p.h
Generated on
for Qt by
1.18.0