wasm: implement move for QWasmEventHandler

Implement move by copying the emscripten::val member
variables, and clearing the moved-from ones. This ensures
that the moved-from val is in a known state ("undefined").

Also move the non-default constructor declaration up to
the top of the class.

Change-Id: If9574cc91a53d4029d49b29b89819d98319acaa3
Reviewed-by: Even Oscar Andersen <even.oscar.andersen@qt.io>
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
This commit is contained in:
Morten Sørvig 2025-03-07 10:27:04 +01:00 committed by Lorn Potter
parent dc033758a3
commit 57f98a095e
2 changed files with 29 additions and 3 deletions

View File

@ -180,12 +180,37 @@ QWasmEventHandler::QWasmEventHandler(emscripten::val element, const std::string
QWasmEventHandler::~QWasmEventHandler()
{
// Do nothing if this instance is default-constructed, or was moved from.
if (m_element.isUndefined())
return;
QWasmSuspendResumeControl *suspendResume = QWasmSuspendResumeControl::get();
Q_ASSERT(suspendResume);
m_element.call<void>("removeEventListener", m_name, suspendResume->jsEventHandlerAt(m_eventHandlerIndex));
suspendResume->removeEventHandler(m_eventHandlerIndex);
}
QWasmEventHandler::QWasmEventHandler(QWasmEventHandler&& other) noexcept
:m_element(std::move(other.m_element))
,m_name(std::move(other.m_name))
,m_eventHandlerIndex(other.m_eventHandlerIndex)
{
other.m_element = emscripten::val();
other.m_name = emscripten::val();
other.m_eventHandlerIndex = 0;
}
QWasmEventHandler& QWasmEventHandler::operator=(QWasmEventHandler&& other) noexcept
{
m_element = std::move(other.m_element);
other.m_element = emscripten::val();
m_name = std::move(other.m_name);
other.m_name = emscripten::val();
m_eventHandlerIndex = other.m_eventHandlerIndex;
other.m_eventHandlerIndex = 0;
return *this;
}
//
// The QWasmTimer class creates a native single-shot timer. The event handler is provided in the
// constructor and can be reused: each call setTimeout() sets a new timeout, though with the

View File

@ -51,12 +51,13 @@ class Q_CORE_EXPORT QWasmEventHandler
{
public:
QWasmEventHandler() = default;
QWasmEventHandler(emscripten::val element, const std::string &name,
std::function<void(emscripten::val)> fn);
~QWasmEventHandler();
QWasmEventHandler(QWasmEventHandler const&) = delete;
QWasmEventHandler& operator=(QWasmEventHandler const&) = delete;
QWasmEventHandler(emscripten::val element, const std::string &name,
std::function<void(emscripten::val)> fn);
QWasmEventHandler(QWasmEventHandler&& other) noexcept;
QWasmEventHandler& operator=(QWasmEventHandler&& other) noexcept;
private:
emscripten::val m_element;
emscripten::val m_name;