wasm: fix redundant string conversions between wasm and JavaScript
Avoid redundant conversions from UTF16 to UTF8 to UTF16 with help of new class QWasmString static methods: + QWasmString::fromQString to convert QString to js string using js Module.UTF16ToString + QWasmString::toQString to convert js string to QString using js Module.stringToUTF16 Fixed document.getElementById calls for cavasId with unicode characters. Change-Id: I3fc55bfeb6aeda75fa3acd85d22cea667b542f38 Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io> Reviewed-by: Lorn Potter <lorn.potter@gmail.com>
This commit is contained in:
parent
21ee3b17b7
commit
0a4c5b5119
@ -35,6 +35,7 @@ EMCC_COMMON_LFLAGS += \
|
||||
-s USE_WEBGL2=1 \
|
||||
-s NO_EXIT_RUNTIME=0 \
|
||||
-s ERROR_ON_UNDEFINED_SYMBOLS=1 \
|
||||
-s EXTRA_EXPORTED_RUNTIME_METHODS=[\"UTF16ToString\",\"stringToUTF16\"] \
|
||||
--bind
|
||||
|
||||
# The -s arguments can also be used with release builds,
|
||||
|
@ -29,6 +29,7 @@
|
||||
|
||||
#include "qwasmclipboard.h"
|
||||
#include "qwasmwindow.h"
|
||||
#include "qwasmstring.h"
|
||||
|
||||
#include <emscripten.h>
|
||||
#include <emscripten/html5.h>
|
||||
@ -40,22 +41,22 @@
|
||||
using namespace emscripten;
|
||||
|
||||
// there has got to be a better way...
|
||||
static QByteArray g_clipboardArray;
|
||||
static QByteArray g_clipboardFormat;
|
||||
static QString g_clipboardText;
|
||||
static QString g_clipboardFormat;
|
||||
|
||||
static val getClipboardData()
|
||||
{
|
||||
return val(g_clipboardArray.constData());
|
||||
return QWasmString::fromQString(g_clipboardText);
|
||||
}
|
||||
|
||||
static val getClipboardFormat()
|
||||
{
|
||||
return val(g_clipboardFormat.constData());
|
||||
return QWasmString::fromQString(g_clipboardFormat);
|
||||
}
|
||||
|
||||
static void pasteClipboardData(emscripten::val format, emscripten::val dataPtr)
|
||||
{
|
||||
QString formatString = QString::fromStdString(format.as<std::string>());
|
||||
QString formatString = QWasmString::toQString(format);
|
||||
QByteArray dataArray = QByteArray::fromStdString(dataPtr.as<std::string>());
|
||||
QMimeData *mMimeData = new QMimeData;
|
||||
mMimeData->setData(formatString, dataArray);
|
||||
@ -102,11 +103,10 @@ static void qClipboardPasteTo(val event)
|
||||
bool hasClipboardApi = QWasmIntegration::get()->getWasmClipboard()->hasClipboardApi;
|
||||
val clipdata = hasClipboardApi ?
|
||||
val::global("Module").call<val>("qtGetClipboardData") :
|
||||
event["clipboardData"].call<val>("getData", std::string("text"));
|
||||
event["clipboardData"].call<val>("getData", val("text"));
|
||||
|
||||
const std::string data = clipdata.as<std::string>();
|
||||
if (data.length() > 0) {
|
||||
QString qstr = QString::fromStdString(data);
|
||||
const QString qstr = QWasmString::toQString(clipdata);
|
||||
if (qstr.length() > 0) {
|
||||
QMimeData *mMimeData = new QMimeData;
|
||||
mMimeData->setText(qstr);
|
||||
QWasmClipboard::qWasmClipboardPaste(mMimeData);
|
||||
@ -133,7 +133,7 @@ QWasmClipboard::QWasmClipboard()
|
||||
|
||||
QWasmClipboard::~QWasmClipboard()
|
||||
{
|
||||
g_clipboardArray.clear();
|
||||
g_clipboardText.clear();
|
||||
g_clipboardFormat.clear();
|
||||
}
|
||||
|
||||
@ -148,11 +148,11 @@ QMimeData* QWasmClipboard::mimeData(QClipboard::Mode mode)
|
||||
void QWasmClipboard::setMimeData(QMimeData* mimeData, QClipboard::Mode mode)
|
||||
{
|
||||
if (mimeData->hasText()) {
|
||||
g_clipboardFormat = mimeData->formats().at(0).toUtf8();
|
||||
g_clipboardArray = mimeData->text().toUtf8();
|
||||
g_clipboardFormat = mimeData->formats().at(0);
|
||||
g_clipboardText = mimeData->text();
|
||||
} else if (mimeData->hasHtml()) {
|
||||
g_clipboardFormat =mimeData->formats().at(0).toUtf8();
|
||||
g_clipboardArray = mimeData->html().toUtf8();
|
||||
g_clipboardFormat = mimeData->formats().at(0);
|
||||
g_clipboardText = mimeData->html();
|
||||
}
|
||||
|
||||
QPlatformClipboard::setMimeData(mimeData, mode);
|
||||
@ -199,13 +199,13 @@ void QWasmClipboard::installEventHandlers(const QString &canvasId)
|
||||
|
||||
// Fallback path for browsers which do not support direct clipboard access
|
||||
val document = val::global("document");
|
||||
val canvas = document.call<val>("getElementById", val(canvasId.toUtf8().constData()));
|
||||
val canvas = document.call<val>("getElementById", QWasmString::fromQString(canvasId));
|
||||
|
||||
canvas.call<void>("addEventListener", std::string("cut"),
|
||||
canvas.call<void>("addEventListener", val("cut"),
|
||||
val::module_property("qtClipboardCutTo"));
|
||||
canvas.call<void>("addEventListener", std::string("copy"),
|
||||
canvas.call<void>("addEventListener", val("copy"),
|
||||
val::module_property("qtClipboardCopyTo"));
|
||||
canvas.call<void>("addEventListener", std::string("paste"),
|
||||
canvas.call<void>("addEventListener", val("paste"),
|
||||
val::module_property("qtClipboardPasteTo"));
|
||||
}
|
||||
|
||||
@ -226,6 +226,6 @@ void QWasmClipboard::writeTextToClipboard()
|
||||
val txt = module.call<val>("qtGetClipboardData");
|
||||
val format = module.call<val>("qtGetClipboardFormat");
|
||||
val navigator = val::global("navigator");
|
||||
navigator["clipboard"].call<void>("writeText", txt.as<std::string>());
|
||||
navigator["clipboard"].call<void>("writeText", txt);
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,7 @@
|
||||
|
||||
#include "qwasmcursor.h"
|
||||
#include "qwasmscreen.h"
|
||||
#include "qwasmstring.h"
|
||||
|
||||
#include <QtCore/qdebug.h>
|
||||
#include <QtGui/qwindow.h>
|
||||
@ -56,11 +57,11 @@ void QWasmCursor::changeCursor(QCursor *windowCursor, QWindow *window)
|
||||
htmlCursorName = "auto";
|
||||
|
||||
// Set cursor on the canvas
|
||||
QByteArray canvasId = QWasmScreen::get(screen)->canvasId().toUtf8();
|
||||
val jsCanvasId = QWasmString::fromQString(QWasmScreen::get(screen)->canvasId());
|
||||
val document = val::global("document");
|
||||
val canvas = document.call<val>("getElementById", val(canvasId.constData()));
|
||||
val canvas = document.call<val>("getElementById", jsCanvasId);
|
||||
val canvasStyle = canvas["style"];
|
||||
canvasStyle.set("cursor", emscripten::val(htmlCursorName.constData()));
|
||||
canvasStyle.set("cursor", val(htmlCursorName.constData()));
|
||||
}
|
||||
|
||||
QByteArray QWasmCursor::cursorShapeToHtml(Qt::CursorShape shape)
|
||||
|
@ -32,6 +32,7 @@
|
||||
#include "qwasmcompositor.h"
|
||||
#include "qwasmintegration.h"
|
||||
#include "qwasmclipboard.h"
|
||||
#include "qwasmstring.h"
|
||||
|
||||
#include <QtGui/qevent.h>
|
||||
#include <qpa/qwindowsysteminterface.h>
|
||||
@ -355,9 +356,10 @@ void QWasmEventTranslator::initEventHandlers()
|
||||
|
||||
if (emscripten::val::global("window")["safari"].isUndefined()) {
|
||||
val document = val::global("document");
|
||||
val canvas = document.call<val>("getElementById", val(canvasId));
|
||||
val jsCanvasId = QWasmString::fromQString(screen()->canvasId());
|
||||
val canvas = document.call<val>("getElementById", jsCanvasId);
|
||||
canvas.call<void>("addEventListener",
|
||||
std::string("wheel"),
|
||||
val("wheel"),
|
||||
val::module_property("qtMouseWheelEvent"));
|
||||
}
|
||||
}
|
||||
|
@ -36,6 +36,7 @@
|
||||
#include "qwasmclipboard.h"
|
||||
#include "qwasmservices.h"
|
||||
#include "qwasmoffscreensurface.h"
|
||||
#include "qwasmstring.h"
|
||||
|
||||
#include "qwasmwindow.h"
|
||||
#ifndef QT_NO_OPENGL
|
||||
@ -67,19 +68,19 @@ static void browserBeforeUnload(emscripten::val)
|
||||
|
||||
static void addCanvasElement(emscripten::val canvas)
|
||||
{
|
||||
QString canvasId = QString::fromStdString(canvas["id"].as<std::string>());
|
||||
QString canvasId = QWasmString::toQString(canvas["id"]);
|
||||
QWasmIntegration::get()->addScreen(canvasId);
|
||||
}
|
||||
|
||||
static void removeCanvasElement(emscripten::val canvas)
|
||||
{
|
||||
QString canvasId = QString::fromStdString(canvas["id"].as<std::string>());
|
||||
QString canvasId = QWasmString::toQString(canvas["id"]);
|
||||
QWasmIntegration::get()->removeScreen(canvasId);
|
||||
}
|
||||
|
||||
static void resizeCanvasElement(emscripten::val canvas)
|
||||
{
|
||||
QString canvasId = QString::fromStdString(canvas["id"].as<std::string>());
|
||||
QString canvasId = QWasmString::toQString(canvas["id"]);
|
||||
QWasmIntegration::get()->resizeScreen(canvasId);
|
||||
}
|
||||
|
||||
@ -115,11 +116,11 @@ QWasmIntegration::QWasmIntegration()
|
||||
int screenCount = qtCanvaseElements["length"].as<int>();
|
||||
for (int i = 0; i < screenCount; ++i) {
|
||||
emscripten::val canvas = qtCanvaseElements[i].as<emscripten::val>();
|
||||
QString canvasId = QString::fromStdString(canvas["id"].as<std::string>());
|
||||
QString canvasId = QWasmString::toQString(canvas["id"]);
|
||||
addScreen(canvasId);
|
||||
}
|
||||
} else if (!canvas.isUndefined()){
|
||||
QString canvasId = QString::fromStdString(canvas["id"].as<std::string>());
|
||||
QString canvasId = QWasmString::toQString(canvas["id"]);
|
||||
addScreen(canvasId);
|
||||
}
|
||||
|
||||
|
@ -32,6 +32,8 @@
|
||||
#include "qwasmeventtranslator.h"
|
||||
#include "qwasmcompositor.h"
|
||||
#include "qwasmintegration.h"
|
||||
#include "qwasmstring.h"
|
||||
|
||||
#include <emscripten/bind.h>
|
||||
#include <emscripten/val.h>
|
||||
|
||||
@ -184,7 +186,7 @@ void QWasmScreen::updateQScreenAndCanvasRenderSize()
|
||||
|
||||
QSizeF canvasSize = cssSize * devicePixelRatio();
|
||||
val document = val::global("document");
|
||||
val canvas = document.call<val>("getElementById", val(canvasId.constData()));
|
||||
val canvas = document.call<val>("getElementById", QWasmString::fromQString(m_canvasId));
|
||||
|
||||
canvas.set("width", canvasSize.width());
|
||||
canvas.set("height", canvasSize.height());
|
||||
|
@ -28,6 +28,8 @@
|
||||
****************************************************************************/
|
||||
|
||||
#include "qwasmservices.h"
|
||||
#include "qwasmstring.h"
|
||||
|
||||
#include <QtCore/QUrl>
|
||||
#include <QtCore/QDebug>
|
||||
|
||||
@ -37,8 +39,8 @@ QT_BEGIN_NAMESPACE
|
||||
|
||||
bool QWasmServices::openUrl(const QUrl &url)
|
||||
{
|
||||
QByteArray utf8Url = url.toString().toUtf8();
|
||||
emscripten::val::global("window").call<void>("open", emscripten::val(utf8Url.constData()), emscripten::val("_blank"));
|
||||
emscripten::val jsUrl = QWasmString::fromQString(url.toString());
|
||||
emscripten::val::global("window").call<void>("open", jsUrl, emscripten::val("_blank"));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
61
src/plugins/platforms/wasm/qwasmstring.cpp
Normal file
61
src/plugins/platforms/wasm/qwasmstring.cpp
Normal file
@ -0,0 +1,61 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2020 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of the plugins of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:GPL$
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3 or (at your option) any later version
|
||||
** approved by the KDE Free Qt Foundation. The licenses are as published by
|
||||
** the Free Software Foundation and appearing in the file LICENSE.GPL3
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "qwasmstring.h"
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
using namespace emscripten;
|
||||
|
||||
val QWasmString::fromQString(const QString &str)
|
||||
{
|
||||
static const val UTF16ToString(
|
||||
val::global("Module")["UTF16ToString"]);
|
||||
|
||||
auto ptr = quintptr(str.utf16());
|
||||
return UTF16ToString(val(ptr));
|
||||
}
|
||||
|
||||
QString QWasmString::toQString(const val &v)
|
||||
{
|
||||
QString result;
|
||||
if (!v.isString())
|
||||
return result;
|
||||
|
||||
static const val stringToUTF16(
|
||||
val::global("Module")["stringToUTF16"]);
|
||||
static const val length("length");
|
||||
|
||||
result.resize(v[length].as<int>());
|
||||
auto ptr = quintptr(result.utf16());
|
||||
stringToUTF16(v, val(ptr));
|
||||
return result;
|
||||
}
|
||||
|
||||
QT_END_NAMESPACE
|
45
src/plugins/platforms/wasm/qwasmstring.h
Normal file
45
src/plugins/platforms/wasm/qwasmstring.h
Normal file
@ -0,0 +1,45 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2020 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of the plugins of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:GPL$
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3 or (at your option) any later version
|
||||
** approved by the KDE Free Qt Foundation. The licenses are as published by
|
||||
** the Free Software Foundation and appearing in the file LICENSE.GPL3
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <qstring.h>
|
||||
|
||||
#include <emscripten/val.h>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
class QWasmString
|
||||
{
|
||||
public:
|
||||
static emscripten::val fromQString(const QString &str);
|
||||
static QString toQString(const emscripten::val &v);
|
||||
};
|
||||
QT_END_NAMESPACE
|
||||
|
@ -23,7 +23,8 @@ SOURCES = \
|
||||
qwasmtheme.cpp \
|
||||
qwasmclipboard.cpp \
|
||||
qwasmservices.cpp \
|
||||
qwasmoffscreensurface.cpp
|
||||
qwasmoffscreensurface.cpp \
|
||||
qwasmstring.cpp
|
||||
|
||||
HEADERS = \
|
||||
qwasmintegration.h \
|
||||
@ -39,7 +40,8 @@ HEADERS = \
|
||||
qwasmtheme.h \
|
||||
qwasmclipboard.h \
|
||||
qwasmservices.h \
|
||||
qwasmoffscreensurface.h
|
||||
qwasmoffscreensurface.h \
|
||||
qwasmstring.h
|
||||
|
||||
wasmfonts.files = \
|
||||
../../../3rdparty/wasm/Vera.ttf \
|
||||
|
Loading…
x
Reference in New Issue
Block a user