summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qcore_wasm.cpp
blob: fb12ae50c3e9ecbdfce623e7530fdd67773d3223 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include <QtCore/qrect.h>

#include <emscripten/val.h>

#if !defined(Q_OS_WASM)
#error This is a wasm-only file.
#endif // !defined(Q_OS_WASM)

QT_BEGIN_NAMESPACE

/*!
    Converts the DOMRect (https://www.w3.org/TR/geometry-1/) \a domRect to QRectF. The behavior is
    undefined if the provided parameter is not a DOMRect.

    \since 6.5
    \ingroup platform-type-conversions

    \sa toDOMRect()
*/
QRectF QRectF::fromDOMRect(emscripten::val domRect)
{
    Q_ASSERT_X(domRect["constructor"]["name"].as<std::string>() == "DOMRect", Q_FUNC_INFO,
               "Passed object is not a DOMRect");

    return QRectF(domRect["left"].as<qreal>(), domRect["top"].as<qreal>(),
                  domRect["width"].as<qreal>(), domRect["height"].as<qreal>());
}

/*!
    Converts this object to a DOMRect (https://www.w3.org/TR/geometry-1/).

    \since 6.5
    \ingroup platform-type-conversions

    \sa fromDOMRect()
*/
emscripten::val QRectF::toDOMRect() const
{
    return emscripten::val::global("DOMRect").new_(left(), top(), width(), height());
}

/*!
    Converts the \l {https://262.ecma-international.org/#sec-string-object}{ECMAScript string} \a
    jsString to QString. Behavior is undefined if the provided parameter is not a string.

    \since 6.6
    \ingroup platform-type-conversions

    \sa toEcmaString()
*/
QString QString::fromEcmaString(emscripten::val jsString)
{
    Q_ASSERT_X(jsString.isString(), Q_FUNC_INFO, "Passed object is not a string");

    const double length = jsString["length"].as<double>();

    Q_ASSERT_X((double(uint64_t(length)) != double(uint64_t(length) - 1)
                && double(uint64_t(length)) != double(uint64_t(length) + 1))
                       || !std::numeric_limits<double>::is_iec559,
               Q_FUNC_INFO, "The floating-point length cannot precisely represent an integer");

    constexpr int zeroTerminatorLength = 1;
    const auto lengthOfUtf16 = (length + zeroTerminatorLength) * 2;

    Q_ASSERT_X((double(uint64_t(lengthOfUtf16)) != double(uint64_t(lengthOfUtf16) - 1)
                && double(uint64_t(lengthOfUtf16)) != double(uint64_t(lengthOfUtf16) + 1))
                       || !std::numeric_limits<double>::is_iec559,
               Q_FUNC_INFO,
               "The floating-point lengthOfUtf16 cannot precisely represent an integer");

    const QString result(uint64_t(length), Qt::Uninitialized);

    static const emscripten::val stringToUTF16(emscripten::val::module_property("stringToUTF16"));
    stringToUTF16(jsString, emscripten::val(quintptr(result.data())),
                  emscripten::val(lengthOfUtf16));
    return result;
}

/*!
    Converts this object to an
    \l {https://262.ecma-international.org/#sec-string-object}{ECMAScript string}.

    \since 6.6
    \ingroup platform-type-conversions

    \sa fromEcmaString()
*/
emscripten::val QString::toEcmaString() const
{
    static const emscripten::val UTF16ToString(emscripten::val::module_property("UTF16ToString"));
    return UTF16ToString(emscripten::val(quintptr(utf16())));
}

QT_END_NAMESPACE