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