Skip to content

Commit

Permalink
Added Base64 conversion functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ksuprynowicz committed Mar 14, 2024
1 parent ef4c99b commit c50a4f9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,11 @@ void ScriptManagerScriptingInterface::removeServerEntityScriptMessagesRequest(co
_manager->engine()->raiseException("Uuid must not be specified when removeServerEntityScriptMessagesRequest is invoked from entity script");
}
}

QString ScriptManagerScriptingInterface::btoa(const QByteArray &binary) {
return binary.toBase64();
}

QByteArray ScriptManagerScriptingInterface::atob(const QString &base64) {
return QByteArray::fromBase64(base64.toUtf8());
}
16 changes: 16 additions & 0 deletions libraries/script-engine/src/ScriptManagerScriptingInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,22 @@ class ScriptManagerScriptingInterface : public QObject {
Q_INVOKABLE void removeServerEntityScriptMessagesRequest();
Q_INVOKABLE void removeServerEntityScriptMessagesRequest(const QUuid& entityID);

/*@jsdoc
* This decodes Base64 string and returns contents as ArrayBuffer.
* @function Script.atob
* @param {String} base64 - String with Base64-encoded binary data.
* @returns {ArrayBuffer} Decoded binary data.
*/
Q_INVOKABLE QByteArray atob(const QString &base64);

/*@jsdoc
* This encodes ArrayBuffer and returns Base64-encoded string.
* @function Script.btoa
* @param {ArrayBuffer} binary - Data to be encoded.
* @returns {String} String with Base64-encoded binary data.
*/
Q_INVOKABLE QString btoa(const QByteArray &binary);

signals:

/*@jsdoc
Expand Down
2 changes: 1 addition & 1 deletion libraries/script-engine/src/v8/FastScriptValueUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ bool qBytearrayFromScriptValue(const ScriptValue& object, QByteArray &qByteArray
if(!v8Value->IsArrayBuffer()) {
return false;
}
v8::Local<v8::ArrayBuffer> arrayBuffer;
v8::Local<v8::ArrayBuffer> arrayBuffer = v8::Local<v8::ArrayBuffer>::Cast(v8Value);
qByteArray.resize(arrayBuffer->ByteLength());
memcpy(qByteArray.data(), arrayBuffer->Data(), arrayBuffer->ByteLength());
return true;
Expand Down

0 comments on commit c50a4f9

Please sign in to comment.