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
qcore_wasm.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 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/qrect.h>
5
6#include <emscripten/val.h>
7
8#if !defined(Q_OS_WASM)
9#error This is a wasm-only file.
10#endif // !defined(Q_OS_WASM)
11
13
14/*!
15 Converts the DOMRect (https://www.w3.org/TR/geometry-1/) \a domRect to QRectF. The behavior is
16 undefined if the provided parameter is not a DOMRect.
17
18 \since 6.5
19 \ingroup platform-type-conversions
20
21 \sa toDOMRect()
22*/
23QRectF QRectF::fromDOMRect(emscripten::val domRect)
24{
25 Q_ASSERT_X(domRect["constructor"]["name"].as<std::string>() == "DOMRect", Q_FUNC_INFO,
26 "Passed object is not a DOMRect");
27
28 return QRectF(domRect["left"].as<qreal>(), domRect["top"].as<qreal>(),
29 domRect["width"].as<qreal>(), domRect["height"].as<qreal>());
30}
31
32/*!
33 Converts this object to a DOMRect (https://www.w3.org/TR/geometry-1/).
34
35 \since 6.5
36 \ingroup platform-type-conversions
37
38 \sa fromDOMRect()
39*/
40emscripten::val QRectF::toDOMRect() const
41{
42 return emscripten::val::global("DOMRect").new_(left(), top(), width(), height());
43}
44
45/*!
46 Converts the \l {https://262.ecma-international.org/#sec-string-object}{ECMAScript string} \a
47 jsString to QString. Behavior is undefined if the provided parameter is not a string.
48
49 \since 6.6
50 \ingroup platform-type-conversions
51
52 \sa toEcmaString()
53*/
54QString QString::fromEcmaString(emscripten::val jsString)
55{
56 Q_ASSERT_X(jsString.isString(), Q_FUNC_INFO, "Passed object is not a string");
57
58#ifdef __wasm64__
59 return QString::fromStdU16String(jsString.as<std::u16string>());
60#endif
61
62 const double length = jsString["length"].as<double>();
63
64 Q_ASSERT_X((double(uint64_t(length)) != double(uint64_t(length) - 1)
65 && double(uint64_t(length)) != double(uint64_t(length) + 1))
66 || !std::numeric_limits<double>::is_iec559,
67 Q_FUNC_INFO, "The floating-point length cannot precisely represent an integer");
68
69 constexpr int zeroTerminatorLength = 1;
70 const auto lengthOfUtf16 = (length + zeroTerminatorLength) * 2;
71
72 Q_ASSERT_X((double(uint64_t(lengthOfUtf16)) != double(uint64_t(lengthOfUtf16) - 1)
73 && double(uint64_t(lengthOfUtf16)) != double(uint64_t(lengthOfUtf16) + 1))
74 || !std::numeric_limits<double>::is_iec559,
75 Q_FUNC_INFO,
76 "The floating-point lengthOfUtf16 cannot precisely represent an integer");
77
78 const QString result(uint64_t(length), Qt::Uninitialized);
79
80 static const emscripten::val stringToUTF16(emscripten::val::module_property("stringToUTF16"));
81 stringToUTF16(jsString, emscripten::val(quintptr(result.data())),
82 emscripten::val(lengthOfUtf16));
83 return result;
84}
85
86/*!
87 Converts this object to an
88 \l {https://262.ecma-international.org/#sec-string-object}{ECMAScript string}.
89
90 \since 6.6
91 \ingroup platform-type-conversions
92
93 \sa fromEcmaString()
94*/
95emscripten::val QString::toEcmaString() const
96{
97#ifdef __wasm64__
98 return emscripten::val::u16string(toStdU16String().c_str());
99#endif
100 static const emscripten::val UTF16ToString(emscripten::val::module_property("UTF16ToString"));
101 return UTF16ToString(emscripten::val(quintptr(utf16())));
102}
103
104QT_END_NAMESPACE
\inmodule QtCore\reentrant
Definition qrect.h:509