Skip to content

Commit

Permalink
QJSValue returning array of int
Browse files Browse the repository at this point in the history
  • Loading branch information
christophehenry committed Oct 23, 2024
1 parent 3b31cd6 commit 96cc536
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 48 deletions.
26 changes: 13 additions & 13 deletions res/controllers/engine-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,11 @@ declare namespace engine {
function softStart(deck: number, activate: boolean, factor?: number): void;

enum WellKnownCharsets {
LATIN_1,
Latin1,
ISO_8859_1,
LATIN_9,
Latin9,
ISO_8859_15,
UCS_2,
UCS2,
ISO_10646_UCS_2
}

Expand All @@ -318,15 +318,15 @@ declare namespace engine {
* Available charset names are listed here: http://www.iana.org/assignments/character-sets/character-sets.xhtml
* @param {string} targetCharset The charset to encode the string into.
* @param {string} value The string to encode
* @returns {ArrayBuffer | undefined}The converted String as an array of bytes or undefined if an error happened when performing conversion
* @returns {ArrayBuffer} The converted String as an array of bytes. Will return an empty buffer on conversion error.
*/
function convertCharset(targetCharset: string, value: string): ArrayBuffer | undefined

/**
* Version of {@link engine.convertCharset} to use with {@link engine.WellKnownCharsets}.
* @param {engine.WellKnownCharsets} targetCharset The charset to encode the string into.
* @param {string} value The string to encode
* @returns {ArrayBuffer | undefined}The converted String as an array of bytes or undefined if an error happened when performing conversion
*/
function convertCharset(targetCharset: WellKnownCharsets, value: string): ArrayBuffer | undefined
function convertCharset(targetCharset: string, value: string): ArrayBuffer

/**
* Version of {@link engine.convertCharset} to use with {@link engine.WellKnownCharsets}.
* @param {engine.WellKnownCharsets} targetCharset The charset to encode the string into.
* @param {string} value The string to encode
* @returns {ArrayBuffer} The converted String as an array of bytes. Will return an empty buffer on conversion error.
*/
function convertCharset(targetCharset: WellKnownCharsets, value: string): ArrayBuffer
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
#include <QTextCodec>
#else
#include <QStringEncoder>
#endif
#include <gsl/pointers>

Expand Down Expand Up @@ -1055,18 +1057,18 @@ QByteArray ControllerScriptInterfaceLegacy::convertCharset(
const ControllerScriptInterfaceLegacy::WellKnownCharsets targetCharset,
const QString& value) {
switch (targetCharset) {
case WellKnownCharsets::LATIN_1:
case WellKnownCharsets::Latin1:
case WellKnownCharsets::ISO_8859_1:
return this->convertCharset("ISO-8859-1", value);
case WellKnownCharsets::LATIN_9:
return convertCharset(QStringLiteral("ISO-8859-1"), value);
case WellKnownCharsets::Latin9:
case WellKnownCharsets::ISO_8859_15:
return this->convertCharset("ISO-8859-15", value);
case WellKnownCharsets::UCS_2:
return convertCharset(QStringLiteral("ISO-8859-15"), value);
case WellKnownCharsets::UCS2:
case WellKnownCharsets::ISO_10646_UCS_2:
return this->convertCharset("ISO-10646-UCS-2", value);
return convertCharset(QStringLiteral("ISO-10646-UCS-2"), value);
default:
m_pScriptEngineLegacy->throwJSError(QStringLiteral("Unknown charset specified"));
return nullptr;
return QByteArray();
}
}

Expand All @@ -1075,14 +1077,20 @@ QByteArray ControllerScriptInterfaceLegacy::convertCharset(
#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
auto* pCodec = QTextCodec::codecForName(targetCharset.toUtf8());
if (!pCodec) {
return nullptr;
m_pScriptEngineLegacy->logOrThrowError(QStringLiteral("Unable to open encoder"));
return QByteArray();
}
return pCodec->fromUnicode(value)
return pCodec->fromUnicode(value);
#else
#if QT_VERSION > QT_VERSION_CHECK(6, 8, 0)
QStringEncoder fromUtf16 = QStringEncoder(targetCharset.data());
#else
QStringEncoder fromUtf8 = QStringEncoder(targetCharset.toUtf8().data());
if (!fromUtf8.isValid()) {
return nullptr;
QStringEncoder fromUtf16 = QStringEncoder(targetCharset.toUtf8().data());
#endif
if (!fromUtf16.isValid()) {
m_pScriptEngineLegacy->logOrThrowError(QStringLiteral("Unable to open encoder"));
return QByteArray();
}
return fromUtf8(value);
return fromUtf16(value);
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ class ConfigKey;
class ControllerScriptInterfaceLegacy : public QObject {
Q_OBJECT
public:
enum WellKnownCharsets {
LATIN_1,
enum class WellKnownCharsets {
Latin1,
ISO_8859_1,
LATIN_9,
Latin9,
ISO_8859_15,
UCS_2,
UCS2,
ISO_10646_UCS_2

};
Q_ENUM(WellKnownCharsets)

Expand Down
41 changes: 24 additions & 17 deletions src/test/controllerscriptenginelegacy_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -747,27 +747,34 @@ TEST_F(ControllerScriptEngineLegacyTest, screenWillSentRawDataIfConfigured) {
#endif

TEST_F(ControllerScriptEngineLegacyTest, convertCharsetUndefinedOnUnknownCharset) {
ASSERT_TRUE(evaluate("engine.convertCharset('NULL', 'Hello!')").isUndefined());
logMessagesExpected.append(std::tuple<QtMsgType, QRegularExpression>(
QtMsgType::QtWarningMsg, QStringLiteral("Unable to open encoder")));
const auto result = evaluate("engine.convertCharset('NULL', 'Hello!')");
EXPECT_EQ(qjsvalue_cast<QByteArray>(result), QByteArray());
}

TEST_F(ControllerScriptEngineLegacyTest, convertCharsetCorrectValueWellKnown) {
const auto result = evaluate(R"Javascript(
Array.from(
new Uint8Array(
engine.convertCharset(engine.WellKnownCharsets.LATIN_9, "Hello!")))
.map(it => it.toString(16))
.join(",")
)Javascript");
ASSERT_QSTRING_EQ(result.toString(), "48,65,6c,6c,6f,21");
const auto result = evaluate(
"engine.convertCharset(engine.WellKnownCharsets.Latin9, 'Hello!')");

// int repr for 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x21
// aka ISO-8859-15 encoding for 'Hello!'
const char expected[] = {72, 101, 108, 108, 111, 33};
EXPECT_EQ(qjsvalue_cast<QByteArray>(result), QByteArray(expected, 6));
}

TEST_F(ControllerScriptEngineLegacyTest, convertCharsetCorrectValueStringCharset) {
const auto result = evaluate(R"Javascript(
Array.from(
new Uint8Array(
engine.convertCharset('ISO-8859-1', "Hello!")))
.map(it => it.toString(16))
.join(",")
)Javascript");
ASSERT_QSTRING_EQ(result.toString(), "48,65,6c,6c,6f,21");
const auto result = evaluate("`${engine.convertCharset('ISO-8859-15', 'Hello!')}`");
// int repr for 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x21
// aka ISO-8859-15 encoding for 'Hello!'
const char expected[] = {72, 101, 108, 108, 111, 33};
EXPECT_EQ(qjsvalue_cast<QByteArray>(result), QByteArray(expected, 6));
}

TEST_F(ControllerScriptEngineLegacyTest, convertCharsetUnsupportedChars) {
const auto result = evaluate("`${engine.convertCharset('ISO-8859-15', 'مايأ نامز')}`");
// int repr for 0x1A which is ISO-8859-15 for
// https://en.wikipedia.org/wiki/Substitute_character
const char expected[] = {26, 26, 26, 26, 32, 26, 26, 26, 26};
EXPECT_EQ(qjsvalue_cast<QByteArray>(result), QByteArray(expected, 9));
}

0 comments on commit 96cc536

Please sign in to comment.