diff --git a/src/corelib/platform/wasm/qstdweb.cpp b/src/corelib/platform/wasm/qstdweb.cpp index 2bbf57953ba..e58447db1a0 100644 --- a/src/corelib/platform/wasm/qstdweb.cpp +++ b/src/corelib/platform/wasm/qstdweb.cpp @@ -151,7 +151,7 @@ void File::stream(uint32_t offset, uint32_t length, char *buffer, const std::fun return; } char *nextChunkBuffer = chunkBuffer + result.byteLength(); - fileReader->onLoad([=]() { (*chunkCompleted)(nextChunkBegin, nextChunkBuffer); }); + fileReader->onLoad([=](emscripten::val) { (*chunkCompleted)(nextChunkBegin, nextChunkBuffer); }); qstdweb::Blob blob = fileHandle.slice(nextChunkBegin, nextChunkEnd); fileReader->readAsArrayBuffer(blob); }; @@ -203,17 +203,17 @@ void FileReader::readAsArrayBuffer(const Blob &blob) const m_fileReader.call("readAsArrayBuffer", blob.m_blob); } -void FileReader::onLoad(const std::function &onLoad) +void FileReader::onLoad(const std::function &onLoad) { m_onLoad.reset(new EventCallback(m_fileReader, "load", onLoad)); } -void FileReader::onError(const std::function &onError) +void FileReader::onError(const std::function &onError) { m_onError.reset(new EventCallback(m_fileReader, "error", onError)); } -void FileReader::onAbort(const std::function &onAbort) +void FileReader::onAbort(const std::function &onAbort) { m_onAbort.reset(new EventCallback(m_fileReader, "abort", onAbort)); } @@ -305,11 +305,19 @@ emscripten::val Uint8Array::constructor_() // Registers a callback function for a named event on the given element. The event // name must be the name as returned by the Event.type property: e.g. "load", "error". -EventCallback::EventCallback(emscripten::val element, const std::string &name, const std::function &fn) -:m_fn(fn) +EventCallback::~EventCallback() { - element.set(contextPropertyName(name).c_str(), emscripten::val(intptr_t(this))); - element.set((std::string("on") + name).c_str(), emscripten::val::module_property("qtStdWebEventCallbackActivate")); + m_element.set(contextPropertyName(m_eventName).c_str(), emscripten::val::undefined()); + m_element.set((std::string("on") + m_eventName).c_str(), emscripten::val::undefined()); +} + +EventCallback::EventCallback(emscripten::val element, const std::string &name, const std::function &fn) + :m_element(element) + ,m_eventName(name) + ,m_fn(fn) +{ + m_element.set(contextPropertyName(m_eventName).c_str(), emscripten::val(intptr_t(this))); + m_element.set((std::string("on") + m_eventName).c_str(), emscripten::val::module_property("qtStdWebEventCallbackActivate")); } void EventCallback::activate(emscripten::val event) @@ -317,7 +325,7 @@ void EventCallback::activate(emscripten::val event) emscripten::val target = event["target"]; std::string eventName = event["type"].as(); EventCallback *that = reinterpret_cast(target[contextPropertyName(eventName).c_str()].as()); - that->m_fn(); + that->m_fn(event); } std::string EventCallback::contextPropertyName(const std::string &eventName) diff --git a/src/corelib/platform/wasm/qstdweb_p.h b/src/corelib/platform/wasm/qstdweb_p.h index 9d5e2418eca..cd3f0298602 100644 --- a/src/corelib/platform/wasm/qstdweb_p.h +++ b/src/corelib/platform/wasm/qstdweb_p.h @@ -129,9 +129,9 @@ namespace qstdweb { ArrayBuffer result() const; void readAsArrayBuffer(const Blob &blob) const; - void onLoad(const std::function &onLoad); - void onError(const std::function &onError); - void onAbort(const std::function &onAbort); + void onLoad(const std::function &onLoad); + void onError(const std::function &onError); + void onAbort(const std::function &onAbort); private: emscripten::val m_fileReader = emscripten::val::global("FileReader").new_(); @@ -166,11 +166,18 @@ namespace qstdweb { class EventCallback { public: - EventCallback(emscripten::val element, const std::string &name, const std::function &fn); + EventCallback() = default; + ~EventCallback(); + EventCallback(EventCallback const&) = delete; + EventCallback& operator=(EventCallback const&) = delete; + EventCallback(emscripten::val element, const std::string &name, + const std::function &fn); static void activate(emscripten::val event); private: static std::string contextPropertyName(const std::string &eventName); - std::function m_fn; + emscripten::val m_element = emscripten::val::undefined(); + std::string m_eventName; + std::function m_fn; }; } diff --git a/src/gui/platform/wasm/qwasmlocalfileaccess.cpp b/src/gui/platform/wasm/qwasmlocalfileaccess.cpp index 76e9c6c5191..cd1c26c72e3 100644 --- a/src/gui/platform/wasm/qwasmlocalfileaccess.cpp +++ b/src/gui/platform/wasm/qwasmlocalfileaccess.cpp @@ -96,7 +96,7 @@ void openFileDialog(const std::string &accept, FileSelectMode fileSelectMode, // Note: there is no event in case the user cancels the file dialog. static std::unique_ptr changeEvent; - auto callback = [=]() { filesSelected(qstdweb::FileList(input["files"])); }; + auto callback = [=](emscripten::val) { filesSelected(qstdweb::FileList(input["files"])); }; changeEvent.reset(new qstdweb::EventCallback(input, "change", callback)); // Activate file input